28
CS3000: Algorithms & Data Jonathan Ullman Lecture 5: Dynamic Programming: Fibonacci Numbers, Interval Scheduling Jan 22, 2020

CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

CS3000:Algorithms&DataJonathanUllman

Lecture5:• DynamicProgramming:

FibonacciNumbers,IntervalScheduling

Jan22,2020

Page 2: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

DynamicProgramming

• Don’tthinktoohardaboutthename• Ithoughtdynamicprogrammingwasagoodname.Itwassomethingnotevenacongressmancouldobjectto.SoIuseditasanumbrellaformyactivities. -Bellman

• Dynamicprogrammingiscarefulrecursion• Breaktheproblemupintosmallpieces• Recursivelysolvethesmallerpieces• KeyChallenge:identifyingthepiecesE

Page 3: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

Warmup:FibonacciNumbers

Page 4: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

FibonacciNumbers

• 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,…• * + = * + − 1 + * + − 2

• * + → 01 ≈ 1.621

• 0 = 56 7�9 isthegoldenratio

f NflD

Page 5: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

FibonacciNumbers:TakeI

• HowmanyrecursivecallsdoesFibI(n)make?

FibI(n):If (n = 0): return 0ElseIf (n = 1): return 1Else: return FibI(n-1) + FibI(n-2)

n of calls made by FibI n

yn

Clm Un n ich 4tf tf

Chi Foth 1.62r r f

Page 6: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

FibonacciNumbers:TakeII

• HowmanyrecursivecallsdoesFibII(n)make?

M ← empty array, M[0]←0, M[1]←1FibII(n):If (M[n] is not empty): return M[n]ElseIf (M[n] is empty): M[n] ← FibII(n-1) + FibII(n-2) return M[n]

Memoication Top Down DynamicProgramming

bICo 12

We fill n l new elements of M

Each pair of recursive calls fills one elf

At most 21h D callsOln

Page 7: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

FibonacciNumbers:TakeIII

• WhatistherunningtimeofFibIII(n)?

FibIII(n):M[0] ← 0, M[1] ← 1For i = 2,…,n:M[i]← M[i-1] + M[i-2]

return M[n]

M DODD BB FEED

Bottom Up Dynamic Programming

Page 8: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

FibonacciNumbers

• 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,…• * + = * + − 1 + * + − 2

• Solvingtherecurrencerecursivelytakes≈ 1.621 time• Problem:Recomputethesamevalues* < manytimes

• Twowaystoimprovetherunningtime• Remembervaluesyou’vealreadycomputed(“topdown”)• Iterateoverallvalues* < (“bottomup”)

• Fact: CansolveevenfasterusingKaratsuba’salgorithm!

Page 9: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

DynamicProgramming:IntervalScheduling

Page 10: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

IntervalScheduling

• Howcanweoptimallyschedulearesource?• Thisclassroom,acomputingcluster,…

• Input:+ intervals =>, ?> eachwithvalue@>• Assumeintervalsaresortedso?5 < ?9 < ⋯ < ?1

• Output:acompatiblescheduleC maximizingthetotalvalueofallintervals• Aschedule isasubsetofintervalsC ⊆ {1,… , +}• AscheduleC is compatible ifno<, G ∈ C overlap• Thetotalvalue ofC is∑ @>�

>∈J

O

Page 11: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

IntervalSchedulingvalue El 3,53 2 4 2 8

O inde

i

corea oii

0 i

Page 12: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

PossibleAlgorithms

• Chooseintervalsindecreasingorderof@>

Page 13: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

PossibleAlgorithms

• Chooseintervalsinincreasingorderof=>

Page 14: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

PossibleAlgorithms

• Chooseintervalsinincreasingorderof?> − =>

Page 15: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

ARecursiveFormulation

• LetK betheoptimalschedule• BoldStatement:K eithercontainsthelastintervaloritdoesnot.

Page 16: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

ARecursiveFormulation

• LetK betheoptimalschedule• Case1: FinalintervalisnotinK (i.e.6 ∉ K)

The 0 mustbe the optimal schedule for El 2,3 4,53

111111111

Page 17: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

ARecursiveFormulation

• LetK betheoptimalschedule• Case2:Final intervalisinK (i.e.6 ∈ K)O must be 63 theoptimal schedule for 9112,33

1111111111 11 111411411111

O

Page 18: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

ARecursiveFormulation

• LetK> betheoptimalschedule usingonlytheintervals 1,… , <• Case1:Final intervalisnotinK (< ∉ K)

• ThenK mustbetheoptimalsolutionfor 1,… , < − 1• Case2:FinalintervalisinK (< ∈ K)

• Assumeintervalsaresortedsothat?5 < ?9 < ⋯ < ?1• LetM < bethelargestG suchthat?N < =>• ThenK mustbe< +theoptimalsolutionfor 1,… , M <

Oi Oi

Oi EstOpe

Anyof 9,2 plil are compatiblewith i t.io

i TeiD

If vitvalve Open value Oi 1then Oi i3 Open

Page 19: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

ARecursiveFormulation

• LetKOP(<) bethevalueoftheoptimalscheduleusingonlytheintervals 1,… , <• Case1:Final intervalisnotinK (< ∉ K)

• ThenK mustbetheoptimalsolutionfor 1,… , < − 1• Case2:FinalintervalisinK (< ∈ K)

• Assumeintervalsaresortedsothat?5 < ?9 < ⋯ < ?1• LetM < bethelargestG suchthat?N < =>• ThenK mustbe< +theoptimalsolutionfor 1,… , M <

• KOP < = max KOP < − 1 , @1 + KOP M <• KOP 0 = 0, KOP 1 = @5

a

Page 20: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

IntervalScheduling:TakeI

• WhatistherunningtimeofFindOPT(n)?

// All inputs are global varsFindOPT(n):if (n = 0): return 0elseif (n = 1): return v1else:return max{FindOPT(n-1), vn + FindOPT(p(n))}

Can beexponential in n

Page 21: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

IntervalScheduling:TakeII

• WhatistherunningtimeofFindOPT(n)?

// All inputs are global varsM ← empty array, M[0]←0, M[1]←v1FindOPT(n):if (M[n] is not empty): return M[n]else:M[n] ← max{FindOPT(n-1), vn + FindOPT(p(n))}return M[n]

Top DownMEi stores OPT i

0 n 2 recursive calls array eH

x n 1 arrayelts

Page 22: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

IntervalScheduling:TakeIII

• WhatistherunningtimeofFindOPT(n)?

// All inputs are global varsFindOPT(n):M[0]←0, M[1]←v1for (i = 2,…,n):M[i] ← max{FindOPT(n-1), vn + FindOPT(p(n))}

return M[n]mi qui

FindOPTployhumorous

01N time

Page 23: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

IntervalScheduling:TakeII

M[0] M[1] M[2] M[3] M[4] M[5] M[6]

Pcl o

p 2 O

l p 3 L

Ii p 4 Ot

p 5 3II

p G 3

ME23 max MEI ist Meo Mfs max MEN v MELT

maxE2 4 03 4 max 4,4 233 6

yes yes yes yes yes no

0 2 4 6 7 8MEG max'sMED rotMC333 valueof the

max 980 I 163 optimal schedule

Page 24: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

NowYouTry@5 = 4

@V = 12

@9 = 2

@7 = 8

@W = 5

@X = 1

1

2

3

4

5

6

M 1 = 0

M 2 = 1

M 3 = 0

M 4 = 2

M 5 = 2

M 6 = 3

M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7]0 4

@Y = 107 M 7 = 1

Page 25: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

FindingtheOptimalSchedule

• LetKOP(<) bethevalueoftheoptimalscheduleusingonlytheintervals 1,… , <• Case1:Final intervalisnotinK (< ∉ K)• Case2:FinalintervalisinK (< ∈ K)

• KOP < = max KOP < − 1 , @1 + KOP M <

Doe hsgobth

IOPT i OPT o l

OPT i vi t OPT pci

Be careful Both could be true if there are multiple urls

do

Ifthis is the max If this is the max

the Oi Oi then Oi S 3 10pct

Page 26: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

IntervalScheduling:TakeII

M[0] M[1] M[2] M[3] M[4] M[5] M[6]

Page 27: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

IntervalScheduling:TakeIII

• WhatistherunningtimeofFindSched(n)?

// All inputs are global varsFindSched(M,n):if (n = 0): return ∅elseif (n = 1): return {1}elseif (vn + M[p(n)] > M[n-1]):return {n} + FindSched(M,p(n))

else:return FindSched(M,n-1)

Page 28: CS3000: Algorithms & Data Jonathan Ullman · 2020. 1. 23. · Finding the Optimal Schedule •Let KOP(

DynamicProgrammingRecap

• Expresstheoptimalsolutionasarecurrence• Identifyasmallnumberofsubproblems• Relatetheoptimalsolutiononsubproblems

• Efficientlysolveforthevalue oftheoptimum• Simpleimplementationisexponentialtime• Top-Down:storesolutiontosubproblems• Bottom-Up:iteratethroughsubproblemsinorder

• Findthesolution usingthetableofvalues