Sub topic 9
Fractional Knapsack: Best Value per Weight
This chapter assumes you know Python sorting with key= functions and the exchange argument from the earlier proof chapter. You will use that argument here to justify a greedy rule.
The Problem
You have a knapsack that holds at most W units of weight. Each item has a value and a weight. In the fractional version you may take any fraction of an item, so taking half of an item gives half its value and adds half its weight. The goal is to maximize total value without going over W.
Think of items as bulk goods like gold dust, rice or spices, rather than whole objects.
The Greedy Rule
Compute each item's value density, which is value divided by weight. Sort the items from highest density to lowest. Take whole items in that order until the next one does not fit. Then take the fraction of that item that fills the remaining space.
Implementation
def fractional_knapsack(items, capacity):
total = 0.0
for value, weight in sorted(items, key=lambda it: it[0] / it[1], reverse=True):
if capacity <= 0:
break
take = min(weight, capacity)
total += value * take / weight
capacity -= take
return total
print(fractional_knapsack([(60, 10), (100, 20), (120, 30)], 50)) # 240.0Here is the trace for capacity 50:
| Item (value, weight) | Density | Taken | Value added | Capacity left |
|---|---|---|---|---|
| (60, 10) | 6 | 10 | 60 | 40 |
| (100, 20) | 5 | 20 | 100 | 20 |
| (120, 30) | 4 | 20 | 80 | 0 |
Sorting dominates the cost, so the running time is O(n log n).
Why It Is Optimal: An Exchange Argument
Take any optimal solution that differs from the greedy one. Somewhere it must carry some weight of a lower-density item j while leaving part of a higher-density item i unused. Swap a small amount ε of weight: remove ε of j and add ε of i. The total weight stays the same, and the value changes by ε(dᵢ − dⱼ) ≥ 0. Repeat this swap and the optimal solution turns into the greedy one without ever losing value, so greedy is optimal.
The argument depends on being able to choose a small ε. Divisibility is the property that makes the swap legal.
Common Errors
Other mistakes to watch for:
- Sorting by value or by weight alone. A heavy, valuable item can have poor density. Always sort by the ratio.
- Forgetting the partial item. If you stop at the last whole item, you leave value unused.
- Integer division. In Python 3,
/is safe. In other languages, cast to a floating-point type before computing densities. - Zero-weight items. These would cause a division by zero. Take them unconditionally before the main loop, since they cost no capacity.
Exercise
Capacity is 10. The items, as (value, weight), are (18, 6), (30, 5) and (20, 4). What does the greedy algorithm return, and in what order does it take the items?
Show answer
The densities are 3, 6 and 5. Greedy takes all of (30, 5), leaving 5 capacity. It then takes all of (20, 4), leaving 1. Finally it takes 1/6 of (18, 6), adding 3. The total is 30 + 20 + 3 = 53.0.
Exercise
Give a one-sentence reason why the exchange argument proves greedy optimal for fractional knapsack but not for 0/1 knapsack.
Show answer
The proof swaps an arbitrarily small amount of weight from a lower-density item to a higher-density item, and that swap is only possible when items can be split.