p x q q x r
{
{
{
pqr
rq
q
Example
Two ways to
parenthesize
A
1
A
2
A
3
10 100 5 50
( A
1
A
2
) A
3
10 100 5 50
= (10)(100)(5) + (10)(5)(50)
= 5000 + 2500 = 7500
A
1
( A
2
A
3
)
10 100 5 50
= (100)(5)(50) + (10)(100)(50)
= 25000 + 50000 = 75000
2
1 2 3 4
A A A A Can be parenthesized in 5 ways
( ) ( ) ( )
1 2 3 4
A A A A
( ) ( ) ( )
1 2 3 4
A A A A
( )( ) ( )
1 2 3 4
A A A A
( ) ( ) ( )
1 2 3 4
A A A A
( ) ( ) ( )
1 2 3 4
A A A A
Let number of ways to parenthesize a
product of n matrices, then
( ) P n =
1
1 1
( )
( ) ( ) 2
n
k
if n
P n
P k P n k if n
=
=
[ ] , m i j = Min # of mult. to evaluate
i j
A
[ ]
( )
, 1, 1
0
,
min
i k k j i k j
i k j
if i j
m i j
m m p p p if i j
+
=
+ + <
= + + +
Objective
Find a way to fully parenthesize the product
in such a way that the cost is minimum
1 1 2 n n
A A A A =
3
Optimal Substruc. Property (  ) (  )
Overlapping Subproblems Property
Recursive formula for m[I, j] leads to an
exponential problem
Observation: Relatively few problems
Set of problems { ij }
So # of subprobs. is the # of ways of
choosing I, j such that ,
which is
1 i j n
2
n
n
 
+

\ .
Overlap
( )( ) ( ) ( )
4
Analysis of Recursive Analysis of Recursive Matrix Matrix Chain Chain
( ) T n = time to compute optimal parenth. of
chain of n matrices
( )
1
1
(1) 1
( ) 1 ( ) ( ) 1 1
n
k
T
T n T k T n k for n
+ + + >
( )
1
1
1
1
( ) 1 ( ) ( ) 1
2 ( )
n
k
n
k
T n T k T n k
T k n
=
+ + +
= +
1 n
( )
1 2
1
1 0
1
1
( ) 2 2 2 2
2 2 1 2 2
2
n n
k k
k k
n n
n
T n n n
n n
= =
+ = +
= + = +
5
Memoization Memoization
Create a table for solns to subprobs so far
computed
Initialize each table entry to a symbol
indicating subprob not yet computed
Each time a subproblem is first encountered,
its soln is computed and placed in the table
Each subsequent time it is encountered, its
previously computed soln is simply retrieved
from the table
Memoization Memoization
How to Design a Dynamic How to Design a Dynamic Prog Prog. Algorithm . Algorithm
1. Identify the subproblems
2. Characterize the op. soln as a combination
of op solns to subprobs
Subprobs must be of same type as original probs
Subprobs must be smaller
How to specify each subprob?
What is the base case? What is the recursive case?
Focus on value of soln rather than description of soln
Introduce notation to rep. value of op soln to each subprob
Consider trying all possible solns to prob
How to Design a Dynamic How to Design a Dynamic Prog Prog. Algorithm . Algorithm
3. Apply generic dynamic prog. algorithm
4. Modify your algorithm to compute
description of soln to value of soln, if needed
Use bottomup version
use topdown version (Memoization)
Add additional table
6
Longest Common Subsequence Longest Common Subsequence
(LCS) (LCS)
If
then common subsequences are, for
example,
and
Both are common subsequences (CS). But
only the last is a LCS.
, , , , , ,
, , , , ,
X A B C B D A B
Y B D C A B A
=
=
BCA BCBA
{
Longest Common Subsequence (LCS) Longest Common Subsequence (LCS)
Def. A seq is a subseq of
if there is a strictly
increasing seq of the indices of
such that
LCS Problem: Given two seqs, find an LCS
Notation: Given , the ith
prefix is defined as the subseq
1 2,
, ,
k
Z z z z =
1 2,
, ,
n
X x x x =
1 2
, , ,
k
i i i X
j
i j
x z =
1 2,
, ,
n
X x x x =
i
X
1 2,
, ,
i
x x x
Thm 16 (p351). (Optimal substructure of LCS)
Let be seqs,
and let , then
1. and
2. .
3. .
1 2
1 2
, , ,
, , ,
m
n
X x x x
Y y y y
=
`
=
)
( )
1 2
, , , ,
k
Z z z z LCS X Y = =
m n k m n
x y z x y = = = ( )
1 1 1
,
k m n
Z LCS X Y
=
( )
1
& ,
m n
m
k m
x y
Z L C S X Y
z x
=
`
)
( )
1
& ,
m n
n
k n
x y
Z L C S X Y
z y
=
`
)
So So
Yes
NO
?
m n
x y
=
?
( )
1 1 1
&
,
k m n
k m n
z x y
Z LCS X Y
= =
=
( )
1
&
,
k m
m
z x
Z LCS X Y
= ( )
1
&
,
k n
n
z y
Z LCS X Y
=
7
Notation Notation
Length of
points to the entry of
corresponding to the optimal subprob soln
chosen when computing
[ ] , c i j = ( )
,
i j
LCS X Y
[ ] [ ]
[ ] [ ] ( )
0 0 0
, 1, 1 1 , 0&
, 1 , 1, , 0&
i j
i j
if i or j
c i j c i j if i j x y
Max c i j c i j if i j x y
= =
= + > =
>
[ ] , b i j
[ ] 0 , 0 c i j
[ ] , c i j