Sub topic 3
Unbounded Knapsack and Beyond
This chapter assumes you know the 0/1 one-array DP from the previous chapter, where the capacity loop runs backwards.
Unbounded Knapsack: Reusing Items
In unbounded knapsack, you may take each item any number of times. The DP stays the same except that the capacity loop now runs forwards. When dp[c - w] is read, it may already include the current item, so the item can be used again.
def knapsack_unbounded(values, weights, capacity):
dp = [0] * (capacity + 1)
for v, w in zip(values, weights):
for c in range(w, capacity + 1):
dp[c] = max(dp[c], dp[c - w] + v)
return dp[capacity]
print(knapsack_unbounded([60, 100, 120], [10, 20, 30], 50)) # 300A common bug is writing the loop in the wrong direction. Backwards gives 0/1 knapsack and forwards gives unbounded. Both versions run without error, so the mistake only shows up as a wrong answer.
Bounded Knapsack: Binary Splitting
When an item has k copies, split it into bundles of 1, 2, 4, and so on, plus a remainder. Sums of these bundles can form every count from 0 to k. Run the 0/1 DP on the bundles. This creates O(log k) items for each original item, instead of k.
def split_bounded(values, weights, counts):
items = []
for v, w, k in zip(values, weights, counts):
p = 1
while k > 0:
take = min(p, k)
items.append((v * take, w * take))
k -= take
p *= 2
return items
print(split_bounded([10], [3], [5])) # [(10, 3), (20, 6), (20, 6)]Pseudo-Polynomial Time and NP-Hardness
The weight-indexed DP takes O(nW) time. The input stores W in only about log W bits, so the running time grows exponentially in the input size. An algorithm like this is called pseudo-polynomial. 0/1 knapsack is NP-hard, and the DP does not contradict that, because it is fast only when W is small.
Alternatives for Awkward Instances
| Situation | Technique | Cost |
|---|---|---|
| Small W | Weight-indexed DP | O(nW) |
| Huge W, small total value V | Value-indexed DP: `dp[t]` = minimum weight that reaches value exactly `t` | O(nV) |
| Hard instance, need the exact optimum | Branch and bound: prune a branch when its fractional-greedy upper bound cannot beat the best solution found so far | Exponential worst case, often fast in practice |
| n ≤ about 40, any W | Meet in the middle: list the subsets of each half, sort one list, and match the halves with binary search | O(2^(n/2) · n) |
| Near-optimal is enough | FPTAS: scale each value down by εv_max/n, then run the value-indexed DP | O(n³/ε), result ≥ (1−ε)·OPT |
Beyond One Constraint
Multidimensional knapsack adds more limits, such as weight and volume. The DP table then gains one dimension per limit, and its size grows multiplicatively. The FPTAS no longer applies, because the problem has no FPTAS unless P = NP. Related problems include subset sum, which is knapsack where each value equals its weight, and multiple knapsack, which has several bins.
Exercise
Weights are up to 10^9, n = 100, and every value is at most 50. Which method fits, and roughly how large is its table?
Show answer
The value-indexed DP fits, because W is far too large to index by. The total value is at most 100 × 50 = 5000, so the table has about 5001 entries, and the run takes about 100 × 5000 = 500,000 updates.
Exercise
In the 0/1 one-array DP, what happens if you change the capacity loop to run forwards?
Show answer
It becomes the unbounded knapsack, because an item can then be used more than once. On the sample input it returns 300 instead of 220.