Lecture 19ppt
Lecture 19ppt
Lecture 19ppt
Algorithms – p. 352
Chain Matrix Multiply
• Suppose we wish to multiply a series of
matrices:
A1 A2 . . . An
• In what order should the multiplication be
done?
Algorithms – p. 353
Matrix Multiplication
• A p × q matrix A can be multiplied with a
q × r matrix B.
• The result will be a p × r matrix C.
• In particular, for 1 ≤ i ≤ p and 1 ≤ j ≤ r,
q
X
C[i, j] = A[i, k]B[k, j]
k=1
Algorithms – p. 354
Matrix Multiplication
• There are (p · r) total entries in C and each
takes O(q) to compute.
q
X
C[i, j] = A[i, k]B[k, j]
k=1
Algorithms – p. 355
Chain Matrix Multiplication
• Consider the case of 3 matrices: A1 is 5 × 4,
A2 is 4 × 6 and A3 is 6 × 2
• The multiplication can be carried out either as
((A1 A2 )A3 ) or (A1 (A2 A3 )).
• The cost of the two is
((A1 A2 )A3 ) = (5 · 4 · 6) + (5 · 6 · 2)= 180
(A1 (A2 A3 )) = (4 · 6 · 2) + (5 · 4 · 2) = 88
Algorithms – p. 356
Chain Matrix Multiplication
• There is considerable savings achieved even
for this simple example.
• In general, however, in what order should we
multiply a series of matrices A1 A2 . . . An .
• Matrix multiplication is an associative but not
commutative operation.
• We are free to add parenthesis the above
multiplication but the order of matrices can
not be changed.
Algorithms – p. 357
Chain Matrix Multiplication
Algorithms – p. 358
Chain Matrix Multiplication
• We could write a procedure that tries all
possible parenthesizations.
• Unfortunately, the number of ways of
parenthesizing an expression is very large.
Algorithms – p. 359
Chain Matrix Multiplication
• If there are n items, there are n − 1 ways in
which outer most pair of parentheses can
placed.
(A1 )(A2 A3 A4 . . . An )
or (A1 A2 )(A3 A4 . . . An )
or (A1 A2 A3 )(A4 . . . An )
... ...
or (A1 A2 A3 A4 . . . An−1 )(An )
Algorithms – p. 360
Chain Matrix Multiplication
Algorithms – p. 361
Chain Matrix Multiplication
• Since these are independent choices, if there
are L ways of parenthesizing the left sublist
and R ways to parenthesize the right sublist,
then the total is L · R.
Algorithms – p. 362
Chain Matrix Multiplication
• This suggests the following recurrence for
P(n), the number of different ways of
parenthesizing n items:
1 if n = 1,
P(n) = Pn−1
k=1 P(k)P(n − k) if n ≥ 2
Algorithms – p. 363
Chain Matrix Multiplication
• This is related to a famous function in
combinatorics called the Catalan numbers.
• Catalan numbers are related the number of
different binary trees on n nodes.
• Catalan number is given by the formula:
1 2n
C(n) =
n+1 n
Algorithms – p. 364
Chain Matrix Multiplication
• In particular, P(n) = C(n − 1)
• C(n) ∈ Ω(4n /n3/2 )
• The dominating term is the exponential 4n
thus P(n) will grow large very quickly. So this
approach is not practical.
Algorithms – p. 365
Chain Matrix Multiplication-DP
• The dynamic programming solution involves
breaking up the problem into subproblems
whose solutions can be combined to solve
the global problem.
• Let Ai..j be the result of multiplying matrices i
through j.
• It is easy to see that Ai..j is a pi−1 × pj matrix.
A3 A4 A5 A6 = A3..6
4×5 5×2 2×8 8×7 4×7
Algorithms – p. 366
Chain Matrix Multiplication-DP
• At the highest level of parenthesization we
multiply two matrices
A1..n = A1..k Ȧk+1..n 1 ≤ k ≤ n − 1.
• The question now is: what is the optimum
value of k for the split and how do we
parenthesis the sub-chains A1..k and Ak+1..n .
Algorithms – p. 367
Chain Matrix Multiplication-DP
• We can not use divide and conquer because
we do not know what is the optimum k.
• We will have to consider all possible values of
k and take the best of them.
• We will apply this strategy to solve the
subproblems optimally.
Algorithms – p. 368
Chain Matrix Multiplication
Algorithms – p. 369