int check(const igraph_vector_t *v1, const igraph_vector_t *v2, int code) { igraph_vector_t v; long int i, n=igraph_vector_size(v1); igraph_real_t m; igraph_vector_copy(&v, v1); igraph_vector_sub(&v, v2); for (i=0; i<n; i++) { VECTOR(v)[i] = fabs(VECTOR(v)[i]); } if ( (m=igraph_vector_max(&v)) > 0.01) { printf("Difference: %g\n", m); exit(code); } igraph_vector_destroy(&v); return 0; }
/* Fan-in/ Fan-out method */ igraph_t *ggen_generate_fifo(gsl_rng *r, unsigned long n, unsigned long od, unsigned long id) { igraph_t *g = NULL; igraph_vector_t available_od; igraph_vector_t out_degrees; igraph_vector_t vertices; igraph_vector_t choice; igraph_vector_t edges; unsigned long max; unsigned long i,j,k; unsigned long vcount = 1; int err; ggen_error_start_stack(); if(r == NULL) GGEN_SET_ERRNO(GGEN_EINVAL); if(id == 0 || od == 0 || od > n || id > n) GGEN_SET_ERRNO(GGEN_EINVAL); g = malloc(sizeof(igraph_t)); GGEN_CHECK_ALLOC(g); GGEN_FINALLY3(free,g,1); GGEN_CHECK_IGRAPH(igraph_empty(g,1,1)); GGEN_FINALLY3(igraph_destroy,g,1); GGEN_CHECK_IGRAPH(igraph_vector_init(&available_od,n)); GGEN_FINALLY(igraph_vector_destroy,&available_od); GGEN_CHECK_IGRAPH(igraph_vector_init(&out_degrees,n)); GGEN_FINALLY(igraph_vector_destroy,&out_degrees); GGEN_CHECK_IGRAPH(igraph_vector_init(&vertices,n)); GGEN_FINALLY(igraph_vector_destroy,&vertices); GGEN_CHECK_IGRAPH(igraph_vector_init(&choice,n)); GGEN_FINALLY(igraph_vector_destroy,&choice); GGEN_CHECK_IGRAPH(igraph_vector_init(&edges,n*2)); GGEN_FINALLY(igraph_vector_destroy,&edges); while(vcount < n) { // never trigger errors as it doesn't allocate or free memory igraph_vector_resize(&available_od,vcount); igraph_vector_resize(&out_degrees,vcount); igraph_vector_resize(&vertices,vcount); // compute the available out degree of each vertex GGEN_CHECK_IGRAPH(igraph_degree(g,&out_degrees,igraph_vss_all(),IGRAPH_OUT,0)); // fill available with od and substract out_degrees igraph_vector_fill(&available_od,od); GGEN_CHECK_IGRAPH(igraph_vector_sub(&available_od,&out_degrees)); if(gsl_ran_bernoulli(r,0.5)) //Fan-out Step { // find max max = igraph_vector_max(&available_od); // register all vertices having max as outdegree j = 0; for (i = 0; i < vcount; i++) if(VECTOR(available_od)[i] == max) VECTOR(vertices)[j++] = i; // choose randomly a vertex among availables GGEN_CHECK_GSL_DO(i = gsl_rng_uniform_int(r,j)); // how many children ? GGEN_CHECK_GSL_DO(j = gsl_rng_uniform_int(r,max)); j = j+1; // create all new nodes and add edges GGEN_CHECK_IGRAPH(igraph_add_vertices(g,j,NULL)); // cannot fail igraph_vector_resize(&edges,j*2); for(k = 0; k < j; k++) { VECTOR(edges)[2*k] = i; VECTOR(edges)[2*k+1] = vcount + k; } vcount+=k; } else //Fan-In Step { // register all vertices having an available outdegree j = 0; for (i = 0; i < vcount; i++) if(VECTOR(available_od)[i] > 0) VECTOR(vertices)[j++] = i; // we can add at most id vertices max =( j > id)? id: j; // how many edges to add GGEN_CHECK_GSL_DO(k = gsl_rng_uniform_int(r,max)); k = k+1; // choose that many nodes and add edges from them to the new node // cannot fail either igraph_vector_resize(&choice,k); gsl_ran_choose(r,VECTOR(choice),k, VECTOR(vertices),j,sizeof(VECTOR(vertices)[0])); // add a vertex to the graph GGEN_CHECK_IGRAPH(igraph_add_vertices(g,1,NULL)); igraph_vector_resize(&edges,k*2); // be carefull, vcount is the last ID of vertices not vcount +1 for(i = 0; i < k; i++) { VECTOR(edges)[2*i] = VECTOR(choice)[i]; VECTOR(edges)[2*i+1] = vcount; } vcount++; } // in all cases, edges should be added GGEN_CHECK_IGRAPH(igraph_add_edges(g,&edges,NULL)); } ggen_error_clean(1); return g; ggen_error_label: return NULL; }