Here I am going to explain about 0/1 Knapsack problem using dynamic programming. We already know about what is knapsack problem? in greedy approach.
The main concept in both the approach is same that is we must select the item and fill it into sack (just like a bag) in such a way that we get more profit.
In greedy approach we can fill the item into the sack partially that means if the weight of item 20 and the space available in sack is 10 than can select 10 weight out of 20 weight and put into the sack that means factorial part is allowed here.
But in dynamic programming when we solving knapsack problem fractional part is not allowed. We can select either complete portion of item that means 1 or select nothing from the item that means 0 , that's why it is known as 0/1 knapsack problem.
Here the item / object are not divisible or breakable such as mobiles, laptop, mouse, keyboard etc.
So here our objective is to maximise profit:
So above method is time consuming if we are finding all the possibilities for the selection of items.
Now we will discuss how we do same thing in easy way so that we save time and effort. Let we understand dynamic programming by the help of an example.
Example:1
suppose we have
P = { 1, 2, 5, 6 }
W= { 2, 3, 4, 5 }
and m=8 , n=4
For solving the above method we are using tabulation method.
Create column according to the capacity of sack / bag i.e here 8.
Taking all object at a time is not possible , So here one by one we will consider the object/item like 1, 2, 3, ...,8.
Here we create table of 9 columns which is depend on the value of m i.e m=8. We have 4 items so we take 5 rows.
Initially R row and C column according to the above table will be 0.
Note: (According to this table)
If we consider row-2 for item-2 than we also consider item 1 with their (P,W).
If we consider row-3 for item-3 than we consider item-2 and item-1 with their (P,W).
If we consider row-4 for item-4 than we consider item-3,item-2 and item-1 with their (P,W).
If we consider row-5 than we consider item-4,item-3,item-2 and item-1 with their (P,W).
So we can say that if we are in ith row than we consider all the previous rows also.
Now Let we start:
If we select item-1 than we see what is the profit i.e 1, So put 1 in col-2 column.
Now understand the logic for putting profit in columns suppose here for item-1 the weight is 2, Now imagine can you put suppose 2 kg of item in 1 kg of bag the answer is no so we put 0(profit) in column 1,
Now check for next can you put suppose 2 kg of item in 2 kg of bag the answer is yes,so we put 1(profit) in column 2, In the same way answer is yes for all column 3,4,5,6,7,8 so we put 1 in all column.
Now the table will be
Second case we select item-2 whose weight W=3 & P=2 , so put their profit 2 into column-3 and all cells left side of column-3 will be remain same because we cannot put suppose 3 kg of item in 1 or 2 kg of bag.
Same way for col 7 & col 8
Now Calculate for row-3
Here weight of item is 4 and profit is 5
Write as it is value for col-1,col-2,col-3 of row-3
Now calculate for row-3 & col-4
so write 5 in col-4 row-3
Now calculate for row-3 & col-5
Now Calculate for Item-4 or Row-4
Here weight of item is 5 and profit is 6
Write as it is value for col-1,col-2,col-3,col-4 of row-4
Now how we find considered item in a sack the procedure is first pickup the maximum profit.What we get like here is 8 then we find whether this value is available in previous row. If not available than 8 is final profit.