Sub topic 9
Using One Array: Why the Loop Runs Backwards
This chapter assumes you already know the bottom-up table dp[i][c] from the earlier chapter, Python range with a negative step, and Big-O notation for space. The goal is to cut memory from O(nW) to O(W) without changing the answer.
Only one row is ever read
Look at the update rule of the two-dimensional table:
dp[i][c] = max(dp[i-1][c], dp[i-1][c-w] + v)
Row i reads only from row i-1. Older rows are never used again. So you can keep a single array dp of length W + 1 and overwrite it once per item. Before item i is processed, dp holds row i-1. Afterwards it holds row i.
The difficulty is that the new value of dp[c] depends on dp[c-w], and that cell must still hold the old row's value when you read it.
Why the direction matters
If c increases, dp[c-w] has already been updated for the current item before you reach dp[c]. Reading it means using the current item a second time. If c decreases, dp[c-w] sits at a smaller index that has not been visited yet, so it still holds the value from the previous row.
def knapsack_01_1d(values, weights, capacity):
dp = [0] * (capacity + 1)
for v, w in zip(values, weights):
for c in range(capacity, w - 1, -1):
dp[c] = max(dp[c], dp[c - w] + v)
return dp[capacity]
print(knapsack_01_1d([60, 100, 120], [10, 20, 30], 50)) # 220The loop stops at w because capacities below w cannot take the item. Those cells keep their old value, which matches the "skip" branch of the recurrence.
Tracing the array
This table shows dp at capacities 0, 10, ..., 50 after each item is processed.
| After item | 0 | 10 | 20 | 30 | 40 | 50 |
|---|---|---|---|---|---|---|
| (60, 10) | 0 | 60 | 60 | 60 | 60 | 60 |
| (100, 20) | 0 | 60 | 100 | 160 | 160 | 160 |
| (120, 30) | 0 | 60 | 100 | 160 | 180 | 220 |
In the third row, dp[50] reads dp[20] = 100 from the second row and gets 220. That read is correct because index 20 had not yet been overwritten.
The common bug: a forward loop
def knapsack_wrong(values, weights, capacity):
dp = [0] * (capacity + 1)
for v, w in zip(values, weights):
for c in range(w, capacity + 1): # forward: wrong for 0/1
dp[c] = max(dp[c], dp[c - w] + v)
return dp[capacity]
print(knapsack_wrong([60], [10], 20)) # 120: the single item was taken twice
print(knapsack_01_1d([60], [10], 20)) # 60What you give up
| Version | Time | Space | Can recover chosen items? |
|---|---|---|---|
| 2D table | O(nW) | O(nW) | Yes, by walking back through rows |
| 1D array | O(nW) | O(W) | Not directly; earlier rows are overwritten |
Use the one-array version when you need only the optimal value, or when n * W cells would not fit in memory.
Exercise
With values [10, 15] and weights [2, 3] and capacity 5, run knapsack011d by hand and give dp after each item. What would a forward loop return for the same input?
Show answer
After item (10, 2): dp = [0, 0, 10, 10, 10, 10]. After item (15, 3), going backwards: dp[5] = max(10, dp[2] + 15) = 25, dp[4] = max(10, dp[1] + 15) = 15, dp[3] = max(10, dp[0] + 15) = 15, so dp = [0, 0, 10, 15, 15, 25] and the answer is 25. With a forward loop, the first item alone gives dp[4] = 20, and the second item then gives dp[5] = max(20, dp[2] + 15) = 25, so the result is still 25. The bug is real but hidden here: dp[4] = 20 uses item 0 twice. Testing with capacity 4 exposes it, giving 20 instead of the correct 15.