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