Matrix Chain Multiplication - Lecture Notes | CS 422, Study notes of Algorithms and Programming

Material Type: Notes; Professor: Kowalczyk; Class: Algorithms Design And Analysis; Subject: Computer Science; University: Northern Michigan University; Term: Winter 2008;

Typology: Study notes

Pre 2010

Uploaded on 08/01/2009

koofers-user-0vj
koofers-user-0vj 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithm Design and Analysis 3/16/2008
Lecture 23: Matrix chain multiplication
Instructor: Mike Kowalczyk
1 Matrix chains
3 2 3
2 9 0
2 2 1
·
5 2 1
4 5 8
218
How efficient is matrix multiplication? That is, if we have two n×nmatrices, how many
multiplications and additions do we need carry out to multiply those matrices? If we have some
particular row and column that we want to multiply together, that will take nmultiplications (and
n1 additions). This gives us the answer for just one entry in the resulting matrix. But we need
n2answers, so the total is n3multiplications, and n3n2additions. There are more efficient ways
of multiplying matrices. For example, a cleverly designed divide-and-conquer technique can get
o(n3) multiplications, but today we will just use the straightforward way of multiplying matrices.
How many multiplications does it take when we have a p×qmatrix times a q×rmatrix? A
little thought shows that the total number of multiplications is pqr, by a similar argument to what
we saw above. Now consider a problem like this:
3 2
2 9
2 2
·5 2 1 5
8 2 1 8 ·
5
8
2
1
When you multiply a p×qmatrix by a q×rmatrix, you get a p×rmatrix. We could do this
3-matrix problem by multiplying the left two first or the right two first. Which one is better? If
we do the right two mutliplied first, then we get 2 ·4·1+3·2·1 = 14 multiplications. The other
way, we get 3 ·2·4 + 3 ·4·1 = 36 multiplications. Much worse! So apparently, it makes a big
difference which matrix multiplications you do first. Given a big chain of matrices, figuring out the
most efficient way to multiply them together in this way (which order to do first) is called matrix
parenthesization. Can we use dynamic programming to solve this problem in Θ(nk) steps for some
constant k, thereby proving that the matrix parenthesization problem is in the complexity class P?
We can multiply pairs of matrices together and build a table to keep track of how many mul-
tiplications it took to get to that point. At each level we could use an array keep track of these
different values. With the different possibilities for one level in hand, we can figure out the optimal
number of multiplications for each node in the next level. But how is this table organized? What
does each entry represent? How are we convinced that this works? Can we write pseudocode for
this algorithm? What is the runtime?
1

Partial preview of the text

Download Matrix Chain Multiplication - Lecture Notes | CS 422 and more Study notes Algorithms and Programming in PDF only on Docsity!

Algorithm Design and Analysis 3/16/

Lecture 23: Matrix chain multiplication

Instructor: Mike Kowalczyk

1 Matrix chains

How efficient is matrix multiplication? That is, if we have two n × n matrices, how many multiplications and additions do we need carry out to multiply those matrices? If we have some particular row and column that we want to multiply together, that will take n multiplications (and n − 1 additions). This gives us the answer for just one entry in the resulting matrix. But we need n^2 answers, so the total is n^3 multiplications, and n^3 − n^2 additions. There are more efficient ways of multiplying matrices. For example, a cleverly designed divide-and-conquer technique can get o(n^3 ) multiplications, but today we will just use the straightforward way of multiplying matrices. How many multiplications does it take when we have a p × q matrix times a q × r matrix? A little thought shows that the total number of multiplications is pqr, by a similar argument to what we saw above. Now consider a problem like this:  

[

]

When you multiply a p × q matrix by a q × r matrix, you get a p × r matrix. We could do this 3-matrix problem by multiplying the left two first or the right two first. Which one is better? If we do the right two mutliplied first, then we get 2 · 4 · 1 + 3 · 2 · 1 = 14 multiplications. The other way, we get 3 · 2 · 4 + 3 · 4 · 1 = 36 multiplications. Much worse! So apparently, it makes a big difference which matrix multiplications you do first. Given a big chain of matrices, figuring out the most efficient way to multiply them together in this way (which order to do first) is called matrix parenthesization. Can we use dynamic programming to solve this problem in Θ(nk) steps for some constant k, thereby proving that the matrix parenthesization problem is in the complexity class P? We can multiply pairs of matrices together and build a table to keep track of how many mul- tiplications it took to get to that point. At each level we could use an array keep track of these different values. With the different possibilities for one level in hand, we can figure out the optimal number of multiplications for each node in the next level. But how is this table organized? What does each entry represent? How are we convinced that this works? Can we write pseudocode for this algorithm? What is the runtime?