Lecture 5 Dynamic Programming. Dynamic Programming Self-reducibility.
Lecture 21: Dynamic Programming IV
Transcript of Lecture 21: Dynamic Programming IV
![Page 1: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/1.jpg)
6.006IntroductiontoAlgorithms
Lecture21:DynamicProgrammingIVProf.ErikDemaine
![Page 2: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/2.jpg)
Today• Pianofingering• Platformvideogames• Structuraldynamicprogramming• Vertexcover• Widgetlayout
![Page 3: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/3.jpg)
Recall:WhatisDynamicProgramming?
• “Controlled”bruteforce/exhaustivesearch• Keyideas:
– Subproblems:likeoriginalproblem,butsmaller• Writesolutiontoonesubproblem intermsofsolutionstosmallersubproblems
– Memoization:rememberthesolutiontosubproblems we’vealreadysolved,andre‐use• Avoidexponentials
– Guessing: ifyoudon’tknowsomething,guessit!(tryallpossibilities)
![Page 4: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/4.jpg)
Recall:HowtoDynamicProgram
Fiveeasysteps!1. Definesubproblems2. Guess something(partofsolution)3. Relatesubproblem solutions(recurrence)4. Recurse andmemoize (topdown)
or BuildDPtablebottomup5. Solve originalproblemviasubproblems
(usuallyeasy)
![Page 5: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/5.jpg)
Recall: HowtoAnalyzeDynamicPrograms
Fiveeasysteps!1. Definesubproblems count#subproblems2. Guesssomething count#choices3. Relatesubproblem solutions
analyzetimepersubproblem4. DPrunningtime =#subproblems
timepersubproblem5. Sometimesadditionalrunningtime
tosolveoriginalproblem
![Page 6: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/6.jpg)
TwoKindsofGuessing1. Withinasubproblem
– CrazyEights:previouscardintrick– Sequencealignment:align/droponecharacter– Bellman‐Ford:previousedgeinpath– Floyd‐Warshall:usevertex ?– Parenthesization:lastmultiplication– Knapsack:includeitem ?– Tetristraining:howtoplacepiece
2. Usingadditionalsubproblems– Knapsack:howmuchspaceleftinknapsack– Tetristraining:currentboardconfiguration
![Page 7: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/7.jpg)
PianoFingering
photobyBrianRichardson(seriousbri),2009http://www.flickr.com/photos/seriousbri/4148739768/
![Page 8: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/8.jpg)
PianoFingering
imagesfromhttp://www.piano‐lessons‐central.com/music‐notation/how‐to‐read‐music/
![Page 9: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/9.jpg)
PianoFingering[Parncutt,Sloboda,Clarke,Raekallio,Desain 1997;
Hart,Bosch,Tsai2000;AlKasimi,Nichols,Raphael2007]
• Givenmusicalpiecetoplay– Say,sequenceofsinglenoteswithrighthand– (Canextendtobothhands,multiplenotes,etc.)
• Givenmetric ofdifficulty goingfromfinger onnote tofinger onnote– Crossing:Highif and– Stretch:Highif– Legato: if– Weakfinger:Highif– :Highif– …
References:http://www.jstor.org/pss/40285730
http://www.jstor.org/pss/10.1525/mp.2001.18.4.505
http://www.cse.unsw.edu.au/~cs9024/05s2/ass/ass01/fingering.pdf
http://ismir2007.ismir.net/posters/ISMIR2007_p355_kasimi_poster.pdf
![Page 10: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/10.jpg)
PianoFingeringDP1. Subproblems: for :
minimumdifficultypossiblefornote2. Guess: finger fornote3. Recurrence:
• Howtoknowfingeringfornextnote ?• Guess!
![Page 11: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/11.jpg)
PianoFingeringDP1. Subproblems: for &finger :
minimumdifficultypossiblefornotestartingonfinger
2. Guess: finger fornote3. Recurrence:
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
![Page 12: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/12.jpg)
http://media.ign.com/games/image/article/833/833615/ign‐presents‐the‐history‐of‐super‐mario‐bros‐20071108044755327.gifhttp://images.inquisitr.com/wp‐content/2011/03/super‐mario‐bros‐modern‐sound.jpg
![Page 13: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/13.jpg)
PlatformVideoGames• Givenentirelevel:objects,enemies,etc.• Anythingoutside screenisreset• Configuration =screenstate,score,velocity,…• Giventransitionfunctionforeachtimestep
– Movement,enemies,…• Goal: Maximizescoresubjecttosurvivingandreachinggoal
![Page 14: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/14.jpg)
PlatformVideoGameDP1. Subproblems: forconfiguration :
bestpossiblescorestartingfrom2. Guess: whichactiontotake(ifany)3. Recurrence:
4. DPtime =#subproblems time/subproblem
5. Originalproblem
![Page 15: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/15.jpg)
CyclesinSubproblems•mightleadtocycles
• Inthisproblem,neverhelpstocycle– capturesentirestate,includingscore
• Somarksubproblematstart,andifcycle,ignorethatsubproblem
• OR: SMBtimerin ,soactuallynocycles
memo={}def mario :if notinmemo:
memomemo
returnmemo
![Page 16: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/16.jpg)
StructuralDynamicProgramming
• Followacombinatorialstructureotherthanasequence/afewsequences– Likestructuralvs.regularinduction
• Mainexample: Treestructure• Usefulsubproblems:foreveryvertex ,subtreerootedat
![Page 17: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/17.jpg)
VertexCover• Givenanundirectedgraph• Findaminimum‐cardinalityset ofverticescontainingatleastoneendpointofeveryedge– Equivalently,findaminimumsetofguardsforabuildingofcorridors,or(unaligned)streetsincity
Example:
![Page 18: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/18.jpg)
VertexCoverAlgorithms• Extremelyunlikelytohaveapolynomial‐timealgorithm,evenforplanargraphs(seeLecture25)
• Butpolynomially solvableontrees,usingdynamicprogramming
![Page 19: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/19.jpg)
VertexCoverinTreeDP1. Subproblems: for :sizeofsmallest
vertexcoverinsubtree rootedat2. Guess: is inthecover?
– YES:• Coverchildrenedges• Leftwithchildrensubtrees
– NO:• Allchildrenmustbeincover• Leftwithgrandchildren
subtrees
![Page 20: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/20.jpg)
VertexCoverinTreeDP1. Subproblems: for :sizeofsmallest
vertexcoverinsubtree rootedat2. Guess: is inthecover?3. Recurrence:
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
![Page 21: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/21.jpg)
ImprovedVertexCoverinTreeDP
1. Subproblems: for & :sizeofsmallestvertexcover insubtree rootedatsuchthat (unconstrainedif )
2. Guess: Does ?3. Recurrence:
4. DPtime =#subproblems time/subproblem
5. Originalproblem
![Page 22: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/22.jpg)
WidgetLayout• Givenahierarchyofwidgets• Leafwidget=button,image,…
– Listofpossiblerectangularsizes• Internalwidget=rectangularcontainer– Canjoinchildrenhorizontallyorvertically
• Goal: Fitintoagivenrectangularscreen
http://lin‐kuei.blogspot.com/2010/12/android‐app‐menu‐widgets‐and‐site‐map.html
![Page 23: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/23.jpg)
WidgetLayoutDP1. Subproblems: for & :
minimum suchthatwidget fitsinto2. Guess: Leaf : Whichsizetouse?
Internal : Horizontalorvertical?3. Recurrence:
![Page 24: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/24.jpg)
HorizontalLayoutDP1. Subproblems: for , ,
:minimum suchthathorizontallayoutof
fitsinto rectangle2. Guess: Width ofchild3. Recurrence:
4. DPtime =#subproblems time/subproblem
![Page 25: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/25.jpg)
WidgetLayoutDP1. Subproblems: for & :
minimum suchthatwidget fitsinto2. Guess: Leaf : Whichsizetouse?
Internal : Horizontalorvertical?3. Recurrence:
4. DPtime =#subproblems time/subproblem
5. Originalproblem
![Page 26: Lecture 21: Dynamic Programming IV](https://reader031.fdocuments.net/reader031/viewer/2022021923/586a46f71a28ab9c7d8b9c65/html5/thumbnails/26.jpg)
WidgetLayoutSummary• Two“levels”ofdynamicprogramming
1. Optimalheightforgivenwidthofsubtree rootedat
2. Optimallayout(partitioning)ofchildrenintohorizontalarrangement
• Reallyjustonebiggerdynamicprogram• Pseudopolynomial runningtime: