The rows are the items with their weights and value respectively. Lets have a look at the array which we have to create. I(If included and small enough) = value(i) + M The row and column contains one items extra considering the solution with zero capacity and no item. M is the two dimensional array which will store the value for each of the maximum possible value for each sub problem.We can start with knapsack of 0,1,2,3,4 capacity. Divide the problem with having a smaller knapsack with smaller problems.To solve this problem we need to keep the below points in mind: This will result in explosion of result and in turn will result in explosion of the solutions taking huge time to solve the problem. In that scenario we need some particular algorithm to solve this problem.Ĭan we try to solve the above problem using brute force algorithm where we just include each item 0 or 1 time.įor three items we will have : 2*2*2 = (2) to the power of 3 combinations and for n items we will have (2) to the power of N items. But suppose we have a huge number of the items to be arranged. Though the number of the items are smaller we can solve this problem easily. Finally I want to put these inside the knapsack. In the above figure I have listed all the items with their weights and values. In my previous article I have solved the Fibonacci series by using the cache build from top. There are two strategies to build the cache. Keep the result of the smaller problems in cache and then solving the bigger problem using the cache. In dynamic programming we solve the bigger problem by diving it into smaller problems. This algorithm is a part of the dynamic programming. Each item can be placed zero or one time. We should place all those items in the knapsack in such a way that they the all the items weight should not exceed the knapsack’s capacity and still having maximum value. The companies like Google, Facebook, Amazon will have some interview question based on this algorithm. The knapsack algorithm can be used to solve a number of programming problems asked by top product based companies in interview. This is called the by this particular name as we have to solve here a problem with in which we are provided with some specific items with their weights and values and a knapsack with some capacity. In this article I will discuss about one of the important algorithm of the computer programming.
0 Comments
Leave a Reply. |