Solution: The 0/1 Knapsack Problem
Explore various approaches to solving the 0/1 Knapsack problem in detail.
Solution 1: Brute force
Explanation
We start at the beginning of the weight list and check if the item is within the maximum capacity on line 27.
For each item starting from the end:
- We create a new set that includes item if the total weight does not exceed the capacity and recursively process the remaining capacity and items. We save the result in
profit1(line 27). - We create a new set without item , recursively process the remaining items, and save the result in the variable,
profit2(line 31). - We return the set from the above two sets with higher profit (line 36).
Let’s draw the recursive calls to see if there are any overlapping subproblems. As we can see, in each recursive call, the profits and weights lists remain constant, and only the capacity and the current index change. For simplicity, let’s denote capacity with j and the current index with n:
Time complexity
The time complexity of the algorithm above is , i.e., exponential, where is the total number of items. This is because we will have that many calls. This is owing to the overlapping subproblems. Let’s see how we can reduce it with a dynamic programming approach.