Knapsack Problem - Recursive Solution
Tags: algorithms, programming, firstpost
Published: 2021-02-22 08:11:48

# Understanding the Problem
<p>
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 (<em>the best being DP</em>) but if you are able to write a recursive solution then within minutes you can optimise into the dynamic programming solution.
</p>
<p>
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 <strong>maximum value</strong> 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.
</p>
<p>
<strong>Example time :</strong>  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 <em>2nd and 3rd item with combined value 9</em>. Another solution is <em>1st and 4th item with value 8</em>. We see that the first solution gives maximum value. 
</p>

# Approach
<p>
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 <strong>choices or decisions</strong> are to be made there is recursion involved. 
</p>
<p>
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<strong>two recursive calls</strong> indicating the two choices. <br />

<ul>
<li>In the first call we consider the item to be placed in the bag and update our knapsack capacity</li>
<li>In the second call we leave the item and the capacity remains unchanged.</li>
</ul>
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.
</p>


![knapsack](https://cdn.hashnode.com/res/hashnode/image/upload/v1613974519313/0Q93ab7Cn.png)



# Code



![code.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613975418544/QBZF6Lkj7.png)

<p>
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.
</p>
<p>
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.
</p>