Dynamic Programming21 sections · 916 units
Open in Course

0/1 Knapsack - State Design

Two dimensions

What information do you need to make a decision about item ii? You need to know which items you've already considered and how much capacity remains. Define dp[i][w]dp[i][w] as the maximum value using items 11 to ii with capacity ww. Here ii ranges from 00 to nn, and ww ranges from 00 to WW.

This gives you (n+1)×(W+1)(n+1) \times (W+1) states. Each state (aa specific combination of ii and ww) represents a smaller problem to solve: "What's the best I can do with these items and this capacity?"