Solution of knapsack problem using dynamic programming
Solution of knapsack problem using dynamic programming
To get as much value into the knapsack as possible given the weight constraint of the knapsack.
Item | 1 | 2 | 3 | 4 |
---|---|---|---|---|
Value | 100 | 20 | 60 | 40 |
Weight | 3 | 2 | 4 | 1 |
Value Matrix
V[i,w] | w=0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
i=0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 100 | 100 | 100 |
2 | 0 | 0 | 20 | 100 | 100 | 120 |
3 | 0 | 0 | 20 | 100 | 100 | 120 |
4 | 0 | 40 | 40 | 100 | 140 | 140 |
Maximum value we can put the knapsack is V[4,5] = 140
Chosen items are 1 and 4
How the items are selected can be seen in the code