Given an array p[] of size n, which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[I], Where i is in the range of 1 to n – 1. The task is to find the minimum number of multiplications needed to multiply the chain.
Example:
Input: p[] = {40, 20, 30, 10, 30}
Output: 26000
Explanation: There are 4 matrices of dimensions 40×20, 20×30, 30×10 and 10×30. 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
Explanation: There are 4 matrices of dimensions 10×20, 20×30, 30×40 and 40×30. 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
Explanation: There are only two matrices of dimensions 10×20 and 20×30. So there is only one way to multiply the matrices, cost of which is 10*20*30
Here are some more illustrations of the problem statement: 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.
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 parenthesization requires less number of operations.
We have discussed a O(n^3) solution for Matrix Chain Multiplication Problem.
Note: Below solution does not work for many cases. For example: for input {2, 40, 2, 40, 5}, the correct answer is 580 but this method returns 720
There is another similar approach to solving this problem. More intuitive and recursive approach.
Assume there are following available method
minCost(M1, M2) -> returns min cost of multiplying matrices M1 and M2
Then, for any chained product of matrices like,
M1.M2.M3.M4…Mn
min cost of chain = min(minCost(M1, M2.M3…Mn), minCost(M1.M2..Mn-1, Mn))
Now we have two subchains (sub problems) :
M2.M3…Mn
M1.M2..Mn-1
Implementation:
C++
#include<iostream>
using namespace std;
int MatrixChainOrder( int p[], int n)
{
int dp[n][n];
for ( int i=1; i<n; i++)
dp[i][i] = 0;
for ( int L=1; L<n-1; L++)
for ( int i=1; i<n-L; i++)
dp[i][i+L] = min(dp[i+1][i+L] + p[i-1]*p[i]*p[i+L],
dp[i][i+L-1] + p[i-1]*p[i+L-1]*p[i+L]);
return dp[1][n-1];
}
int main()
{
int arr[] = {10, 20, 30, 40, 30} ;
int size = sizeof (arr)/ sizeof (arr[0]);
printf ( "Minimum number of multiplications is %d " ,
MatrixChainOrder(arr, size));
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
static int MatrixChainOrder( int p[], int n)
{
int [][]dp= new int [n][n];
for ( int i= 1 ; i<n; i++)
dp[i][i] = 0 ;
for ( int L= 1 ; L<n- 1 ; L++)
for ( int i= 1 ; i<n-L; i++)
dp[i][i+L] = Math.min(dp[i+ 1 ][i+L] + p[i- 1 ]*p[i]*p[i+L],
dp[i][i+L- 1 ] + p[i- 1 ]*p[i+L- 1 ]*p[i+L]);
return dp[ 1 ][n- 1 ];
}
public static void main(String args[])
{
int arr[] = { 10 , 20 , 30 , 40 , 30 } ;
int size = arr.length;
System.out.print( "Minimum number of multiplications is " +
MatrixChainOrder(arr, size));
}
}
|
Python3
def MatrixChainOrder(p, n):
dp = [[ 0 for i in range (n)]
for i in range (n)]
for i in range ( 1 , n):
dp[i][i] = 0
for L in range ( 1 , n - 1 ):
for i in range (n - L):
dp[i][i + L] = min (dp[i + 1 ][i + L] +
p[i - 1 ] * p[i] * p[i + L],
dp[i][i + L - 1 ] +
p[i - 1 ] * p[i + L - 1 ] * p[i + L])
return dp[ 1 ][n - 1 ]
arr = [ 10 , 20 , 30 , 40 , 30 ]
size = len (arr)
print ( "Minimum number of multiplications is" ,
MatrixChainOrder(arr, size))
|
C#
using System;
class GFG
{
static int MatrixChainOrder( int []p, int n)
{
int [,]dp = new int [n, n];
for ( int i = 1; i < n; i++)
dp[i, i] = 0;
for ( int L = 1; L < n - 1; L++)
for ( int i = 1; i < n - L; i++)
dp[i, i + L] = Math.Min(dp[i + 1, i + L] +
p[i - 1] * p[i] * p[i + L],
dp[i, i + L - 1] + p[i - 1] *
p[i + L - 1] * p[i + L]);
return dp[1, n - 1];
}
public static void Main()
{
int []arr = {10, 20, 30, 40, 30} ;
int size = arr.Length;
Console.WriteLine( "Minimum number of multiplications is " +
MatrixChainOrder(arr, size));
}
}
|
PHP
<?php
function MatrixChainOrder( $p , $n )
{
$dp = array ();
for ( $i =1; $i < $n ; $i ++)
$dp [ $i ][ $i ] = 0;
for ( $L = 1; $L < $n - 1; $L ++)
for ( $i = 1; $i < $n - $L ; $i ++)
$dp [ $i ][ $i + $L ] = min( $dp [ $i + 1][ $i + $L ] + $p [ $i - 1] *
$p [ $i ] * $p [ $i + $L ],
$dp [ $i ][ $i + $L - 1] + $p [ $i - 1] *
$p [ $i + $L - 1] * $p [ $i + $L ]);
return $dp [1][ $n - 1];
}
$arr = array (10, 20, 30, 40, 30) ;
$size = sizeof( $arr );
echo "Minimum number of multiplications is " .
MatrixChainOrder( $arr , $size );
?>
|
Javascript
<script>
function MatrixChainOrder(p, n)
{
var dp = Array.from(Array(n), ()=> Array(n));
for ( var i=1; i<n; i++)
dp[i][i] = 0;
for ( var L=1; L<n-1; L++)
for ( var i=1; i<n-L; i++)
dp[i][i+L] = Math.min(dp[i+1][i+L] +
p[i-1]*p[i]*p[i+L],
dp[i][i+L-1] + p[i-1]*p[i+L-1]*p[i+L]);
return dp[1][n-1];
}
var arr = [10, 20, 30, 40, 30];
var size = arr.length;
document.write( "Minimum number of multiplications is " +
MatrixChainOrder(arr, size));
</script>
|
Output
Minimum number of multiplications is 30000
Time Complexity: O(n2)
Auxiliary Space: O(n2)
Printing Brackets in Matrix Chain Multiplication
Thanks to Rishi_Lazy for providing above optimized solution.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!