

So we can establish that, if the weight of the current item is greater than our maximum capacity, we can't take that item. For Table, since 2 is less than weight, that is the weight of the second item, we can't take the item. Now what is the best we can do without taking item 2? The value from the top, that is Table which contains the maximum value we can get if we had maximum capacity 1 and we didn't pick the second item. Moving on, for Table, we are asking ourselves, if we had item 1 and 2 and if the maximum capacity of our knapsack was 1, what is the maximum value we can get? If we take both item 1 and 2, the total weight will be 4, which will exceed our current knapsack capacity. Since we have only 1 quantity of each item, no matter how we increase the capacity of our knapsack, from one item, 1 is the best value we can make. This is because we only have one item, which gives us value 1. This will be same for Table, Table, Table, Table and Table. For Table that means if our maximum capacity is 2 and we only have the first item, the maximum value we can get is 1. When we are filling Table, we are asking ourselves if our maximum capacity was 1 and we had only the first item, what would be our maximum value? The best we can do is 1, by picking the item. It means if our maximum capacity is 0, no matter whatever item we have, since we can't pick any items, our maximum value will be 0. Remember these are not part of the array, these are for calculation purpose only, you need not store these values in table array. We've incorporate the weight and value of each item to the array for our convenience. And the number of rows will be same as the number of items. We'll take a 2D array table, where the number of columns will be the maximum value we can get by taking the items + 1. Let's try to understand it with our example: If we don't pick the item, we'll pick the items that gives us maximum value without including the item.
0 1 knapsack problem plus#
Now if we pick the item, our value will be the value of the item, plus whatever the value we can get by subtracting the value from our capacity and the maximum we can get for that remaining weight. Our strategy will be: whenever a new item comes, we'll check if we can pick the item or not and again we'll pick the items that give us maximum value. If we minimize the weight and maximize the value, we can find out our optimal solution.įor these reasons, we'll use dynamic programming to solve our problem.If we calculate the combinations for item.So our problem can be divided into subproblems. We can take lesser items and calculate the maximum value we can get using those items and combine them.Here, a few aspects we can notice, that is: This is quite cumbersome when the number of items increase. For 4 items, we'll need to check ( 4! - 1) = 23 possible combinations (excluding one with no items). Then we can calculate their total weights and exclude them which exceed our knapsack's capacity and find out the one that gives us maximum value. One brute force method would be taking all possible combinations of items. Their weights and values are: +-+-+-+-+-+ For example, let's say we have a knapsack capacity of 7. Let's, for now, concentrate on our problem at hand. If it was not a 0-1 knapsack problem, that means if you could have split the items, there's a greedy solution to it, which is called fractional knapsack problem. Suppose you are asked, given the total weight you can carry on your knapsack and some items with their weight and values, how can you take those items in such a way that the sum of their values are maximum, but the sum of their weights don't exceed the total weight you can carry? The 0-1 indicates either you pick the item or you don't.
