0% found this document useful (0 votes)
125 views6 pages

MIT6 006F11 Lec20 PDF

This document summarizes key concepts from a lecture on dynamic programming II, including examples of text justification, perfect-information blackjack, and using parent pointers. It discusses the 5 steps to dynamic programming: 1) defining subproblems, 2) guessing part of the solution, 3) relating subproblem solutions, 4) recursively solving and memoizing subproblems, and 5) solving the original problem. For each example, it outlines the subproblems, guesses, recurrence relations, and running times. It also briefly describes using parent pointers to recover the actual solution in addition to the cost.

Uploaded by

Nit_su
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views6 pages

MIT6 006F11 Lec20 PDF

This document summarizes key concepts from a lecture on dynamic programming II, including examples of text justification, perfect-information blackjack, and using parent pointers. It discusses the 5 steps to dynamic programming: 1) defining subproblems, 2) guessing part of the solution, 3) relating subproblem solutions, 4) recursively solving and memoizing subproblems, and 5) solving the original problem. For each example, it outlines the subproblems, guesses, recurrence relations, and running times. It also briefly describes using parent pointers to recover the actual solution in addition to the cost.

Uploaded by

Nit_su
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture 20

Dynamic Programming II of IV

6.006 Fall 2011

Lecture 20: Dynamic Programming II


Lecture Overview
5 easy steps
Text justification
Perfect-information Blackjack
Parent pointers

Summary
* DP careful brute force
* DP guessing + recursion + memoization
* DP dividing into reasonable # subproblems whose solutions relate acyclicly
usually via guessing parts of solution.
* time = # subproblems time/subproblem
|
{z
}

treating recursive calls as O(1)

(usually mainly guessing)


essentially an amortization
count each subproblem only once; after first time, costs O(1) via memoization
* DP shortest paths in some DAG

5 Easy Steps to Dynamic Programming


1. define subproblems

count # subproblems

2. guess (part of solution)

count # choices

3. relate subproblem solutions

compute time/subproblem

4. recurse + memoize
problems
OR build DP table bottom-up
check subproblems acyclic/topological order

time = time/subproblem # sub-

5. solve original problem: = a subproblem


OR by combining subproblem solutions

= extra time

Lecture 20

Dynamic Programming II of IV

Examples:
subprobs:

time/subpr:
topo. order:
total time:

Fibonacci
Fk
for 1 k n
n
nothing
1
Fk = Fk1
+Fk2
(1)
for k = 1, . . . , n
(n)

orig. prob.:
extra time:

Fn
(1)

# subprobs:
guess:
# choices:
recurrence:

6.006 Fall 2011

Shortest Paths
k (s, v) for v V, 0 k < |V |
= min s v path using k edges
V2
edge into v (if any)
indegree(v) + 1
k (s, v) = min{k1 (s, u) + w(u, v)
| (u, v) E}
(1 + indegree(v))
for k = 0, 1, . . . |V | 1 for v V
(V E)
+ (V 2 ) unless efficient about indeg. 0
|V |1 (s, v) for v V
(V )

Text Justification
Split text into good lines
obvious (MS Word/Open Office) algorithm: put as many words that fit on first line,
repeat
but this can make very bad lines

vs.

blah
blah
blah
blah
reallylongword

:)

:<

blah blah blah


b l a h
reallylongword

Figure 1: Good vs. Bad Text Justification.


Define badness(i, j) for line of words[i : j].
For example, if total length > page width, else (page width total length)3 .
P
goal: split words into lines to min
badness

1. subproblem = min. badness for suffix words[i :]


= # subproblems = (n) where n = # words
2. guessing = where to end first line, say i : j
= # choices = n i = O(n)
2

Lecture 20

Dynamic Programming II of IV

6.006 Fall 2011

3. recurrence:
DP[i] = min(badness (i, j) + DP [j] for j in range (i + 1, n + 1))
DP [n] = 0
= time per subproblem = (n)
4. order: for i = n, n 1, . . . , 1, 0
total time = (n2 )

badness(i,j)

Figure 2: DAG.
5. solution = DP [0]

Perfect-Information Blackjack
Given entire deck order: c0 , c1 , cn1
1-player game against stand-on-17 dealer
when should you hit or stand? GUESS
goal: maximize winnings for fixed bet $1
may benefit from losing one hand to improve future hands!

1. subproblems: BJ(i) = best play of

ci , . . . cn1
where i is # cards already played
| {z }
remaining cards

= # subproblems = n
2. guess: how many times player hits (hit means draw another card)
= # choices n
3. recurrence: BJ(i) = max(
outcome {+1, 0, 1} + BJ(i + # cards used)
for # hits in 0, 1, . . . if valid play dont hit after bust
3

O(n)
O(n)

Lecture 20

Dynamic Programming II of IV

6.006 Fall 2011

)
= time/subproblem = (n2 )
4. order: for i in reversed(range(n))
total time = (n3 )
iO(1)
n
1 nX
X
time is really
(n i #h) = (n3 ) still
i=0

#h=0

5. solution: BJ(0)
detailed recurrence: before memoization (ignoring splits/betting)

(n2 )

(n)

(n)
with
care

valid
plays

BJ(i):
if n i < 4: return 0 (not enough cards)
for p in range(2, n i 1): (# cards taken)
player = sum(ci , ci+2 , ci+4:i+p+2 )
if player > 21: (bust)
options.append(1(bust) + BJ(i + p + 2))
break
for d in range(2, n i p)
dealer = sum(ci+1 , ci+3 , ci+p+2:i+p+d )
if dealer 17: break
if dealer > 21: dealer = 0 (bust)
options.append(cmp(player, dealer) + BJ(i + p + d))
return
max(options)

0
-1
+1

outcomes

Figure 3: DAG View

Parent Pointers
To recover actual solution in addition to cost, store parent pointers (which guess used at
each subproblem) & walk back
4

Lecture 20

Dynamic Programming II of IV

typically: remember argmin/argmax in addition to min/max


example: text justification

(3) DP[i] = min(badness(i,j) + DP[i][0],j)


for j in range(i+1,n+1)
DP[n] = (0, None)
(5) i = 0
while i is not None:
start line before word i
i = DP[i][1]

just like memoization & bottom-up, this transformation is automatic


no thinking required

6.006 Fall 2011

MIT OpenCourseWare
http://ocw.mit.edu

6.006 Introduction to Algorithms


Fall 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like