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
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:
Naive recursive algorithm takes exponential time (something like 2i).
Modifying the algorithm to cache answers gives an O(n*B)-time algorithm.