code/dynamic_programming/src/knapsack/README.md
Given n items, in which the ith item has weight wi and value vi,
find the maximum total value that can be put in a knapsack of capacity W.
You cannot break an item, either pick the complete item, or don't pick it.
This is a dynamic programming algorithm. We first start by building a table consisting of rows of weights and columns of values. Starting from the first row and column, we keep on filling the table till the desired value is obtained.
Let the table be represented as M[n, w] where n is the number of objects included
and w is the left over space in the bag.
For every object, there can be two possibilities:
M[n, w] = M[n - 1, w]M[n, w] = vn + M[n - 1, w - wn], where vn is value of the nth object and wn is its weight.M[n, w] = M[n - 1, w]The value of M[n,w] is the maximum of both these cases.
In short, the optimal substructure to compute M[n, w] is:
M[n, w] = M[n-1, w], if wn > w
max( M[n-1, w], vn + M[n-1, w - wn] ), otherwise
The complexity of above algorithm is O(n*W), as there are n*W states and each state is computed in O(1).