-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP27_Matrix_Chain_multiplication_1dp.cpp
65 lines (60 loc) · 1.53 KB
/
P27_Matrix_Chain_multiplication_1dp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int matrixchain(int arr[], int n)
{
int m[n][n];
int 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-1;k++)
{
q=m[i][k]+m[k+1][j]+arr[i-1]*arr[k]*arr[j];
if (q<m[i][j])
m[i][j]=q;
}
}
}
for( int i=1;i<n;i++){
for(int j=1;j<n;j++){
if(j>=i){
cout<<m[i][j]<<" ";
}
else{
cout<<" ";
}
}
cout<<endl;
}
return m[1][n - 1];
}
int main(){
int n;
cout<<"Enter the elements you want in order array input?"<<endl;
cin>>n;
int arr[n];
cout<<"Enter the elements:"<<endl;
for(int i=0;i<n;i++){
cin>>arr[i];
}
int x=matrixchain(arr,n);
cout<<"The number of scaler multiplications are:"<<x;
}
// Enter the elements you want in order array input?
// 7
// Enter the elements:
// 30 35 15 5 10 20 25
// The number of scaler multiplications are:
// 0 15750 7875 9375 11875 15125
// 0 2625 4375 7125 10500
// 0 750 2500 5375
// 0 1000 3500
// 0 5000
// 0
// 15125