CS502-Fundamentals of Algorithms Lecture No.
20
Lecture No.20
We do not want to calculate m entries recursively. So how should we proceed? We will
fill m along the diagonals. Here is how. Set all m[i, i] = 0 using the base condition.
Compute cost for multiplication of a sequence of 2 matrices. These are m[1, 2],m[2,
3],m[3, 4], . . . ,m[n - 1, n]. m[1, 2], for example is
m[1, 2] = m[1, 1] +m[2, 2] + p0 · p1 · p2
For example, for m for product of 5 matrices at this stage would be:
Next, we compute cost of multiplication for sequences of three matrices. These are
m[1, 3],m[2, 4],m[3, 5], . . . ,m[n - 2, n]. m[1, 3], for example is
We repeat the process for sequences of four, five and higher number of matrices. The
final result will end up in m[1, n].
Example: Let us go through an example. We want to find the optimal multiplication
order for
We will compute the entries of the m matrix starting with the base condition. We first fill
that main diagonal:
Page 1 of 4
© Copyright Virtual University of Pakistan
CS502-Fundamentals of Algorithms Lecture No.20
Next, we compute the entries in the first super diagonal, i.e., the diagonal above the main
diagonal:
m[1, 2] = m[1, 1] +m[2, 2] + p0 · p1 · p2 = 0 + 0 + 5 · 4 · 6 = 120
m[2, 3] = m[2, 2] +m[3, 3] + p1 · p2 · p3 = 0 + 0 + 4 · 6 · 2 = 48
m[3, 4] = m[3, 3] +m[4, 4] + p2 · p3 · p4 = 0 + 0 + 6 · 2 · 7 = 84
m[4, 5] = m[4, 4] +m[5, 5] + p3 · p4 · p5 = 0 + 0 + 2 · 7 · 3 = 42
The matrix m now looks as follows:
We now proceed to the second super diagonal. This time, however, we will need to try
two possible values for k. For example, there are two possible splits for computing m[1,
3]; we will choose the split that yields the minimum:
m[1, 3] = m[1, 1] +m[2, 3] + p0 · p1 · p3 == 0 + 48 + 5 · 4 · 2 = 88
m[1, 3] = m[1, 2] +m[3, 3] + p0 · p2 · p3 = 120 + 0 + 5 · 6 · 2 = 180
the minimum m[1, 3] = 88 occurs with k = 1
Similarly, for m[2, 4] and m[3, 5]:
m[2, 4] = m[2, 2] +m[3, 4] + p1 · p2 · p4 = 0 + 84 + 4 · 6 · 7 = 252
m[2, 4] = m[2, 3] +m[4, 4] + p1 · p3 · p4 = 48 + 0 + 4 · 2 · 7 = 104
minimum m[2, 4] = 104 at k = 3
m[3, 5] = m[3, 3] +m[4, 5] + p2 · p3 · p5 = 0 + 42 + 6 · 2 · 3 = 78
m[3, 5] = m[3, 4] +m[5, 5] + p2 · p4 · p5 = 84 + 0 + 6 · 7 · 3 = 210
minimum m[3, 5] = 78 at k = 3
With the second super diagonal computed, the m matrix looks as follow:
We repeat the process for the remaining diagonals. However, the number of possible
splits (values of k) increases:
Page 2 of 4
© Copyright Virtual University of Pakistan
CS502-Fundamentals of Algorithms Lecture No.20
m[1, 4] = m[1, 1] +m[2, 4] + p0 · p1 · p4 = 0 + 104 + 5 · 4 · 7 = 244
m[1, 4] = m[1, 2] +m[3, 4] + p0 · p2 · p4 = 120 + 84 + 5 · 6 · 7 = 414
m[1, 4] = m[1, 3] +m[4, 4] + p0 · p3 · p4 = 88 + 0 + 5 · 2 · 7 = 158
minimum m[1, 4] = 158 at k = 3
m[2, 5] = m[2, 2] +m[3, 5] + p1 · p2 · p5 = 0 + 78 + 4 · 6 · 3 = 150
m[2, 5] = m[2, 3] +m[4, 5] + p1 · p3 · p5 = 48 + 42 + 4 · 2 · 3 = 114
m[2, 5] = m[2, 4] +m[5, 5] + p1 · p4 · p5 = 104 + 0 + 4 · 7 · 3 = 188
minimum m[2, 5] = 114 at k = 3
The matrix m at this stage is:
That leaves the m[1, 5] which can now be computed:
m[1, 5] = m[1, 1] +m[2, 5] + p0 · p1 · p5 = 0 + 114 + 5 · 4 · 3 = 174
m[1, 5] = m[1, 2] +m[3, 5] + p0 · p2 · p5 = 120 + 78 + 5 · 6 · 3 = 288
m[1, 5] = m[1, 3] +m[4, 5] + p0 · p3 · p5 = 88 + 42 + 5 · 2 · 3 = 160
m[1, 5] = m[1, 4] +m[5, 5] + p0 · p4 · p5 = 158 + 0 + 5 · 7 · 3 = 263
minimum m[1, 5] = 160 at k = 3
We thus have the final cost matrix.
Here is the order in which m entries are calculated
and the split k values that led to a minimum m[i, j] value
Page 3 of 4
© Copyright Virtual University of Pakistan
CS502-Fundamentals of Algorithms Lecture No.20
Based on the computation, the minimum cost for multiplying the five matrices is 160 and
the optimal order for multiplication is
((A1(A2A3))(A4A5))
This can be represented as a binary tree
Figure 6.2: Optimum matrix multiplication order for the five matrices example
Page 4 of 4
© Copyright Virtual University of Pakistan