void show_results_lite(igraph_vector_t * membership, igraph_real_t codelength) { printf("Codelength: %0.5f (in %d modules)\n", codelength, (int)igraph_vector_max(membership) + 1 ); printf("Membership (1/100 of vertices): "); int i; for (i=0; i < igraph_vector_size(membership); i+=100) { printf("%li ", (long)VECTOR(*membership)[i] ); } printf("\n"); }
/* Shrinks communities into single vertices, keeping all the edges. * This method is internal because it destroys the graph in-place and * creates a new one -- this is fine for the multilevel community * detection where a copy of the original graph is used anyway. * The membership vector will also be rewritten by the underlying * igraph_membership_reindex call */ int igraph_i_multilevel_shrink(igraph_t *graph, igraph_vector_t *membership) { igraph_vector_t edges; long int no_of_nodes = igraph_vcount(graph); long int no_of_edges = igraph_ecount(graph); igraph_bool_t directed = igraph_is_directed(graph); long int i; igraph_eit_t eit; if (no_of_nodes == 0) return 0; if (igraph_vector_size(membership) < no_of_nodes) { IGRAPH_ERROR("cannot shrink graph, membership vector too short", IGRAPH_EINVAL); } IGRAPH_VECTOR_INIT_FINALLY(&edges, no_of_edges * 2); IGRAPH_CHECK(igraph_reindex_membership(membership, 0)); /* Create the new edgelist */ igraph_eit_create(graph, igraph_ess_all(IGRAPH_EDGEORDER_ID), &eit); IGRAPH_FINALLY(igraph_eit_destroy, &eit); i = 0; while (!IGRAPH_EIT_END(eit)) { igraph_integer_t from, to; IGRAPH_CHECK(igraph_edge(graph, IGRAPH_EIT_GET(eit), &from, &to)); VECTOR(edges)[i++] = VECTOR(*membership)[(long int) from]; VECTOR(edges)[i++] = VECTOR(*membership)[(long int) to]; IGRAPH_EIT_NEXT(eit); } igraph_eit_destroy(&eit); IGRAPH_FINALLY_CLEAN(1); /* Create the new graph */ igraph_destroy(graph); no_of_nodes = (long int) igraph_vector_max(membership)+1; IGRAPH_CHECK(igraph_create(graph, &edges, (igraph_integer_t) no_of_nodes, directed)); igraph_vector_destroy(&edges); IGRAPH_FINALLY_CLEAN(1); return 0; }
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; }
int igraph_i_local_scan_1_sumweights(const igraph_t *graph, igraph_vector_t *res, const igraph_vector_t *weights) { long int no_of_nodes=igraph_vcount(graph); long int node, i, j, nn; igraph_inclist_t allinc; igraph_vector_int_t *neis1, *neis2; long int neilen1, neilen2; long int *neis; long int maxdegree; igraph_vector_int_t order; igraph_vector_int_t rank; igraph_vector_t degree, *edge1=°ree; /* reuse degree as edge1 */ if (igraph_vector_size(weights) != igraph_ecount(graph)) { IGRAPH_ERROR("Invalid weight vector length", IGRAPH_EINVAL); } igraph_vector_int_init(&order, no_of_nodes); IGRAPH_FINALLY(igraph_vector_int_destroy, &order); IGRAPH_VECTOR_INIT_FINALLY(°ree, no_of_nodes); IGRAPH_CHECK(igraph_degree(graph, °ree, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS)); maxdegree=(long int) igraph_vector_max(°ree)+1; igraph_vector_order1_int(°ree, &order, maxdegree); igraph_vector_int_init(&rank, no_of_nodes); IGRAPH_FINALLY(igraph_vector_int_destroy, &rank); for (i=0; i<no_of_nodes; i++) { VECTOR(rank)[ VECTOR(order)[i] ] = no_of_nodes-i-1; } IGRAPH_CHECK(igraph_inclist_init(graph, &allinc, IGRAPH_ALL)); IGRAPH_FINALLY(igraph_inclist_destroy, &allinc); IGRAPH_CHECK(igraph_i_trans4_il_simplify(graph, &allinc, &rank)); neis=igraph_Calloc(no_of_nodes, long int); if (neis==0) { IGRAPH_ERROR("undirected local transitivity failed", IGRAPH_ENOMEM); } IGRAPH_FINALLY(igraph_free, neis); IGRAPH_CHECK(igraph_strength(graph, res, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS, weights)); for (nn=no_of_nodes-1; nn>=0; nn--) { node=VECTOR(order)[nn]; IGRAPH_ALLOW_INTERRUPTION(); neis1=igraph_inclist_get(&allinc, node); neilen1=igraph_vector_int_size(neis1); /* Mark the neighbors of the node */ for (i=0; i<neilen1; i++) { int edge = VECTOR(*neis1)[i]; int nei = IGRAPH_OTHER(graph, edge, node); VECTOR(*edge1)[nei] = VECTOR(*weights)[edge]; neis[nei] = node+1; } for (i=0; i<neilen1; i++) { long int edge=VECTOR(*neis1)[i]; long int nei=IGRAPH_OTHER(graph, edge, node); igraph_real_t w=VECTOR(*weights)[edge]; neis2=igraph_inclist_get(&allinc, nei); neilen2=igraph_vector_int_size(neis2); for (j=0; j<neilen2; j++) { long int edge2=VECTOR(*neis2)[j]; long int nei2=IGRAPH_OTHER(graph, edge2, nei); igraph_real_t w2=VECTOR(*weights)[edge2]; if (neis[nei2] == node+1) { VECTOR(*res)[node] += w2; VECTOR(*res)[nei2] += w; VECTOR(*res)[nei] += VECTOR(*edge1)[nei2]; } } } } igraph_free(neis); igraph_inclist_destroy(&allinc); igraph_vector_int_destroy(&rank); igraph_vector_destroy(°ree); igraph_vector_int_destroy(&order); IGRAPH_FINALLY_CLEAN(5); 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; }
int igraph_revolver_d_d(const igraph_t *graph, igraph_integer_t niter, const igraph_vector_t *vtime, const igraph_vector_t *etime, igraph_matrix_t *kernel, igraph_matrix_t *sd, igraph_matrix_t *norm, igraph_matrix_t *cites, igraph_matrix_t *expected, igraph_real_t *logprob, igraph_real_t *lognull, const igraph_matrix_t *debug, igraph_vector_ptr_t *debugres) { igraph_integer_t no_of_events, vnoev, enoev; igraph_vector_t st; long int i; igraph_integer_t maxdegree; igraph_vector_t vtimeidx, etimeidx; igraph_lazy_inclist_t inclist; if (igraph_vector_size(vtime) != igraph_vcount(graph)) { IGRAPH_ERROR("Invalid vtime length", IGRAPH_EINVAL); } if (igraph_vector_size(etime) != igraph_ecount(graph)) { IGRAPH_ERROR("Invalid etime length", IGRAPH_EINVAL); } vnoev=(igraph_integer_t) igraph_vector_max(vtime)+1; enoev=(igraph_integer_t) igraph_vector_max(etime)+1; no_of_events= vnoev > enoev ? vnoev : enoev; IGRAPH_VECTOR_INIT_FINALLY(&st, no_of_events); for (i=0; i<no_of_events; i++) { VECTOR(st)[i]=1; } IGRAPH_CHECK(igraph_maxdegree(graph, &maxdegree, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS)); IGRAPH_VECTOR_INIT_FINALLY(&vtimeidx, 0); IGRAPH_VECTOR_INIT_FINALLY(&etimeidx, 0); IGRAPH_CHECK(igraph_vector_order1(vtime, &vtimeidx, no_of_events)); IGRAPH_CHECK(igraph_vector_order1(etime, &etimeidx, no_of_events)); IGRAPH_CHECK(igraph_lazy_inclist_init(graph, &inclist, IGRAPH_ALL)); IGRAPH_FINALLY(igraph_lazy_inclist_destroy, &inclist); IGRAPH_PROGRESS("Revolver d-d", 0, NULL); for (i=0; i<niter; i++) { IGRAPH_ALLOW_INTERRUPTION(); if (i+1 != niter) { /* not the last iteration */ /* measure */ IGRAPH_CHECK(igraph_revolver_mes_d_d(graph, &inclist, kernel, 0 /*sd*/, 0 /*norm*/, 0/*cites*/, 0/*debug*/, 0 /*debugres*/, &st, vtime, &vtimeidx, etime, &etimeidx, no_of_events, maxdegree)); /* normalize */ igraph_matrix_scale(kernel, 1/igraph_matrix_sum(kernel)); /* update st */ IGRAPH_CHECK(igraph_revolver_st_d_d(graph, &inclist, &st, kernel, vtime, &vtimeidx, etime, &etimeidx, no_of_events)); } else { /* measure */ IGRAPH_CHECK(igraph_revolver_mes_d_d(graph, &inclist, kernel, sd, norm, cites, debug, debugres, &st, vtime, &vtimeidx, etime, &etimeidx, no_of_events, maxdegree)); /* normalize */ igraph_matrix_scale(kernel, 1/igraph_matrix_sum(kernel)); /* update st */ IGRAPH_CHECK(igraph_revolver_st_d_d(graph, &inclist, &st, kernel, vtime, &vtimeidx, etime, &etimeidx, no_of_events)); /* expected number of citations */ if (expected) { IGRAPH_CHECK(igraph_revolver_exp_d_d(graph, &inclist, expected, kernel, &st, vtime, &vtimeidx, etime, &etimeidx, no_of_events, maxdegree)); } /* error calculation */ if (logprob || lognull) { IGRAPH_CHECK(igraph_revolver_error_d_d(graph, &inclist, kernel, &st, vtime, &vtimeidx, etime, &etimeidx, no_of_events, maxdegree, logprob, lognull)); } } IGRAPH_PROGRESS("Revolver d-d", 100.0*(i+1)/niter, NULL); } igraph_lazy_inclist_destroy(&inclist); igraph_vector_destroy(&etimeidx); igraph_vector_destroy(&vtimeidx); igraph_vector_destroy(&st); IGRAPH_FINALLY_CLEAN(4); return 0; }