Lecture 20ppt
Lecture 20ppt
Algorithms – p. 369
Chain Matrix Multiplication
• If i = j, there is only one matrix and thus
m[i, i] = 0 (the diagonal entries).
• If i < j, the we are asking for the product Ai..j .
• This can be split by considering each k,
i ≤ k < j, as Ai..k times Ak+1..j .
Algorithms – p. 370
Chain Matrix Multiplication
• The optimum time to compute Ai..k is m[i, k]
and optimum time for Ak+1..j is in m[k + 1, j].
• Since Ai..k is a pi−1 × pk matrix and Ak+1..j is
pk × pj matrix, the time to multiply them is
pi−1 × pk × pj .
Algorithms – p. 371
Chain Matrix Multiplication
• This suggests the following recursive rule:
m[i, i] = 0
m[i, j] = min (m[i, k] + m[k + 1, j] + pi−1 pk pj )
i≤k<j
Algorithms – p. 372
Chain Matrix Multiplication
• We do not want to calculate m entries
recursively.
• How should we proceed?
• We will fill m along the diagonals. Here is
how.
Algorithms – p. 373
Chain Matrix Multiplication
• 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
Algorithms – p. 374
Chain Matrix Multiplication
Algorithms – p. 375
Chain Matrix Multiplication
• 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
m[1, 1] + m[2, 3] + p0 · p1 · p3
m[1, 3] = min
m[1, 2] + m[3, 3] + p0 · p2 · p3
Algorithms – p. 376
Chain Matrix Multiplication
• We repeat the process for sequences of four,
five and higher number of matrices.
• The final result will end up in m[1, n].
• Let us go through an example. We want to
find the optimal multiplication order for
A1 · A2 · A3 · A4 · A5
(5×4) (4×6) (6×2) (2×7) (7×3)
Algorithms – p. 377
Chain Matrix Multiplication
Initially
0
0
0
0
0
Algorithms – p. 378
Chain Matrix Multiplication
Algorithms – p. 379
Chain Matrix Multiplication
Algorithms – p. 380
Chain Matrix Multiplication
0 120
0 48
0 84
0 42
0
Algorithms – p. 381
Chain Matrix Multiplication
Algorithms – p. 382
Chain Matrix Multiplication
Algorithms – p. 383
Chain Matrix Multiplication
Algorithms – p. 384
Chain Matrix Multiplication
0 120 88
0 48 104
0 84 78
0 42
0
Algorithms – p. 385
Chain Matrix Multiplication
Algorithms – p. 386
Chain Matrix Multiplication
Algorithms – p. 387
Chain Matrix Multiplication
Product of 4 matrices
0 120 88 158
0 48 104 114
0 84 78
0 42
0
Algorithms – p. 388
Chain Matrix Multiplication
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
Algorithms – p. 389
Chain Matrix Multiplication
Algorithms – p. 390
Chain Matrix Multiplication
Algorithms – p. 391
Chain Matrix Multiplication
Split k values
0 1 1 3 3
0 2 3 3
0 3 3
0 4
0
Algorithms – p. 392
Chain Matrix Multiplication
1 4
A1 2 A4 A5
A2 A3
Algorithms – p. 393