int main(void) { igraph_real_t avg_path; igraph_t graph; igraph_vector_t dimvector; igraph_vector_t edges; int i; igraph_vector_init(&dimvector, 2); VECTOR(dimvector)[0]=30; VECTOR(dimvector)[1]=30; igraph_lattice(&graph, &dimvector, 0, IGRAPH_UNDIRECTED, 0, 1); igraph_rng_seed(igraph_rng_default(), 42); igraph_vector_init(&edges, 20); for (i=0; i<igraph_vector_size(&edges); i++) { VECTOR(edges)[i] = rand() % (int)igraph_vcount(&graph); } igraph_average_path_length(&graph, &avg_path, IGRAPH_UNDIRECTED, 1); printf("Average path length (lattice): %f\n", (double) avg_path); igraph_add_edges(&graph, &edges, 0); igraph_average_path_length(&graph, &avg_path, IGRAPH_UNDIRECTED, 1); printf("Average path length (randomized lattice): %f\n", (double) avg_path); igraph_vector_destroy(&dimvector); igraph_vector_destroy(&edges); igraph_destroy(&graph); return 0; }
int main() { igraph_t g; igraph_vs_t vertices; igraph_vector_t result1, result2; igraph_rng_seed(igraph_rng_default(), 42); igraph_vector_init(&result1, 0); igraph_vector_init(&result2, 0); igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNP, 100, .1, IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS); igraph_vs_seq(&vertices, 0, 99); igraph_transitivity_local_undirected(&g, &result1, igraph_vss_all(), IGRAPH_TRANSITIVITY_NAN); igraph_transitivity_local_undirected(&g, &result2, vertices, IGRAPH_TRANSITIVITY_NAN); if (!igraph_vector_all_e(&result1, &result2)) { igraph_vector_print(&result1); igraph_vector_print(&result2); return 1; } igraph_vector_destroy(&result1); igraph_vector_destroy(&result2); igraph_vs_destroy(&vertices); igraph_destroy(&g); return 0; }
int main() { igraph_matrix_t A; igraph_vector_t values; igraph_matrix_t vectors; int i, j; igraph_eigen_which_t which; igraph_rng_seed(igraph_rng_default(), 42 * 42); igraph_matrix_init(&A, DIM, DIM); igraph_matrix_init(&vectors, 0, 0); igraph_vector_init(&values, 0); /* All eigenvalues and eigenvectors */ for (i=0; i<DIM; i++) { for (j=i; j<DIM; j++) { MATRIX(A, i, j) = MATRIX(A, j, i) = igraph_rng_get_integer(igraph_rng_default(), 1, 10); } } which.pos=IGRAPH_EIGEN_LM; which.howmany=5; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); which.howmany=8; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); which.pos=IGRAPH_EIGEN_BE; which.howmany=5; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); which.pos=IGRAPH_EIGEN_SM; which.howmany=5; igraph_eigen_matrix_symmetric(&A, /*sA=*/ 0, /*fun=*/ 0, DIM, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_print(&values); check_ev(&A, &values, &vectors); igraph_vector_destroy(&values); igraph_matrix_destroy(&vectors); igraph_matrix_destroy(&A); return 0; }
int main() { const int nodes=10, skip=3; igraph_matrix_t mat2; igraph_vector_complex_t values, values2; igraph_matrix_complex_t vectors, vectors2; igraph_eigen_which_t which; int i; igraph_rng_seed(igraph_rng_default(), 42); igraph_matrix_init(&mat2, nodes, nodes); for (i=0; i<nodes; i++) { int j; for (j=0; j<nodes; j++) { MATRIX(mat2, i, j) = igraph_rng_get_integer(igraph_rng_default(), 1, 10); } } igraph_vector_complex_init(&values, 0); igraph_matrix_complex_init(&vectors, 0, 0); which.pos=IGRAPH_EIGEN_LI; which.howmany=nodes; igraph_eigen_matrix(&mat2, /*sparsemat=*/ 0, /*fun=*/ 0, nodes, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values, &vectors); igraph_vector_complex_init(&values2, 0); igraph_matrix_complex_init(&vectors2, 0, 0); which.pos=IGRAPH_EIGEN_SI; which.howmany=nodes; igraph_eigen_matrix(&mat2, /*sparsemat=*/ 0, /*fun=*/ 0, nodes, /*extra=*/ 0, IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, &values2, &vectors2); igraph_vector_complex_print(&values); igraph_vector_complex_print(&values2); for (i=0; i<nodes; i++) { int j; igraph_real_t d= igraph_complex_abs(igraph_complex_sub(VECTOR(values)[i], VECTOR(values2)[nodes-i-1])); if (d > 1e-15) { DUMP(); return 2; } for (j=0; j<nodes; j++) { igraph_real_t d= igraph_complex_abs(igraph_complex_sub(MATRIX(vectors, j, i), MATRIX(vectors2, j, nodes-i-1))); if (d > 1e-15) { DUMP(); return 3; } } } igraph_vector_complex_destroy(&values); igraph_matrix_complex_destroy(&vectors); igraph_vector_complex_destroy(&values2); igraph_matrix_complex_destroy(&vectors2); igraph_matrix_destroy(&mat2); return 0; }
int main() { igraph_t karate; igraph_vector_t parents, weights; igraph_rng_seed(igraph_rng_default(), 42); igraph_small(&karate, 34, IGRAPH_UNDIRECTED, 0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,10,0,11,0,12,0,13, 0,17,0,19,0,21,0,31, 1,2,1,3,1,7,1,13,1,17,1,19,1,21,1,30, 2,3,2,7,2,27,2,28,2,32,2,9,2,8,2,13, 3,7,3,12,3,13, 4,6,4,10, 5,6,5,10,5,16, 6,16, 8,30,8,32,8,33, 9,33, 13,33, 14,32,14,33, 15,32,15,33, 18,32,18,33, 19,33, 20,32,20,33, 22,32,22,33, 23,25,23,27,23,32,23,33,23,29, 24,25,24,27,24,31, 25,31, 26,29,26,33, 27,33, 28,31,28,33, 29,32,29,33, 30,32,30,33, 31,32,31,33, 32,33, -1); igraph_vector_init(&parents, 0); igraph_vector_init(&weights, 0); igraph_hrg_consensus(&karate, &parents, &weights, /* hrg= */ 0, /* start= */ 0, /* num_samples= */ 100); /* Check */ igraph_vector_print(&parents); igraph_vector_print(&weights); igraph_vector_destroy(&parents); igraph_vector_destroy(&weights); igraph_destroy(&karate); #ifdef __APPLE__ return 0; #else return 77; #endif }
int main(void) { igraph_integer_t diameter; igraph_t graph; igraph_rng_seed(igraph_rng_default(), 42); igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, 1000, 5.0/1000, IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS); igraph_diameter(&graph, &diameter, 0, 0, 0, IGRAPH_UNDIRECTED, 1); printf("Diameter of a random graph with average degree 5: %d\n", (int) diameter); igraph_destroy(&graph); return 0; }
int main() { igraph_t small, big; igraph_matrix_t small_coords, big_coords, merged_coords; igraph_vector_ptr_t graph_ptr, coords_ptr; igraph_arpack_options_t arpack_opts; /* To make things reproducible */ igraph_rng_seed(igraph_rng_default(), 42); igraph_small(&big, 10, IGRAPH_UNDIRECTED, 0,1, 1,2, 2,3, 3,4, 4,5, 5,6, 6,7, 7,8, 8,9, 9,0, -1); igraph_small(&small, 3, IGRAPH_UNDIRECTED, 0,1, 1,2, 2,0, -1); igraph_arpack_options_init(&arpack_opts); igraph_matrix_init(&big_coords, 0, 0); igraph_layout_mds(&big, &big_coords, /*dist=*/ 0, /*dim=*/ 2, &arpack_opts); igraph_matrix_init(&small_coords, 0, 0); igraph_layout_mds(&small, &small_coords, /*dist=*/ 0, /*dim=*/ 2, &arpack_opts); igraph_vector_ptr_init(&graph_ptr, 2); igraph_vector_ptr_init(&coords_ptr, 2); igraph_matrix_init(&merged_coords, 0, 0); VECTOR(graph_ptr)[0] = &big; VECTOR(graph_ptr)[1] = &small; VECTOR(coords_ptr)[0] = &big_coords; VECTOR(coords_ptr)[1] = &small_coords; igraph_layout_merge_dla(&graph_ptr, &coords_ptr, &merged_coords); igraph_matrix_print(&merged_coords); igraph_matrix_destroy(&merged_coords); igraph_matrix_destroy(&small_coords); igraph_matrix_destroy(&big_coords); igraph_vector_ptr_destroy(&graph_ptr); igraph_vector_ptr_destroy(&coords_ptr); igraph_destroy(&small); igraph_destroy(&big); #ifdef __APPLE__ return 0; #else return 77; #endif }
int main() { igraph_t graph; igraph_vector_ptr_t cliques; igraph_rng_t rng; igraph_rng_init(&rng, &igraph_rngtype_mt19937); igraph_rng_seed(&rng, 42); igraph_rng_seed(igraph_rng_default(), 42); igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, /*n=*/ 100, /*p=*/ 0.7, /*directed=*/ 0, /*loops=*/ 0, &rng); igraph_vector_ptr_init(&cliques, 0); igraph_maximal_cliques(&graph, &cliques, /*min_size=*/ 15, /*max_size=*/ 0); print_and_destroy(&cliques); igraph_destroy(&graph); igraph_rng_destroy(&rng); return 0; }
int main() { igraph_t g; igraph_vector_ptr_t res; igraph_integer_t i, n; igraph_rng_seed(igraph_rng_default(), 42); igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNM, 100, 3000, /* directed = */ 0, /* loops= */ 0); igraph_vector_ptr_init(&res, 0); BENCH("1 Cliques in random graph with 100 vertices and 3000 edges", igraph_cliques(&g, &res, /* min_size= */ 0, /* max_size= */ 0); );
int main() { igraph_matrix_t A; igraph_matrix_t vectors_left, vectors_right; igraph_vector_t values_real, values_imag; int i, j; int info=1; int ilo, ihi; igraph_real_t abnrm; igraph_rng_seed(igraph_rng_default(), 42); igraph_matrix_init(&A, DIM, DIM); igraph_matrix_init(&vectors_left, 0, 0); igraph_matrix_init(&vectors_right, 0, 0); igraph_vector_init(&values_real, 0); igraph_vector_init(&values_imag, 0); for (i=0; i<DIM; i++) { for (j=0; j<DIM; j++) { MATRIX(A, i, j) = igraph_rng_get_integer(igraph_rng_default(), 1, 10); } } igraph_lapack_dgeevx(IGRAPH_LAPACK_DGEEVX_BALANCE_BOTH, &A, &values_real, &values_imag, &vectors_left, &vectors_right, &ilo, &ihi, /*scale=*/ 0, &abnrm, /*rconde=*/ 0, /*rcondv=*/ 0, &info); if (check_ev(&A, &values_real, &values_imag, &vectors_left, &vectors_right, /*tol=*/ 1e-8)) { return 1; } /* igraph_matrix_print(&A); */ /* igraph_vector_print(&values_real); */ /* igraph_vector_print(&values_imag); */ /* igraph_matrix_print(&vectors_left); */ /* igraph_matrix_print(&vectors_right); */ igraph_vector_destroy(&values_imag); igraph_vector_destroy(&values_real); igraph_matrix_destroy(&vectors_right); igraph_matrix_destroy(&vectors_left); igraph_matrix_destroy(&A); return 0; }
int main() { igraph_t graph; igraph_vector_t walk, weights; igraph_integer_t ec, i; igraph_rng_seed(igraph_rng_default(), 137); igraph_vector_init(&walk, 0); igraph_vector_init(&weights, 0); /* This directed graph has loop edges. It also has multi-edges when considered as undirected. */ igraph_de_bruijn(&graph, 3, 2); ec = igraph_ecount(&graph); /* unweighted, directed */ igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 1000, IGRAPH_RANDOM_WALK_STUCK_RETURN); assert(igraph_vector_size(&walk) == 1000); /* unweighted, undirected */ igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_ALL, 1000, IGRAPH_RANDOM_WALK_STUCK_RETURN); assert(igraph_vector_size(&walk) == 1000); igraph_vector_resize(&weights, ec); for (i=0; i < ec; ++i) VECTOR(weights)[i] = igraph_rng_get_unif01(igraph_rng_default()); /* weighted, directed */ igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_OUT, 1000, IGRAPH_RANDOM_WALK_STUCK_RETURN); assert(igraph_vector_size(&walk) == 1000); /* weighted, undirecetd */ igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_ALL, 1000, IGRAPH_RANDOM_WALK_STUCK_RETURN); assert(igraph_vector_size(&walk) == 1000); igraph_destroy(&graph); /* 1-vertex graph, should get stuck */ igraph_empty(&graph, 1, /* directed = */ 0); igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 1000, IGRAPH_RANDOM_WALK_STUCK_RETURN); assert(igraph_vector_size(&walk) == 0); igraph_destroy(&graph); igraph_vector_destroy(&weights); igraph_vector_destroy(&walk); return 0; }
int main() { igraph_t graph; igraph_t full, tree; igraph_hrg_t hrg; igraph_t dendrogram; // int i, j; // igraph_vector_t neis; igraph_rng_seed(igraph_rng_default(), 42); // We need attributes igraph_i_set_attribute_table(&igraph_cattribute_table); igraph_full(&full, 10, /*directed=*/ 0, /*loops=*/ 0); igraph_tree(&tree, 15, /*children=*/ 2, /*type=*/ IGRAPH_TREE_UNDIRECTED); igraph_disjoint_union(&graph, &full, &tree); igraph_add_edge(&graph, 0, 10); igraph_destroy(&full); igraph_destroy(&tree); // Fit igraph_hrg_init(&hrg, igraph_vcount(&graph)); igraph_hrg_fit(&graph, &hrg, /*start=*/ 0, /*steps=*/ 0); // Create a graph from it igraph_hrg_dendrogram(&dendrogram, &hrg); // Print the tree, with labels // igraph_vector_init(&neis, 0); // for (i=0; i<igraph_vcount(&graph)-1; i++) { // printf("Vertex # %2i, ", (int) (i+igraph_vcount(&graph))); // igraph_neighbors(&dendrogram, &neis, i+igraph_vcount(&graph), IGRAPH_OUT); // printf("left: # %2i, right: # %2i, ", (int) VECTOR(neis)[0], // (int) VECTOR(neis)[1]); // printf("prob: %6.2g\n", // VAN(&dendrogram, "probability", i+igraph_vcount(&graph))); // } // igraph_vector_destroy(&neis); igraph_destroy(&dendrogram); igraph_hrg_destroy(&hrg); igraph_destroy(&graph); return 0; }
int main() { igraph_matrix_t mat; igraph_sparsemat_t spmat, spmat2; int i; igraph_real_t m1, m2; igraph_rng_seed(igraph_rng_default(), 42); igraph_sparsemat_init(&spmat, DIM1, DIM2, 20); igraph_sparsemat_entry(&spmat, 1, 2, -1.0); igraph_sparsemat_entry(&spmat, 3, 2, 10.0); for (i=0; i<10; i++) { igraph_sparsemat_entry(&spmat, INT(DIM1-1), INT(DIM2-1), 1.0); } igraph_sparsemat_entry(&spmat, 1, 2, -1.0); igraph_sparsemat_entry(&spmat, 3, 2, 10.0); igraph_sparsemat_compress(&spmat, &spmat2); igraph_matrix_init(&mat, 0, 0); igraph_sparsemat_as_matrix(&mat, &spmat2); m1=igraph_sparsemat_min(&spmat2); m2=igraph_matrix_min(&mat); if (m1 != m2) { printf("%f %f\n", m1, m2); return 1; } m1=igraph_sparsemat_max(&spmat2); m2=igraph_matrix_max(&mat); if (m1 != m2) { printf("%f %f\n", m1, m2); return 2; } igraph_sparsemat_minmax(&spmat2, &m1, &m2); if (m1 != igraph_matrix_min(&mat)) { return 3; } if (m2 != igraph_matrix_max(&mat)) { return 4; } igraph_matrix_destroy(&mat); igraph_sparsemat_destroy(&spmat); igraph_sparsemat_destroy(&spmat2); return 0; }
int main() { int nodes=10; igraph_t g; igraph_matrix_t L, R; igraph_sparsemat_t Lsparse, Rsparse; igraph_matrix_t V, V3; igraph_matrix_complex_t V2; igraph_sparsemat_t stochastic, stochasticT; igraph_vector_t groups; igraph_eigen_which_t which; igraph_vector_t p, selcol; igraph_matrix_init(&L, 0, 0); igraph_matrix_init(&R, 0, 0); igraph_matrix_init(&V, 0, 0); igraph_matrix_init(&V3, 0, 0); igraph_vector_init(&groups, 0); igraph_vector_init(&selcol, 1); igraph_rng_seed(igraph_rng_default(), 42); igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED); igraph_sparsemat_init(&stochastic, nodes, nodes, igraph_ecount(&g)*2); igraph_matrix_complex_init(&V2, 0, 0); igraph_vector_init(&p, 0); igraph_rng_seed(igraph_rng_default(), 42); igraph_get_stochastic_sparsemat(&g, &stochastic, /*column-wise=*/ 0); igraph_sparsemat_transpose(&stochastic, &stochasticT, /*values=*/ 1); which.pos=IGRAPH_EIGEN_LR; which.howmany=1; igraph_eigen_matrix(/*matrix=*/ 0, &stochasticT, /*fun=*/ 0, 10, /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, /*values=*/ 0, &V2); igraph_matrix_complex_real(&V2, &V); /* `p' is always the eigenvector corresponding to the 1-eigenvalue */ igraph_matrix_get_col(&V, &p, 0); which.howmany=3; igraph_eigen_matrix(/*matrix=*/ 0, &stochastic, /*fun=*/ 0, 10, /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, /*values=*/ 0, &V2); igraph_matrix_complex_real(&V2, &V3); VECTOR(selcol)[0]=2; igraph_matrix_select_cols(&V3, &V, &selcol); #define SEMI() \ do { \ igraph_scg_semiprojectors(&groups, IGRAPH_SCG_STOCHASTIC, &L, &R, \ &Lsparse, &Rsparse, &p, \ IGRAPH_SCG_NORM_ROW); \ } while(0) #define PRINTRES() \ do { \ printf("----------------------\n"); \ igraph_matrix_print(&L); \ printf("---\n"); \ igraph_matrix_print(&R); \ printf("---\n"); \ } while (0) /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_STOCHASTIC, IGRAPH_SCG_OPTIMUM, &p, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_STOCHASTIC, IGRAPH_SCG_INTERV_KM, &p, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_STOCHASTIC, IGRAPH_SCG_INTERV, &p, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0, /*intervals_vector=*/ 0, IGRAPH_SCG_STOCHASTIC, IGRAPH_SCG_EXACT, &p, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_vector_destroy(&p); igraph_vector_destroy(&selcol); igraph_vector_destroy(&groups); igraph_matrix_destroy(&L); igraph_matrix_destroy(&R); igraph_matrix_destroy(&V); igraph_matrix_destroy(&V3); igraph_matrix_complex_destroy(&V2); igraph_sparsemat_destroy(&stochasticT); igraph_sparsemat_destroy(&stochastic); igraph_destroy(&g); #ifdef __APPLE__ return 0; #else return 77; #endif }
int main() { igraph_t graph; igraph_vector_t walk, weights; igraph_integer_t ec, i; igraph_rng_seed(igraph_rng_default(), 137); igraph_vector_init(&walk, 0); igraph_vector_init(&weights, 0); /* create a small graph, and a compatible weight vector */ igraph_de_bruijn(&graph, 3, 2); /* 9 vertices, 27 edges, average degree: 6 */ ec = igraph_ecount(&graph); igraph_vector_resize(&weights, ec); for (i=0; i < ec; ++i) VECTOR(weights)[i] = igraph_rng_get_unif01(igraph_rng_default()); BENCH(" 1 Random edge walk, directed, unweighted, small graph ", igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 2 Random edge walk, directed, weighted, small graph ", igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 3 Random vertex walk, directed, unweighted, small graph ", igraph_random_walk(&graph, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_EACH, NULL); BENCH(" 4 Random edge walk, undirected, unweighted, small graph ", igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 5 Random edge walk, undirected, weighted, small graph ", igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 6 Random vertex walk, undirected, unweighted, small graph ", igraph_random_walk(&graph, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); igraph_destroy(&graph); /* create a big graph, and a compatible weight vector */ igraph_de_bruijn(&graph, 8, 5); /* 32768 vertices, 262144 edges, average degree: 16 */ ec = igraph_ecount(&graph); igraph_vector_resize(&weights, ec); for (i=0; i < ec; ++i) VECTOR(weights)[i] = igraph_rng_get_unif01(igraph_rng_default()); BENCH(" 7 Random edge walk, directed, unweighted, large graph ", igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 8 Random edge walk, directed, weighted, large graph ", igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH(" 9 Random vertex walk, directed, unweighted, large graph ", igraph_random_walk(&graph, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_EACH, NULL); BENCH("10 Random edge walk, undirected, unweighted, large graph ", igraph_random_edge_walk(&graph, NULL, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH("11 Random edge walk, undirected, weighted, large graph ", igraph_random_edge_walk(&graph, &weights, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); BENCH("12 Random vertex walk, undirected, unweighted, large graph ", igraph_random_walk(&graph, &walk, 0, IGRAPH_OUT, 50000000, IGRAPH_RANDOM_WALK_STUCK_RETURN) ); igraph_destroy(&graph); igraph_vector_destroy(&weights); igraph_vector_destroy(&walk); return 0; }
int main() { igraph_t g; igraph_matrix_t L, R; igraph_sparsemat_t Lsparse, Rsparse; igraph_matrix_t adj, V; igraph_vector_t groups; igraph_eigen_which_t which; igraph_matrix_init(&L, 0, 0); igraph_matrix_init(&R, 0, 0); igraph_matrix_init(&adj, 0, 0); igraph_matrix_init(&V, 0, 0); igraph_vector_init(&groups, 0); igraph_rng_seed(igraph_rng_default(), 42); igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED); igraph_get_adjacency(&g, &adj, IGRAPH_GET_ADJACENCY_BOTH, /*eids=*/ 0); which.pos=IGRAPH_EIGEN_LM; which.howmany=1; igraph_eigen_matrix_symmetric(&adj, /*sparsemat=*/ 0, /*fun=*/ 0, igraph_vcount(&g), /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, /*values=*/ 0, &V); #define SEMI() \ do { \ igraph_scg_semiprojectors(&groups, IGRAPH_SCG_SYMMETRIC, &L, &R, \ &Lsparse, &Rsparse, /*p=*/ 0, \ IGRAPH_SCG_NORM_ROW); \ } while(0) #define PRINTRES() \ do { \ printf("----------------------\n"); \ igraph_matrix_print(&L); \ printf("---\n"); \ igraph_matrix_print(&R); \ printf("---\n"); \ } while (0) /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_OPTIMUM, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_INTERV_KM, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_INTERV, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_EXACT, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_vector_destroy(&groups); igraph_matrix_destroy(&V); igraph_matrix_destroy(&adj); igraph_destroy(&g); return 0; }
int main() { igraph_matrix_t A; igraph_matrix_t values, vectors; igraph_arpack_options_t options; cb2_data_t data = { &A }; int i, j; igraph_rng_seed(igraph_rng_default(), 42 * 42); igraph_matrix_init(&A, DIM, DIM); for (i=0; i<DIM; i++) { for (j=0; j<DIM; j++) { MATRIX(A, i, j) = igraph_rng_get_integer(igraph_rng_default(), -10, 10); } } igraph_arpack_options_init(&options); options.n=DIM; options.start=0; options.nev=4; options.ncv=9; options.which[0]='L' ; options.which[1]='M'; igraph_matrix_init(&values, 0, 0); igraph_matrix_init(&vectors, options.n, 1); igraph_arpack_rnsolve(cb2, /*extra=*/ &data, &options, /*storage=*/ 0, &values, &vectors); if (MATRIX(values, 2, 1) > 0) { MATRIX(values, 2, 1) = -MATRIX(values, 2, 1); MATRIX(values, 3, 1) = -MATRIX(values, 3, 1); } igraph_matrix_print(&values); printf("---\n"); igraph_matrix_print(&vectors); printf("---\n"); /* -------------- */ options.nev=3; options.which[0]='L' ; options.which[1]='M'; igraph_arpack_rnsolve(cb2, /*extra=*/ &data, &options, /*storage=*/ 0, &values, &vectors); if (MATRIX(values, 2, 1) > 0) { MATRIX(values, 2, 1) = -MATRIX(values, 2, 1); } igraph_matrix_print(&values); printf("---\n"); igraph_matrix_print(&vectors); printf("---\n"); /* -------------- */ options.nev=3; options.which[0]='S' ; options.which[1]='R'; igraph_arpack_rnsolve(cb2, /*extra=*/ &data, &options, /*storage=*/ 0, &values, &vectors); igraph_matrix_print(&values); printf("---\n"); igraph_matrix_print(&vectors); printf("---\n"); /* -------------- */ options.nev=3; options.which[0]='L' ; options.which[1]='I'; igraph_arpack_rnsolve(cb2, /*extra=*/ &data, &options, /*storage=*/ 0, &values, &vectors); igraph_matrix_print(&values); printf("---\n"); igraph_matrix_print(&vectors); printf("---\n"); /* -------------- */ igraph_matrix_destroy(&values); igraph_matrix_destroy(&vectors); igraph_matrix_destroy(&A); return 0; }
int main() { int nodes=10; igraph_t g; igraph_matrix_t L, R; igraph_sparsemat_t Lsparse, Rsparse; igraph_matrix_t V; igraph_matrix_complex_t V2; igraph_sparsemat_t laplacian; igraph_vector_t groups; igraph_eigen_which_t which; igraph_matrix_init(&L, 0, 0); igraph_matrix_init(&R, 0, 0); igraph_matrix_init(&V, 0, 0); igraph_matrix_complex_init(&V2, 0, 0); igraph_vector_init(&groups, 0); igraph_rng_seed(igraph_rng_default(), 42); igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED); igraph_sparsemat_init(&laplacian, nodes, nodes, igraph_ecount(&g)*2); igraph_rng_seed(igraph_rng_default(), 42); igraph_laplacian(&g, /*res=*/ 0, /*sparseres=*/ &laplacian, /*normalized=*/ 0, /*weights=*/ 0); which.pos=IGRAPH_EIGEN_LM; which.howmany=1; igraph_eigen_matrix(/*matrix=*/ 0, &laplacian, /*fun=*/ 0, 10, /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, /*values=*/ 0, &V2); igraph_matrix_complex_real(&V2, &V); #define SEMI() \ do { \ igraph_scg_semiprojectors(&groups, IGRAPH_SCG_LAPLACIAN, &L, &R, \ &Lsparse, &Rsparse, /*p=*/ 0, \ IGRAPH_SCG_NORM_ROW); \ } while(0) #define PRINTRES() \ do { \ printf("----------------------\n"); \ igraph_matrix_print(&L); \ printf("---\n"); \ igraph_matrix_print(&R); \ printf("---\n"); \ igraph_sparsemat_destroy(&Lsparse); \ igraph_sparsemat_destroy(&Rsparse); \ } while (0) /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_OPTIMUM, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_INTERV_KM, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_INTERV, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_EXACT, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_matrix_destroy(&L); igraph_matrix_destroy(&R); igraph_matrix_destroy(&V); igraph_matrix_complex_destroy(&V2); igraph_vector_destroy(&groups); igraph_sparsemat_destroy(&laplacian); igraph_destroy(&g); return 0; }
int main() { const int nodes=10; igraph_t g; igraph_matrix_t V; igraph_matrix_complex_t V2; igraph_sparsemat_t laplacian; igraph_vector_t groups; igraph_eigen_which_t which; igraph_tree(&g, nodes, /* children= */ 3, IGRAPH_TREE_UNDIRECTED); igraph_sparsemat_init(&laplacian, nodes, nodes, igraph_ecount(&g)*2); igraph_matrix_complex_init(&V2, 0, 0); igraph_matrix_init(&V, 0, 0); igraph_vector_init(&groups, 0); igraph_rng_seed(igraph_rng_default(), 42); igraph_sparsemat_init(&laplacian, 0, 0, 0); igraph_laplacian(&g, /*res=*/ 0, /*sparseres=*/ &laplacian, /*normalized=*/ 0, /*weights=*/ 0); which.pos=IGRAPH_EIGEN_LR; which.howmany=1; igraph_eigen_matrix(/*matrix=*/ 0, &laplacian, /*fun=*/ 0, nodes, /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, /*values=*/ 0, &V2); igraph_matrix_complex_real(&V2, &V); /* ------------ */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_OPTIMUM, /*p=*/ 0, /*maxiter=*/ 10000); igraph_vector_print(&groups); /* ------------ */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_INTERV_KM, /*p=*/ 0, /*maxiter=*/ 10000); igraph_vector_print(&groups); /* ------------ */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_INTERV, /*p=*/ 0, /*maxiter=*/ 10000); igraph_vector_print(&groups); /* ------------ */ igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_EXACT, /*p=*/ 0, /*maxiter=*/ 10000); igraph_vector_print(&groups); /* ------------ */ igraph_vector_destroy(&groups); igraph_matrix_destroy(&V); igraph_matrix_complex_destroy(&V2); igraph_sparsemat_destroy(&laplacian); igraph_destroy(&g); #ifdef __APPLE__ return 0; #else return 77; #endif }
int main() { igraph_t g, g2, cli; igraph_vector_t perm; igraph_vector_ptr_t cliques; igraph_integer_t no; int i; igraph_rng_seed(igraph_rng_default(), 42); /* Create a graph that has a random component, plus a number of relatively small cliques */ igraph_vector_init_seq(&perm, 0, NODES-1); igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNM, NODES, NODES, /*directed=*/ 0, /*loops=*/ 0, igraph_rng_default()); igraph_full(&cli, CLIQUE_SIZE, /*directed=*/ 0, /*loops=*/ 0); for (i=0; i<NO_CLIQUES; i++) { /* Permute vertices of g */ permutation(&perm); igraph_permute_vertices(&g, &g2, &perm); igraph_destroy(&g); g=g2; /* Add a clique */ igraph_union(&g2, &g, &cli, /*edge_map1=*/ 0, /*edge_map2=*/ 0); igraph_destroy(&g); g=g2; } igraph_simplify(&g, /*multiple=*/ 1, /*loop=*/ 0, /*edge_comb=*/ 0); igraph_vector_destroy(&perm); igraph_destroy(&cli); /* Find the maximal cliques */ igraph_vector_ptr_init(&cliques, 0); igraph_maximal_cliques(&g, &cliques, /*min_size=*/ 3, /*max_size=*/ 0 /*no limit*/); igraph_maximal_cliques_count(&g, &no, /*min_size=*/ 3, /*max_size=*/ 0 /*no limit*/); if (no != igraph_vector_ptr_size(&cliques)) { return 1; } /* Print and destroy them */ print_and_destroy_cliques(&cliques); /* Clean up */ igraph_vector_ptr_destroy(&cliques); igraph_destroy(&g); /* Build a triangle with a loop (thanks to Emmanuel Navarro) */ igraph_small(&g, 3, IGRAPH_UNDIRECTED, 0, 1, 1, 2, 2, 0, 0, 0, -1); /* Find the maximal cliques */ igraph_vector_ptr_init(&cliques, 0); igraph_maximal_cliques(&g, &cliques, /*min_size=*/ 3, /*max_size=*/ 0 /*no limit*/); igraph_maximal_cliques_count(&g, &no, /*min_size=*/ 3, /*max_size=*/ 0 /*no limit*/); if (no != igraph_vector_ptr_size(&cliques)) { return 2; } /* Print and destroy them */ print_and_destroy_cliques(&cliques); /* Clean up */ igraph_vector_ptr_destroy(&cliques); igraph_destroy(&g); return 0; }
int main(int argc,char*argv[]) { if(argc!=2) { cout<<"USAGE:SIS datafile "<<endl; exit(1); } //define the entry string networkname(argv[1]); //network name double a=1; double alpha=0.4; double beta=0.1; int game_rounds=500; //MC rounds int aver_rounds=10; //do average double delta=1;//delta string output_name_pre; //the network name for(int i=0;i!=networkname.size()-4;i++) { output_name_pre.push_back(networkname[i]); } //random generator //to initialize the random seed igraph_rng_init(mrng,&igraph_rngtype_mt19937); unsigned long idum; // random number seed idum=(unsigned long)time(NULL); igraph_rng_seed(mrng,idum); //construct the graph igraph_t network; gen_graph(&network,networkname); int nwsize=igraph_vcount(&network); //the size of the network //to generate the name of the output file //output file name string resname("res.txt"); string totalname=output_name_pre+resname; FILE *outfile; outfile = fopen(totalname.c_str(), "w"); //construct the population for(a=-6;a<6.1;a+=0.1 ) { population popu(output_name_pre,&network,delta,0.01,a,2,3*nwsize,3*nwsize); double res=0; popu.population_dynamics(&res,alpha,beta,1000); fprintf(outfile,"%f\t%f\n",a,res/ nwsize); fflush(outfile); } fclose(outfile); /* igraph_t network; int nwsize = igraph_vcount(&network); //the size of the network igraph_watts_strogatz_game(&network, 1, 500, 4, 0.1, false, false); string output_name_pre("small_world"); population test(output_name_pre,&network, 1, 0.01, 1, 0, 3 * nwsize, 3 * nwsize); test.initial_state(); double res = 0; test.population_dynamics(&res,0.3,0.1,300); */ //for (a=-5;a!=5;a+=0.2) // //{ // population test(&network,delta,0.01,a,type,3*nwsize,3*nwsize); // vector<double> average_rres; // for(int i=0;i!=aver_rounds;++i) // { // double rres=0; // test.initial_state(); // test.population_dynamics(&rres,0.4,0.1,game_rounds); //// printf("%f\n", rres); // average_rres.push_back(rres); // } // fprintf(outfile,"%f\t%f\n",a,double (accumulate(average_rres.begin(),\ // average_rres.end(),0)/average_rres.size())); // fflush(outfile); //} //fclose(outfile); igraph_destroy(&network); return 0; }
int main() { igraph_t g; igraph_vector_t weights; igraph_rng_seed(igraph_rng_default(), 42); /* Two triangles connected by one edge */ printf("# Two triangles connected by one edge\n"); igraph_small(&g, 0, IGRAPH_UNDIRECTED, 0, 1, 1, 2, 2, 0, 3, 4, 4, 5, 5, 3, 0, 5, -1); infomap_test(&g); igraph_destroy(&g); //return 0; /* Two 4-cliques with one commun vertex (vertex 3) */ printf("# Two 4-cliques (0123 and 4567) connected by two edges (0-4 and 1-5)\n"); igraph_small(&g, 0, IGRAPH_UNDIRECTED, 0, 1, 0, 2, 0, 3, 1, 2, 1, 3, 2, 3, // 4-clique 0,1,2,3 7, 4, 7, 5, 7, 6, 4, 5, 4, 6, 5, 6, // 4-clique 4,5,6,7 0, 4, 1, 5, //8, 0, 8, 4, -1); infomap_test(&g); printf("# Two 4-cliques (0123 and 4567) connected by two edges (0-4 and 1-5)\n"); igraph_add_edge(&g, 0, 4); igraph_add_edge(&g, 1, 5); infomap_test(&g); igraph_destroy(&g); /* Zachary Karate club -- this is just a quick smoke test */ printf("# Zachary Karate club\n"); igraph_small(&g, 0, IGRAPH_UNDIRECTED, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, //0, 5, 0, 5, 0, 5, 0, 6, 0, 7, 0, 8, 0, 10, 0, 11, 0, 12, 0, 13, 0, 17, 0, 19, 0, 21, 0, 31, 1, 2, 1, 3, 1, 7, 1, 13, 1, 17, 1, 19, 1, 21, 1, 30, 2, 3, 2, 7, 2, 8, 2, 9, 2, 13, 2, 27, 2, 28, 2, 32, 3, 7, 3, 12, 3, 13, 4, 6, 4, 10, 5, 6, 5, 10, 5, 16, 6, 16, 8, 30, 8, 32, 8, 33, 9, 33, 13, 33, 14, 32, 14, 33, 15, 32, 15, 33, 18, 32, 18, 33, 19, 33, 20, 32, 20, 33, 22, 32, 22, 33, 23, 25, 23, 27, 23, 29, 23, 32, 23, 33, 24, 25, 24, 27, 24, 31, 25, 31, 26, 29, 26, 33, 27, 33, 28, 31, 28, 33, 29, 32, 29, 33, 30, 32, 30, 33, 31, 32, 31, 33, 32, 33, -1); infomap_test(&g); igraph_destroy(&g); /* Flow.net that come in infomap_dir.tgz */ printf("# Flow (from infomap_dir.tgz)\n"); igraph_small(&g, 0, IGRAPH_DIRECTED, 0, 1, 1, 2, 2, 3, 3, 0, 1, 4, 4, 5, 5, 6, 6, 7, 7, 4, 5, 8, 8, 9, 9, 10, 10, 11, 11, 8, 9, 12, 12, 13, 13, 14, 14, 15, 15, 12, 13, 0, -1); infomap_test(&g); igraph_destroy(&g); /* MultiphysChemBioEco40W_weighted_dir.net */ printf("# MultiphysChemBioEco40W_weighted_dir.net (from infomap_dir.tgz)\n"); igraph_small(&g, 0, IGRAPH_DIRECTED, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 16, 0, 18, 0, 0, 1, 2, 1, 3, 1, 5, 1, 6, 1, 7, 1, 9, 1, 10, 1, 16, 1, 18, 1, 0, 2, 3, 2, 4, 2, 5, 2, 6, 2, 7, 2, 0, 3, 1, 3, 2, 3, 4, 3, 5, 3, 6, 3, 7, 3, 8, 3, 9, 3, 10, 3, 11, 3, 13, 3, 14, 3, 16, 3, 17, 3, 18, 3, 19, 3, 26, 3, 30, 3, 1, 4, 3, 4, 5, 4, 6, 4, 13, 4, 18, 4, 0, 5, 1, 5, 2, 5, 3, 5, 6, 5, 7, 5, 9, 5, 1, 6, 3, 6, 7, 6, 9, 6, 16, 6, 0, 7, 1, 7, 2, 7, 3, 7, 5, 7, 6, 7, 9, 7, 3, 8, 5, 8, 3, 9, 7, 9, 12, 10, 13, 10, 14, 10, 15, 10, 16, 10, 17, 10, 18, 10, 19, 10, 21, 10, 3, 11, 18, 11, 10, 12, 14, 12, 16, 12, 17, 12, 18, 12, 3, 13, 10, 13, 14, 13, 16, 13, 10, 14, 12, 14, 13, 14, 15, 14, 16, 14, 17, 14, 18, 14, 10, 15, 14, 15, 18, 15, 0, 16, 2, 16, 3, 16, 6, 16, 10, 16, 12, 16, 13, 16, 14, 16, 17, 16, 18, 16, 10, 17, 12, 17, 14, 17, 18, 17, 3, 18, 10, 18, 12, 18, 14, 18, 15, 18, 16, 18, 17, 18, 19, 18, 21, 18, 11, 19, 16, 19, 17, 19, 16, 20, 18, 20, 21, 20, 22, 20, 23, 20, 24, 20, 25, 20, 26, 20, 27, 20, 28, 20, 29, 20, 3, 21, 14, 21, 18, 21, 20, 21, 22, 21, 23, 21, 24, 21, 25, 21, 26, 21, 27, 21, 28, 21, 29, 21, 35, 21, 36, 21, 38, 21, 18, 22, 20, 22, 21, 22, 23, 22, 24, 22, 25, 22, 26, 22, 27, 22, 29, 22, 3, 23, 20, 23, 21, 23, 22, 23, 24, 23, 25, 23, 26, 23, 27, 23, 28, 23, 29, 23, 35, 23, 38, 23, 39, 23, 20, 24, 21, 24, 23, 24, 25, 24, 26, 24, 27, 24, 28, 24, 29, 24, 9, 25, 20, 25, 21, 25, 22, 25, 23, 25, 24, 25, 26, 25, 27, 25, 28, 25, 29, 25, 18, 26, 20, 26, 21, 26, 22, 26, 23, 26, 25, 26, 27, 26, 28, 26, 29, 26, 30, 26, 32, 26, 35, 26, 36, 26, 38, 26, 39, 26, 3, 27, 14, 27, 20, 27, 21, 27, 22, 27, 23, 27, 24, 27, 25, 27, 26, 27, 28, 27, 29, 27, 38, 27, 3, 28, 18, 28, 20, 28, 21, 28, 23, 28, 24, 28, 25, 28, 26, 28, 27, 28, 29, 28, 35, 28, 14, 29, 16, 29, 18, 29, 20, 29, 21, 29, 22, 29, 23, 29, 24, 29, 25, 29, 26, 29, 27, 29, 28, 29, 31, 30, 32, 30, 33, 30, 34, 30, 35, 30, 36, 30, 38, 30, 39, 30, 30, 31, 32, 31, 34, 31, 36, 31, 30, 32, 34, 32, 35, 32, 36, 32, 30, 33, 32, 33, 34, 33, 35, 33, 36, 33, 38, 33, 30, 34, 31, 34, 32, 34, 33, 34, 35, 34, 36, 34, 38, 34, 39, 34, 26, 35, 30, 35, 32, 35, 33, 35, 34, 35, 36, 35, 38, 35, 39, 35, 30, 36, 34, 36, 35, 36, 38, 36, 39, 36, 34, 37, 26, 38, 30, 38, 32, 38, 33, 38, 34, 38, 35, 38, 36, 38, 39, 38, 26, 39, 30, 39, 33, 39, 34, 39, 35, 39, 36, 39, 38, 39, -1); igraph_vector_init_real(&weights, 306, 5.0, 3.0, 130.0, 4.0, 15.0, 9.0, 7.0, 1.0, 1.0, 3.0, 1.0, 1.0, 1.0, 34.0, 38.0, 2.0, 23.0, 1.0, 1.0, 3.0, 2.0, 2.0, 16.0, 1.0, 3.0, 1.0, 3.0, 63.0, 92.0, 72.0, 25.0, 447.0, 121.0, 65.0, 4.0, 16.0, 35.0, 1.0, 19.0, 1.0, 78.0, 1.0, 45.0, 1.0, 3.0, 1.0, 1.0, 25.0, 1.0, 3.0, 1.0, 1.0, 3.0, 36.0, 19.0, 136.0, 41.0, 96.0, 1.0, 7.0, 26.0, 1.0, 2.0, 2.0, 3.0, 2.0, 2.0, 23.0, 52.0, 4.0, 1.0, 2.0, 1.0, 3.0, 1.0, 11.0, 2.0, 17.0, 1.0, 5.0, 18.0, 86.0, 5.0, 1.0, 1.0, 1.0, 6.0, 1.0, 2.0, 2.0, 20.0, 4.0, 5.0, 1.0, 5.0, 12.0, 4.0, 1.0, 1.0, 4.0, 9.0, 40.0, 2.0, 1.0, 4.0, 1.0, 1.0, 48.0, 2.0, 18.0, 1.0, 7.0, 2.0, 2.0, 53.0, 25.0, 9.0, 1.0, 23.0, 8.0, 62.0, 29.0, 35.0, 4.0, 34.0, 35.0, 3.0, 1.0, 24.0, 1.0, 6.0, 2.0, 2.0, 22.0, 7.0, 2.0, 5.0, 14.0, 3.0, 28.0, 14.0, 20.0, 3.0, 1.0, 5.0, 77.0, 20.0, 25.0, 35.0, 55.0, 35.0, 115.0, 68.0, 105.0, 2.0, 2.0, 2.0, 4.0, 2.0, 17.0, 12.0, 3.0, 3.0, 11.0, 10.0, 7.0, 2.0, 12.0, 31.0, 11.0, 5.0, 11.0, 65.0, 39.0, 17.0, 26.0, 3.0, 4.0, 2.0, 3.0, 6.0, 4.0, 8.0, 1.0, 7.0, 7.0, 6.0, 1.0, 39.0, 42.0, 9.0, 6.0, 9.0, 5.0, 45.0, 43.0, 26.0, 1.0, 2.0, 6.0, 2.0, 15.0, 3.0, 9.0, 2.0, 1.0, 1.0, 1.0, 4.0, 2.0, 9.0, 2.0, 1.0, 2.0, 28.0, 80.0, 10.0, 18.0, 13.0, 17.0, 28.0, 40.0, 76.0, 1.0, 2.0, 1.0, 11.0, 37.0, 5.0, 11.0, 14.0, 4.0, 14.0, 10.0, 1.0, 1.0, 1.0, 1.0, 41.0, 121.0, 6.0, 21.0, 12.0, 30.0, 6.0, 141.0, 43.0, 2.0, 12.0, 6.0, 35.0, 10.0, 7.0, 2.0, 12.0, 6.0, 2.0, 11.0, 1.0, 7.0, 6.0, 5.0, 3.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 67.0, 9.0, 9.0, 11.0, 10.0, 21.0, 7.0, 12.0, 9.0, 16.0, 7.0, 4.0, 11.0, 17.0, 37.0, 32.0, 9.0, 2.0, 2.0, 5.0, 4.0, 2.0, 7.0, 3.0, 3.0, 5.0, 8.0, 14.0, 3.0, 38.0, 3.0, 9.0, 2.0, 8.0, 21.0, 18.0, 58.0); infomap_weighted_test(&g, &weights); igraph_vector_destroy(&weights); igraph_destroy(&g); /* Two triangles connected by one edge */ printf("# Wiktionary english verbs (synonymy 2008)\n"); FILE *wikt = fopen("wikti_en_V_syn.elist", "r"); igraph_read_graph_edgelist(&g, wikt, 0, 0); fclose(wikt); gsumary(&g); infomap_test(&g); igraph_destroy(&g); #ifdef __APPLE__ return 0; #else return 77; #endif }
int main(int argc, char* argv[]) { if (argc != 9) { cout << "Usage: ./release/fixating <Update Rule: \"Bd\", \"dB\"> <integer: population size> <\"directed\" or \"undirected\"> <double: fitness of mutant> <category of graph: \"complete\", \"ER\", \"BB\", \"WS\", \"geo\", or \"custom\" > <secondary parameter for the category of graph: \"GNM\" or \"GNP\" for Erdos Reny, double power of preference for Barabasi, int dimension for small world, bool periodic for geometric, , adjacency matrix for custom> <tertiary parameter for the category of graph: probability for every edge in Erdos-Reny GNP and geometric, number of edges for Erdos-Reny GNM, m for barabasi, probability of rewiring for small world, 0 for custom> <output: \"probability\", \"conditional\", \"unconditional\", or \"all\">" << endl; return -1; } // ---------- If you want to stop time, uncomment all comments with //CLOCK// //CLOCK// std::clock_t start; //CLOCK// double bt = 0; //CLOCK// double st = 0; //counting variable for the graph generators int counts = 0; const unsigned int popSize = atoi(argv[2]); if (popSize > 23) { cout << "Code only possible for population size up to 23... aborting..." << endl; return -1; } const unsigned int numStates = 1 << popSize; string update = argv[1]; if (update != "dB" && update != "Bd") { cout << "Only \"Bd\" or \"dB\" possible for update rule!... aborting..." << endl; return -1; } float fitnessMutants = atof(argv[4]); string direction = argv[3]; string category = argv[5]; igraph_t graph; int admat[popSize * popSize]; string output = argv[8]; if (output != "probability" && output != "conditional" && output != "unconditional" && output != "all") { cout << "Only \"probability\", \"unconditional\", \"conditional\" or \"all\" possible for output!... aborting..." << endl; return -1; } // ---------- Code snippet for fully connected graph ---------- if (category == "complete") { if (direction == "undirected") { igraph_full(&graph, popSize, false, false); } else if (direction == "directed") { igraph_full(&graph, popSize, true, false); } else { cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl; return -1; } } // ---------- Code snippet for random graph ---------- else if (category == "ER") { string gn = argv[6]; igraph_rng_seed(igraph_rng_default(), std::clock()); igraph_bool_t isConnected = 0; if (direction == "directed") { while ((isConnected == 0) & (counts < maxcount)) { if (gn == "GNP") { double edgeprob = atof(argv[7]); if ((edgeprob > 1) || (edgeprob < 0)) { cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl; return -1; } igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, popSize, edgeprob, true, false); } else if (gn == "GNM") { int edgenumber = atoi(argv[7]); if ((edgenumber < 1) || (edgenumber > popSize*(popSize-1))) { cout << "number of edges must be greater than 1 and smaller than N*(N-1) ...aborting..." << endl; return -1; } igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNM, popSize, edgenumber, true, false); } else { cout << "Only \"GNM\" and \"GNP\" possible ... aborting..." << endl; } igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } if (counts == maxcount) { cout << "Probability or number of edges too low... Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl; return -1; } } else if (direction == "undirected") { int counts = 0; while ((isConnected == 0) & (counts < maxcount)) { if (gn == "GNP") { double edgeprob = atof(argv[7]); if ((edgeprob > 1) || (edgeprob < 0)) { cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl; return -1; } igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, popSize, edgeprob, false, false); } else if (gn == "GNM") { int edgenumber = atoi(argv[7]); if ((edgenumber < 1) || (edgenumber > popSize*(popSize-1)/2)) { cout << "number of edges must be greater than 1 and smaller than N*(N-1)/2 ...aborting..." << endl; return -1; } igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNM, popSize, edgenumber, false, false); } else { cout << "Only \"GNM\" and \"GNP\" possible ... aborting..." << endl; } igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } if (counts == maxcount) { cout << "Probability or number of edges too low... Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl; return -1; } } else { cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl; return -1; } } //---------------------------- Code snippet for small world network --------------------------------// else if (category == "WS") { igraph_rng_seed(igraph_rng_default(), std::clock()); igraph_bool_t isConnected = 0; double edgeprob = atof(argv[7]); if ((edgeprob > 1) || (edgeprob < 0)) { cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl; return -1; } int dim = atoi(argv[6]); int latSize = pow(popSize,1/double(dim)); if (direction == "directed") { while ((isConnected == 0) & (counts < maxcount)) { igraph_watts_strogatz_game(&graph, dim, latSize, 1, edgeprob, 0, 0); igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } } else if (direction == "undirected") { while ((isConnected == 0) & (counts < maxcount)) { igraph_watts_strogatz_game(&graph, dim, latSize, 1, edgeprob, 0, 0); igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } } else { cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl; return -1; } if (counts == maxcount) { cout << "Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl; return -1; } } //---------------------------- Code snippet for geometric generator --------------------------------// else if(category == "geo") { igraph_rng_seed(igraph_rng_default(), std::clock()); igraph_bool_t isConnected = 0; double edgeprob = atof(argv[7]); if ((edgeprob > 1) || (edgeprob < 0)) { cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl; return -1; } bool torus = (atoi(argv[6]) == 1); double radius = sqrt(edgeprob/3.14); if (direction == "directed") { while ((isConnected == 0) & (counts < maxcount)) { igraph_grg_game(&graph, popSize, radius, torus, 0, 0); igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } } else if (direction == "undirected") { while ((isConnected == 0) & (counts < maxcount)) { igraph_grg_game(&graph, popSize, radius, torus, 0, 0); igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } } else { cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl; return -1; } if (counts == maxcount) { cout << "Probability or number of edges too low... Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl; return -1; } } //---------------------------- Code snippet for barabasi generator --------------------------------// else if(category == "BB") { double power = atof(argv[6]); int m = atoi(argv[7]); igraph_rng_seed(igraph_rng_default(), std::clock()); igraph_bool_t isConnected = 0; if (direction == "directed") { cout << "directed Barabasi-Albert never creates connected graphs, use undirected instead! aborting..." << endl; return -1; } else if (direction == "undirected") { while ((isConnected == 0) & (counts < maxcount)) { igraph_barabasi_game(&graph, popSize, power, m, 0, 0, 1.0, false, IGRAPH_BARABASI_PSUMTREE, 0); igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } } else { cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl; return -1; } if (counts == maxcount) { cout << "Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl; return -1; } } // ---------- Code snippet for custom graph ---------- else if(category == "custom") { std::string admats = argv[6]; if (admats.size() != popSize*popSize) { cout << "adjacency matrix has the wrong size... aborting..." << endl; return -1; } std::vector<int> ints; std::transform(std::begin(admats), std::end(admats), std::back_inserter(ints), [](char c) { return c - '0'; } ); std::copy(ints.begin(), ints.end(), admat); } else { cout << "Only \"complete\", \"ER\", \"BB\", \"WS\", or \"geo\" as categories... aborting..." << endl; return -1; } // ---------- Here the adjacency matrix gets copied into an array ---------- if(category!="custom") { igraph_matrix_t admatv; igraph_matrix_init(&admatv, 0,0); igraph_get_adjacency( &graph, &admatv,IGRAPH_GET_ADJACENCY_BOTH,false); for(unsigned int i = 0 ; i < popSize ; i++) { for(unsigned int k = 0 ; k < popSize ; k++) { admat[ i*popSize + k] = MATRIX(admatv,i,k ); } } igraph_destroy(&graph); igraph_matrix_destroy(&admatv); } for (unsigned int i=0; i<popSize; i++) { for (unsigned int j=0; j<popSize; j++) { // If you want to print the adjacency matrix: cout<<admat[i * popSize + j]<<" "; } } cout<<endl; t_vectorFP data; t_vectorInt row; t_vectorInt col; data.reserve(popSize * numStates); row.reserve(popSize * numStates); col.reserve(popSize * numStates); //CLOCK// start = std::clock(); createTransitionMatrix(popSize, numStates, fitnessMutants, update, admat, data, row, col); std::vector<T> tripletList; tripletList.reserve(popSize * numStates); for( unsigned int j = 0 ; j < data.size() ; j++) { tripletList.push_back(T(col.at(j),row.at(j),data.at(j))); } SpMat mat(numStates,numStates); mat.setFromTriplets(tripletList.begin(), tripletList.end()); // Stopping time after creating transition matrix //CLOCK// bt = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; //for (int i = 0; i<data.size(); i++) // cout<<"unconditional: transition prob from state "<<row[i]<<" to state "<<col[i]<<" is "<<data[i]<<endl; string s1; /* ---------- No distinguishing between "probability", "unconditional" time, and "conditional" time ---------- */ float * fixProbAllStates = static_cast<float*> (malloc(numStates * sizeof(float))); fixProb(mat, popSize, numStates, fixProbAllStates); // Stopping time after solving fixation probabilities //CLOCK// st = ( std::clock() - start) / (double) CLOCKS_PER_SEC - bt; float probOne = 0.0; for(unsigned int i = 0; i < popSize; i++) { int j = 1 << i; probOne = probOne + fixProbAllStates[j]; } probOne = probOne / (float)(popSize); cout << "fixation probability:" << probOne << endl; /* ---------- Printing the fixation probability starting from all states ---------- */ /* for(unsigned int i = 0; i < numStates; i++) { bitset<23> b1(i); s1 = b1.to_string(); cout<<"fixation probability in state "; cout<< s1.substr(23-popSize,popSize); cout <<" is "<<fixProbAllStates[i]<<endl; } */ if((output == "unconditional")||(output == "all")) { float * uncondFixTimeAllStates = static_cast<float*> (malloc(numStates * sizeof(float))); // Stopping the time for solving for unconditional fixation time //CLOCK// start = std::clock(); //CLOCK// bt = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; time(mat, popSize, numStates, uncondFixTimeAllStates); //CLOCK// float avUncondTime = 0.0; for(unsigned int i = 0 ; i < popSize ; i++) { int j = 1 << i; avUncondTime = avUncondTime + uncondFixTimeAllStates[j]; } avUncondTime = avUncondTime / (float)(popSize); free(uncondFixTimeAllStates); cout<< "unconditional fixation time:" << avUncondTime << endl; } /* ---------- Printing the average unconditional fixation time starting from all states ---------- */ //for(unsigned int i = 0; i < numStates; i++) //{ // bitset<23> b1(i); // s1 = b1.to_string(); //cout<<"Unconditional fixation time in state "; //cout<< s1.substr (23-popSize,popSize); //cout <<" is "<<uncondFixTimeAllStates[i]<<endl; //} //float * fixProbAllStates = (float*) malloc(numStates * sizeof(float)); //fixProb(mat, popSize, numStates, fixProbAllStates); if((output == "conditional")||(output == "all")) { createConditionalTransitionMatrix(popSize, numStates, fixProbAllStates, data, row, col); std::vector<T> tripletListCond; tripletListCond.reserve(popSize * numStates); for( unsigned int j = 0 ; j < data.size() ; j++) { tripletListCond.push_back(T(col.at(j),row.at(j),data.at(j))); } SpMat conditionalMatrix(numStates,numStates); conditionalMatrix.setFromTriplets(tripletListCond.begin(), tripletListCond.end()); float * condFixTimeAllStates = static_cast<float*> (malloc(numStates * sizeof(float))); time(conditionalMatrix, popSize, numStates, condFixTimeAllStates); float avCondTime = 0.0; for(unsigned int i = 0 ; i < popSize ; i++) { int j = 1 << i; avCondTime = avCondTime + condFixTimeAllStates[j]; } avCondTime = avCondTime / (float)(popSize); free(condFixTimeAllStates); cout << "conditional fixation time:" << avCondTime << endl; } free(fixProbAllStates); /* ---------- Printing the average conditional fixation time starting from all states ---------- */ //for(unsigned int i = 0; i < numStates; i++) //{ //bitset<23> b1(i); //s1 = b1.to_string(); //cout<<"Conditional fixation time in state "; //cout<< s1.substr (23-popSize,popSize); //cout <<" is "<<condFixTimeAllStates[i]<<endl; //} st = ( std::clock() - start) / (double) CLOCKS_PER_SEC - bt; //CLOCK// cout<<"building time: "<< bt <<'\n'; //CLOCK// cout<<"solving time: "<< st << "\n\n"; }
int main() { igraph_real_t d; igraph_vector_t u, v; int ret; long int i, k, n; /******************************** * Example usage ********************************/ /* Sequences with one element. Such sequences are trivially permuted. * The result of any Fisher-Yates shuffle on a sequence with one element * must be the original sequence itself. */ n = 1; igraph_vector_init(&v, n); igraph_rng_seed(igraph_rng_default(), time(0)); k = R_INTEGER(-1000, 1000); VECTOR(v)[0] = k; igraph_vector_shuffle(&v); if (VECTOR(v)[0] != k) { return 1; } d = R_UNIF(-1000.0, 1000.0); VECTOR(v)[0] = d; igraph_vector_shuffle(&v); if (VECTOR(v)[0] != d) { return 2; } igraph_vector_destroy(&v); /* Sequences with multiple elements. A Fisher-Yates shuffle of a sequence S * is a random permutation \pi(S) of S. Thus \pi(S) must have the same * length and elements as the original sequence S. A major difference between * S and its random permutation \pi(S) is that the order in which elements * appear in \pi(S) is probably different from how elements are ordered in S. * If S has length n = 1, then both \pi(S) and S are equivalent sequences in * that \pi(S) is merely S and no permutation has taken place. If S has * length n > 1, then there are n! possible permutations of S. Assume that * each such permutation is equally likely to appear as a result of the * Fisher-Yates shuffle. As n increases, the probability that S is different * from \pi(S) also increases. We have a probability of 1 / n! that S and * \pi(S) are equivalent sequences. */ n = 100; igraph_vector_init(&u, n); igraph_vector_init(&v, n); for (i = 0; i < n; i++) { k = R_INTEGER(-1000, 1000); VECTOR(u)[i] = k; VECTOR(v)[i] = k; } igraph_vector_shuffle(&v); /* must have same length */ if (igraph_vector_size(&v) != n) { return 3; } if (igraph_vector_size(&u) != igraph_vector_size(&v)) { return 4; } /* must have same elements */ igraph_vector_sort(&u); igraph_vector_sort(&v); if (!igraph_vector_all_e(&u, &v)) { return 5; } igraph_vector_destroy(&u); igraph_vector_destroy(&v); /* empty sequence */ igraph_vector_init(&v, 0); ret = igraph_vector_shuffle(&v); igraph_vector_destroy(&v); return ret == 0 ? 0 : 6; }