Yasir Explains/Algorithms/Dynamic Programming/0/1 Knapsack
Dynamic Programming

0/1 Knapsack

On this page

ProblemDP recurrence relationDP memoizationDP base casePseudocodeComplexity
Dynamic Programming

0/1 Knapsack

Two-parameter DP: first i items and capacity c — skip or take item i−1, recurse with memo on (i, c).

Problem

You have n items. Item j (0-based) has weight w[j] and value v[j]. Knapsack capacity is W. Each item may be used at most once. Maximize total value without exceeding W.

DP recurrence relation

Let dp(i, c) = maximum value using only items 0, 1, …, i−1 (the first i items) with total weight at most c.

For i ≥ 1 and c ≥ 0:

  • Skip item i−1: value dp(i − 1, c).
  • Take item i−1 (only if w[i−1] ≤ c): value v[i−1] + dp(i − 1, c − w[i−1]).
Example
dp(i, c) = max( dp(i − 1, c), v[i−1] + dp(i − 1, c − w[i−1]) ) if w[i−1] ≤ c
dp(i, c) = dp(i − 1, c) otherwise

Explanation: Any optimal solution for the first i items either does not use item i−1 (first term) or uses it once (second term); smaller subproblems use only the first i − 1 items.

DP memoization

Use a 2D table memo[0…n][0…W] (or a map) with −1 meaning unknown. dp(i, c) only calls dp(i − 1, ·) with the same or smaller capacity, so i strictly decreases along one branch — recursion always moves toward i = 0.

Fill memo[i][c] the first time you compute it. There are (n + 1)(W + 1) states, O(1) work each — O(nW) time, O(nW) space for memo plus stack O(n).

DP base case

Example
dp(0, c) = 0 for every c in 0 … W

Explanation: With zero items available, the best value is 0 no matter the capacity. All recursion eventually reaches i = 0.

Pseudocode

Top-down:

1KNAPSACK-DP(i, c, w, v, memo)
2 if i == 0
3 return 0
4 if memo[i][c] != UNKNOWN
5 return memo[i][c]
6 skip = KNAPSACK-DP(i - 1, c, w, v, memo)
7 if w[i-1] > c
8 memo[i][c] = skip
9 else
10 take = v[i-1] + KNAPSACK-DP(i - 1, c - w[i-1], w, v, memo)
11 memo[i][c] = max(skip, take)
12 return memo[i][c]

Complexity

Time: O(nW). Space: O(nW) for memo (iterative 1D rolling is a separate implementation trick, not shown here).

Complexity Analysis

Time Complexity

O(nW)

Space Complexity

O(nW) for memo

Pseudo-polynomial in W

Growth Rate Comparison

n (input size)O(1)O(log n)O(n)O(n log n)O(n²)