0% found this document useful (0 votes)
25 views2 pages

Implementation of Matrix Chain Algorithm

The document outlines the implementation of the matrix chain algorithm in C++. It describes the algorithm steps including creating a DP table, setting base cases, calculating minimum costs, and returning the result. A sample program is provided, demonstrating the calculation of minimum scalar multiplications for a given set of matrix dimensions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Implementation of Matrix Chain Algorithm

The document outlines the implementation of the matrix chain algorithm in C++. It describes the algorithm steps including creating a DP table, setting base cases, calculating minimum costs, and returning the result. A sample program is provided, demonstrating the calculation of minimum scalar multiplications for a given set of matrix dimensions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

IMPLEMENTATION OF MATRIX CHAIN ALGORITHM

AIM
To implement of matrix chain algorithm using c++ program.

ALGORITHM

1. Create a DP Table: Initialize a 2D array dp where dp[i][j] will store the minimum
multiplications needed to multiply matrices from index i to j.
2. Set Base Cases: Set dp[i][i] = 0 for all matrices since multiplying one matrix
requires zero multiplications.
3. Loop Through Chain Lengths: For chain lengths from 2 to n-1, calculate the
minimum multiplications for all subchains.
4. Calculate Minimum Cost: For each subchain, find the best split point k and compute
the multiplication cost, updating dp[i][j] with the minimum found.
5. Return Result: The answer will be in dp[1][n-1], which gives the minimum
multiplications for the entire chain.

PROGRAM
#include <iostream>

#include <climits>

using namespace std;

int matrixChainMultiplication(int dimensions[], int n) {

int dp[n][n];

for (int i = 1; i < n; i++) {

dp[i][i] = 0;

for (int length = 2; length < n; length++) {

for (int i = 1; i < n - length + 1; i++) {

int j = i + length - 1;

dp[i][j] = INT_MAX;

for (int k = i; k < j; k++) {

int cost = dp[i][k] + dp[k + 1][j] + dimensions[i - 1] * dimensions[k] * dimensions[j];

if (cost < dp[i][j])

dp[i][j] = cost;
}

return dp[1][n - 1];

int main() {

int dimensions[] = {10, 30, 5, 60};

int n = sizeof(dimensions) / sizeof(dimensions[0]);

int minimumMultiplications = matrixChainMultiplication(dimensions, n);

cout << "Minimum number of scalar multiplications: " << minimumMultiplications << endl;

return 0;

SAMPLE OUTPUT

Minimum number of scalar multiplications: 4500.

RESULT
Thus the implementation of matrix chain algorithm was successfully executed.

You might also like