Knapsack problem using dynamic programming

15
KNAPSACK PROBLEM USING DYNAMIC PROGRAMMING BY: KHUSHBOO JETHWA ENROLLMENT NO. : 140950107028 DEPARTMENT : CSE-A BATCH: A1

Transcript of Knapsack problem using dynamic programming

Page 1: Knapsack problem using dynamic programming

KNAPSACK PROBLEM USING DYNAMIC PROGRAMMING

BY: KHUSHBOO JETHWAENROLLMENT NO. : 140950107028

DEPARTMENT : CSE-ABATCH: A1

Page 2: Knapsack problem using dynamic programming

0-1 KNAPSACK PROBLEM• Given weights and value of n items, put these items in a knapsack of

capacity W to get the maximum total value in the knapsack.

• In other words,• • Given: value[0….n-1],weight[0….n-1] and W.• Find: maximum value subset of value[0….n-1] such that sum of weights of

this subset is smaller than or equal to W.

• 0-1 Property: You cannot break an item, either pick it or don’t.

Page 3: Knapsack problem using dynamic programming

Basic example of 0-1 Knapsack Problem

Page 4: Knapsack problem using dynamic programming

Simple Solution

Page 5: Knapsack problem using dynamic programming

Formula • Let i be the highest-numbered item in an optimal

solution S for W pounds. Then S` = S - {i} is an optimal solution for W - wi pounds and the value to the solution S is Vi plus the value of the subproblem.

• We can express this fact in the following formula: define c[i, w] to be the solution for items 1,2, . . . , i and maximum weight w. Then

0 if i = 0 or w = 0c[i,w] = c[i-1, w] if wi ≥ 0

max [vi + c[i-1, w-wi], c[i-1, w]] if i>0 and w ≥ wi 

Page 6: Knapsack problem using dynamic programming

Explanation• This says that the value of the solution to i items either include ith item, in

which case it is vi plus a subproblem solution for (i - 1) items and the weight excluding wi, or does not include ith item, in which case it is a subproblem's solution for (i - 1) items and the same weight.

• The algorithm takes as input the maximum weight W, the number of items n, and the two sequences v = <v1, v2, . . . , vn> and w = <w1, w2, . . . , wn>. It stores the c[i, j]values in the table, that is, a two dimensional array, c[0 . . n, 0 . . w] whose entries are computed in a row-major order. That is, the first row of c is filled in from left to right, then the second row, and so on. At the end of the computation, c[n, w] contains the maximum value that can be picked into the knapsack.

Page 7: Knapsack problem using dynamic programming

Dynamic 0-1 knapsack(v,w,n,W)FOR w = 0 TO W DO c[0, w] = 0FOR i=1 to n DO c[i, 0] = 0 FOR w=1 TO W DO IFf wi ≤ w THEN IF vi + c[i-1, w-wi] THEN c[i, w] = vi + c[i-1, w-wi] ELSE c[i, w] = c[i-1, w] ELSE c[i, w] = c[i-1, w]

•The set of items to take can be deduced from the table, starting at c[n. w] and tracing backwards where the optimal values came from. If c[i, w] = c[i-1, w] item i is not part of the solution, and we are continue tracing with c[i-1, w]. Otherwise item i is part of the solution, and we continue tracing with c[i-1, w-W].

IF c[i,k] != c[i-1,k]{

then mark the ith article as 1k=k-wii=i-1

}Else{

mark the ith article as 0i=i-1

}

Page 8: Knapsack problem using dynamic programming

Analysis• This dynamic-0-1-kanpsack algorithm takes θ(nw) times, broken up as

follows: θ(nw) times to fill the c-table, which has (n +1).(w +1) entries, each requiring θ(1) time to compute. O(n) time to trace the solution, because the tracing process starts in row n of the table and moves up 1 row at each step.

Page 9: Knapsack problem using dynamic programming

Example• Find optimal solution for 0-1 Knapsack problem where n=3,

(w1,w2,w3)={2,3,3}, (p1,p2,p3)-{1,2,4}, m=6

Article Value Weight1 1 22 2 33 4 3

Page 10: Knapsack problem using dynamic programming

• c[1,1] w=1 wi=2 (wi>w)

c[0,1] = 0

• c[1,2] w=2 wi=2 (w=wi)

max{1+ c[0,0], c[0,2]}

max{1,0}= 1

• c[1,3] w=3 wi=2 (w>wi)

max{1+ c[0,1], c[0,3]}

max{1,0}= 1• c[1,4] w=4 wi=2 (w>wi)

max{1+ c[0,2], c[0,4]}

max{1,0}= 1

• c[1,5] w=5 wi=2 (w>wi)

max{1+ c[0,3], c[0,5]}

max{1,0}= 1

• c[1,6] w=6 wi=2 (w>wi)

max{1+ c[0,4], c[0,6]}

max{1,0}= 1

Page 11: Knapsack problem using dynamic programming

• c[2,1] w=1 wi=3 (wi>w)c[1,1] = 0

• c[2,2] w=2 wi=3 (wi>w)c[1,2] = 1

• c[2,3] w=3 wi=3 (w=wi)max{2+ c[1,0], c[1,3]}max{2,2}= 2

• c[2,4] w=4 wi=3 (w>wi)max{2+ c[1,1], c[1,4]}max{2,1}= 2

• c[2,5] w=5 wi=3 (w>wi)max{2+ c[1,2], c[1,5]}max{3,1}= 3

• c[2,6] w=6 wi=3 (w>wi)max{2+ c[1,3], c[1,6]}max{3,1}= 3

Page 12: Knapsack problem using dynamic programming

• c[3,1] w=1 wi=3 (wi>w)c[2,1] = 0

• c[3,2] w=2 wi=3 (wi>w)c[2,2] = 1

• c[3,3] w=3 wi=3 (w=wi)max{4+ c[2,0], c[2,3]}max{4,2}= 4

• c[3,4] w=4 wi=3 (w>wi)max{4+ c[2,1], c[2,4]}max{4,2}= 4

• c[3,5] w=5 wi=3 (w>wi)max{4+ c[2,2], c[2,5]}max{5,3}= 5

• c[3,6] w=6 wi=3 (w>wi)max{4+ c[2,3], c[2,6]}max{6,3}= 6

Page 13: Knapsack problem using dynamic programming

0 1 2 3 4 5 6

0 0 0 0 0 0 0 0

1 0 0 1 1 1 1 1

2 0 0 1 2 2 3 3

3 0 0 1 4 4 5 6

W(weight)i (

artic

le)

Page 14: Knapsack problem using dynamic programming

• Maximum value = c[3,6]=6 so compare it with c[2,6]=3 both are not equal so include article 3 FOR i=3

k=6-3=3i=3-1=2c[2,3]=2

Compare it with c[1,3]=1Both are not equal so include article 2FOR i=2

k=3-3=0i=2-1=1c[1,0]=0

So we can’t include article 1ANSWER: A1 A2 A3

0 1 1

Page 15: Knapsack problem using dynamic programming

THANK YOU