Vous êtes sur la page 1sur 12

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

GeeksforGeeks
A computer science portal for geeks

Log in Home Q&A Interview Corner Ask a question Contribute GATE Algorithms C C++ Books About us Arrays Bit Magic C/C++ Puzzles Articles GFacts Linked Lists MCQ Misc 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:
( A B C ) D=( A B ) ( C D )=A ( B C D )=. . . .

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
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 1/12

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

matrix, and C is a 5 60 matrix. Then,


( A B ) C=( 1 0 3 0 5 )+( 1 0 5 6 0 )=1 5 0 0+3 0 0 0=4 5 0 0o p e r a t i o n s A ( B C )=( 3 0 5 6 0 )+( 1 0 3 0 6 0 )=9 0 0 0+1 8 0 0 0=2 7 0 0 0o p e r a t i o n s .

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.
I n p u t :p [ ]={ 4 0 ,2 0 ,3 0 ,1 0 ,3 0 } O u t p u t :2 6 0 0 0 T h e r ea r e4m a t r i c e so fd i m e n s i o n s4 0 x 2 0 ,2 0 x 3 0 ,3 0 x 1 0a n d1 0 x 3 0 . L e tt h ei n p u t4m a t r i c e sb eA ,B ,Ca n dD . T h em i n i m u mn u m b e ro f m u l t i p l i c a t i o n sa r eo b t a i n e db yp u t t i n gp a r e n t h e s i si nf o l l o w i n gw a y ( A ( B C ) ) D>2 0 * 3 0 * 1 0+4 0 * 2 0 * 1 0+4 0 * 1 0 * 3 0 I n p u t :p [ ]={ 1 0 ,2 0 ,3 0 ,4 0 ,3 0 } O u t p u t :3 0 0 0 0 T h e r ea r e4m a t r i c e so fd i m e n s i o n s1 0 x 2 0 ,2 0 x 3 0 ,3 0 x 4 0a n d4 0 x 3 0 . L e tt h ei n p u t4m a t r i c e sb eA ,B ,Ca n dD . T h em i n i m u mn u m b e ro f m u l t i p l i c a t i o n sa r eo b t a i n e db yp u t t i n gp a r e n t h e s i si nf o l l o w i n gw a y ( ( A B ) C ) D>1 0 * 2 0 * 3 0+1 0 * 3 0 * 4 0+1 0 * 4 0 * 3 0 I n p u t :p [ ]={ 1 0 ,2 0 ,3 0 } O u t p u t :6 0 0 0 T h e r ea r eo n l yt w om a t r i c e so fd i m e n s i o n s1 0 x 2 0a n d2 0 x 3 0 .S ot h e r e i so n l yo n ew a yt om u l t i p l yt h em a t r i c e s ,c o s to fw h i c hi s1 0 * 2 0 * 3 0

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. / *An a i v er e c u r s i v ei m p l e m e n t a t i o nt h a ts i m p l yf o l l o w st h ea b o v eo p t i m a l s u b s t r u c t u r ep r o p e r t y* / # i n c l u d e < s t d i o . h > # i n c l u d e < l i m i t s . h > / /M a t r i xA ih a sd i m e n s i o np [ i 1 ]xp [ i ]f o ri=1 . . n i n tM a t r i x C h a i n O r d e r ( i n tp [ ] ,i n ti ,i n tj ) { i f ( i= =j ) r e t u r n0 ; i n tk ; i n tm i n=I N T _ M A X ;
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 2/12

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

i n tc o u n t ; / /p l a c ep a r e n t h e s i sa td i f f e r e n tp l a c e sb e t w e e nf i r s ta n dl a s tm a t r i x , / /r e c u r s i v e l yc a l c u l a t ec o u n to fm u l t i p l c a t i o n sf o re a c hp a r e n t h e s i s / /p l a c e m e n ta n dr e t u r nt h em i n i m u mc o u n t f o r( k=i ;k< j ;k + + ) { c o u n t=M a t r i x C h a i n O r d e r ( p ,i ,k )+ M a t r i x C h a i n O r d e r ( p ,k + 1 ,j )+ p [ i 1 ] * p [ k ] * p [ j ] ; i f( c o u n t<m i n ) m i n=c o u n t ;

/ /R e t u r nm i n i m u mc o u n t r e t u r nm i n ;

/ /D r i v e rp r o g r a mt ot e s ta b o v ef u n c t i o n i n tm a i n ( ) { i n ta r r [ ]={ 1 ,2 ,3 ,4 ,3 } ; i n tn=s i z e o f ( a r r ) / s i z e o f ( a r r [ 0 ] ) ; p r i n t f ( " M i n i m u mn u m b e ro fm u l t i p l i c a t i o n si s% d" , M a t r i x C h a i n O r d e r ( a r r ,1 ,n 1 ) ) ; g e t c h a r ( ) ; r e t u r n0 ;

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
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 3/12

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

Following is C/C++ implementation for Matrix Chain Multiplication problem using Dynamic Programming. / /S e et h eC o r m e nb o o kf o rd e t a i l so ft h ef o l l o w i n ga l g o r i t h m # i n c l u d e < s t d i o . h > # i n c l u d e < l i m i t s . h > / /M a t r i xA ih a sd i m e n s i o np [ i 1 ]xp [ i ]f o ri=1 . . n i n tM a t r i x C h a i n O r d e r ( i n tp [ ] ,i n tn ) { / *F o rs i m p l i c i t yo ft h ep r o g r a m ,o n ee x t r ar o wa n do n ee x t r ac o l u m na r e a l l o c a t e di nm [ ] [ ] . 0 t hr o wa n d0 t hc o l u m no fm [ ] [ ]a r en o tu s e d* / i n tm [ n ] [ n ] ; i n ti ,j ,k ,L ,q ; / *m [ i , j ]=M i n i m u mn u m b e ro fs c a l a rm u l t i p l i c a t i o n sn e e d e dt oc o m p u t e t h em a t r i xA [ i ] A [ i + 1 ] . . . A [ j ]=A [ i . . j ]w h e r ed i m e n t i o no fA [ i ]i s p [ i 1 ]xp [ i ]* / / /c o s ti sz e r ow h e nm u l t i p l y i n go n em a t r i x . f o r( i=1 ;i<n ;i + + ) m [ i ] [ i ]=0 ; / /Li sc h a i nl e n g t h . f o r( L = 2 ;L < n ;L + + ) { f o r( i = 1 ;i < = n L + 1 ;i + + ) { j=i + L 1 ; m [ i ] [ j ]=I N T _ M A X ; f o r( k = i ;k < = j 1 ;k + + ) { / /q=c o s t / s c a l a rm u l t i p l i c a t i o n s q=m [ i ] [ k ]+m [ k + 1 ] [ j ]+p [ i 1 ] * p [ k ] * p [ j ] ; i f( q<m [ i ] [ j ] ) m [ i ] [ j ]=q ; } } } } r e t u r nm [ 1 ] [ n 1 ] ;

i n tm a i n ( ) { i n ta r r [ ]={ 1 ,2 ,3 ,4 } ; i n ts i z e=s i z e o f ( a r r ) / s i z e o f ( a r r [ 0 ] ) ; p r i n t f ( " M i n i m u mn u m b e ro fm u l t i p l i c a t i o n si s% d" , M a t r i x C h a i n O r d e r ( a r r ,s i z e ) ) ; g e t c h a r ( ) ; r e t u r n0 ;

www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/

4/12

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

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
Like 10 1 Tw eet 0 Share

You may also like following posts 1. 2. 3. 4. 5. Maximum size square sub-matrix with all 1s Dynamic Programming | Set 1 (Overlapping Subproblems Property) Dynamic Programming | Set 5 (Edit Distance) A Boolean Matrix Question Dynamic Programming | Set 6 (Min Cost Path)

Facebook Comments
6 comments

www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/

5/12

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

6 comments

Add a comment 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 Follow 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 Abdullah Hossain Reply Follow Works at Student

I don't get proper answer. 1 Like Follow Post 4 March at 22:17 Follow Gyan vihar

Akash Porwal

the code is asking to give constant value for n in int m[n][n] Reply Like Follow Post 28 February at 22:08 Sandeep Jain subscribers Follow Owner at GeeksforGeeks 126

Akash, please try it with a C99 standard compiler, you can also try it on ideone.com Reply Like 1 March at 11:31
F acebook social plugin

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

/ *P a s t ey o u rc o d eh e r e( Y o um a yd e l e t et h e s el i n e si fn o tw r i t i n gc o d e )*
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 6/12

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

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 i f( q<m [ i ] [ j ] ) m [ i ] [ j ]=q ; modified to maintain one more detail (the K value that determines the parenthesis) i f( q<m [ i ] [ j ] ) { m [ i ] [ j ]=q ; m u l t _ o r d e r [ i ] [ j ] =k ; } And later call a recursive procedure that returns the final multiplied value

/ / t h i sm e t h o dr e c u r s i v e l yc a l l si t s e l fa n dd o e sm u l t i p l i c a t i o n/ / b e t w e e nm i n tm a t r i x M u l t i p l i c a t i o n ( i n tA [ ] ,i n tm u l t _ o r d e r [ ] [ ] ,i n ti ,i n tj ) { / / w en e e dt or e t r e t u r nm a t r i x M u l t i p l i c a t i o n ( A , m u l t _ o r d e r , i , m u l t _ o r d e r [ i ] [ j ] )*m a t r i x M u } 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

/ *P a s t ey o u rc o d eh e r e( Y o um a yd e l e t et h e s el i n e si fn o tw r i t i n gc o d e )*

www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/

7/12

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

// 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; } } } Reply 5. kartikaditya says: February 19, 2012 at 8:10 AM p u b l i cc l a s sM a t r i x C h a i n M u l t{ p r i v a t es t a t i cc l a s sM a t r i x{ i n tn ,m ; } p u b l i ci n tl e a s t C o s t T o M u l t ( M a t r i xm a t s [ ] ){ / /V a l i d a t em u l t f o r( i n ti=1 ;i<m a t s . l e n g t h ;+ + i ){ i f( m a t s [ i ] . n! =m a t s [ i-1 ] . m ){ r e t u r n1 ; } } i n td p [ m a t s . l e n g t h ] [ m a t s . l e n g t h ] ; f o r( i n ti=0 ;i<m a t s . l e n g t h ;+ + i ){ d p [ i ] [ i ]=0 ; } f o r( i n tl e n=1 ;l e n<m a t s . l e n g t h ;+ + l e n ){ f o r( i n ti=0 ;i+l e n<m a t s . l e n g t h ;+ + i ){ i n tj=i+l e n ; i n tc o s t=I n t e g e r . M A X _ V A L U E ; f o r( i n tk=i ;k<j ;+ + k ){ i n tt e m p=d p [ i ] [ k ]+d p [ k+1 ] [ j ]+ m a t s [ i ] . n*m a t s [ k ] . m*m a t s [ j ] . m ; i f( c o s t>t e m p ){ c o s t=t e m p ; } } d p [ i ] [ j ]=c o s t ; } } r e t u r nd p [ 0 ] [ m a t s . l e n g t h-1 ] ;
8/12

www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

} Reply 6. vikas368 says: February 5, 2012 at 1:02 AM f o r( L = 2 ;L < n ;L + + ) s h o u l db e f o r( L = 2 ;L < = n ;L + + ) r e t u r nv a l u es h o u l db e r e t u r nm [ 1 ] [ n ] ; 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? Reply 7. PsychoCoder says: February 4, 2012 at 8:33 AM / /c o s ti sz e r ow h e nm u l t i p l y i n go n em a t r i x . f o r( i=1 ;i<n ;i + + ) m [ i ] [ i ]=0 ; Here loop must iterate upto n. i.e. as index goes from 1 to n / /c o s ti sz e r ow h e nm u l t i p l y i n go n em a t r i x . f o r( i=1 ;i< =n ;i + + ) m [ i ] [ i ]=0 ; 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. Reply nileshbits 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:
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 9/12

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

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

[ s o u r c e c o d el a n g u a g e = " C " ] / *P a s t ey o u rc o d eh e r e( Y o um a yd e l e t et h e s el i n e s i fn o tw r i t i n gc o d e )* / [ / s o u r c e c o d e ]

tags) Notify me of followup comments via e-mail. You can also subscribe without commenting.

Type the two words


Privacy & Terms

Have Your Say

Searc h

www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/

10/12

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

Interview Experiences Advanced Data Structures Dynamic Programming Greedy Algorithms Backtracking Pattern Searching Divide & Conquer Graph Mathematical Algorithms Recursion Java
GeeksforGeeks
Like 21,078 people like GeeksforGeeks.

Popular Posts
All permutations of a given string Memory Layout of C Programs Understanding extern keyword in C Median of two sorted arrays Tree traversal without recursion and without stack! Structure Member Alignment, Padding and Data Packing Intersection point of two Linked Lists Lowest Common Ancestor in a BST. Check if a binary tree is BST or not Sorted Linked List to Balanced BST
Follow @Geeksforgeeks 1,303 follow ers

426

Subscribe

Recent Comments
VAMSHIKRI on Find the first circular tour that visits all petrol pumps VAMSHIKRI on Find the first circular tour that visits all petrol pumps GeeksforGeeks on Inorder Successor in Binary Search Tree rahul on Inorder Successor in Binary Search Tree srinivas on Compute the integer absolute value (abs) without branching Hitesh on Swap Kth node from beginning with Kth node from end in a Linked List atulkumar4568@gmail.com on Rearrange positive and negative numbers in O(n) time and O(1)
www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ 11/12

5/22/13

Dynamic Programming | Set 8 (Matrix Chain Multiplication) - GeeksforGeeks | GeeksforGeeks

extra space Shashank 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

www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/

12/12