You are on page 1of 3

0/1 Knapsack Problem

0/1 knapsack Problem


Dynamic Programming –Tabulation Method.
4 objects, n=4, capacity of knapsack=m=8
Each object has weight and profit associated with it
P= {1, 2, 5, 5}
W= {2, 3, 4, 5}

All objects or subset of objects


Solution in the form of set x= {0, 1, 0-….}
X=0/1 , solid objects

Carry objects max ∑ PiXi


m=8
∑WiXi <= m
• Approach of dynamic programming
(1)Optimization problems—0/1 knapsack problems demands max result
(2)Problem should be solved in sequence of decisions,
Yes for every object, we take a decision, include it or not
• DP- try all possible solutions and select the best one
4 objects: 0 0 0 0 ------------- not feasible
1 1 11--------------- not feasible
1 010-------------- feasible
• For 4 objects: 2n= 24= 16 possible solutions, time complexity is O(2n)
• Time consuming
• DP provides easy method for doing the same thing, not trying all possible
solutions, will be done indirectly.
Fill the table using formula and get the solution for the 0/1 knapsack problem.
Columns-capacity of bag, rows: objects
we will not consider whole capacity at once, will increase capacity one by one
initially, we will not consider any object
W- capacity of bag

0 1 2 3 4 5 6 7 8
Pi Wi O 0 0 0 0 0 0 0 0 0
1 2 1 0 0 1 1 1 1 1 1 1
2 3 2 0 0 1 2 2 3 3 3 3
5 4 3 0 0 1 2 5 5 6 7 7
6 5 4 0 0 1 2 5 6 6 7 8

Remaining profit=8-6= 2 X1 X2 X3 x4
Remaining profit=2-2= 0 0 1 0 1
Max profit of 0/1 knapsack problem

Call the table as V, i is the row number and w is the column no


V[i, W]= max { V[i-1, W], V [i-1, W- w[i] ] + P[i] }
V[4,6]= max { V[3, 6], V[3, 6-5]+6 } = max{ 6, 0+6}= max{6,6} =6

You might also like