Upload
saagar-minocha
View
400
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Dynamic Programming
Citation preview
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 1/12
GeeksforGeeks
A computer science portal for geeks
Log in
Home
Q&AInterview Corner
Ask a question
ContributeGATE
Algorithms
C
C++
Books
About us
Arrays
Bit MagicC/C++ Puzzles
Articles
GFacts
Linked Lists
MCQMisc
Output
Strings
Trees
Dynamic Programming | Set 8 (Matrix Chain Multiplication)
February 2, 2012
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
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 2/12
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 andreturn the minimum value. In a chain of matrices of size n, we can place the first set of parenthesis in n-1ways. 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 dividethe 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..nint MatrixChainOrder(int p[], int i, int j){ if(i == j) return 0; int k; int min = INT_MAX;
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 3/12
Time complexity of the above naive recursive approach is exponential. It should be noted that the abovefunction 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 byconstructing a temporary array m[][] in bottom up manner.
Dynamic Programming Solution
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 functionint 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;}
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 4/12
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..nint 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;}
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 5/12
1 TweetTweet 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
You may also like following posts
1. Maximum size square sub-matrix with all 1s
2. Dynamic Programming | Set 1 (Overlapping Subproblems Property)3. Dynamic Programming | Set 5 (Edit Distance)
4. A Boolean Matrix Question
5. Dynamic Programming | Set 6 (Min Cost Path)
Facebook Comments
6 comments
Like 10 ShareShare
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 6/12
12 comments so far
1. dambigan says:
April 4, 2013 at 6:14 PM
can anyone tell me how to do the same using one dimensional matrix m and s.
Reply
2. Shikhar Singh says:
February 13, 2013 at 4:12 PM
The given algorithm is wrong
Add a comment
Facebook social plugin
6 comments
Akilah James · Teacher Assistant at Prairie View A&M University
How would you display the matrix grouping information?
Reply · Like · Follow Post · 19 April at 03:42
Ravi Rank · Marwadi College Rajkot
lovely code yaar.
Reply · Like · Follow Post · 2 April at 13:05
Shafqat Wali Khan · Works at Doctor
good approached
Reply · Like · Follow Post · 21 March at 00:14
Ajeet Ganga · · A.C.I.T,Bangalore
I am trying to understand if there is any reason for preferring bottom-up
approach over top-down. Top down approach will be easy to write (since
you just cache the function return value before leaving your recursive
function) AND it is lazy initialization, which means only the calculations
that are required are performed. The second part may not be applicable
to this problem but in problems such as coin change it would make a lot
difference.
Reply · Like · Follow Post · 8 March at 07:20
Follow
Abdullah Hossain · · Works at Student
I don't get proper answer.
Reply · · Like · Follow Post · 4 March at 22:17
Follow
1
Akash Porwal · · Gyan vihar
the code is asking to give constant value for n in int m[n][n]
Reply · Like · Follow Post · 28 February at 22:08
Follow
Sandeep Jain · · Owner at GeeksforGeeks · 126
subscribers
Akash, please try it with a C99 standard compiler, you can
also try it on ideone.com
Reply · Like · 1 March at 11:31
Follow
/* Paste your code here (You may delete these lines if not writing code) */
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 7/12
Reply
GeeksforGeeks says:
February 13, 2013 at 5:07 PM
Could you provide more details? Any input for which the algorithm doesn't give correct output?
Reply
3. Karthick says:
June 10, 2012 at 12:58 PM
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
modified to maintain one more detail (the K value that determines the parenthesis)
And later call a recursive procedure that returns the final multiplied value
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...
Reply
4. swan says:
May 25, 2012 at 6:46 PM
I think i should stop such that j becomes less than n
if (q < m[i][j]) m[i][j] = q;
if (q < m[i][j]){ m[i][j] = q; mult_order[i][j]= k;}
//this method recursively calls itself and does multiplication //between matrices A_i.....A_k & A_k+1.........A_jint 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);}
/* Paste your code here (You may delete these lines if not writing code) */
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 8/12
// 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 multiplicationsq = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
Reply
5. kartikaditya says:
February 19, 2012 at 8:10 AM
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]; }
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 9/12
Reply
6. vikas368 says:
February 5, 2012 at 1:02 AM
Reply
kartik says:
February 12, 2012 at 11:34 AM
@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?
Reply7. PsychoCoder says:
February 4, 2012 at 8:33 AM
Here loop must iterate upto n. i.e. as index goes from 1 to n
Reply
kartik says:
February 4, 2012 at 10:45 AM
@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.
Replynileshbits says:
January 20, 2013 at 10:38 AM
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:
}
for (L=2; L<n; L++)should be for (L=2; L<=n; L++) return value should bereturn m[1][n];
// cost is zero when multiplying one matrix.for (i = 1; i < n; i++) m[i][i] = 0;
// cost is zero when multiplying one matrix.for (i = 1; i <= n; i++) m[i][i] = 0;
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 10/12
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
Reply
8. g1 says:
February 2, 2012 at 11:17 PM
how to print placements of parenthesis in the minimum cost solution?
Reply
Comment
Name (Required) Email (Required) Website URI
Your Comment (Writing code? please paste your code between sourcecode
tags)
[sourcecode language="C"]/* Paste your code here (You may delete these lines if not writing code) */[/sourcecode]
Notify me of followup comments via e-mail. You can also subscribe without commenting.
Have Your Say
Search
Privacy & Terms
Type the two words
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 11/12
Interview Experiences
Advanced Data StructuresDynamic Programming
Greedy Algorithms
Backtracking
Pattern Searching
Divide & Conquer
Graph
Mathematical AlgorithmsRecursion
Java
GeeksforGeeks
Like
21,078 people like GeeksforGeeks.
Popular Posts
All permutations of a given stringMemory Layout of C ProgramsUnderstanding “extern” keyword in C
Median of two sorted arraysTree traversal without recursion and without stack!
Structure Member Alignment, Padding and Data PackingIntersection point of two Linked Lists
Lowest Common Ancestor in a BST.Check if a binary tree is BST or notSorted Linked List to Balanced BST
Follow Follow @Geeksforgeeks@Geeksforgeeks 1,303 follow ers 426 Subscribe
Recent Comments
VAMSHIKRI on Find the first circular tour that visits all petrol pumpsVAMSHIKRI on Find the first circular tour that visits all petrol pumps
GeeksforGeeks on Inorder Successor in Binary Search Treerahul on Inorder Successor in Binary Search Tree
srinivas on Compute the integer absolute value (abs) without branchingHitesh on Swap Kth node from beginning with Kth node from end in a Linked List
[email protected] on Rearrange positive and negative numbers in O(n) time and O(1)
5/22/13 Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 12/12
extra spaceShashank on Rearrange positive and negative numbers in O(n) time and O(1) extra space
@geeksforgeeks, Some rights reserved Contact Us
Powered by WordPress & MooTools, customized by geeksforgeeks team