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 main(int argc, char*argv[]) { // the graph is just a list og from to identifier pairs, one per line if (argc != 4) { fprintf (stderr, "Usage: %s <path to graph> <from id> <to id>\n", argv[0]); exit(1); } // 57572 DOCK6 // 7049 TGFBR3 // 2263 FGFBP3 igraph_vs_t vertex_to; igraph_integer_t vertex_from; vertex_from = atol(argv[2]); // in principle, unumber of "to" vertices is arbitrary, // but that is a piece of code I will write some other time //igraph_vs_vector_small(&vertex_to, 2263, 7049, -1); igraph_vs_vector_small(&vertex_to, atol(argv[3]), -1); igraph_t graph; /*The number of vertices in the graph. If smaller than the largest integer in the file it will be ignored. It is thus safe to supply zero here. */ igraph_integer_t zero = 0; FILE * infile = efopen(argv[1], "r") ; igraph_bool_t directed = IGRAPH_UNDIRECTED; igraph_read_graph_edgelist (&graph, infile, zero, directed); /* shortest path calculation: */ /* http://igraph.org/c/doc/igraph-Structural.html#igraph_get_all_shortest_paths int igraph_get_all_shortest_paths(const igraph_t *graph, igraph_vector_ptr_t *res, igraph_vector_t *nrgeo, igraph_integer_t from, const igraph_vs_t to, igraph_neimode_t mode); */ igraph_vector_ptr_t result; igraph_vector_t nrgeo; igraph_integer_t i; // the second argument here is the number of "to" vertices igraph_vector_ptr_init (&result, 1); igraph_vector_init(&nrgeo, 0); igraph_get_all_shortest_paths (&graph, &result, &nrgeo, vertex_from, vertex_to, IGRAPH_ALL); for (i=0; i<igraph_vector_ptr_size(&result); i++) { print_vector(VECTOR(result)[i]); } igraph_vector_ptr_destroy(&result); igraph_vs_destroy(&vertex_to); igraph_destroy(&graph); if (!IGRAPH_FINALLY_STACK_EMPTY) return 1; return 0; }
int main() { igraph_t g; igraph_vector_t v=IGRAPH_VECTOR_NULL; igraph_real_t edges[] = { 0,1, 1,2, 2,2, 2,3, 2,4, 3,4 }; igraph_vector_t v2; long int i; igraph_vit_t vit; igraph_vs_t vs; igraph_integer_t size; igraph_vector_view(&v, edges, sizeof(edges)/sizeof(igraph_real_t)); igraph_create(&g, &v, 0, IGRAPH_DIRECTED); /* Create iterator based on a vector (view) */ igraph_vector_init(&v2, 6); VECTOR(v2)[0]=0; VECTOR(v2)[1]=2; VECTOR(v2)[2]=4; VECTOR(v2)[3]=0; VECTOR(v2)[4]=2; VECTOR(v2)[5]=4; igraph_vit_create(&g, igraph_vss_vector(&v2), &vit); i=0; while (!IGRAPH_VIT_END(vit)) { if (IGRAPH_VIT_GET(vit) != VECTOR(v2)[i]) { return 1; } IGRAPH_VIT_NEXT(vit); i++; } if (i != igraph_vector_size(&v2)) { return 2; } igraph_vit_destroy(&vit); igraph_vector_destroy(&v2); /* Create small vector iterator */ igraph_vs_vector_small(&vs, 0, 2, 4, 0, 2, 4, 2, -1); igraph_vit_create(&g, vs, &vit); igraph_vs_size(&g, &vs, &size); printf("%li ", (long int) size); for (; !IGRAPH_VIT_END(vit); IGRAPH_VIT_NEXT(vit)) { printf("%li ", (long int) IGRAPH_VIT_GET(vit)); } printf("\n"); igraph_vit_destroy(&vit); igraph_vs_destroy(&vs); /* Clean up */ igraph_destroy(&g); return 0; }
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 g, g2; igraph_vector_ptr_t sep; igraph_vs_t vs; igraph_small(&g, 7, IGRAPH_UNDIRECTED, 1,0, 2,0, 3,0, 4,0, 5,0, 6,0, -1); igraph_vector_ptr_init(&sep, 0); igraph_minimum_size_separators(&g, &sep); print_and_destroy(&sep); igraph_destroy(&g); /* ----------------------------------------------------------- */ igraph_small(&g, 5, IGRAPH_UNDIRECTED, 0,3, 1,3, 2,3, 0,4, 1,4, 2,4, -1); igraph_vector_ptr_init(&sep, 0); igraph_minimum_size_separators(&g, &sep); print_and_destroy(&sep); igraph_destroy(&g); /* ----------------------------------------------------------- */ igraph_small(&g, 5, IGRAPH_UNDIRECTED, 2,0, 3,0, 4,0, 2,1, 3,1, 4,1, -1); igraph_vector_ptr_init(&sep, 0); igraph_minimum_size_separators(&g, &sep); print_and_destroy(&sep); igraph_destroy(&g); /* ----------------------------------------------------------- */ igraph_small(&g, 10, IGRAPH_UNDIRECTED, 0,2, 0,3, 1,2, 1,3, 5,2, 5,3, 6,2, 6,3, 7,2, 7,3, 8,2, 8,3, 9,2, 9,3, 2,4, 4,3, -1); igraph_vector_ptr_init(&sep, 0); igraph_minimum_size_separators(&g, &sep); print_and_destroy(&sep); igraph_destroy(&g); /* ----------------------------------------------------------- */ igraph_full(&g, 4, IGRAPH_UNDIRECTED, /*loops=*/ 0); igraph_vector_ptr_init(&sep, 0); igraph_minimum_size_separators(&g, &sep); print_and_destroy(&sep); igraph_destroy(&g); /* ----------------------------------------------------------- */ igraph_small(&g, 23, IGRAPH_UNDIRECTED, 0,1, 0,2, 0,3, 0,4, 0,5, 1,2, 1,3, 1,4, 1,6, 2,3, 2,5, 2,6, 3,4, 3,5, 3,6, 4,5, 4,6, 4,20, 5,6, 6,7, 6,10, 6,13, 6,18, 7,8, 7,10, 7,13, 8,9, 9,11, 9,12, 10,11, 10,13, 11,15, 12,15, 13,14, 14,15, 16,17, 16,18, 16,19, 17,19, 17,20, 18,19, 18,21, 18,22, 19,20, 20,21, 20,22, 21,22, -1); igraph_vector_ptr_init(&sep, 0); igraph_minimum_size_separators(&g, &sep); printf("Orig:\n"); print_and_destroy(&sep); igraph_vector_ptr_init(&sep, 0); igraph_vs_vector_small(&vs, 0,1,2,3,4,5,6, 16,17,18,19,20,21,22, -1); igraph_induced_subgraph(&g, &g2, vs, IGRAPH_SUBGRAPH_AUTO); igraph_minimum_size_separators(&g2, &sep); printf("1-7,17-23:\n"); print_and_destroy(&sep); igraph_vs_destroy(&vs); igraph_destroy(&g2); igraph_vector_ptr_init(&sep, 0); igraph_vs_vector_small(&vs, 6,7,8,9,10,11,12,13,14,15, -1); igraph_induced_subgraph(&g, &g2, vs, IGRAPH_SUBGRAPH_AUTO); igraph_minimum_size_separators(&g2, &sep); printf("7-16:\n"); print_and_destroy(&sep); igraph_vs_destroy(&vs); igraph_destroy(&g2); igraph_vector_ptr_init(&sep, 0); igraph_vs_vector_small(&vs, 16,17,18,19,20,21,22, -1); igraph_induced_subgraph(&g, &g2, vs, IGRAPH_SUBGRAPH_AUTO); igraph_minimum_size_separators(&g2, &sep); printf("17-23:\n"); print_and_destroy(&sep); igraph_vs_destroy(&vs); igraph_destroy(&g2); igraph_vector_ptr_init(&sep, 0); igraph_vs_vector_small(&vs, 6,7,10,13, -1); igraph_induced_subgraph(&g, &g2, vs, IGRAPH_SUBGRAPH_AUTO); igraph_minimum_size_separators(&g2, &sep); printf("7,8,11,14:\n"); print_and_destroy(&sep); igraph_vs_destroy(&vs); igraph_destroy(&g2); igraph_vector_ptr_init(&sep, 0); igraph_vs_vector_small(&vs, 0,1,2,3,4,5,6, -1); igraph_induced_subgraph(&g, &g2, vs, IGRAPH_SUBGRAPH_AUTO); igraph_minimum_size_separators(&g2, &sep); printf("1-7:\n"); print_and_destroy(&sep); igraph_vs_destroy(&vs); igraph_destroy(&g2); igraph_destroy(&g); return 0; }