0 - 1 Knapsack Problem

Use dynamic programming to implement the solution of a problem.

We'll cover the following

Problem statement

For each item, you are given its weight and its value. You want to maximize the total value of all the items you are going to put in the knapsack such that the total weight of the items is less than the knapsack’s capacity. What is this maximum total value?

The only condition is to consider all subsets of items. There can be two cases for every item:

  1. The item is included in the optimal subset.
  2. The item is not included in the optimal set.

Therefore, the maximum value that can be obtained from n items is the maximum of the following two values:

  1. Maximum value obtained by n - 1 items and W weight (excluding nth{n^{th}} item)
  2. Value of nth{n^{th}} item plus maximum value obtained by n - 1 items and W minus weight of the nth{n^{th}} item (including nth{n^{th}} item). If the weight of the nth{n^{th}} item is greater than W, the nth{n^{th}} item cannot be included and case 1 is the only possibility.

Let’s look at the recurrence relation:

  • Base case: If we have explored all the items or if we have reached the maximum capacity of Knapsack,
    if (n=0 or W=0)
        return 0
    
  • If the weight of the nth{n^{th}} item is greater than the capacity of the knapsack, we cannot include this item:
    if (weight[n] > W)
       return solve(n-1, W)
    
  • Otherwise, we can,
    return max{solve(n-1, W), solve(n-1, W-weight[n])}
    
    Here, the expression solve(n - 1, W) means that we have not included the item and the expression solve(n - 1, W - weight[n]) means that we have included that item in the knapsack.

If we build the recursion tree for the above relation, we can see that the property of overlapping sub-problems is satisfied. So, let’s try to solve it using dynamic programming.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.