forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrassen matrix multiplication.cpp
186 lines (165 loc) · 7.56 KB
/
Strassen matrix multiplication.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <bits/stdc++.h>
using namespace std;
#define ROW_1 8
#define COL_1 8
#define ROW_2 8
#define COL_2 8
void print(string display, vector<vector<int>> matrix,
int start_row, int start_column, int end_row,
int end_column)
{
cout << endl
<< display << " =>" << endl;
for (int i = start_row; i <= end_row; i++)
{
for (int j = start_column; j <= end_column; j++)
{
cout << setw(10);
cout << matrix[i][j];
}
cout << endl;
}
cout << endl;
return;
}
vector<vector<int>>
add_matrix(vector<vector<int>> matrix_A,
vector<vector<int>> matrix_B, int split_index,
int multiplier = 1)
{
for (auto i = 0; i < split_index; i++)
for (auto j = 0; j < split_index; j++)
matrix_A[i][j] = matrix_A[i][j] + (multiplier * matrix_B[i][j]);
return matrix_A;
}
vector<vector<int>>
multiply_matrix(vector<vector<int>> matrix_A,
vector<vector<int>> matrix_B)
{
int col_1 = matrix_A[0].size();
int row_1 = matrix_A.size();
int col_2 = matrix_B[0].size();
int row_2 = matrix_B.size();
if (col_1 != row_2)
{
cout << "\nError: The number of columns in Matrix "
"A must be equal to the number of rows in "
"Matrix B\n";
return {};
}
vector<int> result_matrix_row(col_2, 0);
vector<vector<int>> result_matrix(row_1,
result_matrix_row);
if (col_1 == 1)
result_matrix[0][0] = matrix_A[0][0] * matrix_B[0][0];
else
{
int split_index = col_1 / 2;
vector<int> row_vector(split_index, 0);
vector<vector<int>> a00(split_index, row_vector);
vector<vector<int>> a01(split_index, row_vector);
vector<vector<int>> a10(split_index, row_vector);
vector<vector<int>> a11(split_index, row_vector);
vector<vector<int>> b00(split_index, row_vector);
vector<vector<int>> b01(split_index, row_vector);
vector<vector<int>> b10(split_index, row_vector);
vector<vector<int>> b11(split_index, row_vector);
for (auto i = 0; i < split_index; i++)
for (auto j = 0; j < split_index; j++)
{
a00[i][j] = matrix_A[i][j];
a01[i][j] = matrix_A[i][j + split_index];
a10[i][j] = matrix_A[split_index + i][j];
a11[i][j] = matrix_A[i + split_index]
[j + split_index];
b00[i][j] = matrix_B[i][j];
b01[i][j] = matrix_B[i][j + split_index];
b10[i][j] = matrix_B[split_index + i][j];
b11[i][j] = matrix_B[i + split_index]
[j + split_index];
}
vector<vector<int>> p(multiply_matrix(
a00, add_matrix(b01, b11, split_index, -1)));
vector<vector<int>> q(multiply_matrix(
add_matrix(a00, a01, split_index), b11));
vector<vector<int>> r(multiply_matrix(
add_matrix(a10, a11, split_index), b00));
vector<vector<int>> s(multiply_matrix(
a11, add_matrix(b10, b00, split_index, -1)));
vector<vector<int>> t(multiply_matrix(
add_matrix(a00, a11, split_index),
add_matrix(b00, b11, split_index)));
vector<vector<int>> u(multiply_matrix(
add_matrix(a01, a11, split_index, -1),
add_matrix(b10, b11, split_index)));
vector<vector<int>> v(multiply_matrix(
add_matrix(a00, a10, split_index, -1),
add_matrix(b00, b01, split_index)));
vector<vector<int>> result_matrix_00(add_matrix(
add_matrix(add_matrix(t, s, split_index), u,
split_index),
q, split_index, -1));
vector<vector<int>> result_matrix_01(
add_matrix(p, q, split_index));
vector<vector<int>> result_matrix_10(
add_matrix(r, s, split_index));
vector<vector<int>> result_matrix_11(add_matrix(
add_matrix(add_matrix(t, p, split_index), r,
split_index, -1),
v, split_index, -1));
for (auto i = 0; i < split_index; i++)
for (auto j = 0; j < split_index; j++)
{
result_matrix[i][j] = result_matrix_00[i][j];
result_matrix[i][j + split_index] = result_matrix_01[i][j];
result_matrix[split_index + i][j] = result_matrix_10[i][j];
result_matrix[i + split_index]
[j + split_index] = result_matrix_11[i][j];
}
a00.clear();
a01.clear();
a10.clear();
a11.clear();
b00.clear();
b01.clear();
b10.clear();
b11.clear();
p.clear();
q.clear();
r.clear();
s.clear();
t.clear();
u.clear();
v.clear();
result_matrix_00.clear();
result_matrix_01.clear();
result_matrix_10.clear();
result_matrix_11.clear();
}
return result_matrix;
}
int main()
{
vector<vector<int>> matrix_A = {{1, 0, 2, 1, 4, 1, 1, 1},
{2, 0, 3, 2, 2, 1, 0, 0},
{1, 2, 0, 0, 1, 2, 3, 1},
{3, 0, 1, 1, 3, 3, 1, 1},
{4, 1, 1, 0, 2, 4, 2, 2},
{1, 0, 0, 1, 3, 1, 1, 1},
{1, 3, 2, 1, 2, 1, 0, 0},
{4, 2, 2, 1, 1, 0, 0, 0}};
print("Array A", matrix_A, 0, 0, ROW_1 - 1, COL_1 - 1);
vector<vector<int>> matrix_B = {{1, 0, 0, 1, 1, 1, 0, 0},
{0, 1, 1, 0, 0, 0, 1, 1},
{2, 0, 0, 2, 0, 0, 2, 2},
{0, 2, 2, 0, 2, 2, 0, 0},
{3, 3, 3, 1, 4, 3, 2, 3},
{1, 2, 1, 3, 1, 3, 1, 1},
{3, 2, 1, 2, 4, 1, 1, 0},
{2, 2, 2, 3, 3, 2, 4, 4}};
print("Array B", matrix_B, 0, 0, ROW_2 - 1, COL_2 - 1);
vector<vector<int>> result_matrix(
multiply_matrix(matrix_A, matrix_B));
print("Result Array", result_matrix, 0, 0, ROW_1 - 1,
COL_2 - 1);
}