Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).

A simple solution is to consider all subsets of items and calculate the total weight and value of all subsets. Consider the only subsets whose total weight is smaller than W. From all such subsets, pick the maximum value subset.

  • Optimal Substructure:

To consider all subsets of items, there can be two cases for every item: (1) the item is included in the optimal subset, (2) not included in the optimal set.
Therefore, the maximum value that can be obtained from n items is max of following two values.

  • Maximum value obtained by n-1 items and W weight (excluding nth item).
  • Value of nth item plus maximum value obtained by n-1 items and W minus weight of the nth item (including nth item).

If weight of nth item is greater than W, then the nth item cannot be included and case 1 is the only possibility.

  • Overlapping Subproblems

Following is recursive implementation that simply follows the recursive structure mentioned above.

[pastacode lang=”c” manual=”%2F*%20A%20Naive%20recursive%20implementation%20of%200-1%20Knapsack%20problem%20*%2F%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20utility%20function%20that%20returns%20maximum%20of%20two%20integers%0Aint%20max(int%20a%2C%20int%20b)%20%7B%20return%20(a%20%3E%20b)%3F%20a%20%3A%20b%3B%20%7D%0A%20%0A%2F%2F%20Returns%20the%20maximum%20value%20that%20can%20be%20put%20in%20a%20knapsack%20of%20capacity%20W%0Aint%20knapSack(int%20W%2C%20int%20wt%5B%5D%2C%20int%20val%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%2F%2F%20Base%20Case%0A%20%20%20if%20(n%20%3D%3D%200%20%7C%7C%20W%20%3D%3D%200)%0A%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%2F%2F%20If%20weight%20of%20the%20nth%20item%20is%20more%20than%20Knapsack%20capacity%20W%2C%20then%0A%20%20%20%2F%2F%20this%20item%20cannot%20be%20included%20in%20the%20optimal%20solution%0A%20%20%20if%20(wt%5Bn-1%5D%20%3E%20W)%0A%20%20%20%20%20%20%20return%20knapSack(W%2C%20wt%2C%20val%2C%20n-1)%3B%0A%20%0A%20%20%20%2F%2F%20Return%20the%20maximum%20of%20two%20cases%3A%20%0A%20%20%20%2F%2F%20(1)%20nth%20item%20included%20%0A%20%20%20%2F%2F%20(2)%20not%20included%0A%20%20%20else%20return%20max(%20val%5Bn-1%5D%20%2B%20knapSack(W-wt%5Bn-1%5D%2C%20wt%2C%20val%2C%20n-1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20knapSack(W%2C%20wt%2C%20val%2C%20n-1)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20val%5B%5D%20%3D%20%7B60%2C%20100%2C%20120%7D%3B%0A%20%20%20%20int%20wt%5B%5D%20%3D%20%7B10%2C%2020%2C%2030%7D%3B%0A%20%20%20%20int%20%20W%20%3D%2050%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(val)%2Fsizeof(val%5B0%5D)%3B%0A%20%20%20%20printf(%22%25d%22%2C%20knapSack(W%2C%20wt%2C%20val%2C%20n))%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output:

220

It should be noted that the above function computes the same subproblems again and again. See the following recursion tree, K(1, 1) is being evaluated twice. Time complexity of this naive recursive solution is exponential (2^n).

In the following recursion tree, K() refers to knapSack().  The two 
parameters indicated in the following recursion tree are n and W.  
The recursion tree is for following sample inputs.
wt[] = {1, 1, 1}, W = 2, val[] = {10, 20, 30}

                       K(3, 2)         ---------> K(n, W)
                   /            \ 
                 /                \               
            K(2,2)                  K(2,1)
          /       \                  /    \ 
        /           \              /        \
       K(1,2)      K(1,1)        K(1,1)     K(1,0)
       /  \         /   \          /   \
     /      \     /       \      /       \
K(0,2)  K(0,1)  K(0,1)  K(0,0)  K(0,1)   K(0,0)
Recursion tree for Knapsack capacity 2 units and 3 items of 1 unit weight.

Since suproblems are evaluated again, this problem has Overlapping Subprolems property. So the 0-1 Knapsack problem has both properties of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array K[][] in bottom up manner. Following is Dynamic Programming based implementation.

[ad type=”banner”] [pastacode lang=”c” manual=”%2F%2F%20A%20Dynamic%20Programming%20based%20solution%20for%200-1%20Knapsack%20problem%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20utility%20function%20that%20returns%20maximum%20of%20two%20integers%0Aint%20max(int%20a%2C%20int%20b)%20%7B%20return%20(a%20%3E%20b)%3F%20a%20%3A%20b%3B%20%7D%0A%20%0A%2F%2F%20Returns%20the%20maximum%20value%20that%20can%20be%20put%20in%20a%20knapsack%20of%20capacity%20W%0Aint%20knapSack(int%20W%2C%20int%20wt%5B%5D%2C%20int%20val%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20int%20i%2C%20w%3B%0A%20%20%20int%20K%5Bn%2B1%5D%5BW%2B1%5D%3B%0A%20%0A%20%20%20%2F%2F%20Build%20table%20K%5B%5D%5B%5D%20in%20bottom%20up%20manner%0A%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%3D%20n%3B%20i%2B%2B)%0A%20%20%20%7B%0A%20%20%20%20%20%20%20for%20(w%20%3D%200%3B%20w%20%3C%3D%20W%3B%20w%2B%2B)%0A%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20if%20(i%3D%3D0%20%7C%7C%20w%3D%3D0)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20K%5Bi%5D%5Bw%5D%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%20%20%20else%20if%20(wt%5Bi-1%5D%20%3C%3D%20w)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20K%5Bi%5D%5Bw%5D%20%3D%20max(val%5Bi-1%5D%20%2B%20K%5Bi-1%5D%5Bw-wt%5Bi-1%5D%5D%2C%20%20K%5Bi-1%5D%5Bw%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20K%5Bi%5D%5Bw%5D%20%3D%20K%5Bi-1%5D%5Bw%5D%3B%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%7D%0A%20%0A%20%20%20return%20K%5Bn%5D%5BW%5D%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20%20%20int%20val%5B%5D%20%3D%20%7B60%2C%20100%2C%20120%7D%3B%0A%20%20%20%20int%20wt%5B%5D%20%3D%20%7B10%2C%2020%2C%2030%7D%3B%0A%20%20%20%20int%20%20W%20%3D%2050%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(val)%2Fsizeof(val%5B0%5D)%3B%0A%20%20%20%20printf(%22%25d%22%2C%20knapSack(W%2C%20wt%2C%20val%2C%20n))%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output:

220

Time Complexity: O(nW) where n is the number of items and W is the capacity of knapsack.

[ad type=”banner”]