The Knapsack problem is defined as follows:

You are given, for a collection of n items {1,2,...,n}, the cost cost[i] and value value[i] for each item i. Each cost and value is a non-negative integer.

You are also given a budget B (a non-negative integer).

The goal is to find the maximum value of any subset of the items having total cost at most B.

Formally, given a subset S of the items, define cost(S) = ∑_{i∈ S} cost[i]
and define value(S) = ∑_{i∈ S} value[i].
The goal is to compute

- max { value(S) : S ⊆ {1,2,...,n} and cost(S) ≤ B}.

Define V[i,b] = max { value(S) : S ⊆ {1,2,...,i} and cost(S) = b}. In words, V[i,b] is the maximum value of any subset having total cost exactly b and using only items in {1,2,...,i}.

Claim:

- V[0,0] = 0
- V[0,b] = -∞ for b ≠ 0
- V[i,b] = max { V[i-1,b], V[i-1, b-cost[i]] + value[i] } for (i > 0)

Naive recursive algorithm takes exponential time (something like 2^{i}).

Modifying the algorithm to cache answers gives an O(n*B)-time algorithm.

- DynamicProgramming
- GoodrichAndTomassia section 5.3.3