lecture 20: dynamic programming iii - courses.csail.mit.edu · 2011-04-21 · lecture 20: dynamic...
TRANSCRIPT
6.006IntroductiontoAlgorithms
Lecture20:DynamicProgrammingIIIProf.ErikDemaine
Today• Dynamicprogrammingreview• Guessing
– Withinasubproblem– Usingadditionalsubproblems
• Parenthesization• Knapsack• Tetristraining
DynamicProgrammingHistory
‘Bellman…explainedthatheinventedthename“dynamicprogramming”tohidethefactthathewasdoingmathematicalresearchatRANDunderaSecretaryofDefensewho“hadapathologicalfearandhatredoftheterm,research.”Hesettledon“dynamicprogramming”becauseitwouldbedifficultgiveita“pejorativemeaning”andbecause“ItwassomethingnotevenaCongressmancouldobjectto.”’
[JohnRust2006]http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.87.2819&rep=rep1&type=pdf
RichardE.Bellman(1920–1984)
IEEEMedalofHonor,1979http://www.amazon.com/Bellman‐Continuum‐Collection‐Works‐Richard/dp/9971500906
WhatisDynamicProgramming?• “Controlled”bruteforce/exhaustivesearch• Keyideas:
– Subproblems:likeoriginalproblem,butsmaller• Writesolutiontoonesubproblem intermsofsolutionstosmallersubproblems
– Memoization:rememberthesolutiontosubproblems we’vealreadysolved,andre‐use• Avoidexponentials
– Guessing: ifyoudon’tknowsomething,guessit!(tryallpossibilities)
HowtoDynamicProgramFiveeasysteps!1. Definesubproblems2. Guess something(partofsolution)3. Relatesubproblem solutions(recurrence)4. Recurse andmemoize (topdown)
or BuildDPtablebottomup5. Solve originalproblemviasubproblems
(usuallyeasy)
HowtoAnalyzeDynamicPrograms
Fiveeasysteps!1. Definesubproblems count#subproblems2. Guesssomething count#choices3. Relatesubproblem solutions
analyzetimepersubproblem4. DPrunningtime =#subproblems
timepersubproblem5. Sometimesadditionalrunningtime
tosolveoriginalproblem
FibonacciNumber
1. Subproblems: for2. Guess: nothing3. Recurrence: ;
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
photobyRobobobobohttp://www.flickr.com/photos/45493477@N05/4178075187/
CrazyEights
1. Subproblems: =lengthoflongesttrickendingwithcard ,for
2. Guess: previouscard in3. Recurrence:trick =1+max
trick for ifcards match}4. DPtime =#subproblems time/subproblem
5. Originalproblem=max(trick for )
7♣ 7♥ K♣ K♠ 8♥2♣
7♣ 7♥ K♣ K♠ 8♥2♣CrazyEights
recurse+memoize DPtablememo={}def trick :if notinmemo:
memo 1+maxtrick forifcards match}
returnmemoreturnmax(trick for )
trick={}for from to :trick 1+max
trick forifcards match}
returnmax(trick for )
hieroglyphologyMichaelangelo
SequenceAlignment(LCS,EditDistance,etc.)
1. Subproblems:for & :=costofbestalignmentof &
2. Guess: howtoalign/drop and3. Recurrence:
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
hieroglyphologyMichaelangelo
ChoosingSubproblems• Forstring/sequence/array :
– Suffixes– Prefixes– Substrings
Bellman‐Ford(single‐sourceshortestpaths)
1. Subproblems:for & :=weightofshortest
pathusing edges2. Guess: lastedgeinthispath3. Recurrence:
4. DPtime =#subproblems time/subproblem
5. Originalproblem= | | for
Floyd‐Warshall(all‐pairsshortestpaths)
1. Subproblems:for & :=weightofshortest path
usingintermediateverticesin2. Guess: isvertex inthepath?3. Recurrence:
4. DPtime =#subproblems time/subproblem
5. Originalproblem= for
Bottom‐UpFloyd‐Warshall
for in :for in :
[ ifnoedge]for from to :for from to :for from to :if
Parenthesization Problem• Givensequenceofmatrices ofdimensions
• Computeassociativeproductusingsequenceofnormalmatrixmultipliesintheorderthatminimizescost
• Costtomultiply with is
Parenthesization Example
Parenthesization DP1. Subproblems:for :
costofoptimalmultiplicationof2. Guess: lastmultiplicationtodo:
3. Recurrence:
• Prefix/suffixnotenough;usesubstrings
Parenthesization DP1. Subproblems:for :
costofoptimalmultiplicationof2. Guess: lastmultiplicationtodo:
3. Recurrence:
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
KnapsackProblem
• Knapsackofintegersize• Items• Item hasintegersizeandvalue
• Goal: Choosesubsetofitemsofmaximumpossibletotalvalue,subjecttototalsize
photobyErikDemaine
KnapsackDP1. Subproblems:for :
optimalpackingofitems2. Guess: includeitem ?3. Recurrence:
• Howtomaintainremainingspaceinknapsack?
Guess!
http://3.bp.blogspot.com/_lrYZ590iyME/TA‐aqU7MPnI/AAAAAAAABf8/KXABMduUVvM/s1600/Guess+Backpack.jpg
KnapsackDP1. Subproblems:for & :
optimalpackingofitemsintoknapsackofsize
2. Guess: includeitem ?3. Recurrence:
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
PseudopolynomialTime• runningtimeispseudopolynomial• Ingeneral: polynomialin andtheintegersintheprobleminput
• Equivalently: polynomialintheinputsizeiftheintegerswerewritteninunary
• Polynomialtimeassumesencodedinbinary• Knapsackisextremelyunlikelytohaveapolynomial‐timealgorithm(seeLecture25)
TetrisTraining
• Givensequenceof pieces
• Givenboardofsmallwidthandlargerheight
• Goal: Placeeachpieceinsequencetosurvive—staywithinheight —withoutanyholes/overhang
TetrisTrainingDP1. Subproblems:for :
canyousurvivegivenpieces ?2. Guess: howtoplacepiece3. Recurrence:
• Howtoknowvalidmovesforpiece ?• Guess!
TetrisTrainingDP1. Subproblems:for
& :canyousurvivegivenpieces
startingfromcolumnswith heights ?
2. Guess: howtoplacepiece3. Recurrence:
What’sNext?• Dynamicprogrammingovercombinatorialstructuresotherthanarrays
• Moreexamplesofthepowerofguessing