Sub topic 1
What the Knapsack Problem Asks
Prerequisites
This chapter assumes you know basic Python (functions, loops, lists and tuples), recursion, and Big-O notation.
The Problem and Its Variants
You have n items. Item i has a value v_i and a weight w_i. Your knapsack holds a total weight of at most W. The goal is to pick items so that the total value is as high as possible without the total weight going over W.
| Variant | Rule for each item |
|---|---|
| 0/1 | Take it once or not at all |
| Fractional | Take any fraction from 0 to 1 |
| Unbounded | Take any number of copies |
| Bounded | Take up to `k_i` copies |
Trying Every Subset
In the 0/1 variant, every subset of the items is a candidate answer. With n items there are 2^n subsets, and checking one costs O(n), so brute force takes O(n·2^n) time. At 40 items that is already about a trillion subsets.
Fractional Knapsack: Greedy Works
If you can split items, sort them by value per unit of weight and take the best ones first. When an item does not fit, take the fraction that does.
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.0This is optimal because of an exchange argument. Suppose an optimal solution holds some weight of a lower-ratio item while room remains in a higher-ratio item. Swapping that weight for the higher-ratio item never lowers the value. Splitting means the knapsack is always filled exactly.
Greedy Fails for 0/1
Take the same items with W = 50. The ratios are 6, 5 and 4. Greedy takes the 10 and 20 weight items (value 160). The 30 weight item then does not fit. Taking the 20 and 30 weight items gives 220. Greedy failed because it could not take part of an item, so it left 20 units of capacity unused.
Take It or Leave It: The Recurrence
Let best(i, c) be the highest value you can get from items i..n-1 with capacity c. An optimal solution either skips item i or takes it. In both cases, what remains must be an optimal solution to a smaller subproblem. This property is called optimal substructure.
best(n, c) = 0- If
w_i > c:best(i, c) = best(i+1, c) - Otherwise:
best(i, c) = max(best(i+1, c), v_i + best(i+1, c - w_i))
The answer is best(0, W). The arguments i and c can take only (n+1)(W+1) distinct values, and the next chapter uses this to compute the recurrence efficiently.
Exercise
Items (value, weight) are (10, 1), (90, 10), and W = 10. What does greedy by ratio return for 0/1 knapsack, and what is optimal?
Show answer
The ratios are 10 and 9, so greedy takes (10, 1) first. The remaining capacity is 9, so (90, 10) no longer fits and greedy returns 10. The optimal choice is (90, 10) alone, for a value of 90.
Exercise
Using the recurrence, what is best(1, 20) for the items [(60,10), (100,20), (120,30)]?
Show answer
Item 1 has weight 20, which fits in capacity 20. The recurrence gives max(best(2, 20), 100 + best(2, 0)). best(2, 20) = 0 because item 2 weighs 30 and does not fit, and best(2, 0) = 0. So best(1, 20) = 100.