Skip to content
Related Articles

Related Articles

Matrix Chain Multiplication (A O(N^2) Solution)
  • Difficulty Level : Hard
  • Last Updated : 20 Apr, 2021

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 parenthesization requires less number of operations.
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

 

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
 

 

C++




// CPP program to implement optimized matrix chain multiplication problem.
#include<iostream>
using namespace std;
 
// Matrix Mi 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
    dp[][]. 0th row and 0th column of dp[][] are not used */
    int dp[n][n];
 
    /* dp[i, j] = Minimum number of scalar multiplications needed to compute the matrix M[i]M[i+1]...M[j]
                = M[i..j] where dimension of M[i] is p[i-1] x p[i] */
                 
    // cost is zero when multiplying one matrix.
    for (int i=1; i<n; i++)
        dp[i][i] = 0;
 
    // Simply following above recursive formula.
    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];
}
 
// Driver code
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




// Java program to implement optimized matrix chain multiplication problem.
import java.util.*;
import java.lang.*;
import java.io.*;
 
 
class GFG{
// Matrix Mi has dimension p[i-1] x p[i] for i = 1..n
static int MatrixChainOrder(int p[], int n)
{
 
    /* For simplicity of the program, one extra row and one extra column are
     allocated in dp[][]. 0th row and 0th column of dp[][] are not used */
    int [][]dp=new int[n][n];
 
    /* dp[i, j] = Minimum number of scalar multiplications needed to compute the matrix M[i]M[i+1]...M[j]
                = M[i..j] where dimension of M[i] is p[i-1] x p[i] */
                 
    // cost is zero when multiplying one matrix.
    for (int i=1; i<n; i++)
        dp[i][i] = 0;
 
    // Simply following above recursive formula.
    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];
}
 
// Driver code
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




# Python3 program to implement optimized
# matrix chain multiplication problem.
 
# Matrix Mi has dimension
# p[i-1] x p[i] for i = 1..n
def MatrixChainOrder(p, n):
     
    # For simplicity of the program, one
    # extra row and one extra column are
    # allocated in dp[][]. 0th row and
    # 0th column of dp[][] are not used
    dp = [[0 for i in range(n)]
             for i in range(n)]
 
    # dp[i, j] = Minimum number of scalar
    # multiplications needed to compute
    # the matrix M[i]M[i+1]...M[j] = M[i..j]
    # where dimension of M[i] is p[i-1] x p[i]
                 
    # cost is zero when multiplying one matrix.
    for i in range(1, n):
        dp[i][i] = 0
 
    # Simply following above recursive formula.
    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]
 
# Driver code
arr = [10, 20, 30, 40, 30]
size = len(arr)
print("Minimum number of multiplications is",
                 MatrixChainOrder(arr, size))
 
# This code is contributed
# by sahishelangia

C#




// C# program to implement optimized
// matrix chain multiplication problem.
using System;
 
class GFG
{
// Matrix Mi has dimension
// p[i-1] x p[i] for i = 1..n
static int MatrixChainOrder(int []p, int n)
{
 
    /* For simplicity of the program,
       one extra row and one extra
       column are allocated in dp[][].
       0th row and 0th column of dp[][]
       are not used */
    int [,]dp = new int[n, n];
 
    /* dp[i, j] = Minimum number of scalar
       multiplications needed to compute
       the matrix M[i]M[i+1]...M[j] = M[i..j]
       where dimension of M[i] is p[i-1] x p[i] */
                 
    // cost is zero when multiplying
    // one matrix.
    for (int i = 1; i < n; i++)
        dp[i, i] = 0;
 
    // Simply following above
    // recursive formula.
    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];
}
 
// Driver code
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));
}
}
 
// This code is contributed by anuj_67

PHP




<?php
// PHP program to implement optimized
// matrix chain multiplication problem.
 
// Matrix Mi has dimension
// p[i-1] x p[i] for i = 1..n
function MatrixChainOrder($p, $n)
{
 
    /* For simplicity of the program,
    one extra row and one extra column
    are allocated in dp[][]. 0th row
    and 0th column of dp[][] are not used */
    $dp = array();
 
    /* dp[i, j] = Minimum number of scalar
                  multiplications needed to
                  compute the matrix M[i]M[i+1]...M[j]
                = M[i..j] where dimension of M[i]
                  is p[i-1] x p[i] */
                 
    // cost is zero when multiplying
    // one matrix.
    for ($i=1; $i<$n; $i++)
        $dp[$i][$i] = 0;
 
    // Simply following above
    // recursive formula.
    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];
}
 
// Driver code
$arr = array(10, 20, 30, 40, 30) ;
$size = sizeof($arr);
 
echo "Minimum number of multiplications is ".
               MatrixChainOrder($arr, $size);
 
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>
Output: 
Minimum number of multiplications is 30000

 

Thanks to Rishi_Lazy for providing above optimized solution.
Time Complexity : O(n2
Printing Brackets in Matrix Chain Multiplication
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :