Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic...

11
CS Lunch Wednesday, 12:15 PM Kendade 307 RABBIT TRACKS An Animated film by Luke Jaegar! A journey through a mortality-infused landscape populated by mysterious chickens, inconvenient frogs, and other cartoonish creatures. 1 Midterm 2! Monday, April 8 In class Covers Greedy Algorithms and Dynamic Programming Closed book 2 Dynamic Programming “Recipe” Solve subproblems, remember the solutions in an array Gradually build up solution to bigger problems based on subproblem solutions 3 Slides15 - Dynamic Programming Intro.key - March 25, 2019

Transcript of Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic...

Page 1: Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic Programming Intro.key - March 25, 2019. Dynamic Programming Formula Divide a problem into

CS Lunch

Wednesday, 12:15 PMKendade 307

RABBIT TRACKSAn Animated film by Luke Jaegar!

A journey through a mortality-infused landscape populated by mysterious chickens, inconvenient

frogs, and other cartoonish creatures.

1

Midterm 2!

Monday, April 8

In class

Covers Greedy Algorithms and Dynamic Programming

Closed book

2

Dynamic Programming “Recipe”

Solve subproblems, remember the solutions in an array

Gradually build up solution to bigger problems based on subproblem solutions

3

Slides15 - Dynamic Programming Intro.key - March 25, 2019

Page 2: Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic Programming Intro.key - March 25, 2019. Dynamic Programming Formula Divide a problem into

Interval Scheduling (Yes, this is an old problem!)

Job j starts at sj and finishes at fj.Two jobs compatible if they don't overlap.Goal: find maximum subset of mutually compatible jobs.

Time0 1 2 3 4 5 6 7 8 9 10 11

f

g

h

e

b

c

d

a

4

Interval Scheduling: Greedy Solution

Sort jobs by earliest finish time.

Take each job provided it's compatible with the ones already taken.

Time0 1 2 3 4 5 6 7 8 9 10 11

f

g

h

e

a

b

c

d

5-1

Interval Scheduling: Greedy Solution

Sort jobs by earliest finish time.

Take each job provided it's compatible with the ones already taken.

Time0 1 2 3 4 5 6 7 8 9 10 11

f

g

h

e

a

b

c

d

b, e, h

5-2

Slides15 - Dynamic Programming Intro.key - March 25, 2019

Page 3: Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic Programming Intro.key - March 25, 2019. Dynamic Programming Formula Divide a problem into

Weighted Interval SchedulingJob 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.

Time0 1 2 3 4 5 6 7 8 9 10 11

4

3

1

3

3

2

4

1

6

Weighted Interval SchedulingLabel jobs by finishing time: f1 ≤ f2 ≤ . . . ≤ fn .p(j) = largest index i < j such that job i is compatible with j.Ex: p(8) = 5, p(7) = 5, p(2) = 0.

Time0 1 2 3 4 5 6 7 8 9 10 11

4

3

1

3

3

2

4

1

12345678

7

Dynamic Programming: Binary Choice

OPT(j) = value of optimal solution to the problem consisting of job requests 1, 2, ..., j.

Case 1: OPT selects job j.Case 2: OPT does not select job j.

8

Slides15 - Dynamic Programming Intro.key - March 25, 2019

Page 4: Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic Programming Intro.key - March 25, 2019. Dynamic Programming Formula Divide a problem into

If OPT selects job j...can't use incompatible jobs { p(j) + 1, p(j) + 2, ..., j - 1 }must include optimal solution to problem consisting of remaining compatible jobs 1, 2, ..., p(j)

Time0 1 2 3 4 5 6 7 8 9 10 11

5

2

1

3

3

2

4

1

12345678

9-1

If OPT selects job j...can't use incompatible jobs { p(j) + 1, p(j) + 2, ..., j - 1 }must include optimal solution to problem consisting of remaining compatible jobs 1, 2, ..., p(j)

Time0 1 2 3 4 5 6 7 8 9 10 11

5

2

1

3

3

2

4

1

12345678

p(j) + 1, p(j) + 2, ..., j - 1

9-2

If OPT does not select job j...must include optimal solution to problem consisting of remaining compatible jobs 1, 2, ..., j-1

Time0 1 2 3 4 5 6 7 8 9 10 11

4

3

1

3

3

2

4

1

12345678

10-1

Slides15 - Dynamic Programming Intro.key - March 25, 2019

Page 5: Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic Programming Intro.key - March 25, 2019. Dynamic Programming Formula Divide a problem into

If OPT does not select job j...must include optimal solution to problem consisting of remaining compatible jobs 1, 2, ..., j-1

Time0 1 2 3 4 5 6 7 8 9 10 11

4

3

1

3

3

2

4

1

12345678

10-2

Optimal Substructure

OPT( j) =0 if j = 0

max v j + OPT( p( j)), OPT( j −1){ } otherwise# $ %

OPT(j) = value of optimal solution to the problem consisting of job requests 1, 2, ..., j.

Case 1: OPT selects job j.Case 2: OPT does not select job j.

Case 1 Case 2

11

Input: n, s1,…,sn , f1,…,fn , v1,…,vn

Sort jobs by finish times so that f1 ≤ f2 ≤ ... ≤ fn.

Compute p(1), p(2), …, p(n)

Compute-Opt(j) { if (j = 0) return 0 else return max(vj + Compute-Opt(p(j)), Compute-Opt(j-1))}

Straightforward Recursive Algorithm12

Slides15 - Dynamic Programming Intro.key - March 25, 2019

Page 6: Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic Programming Intro.key - March 25, 2019. Dynamic Programming Formula Divide a problem into

Worst Case

3

4

5

1

2

p(1) = 0, p(j) = j-2

13-1

Worst Case

3

4

5

1

2

p(1) = 0, p(j) = j-2

5

13-2

Worst Case

3

4

5

1

2

p(1) = 0, p(j) = j-2

5

4 3

13-3

Slides15 - Dynamic Programming Intro.key - March 25, 2019

Page 7: Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic Programming Intro.key - March 25, 2019. Dynamic Programming Formula Divide a problem into

Worst Case

3

4

5

1

2

p(1) = 0, p(j) = j-2

5

4 3

3 2

13-4

Worst Case

3

4

5

1

2

p(1) = 0, p(j) = j-2

5

4 3

3 2 2 1

13-5

Worst Case

3

4

5

1

2

p(1) = 0, p(j) = j-2

5

4 3

3 2 2 1

2 1

13-6

Slides15 - Dynamic Programming Intro.key - March 25, 2019

Page 8: Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic Programming Intro.key - March 25, 2019. Dynamic Programming Formula Divide a problem into

Worst Case

3

4

5

1

2

p(1) = 0, p(j) = j-2

5

4 3

3 2 2 1

2 1 1 0

13-7

Worst Case

3

4

5

1

2

p(1) = 0, p(j) = j-2

5

4 3

3 2 2 1

2 1 1 0 1 0

13-8

Worst Case

3

4

5

1

2

p(1) = 0, p(j) = j-2

5

4 3

3 2 2 1

2 1

1 0

1 0 1 0

13-9

Slides15 - Dynamic Programming Intro.key - March 25, 2019

Page 9: Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic Programming Intro.key - March 25, 2019. Dynamic Programming Formula Divide a problem into

Sort jobs by finish times so that f1 ≤ f2 ≤ ... ≤ fn.Compute p(1), p(2), …, p(n)

for j = 1 to n M[j] = emptyM[0] = 0

M-Compute-Opt(j) { if (M[j] is empty) M[j] = max(wj + M-Compute-Opt(p(j)), M-Compute-Opt(j-1)) return M[j]}

MemoizationMemoization. Store results of each sub-problem in a cache; lookup as needed.

14

Memoization

Time0 1 2 3 4 5 6 7 8 9 10 11

4

3

1

3

3

2

4

1

12345678

M

M[8] = max(1 + M-Compute-Opt(5), M-Compute-Opt(7)

15

Memoization

3 3 4 4 6 8 9 9M

M[8] = max(1 + 6, 9)

Time0 1 2 3 4 5 6 7 8 9 10 11

4

3

1

3

3

2

4

1

12345678

16

Slides15 - Dynamic Programming Intro.key - March 25, 2019

Page 10: Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic Programming Intro.key - March 25, 2019. Dynamic Programming Formula Divide a problem into

Memoization

3 3 4 4 6 8 9 9M

Time0 1 2 3 4 5 6 7 8 9 10 11

4

3

1

3

3

2

4

1

12345678

17

Running TimeMemoized version of algorithm takes O(n log n) time.

Sort by finish time: O(n log n).Computing p(⋅) : O(n log n) due to sorting by start time.M-Compute-Opt(j): each invocation takes O(1) time and either(i) returns an existing value M[j] (ii) fills in one new entry M[j] and makes two recursive callsProgress measure Φ = # filled entries of M[].

initially Φ = 0, throughout Φ ≤ n. (ii) increases Φ by 1, making 2 recursive callsTo fill n squares, we make at 2n recursive calls.

Overall running time of M-Compute-Opt(n) is O(n).

18

Iterative SolutionBottom-up dynamic programming. Unwind recursion.

Sort jobs by finish times so that f1 ≤ f2 ≤ ... ≤ fn.

Compute p(1), p(2), …, p(n)

Iterative-Compute-Opt { M[0] = 0 for j = 1 to n M[j] = max(vj + M[p(j)], M[j-1])}

19

Slides15 - Dynamic Programming Intro.key - March 25, 2019

Page 11: Slides15 - Dynamic Programming Introblerner/cs312/Slides/Slides15.pdf · Slides15 - Dynamic Programming Intro.key - March 25, 2019. Dynamic Programming Formula Divide a problem into

Dynamic Programming Formula

Divide a problem into a polynomial number of smaller subproblems

Solve subproblem, recording its answer

Build up answer to bigger problem by using stored answers of smaller problems

20

http://imgs.xkcd.com/comics/travelling_salesman_problem.png

Traveling Salesman (sic) Problem

21

Slides15 - Dynamic Programming Intro.key - March 25, 2019