-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCM.cpp
65 lines (59 loc) · 1.3 KB
/
MCM.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<stdio.h>
#define INT_MAX 999999
using namespace std;
int m[100][100];
int s[100][100];
int Matrix_Chain_order(int p[], int n){
int i, j, k, L, q;
for (i = 1; i < n; i++){
m[i][i] = 0;
s[i][i]=0;
}
// L is chain length.
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 = cost/scalar multiplications
q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j];
//printf("%d = m[%d][%d]+m[%d][%d]+p[%d]*p[%d]*p[%d]\n", q, i, k,k+1,j,i-1,k,j);
//printf("%d = %d+%d+%d*%d*%d\n", q, m[i][k],m[k+1][j],p[i-1],p[k],p[j]);
if (q < m[i][j]){
m[i][j] = q;
s[i][j]=k;
//cout<<"Minimun: "<<m[i][j]<<endl;
}
}
cout<<endl<<endl;
}
}
//print(s, 1, n);
return m[1][n-1];
}
void show(){
cout<<"M-Table: "<<endl;
for(int i=1; i<=4; i++){
for(int j=1; j<=4; j++){
printf("%6d", m[i][j]);
}
cout<<endl;
}
cout<<endl;
cout<<"S-Table: "<<endl;
for(int i=1; i<=4; i++){
for(int j=1; j<=4; j++){
printf("%6d", s[i][j]);
}
cout<<endl;
}
cout<<endl;
}
int main(){
int arr[] = {12, 9, 15, 25, 3};
int size = sizeof(arr)/sizeof(arr[0]);
printf("Minimum number of multiplications is %d \n", Matrix_Chain_order(arr, size));
show();
return 0;
}