File: igraph_decompose.c

package info (click to toggle)
igraph 0.10.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 16,176 kB
  • sloc: ansic: 121,500; cpp: 21,699; xml: 2,734; python: 411; makefile: 147; javascript: 20; sh: 9
file content (51 lines) | stat: -rw-r--r-- 1,641 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
46
47
48
49
50
51

#include <igraph.h>
#include <stdlib.h>

int main(void) {

    igraph_t ring, g, *component;
    igraph_graph_list_t complist;
    igraph_integer_t i;
    igraph_integer_t edges[] = { 0, 1, 1, 2, 2, 0,
                              3, 4, 4, 5, 5, 6,
                              8, 9, 9, 10
                            };
    igraph_vector_int_t v;

    igraph_graph_list_init(&complist, 0);

    /* A ring, a single component */
    igraph_ring(&ring, 10, IGRAPH_UNDIRECTED, 0, 1);

    igraph_decompose(&ring, &complist, IGRAPH_WEAK, -1, 0);
    component = igraph_graph_list_get_ptr(&complist, 0);
    igraph_write_graph_edgelist(component, stdout);
    igraph_destroy(&ring);
    igraph_graph_list_clear(&complist);

    /* Random graph with a giant component */
    igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNP, 100, 4.0 / 100,
                            IGRAPH_UNDIRECTED, 0);
    igraph_decompose(&g, &complist, IGRAPH_WEAK, -1, 20);
    if (igraph_graph_list_size(&complist) != 1) {
        return 1;
    }
    igraph_destroy(&g);
    igraph_graph_list_clear(&complist);

    /* A toy graph, three components maximum, with at least 2 vertices each */
    igraph_create(&g,
                  igraph_vector_int_view(&v, edges, sizeof(edges) / sizeof(edges[0])),
                  0, IGRAPH_DIRECTED);
    igraph_decompose(&g, &complist, IGRAPH_WEAK, 3, 2);
    for (i = 0; i < igraph_graph_list_size(&complist); i++) {
        component = igraph_graph_list_get_ptr(&complist, i);
        igraph_write_graph_edgelist(component, stdout);
    }
    igraph_destroy(&g);

    igraph_graph_list_destroy(&complist);

    return 0;
}