Title: First Ingredient of Dynamic Programming
1First Ingredient of Dynamic Programming
1. Optimal substructure
The optimal solution to the problem contains
optimal solutions to the subproblems.
Proof by contradiction (cut-and-paste).
2Second Ingredient of DP
2. Overlapping subproblems.
Few subproblems but many recurring instances.
3Memoization
1. Still recursive
2. After computing the solution to a subproblem,
store it in table.
3. Subsequent calls do table lookup.
4Matrix-Chain Recursion Tree without Memoization
1..4
1..1 2..4 1..2
3..4 1..3 4..4
2..2 3..4 2..3 4..4 1..1 2..2 3..3
4..4 1..1 2..3 1..2 3..3
3..3 4..4 2..2 3..3
2..2 3..3 1..1 2..2
5Matrix-Chain Recursion Tree with Memoization
1..4
1..1 2..4 1..2
3..4 1..3 4..4
2..2 3..4 2..3 4..4 1..1 2..2
1..1 2..3 1..2 3..3
3..3 4..4 2..2 3..3
6Memoized-Matrix-Chain
Lookup-Chain(p, i, j) // chain product
A A dimensions in p if mi, j lt ?
// if cost already
computed then return mi, j // simply return
the cost // otherwise, its the first call
compute the cost recursively if i j then
mi, j 0 else for k i to j 1 do
q Lookup-Chain(p, i, k) Lookup-Chain(p,
k1, j) p p p if q lt mi, j
then mi, j q return mi, j
i j
i-1 k j
Invoke Lookup-Chain(p, 1, n) to compute the chain
product cost.
7Analysis of Memoization
Lookup-Chain(p, 1, n)
LC(p, i, j)
8Analysis (contd)
The first call to Lookup-Chain(p, i, j) requires
computation. // ?(j i) O(n) time //
(excluding time spent on recursively computing
other entries). The rest n i ? j ? 2 calls
result in table lookups // O(1) each call of
this kind
Total running time
9Memoization vs DP