On this page
The 0/1 knapsack problem is a classic first example of dynamic programming: one small table, one rule, and an answer you can check by hand.
What is the 0/1 knapsack problem?
You have n items. Item i has a weight w_i and a value v_i, and your bag holds at most W. Each item is packed whole (1) or left behind (0), and you want the largest total value. This is the 0-1 version in the Wikipedia article on the knapsack problem, which notes that early work on it dates back to 1897.
Our example has capacity W = 7:
Item 1: weight 1, value 1
Item 2: weight 3, value 4
Item 3: weight 4, value 5
Item 4: weight 5, value 7
How does the DP table best[i][w] work?
Define best[i][w] as the largest value you can get using only the first i items with a weight limit of w. Row 0 (no items) is all zeros. Every other cell is one decision about item i:
Skip it: best[i−1][w].
Take it, only if w_i ≤ w: v_i + best[i−1][w − w_i].
best[i][w] is the larger of the two. Each cell looks only at the row above, so you fill row by row. The full table, columns w = 0 to 7:
i = 0: 0, 0, 0, 0, 0, 0, 0, 0
i = 1: 0, 1, 1, 1, 1, 1, 1, 1
i = 2: 0, 1, 1, 4, 5, 5, 5, 5
i = 3: 0, 1, 1, 4, 5, 6, 6, 9
i = 4: 0, 1, 1, 4, 5, 7, 8, 9
Check one cell: for best[3][7], skipping item 3 gives 5, taking it gives 5 + best[2][3] = 9. The bottom-right cell, 9, is the answer.
How do you read back the chosen items?
The table gives the best value, not the items. Walk back from the bottom-right corner:
best[4][7] = best[3][7] = 9, so skip item 4; w stays 7.
best[3][7] = 9 differs from best[2][7] = 5, so item 3 was taken: w = 7 − 4 = 3.
best[2][3] = 4 differs from best[1][3] = 1, so item 2 was taken: w = 3 − 3 = 0.
w = 0, so stop.
The answer is items 2 and 3: weight 3 + 4 = 7, value 4 + 5 = 9.
Why does greedy by value per weight fail?
The tempting shortcut is to pack the best value-per-weight ratios first. Our ratios are 1.40 (item 4), 1.33 (item 2), 1.25 (item 3) and 1.00 (item 1). Greedy packs item 4, finds items 2 and 3 too heavy for the 2 units left, and adds item 1: weight 6, value 8, one short of the optimum.
Items cannot be split, so grabbing item 4 leaves a gap nothing valuable fills, while items 2 and 3 fill the bag exactly. Dynamic programming weighs both choices for every item at every capacity. Greedy does work for writing a fraction as a sum of unit fractions.
Is O(nW) a polynomial-time algorithm?
The table has (n + 1)(W + 1) cells, each constant work, so the method runs in O(nW) time, as the Wikipedia article on the knapsack problem states.
But O(nW) is only pseudo-polynomial. Writing W takes about log W bits, so one extra digit in the capacity makes the table ten times wider while the input grows by one character; the article on pseudo-polynomial time uses knapsack as its example. The decision version of knapsack is NP-complete, and a form of it (closer to what is now called subset sum) is one of Karp's 21 NP-complete problems, from Richard Karp's 1972 paper Reducibility among Combinatorial Problems. So the table is fast when W is small, and no known algorithm is fast on every input.
What changes in the fractional knapsack?
If you may take part of an item, like rice by the kilogram, greedy becomes correct. The continuous (fractional) knapsack problem is solved by taking the best value-per-weight ratios first. The article credits this greedy method to George Dantzig in 1957, whose paper Discrete-Variable Extremum Problems sorts items by value per unit of weight. With comparison sorting it runs in O(n log n) time.
In our example, take all of item 4 (value 7), then 2 of item 2's 3 weight units for 2/3 × 4 ≈ 2.67 more: about 9.67 in total. Every whole-item packing is allowed here too, so the fractional answer is an upper bound on the 0/1 answer.
To practise:
Change item 3's value to 6 and refill rows 3 and 4. Which items win now?
Raise the capacity to 8 by adding one column, and read back the new choice.
The hidden Markov model forward algorithm fills a table step by step in the same spirit. If the recurrence still feels slippery, the Fermi Concept Library covers every concept in physics, chemistry, biology, mathematics and computing, and the Fermi Problem Library has a step-by-step solver that explains the reasoning.



