1
2 #include <igraph.h>
3 #include <math.h>
4
main()5 int main() {
6 igraph_t graph;
7 igraph_vector_t x, y;
8 igraph_vector_t weights;
9 igraph_eit_t eit;
10 igraph_real_t avg_dist;
11
12 /* Set random seed for reproducible results */
13
14 igraph_rng_seed(igraph_rng_default(), 42);
15
16 /* Create a random geometric graph and retrieve vertex coordinates */
17
18 igraph_vector_init(&x, 0);
19 igraph_vector_init(&y, 0);
20
21 igraph_grg_game(&graph, 200, 0.1, /* torus */ 0, &x, &y);
22
23 /* Compute edge weights as geometric distance */
24
25 igraph_vector_init(&weights, igraph_ecount(&graph));
26 igraph_eit_create(&graph, igraph_ess_all(IGRAPH_EDGEORDER_ID), &eit);
27 for (; ! IGRAPH_EIT_END(eit); IGRAPH_EIT_NEXT(eit)) {
28 long int e = IGRAPH_EIT_GET(eit);
29 long int u = IGRAPH_FROM(&graph, e);
30 long int v = IGRAPH_TO(&graph, e);
31
32 VECTOR(weights)[e] = hypot(VECTOR(x)[u] - VECTOR(x)[v], VECTOR(y)[u] - VECTOR(y)[v]);
33 }
34 igraph_eit_destroy(&eit);
35
36 /* Compute average path length */
37
38 igraph_average_path_length_dijkstra(&graph, &avg_dist, NULL, &weights, IGRAPH_UNDIRECTED, /* unconn */ 1);
39
40 printf("Average distance in the geometric graph: %g.\n", avg_dist);
41
42 /* Destroy data structures when no longer needed */
43
44 igraph_vector_destroy(&weights);
45 igraph_destroy(&graph);
46 igraph_vector_destroy(&x);
47 igraph_vector_destroy(&y);
48
49 return 0;
50 }
51