-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparse Matrix.cpp
260 lines (225 loc) · 8.17 KB
/
Sparse Matrix.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <iostream>
#include <cassert>
using namespace std;
struct Node {
int row;
int col;
int data;
};
struct Head {
int row; // Total rows
int col; // Total columns
int numNodes;
Node* nodes; // nodes Array
};
bool isSparse(int** mat, int rows, int columns) {
int zeroCounter = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (mat[i][j] == 0) {
zeroCounter++;
}
}
}
if (zeroCounter >= (rows * columns) / 2) {
return true;
}
return false;
}
void display(int** mat, int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}
void sparseDisplay(Head sparseMatrix) {
cout << "Sparse Matrix:" << endl;
cout << "\tRow\tColumn\tData" << endl;
for (int i = 0; i < sparseMatrix.numNodes; i++) {
cout << "\t" << sparseMatrix.nodes[i].row << "\t" << sparseMatrix.nodes[i].col << "\t" << sparseMatrix.nodes[i].data << endl;
}
}
Head toSparse(int** mat, int rows, int columns) {
if (isSparse(mat, rows, columns)) {
Head newHead;
newHead.row = rows;
newHead.col = columns;
newHead.numNodes = 0;
int Counter = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (mat[i][j] != 0) {
Counter++;
}
}
}
newHead.nodes = new Node[Counter];
int tempIndex = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (mat[i][j] != 0) {
newHead.nodes[tempIndex].row = i;
newHead.nodes[tempIndex].col = j;
newHead.nodes[tempIndex].data = mat[i][j];
newHead.numNodes++;
tempIndex++;
}
}
}
return newHead;
} else {
Head newHead;
newHead.row = rows;
newHead.col = columns;
newHead.numNodes = 0;
newHead.nodes = NULL;
return newHead; // Not Sparse
}
}
Head createSMatrix(){
int rows , columns ;
cout << "Enter row & columns : " ;
cin >> rows >> columns ;
int** mat = new int*[rows];
for (int i = 0; i < rows; i++) {
mat[i] = new int[columns];
for (int j = 0 ; j < columns ; j++){
int tempNum ;
cout << "Enter element of [" << i << "][" << j << "] : " ;
cin >> tempNum ;
mat[i][j] = tempNum ;
}
}
display(mat , rows , columns) ;
Head sparseMatrix = toSparse(mat, rows, columns);
sparseDisplay(sparseMatrix);
return sparseMatrix ;
}
Head adder(Head head1, Head head2) {
assert(head1.row == head2.row && head1.col == head2.col);
Head newHead;
newHead.row = head1.row;
newHead.col = head1.col;
newHead.numNodes = 0;
newHead.nodes = new Node[head1.numNodes + head2.numNodes];
int index1 = 0, index2 = 0, newIndex = 0;
while (index1 < head1.numNodes && index2 < head2.numNodes) {
if (head1.nodes[index1].row < head2.nodes[index2].row
|| (head1.nodes[index1].row == head2.nodes[index2].row && head1.nodes[index1].col < head2.nodes[index2].col)) {
newHead.nodes[newIndex].row = head1.nodes[index1].row;
newHead.nodes[newIndex].col = head1.nodes[index1].col;
newHead.nodes[newIndex].data = head1.nodes[index1].data;
index1++;
} else if (head1.nodes[index1].row > head2.nodes[index2].row
|| (head1.nodes[index1].row == head2.nodes[index2].row && head1.nodes[index1].col > head2.nodes[index2].col)) {
newHead.nodes[newIndex].row = head2.nodes[index2].row;
newHead.nodes[newIndex].col = head2.nodes[index2].col;
newHead.nodes[newIndex].data = head2.nodes[index2].data;
index2++;
} else {
newHead.nodes[newIndex].row = head1.nodes[index1].row;
newHead.nodes[newIndex].col = head1.nodes[index1].col;
newHead.nodes[newIndex].data = head1.nodes[index1].data + head2.nodes[index2].data;
index1++;
index2++;
}
newIndex++;
newHead.numNodes++;
}
// Copy remaining nodes from head1
while (index1 < head1.numNodes) {
newHead.nodes[newIndex].row = head1.nodes[index1].row;
newHead.nodes[newIndex].col = head1.nodes[index1].col;
newHead.nodes[newIndex].data = head1.nodes[index1].data;
index1++;
newIndex++;
newHead.numNodes++;
}
// Copy remaining nodes from head2
while (index2 < head2.numNodes) {
newHead.nodes[newIndex].row = head2.nodes[index2].row;
newHead.nodes[newIndex].col = head2.nodes[index2].col;
newHead.nodes[newIndex].data = head2.nodes[index2].data;
index2++;
newIndex++;
newHead.numNodes++;
}
if (newHead.numNodes >= ((newHead.row * newHead.col)/2) ){
cout << "New matrix isn't sparse!" << endl ;
}
return newHead;
}
Head subtractor(Head head1, Head head2) {
assert(head1.row == head2.row && head1.col == head2.col);
Head newHead;
newHead.row = head1.row;
newHead.col = head1.col;
newHead.numNodes = 0;
newHead.nodes = new Node[head1.numNodes + head2.numNodes];
int index1 = 0, index2 = 0, newIndex = 0;
while (index1 < head1.numNodes && index2 < head2.numNodes) {
if (head1.nodes[index1].row < head2.nodes[index2].row
|| (head1.nodes[index1].row == head2.nodes[index2].row && head1.nodes[index1].col < head2.nodes[index2].col)) {
newHead.nodes[newIndex].row = head1.nodes[index1].row;
newHead.nodes[newIndex].col = head1.nodes[index1].col;
newHead.nodes[newIndex].data = head1.nodes[index1].data;
index1++;
} else if (head1.nodes[index1].row > head2.nodes[index2].row
|| (head1.nodes[index1].row == head2.nodes[index2].row && head1.nodes[index1].col > head2.nodes[index2].col)) {
newHead.nodes[newIndex].row = head2.nodes[index2].row;
newHead.nodes[newIndex].col = head2.nodes[index2].col;
newHead.nodes[newIndex].data = -head2.nodes[index2].data; // ????? ???????
index2++;
} else {
newHead.nodes[newIndex].row = head1.nodes[index1].row;
newHead.nodes[newIndex].col = head1.nodes[index1].col;
newHead.nodes[newIndex].data = head1.nodes[index1].data - head2.nodes[index2].data; // ????? ???????
index1++;
index2++;
}
newIndex++;
newHead.numNodes++;
}
// ??? ???? ???? ??????? ?? head1
while (index1 < head1.numNodes) {
newHead.nodes[newIndex].row = head1.nodes[index1].row;
newHead.nodes[newIndex].col = head1.nodes[index1].col;
newHead.nodes[newIndex].data = head1.nodes[index1].data;
index1++;
newIndex++;
newHead.numNodes++;
}
// ??? ???? ???? ??????? ?? head2
while (index2 < head2.numNodes) {
newHead.nodes[newIndex].row = head2.nodes[index2].row;
newHead.nodes[newIndex].col = head2.nodes[index2].col;
newHead.nodes[newIndex].data = -head2.nodes[index2].data; // ????? ???????
index2++;
newIndex++;
newHead.numNodes++;
}
if (newHead.numNodes >= ((newHead.row * newHead.col)/2) ){
cout << "New matrix isn't sparse!" << endl ;
}
return newHead;
}
void scalarProduct(Head* head, int num) {
for (int i = 0; i < head->numNodes; i++) {
head->nodes[i].data *= num;
}
}
int main() {
//Head mat1 = createSMatrix();
//Head mat2 = createSMatrix();
//scalarProduct(&mat1, 2);
//sparseDisplay(adder(mat1 , mat2));
//sparseDisplay(subtractor(mat1 , mat2));
return 0;
}
/*
Reza Asadi (Github : RezaGooner)
1402/08/22 ~ 2023/11/13
Edited : 08/24 ~ 11/15
*/