This document describes the fractional knapsack problem and provides an example of solving it using the greedy approach. It introduces the fractional knapsack problem, explains that the greedy approach is to calculate the value/weight ratio for each item and sort by that ratio. It then provides an example problem with 5 items, shows sorting the items by ratio, and calculates the total value of items included in the knapsack by taking full items and a fractional part of the last item to fully use the 20kg capacity.
This document describes the fractional knapsack problem and provides an example of solving it using the greedy approach. It introduces the fractional knapsack problem, explains that the greedy approach is to calculate the value/weight ratio for each item and sort by that ratio. It then provides an example problem with 5 items, shows sorting the items by ratio, and calculates the total value of items included in the knapsack by taking full items and a fractional part of the last item to fully use the 20kg capacity.
This document describes the fractional knapsack problem and provides an example of solving it using the greedy approach. It introduces the fractional knapsack problem, explains that the greedy approach is to calculate the value/weight ratio for each item and sort by that ratio. It then provides an example problem with 5 items, shows sorting the items by ratio, and calculates the total value of items included in the knapsack by taking full items and a fractional part of the last item to fully use the 20kg capacity.
Branch: IT Branch: CSE Roll no: 202 Roll no: 203 Enrollment No: 21002170210046 Enrollment No: 21002171210177
Name: Jasani Neel A.
Branch: CE Roll no: 204 Enrollment No: 21002170110048 INTRODUCTION Given the weights and values of N items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In Fractional Knapsack, we can break items for maximizing the total value of the knapsack. An efficient solution is to use the Greedy approach. The basic idea of the greedy approach is to calculate the ratio value/weight for each item and sort the item on the basis of this ratio. Then take the item with the highest ratio and add them until we can’t add the next item as a whole and at the end add the next item as much as we can. Which will always be the optimal solution to this problem. PROBLEM SOLUTION STEP 2 Sort all the items in decreasing order of their value / weight ratio:- I1 I2 I5 I4 I3 (6) (4) (3.6) (3.5) (3) COMPUTE TOTAL VALUE Knapsack weight left to be filled is 20 kg but item-4 has a weight of 22 kg. Since in fractional knapsack problem, even the fraction of any item can be taken. So, knapsack will contain the following items:- < I1 , I2 , I5 , (20/22) I4 >
Total cost of the knapsack
= 160 + (20/22) x 77 = 160 + 70 = 230 units ALGORITHM Calculate the ratio(value/weight) for each item. Sort all the items in decreasing order of the ratio. Initialize res =0, curr_cap = given_cap. Do the following for every item “i” in the sorted order: If the weight of the current item is less than or equal to the remaining capacity then add the value of that item into the result else add the current item as much as we can and break out of the loop Return res Here, Time Complexity will be O(nlogn) and Space Complexity will be O(n). OUTPUT THANK YOU