Problem
Goal: Pack knapsack so as to maximize total value.
- There are n items: item i provides value vi > 0 and weights wi > 0
- Knapsack has weight capacity of W
Assumption: All input values are integral
How to develop a dynamic programming algorithm
Define
OPT(i, w) = max-profit subset of items 1, …, i with weight limit w.
Goal
get OPT(n, W).
There are two cases
Case 1: OPT(i, w) does not select item i.
- IOT(i, w) selects best of {1, 2, …, i - 1} using weight limit w
Case 2: OPT(i, w) selects item i.
- Collect value vi
- New weight limit = w - wi
- OPT(i, w) selects best of {1, 2, …, i - 1} using this new weight limit
Bellman equation
OPT(i, w) = 0 , if i = 0
OPT(i, w) = OPT(i - 1, w), if wi > w
OPT(i, w) = max(OPT(i - 1, w), vi + OPT(i - 1, w - wi)), otherwise
1 | # W = 6, |
After computing optimal values, we can trace back to find which item should be selected:
OPT(i, w) takes item i if and only if $table[i][w] > table[i - 1][w]$
Reference
https://www.coursera.org/lecture/algorithms-greedy/the-knapsack-problem-LIgLJ
http://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/06DynamicProgrammingI-2x2.pdf