Sub topic 10
Unbounded Knapsack: Reusing Items
This chapter assumes you know the 0/1 recurrence, the bottom-up table, and the one-array version from the previous chapter. You should also be comfortable reading nested Python loops.
The problem
In the unbounded knapsack, every item comes in unlimited supply. You pick a non-negative whole number of copies of each item. You want the largest total value whose total weight is at most the capacity W. Some common examples are cutting stock from standard lengths, making change from coin denominations, and filling a truck with standard crates.
The 0/1 recurrence allows each item at most once. Here, taking item i leaves the same choices available as before, so the recurrence changes in one place:
- 0/1:
dp[i][c] = max(dp[i-1][c], dp[i-1][c-w] + v) - Unbounded:
dp[i][c] = max(dp[i-1][c], dp[i][c-w] + v)
The second term stays on row i. After you take a copy of item i, you are still allowed to take another one.
A capacity-only formulation
You can drop the item index completely. Let dp[c] be the best value achievable with capacity at most c. Any optimal packing is either empty, or it contains some item i. Removing one copy of that item leaves a packing that fits in c - w_i. That gives this recurrence:
dp[c] = max(0, max over items with w_i <= c of dp[c - w_i] + v_i)
The version below also records which item achieved the best value at each capacity. You can then recover how many copies of each item were used.
def unbounded_with_counts(values, weights, capacity):
dp = [0] * (capacity + 1)
pick = [-1] * (capacity + 1) # item used last to reach dp[c]
for c in range(1, capacity + 1):
for i, (v, w) in enumerate(zip(values, weights)):
if w <= c and dp[c - w] + v > dp[c]:
dp[c] = dp[c - w] + v
pick[c] = i
counts = [0] * len(values)
c = capacity
while pick[c] != -1:
i = pick[c]
counts[i] += 1
c -= weights[i]
return dp[capacity], counts
print(unbounded_with_counts([60, 100, 120], [10, 20, 30], 50)) # (300, [5, 0, 0])The answer for this input is five copies of the 60-value item, for a total value of 300. The 0/1 version of the same input reached only 220.
The one-array view
The previous chapter used one array with items in the outer loop. In that version, the only change needed for the unbounded variant is to run the capacity loop forwards:
for c in range(w, capacity + 1):
dp[c] = max(dp[c], dp[c - w] + v)When the loop reaches dp[c], the value in dp[c - w] has already been updated in this pass, and it may already include item i. That is exactly what reuse requires. Both formulations run in O(nW) time and O(W) space.
Common errors
| Mistake | Symptom |
|---|---|
| Capacity loop runs backwards | You silently get the 0/1 answer (220 instead of 300 above) |
| An item with weight 0 and positive value | The true optimum is unbounded, and the forward loop reads the cell it is writing to. Reject such items or handle them separately. |
| Swapping loop order in a counting problem | Counting ways (for example, coin change) with capacity outside and items inside counts ordered sequences, not combinations |
Exercise
Items have weights [3, 4] and values [4, 5], with unlimited copies. What is the best value for capacity 10, and which copies achieve it?
Show answer
The best value is 13, from two copies of the weight-3 item and one copy of the weight-4 item (weight 10). The alternatives are worse: three weight-3 copies give 12, and two weight-4 copies give 10.
Exercise
You run knapsack_unbounded from the previous chapter and it returns 220 on the example input instead of 300. What is the most likely bug?
Show answer
The capacity loop runs from capacity down to w. That backward order prevents reuse and turns the code into the 0/1 algorithm.