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:
- The item is included in the optimal subset.
- 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:
- Maximum value obtained by
n - 1
items andW
weight (excluding item) - Value of item plus maximum value obtained by
n - 1
items andW
minus weight of the item (including item). If the weight of the item is greater thanW
, the 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 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,
Here, the expressionreturn max{solve(n-1, W), solve(n-1, W-weight[n])}
solve(n - 1, W)
means that we have not included the item and the expressionsolve(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.