SLIDE 1 Visitor Candidate
Jaime Davila Hampshire College Tuesday, April 2, 4:30 PM Kendade 107
1
Alfred Spector
April 4, 7:00 PM: Data Science - Challenges & Opportunities, Gamble Auditorium April 5, CS lunch: Research Challenges in Computer Science, Kendade 307 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
2
Midterm 2!
Monday, April 8 In class Covers Greedy Algorithms and Divide and Conquer Closed book
3 Slides17 - Subset Sum.key - April 1, 2019
SLIDE 2 Dynamic Programming Formula
Divide a problem into a polynomial number
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
4
Weighted Interval Scheduling
Job j starts at sj, finishes at fj, and has weight or value vj . Two jobs compatible if they don't overlap. Goal: find maximum weight subset of mutually compatible jobs.
Time
1 2 3 4 5 6 7 8 9 10 11
4 3 1 3 3 2 4 1
5
Weighted Scheduling
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: wj + OPT(p(j)) Case 2: OPT(j-1) How do you find the optimal solution from the cases? max(…)
6 Slides17 - Subset Sum.key - April 1, 2019
SLIDE 3 Segmented Least Squares
Points lie roughly on a sequence of several line segments. Given n points in the plane (x1, y1), (x2, y2) , . . . , (xn, yn) with x1 < x2 < ... < xn, find a sequence of lines that fits well.
x y
7
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.
x y
8
Segmented Least Squares
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?
Segmented least squares for points i..j Different values for the starting point of the line segment that ends at point j e(i, j) + c + OPT(i-1) min over all values of i from 1 to j-1
9 Slides17 - Subset Sum.key - April 1, 2019
SLIDE 4
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 10
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-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?
The most we can spend considering items 1..j
11-2 Slides17 - Subset Sum.key - April 1, 2019
SLIDE 5
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?
The most we can spend considering items 1..j Case 1: Do not buy item j Case 2: Buy item j
11-3
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?
The most we can spend considering items 1..j Case 1: Do not buy item j Case 2: Buy item j Case 1: OPT(j-1) Case 2: ???
11-4
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?
The most we can spend considering items 1..j Case 1: Do not buy item j Case 2: Buy item j Case 1: OPT(j-1) Case 2: ??? max(…)
11-5 Slides17 - Subset Sum.key - April 1, 2019
SLIDE 6 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!
12
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-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)? OPT(j, W) = OPT(j-1, W)
13-2 Slides17 - Subset Sum.key - April 1, 2019
SLIDE 7
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)? OPT(j, W) = OPT(j-1, W) OPT(j, W) = wj + OPT(j-1, W - wj)
13-3
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 wi > 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], wi + M[i-1,w-wi] } } } }
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 wi > 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], wi + M[i-1,w-wi] } } } } Running time. O(n W). Polynomial in input size AND input value! "Pseudo-polynomial."
14-2 Slides17 - Subset Sum.key - April 1, 2019
SLIDE 8 Using the Algorithm
Candy $2 Burger $4 Soda $1 Pizza $6 1 2 3 4 5 6 7 8 Null Candy Burger Soda Pizza
$8 to spend
15
Using the Algorithm
Candy $2 Burger $4 Soda $1 Pizza $6 1 2 3 4 5 6 7 8 Null Candy 0
2 2 2 2 2 2 2
Burger Soda Pizza
$8 to spend
16
Using the Algorithm
Candy $2 Burger $4 Soda $1 Pizza $6 1 2 3 4 5 6 7 8 Null Candy 0
2 2 2 2 2 2 2
Burger 0
2 2 4 4 6 6 6
Soda Pizza
$8 to spend
17 Slides17 - Subset Sum.key - April 1, 2019
SLIDE 9 Using the Algorithm
Candy $2 Burger $4 Soda $1 Pizza $6 1 2 3 4 5 6 7 8 Null Candy 0
2 2 2 2 2 2 2
Burger 0
2 2 4 4 6 6 6
Soda
1 2 3 4 5 6 7 7
Pizza
$8 to spend
18
Using the Algorithm
Candy $2 Burger $4 Soda $1 Pizza $6 1 2 3 4 5 6 7 8 Null Candy 0
2 2 2 2 2 2 2
Burger 0
2 2 4 4 6 6 6
Soda
1 2 3 4 5 6 7 7
Pizza 0
1 2 3 4 5 6 7 8
$8 to spend
19
Using the Algorithm
Candy $2 Burger $4 Soda $1 Pizza $6 1 2 3 4 5 6 7 8 Candy 2 2 2 2 2 2 2 Burger 2 2 4 4 6 6 6 Soda 1 2 3 4 5 6 7 7 Pizza 1 2 3 4 5 6 7 8
$8 to spend
20-1 Slides17 - Subset Sum.key - April 1, 2019
SLIDE 10 Using the Algorithm
Candy $2 Burger $4 Soda $1 Pizza $6 1 2 3 4 5 6 7 8 Candy 2 2 2 2 2 2 2 Burger 2 2 4 4 6 6 6 Soda 1 2 3 4 5 6 7 7 Pizza 1 2 3 4 5 6 7 8
$8 to spend
20-2
Using the Algorithm
Candy $2 Burger $4 Soda $1 Pizza $6 1 2 3 4 5 6 7 8 Candy 2 2 2 2 2 2 2 Burger 2 2 4 4 6 6 6 Soda 1 2 3 4 5 6 7 7 Pizza 1 2 3 4 5 6 7 8
$8 to spend
20-3
Using the Algorithm
Candy $2 Burger $4 Soda $1 Pizza $6 1 2 3 4 5 6 7 8 Candy 2 2 2 2 2 2 2 Burger 2 2 4 4 6 6 6 Soda 1 2 3 4 5 6 7 7 Pizza 1 2 3 4 5 6 7 8
$8 to spend
20-4 Slides17 - Subset Sum.key - April 1, 2019
SLIDE 11 Using the Algorithm
Candy $2 Burger $4 Soda $1 Pizza $6 1 2 3 4 5 6 7 8 Candy 2 2 2 2 2 2 2 Burger 2 2 4 4 6 6 6 Soda 1 2 3 4 5 6 7 7 Pizza 1 2 3 4 5 6 7 8
$8 to spend
20-5
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
21 Slides17 - Subset Sum.key - April 1, 2019