lect26-chain-mx-mult - Lecture Notes CMSC 251 Lecture 26:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture Notes CMSC 251 Lecture 26: Chain Matrix Multiplication (Thursday, April 30, 1998) Read: Section 16.1 of CLR. Chain Matrix Multiplication: This problem involves the question of determining the optimal sequence for performing a series of operations. This general class of problem is important in compiler design for code optimization and in databases for query optimization. We will study the problem in a very re- stricted instance, where the dynamic programming issues are easiest to see. Suppose that we wish to multiply a series of matrices A 1 A 2 ...A n Matrix multiplication is an associative but not a commutative operation. This means that we are free to parenthesize the above multiplication however we like, but we are not free to rearrange the order of the matrices. Also recall that when two (nonsquare) matrices are being multiplied, there are restrictions on the dimensions. A p × q matrix has p rows and q columns. You can multiply a p × q matrix A times a q × r matrix B , and the result will be a p × r matrix C . (The number of columns of A must equal the number of rows of B .) In particular for 1 i p and 1 j r , C [ i, j ]= q X k =1 A [ i, k ] B [ k,j ] . Observe that there are pr total entries in C and each takes O ( q ) time to compute, thus the total time (e.g. number of multiplications) to multiply these two matrices is p · q · r . BC = A p q q r p r Multiplication pqr time = = * Figure 33: Matrix Multiplication. Note that although any legal parenthesization will lead to a valid result, not all involve the same number of operations. Consider the case of 3 matrices: A 1 be 5 × 4 , A 2 be 4 × 6 and A 3 be 6 × 2 . mult [(( A 1 A 2 ) A 3 )] = (5 · 4 · 6) + (5 · 6 · 2) = 180 , mult [( A 1 ( A 2 A 3 ))] = (4 · 6 · 2) + (5 · 4 · 2) = 88 . Even for this small example, considerable savings can be achieved by reordering the evaluation se- quence. The Chain Matrix Multiplication problem is: Given a sequence of matrices A 1 ,A 2 ,...,A n and dimensions p 0 ,p 1 ,...,p n where A i is of dimension p i - 1 × p i , determine the multiplication se- quence that minimizes the number of operations. Important Note: This algorithm does not perform the multiplications, it just figures out the best order in which to perform the multiplications. 79
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Notes CMSC 251 Naive Algorithm: We could write a procedure which tries all possible parenthesizations. Unfortunately, the number of ways of parenthesizing an expression is very large. If you have just one item, then there is only one way to parenthesize. If you have n items, then there are n - 1 places where you could break the list with the outermost pair of parentheses, namely just after the 1st item, just after the 2nd item, etc., and just after the ( n - 1) st item. When we split just after the k th item, we create two sublists to be parenthesized, one with k
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

lect26-chain-mx-mult - Lecture Notes CMSC 251 Lecture 26:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online