C Program for Matrix Chain Multiplication

In this problem, we are given a sequence (array) of dimensions that define a chain of matrices. Our task is to create a C program for Matrix chain multiplication to find the minimum number of scalar multiplications required to multiply these matrices.

The array of matrices will contain n elements, which define the dimensions of the matrices as arr[i-1] × arr[i] for the i-th matrix.

Syntax

int matrixChainOrder(int dimensions[], int n);

Understanding the Problem

Let's take an example to understand the problem −

Input

array[] = {3, 4, 5, 6}

This represents three matrices −

Mat1 = 3×4, Mat2 = 4×5, Mat3 = 5×6

For these three matrices, there can be two ways to multiply −

mat1*(mat2*mat3) ? (3×4×6) + (4×5×6) = 72 + 120 = 192
(mat1*mat2)*mat3 ? (3×4×5) + (3×5×6) = 60 + 90 = 150

The minimum number of multiplications will be 150 in case of (mat1*mat2)*mat3.

Solution Using Dynamic Programming

The problem can be solved using dynamic programming as it possesses both optimal substructure and overlapping subproblems properties. We use a 2D table to store the minimum number of multiplications for each subchain.

Example

#include <stdio.h>
#include <limits.h>

int matrixChainMultiplication(int arr[], int n) {
    int dp[n][n];
    int i, j, k, length, cost;
    
    // Cost is zero when multiplying one matrix
    for (i = 1; i < n; i++)
        dp[i][i] = 0;
    
    // length is chain length
    for (length = 2; length < n; length++) {
        for (i = 1; i < n - length + 1; i++) {
            j = i + length - 1;
            dp[i][j] = INT_MAX;
            
            for (k = i; k <= j - 1; k++) {
                cost = dp[i][k] + dp[k + 1][j] + arr[i - 1] * arr[k] * arr[j];
                if (cost < dp[i][j])
                    dp[i][j] = cost;
            }
        }
    }
    
    return dp[1][n - 1];
}

int main() {
    int arr[] = {3, 4, 5, 6};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    printf("Matrix dimensions: ");
    for (int i = 0; i < size - 1; i++) {
        printf("M%d(%dx%d) ", i + 1, arr[i], arr[i + 1]);
    }
    printf("<br>");
    
    int result = matrixChainMultiplication(arr, size);
    printf("Minimum number of scalar multiplications required: %d<br>", result);
    
    return 0;
}
Matrix dimensions: M1(3x4) M2(4x5) M3(5x6) 
Minimum number of scalar multiplications required: 150

How It Works

  • dp[i][j] stores the minimum number of multiplications needed to compute the chain of matrices from i to j.
  • We fill the table in a bottom-up manner, starting with chains of length 2, then 3, and so on.
  • For each subchain, we try all possible split points and choose the one with minimum cost.
  • The final answer is stored in dp[1][n-1] representing the entire chain.

Time and Space Complexity

  • Time Complexity: O(n³) where n is the number of matrices
  • Space Complexity: O(n²) for the DP table

Conclusion

Matrix Chain Multiplication using dynamic programming efficiently finds the optimal parenthesization to minimize scalar multiplications. The algorithm systematically explores all possible ways to split the matrix chain and selects the most efficient one.

Updated on: 2026-03-15T12:51:55+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements