algorithm design and analysis

26
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis LECTURE 13 Dynamic Programming Fibonacci Weighted Interval Scheduling

Upload: caldwell-humphrey

Post on 14-Mar-2016

68 views

Category:

Documents


0 download

DESCRIPTION

L ECTURE 13 Dynamic Programming Fibonacci Weighted Interval Scheduling. Algorithm Design and Analysis. CSE 565. Adam Smith. Design Techniques So Far. Greedy . Build up a solution incrementally, myopically optimizing some local criterion. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Algorithm Design and Analysis

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

Adam Smith

Algorithm Design and Analysis

LECTURE 13Dynamic Programming• Fibonacci• Weighted Interval

Scheduling

Page 2: Algorithm Design and Analysis

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

Design Techniques So Far• Greedy. Build up a solution incrementally,

myopically optimizing some local criterion.• Recursion / divide &conquer. Break up a

problem into subproblems, solve subproblems, and combine solutions.

• Dynamic programming. Break problem into overlapping subproblems, and build up solutions to larger and larger sub-problems.

9/10/10

Page 3: Algorithm Design and Analysis

Dynamic Programming History

Bellman. [1950s] Pioneered the systematic study of dynamic programming.

Etymology. Dynamic programming = planning over time. Secretary of Defense was hostile to mathematical research. Bellman sought an impressive name to avoid confrontation.

Reference: Bellman, R. E. Eye of the Hurricane, An Autobiography.

"it's impossible to use dynamic in a pejorative sense""something not even a Congressman could object to"

Page 4: Algorithm Design and Analysis

Dynamic Programming Applications

Areas. Bioinformatics. Control theory. Information theory. Operations research. Computer science: theory, graphics, AI, compilers, systems,

….

Some famous dynamic programming algorithms. Unix diff for comparing two files. Viterbi for hidden Markov models / decoding convolutional

codes Smith-Waterman for genetic sequence alignment. Bellman-Ford for shortest path routing in networks. Cocke-Kasami-Younger for parsing context free grammars.

Page 5: Algorithm Design and Analysis

Fibonacci Sequence

Sequence defined by • a1 = 1 • a2 = 1• an = an-1 + an-2

How should you compute the Fibonacci sequence?

Recursive algorithm:

Running Time?

1, 1, 3, 5, 8, 13, 21, 34, …

Fib(n)1. If n =1 or n=2, then 2. return 13. Else4. a = Fib(n-1)5. b = Fib(n-2)6. return a+b

Page 6: Algorithm Design and Analysis

Computing Fibonacci Sequence Faster

Observation: Lots of redundancy! The recursive algorithm only solves n-1 different sub-problems

“Memoization”: Store the values returned by recursive calls in a sub-table

Resulting Algorithm:

Linear time, if integer operations take constant time

Fib(n)1. If n =1 or n=2, then 2. return 13. Else4. f[1]=1; f[2]=15. For i=3 to n6. f[i] f[i-1]+f[i-2]7. return f[n]

Page 7: Algorithm Design and Analysis

Computing Fibonacci Sequence Faster

Observation: Fibonacci recurrence is linear

Can compute An using only O(log n) matrix multiplications; each one takes O(1) integer multiplications and additions.

Total running time? O(log n) integer operations. Exponential improvement!

Exercise: how big an improvement if we count bit operations? Multiplying k-bit numbers takes O(k log k) time.

How many bits needed to write down an?

Page 8: Algorithm Design and Analysis

Weighted Interval Scheduling

Weighted interval scheduling problem. 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

f

g

h

e

a

b

c

d

0 1 2 3 4 5 6 7 8 9 10

Page 9: Algorithm Design and Analysis

Unweighted Interval Scheduling Review

Recall. Greedy algorithm works if all weights are 1. Consider jobs in ascending order of finish time. Add job to subset if it is compatible with previously chosen

jobs.

Observation. Greedy algorithm can fail spectacularly with weights

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

b

a

weight = 999

weight = 1

Page 10: Algorithm Design and Analysis

Weighted Interval Scheduling

Notation. Label jobs by finishing time: f1 f2 . . . fn .Def. p(j) = largest index i < j such that job i is compatible with j. = largest i such that fi sj

Ex: p(8) = 5, p(7) = 3, p(2) = 0.

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

6

7

8

4

3

1

2

5

Page 11: Algorithm Design and Analysis

Dynamic Programming: Binary Choice

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

Case 1: OPT selects job j.– collect profit vj– 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)

Case 2: OPT does not select job j.– must include optimal solution to problem consisting of

remaining compatible jobs 1, 2, ..., j-1

OPT( j) 0 if j0

max v j OPT( p( j)), OPT( j 1) otherwise

optimal substructure

Page 12: Algorithm Design and Analysis

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))}

Weighted Interval Scheduling: Brute Force

Brute force algorithm.

Worst case:T(n) = T(n-1) + T(n-2) + (…)

Page 13: Algorithm Design and Analysis

Memoization. Store results of each sub-problem in a table;lookup as needed.

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)

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]}

global array

Weighted Interval Scheduling: Memoization

Page 14: Algorithm Design and Analysis

Weighted Interval Scheduling: Running Time

Claim. Memoized version of algorithm takes O(n log n) time. Sort by finish time: O(n log n). Computing p() : O(n log n) via repeated binary search.

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 calls

Case (ii) occurs at most n times at most 2n recursive calls overall

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

Remark. O(n) if jobs are pre-sorted by start and finish times.

Page 15: Algorithm Design and Analysis

Equivalent algorithm: Bottom-Up

Bottom-up dynamic programming. Unwind recursion.

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)

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

how fast?

Total time = (sorting) + O(n) = O(n log(n))

Page 16: Algorithm Design and Analysis

Weighted Interval Scheduling: Finding a Solution

Q. Dynamic programming algorithms computes optimal value.What if we want the solution itself?A. Do some post-processing.

# of recursive calls n O(n).

Run M-Compute-Opt(n)Run Find-Solution(n)

Find-Solution(j) { if (j = 0) output nothing else if (vj + M[p(j)] > M[j-1]) print j Find-Solution(p(j)) else Find-Solution(j-1)}

Page 17: Algorithm Design and Analysis

A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne10/8/2008

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

1

value

18

22

28

1

weight

5

6

6 2

7

#

1

3

4

5

2

W = 11

Page 18: Algorithm Design and Analysis

A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne10/8/2008

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.

1

value

18

22

28

1

weight

5

6

6 2

7

#

1

3

4

5

2

W = 11

• Greedy: repeatedly add item with maximum ratio vi / wi

• Example: { 5, 2, 1 } achieves only value = 35 greedy not optimal.

Page 19: Algorithm Design and Analysis

A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne10/8/2008

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.

1

value

18

22

28

1

weight

5

6

6 2

7

#

1

3

4

5

2

W = 11

• Greedy algorithm?

Page 20: Algorithm Design and Analysis

A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne10/8/2008

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!

Page 21: Algorithm Design and Analysis

A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne10/8/2008

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 – wi

• OPT selects best of { 1, 2, …, i–1 } with new weight limit

OPT (i, w) 0 if i 0

OPT (i 1, w) if wi wmax OPT (i 1, w), vi OPT (i 1, w wi ) otherwise

Page 22: Algorithm Design and Analysis

A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne10/8/2008

Bottom-up algorithm•Fill up an n-by-W array.

Input: n, W, w1,…,wN, v1,…,vN

for w = 0 to W M[0, w] = 0

for i = 1 to n for w = 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 ]}

return M[n, W]

Page 23: Algorithm Design and Analysis

A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne10/8/2008

Knapsack table

n

1Value

182228

1Weight

56

6 2

7

Item1

345

2

{ 1, 2 }{ 1, 2, 3 }

{ 1, 2, 3, 4 }

{ 1 }

{ 1, 2, 3, 4, 5 }

00

000

0

0

10

111

1

1

20

666

1

6

30

777

1

7

40

777

1

7

50

71818

1

18

60

71922

1

22

70

72424

1

28

80

72528

1

29

90

72529

1

34

100

72529

1

34

110

72540

1

40

W

W = 11

OPT: { 4, 3 }value = 22 + 18 = 40

Page 24: Algorithm Design and Analysis

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

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).

10/8/2008

Page 25: Algorithm Design and Analysis

A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne10/8/2008

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?

1

value

18

22

28

1

weight

5

6

6 2

7

#

1

3

4

5

2

W = 11

Page 26: Algorithm Design and Analysis

A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne10/8/2008

Time and space complexity•Time and space: (n W).–Not polynomial in input size!–Space can be made O(W) (exercise!)–"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]