Docsity

matrix chain multiplication, Exams of Data Mining

Toggle sidebar
bg1
Matrix-Chain Multiplication
•Let Abe an nby mmatrix, let Bbe an mby pmatrix, then
C=AB is an nby pmatrix.
•C=AB can be computed in O(nmp)time, using traditional matrix
multiplication.
•Suppose I want to compute A1A2A3A4.
•Matrix Multiplication is associative, so I can do the multiplication in
several different orders.
Example:
•A1is 10 by 100 matrix
•A2is 100 by 5 matrix
•A3is 5 by 50 matrix
•A4is 50 by 1 matrix
•A1A2A3A4is a 10 by 1 matrix
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download matrix chain multiplication and more Exams Data Mining in PDF only on Docsity!

Matrix-Chain Multiplication

  • Let A be an n by m matrix, let B be an m by p matrix, then C = AB is an n by p matrix.
  • C = AB can be computed in O(nmp) time, using traditional matrix multiplication.
  • Suppose I want to compute A 1 A 2 A 3 A 4.
  • Matrix Multiplication is associative, so I can do the multiplication in several different orders.

Example:

  • A 1 is 10 by 100 matrix
  • A 2 is 100 by 5 matrix
  • A 3 is 5 by 50 matrix
  • A 4 is 50 by 1 matrix
  • A 1 A 2 A 3 A 4 is a 10 by 1 matrix

Example

  • A 1 is 10 by 100 matrix
  • A 2 is 100 by 5 matrix
  • A 3 is 5 by 50 matrix
  • A 4 is 50 by 1 matrix
  • A 1 A 2 A 3 A 4 is a 10 by 1 matrix

5 different orderings = 5 different parenthesizations

  • (A 1 (A 2 (A 3 A 4 )))
  • ((A 1 A 2 )(A 3 A 4 ))
  • (((A 1 A 2 )A 3 )A 4 )
  • ((A 1 (A 2 A 3 ))A 4 )
  • (A 1 ((A 2 A 3 )A 4 ))

Each parenthesization is a different number of mults Let Aij = Ai · · · Aj

Example

  • A 1 is 10 by 100 matrix, A 2 is 100 by 5 matrix, A 3 is 5 by 50 matrix, A 4 is 50 by 1 matrix, A 1 A 2 A 3 A 4 is a 10 by 1 matrix.
  • ((A 1 (A 2 A 3 ))A 4 )
    • A 23 = A 2 A 3 , 25000 mults, result is 100 by 50
    • A 13 = A 1 A 23 , 50000 mults, result is 10 by 50
    • A 14 = A 13 A 4 , 500 mults, results is 10 by
    • Total is 75500
  • (A 1 ((A 2 A 3 )A 4 ))
    • A 23 = A 2 A 3 , 25000 mults, result is 100 by 50
    • A 24 = A 23 A 4 , 5000 mults, result is 100 by 1
    • A 14 = A 1 A 24 , 1000 mults, result is 10 by 1
    • Total is 31000

Conclusion Order of operations makes a huge difference. How do we compute the minimum?

One approach

Parenthesization A product of matrices is fully parenthesized if it is either

  • a single matrix, or
  • a product of two fully parenthesized matrices, surrounded by parenthe- ses

Each parenthesization defines a set of n-1 matrix multiplications. We just need to pick the parenthesization that corresponds to the best ordering.

How many parenthesizations are there?

Use dynamic programming

  1. Characterize the structure of an optimal solution
  2. Recursively define the value of an optimal solution
  3. Compute the value of an optimal solution bottom-up
  4. Construct an optimal solution from the computed information

Structure of an optimal solution If the outermost parenthesization is

((A 1 A 2 · · · Ai)(Ai+1 · · · An)) then the optimal solution consists of solving A 1 i and Ai+1,n optimally and then combining the solutions.

Proof

Structure of an optimal solution If the outermost parenthesization is

((A 1 A 2 · · · Ai)(Ai+1 · · · An)) then the optimal solution consists of solving A 1 i and Ai+1,n optimally and then combining the solutions.

Proof: Consider an optimal algorithm that does not solve A 1 i optimally. Let x be the number of multiplications it does to solve A 1 i , y be the number of multiplications it does to solve Ai+1,n , and z be the number of multiplications it does in the final step. The total number of multiplications is therefore x + y + z. But since it is not solving A 1 i optimally, there is a way to solve A 1 i using x′^ < x multiplications. If we used this optimal algorithm instead of our current one for A 1 i , we would do

x′^ + y + z < x + y + z multiplications and therefore have a better algorithm, contradicting the fact that our algorithms is optimal.

Recursive solution

In the enumeration of the P (n) = Ω(4n/n^3 /^2 ) subproblems, how many unique subproblems are there?

Recursive solution

In the enumeration of the P (n) = Ω(4n/n^3 /^2 ) subproblems, how many unique subproblems are there?

Answer: A subproblem is of the form Aij with 1 ≤ i, j ≤ n , so there are O(n^2 ) subproblems!

Notation

  • Let Ai be pi− 1 by pi.
  • Let m[i, j] be the cost of computing Aij

If the final multiplication for Aij is Aij = AikAk+1,j then

m[i, j] = m[i, k] + m[k + 1, j] + pi− 1 pkpj.

We don’t know k a priori, so we take the minimum

m[i, j] =

  

0 if i = j , i^ min≤k<j{m[i, k] +^ m[k^ + 1, j] +^ pi−^1 pkpj}^ if^ i < j Direct recursion on this does not work! We must use the fact that there are at most O(n^2 ) different calls. What is the order?