1 / 17

Algorithm Design and Analysis

L ECTURE 14 Dynamic Programming Knapsack Independent Set on grid exercise. Algorithm Design and Analysis. CSE 565. Adam Smith. Midterm 1. It’s on Thursday, 8:15pm in Willard 75. BYO coffee You can have one (1) double sided sheet of notes on colored paper

sheena
Download Presentation

Algorithm Design and Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LECTURE 14 • Dynamic Programming • Knapsack • Independent Set on grid exercise Algorithm Design and Analysis CSE 565 Adam Smith A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  2. Midterm 1 • It’s on Thursday, 8:15pm in Willard 75. • BYO coffee • You can have one (1) double sided sheet of notes on colored paper • Everything up to and including today’s lecture is fair game • Sample exams from previous years are on the web. The sample “midterm 2” exams contain questions on material we haven’t yet covered • Network flow, Randomization, … • I will try to post (all?) homework solutions… A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  3. Knapsack A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  4. Knapsack Problem • Given n objects and a "knapsack." • Item i weighs wi> 0 kilograms and has value vi > 0. • Knapsack has capacity of W kilograms. • Goal: fill knapsack so as to maximize total value. • Ex: { 3, 4 } has value 40. • Many “packing” problems fit this model • Assigning production jobs to factories • Deciding which jobs to do on a single processor with bounded time • Deciding which problems to do on an exam W = 11 # value weight 1 1 1 2 6 2 3 18 5 4 22 6 5 28 7 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  5. Knapsack Problem • Given n objects and a "knapsack." • Item i weighs wi> 0 kilograms and has value vi > 0. • Knapsack has capacity of W kilograms. • Goal: fill knapsack so as to maximize total value. • Ex: { 3, 4 } has value 40. W = 11 • Greedy: repeatedly add item with maximum ratio vi / wi • Example: { 5, 2, 1 } achieves only value = 35  greedy not optimal. # value weight 1 1 1 2 6 2 3 18 5 4 22 6 5 28 7 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  6. Dynamic programming: attempt 1 • Definition:OPT(i) = maximum profit subset of items 1, …, i. • Case 1:OPT does not select item i. • OPT selects best of { 1, 2, …, i-1 } • Case 2:OPT selects item i. • without knowing what other items were selected before i,we don't even know if we have enough room for i • Conclusion. Need more sub-problems! A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  7. Adding a new variable • Definition:OPT(i, w) = max profit subset of items 1, …, iwith weight limit w. • Case 1:OPT does not select item i. • OPT selects best of { 1, 2, …, i-1 } with weight limit w • Case 2:OPT selects item i. • new weight limit = w – wi • OPT selects best of { 1, 2, …, i–1 } with new weight limit A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  8. Adding a new variable • How many different possible inputs for the recursive procedure? • n choices for the length i of the list • W choices for the weight limit • Fill a table with n× W entries • Need entries for i-1 before we fill entries for i A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  9. Bottom-up algorithm • Fill up an n-by-W array. Input: n, W, w1,…,wN, v1,…,vN forw = 0 to W M[0, w] = 0 fori = 1 to n forw = 1 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]} returnM[n, W] A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  10. 0 1 2 3 4 5 6 7 8 9 10 11  0 0 0 0 0 0 0 0 0 0 0 0 { 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 Item Value Weight 1 1 1 2 6 2 3 18 5 4 22 6 5 28 7 Knapsack table W n W = 11 OPT: { 4, 3 } value = 22 + 18 = 40 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  11. How do we make turn this into a proof of correctness? • Dynamic programming (and divide and conquer) lends itself directly to induction. • Base cases: OPT(i,0)=0, OPT(0,w)=0 (no items!). • Inductive step: explaining the correctness of recursive formula • If the following values are correctly computed: • OPT(0,w-1),OPT(1,w-1),…,OPT(n,w-1) • OPT(0,w),…,OPT(i-1,w) • Then the recursive formula computes OPT(i,w) correctly • Case 1: …, Case 2: … (from previous slide). A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  12. Time and Space Complexity • Given n objects and a "knapsack." • Item i weighs wi> 0 kilograms and has value vi > 0. • Knapsack has capacity of W kilograms. • Goal: fill knapsack so as to maximize total value. What is the input size? • In words? • In bits? W = 11 # value weight 1 1 1 2 6 2 3 18 5 4 22 6 5 28 7 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  13. Time and space complexity • Time and space:(n W). • Not polynomial in input size! • "Pseudo-polynomial." • Decision version of Knapsack is NP-complete. [KT, chapter 8] • Knapsack approximation algorithm. There is a poly-time algorithm that produces a solution with value within 0.01% of optimum. [KT, section 11.8] A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  14. Review • Weighted independent set on the chain • Input: a chain graph of length n with values v1,..,vn • Goal: find a heaviest independent set • Let OPT({1,…,n}) = heaviest IS among {1,…,n} • Write down a recursive formula for OPT • How many different subproblems? 8 3 7 10 7 2 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  15. Exercise • Do the same with a 2 xn grid graph • Three types of subproblems: • grid(i) • gridTop(i) • gridBottom(i) 8 3 7 10 7 2 12 52 7 14 10 8 8 3 7 10 7 12 52 7 14 10 8 3 7 10 7 2 12 52 7 14 10 8 3 7 10 7 12 52 7 14 10 8 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

  16. Exercise • grid(i): maximum independent set in the subgraph consisting of only the first i pairs of nodes • gridTop(i): maximum independent set in the subgraph consisting of the first i pairs of nodes plus the top node of the (i+1)-st pair • gridBottom(i): maximum independent set in the subgraph consisting of the first i pairs of nodes plus the bottom node of the (i+1)-st pair 8 3 7 10 7 12 52 7 14 10 i pairs 8 3 7 10 7 2 12 52 7 14 10 i pairs 8 3 7 10 7 12 52 7 14 10 8 i pairs

  17. Recursive formulas for subproblems • Say ti, bi are the weights of the nodes in the ith pair • grid(i) = max(bi,ti) if i=1, else max(gridBottom(i-1), ti+ gridBottom(i-2)) • gridBottom(i) = bi if i=0, else max(grid(i-1), bi+ gridTop(i-1) • gridTop(i) = ti if i=0, else max(grid(i-1), ti + gridBottom(i-1) Bottom-up algorithm takes O(n) time. A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

More Related