-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomGraphGenerators.c
More file actions
484 lines (414 loc) · 13.5 KB
/
randomGraphGenerators.c
File metadata and controls
484 lines (414 loc) · 13.5 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* randomGraphGenerators.c
*
* Created on: Sep 26, 2017
* Author: pascal
*/
#include <float.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include "loading.h"
#include "randomGraphGenerators.h"
// Utility Functions
/**
* Box-Muller Transform to create two normally distributed values.
* We are happy that two are generated, as we are interested in moving 2-d points
* in our geometric threshold graphs and return both values right away.
*
* Source: https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
*
* Uses rand() 2 times
*/
void generateGaussianNoise(double* z0, double* z1, double mu, double sigma) {
static const double epsilon = DBL_EPSILON;
static const double two_pi = 2.0*3.14159265358979323846;
double u1, u2;
do {
u1 = rand() * (1.0 / RAND_MAX);
u2 = rand() * (1.0 / RAND_MAX);
} while ( u1 <= epsilon );
*z0 = sqrt(-2.0 * log(u1)) * cos(two_pi * u2) * sigma + mu;
*z1 = sqrt(-2.0 * log(u1)) * sin(two_pi * u2) * sigma + mu;
}
/**
* Box-Muller Transform to create two normally distributed values.
* We are happy that two are generated, as we are interested in moving 2-d points
* in our geometric threshold graphs and return both values right away.
*
* mu (i.e., the mean is given by the initialized values of the two double in/out variables
*
* Source: https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
*
* Uses rand() 2 times
*/
void generateIntegerGaussianNoise(int* z0, int* z1, double sigma) {
static const double epsilon = DBL_EPSILON;
static const double two_pi = 2.0*3.14159265358979323846;
double u1, u2;
do {
u1 = rand() * (1.0 / RAND_MAX);
u2 = rand() * (1.0 / RAND_MAX);
} while ( u1 <= epsilon );
*z0 = *z0 + (sqrt(-2.0 * log(u1)) * cos(two_pi * u2) * sigma) * RAND_MAX;
*z1 = *z1 + (sqrt(-2.0 * log(u1)) * sin(two_pi * u2) * sigma) * RAND_MAX;
}
/**
* Randomly assign vertex labels from 0 to nVertexLabels-1 to the vertices.
*
* Uses rand() g->n times
*/
void randomVertexLabels(struct Graph* g, int nVertexLabels) {
int i;
for (i=0; i<g->n; ++i) {
g->vertices[i]->label = intLabel(rand() % nVertexLabels);
g->vertices[i]->isStringMaster = 1;
}
}
// TODO labels
void makeMinDegree1(struct Graph* g, struct GraphPool* gp) {
for (int v=1; v<g->n; ++v) {
if (g->vertices[v]->neighborhood == NULL) {
addEdgeBetweenVertices(v, v-1, intLabel(1), g, gp);
g->vertices[v]->neighborhood->isStringMaster = 1;
}
}
if (g->vertices[0]->neighborhood == NULL) {
addEdgeBetweenVertices(0,1,intLabel(1), g, gp);
g->vertices[0]->neighborhood->isStringMaster = 1;
}
}
// ERDOS RENYI Graph Generators
/**
* Create a graph in the ER model.
* The returned graph will have exactly n vertices and the probability of any edge being present is p.
* Note that vertices and edges have NULL labels.
*
* Uses rand() n * (n-1) / 2 times
*/
struct Graph* erdosRenyi(int n, double p, struct GraphPool* gp) {
struct Graph* g = createGraph(n, gp);
for (int i=0; i<n; ++i) {
for (int j=i+1; j<n; ++j) {
double value = rand() / (RAND_MAX + 1.0);
if (value < p) {
addEdgeBetweenVertices(i, j, NULL, g, gp);
}
}
}
return g;
}
/**
* Create a graph in the ER model.
* The returned graph will have exactly n vertices and the probability of any edge being present is p.
* Vertices and edges have int labels that are in the specified ranges.
*
* Uses rand() n * (n-1) / 2 + n + m times
*/
struct Graph* erdosRenyiWithLabels(int n, double p, int nVertexLabels, int nEdgeLabels, struct GraphPool* gp) {
struct Graph* g = createGraph(n, gp);
randomVertexLabels(g, nVertexLabels);
for (int i=0; i<n; ++i) {
for (int j=i+1; j<n; ++j) {
double value = rand() / (RAND_MAX + 1.0);
if (value < p) {
// add a labeled edge and set one of the two resulting vertex lists as string master.
addEdgeBetweenVertices(i, j, intLabel(rand() % nEdgeLabels), g, gp);
g->vertices[i]->neighborhood->isStringMaster = 1;
}
}
}
return g;
}
// Chain of Blocks Generator
/**
* Create a labeled graph consisting of a chain of nBlocks biconnected blocks of blockSize vertices each.
* Each block consists of a cycle and a random number of diagonals influenced by diagonalProbability.
* Two consecutive blocks share a single vertex.
* A block between two other blocks contains an edge (on its Hamiltonian cycle) that connects the two articulation vertices
* where it is joined to its neighbor blocks.
*
* The resulting graph has nBlocks * blockSize - nBlocks + 1 vertices and at least nBlocks * blockSize edges.
*
* If nVertexLabels is smaller than 1, then the vertices will be injectively labeled.
*
* Uses rand()
*/
struct Graph* blockChainGenerator(int nBlocks, int blockSize, int nVertexLabels, int nEdgeLabels, double diagonalProbability, struct GraphPool* gp) {
// create empty graph of correct size
int nVertices = nBlocks * blockSize - nBlocks + 1;
struct Graph* g = createGraph(nVertices, gp);
// add vertex labels
if (nVertexLabels < 1) {
for (int v=0; v<nVertices; ++v) {
g->vertices[v]->label = intLabel(v);
g->vertices[v]->isStringMaster = 1;
}
} else {
randomVertexLabels(g, nVertexLabels);
}
// add cycle edges
for (int blockStart=0; blockStart<nVertices-1; blockStart+=blockSize-1) {
for (int v=blockStart; v<blockStart+blockSize; ++v) {
for (int w=v+1; w<blockStart+blockSize; ++w) {
if ((w - v == 1) || (rand() / ((double)RAND_MAX) <= diagonalProbability)) {
addEdgeBetweenVertices(v, w, intLabel(rand() % nEdgeLabels), g, gp);
}
}
}
if (!isIncident(g->vertices[blockStart], g->vertices[blockStart+blockSize-1])) {
addEdgeBetweenVertices(blockStart, blockStart+blockSize-1, intLabel(rand() % nEdgeLabels), g, gp);
}
}
return g;
}
// BARABASI ALBERT Variants
/**
* Create a Graph according to one Formulation of the Barabasi Albert Graph Model.
*
* The core is consumed.
*/
struct Graph* barabasiAlpha(int n, int edgesAddedPerVertex, double alpha, struct Graph* core, struct GraphPool* gp) {
// min degree of core needs to be 1
if ((getMinDegree(core) == 0) || (n < core->n) || (edgesAddedPerVertex > core->n)) {
return NULL;
}
//m <= m_0
struct Graph* g = getGraph(gp);
setVertexNumber(g, n);
// TODO copying of core can be sped up by directly copying in initialized vertex array of g
for (int v=0; v<core->n; ++v) {
// copy vertex into g
g->vertices[v] = core->vertices[v];
core->vertices[v] = NULL;
// store degree of vertex in ->d for fast access
g->vertices[v]->d = degree(g->vertices[v]);
// set label of v
g->vertices[v]->label = intLabel(1);
g->vertices[v]->isStringMaster = 1;
}
g->m = core->m;
for (int v=core->n; v<n; ++v) {
g->vertices[v] = getVertex(gp->vertexPool);
g->vertices[v]->number = v;
g->vertices[v]->label = intLabel(1);
g->vertices[v]->isStringMaster = 1;
}
for (int v=core->n; v<n; ++v) {
if (rand() <= alpha * RAND_MAX) {
int w = rand() % v;
addEdgeBetweenVertices(v, w, intLabel(1), g, gp);
g->vertices[v]->d += 1;
g->vertices[w]->d += 1;
g->vertices[v]->neighborhood->isStringMaster = 1;
} else {
for (int i=0; i<edgesAddedPerVertex; ++i) {
int randV = rand() % (2 * g->m);
int find = 0;
for (int w=0; w<v; ++w) {
find += g->vertices[w]->d;
if (randV < find) {
if (!isNeighbor(g, v, w)) {
addEdgeBetweenVertices(v, w, intLabel(1), g, gp);
g->vertices[v]->d += 1;
g->vertices[w]->d += 1;
g->vertices[v]->neighborhood->isStringMaster = 1;
}
break;
}
}
}
}
}
return g;
}
// core is consumed
struct Graph* barabasiAlbert(int n, int edgesAddedPerVertex, struct Graph* core, struct GraphPool* gp) {
// min degree of core needs to be 1
if ((getMinDegree(core) == 0) || (n < core->n) || (edgesAddedPerVertex > core->n)) {
return NULL;
}
//m <= m_0
struct Graph* g = getGraph(gp);
setVertexNumber(g, n);
// TODO copying of core can be sped up by directly copying in initialized vertex array of g
for (int v=0; v<core->n; ++v) {
// copy vertex into g
g->vertices[v] = core->vertices[v];
core->vertices[v] = NULL;
// store degree of vertex in ->d for fast access
g->vertices[v]->d = degree(g->vertices[v]);
// set label of v
g->vertices[v]->label = intLabel(1);
g->vertices[v]->isStringMaster = 1;
}
g->m = core->m;
for (int v=core->n; v<n; ++v) {
g->vertices[v] = getVertex(gp->vertexPool);
g->vertices[v]->number = v;
g->vertices[v]->label = intLabel(1);
g->vertices[v]->isStringMaster = 1;
}
for (int v=core->n; v<n; ++v) {
for (int i=0; i<edgesAddedPerVertex; ++i) {
int randV = rand() % (2 * g->m);
int find = 0;
for (int w=0; w<v; ++w) {
find += g->vertices[w]->d;
if (randV < find) {
if (!isNeighbor(g, v, w)) {
addEdgeBetweenVertices(v, w, intLabel(1), g, gp);
g->vertices[v]->d += 1;
g->vertices[w]->d += 1;
g->vertices[v]->neighborhood->isStringMaster = 1;
}
break;
}
}
}
}
return g;
}
// RANDOM GEOMETRIC GRAPHS
inline double euclideanDistance(const int vx, const int vy, const int wx, const int wy) {
double vxx = vx / (double)RAND_MAX;
double vyy = vy / (double)RAND_MAX;
double wxx = wx / (double)RAND_MAX;
double wyy = wy / (double)RAND_MAX;
double xdiff = vxx - wxx;
double ydiff = vyy - wyy;
double result = sqrt(xdiff * xdiff + ydiff * ydiff);
return result;
}
inline double euclideanDistanceWrap(const int v, const int w, struct Graph* g) {
return euclideanDistance(g->vertices[v]->d, g->vertices[v]->lowPoint, g->vertices[w]->d, g->vertices[w]->lowPoint);
}
void printOverlapGraphDotFormat(struct Graph* g, FILE* out) {
fprintf(out, "graph %i {\n", g->number);
for (int v=0; v<g->n; ++v) {
fprintf(out, "%i [label=%s, pos=\"%lf,%lf\", pin=true, shape=point];\n", v, g->vertices[v]->label, g->vertices[v]->d / (double)RAND_MAX, g->vertices[v]->lowPoint / (double)RAND_MAX);
}
for (int v=0; v<g->n; ++v) {
for (struct VertexList* e=g->vertices[v]->neighborhood; e!=NULL; e=e->next) {
if (e->startPoint->number < e->endPoint->number) {
fprintf(out, "%i -- %i;\n", e->startPoint->number, e->endPoint->number);
}
}
}
fprintf(out, "}\n");
}
/**
* Create a graph
*/
struct Graph* randomOverlapGraph(int n, double d, struct GraphPool* gp) {
struct Graph* g = createGraph(n, gp);
// every vertex is a two-dimensional point
for (int v=0; v<n; ++v) {
g->vertices[v]->d = rand();
g->vertices[v]->lowPoint = rand();
g->vertices[v]->label = intLabel(1);
}
// add edge iff distance is smaller than d
for (int v=0; v<n; ++v) {
for (int w=v+1; w<n; ++w) {
if (euclideanDistanceWrap(v, w, g) < d) {
addEdgeBetweenVertices(v, w, intLabel(1), g, gp);
}
}
}
return g;
}
/**
* Create a graph
*/
struct Graph* randomOverlapGraphWithLabels(int n, double d, int nVertexLabels, struct GraphPool* gp) {
struct Graph* g = createGraph(n, gp);
// every vertex is a two-dimensional point
for (int v=0; v<n; ++v) {
g->vertices[v]->d = rand();
g->vertices[v]->lowPoint = rand();
g->vertices[v]->label = intLabel(rand() % nVertexLabels);
}
// add edge iff distance is smaller than d
for (int v=0; v<n; ++v) {
for (int w=v+1; w<n; ++w) {
if (euclideanDistanceWrap(v, w, g) < d) {
addEdgeBetweenVertices(v, w, intLabel(1), g, gp);
}
}
}
return g;
}
void moveVertexGaussian(struct Vertex* v, double moveParameter) {
generateIntegerGaussianNoise(&(v->d), &(v->lowPoint), moveParameter);
// mirror in unit interval
// stupid undefined behavior of abs(INT_MIN) requires to do it ourselves:
int a = v->d;
int b = v->lowPoint;
v->d = a == INT_MIN ? RAND_MAX : abs(a);
v->lowPoint = b == INT_MIN ? RAND_MAX : abs(b);
}
/**
* Create a graph
*/
struct Graph* randomClusteredOverlapGraphWithLabels(int n, double d, int nClusters, double mu, struct GraphPool* gp) {
if (nClusters > n) {
fprintf(stderr, "Number of clusters is larger than number of vertices: %i > %i\n", nClusters, n);
return NULL;
}
// ensure that nClusters divides number of vertices.
int nodesPerCluster = n / nClusters;
if (n != nodesPerCluster * nClusters) {
fprintf(stderr, "Randomly generated graph will have %i vertices instead of %i vertices\n", nodesPerCluster * nClusters, n);
n = nodesPerCluster * nClusters;
}
struct Graph* g = createGraph(n, gp);
// every vertex is a two-dimensional point
int i=0;
for (int v=0; v<nClusters; ++v) {
g->vertices[i]->d = rand();
g->vertices[i]->lowPoint = rand();
g->vertices[i]->label = intLabel(v);
g->vertices[i]->isStringMaster = 1;
for (int w=1; w<nodesPerCluster; ++w) {
g->vertices[i+w]->d = g->vertices[i]->d;
g->vertices[i+w]->lowPoint = g->vertices[i]->lowPoint;
g->vertices[i+w]->label = intLabel(v);
g->vertices[i+w]->isStringMaster = 1;
moveVertexGaussian(g->vertices[i+w], mu);
}
i += nodesPerCluster;
}
// add edge iff distance is smaller than d
for (int v=0; v<n; ++v) {
for (int w=v+1; w<n; ++w) {
if (euclideanDistanceWrap(v, w, g) < d) {
addEdgeBetweenVertices(v, w, intLabel(1), g, gp);
g->vertices[v]->neighborhood->isStringMaster = 1;
}
}
}
return g;
}
// Due to strangeness in A
void moveOverlapGraph(struct Graph* g, double moveParameter, double d, struct GraphPool* gp) {
// move vertices
for (int v=0; v<g->n; ++v) {
moveVertexGaussian(g->vertices[v], moveParameter);
}
// dump edges
for (int v=0; v<g->n; ++v) {
dumpVertexListRecursively(gp->listPool, g->vertices[v]->neighborhood);
g->vertices[v]->neighborhood = NULL;
}
g->m = 0;
// add edge iff distance is smaller than d
for (int v=0; v<g->n; ++v) {
for (int w=v+1; w<g->n; ++w) {
if (euclideanDistanceWrap(v, w, g) < d) {
addEdgeBetweenVertices(v, w, intLabel(1), g, gp);
g->vertices[v]->neighborhood->isStringMaster = 1;
}
}
}
}