## Matrix Chain MultiplicationIt is a Method under Dynamic Programming in which previous output is taken as input for next. Here, Chain means one matrix's column is equal to the second matrix's row [always]. In general: If A = ⌊a Then Given following matrices {A A Matrix Multiplication operation is In general, for 1≤ i≤ p and 1≤ j ≤ r It can be observed that the total entries in matrix 'C' is 'pr' as the matrix is of dimension p x r Also each entry takes O (q) times to compute, thus the total time to compute all possible entries for the matrix 'C' which is a multiplication of 'A' and 'B' is proportional to the product of the dimension p q r. It is also noticed that we can save the number of operations by reordering the parenthesis.
Three Matrices can be multiplied in two ways: **A**First multiplying(A_{1},(A_{2},A_{3}):_{2}and A_{3}) then multiplying and resultant withA_{1}.**(A**First multiplying(A_{1},A_{2}),A_{3}:_{1}and A_{2}) then multiplying and resultant withA_{3}.
No of Scalar multiplication in Case 1 will be: No of Scalar multiplication in Case 2 will be: To find the best possible way to calculate the product, we could simply parenthesis the expression in every possible fashion and count each time how many scalar multiplication are required. Matrix Chain Multiplication Problem can be stated as "find the optimal parenthesization of a chain of matrices to be multiplied such that the number of scalar multiplication is minimized".
There are very large numbers of ways of parenthesizing these matrices. If there are n items, there are (n-1) ways in which the outer most pair of parenthesis can place. (A It can be observed that after splitting the kth matrices, we are left with two parenthesized sequence of matrices: one consist 'k' matrices and another consist 'n-k' matrices. Now there are 'L' ways of parenthesizing the left sublist and 'R' ways of parenthesizing the right sublist then the Total will be L.R: Also p (n) = c (n-1) where c (n) is the nth c (n) = On applying Stirling's formula we have c (n) = Ω Which shows that 4 ## Development of Dynamic Programming Algorithm- Characterize the structure of an optimal solution.
- Define the value of an optimal solution recursively.
- Compute the value of an optimal solution in a bottom-up fashion.
- Construct the optimal solution from the computed information.
## Dynamic Programming ApproachLet A Dynamic Programming solution involves breaking up the problems into subproblems whose solution can be combined to solve the global problem. At the greatest level of parenthesization, we multiply two matrices A Thus we are left with two questions: - How to split the sequence of matrices?
- How to parenthesize the subsequence A
_{1.....k}andA_{k+1......n}?
One possible answer to the first question for finding the best value of 'k' is to check all possible choices of 'k' and consider the best among them. But that it can be observed that checking all possibilities will lead to an exponential number of total possibilities. It can also be noticed that there exists only O (n
Let A A If i < j then any parenthesization of the product A
If i=j the chain consist of just one matrix A If i<j we assume that to optimally parenthesize the product we split it between A m [i,j] = m [i, k] + m [k + 1, j] + p There are only (j-1) possible values for 'k' namely k = i, i+1.....j-1. Since the optimal parenthesization must use one of these values for 'k' we need only check them all to find the best. So the minimum cost of parenthesizing the product A To construct an optimal solution, let us define s [i,j] to be the value of 'k' at which we can split the product A m [i,j] = m [i, k] + m [k + 1, j] + p Next TopicMatrix Chain Multiplication Example |