0/1 knapsack
medium 22 minPack items with weights and values into a bag of capacity W to get the most value, each item taken whole or not at all.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What does "0/1" mean in 0/1 knapsack?
What is the time complexity of the DP solution?
In the space-optimised version, why traverse capacity backwards?
What does dp[i][w] represent?
Capacity 7, items (weight, value): (1, 1), (3, 4), (4, 5), (5, 7). What is the best value?
On that same trip, what does greedy by value per weight return?
Item 2 has weight 3 and value 4, and row 1 is [0, 1, 1, 1, 1, 1, 1, 1]. What is dp[2][4]?
Walking back, dp[4][7] = 9 and dp[3][7] = 9. What do you conclude?
One item (weight 1, value 1), W = 7, and the 1D loop runs w from 1 up to 7. What does it return?
Why is the O(n·W) algorithm called pseudo-polynomial?
Pick an answer to see why.
Not quite. For every item you decide to take it whole (1) or leave it (0). You can neither split it nor take it twice.
Not quite. The table has (n + 1)(W + 1) cells and each one is a single max of two numbers.
Not quite. Going from W down, dp[w - wt] has not been overwritten in this pass, so it still describes the previous items. Going forward would let the current item be packed again.
Not quite. Rows count how many items are in play and columns are the room available; the cell is the best value achievable.
Not quite. The sleeping bag (3, 4) and tent (4, 5) weigh exactly 7 for value 9. The stove plus bottle gives only 8.
Not quite. It takes the stove (ratio 1.4), leaving 2 units; the bag and tent no longer fit, only the bottle does: 7 + 1 = 8.
Not quite. max(dp[1][4], 4 + dp[1][1]) = max(1, 4 + 1) = 5: the bag and the bottle together.
Not quite. Equal values mean the best load for room 7 did not need item 4. Only a change between rows means the item was packed.
Not quite. Each dp[w] reads dp[w - 1], already updated with the item, so the item is counted again at every step: 7. That is unbounded knapsack, not 0/1.
Not quite. W needs only about log2 W bits, so n·W = n·2^bits. Polynomial in the numbers, exponential in the input length.
Flashcards
Say your answer, then reveal it.
Card 1 of 12
What does dp[i][w] mean in 0/1 knapsack?
The most value you can get from the first i items with a capacity of w. The answer is dp[n][W].
Say your answer, then reveal it.
What is the recurrence?
dp[i][w] = dp[i-1][w] if item i does not fit, else max(dp[i-1][w], v_i + dp[i-1][w - w_i]): leave it, or take it on top of the best for the room left.
Say your answer, then reveal it.
What are the base cases?
Row 0 (no items) and column 0 (no room) are all zeros.
Say your answer, then reveal it.
What does 0/1 mean?
Each item is taken whole once (1) or left (0). No fractions and no repeats.
Say your answer, then reveal it.
Why does greedy by value per weight fail?
A high-ratio item can leave a gap nothing fills. Camping trip, capacity 7: greedy takes the stove (5, 7) and bottle for 8; sleeping bag and tent give 9. Greedy is only right for fractional knapsack.
Say your answer, then reveal it.
What is the time and space complexity?
O(n·W) time. O(n·W) space for the table, O(W) with one row.
Say your answer, then reveal it.
In the 1D version, why sweep capacity right to left?
So dp[w - wt] still holds the previous item's row. Left to right reads values already updated for this item, letting it be packed again: that is unbounded knapsack.
Say your answer, then reveal it.
Why is O(n·W) called pseudo-polynomial?
It is polynomial in the value of W but W takes only about log2 W bits to write, so it is exponential in the input size.
Say your answer, then reveal it.
How do you find which items were packed?
Walk up from dp[n][W]: if dp[i][w] != dp[i-1][w], item i was packed, so subtract its weight from w; if equal, it was left.
Say your answer, then reveal it.
What is the memo key in the top-down version?
The pair (i, cap): items still in play and room left. The item index alone is not enough.
Say your answer, then reveal it.
How does Partition Equal Subset Sum map to knapsack?
If the total S is even, ask whether some subset sums to S/2: a knapsack with boolean cells, ok[s] = ok[s] or ok[s - x], swept right to left.
Say your answer, then reveal it.
How does Target Sum reduce to subset counting?
P - N = T and P + N = S give P = (S + T)/2. Count subsets summing to P with ways[s] += ways[s - x]; 0 if S + T is odd or |T| > S.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.