Wednesday, November 27, 2024
Google search engine
HomeLanguagesDynamic ProgrammingMatrix Chain Multiplication (A O(N^2) Solution)

Matrix Chain Multiplication (A O(N^2) Solution)

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++




// 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)
?>


Javascript




<script>
  
// Javascript 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 */
    var dp = Array.from(Array(n), ()=> Array(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 (var i=1; i<n; i++)
        dp[i][i] = 0;
 
    // Simply following above recursive formula.
    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];
}
 
// Driver code
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 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments