***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Saturday 23 November 2019

Matrix-chain multiplication

Matrix-chain multiplication

Given a sequence (chain) <A1, A2,…….,An> of n matrices to be multiplied, and compute the product A1.A2 ….An .


Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications.

Matrix multiplication is associative, and so all parenthesizes yield the same product. 


A1A2A3A4 in five distinct ways:
• (A1(A2(A3A4)))
• ((A1A2)(A3A4))
• (((A1A2)A3)A4)
• ((A1(A2A3))A4)
• (A1((A2A3)A4))
For example let A1 is 10 by 100 matrix , A2 is 100 by 5 matrix, A3 is 5 by 50 matrix, A4 is 50 by 1 matrix and A1A2A3A4 is a 10 by 1 matrix.

• (A1(A2(A3A4)))
– A34 = A3A4 , 250 multiplications, result is 5 by 1
– A24 = A2A34 , 500 multiplications, result is 100 by 1
– A14 = A1A24 , 1000 multiplications, result is 10 by 1
– Total is 1750

• ((A1A2)(A3A4))
– A12 = A1A2 , 5000 mults, result is 10 by 5
– A34 = A3A4 , 250 mults, result is 5 by 1
– A14 = A12A34 , 50 mults, result is 10 by 1
– Total is 5300

• (((A1A2)A3)A4)
– A12 = A1A2 , 5000 mults, result is 10 by 5
– A13 = A12A3 , 2500 mults, result is 10 by 50
– A14 = A13A4 , 500 mults, results is 10 by 1
– Total is 8000

• ((A1(A2A3))A4)
– A23 = A2A3 , 25000 mults, result is 100 by 50
– A13 = A1A23 , 50000 mults, result is 10 by 50
– A14 = A13A4 , 500 mults, results is 10 by
– Total is 75500

• (A1((A2A3)A4))
– A23 = A2A3 , 25000 mults, result is 100 by 50
– A24 = A23A4 , 5000 mults, result is 100 by 1
– A14 = A1A24 , 1000 mults, result is 10 by 1
– Total is 31000
As seen above how parenthesize is done on a chain of matrices can have a dramatic impact on the cost of evaluating the product. (A1(A2(A3A4))) has a minimum cost of 1750.


The matrix-chain multiplication problem is stated as follows: given a chain<A1,A2,…..,An> of n matrices, where for i = 1,2,….,n, matrix Ai has dimension pi-1 X pi , fully parenthesize the product A1A2…. An in a way that minimizes the number of scalar multiplications.

Let P(n) be the number of ways to parenthesize n matrices.
Trying all possible parenthesizes is a bad idea.

Applying dynamic programming
Dynamic-programming method is used to determine how to optimally parenthesize a matrix chain.

Step 1: The structure of an optimal parenthesization
For our first step in the dynamic-programming paradigm, we find the optimal substructure and then use it to construct an optimal solution to the problem from optimal solutions to sub problems.
Structure of an optimal solution If the outermost parenthesization is ((A1A2 · · ·Ai) (Ai+1 · · ·An)) then the optimal solution consists of solving A1..Ai and Ai+1..An optimally and then combining the solutions.
Let x be the number of multiplications it to solve A1..Ai , y be the number of multiplications it does to solve Ai+1..An , and z be the number of multiplications it does in the final step. 
The total number of multiplications is therefore x + y + z.
We must ensure that when we search for the correct place to split the product, we have considered all possible places, so that we are sure of having examined the optimal one. 

Step 2: A recursive solution
Next, we define the cost of an optimal solution recursively in terms of the optimal solutions to sub problems. 
Computing the matrix product Ai ..k .Ak+1 . .j takes pi-1pkpj scalar multiplications.
If the final multiplication for Aij is Ai,kAk+1, j then 
minimum number of scalar multiplications needed m[i, j] = m[i, k] + m[k + 1, j] + pi−1pkpj .
Thus, our recursive definition for the minimum cost of parenthesizing the product AiAi+1 . . . Aj becomes
The m[ i, j ] values give the costs of optimal solutions to sub problems, but they do not provide all the information we need to construct an optimal solution.

Step 3: Computing the optimal costs
Optimal cost is computed by using a tabular, bottom-up approach.














The procedure MATRIXCHAIN-ORDER, assumes that matrix Ai has dimensions pi-1 X pi for i = 1,2, . . . ,n. Its input is a sequence p = <p0, p1, . . . ,pn>, where p.length = n + 1. 
The procedure uses an auxiliary table m[1. . n,1. . n] for storing the m[i, j ] costs and another auxiliary table s[1. . n – 1, 2 . . n] that records which index of k achieved the optimal cost in computing m[i, j ]. Table s is used to construct an optimal solution. Each entry s[i, j ] records a value of k such that an optimal parenthesization of AiAi+1 . . .Aj splits the product between Ak and Ak+1.

A simple inspection of the nested loop structure of MATRIX-CHAIN-ORDER yields a running time of O (n3) for the algorithm. The loops are nested three deep, and each loop index (l , i, and k) takes on at most n-1 values. The algorithm requires θ (n2) space to store the m and s tables. 

Thus, MATRIX-CHAIN-ORDER is much more efficient than the exponential-time method of enumerating all possible parenthesizations and checking each one.

No comments:

Post a Comment