Header Ads Widget

Fractional Knapsack

The fractional knapsack problem is also one of the techniques which are used to solve the knapsack problem. In fractional knapsack, the items are broken in order to maximize the profit. The problem in which we break the item is known as a Fractional knapsack problem.

This problem can be solved with the help of using two techniques:

  • Brute-force approach: The brute-force approach tries all the possible solutions with all the different fractions but it is a time-consuming approach.
  • Greedy approach: In Greedy approach, we calculate the ratio of profit/weight, and accordingly, we will select the item. The item with the highest ratio would be selected first.

There are basically three approaches to solve the problem:

  • The first approach is to select the item based on the maximum profit.
  • The second approach is to select the item based on the minimum weight.
  • The third approach is to calculate the ratio of profit/weight.


Consider the below example:

There are various methods are used to solve the Fractional Knapsack Problem such as follows:

  • Select the item based on the Maximum Profit.
  • Select the item based on the Minimum Weight.
  • Calculate the Ratio of Profit/Weight (Greedy Approach).

Here, we used the Ratio of Profit/Weight (Greedy Approach) because it is the best approach among all.


Greedy Approach (Ratio of Profit/Weight)

The given data -

p = (p1, p2, p3, p4, p5, p6) = (18, 5, 9, 10, 12, 7)

w = (w1, w2, w3, w4, w5, w6) = (7, 2, 3, 5, 3, 2)

n = Number of Objects = 6

M = Knapsack Capcacity = 13

Solution -

Step 1 - Calculate the Profit/ Weight Ratio.

ObjectProfitWeightProfit / WeightRatio
1187(p1 / w1) = 18 / 72.57
252(p2 / w2)= 5 / 22.5
393(p3 / w3)= 9 / 33
4105(p4 / w4)= 10 / 52
5123(p5 / w5)= 12 / 34
672(p6 / w6)= 7 / 23.5

Step 2 - Sort all the objects in decreasing order of their Property / Weight Ratio.

Therefore, here sequence of Object as follows:

ObjectRatioProfitWeight
54123
63.572
3393
12.57187
22.552
42105

Step 3 - Start filling the knapsack by putting the object into it one by one.

Knapsack WeightObjects in KnapsackProfit
13NULL0
13 – 3 = 10512
10 – 2 = 85, 612 + 7 = 19
8 – 3 = 55, 6, 319 + 9 = 28

Now,

  • See the next object 1 cannot be chosen because the remaining capacity of the knapsack is less than the weight of object 1.
  • The knapsack weight left to be filled is 5 kg but object 1 weight is 7 kg.
  • But, in the fractional knapsack problem, even the fraction of any object can be taken.
  • Therefore, here knapsack will contain the following objects as follows:

Where,

5 is the remaining capacity of the knapsack.

7 is the weight of object 1.

  • Now, the capacity of the Knapsack is equal to the selected objects.
  • Hence, no more objects can be selected.

Step 4 - Based on this calculate the Total Profit and Total Weight of the Knapsack.

Total Profit of Knapsack =

Total Profit of Knapsack = 40.85

Total Weight of Knapsack =

Total Weight of Knapsack = 13 (for the selected objects 5, 6, 3, & 1)

This also Proves that the given capacity of the Knapsack is equal to the selected objects.

M = 13 = Total Weight of Knapsack = 13 (for the selected objects 5, 6, 3, & 1)

Therefore, no more objects can be selected.

Post a Comment

0 Comments