Implementation of Matrix Chain Algorithm
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>
int dp[n][n];
dp[i][i] = 0;
int j = i + length - 1;
dp[i][j] = INT_MAX;
dp[i][j] = cost;
}
int main() {
cout << "Minimum number of scalar multiplications: " << minimumMultiplications << endl;
return 0;
SAMPLE OUTPUT
RESULT
Thus the implementation of matrix chain algorithm was successfully executed.