Unlike the fractional knapsack, here every item must be taken whole or left. Greedy fails, but dynamic programming solves it exactly.
The Problem
Given n items with weights w₁..wₙ and values v₁..vₙ and a knapsack of capacity W, choose a subset of items with total weight at most W and maximum total value. Each item is taken (1) or not taken (0), hence the name. The problem is a model for budgeting, cargo loading and project selection. There are 2ⁿ subsets, so brute force is exponential.
The Recurrence
Let V[i][j] be the best value using only the first i items with capacity j. For item i there are two choices: leave it, giving V[i − 1][j]; or take it (if wᵢ ≤ j), giving vᵢ + V[i − 1][j − wᵢ]. Therefore V[i][j] = max( V[i − 1][j], vᵢ + V[i − 1][j − wᵢ] ) if wᵢ ≤ j, and V[i][j] = V[i − 1][j] otherwise. The base cases are V[0][j] = 0 and V[i][0] = 0. The answer is V[n][W].
Table, Complexity and Traceback
The table has (n + 1) rows and (W + 1) columns, filled in Θ(n·W) time with Θ(n·W) space (Θ(W) if only the value is needed). This is called pseudo-polynomial, because W is a number, not the length of the input; if W is huge, the method is slow, and indeed 0/1 knapsack is NP-hard. To find which items were chosen, start at V[n][W]: if V[i][j] ≠ V[i − 1][j], item i was taken, so record it and move to column j − wᵢ; then move up to row i − 1.
Definition: 0/1 knapsack. The problem of choosing whole items, each taken or left, to maximise total value without exceeding a weight limit, solved by dynamic programming in Θ(nW) time.
Table 3.4: Greedy versus dynamic programming on knapsack
| Method | Fractional | 0/1 |
|---|---|---|
| Greedy by ratio | Optimal | Not always optimal |
| Dynamic programming | Not needed | Optimal, Θ(nW) |
Example 3.4: Filling the table
Problem: Capacity W = 5; items (weight, value): (2, 12), (1, 10), (3, 20), (2, 15). Find the best value and the items.
Solution:
- Row 1 (item 1: 2, 12): columns 0–5 → 0, 0, 12, 12, 12, 12.
- Row 2 (item 2: 1, 10): 0, 10, 12, 22, 22, 22.
- Row 3 (item 3: 3, 20): 0, 10, 12, 22, 30, 32.
- Row 4 (item 4: 2, 15): 0, 10, 15, 25, 30, 37.
- V[4][5] = 37; traceback: 37 ≠ 32 so item 4 taken (cap 3); V[3][3] = 22 = V[2][3] so item 3 not taken; item 2 and item 1 taken.
Result: Maximum value 37 using items 1, 2 and 4 (weight 5).
def knap(wt, val, W):
n = len(wt)
V = [[0] * (W + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
for j in range(W + 1):
V[i][j] = V[i - 1][j]
if wt[i - 1] <= j:
take = val[i - 1] + V[i - 1][j - wt[i - 1]]
V[i][j] = max(V[i][j], take)
return V
V = knap([2, 1, 3, 2], [12, 10, 20, 15], 5)
for row in V:
print(row)
print("best value:", V[4][5])
Output
[0, 0, 0, 0, 0, 0]
[0, 0, 12, 12, 12, 12]
[0, 10, 12, 22, 22, 22]
[0, 10, 12, 22, 30, 32]
[0, 10, 15, 25, 30, 37]
best value: 37
Common mistakes
- Using the greedy ratio rule for 0/1 knapsack and trusting the answer.
- Allowing an item to be used twice by reading from the same row instead of the previous one.
- Forgetting that Θ(nW) is pseudo-polynomial.
- Stopping at the value and not tracing back the chosen items.
Key takeaways
- V[i][j] = max(V[i − 1][j], vᵢ + V[i − 1][j − wᵢ]) if wᵢ ≤ j.
- Time and space Θ(nW); answer in V[n][W].
- Traceback: item i is taken when V[i][j] ≠ V[i − 1][j].
- Greedy by ratio can fail for 0/1 knapsack.
Practice questions
- What does V[i][j] represent?
- What is the time complexity of the DP knapsack?
- How are the chosen items recovered?
Answers to the practice questions
- The best value using the first i items with capacity j.
- Θ(n·W).
- Trace back from V[n][W]: item i is taken whenever V[i][j] differs from V[i − 1][j].
Sources
- Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit III
