Matrix Chain Multiplication

Problem Statement

Given a sequence of matrices, find the most efficient way to multiply these matrices together. The efficient way is the one that involves the least number of multiplications.

The dimensions of the matrices are given in an array arr[] of size N (such that N = number of matrices + 1) where the ith matrix has the dimensions (arr[i-1] x arr[i]).

Example 1:

Input: N = 5
arr = {40, 20, 30, 10, 30}
Output: 26000
Explaination: There are 4 matrices of dimension 
40x20, 20x30, 30x10, 10x30. Say the matrices are 
named as A, B, C, D. Out of all possible combinations,
the most efficient way is (A*(B*C))*D. 
The number of operations are -
20*30*10 + 40*20*10 + 40*10*30 = 26000.

Example 2:

Input: N = 4
arr = {10, 30, 5, 60}
Output: 4500
Explaination: The matrices have dimensions 
10*30, 30*5, 5*60. Say the matrices are A, B 
and C. Out of all possible combinations,the
most efficient way is (A*B)*C. The 
number of multiplications are -
10*30*5 + 10*5*60 = 4500.

Your Task: You do not need to take input or print anything. Your task is to complete the function matrixMultiplication() which takes the value N and the array arr[] as input parameters and returns the minimum number of multiplication operations needed to be performed.

Expected Time Complexity: O(N3) Expected Auxiliary Space: O(N2)

Constraints: 2 ≤ N ≤ 100 1 ≤ arr[i] ≤ 500

Intuition

Recurse through the array 

Partition at each point and find the answer
Basically

i * k * j + Left partition + right Partition

eg- ABC

A = 10*20
B = 20*30
C = 30*40

ABC Total size = 10 * 40

Multiplicaltion is 10*20*40 + let (AB) + let(C)
or 10*20*40 + let (A) + let(BC)

So on 

https://practice.geeksforgeeks.org/problems/matrix-chain-multiplication0303/1

https://www.youtube.com/watch?v=vRVfmbCFW7Y&ab_channel=takeUforward https://www.youtube.com/watch?v=pDCXsbAw5Cg&ab_channel=takeUforward

Approach 1:

Memoization

dp i j -> refers to value of multiplication count between index i and j
C++
class Solution{
public:
    int find_ans(int i, int j, int arr[], vector<vector<int>> &dp){
        if(i == j)
            return 0;
        
        if(dp[i][j] != -1)
            return dp[i][j];

        int mini = 1e9;

        for(int k=i; k<j; k++){
            int ans =  arr[i-1] * arr[k] * arr[j] + find_ans(i, k, arr, dp) + find_ans(k+1, j, arr, dp);

            if(mini > ans)
                mini = ans;
        }

        return dp[i][j] = mini;
    }

    int matrixMultiplication(int N, int arr[]){
        vector<vector<int>> dp(N, vector<int> (N,-1));
        
        return find_ans(1, N-1, arr, dp);
    }
};

Approach 2:

Tabulation
C++
class Solution{
public:
    int matrixMultiplication(int n, int arr[]){
        vector<vector<int>> dp(n, vector<int> (n,0));
        

        for(int i=n-1; i>=1; i--) {
            for(int j=i+1; j<n; j++) {
                int mini = 1e9;
                for(int k=i; k<j; k++){
                    int ans =  arr[i-1] * arr[k] * arr[j] + dp[i][k] + dp[k+1][j];

                    if(mini > ans)
                        mini = ans;
                }

                dp[i][j] = mini;
            }
        }

        return dp[1][n-1];
    }
};

Approach 3:

C++

Approach 4:

C++

Similar Problems

Last updated