1 /* -*- mode: C -*-  */
2 /*
3    IGraph library.
4    Copyright (C) 2021  The igraph development team <igraph@igraph.org>
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <https://www.gnu.org/licenses/>.
18 */
19 
20 #include <igraph.h>
21 
22 #include "test_utilities.inc"
23 
main()24 int main() {
25     igraph_t g;
26     igraph_real_t  modularity, temperature;
27     igraph_vector_t membership, csize;
28     /* long int i; */
29     igraph_real_t cohesion, adhesion;
30     igraph_integer_t inner_links;
31     igraph_integer_t outer_links;
32 
33     igraph_rng_seed(igraph_rng_default(), 137);
34 
35     /* Two 5-cliques connected by a single edge */
36     igraph_small(&g, 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, 0, 5, -1);
39     igraph_vector_init(&membership, 0);
40     igraph_vector_init(&csize, 0);
41 
42     printf("\nOriginal implementation.\n");
43     igraph_community_spinglass(&g,
44                                NULL, /* no weights */
45                                &modularity,
46                                &temperature,
47                                &membership,
48                                &csize,
49                                10,   /* no of spins */
50                                0,    /* parallel update */
51                                1.0,  /* start temperature */
52                                0.01, /* stop temperature */
53                                0.99, /* cooling factor */
54                                IGRAPH_SPINCOMM_UPDATE_CONFIG,
55                                1.0, /* gamma */
56                                IGRAPH_SPINCOMM_IMP_ORIG,
57                                /*gamma_minus =*/ 0);
58 
59     IGRAPH_ASSERT(igraph_vector_size(&membership) == igraph_vcount(&g));
60     IGRAPH_ASSERT(igraph_vector_size(&csize) == igraph_vector_max(&membership) + 1);
61 
62     /* The following depend on the random seed, however, for this graph,
63        the result is almost always the same (i.e. two clusters). */
64     printf("Modularity: %g\n", modularity);
65     print_vector_round(&membership);
66 
67     printf("\nOriginal implementation, parallel updating.\n");
68     igraph_community_spinglass(&g,
69                                NULL, /* no weights */
70                                &modularity,
71                                &temperature,
72                                &membership,
73                                &csize,
74                                10,   /* no of spins */
75                                1,    /* parallel update */
76                                1.0,  /* start temperature */
77                                0.01, /* stop temperature */
78                                0.99, /* cooling factor */
79                                IGRAPH_SPINCOMM_UPDATE_CONFIG,
80                                1.0, /* gamma */
81                                IGRAPH_SPINCOMM_IMP_ORIG,
82                                /*gamma_minus =*/ 0);
83 
84     IGRAPH_ASSERT(igraph_vector_size(&membership) == igraph_vcount(&g));
85     IGRAPH_ASSERT(igraph_vector_size(&csize) == igraph_vector_max(&membership) + 1);
86 
87     /* The following depend on the random seed, however, for this graph,
88        the result is almost always the same (i.e. two clusters). */
89     printf("Modularity: %g\n", modularity);
90     print_vector_round(&membership);
91 
92     printf("\nNegative implementation.\n");
93     igraph_community_spinglass(&g,
94                                NULL, /* no weights */
95                                &modularity,
96                                &temperature,
97                                &membership,
98                                &csize,
99                                10,   /* no of spins */
100                                0,    /* parallel update */
101                                1.0,  /* start temperature */
102                                0.01, /* stop temperature */
103                                0.99, /* cooling factor */
104                                IGRAPH_SPINCOMM_UPDATE_CONFIG,
105                                1.0, /* gamma */
106                                IGRAPH_SPINCOMM_IMP_NEG,
107                                /*gamma_minus =*/ 0);
108 
109     IGRAPH_ASSERT(igraph_vector_size(&membership) == igraph_vcount(&g));
110     IGRAPH_ASSERT(igraph_vector_size(&csize) == igraph_vector_max(&membership) + 1);
111 
112     /* The following depend on the random seed, however, for this graph,
113        the result is almost always the same (i.e. two clusters). */
114     printf("Modularity: %g\n", modularity);
115     print_vector_round(&membership);
116 
117     /* Try to call this as well, we don't check the results currently.... */
118 
119     igraph_community_spinglass_single(&g,
120                                       /*weights=  */ 0,
121                                       /*vertex=   */ 0,
122                                       /*community=*/ &membership,
123                                       /*cohesion= */ &cohesion,
124                                       /*adhesion= */ &adhesion,
125                                       /*inner_links= */ &inner_links,
126                                       /*outer_links= */ &outer_links,
127                                       /*spins=       */ 2,
128                                       /*update_rule= */ IGRAPH_SPINCOMM_UPDATE_CONFIG,
129                                       /*gamma=       */ 1.0);
130 
131     igraph_destroy(&g);
132     igraph_vector_destroy(&membership);
133     igraph_vector_destroy(&csize);
134 
135     VERIFY_FINALLY_STACK();
136     return 0;
137 }
138