1 Visitor Candidate Jaime Davila Hampshire College Tuesday, April 2, 4:30 PM Kendade 107 2 Alfred Spector CTO at Two Sigma Led Google Research for 8 years Led IBM Software Research for 5 years Fellow of ACM and IEEE Member of the National Academy of Engineering Co-recipient of the 2016 ACM Software Systems Award for the Andrew File System April 4, 7:00 PM: Data Science - Challenges & Opportunities, Gamble Auditorium April 5, CS lunch: Research Challenges in Computer Science, Kendade 307 3 Midterm 2! Monday, April 8 In class Covers Greedy Algorithms and Divide and Conquer Closed book Slides17 - Subset Sum.key - April 1, 2019
4 Dynamic Programming Formula Divide a problem into a polynomial number of smaller subproblems Solve subproblem, recording its answer in an array Do a case analysis where each case uses the subproblems in a different way Compare the cases to find the optimal solution for the current problem 5 Weighted Interval Scheduling Job j starts at s j , finishes at f j , and has weight or value v j . Two jobs compatible if they don't overlap. Goal: find maximum weight subset of mutually compatible jobs. 3 2 4 1 3 4 3 1 Time 0 1 2 3 4 5 6 7 8 9 10 11 Weighted Scheduling 6 What are the subproblems? Maximum weight for jobs 1..j What are the cases? Case 1: Job j is in the optimal solution Case 2: Job j is not in the optimal solution What is the solution for each case? Case 1: w j + OPT(p(j)) Case 2: OPT(j-1) How do you find the optimal solution from the cases? max(…) Slides17 - Subset Sum.key - April 1, 2019
7 Segmented Least Squares Points lie roughly on a sequence of several line segments. Given n points in the plane (x 1 , y 1 ), (x 2 , y 2 ) , . . . , (x n , y n ) with x 1 < x 2 < ... < x n , find a sequence of lines that fits well. y x 8 Segmented Least Squares Find a sequence of lines that minimizes: the errors E in each segment the number of lines L Tradeoff function: E + c L, for some constant c > 0. y x 9 Segmented Least Squares What are the subproblems? Segmented least squares for points i..j What are the cases? Different values for the starting point of the line segment that ends at point j What is the solution for each case? e(i, j) + c + OPT(i-1) How do you find the optimal solution from the cases? min over all values of i from 1 to j-1 Slides17 - Subset Sum.key - April 1, 2019
10 The Price is Right! (or shopping with somebody else’ s money) Spend as much money as possible without going over $100. You can buy at most 1 of each. CD $11 Jeans $33 DVD $20 Dinner $15 Book $13 Ice cream $5 Shoes $70 Pizza $7 11-1 Subset sum What are the subproblems? What are the cases? What is the solution for each case? How do you find the optimal solution from the cases? 11-2 Subset sum What are the subproblems? The most we can spend considering items 1..j What are the cases? What is the solution for each case? How do you find the optimal solution from the cases? Slides17 - Subset Sum.key - April 1, 2019
11-3 Subset sum What are the subproblems? The most we can spend considering items 1..j What are the cases? Case 1: Do not buy item j Case 2: Buy item j What is the solution for each case? How do you find the optimal solution from the cases? 11-4 Subset sum What are the subproblems? The most we can spend considering items 1..j What are the cases? Case 1: Do not buy item j Case 2: Buy item j What is the solution for each case? Case 1: OPT(j-1) Case 2: ??? How do you find the optimal solution from the cases? 11-5 Subset sum What are the subproblems? The most we can spend considering items 1..j What are the cases? Case 1: Do not buy item j Case 2: Buy item j What is the solution for each case? Case 1: OPT(j-1) Case 2: ??? How do you find the optimal solution from the cases? max(…) Slides17 - Subset Sum.key - April 1, 2019
12 Dynamic Programming: False Start OPT(i) = max value 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. accepting item i does not immediately imply that we will have to reject other items 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! 13-1 Defining OPT 2: Adding a Variable Define OPT(j, W) to be the maximum amount we can spend considering the first j items and not exceeding amount W . If OPT(j, W) does not include item j, how do we calculate OPT(j, W)? If OPT(j) includes item j, how do we calculate OPT(j)? 13-2 Defining OPT 2: Adding a Variable Define OPT(j, W) to be the maximum amount we can spend considering the first j items and not exceeding amount W . If OPT(j, W) does not include item j, how do we calculate OPT(j, W)? OPT(j, W) = OPT(j-1, W) If OPT(j) includes item j, how do we calculate OPT(j)? Slides17 - Subset Sum.key - April 1, 2019
13-3 Defining OPT 2: Adding a Variable Define OPT(j, W) to be the maximum amount we can spend considering the first j items and not exceeding amount W . If OPT(j, W) does not include item j, how do we calculate OPT(j, W)? OPT(j, W) = OPT(j-1, W) If OPT(j) includes item j, how do we calculate OPT(j)? OPT(j, W) = w j + OPT(j-1, W - w j ) 14-1 Algorithm Subset-Sum (n, w) { initialize M[0, w] = 0 for w = 0, 1, ..., W for i = 1..n { / / items for w = 0..W { / / money if w i > w { / / can’ t afford M[i,w] = M[i-1,w] } else { / / compare not buying, with buying and / / remaining money M[i,w] = max(M[i-1,w], w i + M[i-1,w-w i ] } } } } 14-2 Algorithm Subset-Sum (n, w) { initialize M[0, w] = 0 for w = 0, 1, ..., W for i = 1..n { / / items for w = 0..W { / / money if w i > w { / / can’ t afford M[i,w] = M[i-1,w] } else { / / compare not buying, with buying and / / remaining money M[i,w] = max(M[i-1,w], w i + M[i-1,w-w i ] } Running time. O (n W). } Polynomial in input size AND input value! } "Pseudo-polynomial." } Slides17 - Subset Sum.key - April 1, 2019
15 Using the Algorithm Candy $2 Burger $4 0 1 2 3 4 5 6 7 8 Soda $1 Pizza $6 Null 0 0 0 0 0 0 0 0 0 $8 to Candy spend Burger Soda Pizza 16 Using the Algorithm Candy $2 Burger $4 0 1 2 3 4 5 6 7 8 Soda $1 Pizza $6 Null 0 0 0 0 0 0 0 0 0 Candy 0 $8 to 0 2 2 2 2 2 2 2 spend Burger Soda Pizza 17 Using the Algorithm Candy $2 Burger $4 0 1 2 3 4 5 6 7 8 Soda $1 Pizza $6 Null 0 0 0 0 0 0 0 0 0 Candy 0 $8 to 0 2 2 2 2 2 2 2 spend Burger 0 0 2 2 4 4 6 6 6 Soda Pizza Slides17 - Subset Sum.key - April 1, 2019
18 Using the Algorithm Candy $2 Burger $4 0 1 2 3 4 5 6 7 8 Soda $1 Pizza $6 Null 0 0 0 0 0 0 0 0 0 Candy 0 $8 to 0 2 2 2 2 2 2 2 spend Burger 0 0 2 2 4 4 6 6 6 0 1 2 3 4 5 6 7 7 Soda Pizza 19 Using the Algorithm Candy $2 Burger $4 0 1 2 3 4 5 6 7 8 Soda $1 Pizza $6 Null 0 0 0 0 0 0 0 0 0 Candy 0 $8 to 0 2 2 2 2 2 2 2 spend Burger 0 0 2 2 4 4 6 6 6 0 1 2 3 4 5 6 7 7 Soda Pizza 0 1 2 3 4 5 6 7 8 20-1 Using the Algorithm Candy $2 Burger $4 0 1 2 3 4 5 6 7 8 Soda $1 Pizza $6 Candy 0 0 2 2 2 2 2 2 2 $8 to Burger 0 0 2 2 4 4 6 6 6 spend Soda 0 1 2 3 4 5 6 7 7 Pizza 0 1 2 3 4 5 6 7 8 Slides17 - Subset Sum.key - April 1, 2019
20-2 Using the Algorithm Candy $2 Burger $4 0 1 2 3 4 5 6 7 8 Soda $1 Pizza $6 Candy 0 0 2 2 2 2 2 2 2 $8 to Burger 0 0 2 2 4 4 6 6 6 spend Soda 0 1 2 3 4 5 6 7 7 Pizza 0 1 2 3 4 5 6 7 8 20-3 Using the Algorithm Candy $2 Burger $4 0 1 2 3 4 5 6 7 8 Soda $1 Pizza $6 Candy 0 0 2 2 2 2 2 2 2 $8 to Burger 0 0 2 2 4 4 6 6 6 spend Soda 0 1 2 3 4 5 6 7 7 Pizza 0 1 2 3 4 5 6 7 8 20-4 Using the Algorithm Candy $2 Burger $4 0 1 2 3 4 5 6 7 8 Soda $1 Pizza $6 Candy 0 0 2 2 2 2 2 2 2 $8 to Burger 0 0 2 2 4 4 6 6 6 spend Soda 0 1 2 3 4 5 6 7 7 Pizza 0 1 2 3 4 5 6 7 8 Slides17 - Subset Sum.key - April 1, 2019
20-5 Using the Algorithm Candy $2 Burger $4 0 1 2 3 4 5 6 7 8 Soda $1 Pizza $6 Candy 0 0 2 2 2 2 2 2 2 $8 to Burger 0 0 2 2 4 4 6 6 6 spend Soda 0 1 2 3 4 5 6 7 7 Pizza 0 1 2 3 4 5 6 7 8 21 Dynamic Programming Formula Divide a problem into a polynomial number of smaller subproblems We often think recursively to identify the subproblems Solve subproblem, recording its answer Build up answer to bigger problem by using stored answers of smaller problems We develop algorithm that builds up the answer iteratively Slides17 - Subset Sum.key - April 1, 2019
Recommend
More recommend