Lecture 20

6.006
IntroductiontoAlgorithms
Lecture20:DynamicProgrammingIII
Prof.ErikDemaine
Today
• Dynamicprogrammingreview
• Guessing
– Withinasubproblem
– Usingadditionalsubproblems
• Parenthesization
• Knapsack
• Tetristraining
DynamicProgramming
History
‘Bellman…explainedthathe
inventedthename“dynamic
programming”tohidethefactthat
hewasdoingmathematical
researchatRANDundera
SecretaryofDefensewho“hada
pathologicalfearandhatredofthe
term,research.”Hesettledon
“dynamicprogramming”becauseit
wouldbedifficultgiveita
“pejorativemeaning”andbecause
“Itwassomethingnotevena
Congressmancouldobjectto.”’
[JohnRust2006]
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.87.2819&rep=rep1&type=pdf
RichardE.Bellman
(1920–1984)
IEEEMedalofHonor,1979
http://www.amazon.com/Bellman‐Continuum‐
Collection‐Works‐Richard/dp/9971500906
WhatisDynamicProgramming?
• “Controlled”bruteforce/exhaustivesearch
• Keyideas:
– Subproblems:likeoriginalproblem,butsmaller
• Writesolutiontoonesubproblem intermsof
solutionstosmallersubproblems
– Memoization:rememberthesolutionto
subproblems we’vealreadysolved,andre‐use
• Avoidexponentials
– Guessing: ifyoudon’tknowsomething,guessit!
(tryallpossibilities)
HowtoDynamicProgram
Fiveeasysteps!
1. Definesubproblems
2. Guess something(partofsolution)
3. Relatesubproblem solutions(recurrence)
4. Recurse andmemoize (topdown)
or BuildDPtablebottomup
5. Solve originalproblemviasubproblems
(usuallyeasy)
HowtoAnalyze
DynamicPrograms
Fiveeasysteps!
1. Definesubproblems count#subproblems
2. Guesssomething
count#choices
3. Relatesubproblem solutions
analyzetimepersubproblem
4. DPrunningtime =#subproblems
timepersubproblem
5. Sometimesadditionalrunningtime
tosolveoriginalproblem
Fibonacci
Number
1. Subproblems:
2. Guess: nothing
3. Recurrence:
for
;
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
photobyRobobobobo
http://www.flickr.com/photos/45493477@N05/4178075187/
CrazyEights
7♣
7♥
K♣
K♠
2♣
8♥
1. Subproblems:
=lengthoflongest
trickendingwithcard ,for
2. Guess: previouscard in
3. Recurrence:trick =1+max
trick for
ifcards match}
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
max(trick for
)
CrazyEights
7♣
recurse+memoize
memo={}
def trick :
if notinmemo:
memo
1+max
trick for
ifcards match}
returnmemo
returnmax(
trick for
)
7♥
K♣
K♠
2♣
8♥
DPtable
trick={}
for from to :
trick
1+max
trick for
ifcards match}
returnmax(
)
trick for
SequenceAlignment
(LCS,EditDistance,etc.)
hieroglyphology
Michaelangelo
1. Subproblems:for
&
=costofbestalignmentof
2. Guess: howtoalign/drop
and
3. Recurrence:
&
:
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
ChoosingSubproblems
• Forstring/sequence/array :
– Suffixes
– Prefixes
– Substrings
Bellman‐Ford
(single‐sourceshortestpaths)
1. Subproblems:for
=weightofshortest
pathusing
edges
2. Guess: lastedgeinthispath
3. Recurrence:
&
:
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
| |
for
Floyd‐Warshall
(all‐pairsshortestpaths)
1. Subproblems:for
&
=weightofshortest
path
usingintermediateverticesin
2. Guess: isvertex inthepath?
3. Recurrence:
:
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
for
Bottom‐UpFloyd‐Warshall
for in :
for in :
[
for from to :
for from to :
for from to :
if
ifnoedge]
Parenthesization Problem
• Givensequenceofmatrices
of
dimensions
• Computeassociativeproduct
usingsequenceofnormalmatrixmultiplies
intheorderthatminimizescost
with
is
• Costtomultiply
Parenthesization Example
Parenthesization DP
1. Subproblems:for
:
costofoptimalmultiplicationof
2. Guess: lastmultiplicationtodo:
3. Recurrence:
• Prefix/suffixnotenough;usesubstrings
Parenthesization DP
1. Subproblems:for
:
costofoptimalmultiplicationof
2. Guess: lastmultiplicationtodo:
3. Recurrence:
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
Knapsack
Problem
• Knapsackofintegersize
• Items
• Item hasintegersize
andvalue
• Goal: Choosesubsetof
itemsofmaximum
possibletotalvalue,
subjecttototalsize
photobyErikDemaine
KnapsackDP
1. Subproblems:for
optimalpackingofitems
2. Guess: includeitem ?
3. Recurrence:
:
• Howtomaintainremainingspaceinknapsack?
Guess!
http://3.bp.blogspot.com/_lrYZ590iyME/TA‐
aqU7MPnI/AAAAAAAABf8/KXABMduUVvM/s1600/Guess+Backpack.jpg
KnapsackDP
1. Subproblems:for
optimalpackingofitems
intoknapsackofsize
2. Guess: includeitem ?
3. Recurrence:
&
:
4. DPtime =#subproblems time/subproblem
5. Originalproblem=
PseudopolynomialTime
•
runningtimeispseudopolynomial
• Ingeneral: polynomialin andthe
integersintheprobleminput
• Equivalently: polynomialintheinputsize
iftheintegerswerewritteninunary
• Polynomialtimeassumesencodedinbinary
• Knapsackisextremelyunlikelytohavea
polynomial‐timealgorithm(seeLecture25)
TetrisTraining
• Givensequenceof pieces
• Givenboardofsmallwidth
andlargerheight
• Goal: Placeeachpiecein
sequencetosurvive —
staywithinheight —
withoutanyholes/overhang
TetrisTrainingDP
1. Subproblems:for
:
canyousurvivegivenpieces
2. Guess: howtoplacepiece
3. Recurrence:
• Howtoknowvalidmovesforpiece ?
• Guess!
?
TetrisTrainingDP
1. Subproblems:for
&
:
canyousurvivegivenpieces
startingfrom
columnswith heights
2. Guess: howtoplacepiece
3. Recurrence:
?
What’sNext?
• Dynamicprogrammingovercombinatorial
structuresotherthanarrays
• Moreexamplesofthepowerofguessing