...
/Solution Review: The Knapsack Problem
Solution Review: The Knapsack Problem
In this lesson, we will review the solution to the knapsack problem.
Solution 1: Simple recursion
def solveKnapsack(weights, prices, capacity, index):# base case of when we have run out of capacity or objectsif capacity <= 0 or index >= len(weights):return 0# if weight at index-th position is greater than capacity, skip this objectif weights[index] > capacity:return solveKnapsack(weights, prices, capacity, index + 1)# recursive call, either we can include the index-th object or we cannot, we check both possibilities and return the most optimal one using maxreturn max(prices[index]+solveKnapsack(weights, prices, capacity - weights[index], index+1),solveKnapsack(weights, prices, capacity, index + 1))def knapsack(weights, prices, capacity):return solveKnapsack(weights, prices, capacity, 0)print(knapsack([2,1,1,3], [2,8,1,10], 4))
Explanation
Let’s go over the intuition of the problem before moving on to the explanation of the code. Remember your goal was to select a subset of objects which return the maximum total profit while obeying the constraint that their weight is less than or equal to the total capacity of the knapsack. Now any object, let’s say a book, either can or cannot be a part of the optimal subset. There cannot be any other possibility for this book, right? This is what we are doing in this solution. We make two recursive calls: one with an object included (line 9), and another without (line 10). Then we take the maximum of these two profit values. We continue doing this ...