Analysis of Algorithms
CS 477/677
Instructor: Monica Nicolescu
Lecture 15
Dynamic Programming
• Used for optimization problems
– Find a solution with the optimal value (minimum
or maximum)
– A set of choices must be made to get an optimal
solution
– There may be many solutions that return the
optimal value: we want to find one of them
CS 477/677 - Lecture 15
2
Matrix-Chain Multiplication
• Given a chain of matrices ⟨A1, A2, …, An⟩,
where for i = 1, 2, …, n matrix Ai has
dimensions pi-1x pi, fully parenthesize the
product A1 ∙A2 ∙∙∙An in a way that minimizes
the number of scalar multiplications.
A1 ∙ A2
p0 x p1
p1 x p2
∙∙∙ Ai ∙ Ai+1
pi-1 x pi
∙∙∙ An
pi x pi+1
CS 477/677 - Lecture 15
pn-1 x pn
3
1. The Structure of an Optimal
Parenthesization
• Notation:
Ai…j = Ai Ai+1 ∙∙∙Aj, i ≤ j
• For i < j:
Ai…j = Ai Ai+1 ∙∙∙ Aj
= Ai Ai+1 ∙∙∙ Ak Ak+1 ∙∙∙ Aj
= Ai…k Ak+1…j
• Suppose that an optimal parenthesization of
Ai…j splits the product between Ak and Ak+1,
where
i≤k<j
CS 477/677 - Lecture 15
4
Optimal Substructure
Ai…j = Ai…k Ak+1…j
• The parenthesization of the “prefix” Ai…k must be an
optimal parentesization
• If there were a less costly way to parenthesize Ai…k,
we could substitute that one in the parenthesization
of Ai…j and produce a parenthesization with a lower
cost than the optimum ⇒ contradiction!
• An optimal solution to an instance of the matrix-chain
multiplication contains within it optimal solutions to
subproblems
CS 477/677 - Lecture 15
5
2. A Recursive Solution
• Subproblem:
determine the minimum cost of parenthesizing
Ai…j = Ai Ai+1 ∙∙∙Aj
for 1 ≤ i ≤ j ≤ n
• Let m[i, j] = the minimum number of
multiplications needed to compute Ai…j
– Full problem (A1..n): m[1, n]
– i = j: Ai…i = Ai ⇒ m[i, i] = 0, for i = 1, 2, …, n
CS 477/677 - Lecture 15
6
2. A Recursive Solution
Consider the subproblem of parenthesizing
Ai…j = Ai Ai+1 ∙∙∙Aj
= Ai…k Ak+1…j
m[i, k]
for 1 ≤ i ≤ j ≤ n
pi-1pkpj
for i ≤ k < j
m[k+1,j]
• Assume that the optimal parenthesization
splits the product Ai Ai+1 ∙∙∙ Aj at k (i ≤ k < j)
m[i, j] = m[i, k]
min # of multiplications
to compute Ai…k
+
m[k+1, j]
+
pi-1pkpj
min # of multiplications # of multiplications
to compute Ak+1…j
to compute
Ai…kAk…j
CS 477/677 - Lecture 15
7
2. A Recursive Solution
m[i, j] = m[i, k]
+
m[k+1, j]
+
pi-1pkpj
• We do not know the value of k
– There are j – i possible values for k: k = i, i+1, …, j-1
• Minimizing the cost of parenthesizing the
product Ai Ai+1 ∙∙∙ Aj becomes:
0
if i = j
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj} if i < j
i≤ k<j
CS 477/677 - Lecture 15
8
3. Computing the Optimal Costs
0
if i = j
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj} if i < j
i≤k<j
• How many subproblems do we have?
– Parenthesize Ai…j
for 1 ≤ i ≤ j ≤ n ⇒ Θ (n2)
n
– One subproblem for each
choice of i and j
1
2
3
n
j
3
2
1
CS 477/677 - Lecture 15
i
9
3. Computing the Optimal Costs
0
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj}
i≤k<j
if i = j
if i < j
• How do we fill in table m[1..n, 1..n]?
– Determine which entries of the table are used in computing
m[i, j]
Ai…j = Ai…k Ak+1…j
– Fill in m such that it corresponds to solving problems of
increasing length
CS 477/677 - Lecture 15
10
3. Computing the Optimal Costs
0
if i = j
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj} if i < j
i≤k<j
• Length = 1: i = j, i = 1, 2, …, n
• Length = 2: j = i + 1, i = 1, 2, …, n-1
1
m[1, n] gives the optimal
solution to the problem
2
3
n
n
3
Compute elements on each
diagonal, starting with the
2
longest diagonal.
1
In a similar matrix s we keep
the optimal values of k. CS 477/677 - Lecture 15
j
i
11
Example: min {m[i, k] + m[k+1, j] + pi-1pkpj}
m[2, 2] + m[3, 5] + p1p2p5
m[2, 3] + m[4, 5] + p1p3p5
m[2, 4] + m[5, 5] + p1p4p5
m[2, 5] = min
1
2
3
4
5
k=2
k=3
k=4
6
6
5
4
j
3
2
• Values m[i, j] depend
only on values that have
been previously
computed
1
i
CS 477/677 - Lecture 15
12
Example min {m[i, k] + m[k+1, j] + pi-1pkpj}
Compute A1 ∙ A2 ∙ A3
1
• A1: 10 x 100 (p0 x p1)
3
• A2: 100 x 5 (p1 x p2)
2
• A3: 5 x 50
1
(p2 x p3)
2
1
2
2
7500
5000
3
25000
0
0
0
m[i, i] = 0 for i = 1, 2, 3
m[1, 2] = m[1, 1] + m[2, 2] + p0p1p2
(A1A2)
= 0 + 0 + 10 *100* 5 = 5,000
m[2, 3] = m[2, 2] + m[3, 3] + p1p2p3
(A2A3)
= 0 + 0 + 100 * 5 * 50 = 25,000
m[1, 3] = min m[1, 1] + m[2, 3] + p0p1p3 = 75,000 (A1(A2A3))
m[1, 2] + m[3, 3] + p0p2p3 = 7,500 ((A1A2)A3)
CS 477/677 - Lecture 15
13
4. Construct the Optimal Solution
• Store the optimal
choice made at
each subproblem
• s[i, j] = a value of k
such that an optimal
parenthesization of
Ai..j splits the product
between Ak and Ak+1
1
2
3
n
n
k
j
3
2
1
i
CS 477/677 - Lecture 15
14
4. Construct the Optimal Solution
• s[1, n] is associated with
the entire product A1..n
– The final matrix
multiplication will be split
at k = s[1, n]
A1..n = A1..k ∙ Ak+1..n
A1..n = A1..s[1, n] ∙ As[1, n]+1..n
– For each subproduct
recursively find the
corresponding value of k
that results in an optimal
parenthesization
1
2
3
n
n
j
3
2
1
CS 477/677 - Lecture 15
i
15
4. Construct the Optimal Solution
• s[i, j] = value of k such that the optimal
parenthesization of Ai Ai+1 ∙∙∙Aj splits the
product between Ak and Ak+1
1
2
3
4
5
6
6
3
3
3
5
5
-
5
3
3
2
-
3
3
-
4
-
-
2
3
3
1
1
1
-
4
3
• s[1, n] = 3 ⇒ A1..6 = A1..3 A4..6
• s[1, 3] = 1 ⇒ A1..3 = A1..1 A2..3
• s[4, 6] = 5 ⇒ A4..6 = A4..5 A6..6
j
i
CS 477/677 - Lecture 15
16
4. Construct the Optimal Solution
PRINT-OPT-PARENS(s, i, j)
1
2
3
4
5
6
6
3
3
3
5
5
-
then print “A”i
5
4
3
3
-
4
-
2
3
3
2
-
-
else print “(”
3
3
1
1
1
-
if i = j
PRINT-OPT-PARENS(s, i, s[i, j])
PRINT-OPT-PARENS(s, s[i, j] + 1, j)
print “)”
3
j
i
CS 477/677 - Lecture 15
17
Example: A1∙∙∙A6
( ( A1 ( A2 A3 ) ) ( ( A4 A5 ) A6 ) )
s[1..6, 1..6]
PRINT-OPT-PARENS(s, i, j)
if i = j
6
then print “A”i
5
else print “(”
4
PRINT-OPT-PARENS(s, i, s[i, j])
3
PRINT-OPT-PARENS(s, s[i, j] + 1, j)
2
print “)”
1
2
3
4
5
6
3
3
3
3
3
3
3
3
3
5
4
-
5
-
-
1
1
-
2
-
-
j
1
P-O-P(s, 1, 6) s[1, 6] = 3
i = 1, j = 6 “(“ P-O-P (s, 1, 3) s[1, 3] = 1
i
i = 1, j = 3 “(“ P-O-P(s, 1, 1) ⇒ “A1”
P-O-P(s, 2, 3) s[2, 3] = 2
i = 2, j = 3
“(“ P-O-P (s, 2, 2) ⇒ “A2”
P-O-P (s, 3, 3) ⇒ “A3”
“)”
… - Lecture 15
CS 477/677
18
“)”
Memoization
• Top-down approach with the efficiency of typical
bottom-up dynamic programming approach
• Maintains an entry in a table for the solution to each
subproblem
– memoize the inefficient recursive top-down algorithm
• When a subproblem is first encountered its solution is
computed and stored in that table
• Subsequent “calls” to the subproblem simply look up
that value
CS 477/677 - Lecture 15
19
Memoized Matrix-Chain
Alg.: MEMOIZED-MATRIX-CHAIN(p)
1. n ← length[p]
2. for i ← 1 to n
3.
4.
do for j ← i to n
do m[i, j] ←∞
Initialize the m table with
large values that indicate
whether the values of m[i, j]
have been computed
5. return LOOKUP-CHAIN(p, 1, n)
CS 477/677 - Lecture 15
Top-down approach
20
Memoized Matrix-Chain
Alg.: LOOKUP-CHAIN(p, i, j)
1.
2.
3.
if m[i, j] < ∞
then return m[i, j]
if i = j
4.
then m[i, j] ← 0
5.
else for k ← i to j – 1
6.
Running time is O(n3)
m[i, j] = min {m[i, k] + m[k+1, j] + pi-1pkpj}
i≤k<j
do q ← LOOKUP-CHAIN(p, i, k) +
LOOKUP-CHAIN(p, k+1, j) + pi-1pkpj
7.
8.
9. return m[i, j]
if q < m[i, j]
then m[i, j] ← q
CS 477/677 - Lecture 15
21
Dynamic Progamming vs.
Memoization
• Advantages of dynamic programming vs.
memoized algorithms
– No overhead for recursion
– The regular pattern of table accesses may be
used to reduce time or space requirements
• Advantages of memoized algorithms vs.
dynamic programming
– More intuitive
CS 477/677 - Lecture 15
22
Elements of Dynamic Programming
• Optimal Substructure
– An optimal solution to a problem contains within
it an optimal solution to subproblems
– Optimal solution to the entire problem is build in a
bottom-up manner from optimal solutions to
subproblems
• Overlapping Subproblems
– If a recursive algorithm revisits the same
subproblems again and again ⇒ the problem
has overlapping subproblems
CS 477/677 - Lecture 15
23
Optimal Substructure - Examples
• Assembly line
– Fastest way of going through a station j contains:
the fastest way of going through station j-1 on
either line
• Matrix multiplication
– Optimal parenthesization of Ai ∙ Ai+1 ∙∙∙ Aj that
splits the product between Ak and Ak+1 contains:
• an optimal solution to the problem of parenthesizing Ai..k
• an optimal solution to the problem of parenthesizing
Ak+1..j
CS 477/677 - Lecture 15
24
Parameters of Optimal Substructure
• How many subproblems are used in an
optimal solution for the original problem
– Assembly line:
One subproblem (the line that gives best time)
– Matrix multiplication: Two subproblems (subproducts Ai..k, Ak+1..j)
• How many choices we have in determining
which subproblems to use in an optimal
solution
– Assembly line: Two choices (line 1 or line 2)
– Matrix multiplication: j - i choices for k (splitting the product)
CS 477/677 - Lecture 15
25
Parameters of Optimal Substructure
• Intuitively, the running time of a dynamic
programming algorithm depends on two factors:
– Number of subproblems overall
– How many choices we examine for each subproblem
• Assembly line
– Θ(n) subproblems (n stations)
– 2 choices for each subproblem
Θ(n) overall
• Matrix multiplication:
– Θ(n2) subproblems (1 ≤ i ≤ j ≤ n)
– At most n-1 choices
CS 477/677 - Lecture 15
Θ(n3) overall
26
Longest Common Subsequence
• Given two sequences
X = ⟨x1, x2, …, xm⟩
Y = ⟨y1, y2, …, yn⟩
find a maximum length common
subsequence (LCS) of X and Y
• E.g.:
X = ⟨A, B, C, B, D, A, B⟩
• Subsequence of X:
– A subset of elements in the sequence taken in order
(but not necessarily consecutive)
⟨A, B, D⟩, ⟨B, C, D, B⟩, etc.
CS 477/677 - Lecture 15
27
27
Example
X = ⟨A, B, C, B, D, A, B⟩
X = ⟨A, B, C, B, D, A, B⟩
Y = ⟨B, D, C, A, B, A⟩
Y = ⟨B, D, C, A, B, A⟩
• ⟨B, C, B, A⟩ and ⟨B, D, A, B⟩ are longest
common subsequences of X and Y (length = 4)
• ⟨B, C, A⟩ , however is not a LCS of X and Y
CS 477/677 - Lecture 15
28
28
Brute-Force Solution
• For every subsequence of X, check whether
it’s a subsequence of Y
• There are 2m subsequences of X to check
• Each subsequence takes Θ(n) time to check
– scan Y for first letter, from there scan for second,
and so on
• Running time: Θ(n2m)
CS 477/677 - Lecture 15
29
1. Making the choice
X = ⟨A, B, D, E⟩
Y = ⟨Z, B, E⟩
• Choice: include one element into the
common sequence (E) and solve the
resulting subproblem
X = ⟨A, B, D, ✕
G⟩
X = ⟨A, B, D, G⟩
Y = ⟨Z, B, D⟩
Y = ⟨Z, B,✕
D⟩
• Choice: exclude an element from a string
and solve the resulting subproblem
CS 477/677 - Lecture 15
30
Readings
• Chapters 14, 15
CS 477/677 - Lecture 15
31
© Copyright 2026 Paperzz