{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec4Knapsack

# W 11 1 1 1 2 6 2 3 18 5 walk back through the table 4

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: of { 1, 2, …, i–1 } using weight limit W- wi and add vi: OPT(i- 1,W- wi)+vi " if i = 0 \$0 \$ OPT (i, W ) = # OPT (i − 1, W ) if wi > W \$ \$ max { OPT (i − 1, W ), vi + OPT (i − 1, W − wi ) } otherwise % Knapsack Problem: Bottom- Up Dynamic Programming !   Knapsack. Fill an n- by- W array. Input: n, W, w1,…,wN, v1,…,vN for w = 0 to W M[0, w] = 0 for i = 1 to n for w = 0 to W if wi > w : M[i, w] = M[i-1, w] else : M[i, w] = max (M[i-1, w], vi + M[i-1, w-wi ]) return M[n, W] Knapsack Algorithm W+1 0 2 3 4 5 6 7 8 9 10 11 φ 0 0 0 0 0 0 0 0 0 0 0 0 {1} n+1 1 0 1 1 1 1 1 1 1 1 1 1 1 { 1, 2 } 0 1 6 7 7 7 7 7 7 7 7 7 { 1, 2, 3 } 0 1 6 7 7 18 19 24 25 25 25 25 { 1, 2, 3, 4 } 0 1 6 7 7 18 22 24 28 29 29 40 { 1, 2, 3, 4, 5 } 0 1 6 7 7 18 22 28 29 34 34 40 OPT: 40 Item Value Weight How do we find the choice vector x, in other words the objects picked in the optimum solution? W = 11 1 1 1 2 6 2 3 18 5 Walk back through the table!! 4 22 6 5 28 7 Knapsack Algorithm W+1 0 2 3 4 5 6 7 8 9 10 11 φ 0 0 0 0 0 0 0 0 0 0 0 0 {1} n+1 1 0 1 1 1 1 1 1 1 1 1 1 1 { 1, 2 } 0 1 6 7 7 7 7 7 7 7 7 7 { 1, 2, 3 } 0 1 6 7 7 18 19 24 25 25 25 25 { 1, 2, 3, 4 } 0 1 6 7 7 18 22 24 28 29 29 40 { 1, 2, 3, 4, 5 } 0 1 6 7 7 18 22 28 29 34 34 40 OPT: 40 n=5 Don’t take object 5 Item Weight 1 1 1 2 W = 11 Value 6 2 3 18 5 4 22 6 5 28 7 Knapsack Algorithm W+1 0 2 3 4 5 6 7 8 9 10 11...
View Full Document

{[ snackBarMessage ]}