-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandmio_dir.cpp
92 lines (67 loc) · 2.03 KB
/
randmio_dir.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
#include "bct.h"
/*
* Returns a randomized graph with equivalent degree sequence to the original
* weighted directed graph. On average, each edge is rewired ITER times. Out-
* strength is preserved for weighted graphs, while in-strength is not.
*/
MATRIX_T* BCT_NAMESPACE::randmio_dir(const MATRIX_T* R, int ITER) {
if (safe_mode) check_status(R, SQUARE | DIRECTED, "randmio_dir");
gsl_rng* rng = get_rng();
// [i j]=find(R);
MATRIX_T* find_R = find_ij(R);
VECTOR_ID(view) i = MATRIX_ID(column)(find_R, 0);
VECTOR_ID(view) j = MATRIX_ID(column)(find_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 ~(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(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);
// 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_R);
return _R;
}