-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRandom.cpp
More file actions
77 lines (65 loc) · 1.99 KB
/
Random.cpp
File metadata and controls
77 lines (65 loc) · 1.99 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
/* -------------------------------------------------
_ _ ___
__ _| |_ _ _(_)___/ __| __ __ _ _ _ _ _ ___ _ _
/ _` | _| '_| / -_)__ \/ _/ _` | ' \| ' \/ -_) '_|
\__, |\__|_| |_\___|___/\__\__,_|_||_|_||_\___|_|
|___/
gtrieScanner: quick discovery of network motifs
Released under Artistic License 2.0
(see README and LICENSE)
Pedro Ribeiro - CRACS & INESC-TEC, DCC/FCUP
----------------------------------------------------
Randomization methods
Last Update: 11/02/2012
---------------------------------------------------- */
#include "Random.h"
// Initialize pseudo-random generator with seed 's'
void Random::seed(int s) {
srandom(s);
}
// Pseudo-Random number between 'a' and 'b' (inclusive)
int Random::getInteger(int a, int b) {
double aux = random() / (double)RAND_MAX;
return a + aux*(b-a+1);
}
// Pseudo-Random number between 0 and 1
double Random::getDouble() {
return random() / (double)RAND_MAX;
}
// Randomize 'g' network with 'num' exchanges per edge and 'tries' attempts per edge
void Random::markovChainPerEdge(Graph *g, int num, int tries) {
int i, j, k, n, edges, nodes = g->numNodes();
int a, b, c, d, aux;
vector<int> *v, *u;
vector<int>:: iterator ii;
a=b=c=d=0;
for (n=0; n<num; n++)
for (i=0; i<nodes; i++) {
v = g->outEdges(i);
edges = v->size();
a = i;
for (j=0; j<edges; j++) {
b = (*v)[j];
for (k=0; k<tries; k++) {
c = getInteger(0, nodes-1);
if (a==c || b==c || g->hasEdge(c,b)) continue;
aux = g->nodeOutEdges(c);
if (aux==0) continue;
u = g->outEdges(c);
d = getInteger(1, aux);
d = (*u)[d-1];
if (a==d || b==d || g->hasEdge(a,d)) continue;
break;
}
if (k<tries) { // Found an edge to swap!
fflush(stdout);
g->rmEdge(a,b); g->rmEdge(c,d);
g->addEdge(a,d); g->addEdge(c,b);
if (g->type() == UNDIRECTED) {
g->rmEdge(b,a); g->rmEdge(d,c);
g->addEdge(d,a); g->addEdge(b,c);
}
}
}
}
}