Loading...
Maximize total value by taking items greedily by highest value-to-weight ratio. Greedy works here because fractions are allowed, making the ratio choice provably optimal.
1procedure FRACTIONAL_KNAPSACK(items, W)2 sort items by value/weight in decreasing order3 total ← 04 for each item in items do5 if W = 0 then break6 if item.weight ≤ W then7 take 100% of item; W ← W − item.weight; total ← total + item.value8 else9 take fraction = W / item.weight; total ← total + item.value * fraction; W ← 010 end if11 end for12 return total13end procedure