{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect26-chain-mx-mult

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

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

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 . B C = 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

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

View Full Document
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
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern