Sub topic 3
Fractional Knapsack: When Greedy Is Optimal
This chapter assumes you can read Python functions and loops, know what sorted with a key does, and are comfortable with Big-O notation. It also assumes the problem statement from the first chapter: items have a value and a weight, and the knapsack has a capacity.
The Fractional Rule
In the fractional knapsack you may take any fraction of an item, from 0 to 1. If you take a fraction f of an item, you get f times its value and use f times its weight. Gold dust, grain or bandwidth fit this model, because you can cut any of them into pieces of any size.
When you can split items, the only number that matters is value density: value divided by weight. Each unit of capacity should go to the densest material still available.
The Greedy Algorithm
- Compute value / weight for every item.
- Sort items by that ratio, highest first.
- Take each item whole while it fits.
- When an item no longer fits, take the fraction that fills the remaining capacity, then stop.
def fractional_knapsack(items, capacity):
# items: list of (value, weight) with weight > 0
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.0Tracing the Example
| Item (value, weight) | Ratio | Taken | Value gained | Capacity left |
|---|---|---|---|---|
| (60, 10) | 6 | all 10 | 60 | 40 |
| (100, 20) | 5 | all 20 | 100 | 20 |
| (120, 30) | 4 | 20 of 30 | 80 | 0 |
The total is 240. Sorting dominates the cost, so the algorithm runs in O(n log n) time. If you use a linear-time selection method in place of a full sort, it runs in O(n).
Why Greedy Is Optimal
The proof uses an exchange argument. Take any optimal solution that differs from the greedy one. Somewhere it must hold less of a higher-ratio item A than greedy does, and some amount of a lower-ratio item B. Remove a small weight d of B and add the same weight d of A. The total weight stays the same. The value changes by d × (ratio of A − ratio of B), which is zero or more. Repeat this swap until the solution matches greedy, and the value never goes down along the way. So greedy is at least as good as any optimal solution, which means greedy is optimal.
Every swap depends on moving an arbitrarily small amount of weight. That is only possible because items can be split.
Exercises
Exercise
Items are (40, 20), (30, 10), (50, 25) as (value, weight) and the capacity is 40. What total value does fractional greedy return, and how much of each item does it take?
Show answer
The ratios are 2, 3 and 2. Greedy takes all of (30, 10) for 30, leaving 30 capacity. It then takes all of (40, 20) for 40, leaving 10. Finally it takes 10/25 of (50, 25) for 20. The total is 90. Breaking the tie between the two ratio-2 items the other way also gives 90.
Exercise
Which step of the exchange argument fails if items cannot be split?
Show answer
The swap of a small weight d from item B to item A. With whole items you can only swap entire weights, and removing B whole may free a different amount of weight than A needs, so the swap may not fit or may waste capacity.