0% found this document useful (0 votes)
5 views

exp 5

The document outlines the implementation of the Fractional Knapsack Problem using a greedy approach to maximize profit. It compares three methods: selecting items based on maximum profit, minimum weight, and profit/weight ratio, concluding that the third method yields the highest profit. The experiment demonstrates the effectiveness of the greedy approach in solving the fractional knapsack problem.

Uploaded by

jatin.v.yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

exp 5

The document outlines the implementation of the Fractional Knapsack Problem using a greedy approach to maximize profit. It compares three methods: selecting items based on maximum profit, minimum weight, and profit/weight ratio, concluding that the third method yields the highest profit. The experiment demonstrates the effectiveness of the greedy approach in solving the fractional knapsack problem.

Uploaded by

jatin.v.yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT NO 5

Title: To implement Fractional Knapsack Problem using Greedy Approach.

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.

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.

Algorithm:

Consider the below example:


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.
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.

Conclusion: - Thus we have implemented fractional knapsack using a greedy approach.

You might also like