Dynamic Programming | Set 8 (Matrix Chain Multiplication)
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.
We have many options to multiply a chain of matrices because matrix multiplication is associative. In other words, no matter how we parenthesize the product, the result will be the same. For example, if we had four matrices A, B, C, and D, we would have:
(ABC)D = (AB)(CD) = A(BCD) = ....
However, the order in which we parenthesize the product affects the number of simple arithmetic operations needed to compute the product, or the efficiency. For example, suppose A is a 10 × 30 matrix, B is a 30 × 5 matrix, and C is a 5 × 60 matrix. Then,
(AB)C = (10×30×5) + (10×5×60) = 1500 + 3000 = 4500 operations
A(BC) = (30×5×60) + (10×30×60) = 9000 + 18000 = 27000 operations.
Clearly the first method is the more efficient.
Given an array p[] which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i]. We need to write a function MatrixChainOrder() that should return the minimum number of multiplications needed to multiply the chain.
Input: p[] = {40, 20, 30, 10, 30}
Output: 26000
There are 4 matrices of dimensions 40x20, 20x30, 30x10 and 10x30.
Let the input 4 matrices be A, B, C and D. The minimum number of
multiplications are obtained by putting parenthesis in following way
(A(BC))D --> 20*30*10 + 40*20*10 + 40*10*30
Input: p[] = {10, 20, 30, 40, 30}
Output: 30000
There are 4 matrices of dimensions 10x20, 20x30, 30x40 and 40x30.
Let the input 4 matrices be A, B, C and D. The minimum number of
multiplications are obtained by putting parenthesis in following way
((AB)C)D --> 10*20*30 + 10*30*40 + 10*40*30
Input: p[] = {10, 20, 30}
Output: 6000
There are only two matrices of dimensions 10x20 and 20x30. So there
is only one way to multiply the matrices, cost of which is 10*20*30
1) Optimal Substructure:
A simple solution is to place parenthesis at all possible places, calculate the cost for each placement and return the minimum value. In a chain of matrices of size n, we can place the first set of parenthesis in n-1 ways. For example, if the given chain is of 4 matrices. let the chain be ABCD, then there are 3 way to place first set of parenthesis: A(BCD), (AB)CD and (ABC)D. So when we place a set of parenthesis, we divide the problem into subproblems of smaller size. Therefore, the problem has optimal substructure property and can be easily solved using recursion.
Minimum number of multiplication needed to multiply a chain of size n = Minimum of all n-1 placements (these placements create subproblems of smaller size)
2) Overlapping Subproblems
Following is a recursive implementation that simply follows the above optimal substructure property.
/* A naive recursive implementation that simply follows the above optimal
substructure property */
#include<stdio.h>
#include<limits.h>
// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
int MatrixChainOrder(int p[], int i, int j)
{
if(i == j)
return 0;
int k;
int min = INT_MAX;
int count;
// place parenthesis at different places between first and last matrix,
// recursively calculate count of multiplcations for each parenthesis
// placement and return the minimum count
for (k = i; k <j; k++)
{
count = MatrixChainOrder(p, i, k) +
MatrixChainOrder(p, k+1, j) +
p[i-1]*p[k]*p[j];
if (count < min)
min = count;
}
// Return minimum count
return min;
}
// Driver program to test above function
int main()
{
int arr[] = {1, 2, 3, 4, 3};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Minimum number of multiplications is %d ",
MatrixChainOrder(arr, 1, n-1));
getchar();
return 0;
}
Time complexity of the above naive recursive approach is exponential. It should be noted that the above function computes the same subproblems again and again. See the following recursion tree for a matrix chain of size 4. The function MatrixChainOrder(p, 3, 4) is called two times. We can see that there are many subproblems being called more than once.
Since same suproblems are called again, this problem has Overlapping Subprolems property. So Matrix Chain Multiplication problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array m[][] in bottom up manner.
Dynamic Programming Solution
Following is C/C++ implementation for Matrix Chain Multiplication problem using Dynamic Programming.
// See the Cormen book for details of the following algorithm
#include<stdio.h>
#include<limits.h>
// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
int MatrixChainOrder(int p[], int n)
{
/* For simplicity of the program, one extra row and one extra column are
allocated in m[][]. 0th row and 0th column of m[][] are not used */
int m[n][n];
int i, j, k, L, q;
/* m[i,j] = Minimum number of scalar multiplications needed to compute
the matrix A[i]A[i+1]...A[j] = A[i..j] where dimention of A[i] is
p[i-1] x p[i] */
// cost is zero when multiplying one matrix.
for (i = 1; i < n; i++)
m[i][i] = 0;
// L is chain length.
for (L=2; L<n; L++)
{
for (i=1; i<=n-L+1; i++)
{
j = i+L-1;
m[i][j] = INT_MAX;
for (k=i; k<=j-1; k++)
{
// q = cost/scalar multiplications
q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
return m[1][n-1];
}
int main()
{
int arr[] = {1, 2, 3, 4};
int size = sizeof(arr)/sizeof(arr[0]);
printf("Minimum number of multiplications is %d ",
MatrixChainOrder(arr, size));
getchar();
return 0;
}
Time Complexity: O(n^3)
Auxiliary Space: O(n^2)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
References:
http://en.wikipedia.org/wiki/Matrix_chain_multiplication
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Dynamic/chainMatrixMult.htm

I think the condition in second for loop should be
for(i=1;i<=n-l+1;i++)
should be replaced by
for(i=1;i<n-l+1;i++)
otherwise it goes out of the bound and starts calculating m[i][4] (as far as this example is concerned.) But, the maximum value j can hold is 3 (n-1). Please update the code above or correct me.
Let L = j ? i + 1 denote the length of the subchain being multiplied. The subchains of length 1 (m[i, i]) are trivial. Then we build up by computing the subchains of length 2, 3, ..., n. The final answer is m[1, n].
Now set up the loop: Observe that if a subchain of length L starts at position i, then j = i + L ? 1. Since, we would like to keep j in bounds, this means we want j ? n, this, in turn, means that we want i + L ? 1 ? n, actually what we are saying here is that we want i ? n ? L +1. This gives us the closed interval for i. So our loop for i runs from 1 to n ? L + 1.:)
Hey, Thanks for reply.
Though, I would request you to do the following.
Please paste this line
printf ("\n%d %d %d",i,j, m[i][j]);
just before the inner loop exits.
i.e.,
if (q < m[i][j])
{ printf("\nChanged!");
m[i][j] = q;}
}
printf ("\n%d %d %d",i,j, m[i][j]);
}
}
You will get line for j =4 and m[i][j] will give garbage values which is obvious because m[i][4] does not exist in the current matrix. Max. value hold by column variable is 3. Please correct me if I am wrong.
Can you please explain recursive version of the code.
beautifully explained
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Dynamic/chainMatrixMult.htm
Intelligent
can anyone tell me how to do the same using one dimensional matrix m and s.
The given algorithm is wrong
Could you provide more details? Any input for which the algorithm doesn't give correct output?
This is a great web site. Lot of interesting info. Kudos.
My first comment/contribution.
A good extension to this problem would be to know the multiplication-sequence/multiplication-result (of the minimum count of multiplications that has been found).
This should be easy to do with the following IF block
if (q < m[i][j]) m[i][j] = q;modified to maintain one more detail (the K value that determines the parenthesis)
if (q < m[i][j]){ m[i][j] = q; mult_order[i][j]= k; }And later call a recursive procedure that returns the final multiplied value
//this method recursively calls itself and does multiplication //between matrices A_i.....A_k & A_k+1.........A_j int matrixMultiplication(int A[], int mult_order[][], int i, int j){ //we need to ret return matrixMultiplication(A,mult_order,i,mult_order[i][j]) * matrixMultiplication(A, mult_order, mult_order[i][j]+1,j); }The above method is called as matrixMultiplication(A,mult_order,1,n);
Note: Concept is already there defined on a lot of university sites. I just wanted to extend the existing problem in this link.
Let me know what you think...
I think i should stop such that j becomes less than n
// L is chain length.
for (L=2; L<n; L++)
{
for (i=1; i<=n-L+1; i++) <------------- may be i<n-L+1
{
j = i+L-1;
m[i][j] = INT_MAX;
for (k=i; k<=j-1; k++)
{
// q = cost/scalar multiplications
q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
public class MatrixChainMult { private static class Matrix { int n, m; } public int leastCostToMult(Matrix mats[]) { // Validate mult for (int i = 1; i < mats.length; ++i) { if (mats[i].n != mats[i - 1].m) { return -1; } } int dp[mats.length][mats.length]; for (int i = 0; i < mats.length; ++i) { dp[i][i] = 0; } for (int len = 1; len < mats.length; ++len) { for (int i = 0; i + len < mats.length; ++i) { int j = i + len; int cost = Integer.MAX_VALUE; for (int k = i; k < j; ++k) { int temp = dp[i][k] + dp[k + 1][j] + mats[i].n * mats[k].m * mats[j].m; if (cost > temp) { cost = temp; } } dp[i][j] = cost; } } return dp[0][mats.length - 1]; } }@vikas368: The conditions given in the original program look correct. Could you please let us know why you felt that the conditions are incorrect? Did the original program fail for any case?
// cost is zero when multiplying one matrix. for (i = 1; i < n; i++) m[i][i] = 0;Here loop must iterate upto n. i.e. as index goes from 1 to n
// cost is zero when multiplying one matrix. for (i = 1; i <= n; i++) m[i][i] = 0;@PsychoCoder: Take a closer look at the program. Size of m[][] is nxn as there will n-1 matrices represented by an array p[] of size n.
Well there is a little contradiction in the code... the first line says - "// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n" which seems to imply there are n matrices and p is filled from 0 to n.
I was surprised to see it return m[1][n-1]. I did a dry run and I'm convinced that there is a flaw somewhere in that code. Here's how:
Lets take the last iterations for all the loops:
L = n-1
i = n-L+1 = n-(n-1)+1 = 2
j = i+L-1 = 2+(n-1)-1 = n
Wait - that makes it [2][n] - which means you are calculating upto m[2][n] --> which is bound to be erroneous since you only declared m[n][n] and that means it should be used only upto (n-1) like you pointed out.
You might want to check that piece of code.
On the other hand - great resource mate! Gotta thank you for this website
how to print placements of parenthesis in the minimum cost solution?