-
Notifications
You must be signed in to change notification settings - Fork 0
/
randmio_und.cpp
114 lines (84 loc) · 2.65 KB
/
randmio_und.cpp
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
#include "bct.h"
/*
* Returns a randomized graph with equivalent degree sequence to the original
* weighted undirected graph. On average, each edge is rewired ITER times.
* Strength distributions are not preserved for weighted graphs.
*/
MATRIX_T* BCT_NAMESPACE::randmio_und(const MATRIX_T* R, int ITER) {
if (safe_mode) check_status(R, SQUARE | UNDIRECTED, "randmio_und");
gsl_rng* rng = get_rng();
// [i j]=find(tril(R));
MATRIX_T* tril_R = tril(R);
MATRIX_T* find_tril_R = find_ij(tril_R);
MATRIX_ID(free)(tril_R);
VECTOR_ID(view) i = MATRIX_ID(column)(find_tril_R, 0);
VECTOR_ID(view) j = MATRIX_ID(column)(find_tril_R, 1);
// K=length(i);
int K = length(&i.vector);
// ITER=K*ITER;
ITER = K * ITER;
MATRIX_T* _R = copy(R);
// for iter=1:ITER
for (int iter = 1; iter <= ITER; iter++) {
// while 1
while (true) {
int e1, e2;
int a, b, c, d;
// while 1
while (true) {
// e1=ceil(K*rand);
e1 = gsl_rng_uniform_int(rng, K);
// e2=ceil(K*rand);
e2 = gsl_rng_uniform_int(rng, K);
// while (e2==e1),
while (e2 == e1) {
// e2=ceil(K*rand);
e2 = gsl_rng_uniform_int(rng, K);
}
// a=i(e1); b=j(e1);
a = (int)VECTOR_ID(get)(&i.vector, e1);
b = (int)VECTOR_ID(get)(&j.vector, e1);
// c=i(e2); d=j(e2);
c = (int)VECTOR_ID(get)(&i.vector, e2);
d = (int)VECTOR_ID(get)(&j.vector, e2);
// if all(a~=[c d]) && all(b~=[c d]);
if (a != c && a != d && b != c && b != d) {
// break
break;
}
}
// if rand>0.5
if (gsl_rng_uniform(rng) > 0.5) {
// i(e2)=d; j(e2)=c;
VECTOR_ID(set)(&i.vector, e2, (FP_T)d);
VECTOR_ID(set)(&j.vector, e2, (FP_T)c);
// c=i(e2); d=j(e2);
c = (int)VECTOR_ID(get)(&i.vector, e2);
d = (int)VECTOR_ID(get)(&j.vector, e2);
}
// if ~(R(a,d) || R(c,b))
if (fp_zero(MATRIX_ID(get)(_R, a, d)) && fp_zero(MATRIX_ID(get)(_R, c, b))) {
// R(a,d)=R(a,b); R(a,b)=0;
MATRIX_ID(set)(_R, a, d, MATRIX_ID(get)(_R, a, b));
MATRIX_ID(set)(_R, a, b, 0.0);
// R(d,a)=R(b,a); R(b,a)=0;
MATRIX_ID(set)(_R, d, a, MATRIX_ID(get)(_R, b, a));
MATRIX_ID(set)(_R, b, a, 0.0);
// R(c,b)=R(c,d); R(c,d)=0;
MATRIX_ID(set)(_R, c, b, MATRIX_ID(get)(_R, c, d));
MATRIX_ID(set)(_R, c, d, 0.0);
// R(b,c)=R(d,c); R(d,c)=0;
MATRIX_ID(set)(_R, b, c, MATRIX_ID(get)(_R, d, c));
MATRIX_ID(set)(_R, d, c, 0.0);
// j(e1) = d;
VECTOR_ID(set)(&j.vector, e1, (FP_T)d);
// j(e2) = b;
VECTOR_ID(set)(&j.vector, e2, (FP_T)b);
// break;
break;
}
}
}
MATRIX_ID(free)(find_tril_R);
return _R;
}