Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic programming vs Greedy algo – con’t

Similar presentations


Presentation on theme: "Dynamic programming vs Greedy algo – con’t"— Presentation transcript:

1 Dynamic programming vs Greedy algo – con’t
KNAPSACK KNAPSACK KNAPSACK KNAPSACK Input: Output: Objective: Input: Output: Objective: Input: Output: Objective: a number W and a set of n items, the i-th item has a weight wi and a cost ci a subset of items with total weight · W maximize cost a number W and a set of n items, the i-th item has a weight wi and a cost ci a subset of items with total weight · W maximize cost a number W and a set of n items, the i-th item has a weight wi and a cost ci a subset of items with total weight · W maximize cost a number W and a set of n items, the i-th item has a weight wi and a cost ci a subset of items with total weight · W maximize cost Version 1: Items are divisible. Version 1: Items are divisible.

2 KNAPSACK – divisible: a greedy solution
KNAPSACK-DIVISIBLE(n,c,w,W) sort items in decreasing order of ci/wi i = 1 currentW = 0 while (currentW + wi < W) { take item of weight wi and cost ci currentW += wi i++ } take W-currentW portion of item i Correctness: Running time:

3 KNAPSACK – indivisible
Version 2: Items are indivisible. Does previous algorithm work for this version of KNAPSACK?

4 KNAPSACK – indivisible: a dyn-prog solution
The heart of the algorithm: S[k][v] =

5 KNAPSACK – indivisible: a dyn-prog solution
The heart of the algorithm: S[k][v] = maximum cost of a subset of the first k items, where the weight of the subset is at most v

6 KNAPSACK – indivisible: a dyn-prog solution
The heart of the algorithm: S[k][v] = maximum cost of a subset of the first k items, where the weight of the subset is at most v KNAPSACK-INDIVISIBLE(n,c,w,W) init S[0][v]=0 for every v=0,…,W init S[k][0]=0 for every k=0,…,n 3. for v=1 to W do for k=1 to n do S[k][v] = S[k-1][v] if (wk · v) and (S[k-1][v-wk]+ck > S[k][v]) then S[k][v] = S[k-1][v-wk]+ck RETURN S[n][W]

7 KNAPSACK – indivisible: a dyn-prog solution
The heart of the algorithm: S[k][v] = maximum cost of a subset of the first k items, where the weight of the subset is at most v KNAPSACK-INDIVISIBLE(n,c,w,W) init S[0][v]=0 for every v=0,…,W init S[k][0]=0 for every k=0,…,n 3. for v=1 to W do for k=1 to n do S[k][v] = S[k-1][v] if (wk · v) and (S[k-1][v-wk]+ck > S[k][v]) then S[k][v] = S[k-1][v-wk]+ck RETURN S[n][W] Running time:

8 KNAPSACK – indivisible: a dyn-prog solution
The heart of the algorithm: S[k][v] = maximum cost of a subset of the first k items, where the weight of the subset is at most v KNAPSACK-INDIVISIBLE(n,c,w,W) init S[0][v]=0 for every v=0,…,W init S[k][0]=0 for every k=0,…,n 3. for v=1 to W do for k=1 to n do S[k][v] = S[k-1][v] if (wk · v) and (S[k-1][v-wk]+ck > S[k][v]) then S[k][v] = S[k-1][v-wk]+ck RETURN S[n][W] How to output a solution ?


Download ppt "Dynamic programming vs Greedy algo – con’t"

Similar presentations


Ads by Google