Académique Documents
Professionnel Documents
Culture Documents
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
Lecture 19
Dynamic Programming I of IV
Lecture Overview
Fibonacci Warmup
Memoization and subproblems
Shortest Paths
Crazy Eights
Guessing Viewpoint
Readings
CLRS 15
Fibonacci Numbers
F1 = F2 = 1;
Fn = Fn1 + Fn2
Naive Algorithm
follow recursive denition
b(n):
if n 2: return 1
else return b(n 1) + b(n 2)
= T (n) = T (n 1) + T (n 2) + O(1) n
2T (n 2) + O(1) 2n/2
EXPONENTIAL - BAD!
Lecture 19
Dynamic Programming I of IV
Fn
Fn-2
Fn-1
Fn-2
Fn-3
Fn-3
Fn-4
Lecture 19
Dynamic Programming I of IV
Shortest Paths
Recursive formulation:
(s, v) E}
subproblems
time/subproblem
= O(V
deg(V )) = O(V E)
V
Lecture 19
Dynamic Programming I of IV
e.g., 7, 6, 7, 3, 8, J
recursive formulation:
trick(i) = length of best trick starting at c[i]
= 1 + max(trick(j) for j in range(i + 1, n) if match (c[i], c[j]))
best
= max(trick(i) for i in range(n))
memoize: trick(i) depends only on trick(> i)
= time = subproblems time/subproblem
O(n)
2
O(n )
O(n)
Guessing Viewpoint
what is the rst card in best trick? guess!
poly(n)O(1)