-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix-mul.cpp
57 lines (51 loc) · 1.2 KB
/
matrix-mul.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
#include <iostream>
using namespace std;
int dimensions[] = { 10, 100, 20, 5, 80 };
const int n = sizeof(dimensions) / sizeof(dimensions[0]);
int dp[n][n];
int divisions[n][n];
void print(int arr[][n]) {
for (int i = 0; i < n; i++) {
for (int k = 0; k < n; k++) {
cout << arr[i][k] << ' ';
}
cout << '\n';
}
cout << '\n';
}
void printParenthesis(int i, int j, char &name) {
if (i == j) {
cout << name++;
return;
}
cout << "(";
printParenthesis(i, divisions[i][j], name);
printParenthesis(divisions[i][j] + 1, j, name);
cout << ")";
}
int matrixMultiplication() {
int i, j, k, L, q;
for (L = 2; L < n; L++) {
for (i = 1; i < n - L + 1; i++) {
j = i + L - 1;
dp[i][j] = INT_MAX;
for (k = i; k < j; k++) {
q = dp[i][k] + dp[k + 1][j] + dimensions[i - 1] * dimensions[k] * dimensions[j];
if (q < dp[i][j]) {
dp[i][j] = q;
divisions[i][j] = k;
}
}
}
}
print(dp);
print(divisions);
char name = 'A';
printParenthesis(1, n - 1, name);
cout << '\n';
return dp[1][n - 1];
}
int main() {
cout << matrixMultiplication();
return 0;
}