Knapsack Problem - Recursive Solution

·

4 min read

Knapsack Problem - Recursive Solution

Understanding the Problem

Knapsack problem is a famous problem and people usually start of with dynamic programming approach. But all dynamic programming solutions are nothing but optimised recursive solutions. Yes recursive solution isn't the optimal method to solve it (the best being DP ) but if you are able to write a recursive solution then within minutes you can optimise into the dynamic programming solution.

For those of you who haven't heard about the problem before, here it goes. We are given 'n' items with each item having a weight and a value. We are also given a knapsack(bag) which has a fixed capacity. The question is to find out what is the maximum value which we can obtain by placing items in the bag such that the combined weight doesn't exceed knapsack capacity. There is only instance of item in the bag which means to say it is an instance of 0/1 Knapsack.

Example time : items_weight : [2,3,4,5] items_value : [1,4,5,7] and knapsack capacity : 7. There are four items with weights and values as given above. We have to select items such that value is maximum and the total weight does not exceed the capacity. One possible solution is 2nd and 3rd item with combined value 9. Another solution is 1st and 4th item with value 8. We see that the first solution gives maximum value.

Approach

Before diving into the recursive approach let us understand why to use recursion here. If we think about the problem, we will notice that for every item we have a choice either to put it in the bag or not. This decision has to be taken for every item. Whenever such choices or decisions are to be made there is recursion involved.

Now let us consider the last item. Before we choose to put it in the bag we have to check its weight. If it exceeds knapsack capacity then it cannot go and we will have no choice here. But if its weight is less than the knapsack capacity then we make two recursive calls indicating the two choices.

  • In the first call we consider the item to be placed in the bag and update our knapsack capacity
  • In the second call we leave the item and the capacity remains unchanged.
After this we then go to the second last item and make choices as shown above and this continues till we hit the base case.

knapsack

Code

code.png

Base case is when the choice has been made for all the items to be placed or not. Also if we run out of knapsack capacity, maximum value will be 0. We are making the recursive calls with items-1 to indicate that last item has been processed. This is the reason we considered last item in the above explanation.

We will optimise it using dynamic programming approach in the next article. This is my first article. Your comments and thoughts will be appreciated. Thanks for reading.