exp 5
exp 5
Aim: - Implement Fractional Knapsack Algorithm to find maximum profit using greedy approach and
analyze its complexity.
Lab Outcomes : CSL401.2: Ability to implement the algorithm using Greedy approach.
Theory:-
The fractional knapsack problem is also one of the techniques which are used to solve the knapsack problem.
In a 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.
● 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.
Algorithm:
Second approach:
The second approach is to select the item based on the minimum weight.
Third Approach:
In the third approach, we will calculate the ratio of profit/weight.
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.