File: tutorial2.c

package info (click to toggle)
igraph 1.0.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,444 kB
  • sloc: ansic: 155,759; cpp: 32,544; xml: 2,960; python: 411; makefile: 168; javascript: 20; sh: 9
file content (45 lines) | stat: -rw-r--r-- 1,531 bytes parent folder | download | duplicates (2)
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;
}