

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Suppose we wish to multiply a series of matrices: A 1 A 2.. .An In what order should the multiplication be done? 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, C[i, j] =
There are (p · r) total entries in C and each takes O(q) to compute.
Thus the total number of multiplications is p · q · r. Consider the case of 3 matrices: A 1 is 5 × 4, A 2 is 4 × 6 and A 3 is 6 × 2 The multiplication can be carried out either as ((A 1 A 2 )A 3 ) or (A 1 (A 2 A 3 )). The cost of the two is
((A 1 A 2 )A 3 ) = (5 · 4 · 6) + (5 · 6 · 2)= 180 (A 1 (A 2 A 3 )) = (4 · 6 · 2) + (5 · 4 · 2) = 88
There is considerable savings achieved even for this simple example. In general, however, in what order should we multiply a series of matrices A 1 A 2.. .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. The Chain Matrix Multiplication Problem is stated as follows:
Given a sequence A 1 ,A 2 ,... ,An and dimensions p 0 , p 1 ,... , pn where Ai is of dimension pi-1 × pi, determine the order of multiplication that minimizes the number of operations.
We could write a procedure that tries all possible parenthesizations. Unfortunately, the number of ways of parenthesizing an expression is very large. If there are n items, there are n - 1 ways in which outer most pair of parentheses can placed.
(A 1 )(A 2 A 3 A 4.. .An ) or (A 1 A 2 )(A 3 A 4.. .An ) or (A 1 A 2 A 3 )(A 4.. .An )
...... or (A 1 A 2 A 3 A 4.. .An-1 )(An )
Once we split just after the kth matrix, we create two sublists to be parethesized, one with k and other with n - k matrices.
(A 1 A 2.. .Ak) (Ak+1.. .An ) We could consider all the ways of parenthesizing these two. 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. This suggests the following recurrence for P(n), the number of different ways of parenthesizing n items:
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:
In particular, P(n) = C(n - 1) C(n) ∈ (4n/n3/2) The dominating term is the exponential 4 n^ thus P(n) will grow large very quickly. So this approach is not practical.
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.
At the highest level of parenthesization, we multiply two matrices A1..n = A1..k · Ak+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. 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 sub problems optimally. We will store the solutions to the sub problem 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.