Sub topic 4
A Counterexample: Greedy Fails for 0/1 Knapsack
This chapter assumes you have read the fractional knapsack chapter, and that you can read basic Python and follow a brute-force search over subsets. In the fractional chapter, sorting by value per unit of weight gave the optimal answer. The obvious next step is to use the same rule when items cannot be split. That rule fails, and one small example is enough to show it.
The instance
We use the three items from the fractional chapter and a capacity of 50.
| Item | Value | Weight | Value/weight |
|---|---|---|---|
| A | 60 | 10 | 6.0 |
| B | 100 | 20 | 5.0 |
| C | 120 | 30 | 4.0 |
Greedy by ratio works through the items in the order A, B, C:
- It takes A. The weight used is 10, so 40 remains.
- It takes B. The weight used is 30, so 20 remains.
- C weighs 30 and does not fit, and greedy is not allowed to take a fraction of it.
Greedy finishes with a value of 160 and 20 units of capacity left empty. Taking B and C instead fills the knapsack exactly and gives 220.
Checking it in code
The script below compares greedy against an exhaustive search over all subsets.
from itertools import combinations
def greedy_01(items, capacity):
total = 0
for value, weight in sorted(items, key=lambda it: it[0] / it[1], reverse=True):
if weight <= capacity:
total += value
capacity -= weight
return total
def best_subset(items, capacity):
best = 0
for r in range(len(items) + 1):
for combo in combinations(items, r):
if sum(w for _, w in combo) <= capacity:
best = max(best, sum(v for v, _ in combo))
return best
items = [(60, 10), (100, 20), (120, 30)]
print(greedy_01(items, 50)) # 160
print(best_subset(items, 50)) # 220Why the fractional argument breaks
The fractional proof is an exchange argument. Suppose some solution uses part of a low-ratio item while a higher-ratio item still has room. You can swap weight from the worse item to the better one, and the total value never goes down. That swap works because you can move any amount of weight, including a fraction of an item. In the 0/1 problem you can only move whole items. A whole item may not fit into the space left over, so the swap is not always possible. Whatever capacity greedy cannot fill earns nothing, and a choice that looks best on its own can block a better combination later.
Other greedy rules fail too
Changing the sort key does not fix the problem. Each rule below has an instance where it loses:
| Greedy rule | Counterexample (value, weight), capacity | Greedy | Optimal |
|---|---|---|---|
| Highest ratio | (60,10), (100,20), (120,30), cap 50 | 160 | 220 |
| Highest value | (6,10), (5,5), (5,5), cap 10 | 6 | 10 |
| Lightest first | (1,1), (10,10), cap 10 | 1 | 10 |
The fix is to stop committing to a choice early. For each item, compare the best result when you take it with the best result when you skip it. The next chapter turns that comparison into a recurrence.
Exercise
Find a two-item instance where greedy by ratio fails.
Show answer
Use items (2, 1) and (9, 10) with capacity 10. Greedy takes (2, 1) first because its ratio is 2.0, against 0.9 for the other item. That leaves 9 units of capacity, so (9, 10) no longer fits and greedy ends with 2. Taking (9, 10) alone gives 9.
Exercise
If every item has the same weight, is greedy by ratio optimal for 0/1 knapsack? Explain.
Show answer
Yes. When all weights are equal, sorting by ratio gives the same order as sorting by value. The capacity limits you to a fixed number k of items, so the best choice is the k items with the highest values, and those are exactly the ones greedy picks.