/* Erdos-Renyi : G(n,M) */ igraph_t *ggen_generate_erdos_gnm(gsl_rng *r, unsigned long n, unsigned long m) { igraph_matrix_t adj; igraph_t *g = NULL; int err; unsigned long i,j; unsigned long added_edges = 0; ggen_error_start_stack(); if(r == NULL) GGEN_SET_ERRNO(GGEN_EINVAL); if(m > (n*(n-1)/2)) GGEN_SET_ERRNO(GGEN_EINVAL); g = malloc(sizeof(igraph_t)); GGEN_CHECK_ALLOC(g); GGEN_FINALLY3(free,g,1); if(m == 0 || n <= 1) { GGEN_CHECK_IGRAPH(igraph_empty(g,n,1)); goto end; } if(m == (n*(n-1))/2) { GGEN_CHECK_IGRAPH(igraph_full_citation(g,n,1)); goto end; } GGEN_CHECK_IGRAPH(igraph_matrix_init(&adj,n,n)); GGEN_FINALLY(igraph_matrix_destroy,&adj); igraph_matrix_null(&adj); while(added_edges < m) { GGEN_CHECK_GSL_DO(i = gsl_rng_uniform_int(r,n)); GGEN_CHECK_GSL_DO(j = gsl_rng_uniform_int(r,n)); if(i < j && igraph_matrix_e(&adj,i,j) == 0) { igraph_matrix_set(&adj,i,j,1); added_edges++; } } GGEN_CHECK_IGRAPH(igraph_adjacency(g,&adj,IGRAPH_ADJ_DIRECTED)); end: ggen_error_clean(1); return g; ggen_error_label: return NULL; }
/* Erdos-Renyi : G(n,p) */ igraph_t *ggen_generate_erdos_gnp(gsl_rng *r, unsigned long n, double p) { igraph_matrix_t m; igraph_t *g = NULL; int err; unsigned long i,j; ggen_error_start_stack(); if(r == NULL) GGEN_SET_ERRNO(GGEN_EINVAL); if(p < 0.0 || p > 1.0) GGEN_SET_ERRNO(GGEN_EINVAL); g = malloc(sizeof(igraph_t)); GGEN_CHECK_ALLOC(g); GGEN_FINALLY3(free,g,1); if(p == 0.0) { GGEN_CHECK_IGRAPH(igraph_empty(g,n,1)); goto end; } if(p == 1.0) { GGEN_CHECK_IGRAPH(igraph_full_citation(g,n,1)); goto end; } GGEN_CHECK_IGRAPH(igraph_matrix_init(&m,n,n)); GGEN_FINALLY(igraph_matrix_destroy,&m); for(i = 0; i < n; i++) for(j = 0; j < n; j++) if(i < j) // coin flipping to determine if we add an edge or not igraph_matrix_set(&m,i,j,gsl_ran_bernoulli(r,p)); else igraph_matrix_set(&m,i,j,0); GGEN_CHECK_IGRAPH(igraph_adjacency(g,&m,IGRAPH_ADJ_DIRECTED)); end: ggen_error_clean(1); return g; ggen_error_label: return NULL; }
int main(int argc,char** argv) { igraph_vector_t *lp; igraph_t g; // all ggen methods should fail on incorrect arguments assert(ggen_analyze_longest_path(NULL) == NULL); // an empty graph as a longest path of zero igraph_empty(&g,10,1); lp = ggen_analyze_longest_path(&g); assert(lp != NULL); assert(igraph_vector_size(lp) == 0); igraph_destroy(&g); igraph_vector_destroy(lp); free((void *)lp); // a full dag as a longest path of containing all vertices igraph_full_citation(&g,10,1); lp = ggen_analyze_longest_path(&g); assert(lp != NULL); assert(igraph_vector_size(lp) == 10); igraph_destroy(&g); igraph_vector_destroy(lp); free((void *)lp); // a wrong graph (not dag) should fail // graph is 0 -> 1 -> 2 and 2 -> 0 igraph_small(&g,10,1,0,1,1,2,2,0,-1); assert(ggen_analyze_longest_path(&g) == NULL); igraph_destroy(&g); // a simple graph should work too // graph is 0 -> 1 -> 2 and 0 -> 2 igraph_small(&g,10,1,0,1,1,2,0,2,-1); lp = ggen_analyze_longest_path(&g); assert(lp != NULL); assert(igraph_vector_size(lp) == 3); igraph_destroy(&g); igraph_vector_destroy(lp); free((void *)lp); return 0; }
igraph_t *ggen_generate_erdos_lbl(gsl_rng *r, unsigned long n, double p, unsigned long nbl) { igraph_t *g = NULL; igraph_matrix_t m; igraph_vector_t layers; unsigned long i,j; int err; ggen_error_start_stack(); if(r == NULL) GGEN_SET_ERRNO(GGEN_EINVAL); if(p < 0.0 || p > 1.0) GGEN_SET_ERRNO(GGEN_EINVAL); if(nbl > n || nbl == 0) GGEN_SET_ERRNO(GGEN_EINVAL); g = malloc(sizeof(igraph_t)); GGEN_CHECK_ALLOC(g); GGEN_FINALLY3(free,g,1); if(p == 0.0) { GGEN_CHECK_IGRAPH(igraph_empty(g,n,1)); goto end; } if(p == 1.0 && nbl == n) { GGEN_CHECK_IGRAPH(igraph_full_citation(g,n,1)); goto end; } GGEN_CHECK_IGRAPH(igraph_matrix_init(&m,n,n)); GGEN_FINALLY(igraph_matrix_destroy,&m); GGEN_CHECK_IGRAPH(igraph_vector_init(&layers,n)); GGEN_FINALLY(igraph_vector_destroy,&layers); // asign to each vertex a layer for(i = 0; i < n; i++) { GGEN_CHECK_GSL_DO(j = gsl_rng_uniform_int(r,nbl)); VECTOR(layers)[i] = j; } // create edges for(i = 0; i < n; i++) for(j = 0; j < n; j++) // if the layer allocation allows the edge, we test for it if(VECTOR(layers)[i]<VECTOR(layers)[j]) igraph_matrix_set(&m,i,j,gsl_ran_bernoulli(r,p)); else igraph_matrix_set(&m,i,j,0); //translate the matrix to a graph GGEN_CHECK_IGRAPH(igraph_adjacency(g,&m,IGRAPH_ADJ_DIRECTED)); end: ggen_error_clean(1); return g; ggen_error_label: return NULL; }