Fractional Knapsack ProblemThe 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:
There are basically three approaches to solve the problem:
Consider the below example: Objects: 1 2 3 4 5 6 7 Profit (P): 10 15 7 8 9 4 Weight(w): 1 3 5 4 1 3 2 W (Weight of the knapsack): 15 n (no of items): 7 First approach:First approach:
The total profit would be equal to (15 + 10 + 9 + 8 + 5.25) = 47.25 Second approach:The second approach is to select the item based on the minimum weight.
In this case, the total profit would be equal to (5 + 7 + 4 + 10 + 9 + 7 + 3) = 46 Third approach:In the third approach, we will calculate the ratio of profit/weight. Objects: 1 2 3 4 5 6 7 Profit (P): 5 10 15 7 8 9 4 Weight(w): 1 3 5 4 1 3 2 In this case, we first calculate the profit/weight ratio. Object 1: 5/1 = 5 Object 2: 10/3 = 3. 33 Object 3: 15/5 = 3 Object 4: 7/4 = 1.7 Object 5: 8/1 = 8 Object 6: 9/3 = 3 Object 7: 4/2 = 2 P:w: 5 3.3 3 1.7 8 3 2 In this approach, we will select the objects based on the maximum profit/weight ratio. Since the P/W of object 5 is maximum so we select object 5.
After object 5, object 1 has the maximum profit/weight ratio, i.e., 5. So, we select object 1 shown in the below table:
After object 1, object 2 has the maximum profit/weight ratio, i.e., 3.3. So, we select object 2 having profit/weight ratio as 3.3.
After object 2, object 3 has the maximum profit/weight ratio, i.e., 3. So, we select object 3 having profit/weight ratio as 3.
After object 3, object 6 has the maximum profit/weight ratio, i.e., 3. So we select object 6 having profit/weight ratio as 3.
After object 6, object 7 has the maximum profit/weight ratio, i.e., 2. So we select object 7 having profit/weight ratio as 2.
As we can observe in the above table that the remaining weight is zero which means that the knapsack is full. We cannot add more objects in the knapsack. Therefore, the total profit would be equal to (8 + 5 + 10 + 15 + 9 + 4), i.e., 51. In the first approach, the maximum profit is 47.25. The maximum profit in the second approach is 46. The maximum profit in the third approach is 51. Therefore, we can say that the third approach, i.e., maximum profit/weight ratio is the best approach among all the approaches.
Next TopicHuffman Codes
|