+ All Categories
Home > Documents > Lecture 21: Dynamic Programming IV

Lecture 21: Dynamic Programming IV

Date post: 02-Jan-2017
Category:
Upload: trannhu
View: 221 times
Download: 0 times
Share this document with a friend
26
6.006 Introduction to Algorithms Lecture 21: Dynamic Programming IV Prof. Erik Demaine
Transcript
Page 1: Lecture 21: Dynamic Programming IV

6.006IntroductiontoAlgorithms

Lecture21:DynamicProgrammingIVProf.ErikDemaine

Page 2: Lecture 21: Dynamic Programming IV

Today• Pianofingering• Platformvideogames• Structuraldynamicprogramming• Vertexcover• Widgetlayout

Page 3: Lecture 21: Dynamic Programming IV

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

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

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

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

PianoFingering

photobyBrianRichardson(seriousbri),2009http://www.flickr.com/photos/seriousbri/4148739768/

Page 8: Lecture 21: Dynamic Programming IV

PianoFingering

imagesfromhttp://www.piano‐lessons‐central.com/music‐notation/how‐to‐read‐music/

Page 9: Lecture 21: Dynamic Programming IV

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

PianoFingeringDP1. Subproblems: for :

minimumdifficultypossiblefornote2. Guess: finger fornote3. Recurrence:

• Howtoknowfingeringfornextnote ?• Guess!

Page 11: Lecture 21: Dynamic Programming IV

PianoFingeringDP1. Subproblems: for &finger :

minimumdifficultypossiblefornotestartingonfinger

2. Guess: finger fornote3. Recurrence:

4. DPtime =#subproblems time/subproblem

5. Originalproblem=

Page 12: Lecture 21: Dynamic Programming IV

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

PlatformVideoGames• Givenentirelevel:objects,enemies,etc.• Anythingoutside screenisreset• Configuration =screenstate,score,velocity,…• Giventransitionfunctionforeachtimestep

– Movement,enemies,…• Goal: Maximizescoresubjecttosurvivingandreachinggoal

Page 14: Lecture 21: Dynamic Programming IV

PlatformVideoGameDP1. Subproblems: forconfiguration :

bestpossiblescorestartingfrom2. Guess: whichactiontotake(ifany)3. Recurrence:

4. DPtime =#subproblems time/subproblem

5. Originalproblem

Page 15: Lecture 21: Dynamic Programming IV

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

StructuralDynamicProgramming

• Followacombinatorialstructureotherthanasequence/afewsequences– Likestructuralvs.regularinduction

• Mainexample: Treestructure• Usefulsubproblems:foreveryvertex ,subtreerootedat

Page 17: Lecture 21: Dynamic Programming IV

VertexCover• Givenanundirectedgraph• Findaminimum‐cardinalityset ofverticescontainingatleastoneendpointofeveryedge– Equivalently,findaminimumsetofguardsforabuildingofcorridors,or(unaligned)streetsincity

Example:

Page 18: Lecture 21: Dynamic Programming IV

VertexCoverAlgorithms• Extremelyunlikelytohaveapolynomial‐timealgorithm,evenforplanargraphs(seeLecture25)

• Butpolynomially solvableontrees,usingdynamicprogramming

Page 19: Lecture 21: Dynamic Programming IV

VertexCoverinTreeDP1. Subproblems: for :sizeofsmallest

vertexcoverinsubtree rootedat2. Guess: is inthecover?

– YES:• Coverchildrenedges• Leftwithchildrensubtrees

– NO:• Allchildrenmustbeincover• Leftwithgrandchildren

subtrees

Page 20: Lecture 21: Dynamic Programming IV

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

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

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

WidgetLayoutDP1. Subproblems: for & :

minimum suchthatwidget fitsinto2. Guess: Leaf : Whichsizetouse?

Internal : Horizontalorvertical?3. Recurrence:

Page 24: Lecture 21: Dynamic Programming IV

HorizontalLayoutDP1. Subproblems: for , ,

:minimum suchthathorizontallayoutof

fitsinto rectangle2. Guess: Width ofchild3. Recurrence:

4. DPtime =#subproblems time/subproblem

Page 25: Lecture 21: Dynamic Programming IV

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

WidgetLayoutSummary• Two“levels”ofdynamicprogramming

1. Optimalheightforgivenwidthofsubtree rootedat

2. Optimallayout(partitioning)ofchildrenintohorizontalarrangement

• Reallyjustonebiggerdynamicprogram• Pseudopolynomial runningtime:


Recommended