Dynamic programming vs Greedy algo cont KNAPSACK KNAPSACK - - PowerPoint PPT Presentation

dynamic programming vs greedy algo con t
SMART_READER_LITE
LIVE PREVIEW

Dynamic programming vs Greedy algo cont KNAPSACK KNAPSACK - - PowerPoint PPT Presentation

Dynamic programming vs Greedy algo cont KNAPSACK KNAPSACK KNAPSACK KNAPSACK Input: a number W and a set of n items, the i-th item has a weight w i and a cost c i a subset of items with total weight W Output: Objective: maximize


slide-1
SLIDE 1

Dynamic programming vs Greedy algo – con’t

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 Version 1: Items are divisible. KNAPSACK KNAPSACK KNAPSACK KNAPSACK

slide-2
SLIDE 2

KNAPSACK – divisible: a greedy solution

KNAPSACK-DIVISIBLE(n,c,w,W)

  • 1. sort items in decreasing order of ci/wi
  • 2. i = 1
  • 3. currentW = 0
  • 4. while (currentW + wi < W) {
  • 5. take item of weight wi and cost ci
  • 6. currentW += wi
  • 7. i++
  • 8. }
  • 9. take W-currentW portion of item i

Correctness: Running time:

slide-3
SLIDE 3

KNAPSACK – indivisible

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

slide-4
SLIDE 4

KNAPSACK – indivisible: a dyn-prog solution

The heart of the algorithm: S[k][v] =

slide-5
SLIDE 5

KNAPSACK – indivisible: a dyn-prog solution

maximum cost of a subset of the first k items, where the weight of the subset is at most v The heart of the algorithm: S[k][v] =

slide-6
SLIDE 6

KNAPSACK – indivisible: a dyn-prog solution

maximum cost of a subset of the first k items, where the weight of the subset is at most v The heart of the algorithm: S[k][v] = KNAPSACK-INDIVISIBLE(n,c,w,W)

  • 1. init S[0][v]=0 for every v=0,…,W
  • 2. init S[k][0]=0 for every k=0,…,n
  • 3. for v=1 to W do
  • 4. for k=1 to n do
  • 5. S[k][v] = S[k-1][v]
  • 6. if (wk · v) and

(S[k-1][v-wk]+ck > S[k][v]) then

  • 7. S[k][v] = S[k-1][v-wk]+ck
  • 8. RETURN S[n][W]
slide-7
SLIDE 7

KNAPSACK – indivisible: a dyn-prog solution

maximum cost of a subset of the first k items, where the weight of the subset is at most v The heart of the algorithm: S[k][v] = Running time: KNAPSACK-INDIVISIBLE(n,c,w,W)

  • 1. init S[0][v]=0 for every v=0,…,W
  • 2. init S[k][0]=0 for every k=0,…,n
  • 3. for v=1 to W do
  • 4. for k=1 to n do
  • 5. S[k][v] = S[k-1][v]
  • 6. if (wk · v) and

(S[k-1][v-wk]+ck > S[k][v]) then

  • 7. S[k][v] = S[k-1][v-wk]+ck
  • 8. RETURN S[n][W]
slide-8
SLIDE 8

KNAPSACK – indivisible: a dyn-prog solution

maximum cost of a subset of the first k items, where the weight of the subset is at most v The heart of the algorithm: S[k][v] = How to

  • utput a

solution ? KNAPSACK-INDIVISIBLE(n,c,w,W)

  • 1. init S[0][v]=0 for every v=0,…,W
  • 2. init S[k][0]=0 for every k=0,…,n
  • 3. for v=1 to W do
  • 4. for k=1 to n do
  • 5. S[k][v] = S[k-1][v]
  • 6. if (wk · v) and

(S[k-1][v-wk]+ck > S[k][v]) then

  • 7. S[k][v] = S[k-1][v-wk]+ck
  • 8. RETURN S[n][W]