-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix_Chain_Multiplication-DP.cpp
More file actions
56 lines (51 loc) · 1.18 KB
/
Copy pathMatrix_Chain_Multiplication-DP.cpp
File metadata and controls
56 lines (51 loc) · 1.18 KB
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
#include <bits/stdc++.h>
using namespace std;
/*Helper Function that prints the maximum
number of multiplication needed for given matrices*/
void MCM(int P[] , int n)
{
int C[n][n] = {0}; //table to contain the multiplications
int K[n][n] = {0}; //table to obtain proper place for the parenthesis
int q,minimum,i,j,k,d;
for(d=1 ; d<n-1 ; d++)
{
for(i=1 ; i<n-d ; i++)
{
j = d+i;
minimum = 32767;
for(k=1 ; k<=j-1 ; k++)
{
q = C[i][k] + C[k+1][j] + P[i-1] * P[k] * P[j];
if(q < minimum)
{
minimum = q;
K[i][j] = k;
}
}
C[i][j] = minimum;
}
}
cout<<"Minimum number of multiplications : "<<C[1][n-1]<<endl;
/*
for(i=0 ; i<n ; i++)
{
for(j=0 ; j<n ; j++)
cout<<C[i][j]<<" ";
cout<<endl;
}
cout<<endl;
for(i=0 ; i<n ; i++)
{
for(j=0 ; j<n ; j++)
cout<<K[i][j]<<" ";
cout<<endl;
}
*/
}
/*Driver Program*/
int main()
{
int P[] = {3,2,4,2,5};
MCM(P , 5);
return 0;
}