-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathSparseMat.cpp
More file actions
333 lines (255 loc) · 8.27 KB
/
SparseMat.cpp
File metadata and controls
333 lines (255 loc) · 8.27 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <iostream>
using namespace std;
class sparse_matrix
{
// Maximum number of elements in matrix
const static int MAX = 100;
// Double-pointer initialized by
// the constructor to store
// the triple-represented form
int **data;
// dimensions of matrix
int row, col;
// total number of elements in matrix
int len;
public:
sparse_matrix(int r, int c)
{
// initialize row
row = r;
// initialize col
col = c;
// initialize length to 0
len = 0;
//Array of Pointer to make a matrix
data = new int *[MAX];
// Array representation
// of sparse matrix
//[,0] represents row
//[,1] represents col
//[,2] represents value
for (int i = 0; i < MAX; i++)
data[i] = new int[3];
}
// insert elements into sparse matrix
void insert(int r, int c, int val)
{
// invalid entry
if (r > row || c > col)
{
cout << "Wrong entry";
}
else
{
// insert row value
data[len][0] = r;
// insert col value
data[len][1] = c;
// insert element's value
data[len][2] = val;
// increment number of data in matrix
len++;
}
}
void add(sparse_matrix b)
{
// if matrices don't have same dimensions
if (row != b.row || col != b.col)
{
cout << "Matrices can't be added";
}
else
{
int apos = 0, bpos = 0;
sparse_matrix result(row, col);
while (apos < len && bpos < b.len)
{
// if b's row and col is smaller
if (data[apos][0] > b.data[bpos][0] ||
(data[apos][0] == b.data[bpos][0] &&
data[apos][1] > b.data[bpos][1]))
{
// insert smaller value into result
result.insert(b.data[bpos][0],
b.data[bpos][1],
b.data[bpos][2]);
bpos++;
}
// if a's row and col is smaller
else if (data[apos][0] < b.data[bpos][0] ||
(data[apos][0] == b.data[bpos][0] &&
data[apos][1] < b.data[bpos][1]))
{
// insert smaller value into result
result.insert(data[apos][0],
data[apos][1],
data[apos][2]);
apos++;
}
else
{
// add the values as row and col is same
int addedval = data[apos][2] +
b.data[bpos][2];
if (addedval != 0)
result.insert(data[apos][0],
data[apos][1],
addedval);
// then insert
apos++;
bpos++;
}
}
// insert remaining elements
while (apos < len)
result.insert(data[apos][0],
data[apos][1],
data[apos++][2]);
while (bpos < b.len)
result.insert(b.data[bpos][0],
b.data[bpos][1],
b.data[bpos++][2]);
// print result
result.print();
}
}
sparse_matrix transpose()
{
// new matrix with inversed row X col
sparse_matrix result(col, row);
// same number of elements
result.len = len;
// to count number of elements in each column
int *count = new int[col + 1];
// initialize all to 0
for (int i = 1; i <= col; i++)
count[i] = 0;
for (int i = 0; i < len; i++)
count[data[i][1]]++;
int *index = new int[col + 1];
// to count number of elements having
// col smaller than particular i
// as there is no col with value < 0
index[0] = 0;
// initialize rest of the indices
for (int i = 1; i <= col; i++)
index[i] = index[i - 1] + count[i - 1];
for (int i = 0; i < len; i++)
{
// insert a data at rpos and
// increment its value
int rpos = index[data[i][1]]++;
// transpose row=col
result.data[rpos][0] = data[i][1];
// transpose col=row
result.data[rpos][1] = data[i][0];
// same value
result.data[rpos][2] = data[i][2];
}
// the above method ensures
// sorting of transpose matrix
// according to row-col value
return result;
}
void multiply(sparse_matrix b)
{
if (col != b.row)
{
// Invalid multiplication
cout << "Can't multiply, Invalid dimensions";
return;
}
// transpose b to compare row
// and col values and to add them at the end
b = b.transpose();
int apos, bpos;
// result matrix of dimension row X b.col
// however b has been transposed,
// hence row X b.row
sparse_matrix result(row, b.row);
// iterate over all elements of A
for (apos = 0; apos < len;)
{
// current row of result matrix
int r = data[apos][0];
// iterate over all elements of B
for (bpos = 0; bpos < b.len;)
{
// current column of result matrix
// data[,0] used as b is transposed
int c = b.data[bpos][0];
// temporary pointers created to add all
// multiplied values to obtain current
// element of result matrix
int tempa = apos;
int tempb = bpos;
int sum = 0;
// iterate over all elements with
// same row and col value
// to calculate result[r]
while (tempa < len && data[tempa][0] == r &&
tempb < b.len && b.data[tempb][0] == c)
{
if (data[tempa][1] < b.data[tempb][1])
// skip a
tempa++;
else if (data[tempa][1] > b.data[tempb][1])
// skip b
tempb++;
else
// same col, so multiply and increment
sum += data[tempa++][2] *
b.data[tempb++][2];
}
// insert sum obtained in result[r]
// if its not equal to 0
if (sum != 0)
result.insert(r, c, sum);
while (bpos < b.len &&
b.data[bpos][0] == c)
// jump to next column
bpos++;
}
while (apos < len && data[apos][0] == r)
// jump to next row
apos++;
}
result.print();
}
// printing matrix
void print()
{
cout << "\nDimension: " << row << "x" << col;
cout << "\nSparse Matrix: \nRow\tColumn\tValue\n";
for (int i = 0; i < len; i++)
{
cout << data[i][0] << "\t " << data[i][1]
<< "\t " << data[i][2] << endl;
}
}
};
// Driver Code
int main()
{
// create two sparse matrices and insert values
sparse_matrix a(4, 4);
sparse_matrix b(4, 4);
a.insert(1, 2, 10);
a.insert(1, 4, 12);
a.insert(3, 3, 5);
a.insert(4, 1, 15);
a.insert(4, 2, 12);
b.insert(1, 3, 8);
b.insert(2, 4, 23);
b.insert(3, 3, 9);
b.insert(4, 1, 20);
b.insert(4, 2, 25);
// Output result
cout << "Addition: ";
a.add(b);
cout << "\nMultiplication: ";
a.multiply(b);
cout << "\nTranspose: ";
sparse_matrix atranspose = a.transpose();
atranspose.print();
}