Matrix Chain Multiplication: A O(n^3) Solution in C++



If a chain of matrices is given, we have to find a minimum number of correct sequences of matrices to multiply.
We know that the matrix multiplication is associative, so for four matrices ABCD, we can multiply A(BCD), (AB)(CD), (ABC)D, and A(BC)D, in these sequences. Like these sequences, our task is to find which ordering is efficient to multiply.

Example

In the given input there is an array say arr, which contains arr[] = {1, 2, 3, 4}. It means the matrices are of the order (1 x 2), (2 x 3), (3 x 4).

Input ? The orders of the input matrices. {1, 2, 3, 4}. It means the matrices are

{(1 x 2), (2 x 3), (3 x 4)}.

Output ? Minimum number of operations need to multiply these three matrices. Here the result is 18.

Algorithm

Here is the following algorithm we will use for matrix chain multiplication in C++.

    matOrder(array, n)

    Input: List of matrices, the number of matrices in the list.

    Output: Minimum number of matrix multiplication.

    Begin

      Define table minMul of size n x n, initially fill with all 0s.

      For length := 2 to n, do

        For i := 1 to n-length, do

          j := i + length - 1

          minMul[i, j] := ?

          For k := i to j-1, do

            q := minMul[i, k] + minMul[k+1, j] + array[i-1] * array[k] * array[j]

            If q < minMul[i, j], then

              minMul[i, j] := q

          Done

        Done

      Done

      Return minMul[1, n-1]

    End

Code

Here is the following code for matrix chain multiplication in C++.

#include <iostream>
#include <climits> // For INT_MAX
using namespace std;

int matOrder(int array[], int n) {
// Create a 2D array to store the minimum number of scalar multiplications needed
    int minMul[n][n];

// Initialize the diagonal elements to 0
    for (int i = 1; i < n; i++) {
// Multiplying one matrix costs 0 scalar multiplications
        minMul[i][i] = 0; 
    }

// Chain length starts from 2 and increases up to n-1
    for (int length = 2; length < n; length++) {
        for (int i = 1; i < n - length + 1; i++) {
            int j = i + length - 1;
// Initialize to a very high value (infinity)
            minMul[i][j] = INT_MAX; 
// Find the minimum cost by splitting at different positions
            for (int k = i; k <= j - 1; k++) {
// Calculate cost for splitting at position k
                int q = minMul[i][k] + minMul[k + 1][j] + array[i - 1] * array[k] * array[j];
                if (q < minMul[i][j]) {
                    minMul[i][j] = q; // Update minimum cost
                }
            }
        }
    }

// Return the minimum cost for the full chain (1 to n-1)
    return minMul[1][n - 1];
}

int main() {
    int arr[] = {1, 2, 3, 4}; // Dimensions of matrices: 1x2, 2x3, 3x4
    int size = sizeof(arr) / sizeof(arr[0]);

    cout << "Minimum number of matrix multiplications: " << matOrder(arr, size) << endl;

    return 0;
}

Output

Minimum number of matrix multiplications: 18
Updated on: 2024-12-02T11:54:42+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started