Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

Matrix Chain Multiplication

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]).

Method 1: Recursive Approach

1) k=i to j-1 then, recure for i to k , k+1 to j  OR
2) k=i+1 to j then, recure for i to k-1 , k to j

Code

int matrixChainMultiplication(int a[], int i, int j)
{
    if (i >= j)
        return 0;

    int temp = 0, mn = INT_MAX;

    for (int k = i; k < j; k++)
    {
        temp = matrixChainMultiplication(a, i, k) + matrixChainMultiplication(a, k + 1, j) + a[i - 1] * a[k] * a[j];

        mn = min(mn, temp);
    }
    return mn;
}
int matrixMultiplication(int N, int arr[])
{
    matrixChainMultiplication(arr, 1, N - 1);
}

DP APPROACHES

A) Memoization(Top-Down)

Code

int dp[101][101];
int matrixChainMultiplication(int *a, 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++)
    {
        int temp = matrixChainMultiplication(a, i, k) + matrixChainMultiplication(a, k + 1, j) + a[i - 1] * a[k] * a[j];

        dp[i][j] = min(dp[i][j], temp);
    }
    return dp[i][j];
}

int matrixMultiplication(int N, int arr[])
{
    memset(dp, -1, sizeof(dp));
    matrixChainMultiplication(arr, 1, N - 1);
}

Complexity Analysis

References