Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
126 views

0-1 Knapsack Problem - Dynamic Programming

The document discusses the 0/1 knapsack problem in dynamic programming. It describes the problem as selecting a subset of objects with weights and profits to maximize the total profit without exceeding the knapsack capacity. It presents an example with 4 objects, weights of 2, 3, 4, 5 and profits of 1, 2, 5, 5. The knapsack capacity is 8. It then outlines solving the problem using dynamic programming by filling a table to find the maximum profit at each capacity level. The table is used to determine the optimal solution is to select objects 1, 2, and 4 for a total profit of 8.

Uploaded by

Danyal zeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

0-1 Knapsack Problem - Dynamic Programming

The document discusses the 0/1 knapsack problem in dynamic programming. It describes the problem as selecting a subset of objects with weights and profits to maximize the total profit without exceeding the knapsack capacity. It presents an example with 4 objects, weights of 2, 3, 4, 5 and profits of 1, 2, 5, 5. The knapsack capacity is 8. It then outlines solving the problem using dynamic programming by filling a table to find the maximum profit at each capacity level. The table is used to determine the optimal solution is to select objects 1, 2, and 4 for a total profit of 8.

Uploaded by

Danyal zeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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