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
|
/*
*
* gengraph - generation of random simple connected graphs with prescribed
* degree sequence
*
* Copyright (C) 2006 Fabien Viger
*
* 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 2 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/>.
*/
#include "gengraph_definitions.h"
#include "gengraph_random.h"
#include "gengraph_powerlaw.h"
#include "gengraph_degree_sequence.h"
#include "gengraph_hash.h"
#include "igraph_statusbar.h"
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <vector>
// using namespace __gnu_cxx;
using namespace std;
namespace gengraph {
// shuffle an int[] randomly
void random_permute(int *a, int n);
// sort an array of positive integers in time & place O(n + max)
void cumul_sort(int *q, int n);
void degree_sequence::detach() {
deg = NULL;
}
degree_sequence::~degree_sequence() {
if (deg != NULL) {
delete[] deg;
}
deg = NULL;
}
void degree_sequence::make_even(int mini, int maxi) {
if (total % 2 == 0) {
return;
}
if (maxi < 0) {
maxi = 0x7FFFFFFF;
}
int i;
for (i = 0; i < n; i++) {
if (deg[i] > mini) {
deg[i]--;
total--;
break;
} else if (deg[i] < maxi) {
deg[i]++;
total++;
break;
}
}
if (i == n) {
IGRAPH_WARNING("Warning: degree_sequence::make_even() forced one "
"degree to go over degmax");
deg[0]++;
total++;
}
}
void degree_sequence::shuffle() {
random_permute(deg, n);
}
void degree_sequence::sort() {
cumul_sort(deg, n);
}
void degree_sequence::compute_total() {
total = 0;
for (int i = 0; i < n; i++) {
total += deg[i];
}
}
degree_sequence::
degree_sequence(int n0, int *degs) {
deg = degs;
n = n0;
compute_total();
}
degree_sequence::
degree_sequence(const igraph_vector_t *out_seq) {
n = igraph_vector_size(out_seq);
deg = new int[n];
for (long int i = 0; i < n; i++) {
deg[i] = VECTOR(*out_seq)[i];
}
compute_total();
}
#ifndef FBUFF_SIZE
#define FBUFF_SIZE 999
#endif //FBUFF_SIZE
// degree_sequence::degree_sequence(FILE *f, bool DISTRIB) {
// n = 0;
// total = 0;
// char *buff = new char[FBUFF_SIZE];
// char *c;
// vector<int> degree;
// if(!DISTRIB) {
// // Input is a 'raw' degree sequence d0 d1 d2 d3 ...
// while(fgets(buff, FBUFF_SIZE, f)) {
// int d = strtol(buff, &c, 10);
// if(c == buff) continue;
// degree.push_back(d);
// total += d;
// }
// n = int(degree.size());
// deg = new int[n];
// int *yo = deg;
// vector<int>::iterator end = degree.end();
// for(vector<int>::iterator it=degree.begin(); it!=end; *(yo++) = *(it++));
// }
// else {
// // Input is a degree distribution : d0 #(degree=d0), d1 #(degree=d1), ...
// vector<int> n_with_degree;
// int line = 0;
// int syntax = 0;
// int ignored = 0;
// int first_syntax = 0;
// int first_ignored = 0;
// while(fgets(buff, FBUFF_SIZE, f)) {
// line++;
// int d = strtol(buff, &c, 10);
// if(c == buff) { ignored++; first_ignored = line; continue; }
// char *cc;
// int i = strtol(c, &cc, 10);
// if(cc == c) { syntax++; first_syntax = line; continue; }
// n += i;
// total += i*d;
// degree.push_back(d);
// n_with_degree.push_back(i);
// if( cc != c) { syntax++; first_syntax = line; }
// }
// if(VERBOSE()) {
// if(ignored > 0) fprintf(stderr,"Ignored %d lines (first was line #%d)\n", ignored, first_ignored);
// if(syntax > 0) fprintf(stderr,"Found %d probable syntax errors (first was line #%d)\n", syntax, first_syntax);
// }
// deg = new int[n];
// int *yo = deg;
// vector<int>::iterator it_n = n_with_degree.begin();
// for(vector<int>::iterator it = degree.begin(); it != degree.end(); it++)
// for(int k = *(it_n++); k--; *yo++ = *it);
// }
// if(VERBOSE()) {
// if(total % 2 != 0) fprintf(stderr,"Warning: degree sequence is odd\n");
// fprintf(stderr,"Degree sequence created. N=%d, 2M=%d\n", n, total);
// }
// }
// n vertices, exponent, min degree, max degree, average degree (optional, default is -1)
degree_sequence::
degree_sequence(int _n, double exp, int degmin, int degmax, double z) {
n = _n;
if (exp == 0.0) {
// Binomial distribution
if (z < 0) {
igraph_error("Fatal error in degree_sequence Ctor: "
"positive average degree must be specified", __FILE__,
__LINE__, IGRAPH_EINVAL);
}
if (degmax < 0) {
degmax = n - 1;
}
total = int(floor(double(n) * z + 0.5));
deg = new int[n];
KW_RNG::RNG myrand;
double p = (z - double(degmin)) / double(n);
total = 0;
for (int i = 0; i < n; i++) {
do {
deg[i] = 1 + myrand.binomial(p, n);
} while (deg[i] > degmax);
total += deg[i];
}
} else {
// Power-law distribution
igraph_status("Creating powerlaw sampler...", 0);
powerlaw pw(exp, degmin, degmax);
if (z == -1.0) {
pw.init();
igraph_statusf("done. Mean=%f\n", 0, pw.mean());
} else {
double offset = pw.init_to_mean(z);
igraph_statusf("done. Offset=%f, Mean=%f\n", 0, offset, pw.mean());
}
deg = new int[n];
total = 0;
int i;
igraph_statusf("Sampling %d random numbers...", 0, n);
for (i = 0; i < n; i++) {
deg[i] = pw.sample();
total += deg[i];
}
igraph_status("done\nSimple statistics on degrees...", 0);
int wanted_total = int(floor(z * n + 0.5));
sort();
igraph_statusf("done : Max=%d, Total=%d.\n", 0, deg[0], total);
if (z != -1.0) {
igraph_statusf("Adjusting total to %d...", 0, wanted_total);
int iterations = 0;
while (total != wanted_total) {
sort();
for (i = 0; i < n && total > wanted_total; i++) {
total -= deg[i];
if (total + degmin <= wanted_total) {
deg[i] = wanted_total - total;
} else {
deg[i] = pw.sample();
}
total += deg[i];
}
iterations += i;
for (i = n - 1; i > 0 && total < wanted_total; i--) {
total -= deg[i];
if (total + (deg[0] >> 1) >= wanted_total) {
deg[i] = wanted_total - total;
} else {
deg[i] = pw.sample();
}
total += deg[i];
}
iterations += n - 1 - i;
}
igraph_statusf("done(%d iterations).", 0, iterations);
igraph_statusf(" Now, degmax = %d\n", 0, dmax());
}
shuffle();
}
}
// void degree_sequence::print() {
// for(int i=0; i<n; i++) printf("%d\n",deg[i]);
// }
// void degree_sequence::print_cumul() {
// if(n==0) return;
// int dmax = deg[0];
// int dmin = deg[0];
// int i;
// for(i=1; i<n; i++) if(dmax<deg[i]) dmax=deg[i];
// for(i=1; i<n; i++) if(dmin>deg[i]) dmin=deg[i];
// int *dd = new int[dmax-dmin+1];
// for(i=dmin; i<=dmax; i++) dd[i-dmin]=0;
// if(VERBOSE()) fprintf(stderr,"Computing cumulative distribution...");
// for(i=0; i<n; i++) dd[deg[i]-dmin]++;
// if(VERBOSE()) fprintf(stderr,"done\n");
// for(i=dmin; i<=dmax; i++) if(dd[i-dmin]>0) printf("%d %d\n",i,dd[i-dmin]);
// delete[] dd;
// }
bool degree_sequence::havelhakimi() {
int i;
int dm = dmax() + 1;
// Sort vertices using basket-sort, in descending degrees
int *nb = new int[dm];
int *sorted = new int[n];
// init basket
for (i = 0; i < dm; i++) {
nb[i] = 0;
}
// count basket
for (i = 0; i < n; i++) {
nb[deg[i]]++;
}
// cumul
int c = 0;
for (i = dm - 1; i >= 0; i--) {
int t = nb[i];
nb[i] = c;
c += t;
}
// sort
for (i = 0; i < n; i++) {
sorted[nb[deg[i]]++] = i;
}
// Binding process starts
int first = 0; // vertex with biggest residual degree
int d = dm - 1; // maximum residual degree available
for (c = total / 2; c > 0; ) {
// We design by 'v' the vertex of highest degree (indexed by first)
// look for current degree of v
while (nb[d] <= first) {
d--;
}
// store it in dv
int dv = d;
// bind it !
c -= dv;
int dc = d; // residual degree of vertices we bind to
int fc = ++first; // position of the first vertex with degree dc
while (dv > 0 && dc > 0) {
int lc = nb[dc];
if (lc != fc) {
while (dv > 0 && lc > fc) {
// binds v with sorted[--lc]
dv--;
lc--;
}
fc = nb[dc];
nb[dc] = lc;
}
dc--;
}
if (dv != 0) { // We couldn't bind entirely v
delete[] nb;
delete[] sorted;
return false;
}
}
delete[] nb;
delete[] sorted;
return true;
}
//*************************
// Subroutines definitions
//*************************
inline int int_adjust(double x) {
return (int(floor(x + random_float())));
}
void random_permute(int *a, int n) {
int j, tmp;
for (int i = 0; i < n - 1; i++) {
j = i + my_random() % (n - i);
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
void cumul_sort(int *q, int n) {
// looks for the maximum q[i] and minimum
if (n == 0) {
return;
}
int qmax = q[0];
int qmin = q[0];
int i;
for (i = 0; i < n; i++) if (q[i] > qmax) {
qmax = q[i];
}
for (i = 0; i < n; i++) if (q[i] < qmin) {
qmin = q[i];
}
// counts #q[i] with given q
int *nb = new int[qmax - qmin + 1];
for (int *onk = nb + (qmax - qmin + 1); onk != nb; * (--onk) = 0) { }
for (i = 0; i < n; i++) {
nb[q[i] - qmin]++;
}
// counts cumulative distribution
for (i = qmax - qmin; i > 0; i--) {
nb[i - 1] += nb[i];
}
// sort by q[i]
int last_q;
int tmp;
int modifier = qmax - qmin + 1;
for (int current = 0; current < n; current++) {
tmp = q[current];
if (tmp >= qmin && tmp <= qmax) {
last_q = qmin;
do {
q[current] = last_q + modifier;
last_q = tmp;
current = --nb[last_q - qmin];
} while ((tmp = q[current]) >= qmin && tmp <= qmax);
q[current] = last_q + modifier;
}
}
delete[] nb;
for (i = 0; i < n; i++) {
q[i] = q[i] - modifier;
}
}
} // namespace gengraph
|