int main() { igraph_vs_t vs; igraph_vit_t vit; igraph_t g; igraph_integer_t size; igraph_ring(&g, 10, IGRAPH_UNDIRECTED, 0, 1); igraph_vs_seq(&vs, 0, 9); igraph_vit_create(&g, vs, &vit); igraph_vs_size(&g, &vs, &size); printf("%li", (long int) size); while (!IGRAPH_VIT_END(vit)) { printf(" %li", (long int)IGRAPH_VIT_GET(vit)); IGRAPH_VIT_NEXT(vit); } printf("\n"); igraph_vit_destroy(&vit); igraph_vs_destroy(&vs); igraph_destroy(&g); return 0; }
int main() { igraph_t g; igraph_vector_t vids, layers, parents; igraph_ring(&g, 10, IGRAPH_UNDIRECTED, 0, 0); igraph_vector_init(&vids, 0); igraph_vector_init(&layers, 0); igraph_vector_init(&parents, 0); igraph_i_bfs(&g, 0, IGRAPH_ALL, &vids, &layers, &parents); vector_print(&vids); vector_print(&layers); vector_print(&parents); igraph_destroy(&g); igraph_tree(&g, 20, 2, IGRAPH_TREE_UNDIRECTED); igraph_i_bfs(&g, 0, IGRAPH_ALL, &vids, &layers, &parents); vector_print(&vids); vector_print(&layers); vector_print(&parents); igraph_destroy(&g); igraph_vector_destroy(&vids); igraph_vector_destroy(&layers); igraph_vector_destroy(&parents); return 0; }
int main() { igraph_t g; igraph_vector_ptr_t vecs; long int i; igraph_vs_t vs; igraph_ring(&g, 10, IGRAPH_DIRECTED, 0, 1); igraph_vector_ptr_init(&vecs, 5); for (i=0; i<igraph_vector_ptr_size(&vecs); i++) { VECTOR(vecs)[i] = calloc(1, sizeof(igraph_vector_t)); igraph_vector_init(VECTOR(vecs)[i], 0); } igraph_vs_vector_small(&vs, 1, 3, 5, 2, 1, -1); igraph_get_shortest_paths(&g, &vecs, 0, vs, IGRAPH_OUT); for (i=0; i<igraph_vector_ptr_size(&vecs); i++) { print_vector(VECTOR(vecs)[i]); igraph_vector_destroy(VECTOR(vecs)[i]); free(VECTOR(vecs)[i]); } igraph_vector_ptr_destroy(&vecs); igraph_vs_destroy(&vs); igraph_destroy(&g); return 0; }
int test_unnormalized_laplacian(igraph_bool_t dir) { igraph_t g; igraph_matrix_t m; igraph_vector_t vec; igraph_matrix_init(&m, 1, 1); /* No loop or multiple edges */ igraph_ring(&g, 5, dir, 0, 1); igraph_laplacian(&g, &m, 0); print_matrix(&m); printf("===\n"); /* Add some loop edges */ igraph_vector_init_real(&vec, 4, 1.0, 1.0, 2.0, 2.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); igraph_laplacian(&g, &m, 0); print_matrix(&m); printf("===\n"); /* Duplicate some edges */ igraph_vector_init_real(&vec, 4, 1.0, 2.0, 3.0, 4.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); igraph_laplacian(&g, &m, 0); print_matrix(&m); igraph_destroy(&g); igraph_matrix_destroy(&m); return 0; }
int check_ring(const ring_test_t *test) { igraph_t graph, othergraph; igraph_vector_t otheredges; igraph_bool_t iso; int ret; /* Create ring */ igraph_ring(&graph, test->n, test->directed, test->mutual, test->circular); /* Check its properties */ if ((ret=check_ring_properties(&graph, test->directed, test->mutual, test->circular))) { return ret;} /* Check that it is isomorphic to the stored graph */ igraph_vector_view(&otheredges, test->edges, test->m * 2); igraph_create(&othergraph, &otheredges, test->n, test->directed); igraph_isomorphic(&graph, &othergraph, &iso); if (!iso) { return 50; } /* Clean up */ igraph_destroy(&graph); igraph_destroy(&othergraph); return 0; }
int main() { igraph_t g; igraph_integer_t result; igraph_integer_t from, to; igraph_vector_t path; igraph_rng_t rng; igraph_rng_init(&rng, &igraph_rngtype_mt19937); igraph_barabasi_game(&g, 30, /*power=*/ 1, 30, 0, 0, /*A=*/ 1, IGRAPH_DIRECTED, IGRAPH_BARABASI_BAG, /*start_from=*/ 0, &rng); igraph_diameter(&g, &result, 0, 0, 0, IGRAPH_UNDIRECTED, 1); /* printf("Diameter: %li\n", (long int) result); */ igraph_destroy(&g); igraph_ring(&g, 10, IGRAPH_DIRECTED, 0, 0); igraph_vector_init(&path, 0); igraph_diameter(&g, &result, &from, &to, &path, IGRAPH_DIRECTED, 1); printf("diameter: %li, from %li to %li\n", (long int) result, (long int) from, (long int) to); print_vector(&path); igraph_vector_destroy(&path); igraph_destroy(&g); igraph_rng_destroy(&rng); return 0; }
int main() { igraph_t graph, ring; igraph_vector_t order,order_out, father,dist; long int i; igraph_ring(&ring, 10, /*directed=*/ 0, /*mutual=*/ 0, /*circular=*/ 1); igraph_disjoint_union(&graph, &ring, &ring); igraph_destroy(&ring); igraph_vector_init(&order, 0); igraph_vector_init(&order_out, 0); igraph_vector_init(&father, 0); igraph_vector_init(&dist, 0); igraph_dfsd(&graph, /*root=*/0, /*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 1,&order,&order_out,&father,&dist, /*in_callback=*/ 0,/*out_callback*/0, /*extra=*/ 0); igraph_vector_print(&order); igraph_vector_print(&order_out); igraph_vector_print(&father); igraph_vector_print(&dist); igraph_vector_destroy(&order); igraph_vector_destroy(&order_out); igraph_vector_destroy(&father); igraph_vector_destroy(&dist); /* Test the callback */ igraph_dfsd(&graph, /*root=*/ 0,/*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 1,0, 0, 0, 0,&dfs_callback,0, 0); printf("\n"); igraph_dfsd(&graph, /*root=*/ 0,/*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 1,0, 0, 0, 0,0,&dfs_callback, 0); printf("\n"); /* Test different roots */ igraph_dfsd(&graph, /*root=*/ 2,/*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 1, 0, 0, 0, 0,&dfs_callback,0, 0); printf("\n"); igraph_dfsd(&graph, /*root=*/ 2,/*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 1, 0, 0, 0, 0,0,&dfs_callback,0); printf("\n"); igraph_destroy(&graph); return 0; }
int main() { igraph_t g; igraph_strvector_t names; igraph_i_set_attribute_table(&igraph_cattribute_table); /* save a simple ring graph */ igraph_ring(&g, 10, IGRAPH_DIRECTED, 0 /* mutual */, 1 /* circular */); igraph_write_graph_pajek(&g, stdout); /* add some vertex attributes */ igraph_strvector_init(&names, 0); igraph_strvector_add(&names, "A"); igraph_strvector_add(&names, "B"); igraph_strvector_add(&names, "C"); igraph_strvector_add(&names, "D"); igraph_strvector_add(&names, "E"); igraph_strvector_add(&names, "F"); igraph_strvector_add(&names, "G"); igraph_strvector_add(&names, "H"); igraph_strvector_add(&names, "I"); igraph_strvector_add(&names, "J"); SETVASV(&g, "id", &names); igraph_strvector_destroy(&names); /* save the graph with vertex names */ igraph_write_graph_pajek(&g, stdout); igraph_strvector_init(&names, 0); igraph_strvector_add(&names, "square"); igraph_strvector_add(&names, "square"); igraph_strvector_add(&names, "square"); igraph_strvector_add(&names, "square"); igraph_strvector_add(&names, "escaping spaces"); igraph_strvector_add(&names, "square"); igraph_strvector_add(&names, "square"); igraph_strvector_add(&names, "escaping \\backslashes\\"); igraph_strvector_add(&names, "square"); igraph_strvector_add(&names, "escaping \"quotes\""); SETVASV(&g, "shape", &names); igraph_strvector_destroy(&names); /* save the graph with escaped shapes */ igraph_write_graph_pajek(&g, stdout); /* destroy the graph */ igraph_destroy(&g); return 0; }
int main() { igraph_t g; igraph_vector_t v, v2; igraph_vector_init(&v, 0); igraph_vector_init(&v2, 0); igraph_ring(&g, 10, /*directed=*/ 0, /*mutual=*/ 0, /*circular=*/ 1); igraph_avg_nearest_neighbor_degree(&g, igraph_vss_all(), &v, &v2, /*weights=*/ 0); igraph_destroy(&g); igraph_vector_destroy(&v); igraph_vector_destroy(&v2); return 0; }
int test_normalized_laplacian(igraph_bool_t dir) { igraph_t g; igraph_matrix_t m; igraph_vector_t vec; igraph_matrix_init(&m, 1, 1); igraph_bool_t ok = 1; /* Undirected graph, no loop or multiple edges */ igraph_ring(&g, 5, dir, 0, 1); igraph_laplacian(&g, &m, 1); ok = ok && check_laplacian(&g, &m); /* Add some loop edges */ igraph_vector_init_real(&vec, 4, 1.0, 1.0, 2.0, 2.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); igraph_laplacian(&g, &m, 1); ok = ok && check_laplacian(&g, &m); /* Duplicate some edges */ igraph_vector_init_real(&vec, 4, 1.0, 2.0, 3.0, 4.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); igraph_laplacian(&g, &m, 1); ok = ok && check_laplacian(&g, &m); igraph_destroy(&g); igraph_matrix_destroy(&m); if (ok) printf("OK\n"); return !ok; }
int main() { igraph_t g; igraph_vector_ptr_t vecs; long int i; igraph_real_t weights[] = { 1, 2, 3, 4, 5, 1, 1, 1, 1, 1 }; igraph_real_t weights2[] = { 0,2,1, 0,5,2, 1,1,0, 2,2,8, 1,1,3, 1,1,4, 2,1 }; igraph_vector_t weights_vec; igraph_vs_t vs; /* Simple ring graph without weights */ igraph_ring(&g, 10, IGRAPH_UNDIRECTED, 0, 1); igraph_vector_ptr_init(&vecs, 5); for (i=0; i<igraph_vector_ptr_size(&vecs); i++) { VECTOR(vecs)[i] = calloc(1, sizeof(igraph_vector_t)); igraph_vector_init(VECTOR(vecs)[i], 0); } igraph_vs_vector_small(&vs, 1, 3, 5, 2, 1, -1); igraph_get_shortest_paths_dijkstra(&g, &vecs, 0, vs, 0, IGRAPH_OUT); for (i=0; i<igraph_vector_ptr_size(&vecs); i++) print_vector(VECTOR(vecs)[i]); /* Same ring, but with weights */ igraph_vector_view(&weights_vec, weights, sizeof(weights)/sizeof(igraph_real_t)); igraph_get_shortest_paths_dijkstra(&g, &vecs, 0, vs, &weights_vec, IGRAPH_OUT); for (i=0; i<igraph_vector_ptr_size(&vecs); i++) print_vector(VECTOR(vecs)[i]); igraph_destroy(&g); /* More complicated example */ igraph_small(&g, 10, IGRAPH_DIRECTED, 0,1, 0,2, 0,3, 1,2, 1,4, 1,5, 2,3, 2,6, 3,2, 3,6, 4,5, 4,7, 5,6, 5,8, 5,9, 7,5, 7,8, 8,9, 5,2, 2,1, -1); igraph_vector_view(&weights_vec, weights2, sizeof(weights2)/sizeof(igraph_real_t)); igraph_get_shortest_paths_dijkstra(&g, &vecs, 0, vs, &weights_vec, IGRAPH_OUT); for (i=0; i<igraph_vector_ptr_size(&vecs); i++) { print_vector(VECTOR(vecs)[i]); igraph_vector_destroy(VECTOR(vecs)[i]); free(VECTOR(vecs)[i]); } igraph_vector_ptr_destroy(&vecs); igraph_vs_destroy(&vs); igraph_destroy(&g); if (!IGRAPH_FINALLY_STACK_EMPTY) return 1; return 0; }
int main() { igraph_t graph, ring; igraph_vector_t order, rank, father, pred, succ, dist; igraph_vector_t restricted; igraph_vector_t roots; long int i; igraph_ring(&ring, 10, /*directed=*/ 0, /*mutual=*/ 0, /*circular=*/ 1); igraph_disjoint_union(&graph, &ring, &ring); igraph_destroy(&ring); igraph_vector_init(&order, 0); igraph_vector_init(&rank, 0); igraph_vector_init(&father, 0); igraph_vector_init(&pred, 0); igraph_vector_init(&succ, 0); igraph_vector_init(&dist, 0); igraph_bfsr(&graph, /*root=*/0, /*roots=*/ 0, /*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 1, /*restricted=*/ 0, &order, &rank, &father, &pred, &succ, &dist, /*callback=*/ 0, /*extra=*/ 0); igraph_vector_print(&order); igraph_vector_print(&rank); igraph_vector_print(&father); igraph_vector_print(&pred); igraph_vector_print(&succ); igraph_vector_print(&dist); igraph_vector_destroy(&order); igraph_vector_destroy(&rank); igraph_vector_destroy(&father); igraph_vector_destroy(&pred); igraph_vector_destroy(&succ); igraph_vector_destroy(&dist); /* Test the callback */ igraph_bfsr(&graph, /*root=*/ 0, /*roots=*/ 0, /*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 1, /*restricted=*/ 0, 0, 0, 0, 0, 0, 0, &bfs_callback, 0); printf("\n"); /* Test different roots */ igraph_bfsr(&graph, /*root=*/ 2, /*roots=*/ 0, /*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 1, /*restricted=*/ 0, 0, 0, 0, 0, 0, 0, &bfs_callback, 0); printf("\n"); /* Test restricted */ igraph_vector_init(&restricted, 0); for (i=5; i<igraph_vcount(&graph); i++) { igraph_vector_push_back(&restricted, i); } igraph_bfsr(&graph, /*root=*/ 5, /*roots=*/ 0, /*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 1, &restricted, 0, 0, 0, 0, 0, 0, &bfs_callback, 0); printf("\n"); /* Root not in restricted set */ igraph_bfsr(&graph, /*root=*/ 4, /*roots=*/ 0, /*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 1, &restricted, 0, 0, 0, 0, 0, 0, &bfs_callback, 0); printf("\n"); igraph_bfsr(&graph, /*root=*/ 3, /*roots=*/ 0, /*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 0, &restricted, 0, 0, 0, 0, 0, 0, &bfs_callback, 0); printf("\n"); /* Multiple root vertices */ igraph_vector_init(&roots, 3); VECTOR(roots)[0]=3; VECTOR(roots)[1]=4; VECTOR(roots)[2]=6; igraph_bfsr(&graph, /*root=*/ -1, &roots, /*neimode=*/ IGRAPH_OUT, /*unreachable=*/ 0, &restricted, 0, 0, 0, 0, 0, 0, &bfs_callback, 0); printf("\n"); igraph_vector_destroy(&roots); igraph_vector_destroy(&restricted); igraph_destroy(&graph); return 0; }
int test_unnormalized_laplacian(igraph_vector_t* w, igraph_bool_t dir) { igraph_t g; igraph_matrix_t m, m2; igraph_sparsemat_t sm; igraph_vector_t vec, *weights = 0; igraph_matrix_init(&m, 1, 1); igraph_sparsemat_init(&sm, 0, 0, 0); if (w) { weights = (igraph_vector_t*)calloc(1, sizeof(igraph_vector_t)); igraph_vector_copy(weights, w); } /* No loop or multiple edges */ igraph_ring(&g, 5, dir, 0, 1); igraph_laplacian(&g, &m, &sm, 0, weights); igraph_matrix_init(&m2, 0, 0); igraph_sparsemat_as_matrix(&m2, &sm); if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { return 41; } igraph_matrix_destroy(&m2); igraph_matrix_print(&m); printf("===\n"); /* Add some loop edges */ igraph_vector_init_real(&vec, 4, 1.0, 1.0, 2.0, 2.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); if (weights) { igraph_vector_push_back(weights, 2); igraph_vector_push_back(weights, 2); } igraph_laplacian(&g, &m, &sm, 0, weights); igraph_matrix_init(&m2, 0, 0); igraph_sparsemat_as_matrix(&m2, &sm); if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { return 42; } igraph_matrix_destroy(&m2); igraph_matrix_print(&m); printf("===\n"); /* Duplicate some edges */ igraph_vector_init_real(&vec, 4, 1.0, 2.0, 3.0, 4.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); if (weights) { igraph_vector_push_back(weights, 3); igraph_vector_push_back(weights, 3); } igraph_laplacian(&g, &m, &sm, 0, weights); igraph_matrix_init(&m2, 0, 0); igraph_sparsemat_as_matrix(&m2, &sm); if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { return 43; } igraph_matrix_destroy(&m2); igraph_matrix_print(&m); igraph_destroy(&g); igraph_matrix_destroy(&m); if (weights) { igraph_vector_destroy(weights); free(weights); } return 0; }
int test_normalized_laplacian(igraph_vector_t *w, igraph_bool_t dir) { igraph_t g; igraph_matrix_t m, m2; igraph_sparsemat_t sm; igraph_vector_t vec, *weights = 0; igraph_bool_t ok = 1; igraph_matrix_init(&m, 1, 1); igraph_sparsemat_init(&sm, 0, 0, 0); if (w) { weights = (igraph_vector_t*)calloc(1, sizeof(igraph_vector_t)); igraph_vector_copy(weights, w); } /* Undirected graph, no loop or multiple edges */ igraph_ring(&g, 5, dir, 0, 1); igraph_laplacian(&g, &m, &sm, 1, weights); igraph_matrix_init(&m2, 0, 0); igraph_sparsemat_as_matrix(&m2, &sm); if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { return 44; } igraph_matrix_destroy(&m2); ok = ok && check_laplacian(&g, &m, weights); /* Add some loop edges */ igraph_vector_init_real(&vec, 4, 1.0, 1.0, 2.0, 2.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); if (weights) { igraph_vector_push_back(weights, 2); igraph_vector_push_back(weights, 2); } igraph_laplacian(&g, &m, &sm, 1, weights); igraph_matrix_init(&m2, 0, 0); igraph_sparsemat_as_matrix(&m2, &sm); if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { return 45; } igraph_matrix_destroy(&m2); ok = ok && check_laplacian(&g, &m, weights); /* Duplicate some edges */ igraph_vector_init_real(&vec, 4, 1.0, 2.0, 3.0, 4.0); igraph_add_edges(&g, &vec, 0); igraph_vector_destroy(&vec); if (weights) { igraph_vector_push_back(weights, 3); igraph_vector_push_back(weights, 3); } igraph_laplacian(&g, &m, &sm, 1, weights); igraph_matrix_init(&m2, 0, 0); igraph_sparsemat_as_matrix(&m2, &sm); if (!igraph_matrix_all_e_tol(&m, &m2, 0)) { return 46; } igraph_matrix_destroy(&m2); ok = ok && check_laplacian(&g, &m, weights); igraph_destroy(&g); igraph_matrix_destroy(&m); if (weights) { igraph_vector_destroy(weights); free(weights); } if (ok) printf("OK\n"); return !ok; }
void test_bliss() { igraph_t ring1, ring2, directed_ring; igraph_vector_t perm; igraph_bool_t iso; igraph_bliss_info_t info; igraph_vector_int_t color; igraph_vector_ptr_t generators; igraph_ring(&ring1, 100, /*directed=*/ 0, /*mutual=*/ 0, /*circular=*/1); igraph_vector_init_seq(&perm, 0, igraph_vcount(&ring1)-1); random_permutation(&perm); igraph_permute_vertices(&ring1, &ring2, &perm); igraph_ring(&directed_ring, 100, /* directed= */ 1, /* mutual = */0, /* circular = */1); igraph_vector_ptr_init(&generators, 0); IGRAPH_VECTOR_PTR_SET_ITEM_DESTRUCTOR(&generators, igraph_vector_destroy); igraph_isomorphic_bliss(&ring1, &ring2, NULL, NULL, &iso, NULL, NULL, IGRAPH_BLISS_F, NULL, NULL); if (! iso) printf("Bliss failed on ring isomorphism.\n"); igraph_automorphisms(&ring1, NULL, IGRAPH_BLISS_F, &info); if (strcmp(info.group_size, "200") != 0) printf("Biss automorphism count failed: ring1.\n"); igraph_free(info.group_size); igraph_automorphisms(&ring2, NULL, IGRAPH_BLISS_F, &info); if (strcmp(info.group_size, "200") != 0) printf("Biss automorphism count failed: ring2.\n"); igraph_free(info.group_size); igraph_automorphisms(&directed_ring, NULL, IGRAPH_BLISS_F, &info); if (strcmp(info.group_size, "100") != 0) printf("Biss automorphism count failed: directed_ring.\n"); igraph_free(info.group_size); // The follwing test is included so there is at least one call to igraph_automorphism_group // in the test suite. However, the generator set returned may depend on the splitting // heursitics as well as on the Bliss version. If the test fails, please verify manually // that the generating set is valid. For a undirected cycle graph like ring2, there should // be two generators: a cyclic permutation and a reversal of the vertex order. igraph_automorphism_group(&ring2, NULL, &generators, IGRAPH_BLISS_F, NULL); if (igraph_vector_ptr_size(&generators) != 2) printf("Bliss automorphism generators may have failed with ring2. " "Please verify the generators manually. " "Note that the generator set is not guaranteed to be minimal.\n"); igraph_vector_ptr_free_all(&generators); // For a directed ring, the only generator should be a cyclic permutation. igraph_automorphism_group(&directed_ring, NULL, &generators, IGRAPH_BLISS_F, NULL); if (igraph_vector_ptr_size(&generators) != 1) printf("Bliss automorphism generators may have failed with directed_ring. " "Please verify the generators manually. " "Note that the generator set is not guaranteed to be minimal.\n"); igraph_vector_ptr_free_all(&generators); igraph_vector_int_init_seq(&color, 0, igraph_vcount(&ring1)-1); igraph_automorphisms(&ring1, &color, IGRAPH_BLISS_F, &info); if (strcmp(info.group_size, "1") != 0) printf("Biss automorphism count with color failed: ring1.\n"); igraph_free(info.group_size); // There's only one automorphism for this coloured graph, so the generating set is empty. igraph_automorphism_group(&ring1, &color, &generators, IGRAPH_BLISS_F, NULL); if (igraph_vector_ptr_size(&generators) != 0) printf("Bliss automorphism generators failed with colored graph.\n"); igraph_vector_ptr_destroy_all(&generators); igraph_vector_int_destroy(&color); igraph_vector_destroy(&perm); igraph_destroy(&ring1); igraph_destroy(&ring2); igraph_destroy(&directed_ring); }