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
|
/**
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*
* (c) Vincenzo Nicosia 2009-2017 -- <v.nicosia@qmul.ac.uk>
*
* This file is part of NetBunch, a package for complex network
* analysis and modelling. For more information please visit:
*
* http://www.complex-networks.net/
*
* If you use this software, please add a reference to
*
* V. Latora, V. Nicosia, G. Russo
* "Complex Networks: Principles, Methods and Applications"
* Cambridge University Press (2017)
* ISBN: 9781107103184
*
***********************************************************************
*
* This program samples a degree sequence from a discrete power-law
* distribution with a given exponent.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <errno.h>
void usage(char *argv[]){
printf("********************************************************************\n"
"** **\n"
"** -*- power_law -*- **\n"
"** **\n"
"** Sample 'N' elements from a power law degree distribution **\n"
"** P(k) ~= k^{gamma} **\n"
"** with degrees in the range [k_min, k_max], and print them **\n"
"** on STDOUT. **\n"
"** **\n"
"** If the obtained degree sequence is even, i.e., if the sum **\n"
"** of all the sampled degrees is even, the program returns 0 **\n"
"** (zero), otherwise it returns 1. **\n"
"** **\n"
"********************************************************************\n"
" This is Free Software - You can use and distribute it under \n"
" the terms of the GNU General Public License, version 3 or later\n\n"
" (c) Vincenzo Nicosia 2010-2017 (v.nicosia@qmul.ac.uk)\n\n"
"********************************************************************\n\n"
);
printf("Usage: %s <gamma> <k_min> <k_max> <N>\n" , argv[0]);
}
void create_distr(double *v, double gamma, int k_min, int k_max){
int n, i;
double sum, new_sum;
n = k_max-k_min + 1;
sum = 0;
for(i=0; i<n; i++){
v[i] = pow((k_min + i), gamma);
sum += v[i];
}
new_sum = 0;
/* Now we normalize the array*/
for(i=0; i<n; i++){
v[i]/= sum;
new_sum += v[i];
}
/* Now we compute the cumulative distribution*/
for(i=1; i<n; i++){
v[i] += v[i-1];
}
}
int find_degree(double *v, int dim, double xi){
int i;
i=0;
while(xi > v[i])
i++;
return i;
}
int main(int argc, char *argv[]){
double gamma, xi, val, q;
unsigned int N, i, distr_num, k, k_min, k_max, K;
double *distr;
if (argc < 5){
usage(argv);
exit(1);
}
srand(time(NULL));
gamma = atof(argv[1]);
k_min = atoi(argv[2]);
k_max = atoi(argv[3]);
N = atoi(argv[4]);
K = 0;
distr_num = k_max - k_min + 1;
distr = malloc(distr_num * sizeof(double));
create_distr(distr, gamma, k_min, k_max);
for(i=0; i<N;){
xi = rand()* 1.0 / RAND_MAX;
k = find_degree(distr, distr_num, xi);
val = rand()*1.0/RAND_MAX;
q = k_min + xi * distr_num;
q = q / (floor(q) + 1);
q = pow(q, gamma);
if (val <= q){
printf("%d\n", k+k_min);
K += k+k_min;
i++;
}
}
free(distr);
/* Return 0 if the degree sequence is even, or 1 otherwise */
return K%2;
}
|