The 0/1 Knapsack Problem

The 0/1 Knapsack Problem is a fundamental problem in combinatorial optimization. Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.

The "0/1" indicates that you cannot break an item; you either take it in its entirety or leave it behind.


1. Defining the State

To solve this using Dynamic Programming, we define a 2D table dp[i][w] where:


2. The Recurrence Relation

For each item i with weight wi and value vi:

  1. Option 1: Exclude the item: The value remains the same as for the first i-1 items at the same weight: dp[i-1][w].
  2. Option 2: Include the item: Add its value to the maximum value possible for the remaining weight: vi + dp[i-1][w - wi]. (This is only possible if wi <= w).

The Decision: dp[i][w] = max(Option 1, Option 2)


3. Complexity Analysis

Metric Complexity Description
Time O(N * W) Where N is the number of items and W is the capacity.
Space O(N * W) To store the 2D results table.
Space (Optimized) O(W) Using a single 1D array by iterating backwards.

4. Implementation (Space Optimized)

function knapsack(weights, values, capacity) {
  let n = weights.length;
  let dp = new Array(capacity + 1).fill(0);

  for (let i = 0; i < n; i++) {
    // Iterate backwards to avoid using the same item multiple times
    for (let w = capacity; w >= weights[i]; w--) {
      dp[w] = Math.max(dp[w], values[i] + dp[w - weights[i]]);
    }
  }
  return dp[capacity];
}

5. Interview Pro-Tips: Why iterate backwards?

If an interviewer asks why the 1D space optimization requires iterating backwards through the weights:


Technical Summary

  1. Decision: To take or not to take.
  2. Pseudo-Polynomial: The complexity O(N*W) depends on the numeric value of the capacity, not just the number of items.
  3. Overlapping: Many weight combinations lead to the same sub-problems.
  4. Backwards Iteration: The key to 1D space optimization.