0% found this document useful (0 votes)
5 views25 pages

Lecture 20ppt

The document outlines the dynamic programming approach to solve the Chain Matrix Multiplication problem, detailing how to compute the minimum number of multiplications needed to multiply a chain of matrices. It presents a recursive formulation and explains how to fill a table of solutions bottom-up, starting from the base case where a single matrix requires zero multiplications. The final result provides an optimal order for multiplying the matrices to minimize computational cost.

Uploaded by

aroojabbas03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views25 pages

Lecture 20ppt

The document outlines the dynamic programming approach to solve the Chain Matrix Multiplication problem, detailing how to compute the minimum number of multiplications needed to multiply a chain of matrices. It presents a recursive formulation and explains how to fill a table of solutions bottom-up, starting from the base case where a single matrix requires zero multiplications. The final result provides an optimal order for multiplying the matrices to minimize computational cost.

Uploaded by

aroojabbas03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Chain Matrix Multiplication

Dynamic Programming Formulation


• We will store the solutions to the subproblem
in a table and build the table bottom-up
(why)?
• For 1 ≤ i ≤ j ≤ n, let m[i, j] denote the
minimum number of multiplications needed to
compute Ai..j .
• The optimum can be described by the
following recursive formulation.

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

For example, for m for product of 5 matrices at


this stage would be:
←m[1, 2]
m[1, 1]

←m[2, 3]
m[2, 2]

←m[3, 4]
m[3, 3]

←m[4, 5]
m[4, 4]

m[5, 5]

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

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

Algorithms – p. 379
Chain Matrix Multiplication

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

Algorithms – p. 380
Chain Matrix Multiplication

after product of 2 matrices (first super diagonal)

0 120
0 48
0 84
0 42
0

Algorithms – p. 381
Chain Matrix Multiplication

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
minimum m[1, 3] = 88 at k = 1

Algorithms – p. 382
Chain Matrix Multiplication

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

Algorithms – p. 383
Chain Matrix Multiplication

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

Algorithms – p. 384
Chain Matrix Multiplication

product of 3 matrices, (second super diagonal)

0 120 88
0 48 104
0 84 78
0 42
0

Algorithms – p. 385
Chain Matrix Multiplication

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

Algorithms – p. 386
Chain Matrix Multiplication

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

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

Final cost matrix


0 120 88 158 160
0 0 48 104 114
0 0 0 84 78
0 0 0 0 42
0 0 0 0 0

Algorithms – p. 390
Chain Matrix Multiplication

Order in which m entries are calculated


0 1 5 8 10
0 0 2 6 9
0 0 0 3 7
0 0 0 0 4
0 0 0 0 0

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

Optimal Order for multiplication


((A1 (A2 A3 ))(A4 A5 ))

1 4

A1 2 A4 A5

A2 A3

Algorithms – p. 393

You might also like