You are on page 1of 31

Lecture 11

Dynamic Programming

CSE373: Design
and Analysis of
Matrix-chain Multiplication
Matrix-chain Multiplication
Matrix-chain Multiplication

Cost of Multiplying two Matrices
Matrix-chain Multiplication
Number of Parenthesizations
Number of Parenthesizations
Establishing the Recurrence

Consider the subproblem of parenthesizing


Ai…j = Ai Ai+1  Aj for 1  i  j  n
pi-1pkpj

= Ai…k
m[i, k] Ak+1…j m[k+1,j] for i  k < j

Assume that the optimal parenthesization splits the
product Ai Ai+1  Aj at k (i  k < j)
m[i, j] = m[i, k] + m[k+1, j] + pi-1pkpj

min # of multiplications min # of multiplications # of multiplications


to compute Ai…k to compute Ak+1…j to compute Ai…
kAk…j
Recursive Matrix-chain
Recursive Matrix-chain
Running Time of Recursive Matrix-
chain
Running Time of Recursive Matrix-
chain
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 2 3 n
m[1, n] gives the optimal
1
solution to the problem
2

3
i Compute rows from bottom to top and
from left to right
In a similar matrix s we keep the optimal
se values of k
n c on
d
firs
j t
Matrix-Chain-Order
Alg.: MATRIX-CHAIN-ORDER(p)
1. n = p.length-1

2. let m[1..n, 1..n] and s[1..n-1, 2..n] be new tabels

3. for i = 1 to n

4. m[i, i] = 0
5. for l = 2 to n

6. for i = 1 to n – l + 1
7. j=i+l–1
8. m[i, j] = 
9. for k = i to j – 1
10. q = m[i, k] + m[k+1, j] + pi-1pkpj
11. if q < m[i, j]
12. m[i, j] = q
13. s[i, j] = k
Example:
• p = (10, 5, 1, 10, 2, 10)
• [10×5]×[5×1]×[1×10]×[10×2]×[2×10]

1 2 3 4 5 1 2 3 4 5

1 1

2 2

3 3

4 4

5 5

c(i,j), i ≤j s(i,j), i ≤j
Example:
• p = (10, 5, 1, 10, 2, 10)
• [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
• c(i,i) = 0

1 2 3 4 5 1 2 3 4 5

1 0 1

2 0 2

3 0 3

4 0 4

5 0 5

c(i,j), i ≤j s(i,j), i ≤j
Example:
• p = (10, 5, 1, 10, 2, 10)
• [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
• c(i,i+1) = pipi+1pi+2
• s(i,i+1) = i
1 2 3 4 5 1 2 3 4 5

1 0 50 1 1

2 0 50 2 2

3 0 20 3 3

4 0 200 4 4

5 0 5

c(i,j), i ≤j s(i,j), i ≤j
Example:
• p = (10, 5, 1, 10, 2, 10)
• [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
• c(i,i+2) = min{c(i,i) + c(i+1,i+2) + pipi+1pi+3,
c(i,i+1) + c(i+2,i+2) + pipi+2pi+3}
1 2 3 4 5 1 2 3 4 5

1 0 50 1 1

2 0 50 2 2

3 0 20 3 3

4 0 200 4 4

5 0 5

c(i,j), i ≤j s(i,j), i ≤j
Example:
• p = (10, 5, 1, 10, 2, 10)
• [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
• c(2,4) = min{c(2,2) + c(3,4) + p2p3p5, c(2,3) + c(4,4) +
p2p4p5}
• c(3,5)
1
=…
2 3 4 5 1 2 3 4 5

1 0 50 150 1 1 2

2 0 50 30 2 2 2

3 0 20 40 3 3 3

4 0 200 4 4

5 0 5

c(i,j), i ≤j s(i,j), i ≤j
Example:
• [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
• c(i,i+3) = min{c(i,i) + c(i+1,i+3) + pipi+1pi+4,
c(i,i+1) + c(i+2,i+3) + pipi+2pi+4,
c(i,i+2) + c(i+3,i+3) + pipi+3pi+4}
1 2 3 4 5 1 2 3 4 5

1 0 50 150 90 1 1 2 2

2 0 50 30 90 2 2 2 2

3 0 20 40 3 3 3

4 0 200 4 4

5 0 5

c(i,j), i ≤j s(i,j), i ≤j
Example:
• [10×5]×[5×1]×[1×10]×[10×2]×[2×10]
• c(i,i+4) = min{c(i,i) + c(i+1,i+4) + pipi+1pi+5,
c(i,i+1) + c(i+2,i+4) + pipi+2pi+5, c(i,i+2) + c(i+3,i+4) +
pipi+3pi+5, c(i,i+3) + c(i+4,i+4) + pipi+4pi+5}

1 2 3 4 5 1 2 3 4 5

1 0 50 150 90 190 1 1 2 2 2

2 0 50 30 90 2 2 2 2

3 0 20 40 3 3 3

4 0 200 4 4

5 0 5

c(i,j), i ≤j s(i,j), i ≤j
Print optimal parenthesis
Alg.: PRINT-OPTIMAL-PARENS(s, i, j)

1. if i == j

2. print “A” i

3. else print “(”

4. PRINT-OPTIMAL-PARENS(s, i, s[i, j])

5. PRINT-OPTIMAL-PARENS(s, s[i, j] + 1, j)

6. print “)”
Example:

Optimal multiplication sequence
• s(1,5) = 2
▶ M15 = M12×M35

1 2 3 4 5 1 2 3 4 5

1 0 50 150 90 190 1 1 2 2 2

2 0 50 30 90 2 2 2 2

3 0 20 40 3 3 3

4 0 200 4 4

5 0 5

c(i,j), i ≤j s(i,j), i ≤j
Example:
• M15 = M12×M35
• s(1,2) = 1 ▶ M12 = M11×M22
→ M15 = (M11×M22)×M35

1 2 3 4 5 1 2 3 4 5

1 0 50 150 90 190 1 1 2 2 2

2 0 50 30 90 2 2 2 2

3 0 20 40 3 3 4

4 0 200 4 4

5 0 5

c(i,j), i ≤j s(i,j), i ≤j
Example:
• M15 = (M11×M22)×M35
• s(3,5) = 4 ▶ M35 = M34×M55
→ M15 = (M11×M22)×(M34×M55)

1 2 3 4 5 1 2 3 4 5

1 0 50 150 90 190 1 1 2 2 2

2 0 50 30 90 2 2 2 2

3 0 20 40 3 3 4

4 0 200 4 4

5 0 5

c(i,j), i ≤j s(i,j), i ≤j
Example:
• M15 = (M11×M22)×(M34×M55)
• s(3,4) = 3 ▶ M34 = M33×M44
→ M15 = (M11×M22)×((M33×M44)×M55)

1 2 3 4 5 1 2 3 4 5

1 0 50 150 90 190 1 1 2 2 2

2 0 50 30 90 2 2 2 2

3 0 20 40 3 3 4

4 0 200 4 4

5 0 5

c(i,j), i ≤j s(i,j), i ≤j
Analysis
lOur algorithm computes the minimum-cost table m
and the split table s
lThe optimal solution can be constructed from the
split table s
lEach entry s[i, j ]=k shows where to split the product
Ai Ai+1 … Aj for the minimum cost
lThere are 3 nested loops and each can iterate at
most n times, so the total running time is (n3).
Memoization
Memoization
Memoized Matrix-Chain
Alg.: MEMOIZED-MATRIX-CHAIN(p)

1. n  length[p] – 1

2. for i  1 to n
Initialize the m table with
large values that indicate
3. do for j  i to n whether the values of m[i, j]
have been computed

4. do m[i, j]  
Top-down approach
5. return LOOKUP-CHAIN(p, 1, n)
Memoized Matrix-Chain
Alg.: LOOKUP-CHAIN(p, i, j)

1. if m[i, j] < 

2. then return m[i, j]

3. if i = j

4. then m[i, j]  0

5. else for k  i to j – 1

6. do q  LOOKUP-CHAIN(p, i, k) +

LOOKUP-CHAIN(p, k+1, j) + pi-1pkpj

7. if q < m[i, j]