Matrix Chain Multiplication | DP-8 - GeeksforGeeks

28 Feb, 2022
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
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 ways to place first set of parenthesis outer side: (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. 
Below is the implementation of the above idea:
C++
C
Java
Python3
C#
PHP
Javascript
/* A naive recursive implementation that simplyfollows the above optimal substructure property */#include <bits/stdc++.h>using namespace std; // 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;    int count;     // place parenthesis at different places    // between first and last matrix, recursively    // calculate count of multiplications 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 Codeint main(){    int arr[] = { 1, 2, 3, 4, 3 };    int n = sizeof(arr) / sizeof(arr[0]);     cout << "Minimum number of multiplications is "         << MatrixChainOrder(arr, 1, n - 1);} // This code is contributed by Shivi_Aggarwal
/* A naive recursive implementation that simply  follows the above optimal substructure property */#include <limits.h>#include <stdio.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;    int count;     // place parenthesis at different places between first    // and last matrix, recursively calculate count of    // multiplications 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 codeint 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;}
/* A naive recursive implementation that simply follows   the above optimal substructure property */class MatrixChainMultiplication {    // Matrix Ai has dimension p[i-1] x p[i] for i = 1..n    static int MatrixChainOrder(int p[], int i, int j)    {        if (i == j)            return 0;         int min = Integer.MAX_VALUE;         // place parenthesis at different places between        // first and last matrix, recursively calculate        // count of multiplications for each parenthesis        // placement and return the minimum count        for (int k = i; k < j; k++)        {            int 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 code    public static void main(String args[])    {        int arr[] = new int[] { 1, 2, 3, 4, 3 };        int n = arr.length;         System.out.println(            "Minimum number of multiplications is "            + MatrixChainOrder(arr, 1, n - 1));    }}/* This code is contributed by Rajat Mishra*/
# A naive recursive implementation that# simply follows the above optimal# substructure propertyimport sys # Matrix A[i] has dimension p[i-1] x p[i]# for i = 1..n  def MatrixChainOrder(p, i, j):     if i == j:        return 0     _min = sys.maxsize     # place parenthesis at different places    # between first and last matrix,    # recursively calculate count of    # multiplications for each parenthesis    # placement and return the minimum count    for k in range(i, j):         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 codearr = [1, 2, 3, 4, 3]n = len(arr) print("Minimum number of multiplications is ",      MatrixChainOrder(arr, 1, n-1)) # This code is contributed by Aryan Garg
/* C# code for naive recursive implementationthat simply follows the above optimalsubstructure property */using System; class GFG {     // Matrix Ai has dimension p[i-1] x p[i]    // for i = 1..n    static int MatrixChainOrder(int[] p, int i, int j)    {         if (i == j)            return 0;         int min = int.MaxValue;         // place parenthesis at different places        // between first and last matrix, recursively        // calculate count of multiplications for each        // parenthesis placement and return the        // minimum count        for (int k = i; k < j; k++)        {            int 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 code    public static void Main()    {        int[] arr = new int[] { 1, 2, 3, 4, 3 };        int n = arr.Length;         Console.Write(            "Minimum number of multiplications is "            + MatrixChainOrder(arr, 1, n - 1));    }} // This code is contributed by Sam007.
<?php// A naive recursive implementation// that simply follows the above// optimal substructure property // Matrix Ai has dimension// p[i-1] x p[i] for i = 1..nfunction MatrixChainOrder(&$p, $i, $j){    if($i == $j)        return 0;    $min = PHP_INT_MAX;     // place parenthesis at different places    // between first and last matrix, recursively    // calculate count of multiplications 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 Code$arr = array(1, 2, 3, 4, 3);$n = sizeof($arr); echo "Minimum number of multiplications is " .      MatrixChainOrder($arr, 1, $n - 1); // This code is contributed by ita_c?>
<script> /* A naive recursive implementation that simply follows   the above optimal substructure property */ // Matrix Ai has dimension p[i-1] x p[i] for i = 1..nfunction MatrixChainOrder(p , i , j){    if (i == j)        return 0;     var min = Number.MAX_VALUE;     // place parenthesis at different places between    // first and last matrix, recursively calculate    // count of multiplications for each parenthesis    // placement and return the minimum count    var k=0;    for (k = i; k < j; k++)    {        var 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 codevar arr = [ 1, 2, 3, 4, 3 ];var n = arr.length; document.write(    "Minimum number of multiplications is "    + MatrixChainOrder(arr, 1, n - 1)); // This code contributed by shikhasingrajput </script>
Minimum number of multiplications is 30
The 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 subproblems are called again, this problem has Overlapping Subproblems 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 Following is the implementation of the Matrix Chain Multiplication problem using Dynamic Programming (Tabulation vs Memoization) 
Using Memoization –  
C++
Java
Python3
C#
Javascript
// C++ program using memoization#include <bits/stdc++.h>using namespace std;int dp[100][100]; // Function for matrix chain multiplicationint matrixChainMemoised(int* p, int i, int j){    if (i == j)    {        return 0;    }    if (dp[i][j] != -1)    {        return dp[i][j];    }    dp[i][j] = INT_MAX;    for (int k = i; k < j; k++)    {        dp[i][j] = min(            dp[i][j], matrixChainMemoised(p, i, k)                     + matrixChainMemoised(p, k + 1, j)                       + p[i - 1] * p[k] * p[j]);    }    return dp[i][j];}int MatrixChainOrder(int* p, int n){    int i = 1, j = n - 1;    return matrixChainMemoised(p, i, j);} // Driver Codeint main(){    int arr[] = { 1, 2, 3, 4 };    int n = sizeof(arr) / sizeof(arr[0]);    memset(dp, -1, sizeof dp);     cout << "Minimum number of multiplications is "         << MatrixChainOrder(arr, n);} // This code is contributed by Sumit_Yadav
// Java program using memoizationimport java.io.*;import java.util.*;class GFG{   static int[][] dp = new int[100][100];   // Function for matrix chain multiplication  static int matrixChainMemoised(int[] p, int i, int j)  {    if (i == j)     {      return 0;    }    if (dp[i][j] != -1)     {      return dp[i][j];    }    dp[i][j] = Integer.MAX_VALUE;    for (int k = i; k < j; k++)     {      dp[i][j] = Math.min(        dp[i][j], matrixChainMemoised(p, i, k)        + matrixChainMemoised(p, k + 1, j)        + p[i - 1] * p[k] * p[j]);    }    return dp[i][j];  }   static int MatrixChainOrder(int[] p, int n)  {    int i = 1, j = n - 1;    return matrixChainMemoised(p, i, j);  }   // Driver Code  public static void main (String[] args)  {     int arr[] = { 1, 2, 3, 4 };    int n= arr.length;     for (int[] row : dp)      Arrays.fill(row, -1);     System.out.println("Minimum number of multiplications is " + MatrixChainOrder(arr, n));  }} // This code is contributed by avanitrachhadiya2155
# Python program using memoizationimport sysdp = [[-1 for i in range(100)] for j in range(100)] # Function for matrix chain multiplicationdef matrixChainMemoised(p, i, j):    if(i == j):        return 0         if(dp[i][j] != -1):        return dp[i][j]         dp[i][j] = sys.maxsize         for k in range(i,j):        dp[i][j] = min(dp[i][j], matrixChainMemoised(p, i, k) + matrixChainMemoised(p, k + 1, j)+ p[i - 1] * p[k] * p[j])         return dp[i][j] def MatrixChainOrder(p,n):    i = 1    j = n - 1       return matrixChainMemoised(p, i, j) # Driver Codearr = [1, 2, 3, 4]n = len(arr)print("Minimum number of multiplications is",MatrixChainOrder(arr, n)) # This code is contributed by rag2127
// C# program using memoizationusing System;class GFG{         static int[,] dp = new int[100, 100];    // Function for matrix chain multiplication  static int matrixChainMemoised(int[] p, int i, int j)  {    if (i == j)     {      return 0;    }    if (dp[i, j] != -1)     {      return dp[i, j];    }    dp[i, j] = Int32.MaxValue;    for (int k = i; k < j; k++)     {      dp[i, j] = Math.Min(        dp[i, j], matrixChainMemoised(p, i, k)        + matrixChainMemoised(p, k + 1, j)        + p[i - 1] * p[k] * p[j]);    }    return dp[i,j];  }    static int MatrixChainOrder(int[] p, int n)  {    int i = 1, j = n - 1;    return matrixChainMemoised(p, i, j);  }     // Driver code  static void Main()  {    int[] arr = { 1, 2, 3, 4 };    int n = arr.Length;         for(int i = 0; i < 100; i++)    {        for(int j = 0; j < 100; j++)        {            dp[i, j] = -1;        }    }      Console.WriteLine("Minimum number of multiplications is " +                      MatrixChainOrder(arr, n));  }} // This code is contributed by divyeshrabadiya07.
<script> // Javascript program using memoizationlet dp = new Array(100);for(var i = 0; i < dp.length; i++){    dp[i] = new Array(2);} // Function for matrix chain multiplicationfunction matrixChainMemoised(p, i, j){    if (i == j)     {        return 0;    }    if (dp[i][j] != -1)     {        return dp[i][j];    }         dp[i][j] = Number.MAX_VALUE;    for(let k = i; k < j; k++)     {        dp[i][j] = Math.min(            dp[i][j], matrixChainMemoised(p, i, k) +                      matrixChainMemoised(p, k + 1, j) +                      p[i - 1] * p[k] * p[j]);    }    return dp[i][j];} function MatrixChainOrder(p, n){    let i = 1, j = n - 1;    return matrixChainMemoised(p, i, j);} // Driver codelet arr = [ 1, 2, 3, 4 ];let n = arr.length; for(var i = 0; i < dp.length; i++){    for(var j = 0; j < dp.length; j++)    {        dp[i][j] = -1;    }} document.write("Minimum number of multiplications is " +               MatrixChainOrder(arr, n));                // This code is contributed by target_2 </script>
Minimum number of multiplications is 18
Time Complexity: O(n3 )
Auxiliary Space: O(n2) ignoring recursion stack space
Using Tabulation –  
C++
C
Java
Python3
C#
PHP
Javascript
// See the Cormen book for details of the// following algorithm#include <bits/stdc++.h>using namespace std; // 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    dimension 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];} // Driver Codeint main(){    int arr[] = { 1, 2, 3, 4 };    int size = sizeof(arr) / sizeof(arr[0]);     cout << "Minimum number of multiplications is "         << MatrixChainOrder(arr, size);     getchar();    return 0;} // This code is contributed// by Akanksha Rai
// See the Cormen book for details of the following// algorithm#include <limits.h>#include <stdio.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 dimension 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];} // Driver  codeint 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;}
// Dynamic Programming Java implementation of Matrix// Chain Multiplication.// See the Cormen book for details of the following// algorithmclass MatrixChainMultiplication{     // Matrix Ai 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 m[][].  0th row        and 0th column of m[][] are not used */        int m[][] = new int[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        dimension 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;                if (j == n)                    continue;                m[i][j] = Integer.MAX_VALUE;                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];    }     // Driver code    public static void main(String args[])    {        int arr[] = new int[] { 1, 2, 3, 4 };        int size = arr.length;         System.out.println(            "Minimum number of multiplications is "            + MatrixChainOrder(arr, size));    }}/* This code is contributed by Rajat Mishra*/
# Dynamic Programming Python implementation of Matrix# Chain Multiplication. See the Cormen book for details# of the following algorithmimport sysmaxint=int(1e9+7)# Matrix Ai 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 m[][].     # 0th row and 0th    # column of m[][] are not used    m = [[0 for x in range(n)] for x in range(n)]     # m[i, j] = Minimum number of scalar    # multiplications needed    # to compute the matrix A[i]A[i + 1]...A[j] =    # A[i..j] where    # dimension of A[i] is p[i-1] x p[i]     # cost is zero when multiplying one matrix.    for i in range(1, n):        m[i][i] = 0     # L is chain length.    for L in range(2, n):        for i in range(1, n-L + 1):            j = i + L-1            m[i][j] = maxint            for k in range(i, j):                 # 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]  # Driver codearr = [1, 2, 3, 4]size = len(arr) print("Minimum number of multiplications is " +      str(MatrixChainOrder(arr, size)))# This Code is contributed by Bhavya Jain
// Dynamic Programming C# implementation of// Matrix Chain Multiplication.// See the Cormen book for details of the// following algorithmusing System; class GFG{     // Matrix Ai 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 m[][]. 0th row and 0th        column of m[][] are not used */        int[, ] m = new int[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 dimension 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;                if (j == n)                    continue;                m[i, j] = int.MaxValue;                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];    }     // Driver code    public static void Main()    {        int[] arr = new int[] { 1, 2, 3, 4 };        int size = arr.Length;         Console.Write("Minimum number of "                      + "multiplications is "                      + MatrixChainOrder(arr, size));    }} // This code is contributed by Sam007
<?php// Dynamic Programming Python implementation// of Matrix Chain Multiplication. // See the Cormen book for details of the// following algorithm Matrix Ai has// dimension p[i-1] x p[i] for i = 1..nfunction MatrixChainOrder($p, $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 */    $m[][] = array($n, $n);     /* m[i, j] = Minimum number of scalar    multiplications needed to compute the    matrix A[i]A[i+1]...A[j] = A[i..j] where    dimension 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;            if($j == $n)                continue;            $m[$i][$j] = PHP_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];} // Driver Code$arr = array(1, 2, 3, 4);$size = sizeof($arr); echo"Minimum number of multiplications is ".              MatrixChainOrder($arr, $size); // This code is contributed by Mukul Singh?>
<script> // Dynamic Programming javascript implementation of Matrix// Chain Multiplication.// See the Cormen book for details of the following// algorithm  // Matrix Ai has dimension p[i-1] x p[i] for i = 1..nfunction MatrixChainOrder(p , 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 */    var m = Array(n).fill(0).map(x => Array(n).fill(0));     var 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    dimension 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;            if (j == n)                continue;            m[i][j] = Number.MAX_VALUE;            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];} // Driver codevar arr = [ 1, 2, 3, 4 ];var size = arr.length; document.write(    "Minimum number of multiplications is "    + MatrixChainOrder(arr, size)); // This code contributed by Princi Singh </script>
Minimum number of multiplications is 18
Time Complexity: O(n3 )Auxiliary Space: O(n2)
Matrix Chain Multiplication (A O(N^2) Solution) Printing brackets in Matrix Chain Multiplication ProblemPlease write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Applications: Minimum and Maximum values of an expression with * and +
References: http://en.wikipedia.org/wiki/Matrix_chain_multiplication http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Dynamic/chainMatrixMult.htm
YouTubeGeeksforGeeks500K subscribersMatrix Chain Multiplication | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 11:32•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=gwmitwgz87s" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
?list=PLqM7alHXFySEQDk2MDfbwEdjd2svVJH9p 
aryan21
ukasp
Code_Mech
Shivi_Aggarwal
Akanksha_Rai
kmradi9
itsvinayak
manjeetsumit16
sommayghosh
avanitrachhadiya2155
rag2127
divyeshrabadiya07
shikhasingrajput
princi singh
anikakapoor
target_2
surinderdawra388
amartyaghoshgfg
prasanna1995
Amazon
matrix-chain-multiplication
Microsoft
Dynamic Programming
Matrix
Amazon
Microsoft
Dynamic Programming
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Largest Sum Contiguous Subarray
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16
Subset Sum Problem | DP-25
Longest Palindromic Substring | Set 1
Program to find largest element in an array
Print a given matrix in spiral form
Program to multiply two matrices
Inplace rotate square matrix by 90 degrees | Set 1
The Celebrity Problem