Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
CS3000:Algorithms&DataJonathanUllman
Lecture5:• DynamicProgramming:
FibonacciNumbers,IntervalScheduling
Jan22,2020
DynamicProgramming
• Don’tthinktoohardaboutthename• Ithoughtdynamicprogrammingwasagoodname.Itwassomethingnotevenacongressmancouldobjectto.SoIuseditasanumbrellaformyactivities. -Bellman
• Dynamicprogrammingiscarefulrecursion• Breaktheproblemupintosmallpieces• Recursivelysolvethesmallerpieces• KeyChallenge:identifyingthepiecesE
Warmup:FibonacciNumbers
FibonacciNumbers
• 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,…• * + = * + − 1 + * + − 2
• * + → 01 ≈ 1.621
• 0 = 56 7�9 isthegoldenratio
f NflD
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
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
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
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!
DynamicProgramming:IntervalScheduling
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
IntervalSchedulingvalue El 3,53 2 4 2 8
O inde
i
corea oii
0 i
PossibleAlgorithms
• Chooseintervalsindecreasingorderof@>
PossibleAlgorithms
• Chooseintervalsinincreasingorderof=>
PossibleAlgorithms
• Chooseintervalsinincreasingorderof?> − =>
ARecursiveFormulation
• LetK betheoptimalschedule• BoldStatement:K eithercontainsthelastintervaloritdoesnot.
ARecursiveFormulation
• LetK betheoptimalschedule• Case1: FinalintervalisnotinK (i.e.6 ∉ K)
The 0 mustbe the optimal schedule for El 2,3 4,53
111111111
ARecursiveFormulation
• LetK betheoptimalschedule• Case2:Final intervalisinK (i.e.6 ∈ K)O must be 63 theoptimal schedule for 9112,33
1111111111 11 111411411111
O
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
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
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
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
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
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
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
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
IntervalScheduling:TakeII
M[0] M[1] M[2] M[3] M[4] M[5] M[6]
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)
DynamicProgrammingRecap
• Expresstheoptimalsolutionasarecurrence• Identifyasmallnumberofsubproblems• Relatetheoptimalsolutiononsubproblems
• Efficientlysolveforthevalue oftheoptimum• Simpleimplementationisexponentialtime• Top-Down:storesolutiontosubproblems• Bottom-Up:iteratethroughsubproblemsinorder
• Findthesolution usingthetableofvalues