Download presentation

Presentation is loading. Please wait.

Published byCarson Cartlidge Modified over 2 years ago

1
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 14 Dynamic Programming Knapsack Independent Set on grid exercise

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… 9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

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

4
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Knapsack Problem Given n objects and a "knapsack." - Item i weighs w i > 0 kilograms and has value v i > 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 1 value 18 22 28 1 weight 5 6 62 7 # 1 3 4 5 2 W = 11

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

6
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne 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!

7
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adding a new variable Definition: OPT(i, w) = max profit subset of items 1, …, i with 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 – w i OPT selects best of { 1, 2, …, i–1 } with new weight limit

8
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne 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

9
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Bottom-up algorithm Fill up an n-by-W array. Input: n, W, w 1,…,w N, v 1,…,v N for w = 0 to W M[0, w] = 0 for i = 1 to n for w = 1 to W if (w i > w) M[i, w] = M[i-1, w] else M[i, w] = max {M[i-1, w], v i + M[i-1, w-w i ]} return M[n, W]

10
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Knapsack table n 1 Value 18 22 28 1 Weight 5 6 62 7 Item 1 3 4 5 2 { 1, 2 } { 1, 2, 3 } { 1, 2, 3, 4 } { 1 } { 1, 2, 3, 4, 5 } 0 0 0 0 0 0 0 1 0 1 1 1 1 1 2 0 6 6 6 1 6 3 0 7 7 7 1 7 4 0 7 7 7 1 7 5 0 7 18 1 6 0 7 19 22 1 7 0 7 24 1 28 8 0 7 25 28 1 29 9 0 7 25 29 1 34 10 0 7 25 29 1 34 11 0 7 25 40 1 W W = 11 OPT: { 4, 3 } value = 22 + 18 = 40

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). 9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

12
About proofs Proof is a rigorous argument about a mathematical statement’s truth –Should convince you –Should not feel like a shot in the dark –What makes a proof “good enough” is social –(Truly rigorous, machine-readable proofs exist but are too painful for human use). In real life, you have to check your own work –Ideal: all problems should have grades 100% or 20% 9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne

13
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Time and Space Complexity Given n objects and a "knapsack." - Item i weighs w i > 0 kilograms and has value v i > 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? 1 value 18 22 28 1 weight 5 6 62 7 # 1 3 4 5 2 W = 11

14
9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne 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]

15
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? 9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne 8 8 3 3 7 7 10 7 7 2 2

16
Exercise Do the same with a 2 x n grid graph Three types of subproblems: –grid(i) –gridTop(i) –gridBottom(i) 9/27/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne 8 8 3 3 7 7 10 7 7 2 2 12 52 7 7 14 10 8 8 8 8 3 3 7 7 1010 1010 7 7 1212 1212 5252 5252 7 7 1414 1414 1010 1010 8 8 3 3 7 7 1010 1010 7 7 2 2 1212 1212 5252 5252 7 7 1414 1414 1010 1010 8 8 3 3 7 7 1010 1010 7 7 1212 1212 5252 5252 7 7 1414 1414 1010 1010 8 8

17
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 9/27/10 8 8 3 3 7 7 1010 1010 7 7 1212 1212 5252 5252 7 7 1414 1414 1010 1010 8 8 3 3 7 7 1010 1010 7 7 2 2 1212 1212 5252 5252 7 7 1414 1414 1010 1010 8 8 3 3 7 7 1010 1010 7 7 1212 1212 5252 5252 7 7 1414 1414 1010 1010 8 8 i pairs

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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google