1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <igraph.h>
int main(void) {
igraph_t graph;
igraph_vector_int_t dimvector;
igraph_vector_int_t edges;
igraph_vector_bool_t periodic;
igraph_real_t avg_path_len;
/* Initialize the library. */
igraph_setup();
igraph_vector_int_init(&dimvector, 2);
VECTOR(dimvector)[0] = 30;
VECTOR(dimvector)[1] = 30;
igraph_vector_bool_init(&periodic, 2);
igraph_vector_bool_fill(&periodic, true);
igraph_square_lattice(&graph, &dimvector, 0, IGRAPH_UNDIRECTED,
/* mutual= */ false, &periodic);
igraph_average_path_length(&graph, NULL, &avg_path_len, NULL,
IGRAPH_UNDIRECTED, /* unconn= */ true);
printf("Average path length (lattice): %g\n", (double) avg_path_len);
/* Seed the RNG to ensure identical results across runs. */
igraph_rng_seed(igraph_rng_default(), 42);
igraph_vector_int_init(&edges, 20);
for (igraph_int_t i = 0; i < igraph_vector_int_size(&edges); i++) {
VECTOR(edges)[i] = RNG_INTEGER(0, igraph_vcount(&graph) - 1);
}
igraph_add_edges(&graph, &edges, NULL);
igraph_average_path_length(&graph, NULL, &avg_path_len, NULL,
IGRAPH_UNDIRECTED, /* unconn= */ true);
printf("Average path length (randomized lattice): %g\n", (double) avg_path_len);
igraph_vector_bool_destroy(&periodic);
igraph_vector_int_destroy(&dimvector);
igraph_vector_int_destroy(&edges);
igraph_destroy(&graph);
return 0;
}
|