Dynamic Programming Solution To The Matrix-Chain Multiplication Problem
Dynamic Programming Solution To The Matrix-Chain Multiplication Problem
Dynamic Programming Solution To The Matrix-Chain Multiplication Problem
Methodology
(1) Characterize the Structure of an Optimal Solution.
Claim 1 Suppose that in and optimal way to parenthesize Ai Ai+1 . . . Aj , we split the product between Ak and
Ak+1 . Then the way we parenthesize the prefix subchain Ai Ai+1 . . . Ak within this optimal parenthesization
of Ai Ai+1 . . . Aj must be an optimal parenthesization of Ai Ai+1 . . . Ak . And the way we parenthesize the
suffix subchain Ak+1 Ak+2 . . . Aj within this optimal parenthesization of Ai Ai+1 . . . Aj must be an optimal
parenthesization of Ak+1 Ak+2 . . . Aj .
prefix subchain suffix subchain
((Ai Ai+1 . . . Ak )(Ak+1 Ak+2 . . . Aj ))
Proof: By contradiction, suppose there were a less costly way to parenthesize Ai Ai+1 . . . Ak , then the
optimal parenthesization of Ai Ai+1 . . . Aj could be replaced with this parenthesization, yielding another
way to parenthesize Ai Ai+1 . . . Aj whose cost was lower than the optimum: a contradiction. An identical
argument applies to the subchain Ak+1 Ak+2 . . . Aj in the optimal parenthesization of Ai Ai+1 . . . Aj . 2
1
(2) Recursively Define the Value of the Optimal Solution. First, we define in English the quan-
tity we shall later define recursively. Let Ai..j be the matrix that results from evaluating the product
Ai Ai+1 . . . Aj , where i ≤ j. Let C[i, j] be the minimum number of scalar multiplications needed to compute
the matrix Ai...j . If i = j, no scalar multiplications are needed. Thus, C[i, i] = 0 for i = 1, 2, . . . , n. When
i < j, we assume that in an optimal parenthesization, the product Ai Ai+1 . . . Aj is split between Ak and
Ak+1 , where i ≤ k < j. Then C[i, j] = C[i, k] + C[k + 1, j] + pi−1 pk pj . We don’t know the value of k in the
optimal parenthesization, but there are only j − i possible values for k, namely k = i, i + 1, . . . , j + 1. So we
should check all these values to find the best. We thus have the following recurrence.
Claim 2 (
0 if i = j,
C[i, j] =
mini≤k<j {C[i, k] + C[k + 1, j] + pi−1 pk pj } if i < j.
Proof: The correctness of this recursive definition is embodied in the paragraph which precedes it. 2
(3) Compute the Value of the Optimal Solution Bottom-up. Consider the following piece os pseu-
docode, where p =< p0 , p1 , . . . , pn >, with p.length = n + 1, table C[1..n, 1..n] stores the costs and table
S[1..n − 1, 2..n] stores which index of k achieved the optimal cost in computing C[i, j].
The way we fill in the tables C and S here is more tricky than that in the knapsack problem and
checkboard problem. In knapsack problem and checkboard problem, we fill in the tables by rows. Here, the
recurrence formula shows that the cost C[i, j] of computing a matrix-chain product of j − i + 1 matrices
depends only on the costs of computing matrix-chain products of fewer than j − i + 1 matrices. Thus, the
tables C and S should be filled in by increasing lengths of the matrix chains. This corresponds to filling in
the tables diagonally.
matrix-chain-order(p)
1 n = p.length − 1
2 let C[1..n, 1..n] and S[1..n − 1, 2..n] be new tables
3 for i = 1 to n
4 C[i, i] = 0
5 for l = 2 to n //l is the chain length
6 for i = 1 to n − l + 1
7 j =i+l−1
8 C[i, j] = 0
9 for k = i to j − 1
10 q = C[i, k] + C[k + 1, j] + pi−1 pk pj
11 if q < C[i, j]
12 C[i, j] = q
13 S[i, j] = k
14 return C and S
Claim 3 When the above procedure terminates, C[i, j] will contain the minimum number of scalar multipli-
cations needed to compute the matrix Ai...j , and S[i, j] will contain the index of k achieved the optimal cost
in computing C[i, j].
Proof: The correctness of the above procedure is based on the fact that it correctly implements the recursive
definition given above. The base case is properly handled in Lines 3-4, and the recursive case is properly
handled in Lines 5-13. At each step, the C[i, j] cost computed in lines 10-13 depends only on table entries
C[i, k] and C[k + 1, j] already computed. Lines 8-12 correctly compute mini≤k<j {C[i, k] + C[k + 1, j] +
pi−1 pk pj }, and C[i, j] is set to this value in Line 12. Lines 8-13 correctly compute arg mini≤k<j {C[i, k] +
2
C[k + 1, j] + pi−1 pk pj }, and S[i, j] is set to this value in Line 13. 2
(4) Construct the Optimal Solution from the Computed Information. Consider the following
piece of pseudocode, where S is the table computed by MATRIX-CHAIN-ORDER.
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 “)”
Claim 4 The above procedure correctly prints an optimal parenthesization of < Ai , Ai+1 , . . . , Aj >.
Proof: S[i, j] indicates the value of k such that an optimal parenthesization of Ai Ai+1 . . . Aj splits the
product between Ak and Ak+1 . The above procedure just recursively splits the parenthesization of a chain
into the parenthesization of its prefix chain and the parenthesization of its suffix chain. 2
(5) Running Time and Space Requirements. The procedure MATRIX-CHAIN-ORDER runs in
O(n3 ) due to the nested loop defined in Lines 5, 6 and 9. We can also show that the running time of this
algorithm is in fact also Ω(n3 ) (exercise). The algorithm requires Θ(n2 ) space to store the C and S tables.
The procedure PRINT-OPTIMAL-PARENS runs in Θ(n) time and uses no additional space. The overall
running time is Θ(n3 ) and the space requirement is Θ(n2 ).
3
C
6 6
5 15125 5 S
j 4 11875 10500 4 i 6 1
9375 7125 5375 j 5 3 2 i
3 3
7875 4375 2500 3500 4 3 3 3
2 2
15750 2625 750 1000 5000 3 3 3 3 4
1 1
0 0 0 0 0 0 2 1 3 3 5 5
1 2 3 4 5
A1 A2 A3 A4 A5 A6
C S
2 15750 0 2 1
1 0 1
1 2 3 4 5 6 1 2 3 4 5 6
i i
Figure 1: The C and S table computed by Matrix-Chain-Order for n = 6 and the following matrix
dimensions:
matrix A1 A2 A3 A4 A5 A6
dimension 30 × 35 35 × 15 15 × 5 5 × 10 10 × 20 20 × 25
The tables at the top are rotated to make the main diagonal runs horizontally. The C table uses only the
main diagonal and upper triangle, since C[i, j] is only defined for i ≤ j. The S table uses only the upper
triangle. Of the colored entries, the pairs that have the same color are taken together in line 10 when
computing
C[2, 2] + C[3, 5] + p1 p2 p5 = 0 + 2500 + 35 · 15 · 20 = 13, 000
C[2, 5] = min C[2, 3] + C[4, 5] + p1 p3 p5 = 2625 + 1000 + 35 · 5 · 20 = 7125
C[2, 4] + C[5, 5] + p1 p4 p5 = 4375 + 0 + 35 · 10 · 20 = 11, 375
= 7125.
The tables at the bottom are normally orientated. The tables are filled in by diagonals.