forked from anshuman8800/Interivew-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixChainMultiplication.cpp
More file actions
132 lines (108 loc) · 2.95 KB
/
matrixChainMultiplication.cpp
File metadata and controls
132 lines (108 loc) · 2.95 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include<climits>
using namespace std;
int sub[10][10]= {0};
int sub2[10][10]= {0};
int store[10][10] ;
int cost1 , cost2 ;
int matChainMul(int p[] , int i , int j )
{
if(i==j)
{
return 0;
//cout<<" "<<sub[i][j];
}
sub[i][j]++;
int mincost = INT_MAX;
int cost , k;
for( k=i ; k<j ; k++)
{
cost = matChainMul(p,i,k) + matChainMul(p,k+1,j) + (p[i-1] * p[k] * p[j]);
if(cost < mincost)
mincost = cost;
}
return mincost;
}
int matChainStoreMul(int p[] , int i , int j)
{
if(i==j)
return 0;
if(store[i][j] != -1)
return store[i][j];
sub2[i][j]++;
int mincost = INT_MAX;
store[i][j] = INT_MAX;
for(int k=i ; k<j ; k++)
{
if(store[i][k] != -1)
cost1 = store[i][k];
else
cost1 = matChainStoreMul(p,i,k);
if(store[k+1][j] != -1)
cost2 = store[k+1][j];
else
cost2 = matChainStoreMul(p , k+1 , j);
int cost = cost1 + cost2 + (p[i-1] * p[k] * p[j]);
//cout<<" "<<cost;
if(cost<mincost)
mincost=cost;
}
store[i][j] = mincost ;
return mincost;
}
int matchainIter(int p[] , int n)
{
int matStore[n][n];
for (int i = 1; i < n; i++)
matStore[i][i] = 0;
for (int L = 2; L < n; L++)
{
for (int i = 1; i < n - L + 1; i++)
{
int j = i + L - 1;
matStore[i][j] = INT_MAX;
for (int k = i; k <= j - 1; k++)
{
int cost = matStore[i][k] + matStore[k + 1][j]
+ (p[i - 1] * p[k] * p[j]);
if (cost < matStore[i][j])
matStore[i][j] = cost;
}
}
}
return matStore[1][n - 1];
}
void printarray(int a[][10] , int n)
{
cout<<"\n";
for(int i=1 ; i<n+1 ; i++)
{
cout<<"\n";
for(int j=1 ; j<n+1 ; j++)
cout<<a[i][j]<<" ";
}
}
int main()
{
int n ;
cout<<"\n Enter the number of Matrices : ";
cin >> n;
int p[n+1];
cout<<"\n Enter the size(n+1) of matrices :";
for(int i=0 ; i<n+1 ; i++)
cin>>p[i];
cout<<"\n Matrix Chain multiplication brute force : ";
cout<<"\n Minimum cost to multiply : "<< matChainMul(p,1,n);
cout<<"\n sub - problem matrix :";
printarray(sub,n);
for(int i=0 ; i<10 ; i++)
for(int j=0 ; j<10 ; j++)
store[i][j]= -1;
cout<<"\n Matrix Chain multiplication after storing : ";
cout<<"\n Minimum cost to multiply : "<< matChainStoreMul(p,1,n);
cout<<"\n sub - problem matrix :";
printarray(sub2,n);
cout<<"\n Matrix Chain multiplication iterative approach : ";
cout<<"\n Minimum cost to multiply : "<< matchainIter(p,n+1);
return 0;
}