Lecture Notes
CMSC 251
Lecture 26: Chain Matrix Multiplication
(Thursday, April 30, 1998)
Read: Section 16.1 of CLR.
Chain Matrix Multiplication:
This problem involves the question of determining the optimal sequence for
performing a series of operations. This general class of problem is important in compiler design for
code optimization and in databases for query optimization. We will study the problem in a very re
stricted instance, where the dynamic programming issues are easiest to see.
Suppose that we wish to multiply a series of matrices
A
1
A
2
...A
n
Matrix multiplication is an associative but not a commutative operation. This means that we are free to
parenthesize the above multiplication however we like, but we are not free to rearrange the order of the
matrices. Also recall that when two (nonsquare) matrices are being multiplied, there are restrictions on
the dimensions. A
p
×
q
matrix has
p
rows and
q
columns. You can multiply a
p
×
q
matrix
A
times a
q
×
r
matrix
B
, and the result will be a
p
×
r
matrix
C
. (The number of columns of
A
must equal the
number of rows of
B
.) In particular for
1
≤
i
≤
p
and
1
≤
j
≤
r
,
C
[
i, j
]=
q
X
k
=1
A
[
i, k
]
B
[
k,j
]
.
Observe that there are
pr
total entries in
C
and each takes
O
(
q
)
time to compute, thus the total time
(e.g. number of multiplications) to multiply these two matrices is
p
·
q
·
r
.
BC
=
A
p
q
q
r
p
r
Multiplication
pqr
time =
=
*
Figure 33: Matrix Multiplication.
Note that although any legal parenthesization will lead to a valid result, not all involve the same number
of operations. Consider the case of 3 matrices:
A
1
be
5
×
4
,
A
2
be
4
×
6
and
A
3
be
6
×
2
.
mult
[((
A
1
A
2
)
A
3
)]
=
(5
·
4
·
6) + (5
·
6
·
2) = 180
,
mult
[(
A
1
(
A
2
A
3
))]
=
(4
·
6
·
2) + (5
·
4
·
2) = 88
.
Even for this small example, considerable savings can be achieved by reordering the evaluation se
quence. The Chain Matrix Multiplication problem is: Given a sequence of matrices
A
1
,A
2
,...,A
n
and dimensions
p
0
,p
1
,...,p
n
where
A
i
is of dimension
p
i

1
×
p
i
, determine the multiplication se
quence that minimizes the number of operations.
Important Note:
This algorithm does not perform the multiplications, it just ﬁgures out the best order
in which to perform the multiplications.
79