#include<bits/stdc++.h>
using namespace std;
int Multiplication(int p[], int n)
{
int m[n][n], i, j, k, l, q;
for(i=1;i<n;i++)
m[i][i] = 0;
for(l=2;l<n;l++)
{
for(i=1;i<n-l+1;i++)
{
j = i+l-1;
m[i][j] = INT_MAX;
for(k=i;k<j;k++)
{
q = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j];
if(q < m[i][j])
{
m[i][j] = q;
}
}
}
}
return m[1][n-1];
}
int main()
{
int n, i;
cout << "Enter Array Size :: ";
cin >> n;
int a[n];
cout << "\nEnter Matrix Dimension :: ";
for(i=0; i<n; i++)
cin >> a[i];
cout << "\nMinimum Cost of Matrix Multiplication is :: ";
cout << Multiplication(a, n) << "\n";
return 0;
}
using namespace std;
int Multiplication(int p[], int n)
{
int m[n][n], i, j, k, l, q;
for(i=1;i<n;i++)
m[i][i] = 0;
for(l=2;l<n;l++)
{
for(i=1;i<n-l+1;i++)
{
j = i+l-1;
m[i][j] = INT_MAX;
for(k=i;k<j;k++)
{
q = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j];
if(q < m[i][j])
{
m[i][j] = q;
}
}
}
}
return m[1][n-1];
}
int main()
{
int n, i;
cout << "Enter Array Size :: ";
cin >> n;
int a[n];
cout << "\nEnter Matrix Dimension :: ";
for(i=0; i<n; i++)
cin >> a[i];
cout << "\nMinimum Cost of Matrix Multiplication is :: ";
cout << Multiplication(a, n) << "\n";
return 0;
}
Comments
Post a Comment