-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm2.c
More file actions
280 lines (217 loc) · 7.23 KB
/
algorithm2.c
File metadata and controls
280 lines (217 loc) · 7.23 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
/*
* algorithm2.c - Source File
* Implementation of the functions declared in the header file
*/
#define EPSILON 0.00001
#define POSITIVE(X) ((X) > EPSILON)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "algorithm2.h"
#include "graph.h"
#include "BHatMatrix.h"
#include "errorHandler.h"
/* --------Functions Deceleration--------- */
void divisionGraphToTwo(BHatMatrix *B, graph *group, double *s, int *out);
void calcFVector(BHatMatrix *B, graph *group);
double* findEigenValue(BHatMatrix *B,graph *group, double *eigenValue);
void creatRandomVector(double* b0, graph *group);
double calcDotProductInt(graph *group, int *degrees, double *vector);
double calcDotProduct(graph *group, double *vector1, double *vector2);
int checkDifference(graph *group, double *vector1, double *vector2, double eps);
void divideByNorm(graph *group, double *vector1, double norm);
void computeS(double *eigenVector, graph *group, double *s);
void createTrivialS(graph *group, double *s);
double computeDQ(double *s,graph *group, BHatMatrix *B);
/* --------Functions Implementation---------*/
/* ------------------------------------- Main Algorithm -------------------------------------- */
void divisionGraphToTwo(BHatMatrix *B, graph *group, double *s, int *out)
{
double *eigenVector;
double eigenValue, dQ;
/*Updating B's F vector according to the current group*/
calcFVector(B, group);
/*Compute the leading eigenpair: eigen-vector and eigen-value of the modularity matrix B^[g] shifted*/
eigenVector = findEigenValue(B, group, &eigenValue);
/*If the eigenvalue is not positive, the network is indivisible*/
if(!POSITIVE(eigenValue)){
*out = -1;
free(eigenVector);
return;
}
/*Computing the division vector s, according to the eigen-vector*/
computeS(eigenVector, group, s);
/*Computing DQ according to the created s*/
dQ = computeDQ(s,group, B);
/*If DQ is not positive, the network is indivisible*/
if(!POSITIVE(dQ))
createTrivialS(group,s);
free(eigenVector);
}
/* ----------------------------------- F Vector Calculation ----------------------------------- */
void calcFVector(BHatMatrix *B, graph *group){
double *f_vector = B -> f_vector;
int *nodes = group -> graph_nodes;
int i = 0, currIndex;
double rowInMatrixA, rowInMatrixD, rowInMatrixB;
for(;i < group -> n; i++){
currIndex = *(nodes + i);
/*Calculating the sum of the i'th row in the adjacency matrix*/
rowInMatrixA = sumRowsA(B-> relate_matrix, group, currIndex);
/*Calculating the sum of the i'th row in degrees matrix - D*/
rowInMatrixD = sumRowsD(group,B, currIndex);
/*Calculating the sum of the i'th row in B[g] matrix*/
rowInMatrixB = rowInMatrixA - rowInMatrixD;
/*Updating B's f vector*/
*(f_vector + currIndex) = rowInMatrixB;
}
}
/* --------------------------------- Eigen - Pair Calculation --------------------------------- */
double* findEigenValue(BHatMatrix *B,graph *group, double *eigenValue)
{
/*Variables Deceleration*/
int ifGreatThenEps = 1, matrixSize, iterationCounter = 0, maxIterations;
double *eigenVector, *tmp, *result;
double vector_norm;
/*To avoid infinite loop, we use a limit on the number of iterations*/
maxIterations = 0.5*(group -> n)*(group -> n) +
10000*(group -> n) + 300000;
matrixSize = B -> originalSize;
eigenVector = (double *) malloc(matrixSize * sizeof(double));
if(eigenVector == NULL) returnErrorByType(5);
/*Initializing the eigen vector as a random vector*/
creatRandomVector(eigenVector, group);
result = (double *) malloc(matrixSize * sizeof(double));
if(result == NULL) returnErrorByType(4);
tmp = result;
/*Iterating while the difference between b_k and b_k+1 is not small enough*/
while(ifGreatThenEps && iterationCounter < maxIterations)
{
iterationCounter++;
/*Performing: B^[g] * eigenVector, and inserting the result into the result vector*/
B -> multBHat(B, group, eigenVector ,result, 1);
/*Calculating result vector norm*/
vector_norm = sqrt(calcDotProduct(group, result, result));
if(vector_norm <= EPSILON) returnErrorByType(7);
/*Normalizing the result vector*/
divideByNorm(group, result, vector_norm);
/*Checking if the difference between the vectors is small enough*/
ifGreatThenEps = checkDifference(group, eigenVector, result, EPSILON);
/*Swapping between the vectors*/
tmp = eigenVector;
eigenVector = result;
result = tmp;
}
/*Calculating the corresponding dominant eigenvalue*/
B -> multBHat(B, group, eigenVector ,result, 1);
/* Calculating the eigen value according to the formula :
*
* (Ab_k)*(b_k) (Ab_k)*(b_k)
* â(shifted matrix) = -------------- = --------------- = (Ab_k)*(b_k)
* b_k * b_ k 1
*
*/
*eigenValue = calcDotProduct(group, result, eigenVector) - (B -> matrixNorm);
free(result);
return eigenVector;
}
void creatRandomVector(double* b0, graph *group)
{
int i;
int *nodes = group -> graph_nodes;
for(i = 0; i < group -> n; i++)
{
*(b0 + *nodes) = rand();
nodes++;
}
}
double calcDotProductInt(graph *group, int *degrees, double *vector)
{
int *nodes = group -> graph_nodes;
double dotProduct = 0.0;
int i = 0, currDeg;
for(; i < group -> n; i++)
{
currDeg = *(degrees + *nodes);
dotProduct += currDeg * (*(vector + *nodes));
nodes++;
}
return dotProduct;
}
double calcDotProduct(graph *group, double *vector1, double *vector2)
{
int *nodes = group -> graph_nodes;
double dotProduct = 0.0;
int i = 0;
for(; i < group -> n; i++)
{
dotProduct += (*(vector1 + *nodes)) * (*(vector2 + *nodes));
nodes++;
}
return dotProduct;
}
int checkDifference(graph *group, double *vector1, double *vector2, double eps)
{
int *nodes = group -> graph_nodes;
int i;
double diffBetweenValues;
for(i = 0; i < group -> n; i++)
{
diffBetweenValues = *(vector1 + *nodes) - *(vector2 + *nodes);
/*If we found two places in the vectors, with difference greater than EPSILON, return 1*/
if(fabs(diffBetweenValues) >= eps)
return 1;
nodes++;
}
return 0;
}
void divideByNorm(graph *group, double *vector1, double norm)
{
int i = 0;
int *nodes = group -> graph_nodes;
for(; i < group -> n; i++)
{
*(vector1 + *nodes) /= norm;
nodes++;
}
}
/* ----------------------------------- S Vector Calculation ----------------------------------- */
void computeS(double *eigenVector, graph *group, double *s)
{
int i = 0;
int *nodes = group -> graph_nodes;
for(; i < group -> n; i++)
{
if(POSITIVE(*(eigenVector + *nodes)))
*(s + *nodes) = 1;
else
*(s + *nodes) = -1;
nodes++;
}
}
void createTrivialS(graph *group, double *s)
{
int i = 0;
int *nodes = group -> graph_nodes;
/*Check the original value in the s vector, and set this value to all the group's nodes*/
int orignialValueInS = *(s + *nodes);
for(; i < group -> n; i++)
{
*(s + *nodes) = orignialValueInS;
nodes++;
}
}
/* ----------------------------------- DQ Value Calculation ----------------------------------- */
double computeDQ(double *s,graph *group, BHatMatrix *B)
{
double dQ;
double *result;
result = (double*)malloc(B -> originalSize * sizeof(double));
if(result == NULL) returnErrorByType(4);
/*Calculate: B^[g] * s*/
B -> multBHat(B, group,s, result,0);
/*Calculate DQ*/
dQ = calcDotProduct(group, s,result);
free(result);
return dQ*(0.5);
}