-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw.cpp
220 lines (189 loc) · 7.02 KB
/
raw.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// code from https://www.geeksforgeeks.org/strassens-matrix-multiplication/ here was used
#include <bits/stdc++.h>
using namespace std;
#include <vector>
#include <ctime>
#include <random>
#define ROW_1 100
#define COL_1 100
#define ROW_2 100
#define COL_2 100
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>>
strassen_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(strassen_multiply_matrix(
a00, add_matrix(b01, b11, split_index, -1)));
vector<vector<int>> q(strassen_multiply_matrix(
add_matrix(a00, a01, split_index), b11));
vector<vector<int>> r(strassen_multiply_matrix(
add_matrix(a10, a11, split_index), b00));
vector<vector<int>> s(strassen_multiply_matrix(
a11, add_matrix(b10, b00, split_index, -1)));
vector<vector<int>> t(strassen_multiply_matrix(
add_matrix(a00, a11, split_index),
add_matrix(b00, b11, split_index)));
vector<vector<int>> u(strassen_multiply_matrix(
add_matrix(a01, a11, split_index, -1),
add_matrix(b10, b11, split_index)));
vector<vector<int>> v(strassen_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;
}
vector<vector<int>>
multiply_matrix(vector<vector<int>> matrix_A,
vector<vector<int>> matrix_B, int N)
{
vector<vector<int>> result_matrix(N, vector<int>(N, 0));
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
for (int k = 0; k < N; k++)
result_matrix[i][j] += matrix_A[i][k] * matrix_B[k][j];
return result_matrix;
}
vector<vector<int>> fillMatrix(int N)
{
vector<vector<int>> matrix(N, vector<int>(N, 0));
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
matrix[i][j] = rand() % 10;
return matrix;
}
int main()
{
vector<vector<int>> matrix_A = fillMatrix(ROW_1);
// print("Array A", matrix_A, 0, 0, ROW_1 - 1, COL_1 - 1);
vector<vector<int>> matrix_B = fillMatrix(ROW_2);
// print("Array B", matrix_B, 0, 0, ROW_2 - 1, COL_2 - 1);
clock_t start, end;
double cpu_time_used;
start = clock();
vector<vector<int>> result_matrix_2(
multiply_matrix(matrix_A, matrix_B, ROW_1));
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC * 1000;
std::cout << "Time taken by for normal: " << cpu_time_used << " ms" << std::endl;
// print("Result Array", result_matrix_2, 0, 0, ROW_1 - 1, COL_2 - 1);
start = clock();
vector<vector<int>> result_matrix(
strassen_multiply_matrix(matrix_A, matrix_B));
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC * 1000;
std::cout << "Time taken by for strassen: " << cpu_time_used << " ms" << std::endl;
// print("Result Array", result_matrix, 0, 0, ROW_1 - 1, COL_2 - 1);
}
// Time Complexity: T(N) = 7T(N/2) + O(N^2) => O(N^Log7)
// which is approximately O(N^2.8074) Code Contributed By:
// lucasletum