-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBHatMatrix.c
403 lines (334 loc) · 9.92 KB
/
BHatMatrix.c
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* BHatMatrix.c - Source File
* Implementation of the functions declared in the header file
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "graph.h"
#include "spmat.h"
#include "BHatMatrix.h"
#include "algorithm2.h"
#include "linkedList.h"
#include "errorHandler.h"
/* --------Functions Deceleration--------- */
/*Initialization*/
BHatMatrix* createMatrixBHat (graph*, spmat*, int*);
void initializeFVector(BHatMatrix*);
/*Multiplying With An Input Vector*/
void multBHat(BHatMatrix*, graph*, double* ,double*, int);
void BgMult(BHatMatrix*, graph*, double*, double*);
void spmat_mult(spmat*, graph*, const double*, double*);
void multNumVec(graph* ,double, int*, double*);
void substractTwoVecs(graph*, double*, double*, double*);
/*Matrix Norm Calculation*/
int sumRowsA(spmat*, graph* , int);
double sumRowsD(graph*, BHatMatrix*, int);
double sumRowB (BHatMatrix*, int);
double calcMatrixNorm(BHatMatrix*);
/*General*/
double calcM(BHatMatrix*);
double calcBgPlace(BHatMatrix* ,int, int);
void freeBHat(BHatMatrix*, int);
/* --------Functions Implementation---------*/
BHatMatrix *createMatrixBHat (graph *G, spmat *relate_matrix, int *degrees)
{
BHatMatrix *B;
B = (BHatMatrix *) malloc (sizeof(BHatMatrix));
if(B == NULL) returnErrorByType(2);
B -> originalGraph = G;
B -> relate_matrix = relate_matrix;
B -> degrees = degrees;
B -> originalSize = G -> n;
B -> constM = calcM(B);
initializeFVector(B);
B -> matrixNorm = calcMatrixNorm(B);
B -> freeBHat = freeBHat;
B -> multBHat = multBHat;
return B;
}
void initializeFVector(BHatMatrix *B)
{
double *f;
f = (double *) malloc(sizeof(double) * B -> originalSize);
if(f == NULL) returnErrorByType(4);
B -> f_vector = f;
}
void BgMult(BHatMatrix *B, graph *group, double *vector, double *result)
{
double *A_s, *D_s, *AminusD;
double K_s;
int *degrees = B -> degrees, *nodes = group -> graph_nodes;
int m, n = B -> originalSize, size = group -> n;
/*Allocating a vector to hold the multiplication result between the relate_matrix and an input vector: A * s*/
A_s = (double *) malloc (sizeof(double) * n);
if(A_s == NULL) returnErrorByType(4);
spmat_mult(B -> relate_matrix, group, vector, A_s);
/*A constant that holds the dot product result between the degrees vector and another vector, multiplied by constM*/
K_s = calcDotProductInt(group, degrees, vector);
K_s *= (B -> constM);
/*Allocating a vector to hold the multiplication result between D matrix and an input vector: D * s*/
D_s = (double *) malloc (sizeof(double) * n);
if(D_s == NULL) returnErrorByType(4);
multNumVec(group, K_s, degrees, D_s);
/*Allocating a vector to hold the multiplication result between the current B matrix and an input vector*/
AminusD = (double *) malloc (sizeof(double) * n);
if(AminusD == NULL) returnErrorByType(4);
/* B * s = (A - D) * s = (A * s) - (D * s)*/
substractTwoVecs(group, A_s, D_s, AminusD);
/*Inserting values to the result vector: result[i] = B_g[i] * vector*/
for(m = 0; m < size; m ++)
{
*result = *(AminusD + *nodes);
nodes++;
result++;
}
free(A_s);
free(D_s);
free(AminusD);
}
double calcBgPlace(BHatMatrix *B, int i, int k){
int A_ik = 0;
double D_ik;
linkedList* mrow;
linkedList_node* currNode;
/*Calculating D[i][k] */
D_ik = (B -> constM) * (*(B -> degrees + i)) * (*(B -> degrees + k));
/*Calculating A[i][k] */
mrow = *((linkedList **)(B -> relate_matrix -> private) + i);
currNode = mrow -> head;
while(currNode != NULL)
{
/*If the current node's index is greater than k, than k is not a neighbor to node i -> A[i][k] == 0*/
if(currNode -> value > k)
return (double)(A_ik - D_ik);
/*If the current node's index is equal to k, than k is a neighbor to node i -> A[i][k] == 1*/
if(currNode -> value == k)
{
A_ik = 1;
return (double)(A_ik - D_ik);
}
currNode = currNode -> next;
}
/*Returning B[i][k] = A[i][k] - D[i][k]*/
return (double)(A_ik - D_ik);
}
void multBHat(BHatMatrix *B, graph *group, double *vector ,double *result, int doShift)
{
double *A_s, *D_s, *AminusD;
double K_s, B_m, v_m, F_m;
int *degrees = B -> degrees, *nodes = group -> graph_nodes;
int m, n = B -> originalSize, size = group -> n, nodeIndex;
/*Allocating a vector to hold the multiplication result between the relate_matrix and an input vector: A * s*/
A_s = (double *) malloc (sizeof(double) * n);
if(A_s == NULL) returnErrorByType(4);
spmat_mult(B -> relate_matrix, group, vector, A_s);
/*A constant that holds the dot product result between the degrees vector and another vector, multiplied by constM*/
K_s = calcDotProductInt(group, degrees, vector);
K_s *= (B -> constM);
/*Allocating a vector to hold the multiplication result between D matrix and an input vector: D * s*/
D_s = (double *) malloc (sizeof(double) * n);
if(D_s == NULL) returnErrorByType(4);
multNumVec(group, K_s, degrees, D_s);
/*Allocating a vector to hold the multiplication result between the current B matrix and an input vector*/
AminusD = (double *) malloc (sizeof(double) * n);
if(AminusD == NULL) returnErrorByType(4);
/* B * s = (A - D) * s = (A * s) - (D * s)*/
substractTwoVecs(group, A_s, D_s, AminusD);
/*Inserting values to the result vector*/
for(m = 0; m < size; m ++)
{
nodeIndex = *nodes;
B_m = *(AminusD + nodeIndex);
v_m = *(vector + nodeIndex);
F_m = *(B -> f_vector + nodeIndex);
/* (B^[g] * s) = (A * s) - (D * s) - (f_g * s) */
*(result + nodeIndex) = B_m - v_m * F_m;
/*In order to create the shifting matrix, we add the matrix's norm to the matrix's main diagonal*/
if(doShift)
*(result + nodeIndex) += (v_m * (B -> matrixNorm));
nodes++;
}
free(A_s);
free(D_s);
free(AminusD);
}
/* Multiplication of a sparse matrix by a vector using linked-lists */
void spmat_mult(spmat *A, graph *group, const double *v, double *result)
{
linkedList_node *currNode;
linkedList *currList, **rows_indices = A -> private;
int *nodes = group -> graph_nodes, *run = nodes;
int row, counter = 0;
double sum;
if(result == NULL) returnErrorByType(4);
/* Iterating through the nodes with "run" pointer,
* while iterating on the list of neighbors in the sparse matrix with "currNode".
* Summing only the places where A[i][j] = 1
*/
for(row = 0; row < group -> n; row++)
{
currList = *(rows_indices + *nodes);
currNode = currList -> head;
sum = 0;
counter = 0;
while (currNode != NULL && counter < group -> n)
{
if(currNode -> value == *run)
{
sum += *(v + currNode -> value);
currNode = currNode->next;
counter++;
run++;
}
else
{
if(currNode -> value < *run)
currNode = currNode->next;
else
{
counter++;
run++;
}
}
}
run -= counter;
*(result + *nodes) = sum;
nodes++;
}
}
void multNumVec(graph *g, double num, int *vec, double *result)
{
int i, nodeIndex;
int n = g -> n;
int *nodes = g -> graph_nodes;
for(i = 0;i < n;i++)
{
nodeIndex = *nodes;
*(result + nodeIndex) = (*(vec + nodeIndex)) * num;
nodes++;
}
}
void substractTwoVecs(graph *g, double *vec1, double *vec2, double *result)
{
int i, nodeIndex;
int n = g -> n;
int *nodes = g -> graph_nodes;
for(i = 0;i < n; i++)
{
nodeIndex = *nodes;
*(result + nodeIndex) = *(vec1 + nodeIndex) - *(vec2 + nodeIndex);
nodes++;
}
}
int sumRowsA(spmat *relate_matrix, graph *group , int m)
{
int counter=0, sum = 0;
int *nodes = group -> graph_nodes;
linkedList* mrow;
linkedList_node* currNode;
mrow = *((linkedList **)(relate_matrix -> private) + m);
currNode = mrow->head;
/*Iterating over the relate matrix row corresponding to index "m"*/
while (currNode != NULL && counter < group -> n)
{
/*If we found the current node value in the linked list of node m, there is an edge between the nodes*/
if(currNode -> value == *nodes)
{
currNode = currNode->next;
counter++;
sum++;
nodes++;
}
else
{
if(currNode -> value < *nodes )
currNode = currNode -> next;
else
{
counter++;
nodes++;
}
}
}
return sum;
}
double sumRowsD(graph *group, BHatMatrix *B, int m)
{
int i, sum = 0;
int *listNodes = group -> graph_nodes, *degrees = B -> degrees;
double d;
if(group -> n == B -> originalSize)
return *(degrees + m);
d = (B -> constM) * (*(degrees + m));
for (i = 0; i < group -> n; i++)
{
sum += *(degrees + *listNodes);
listNodes++;
}
return (double)(sum * d);
}
double sumRowB (BHatMatrix *B, int i)
{
int currNodeValue = 0, j, num;
double sum = 0;
spmat *relate_matrix = B -> relate_matrix;
int *degrees = B -> degrees;
linkedList* mrow;
linkedList_node* currNode;
mrow = *((linkedList **)(relate_matrix -> private) + i);
currNode = mrow -> head;
for(j = 0 ; j < B -> originalSize; j++){
if(currNode == NULL)
{
/*The "num" variable indicates if there is an edge between the input "i" node to the current node*/
num = 0;
/* sum += |B[i][j]| = 0 - |D[i][j]| */
sum += fabs(num - (B -> constM) * (*(degrees + i)) * (*(degrees + j)));
continue;
}
currNodeValue = currNode -> value;
if(currNodeValue != j)
num = 0;
else
{
num = 1;
currNode = currNode -> next;
}
/* Sum += |B[i][j]| = num - |D[i][j]| */
sum += fabs(num - (B -> constM) * (*(degrees + i)) * (*(degrees + j)));
}
return sum;
}
double calcMatrixNorm(BHatMatrix *B)
{
double max = 0, sumRow;
int i;
for(i = 0; i < B -> originalSize ; i++)
{
/*Finding the maximum sum of a row in B*/
sumRow = sumRowB(B, i);
if(max < sumRow)
max = sumRow;
}
return max;
}
double calcM(BHatMatrix *B)
{
int i, M = 0;
if(B -> originalSize == 0) return 0;
for(i = 0;i < B -> originalSize ;i++)
M += *(B -> degrees + i);
return (double)(1.0/M);
}
void freeBHat(BHatMatrix *B, int graphIsOneClique)
{
/*Frees the input graph, only if it is not a clique*/
if(!graphIsOneClique)
B -> originalGraph -> free_graph (B -> originalGraph);
/*Frees the relate matrix represented by a sparse matrix*/
B -> relate_matrix -> spmat_free(B -> relate_matrix);
free(B -> degrees);
free(B -> f_vector);
free(B);
}