1 /* -*- mode: C -*-  */
2 /*
3    IGraph library.
4    Copyright (C) 2006-2012  Gabor Csardi <csardi.gabor@gmail.com>
5    334 Harvard street, Cambridge, MA 02139 USA
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301 USA
21 
22 */
23 
24 #include <igraph.h>
25 
main()26 int main() {
27     igraph_t graph;
28     igraph_vector_t membership, degree;
29     igraph_integer_t nb_clusters;
30     igraph_real_t quality;
31 
32     /* Set default seed to get reproducible results */
33     igraph_rng_seed(igraph_rng_default(), 0);
34 
35     /* Simple unweighted graph */
36     igraph_small(&graph, 10, IGRAPH_UNDIRECTED,
37                  0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 1, 3, 1, 4, 2, 3, 2, 4, 3, 4,
38                  5, 6, 5, 7, 5, 8, 5, 9, 6, 7, 6, 8, 6, 9, 7, 8, 7, 9, 8, 9,
39                  0, 5, -1);
40 
41     /* Perform Leiden algorithm using CPM */
42     igraph_vector_init(&membership, igraph_vcount(&graph));
43     igraph_community_leiden(&graph, NULL, NULL, 0.05, 0.01, 0, &membership, &nb_clusters, &quality);
44 
45     printf("Leiden found %" IGRAPH_PRId " clusters using CPM (resolution parameter 0.05), quality is %.4f.\n", nb_clusters, quality);
46     printf("Membership: ");
47     igraph_vector_print(&membership);
48     printf("\n");
49 
50     /* Start from existing membership to improve it further */
51     igraph_community_leiden(&graph, NULL, NULL, 0.05, 0.01, 1, &membership, &nb_clusters, &quality);
52 
53     printf("Iterated Leiden, using CPM (resolution parameter 0.05), quality is %.4f.\n", quality);
54     printf("Membership: ");
55     igraph_vector_print(&membership);
56     printf("\n");
57 
58     /* Initialize degree vector to use for optimizing modularity */
59     igraph_vector_init(&degree, igraph_vcount(&graph));
60     igraph_degree(&graph, &degree, igraph_vss_all(), IGRAPH_ALL, 1);
61 
62     /* Perform Leiden algorithm using modularity */
63     igraph_community_leiden(&graph, NULL, &degree, 1.0 / (2 * igraph_ecount(&graph)), 0.01, 0, &membership, &nb_clusters, &quality);
64 
65     printf("Leiden found %" IGRAPH_PRId " clusters using modularity, quality is %.4f.\n", nb_clusters, quality);
66     printf("Membership: ");
67     igraph_vector_print(&membership);
68     printf("\n");
69 
70     igraph_vector_destroy(&degree);
71     igraph_vector_destroy(&membership);
72     igraph_destroy(&graph);
73 
74     return 0;
75 }
76