1 /*
2    IGraph library.
3    Copyright (C) 2021  The igraph development team <igraph@igraph.org>
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 
19 #include <igraph.h>
20 #include "test_utilities.inc"
21 
22 char *names[] = {
23     [IGRAPH_COMMCMP_VI] = "VI",
24     [IGRAPH_COMMCMP_NMI] = "NMI",
25     [IGRAPH_COMMCMP_SPLIT_JOIN] = "Split-join",
26     [IGRAPH_COMMCMP_RAND] = "Rand",
27     [IGRAPH_COMMCMP_ADJUSTED_RAND] = "Adjusted Rand",
28 };
29 
30 
compare_and_print(igraph_vector_t * comm1,igraph_vector_t * comm2,igraph_community_comparison_t t,igraph_error_type_t e)31 void compare_and_print(igraph_vector_t *comm1, igraph_vector_t *comm2, igraph_community_comparison_t t, igraph_error_type_t e) {
32     igraph_real_t result;
33     printf("%s result: ", names[t]);
34     IGRAPH_ASSERT(igraph_compare_communities(comm1, comm2, &result, t) == e);
35     if (e == IGRAPH_EINVAL) {
36         printf("failed as expected\n");
37     } else {
38         print_real(stdout, result, "%g");
39         printf("\n");
40     }
41 }
42 
43 
main()44 int main() {
45     igraph_vector_t comm1, comm2;
46 
47     igraph_set_error_handler(igraph_error_handler_ignore);
48 
49     printf("Only one member, both partitions equal to whole set:\n");
50     igraph_vector_init_int(&comm1, 1, 0);
51     igraph_vector_init_int(&comm2, 1, 0);
52 
53 
54     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_VI, IGRAPH_SUCCESS);
55     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_RAND, IGRAPH_EINVAL);
56     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_ADJUSTED_RAND, IGRAPH_EINVAL);
57     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_NMI, IGRAPH_SUCCESS);
58     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_SPLIT_JOIN, IGRAPH_SUCCESS);
59 
60     igraph_vector_destroy(&comm1);
61     igraph_vector_destroy(&comm2);
62 
63     printf("\nEmpty sets:\n");
64     igraph_vector_init(&comm1, 0);
65     igraph_vector_init(&comm2, 0);
66     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_VI, IGRAPH_SUCCESS);
67     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_RAND, IGRAPH_EINVAL);
68     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_ADJUSTED_RAND, IGRAPH_EINVAL);
69     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_NMI, IGRAPH_SUCCESS);
70     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_SPLIT_JOIN, IGRAPH_SUCCESS);
71 
72     igraph_vector_destroy(&comm1);
73     igraph_vector_destroy(&comm2);
74 
75     printf("\nTwo equal, differenly labeled partitions:\n");
76     igraph_vector_init_int(&comm1, 2, 0, 1);
77     igraph_vector_init_int(&comm2, 2, 1, 0);
78     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_VI, IGRAPH_SUCCESS);
79     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_RAND, IGRAPH_SUCCESS);
80     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_ADJUSTED_RAND, IGRAPH_SUCCESS);
81     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_NMI, IGRAPH_SUCCESS);
82     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_SPLIT_JOIN, IGRAPH_SUCCESS);
83 
84     igraph_vector_destroy(&comm1);
85     igraph_vector_destroy(&comm2);
86 
87     printf("\nTwo different partitions: ((5,1), (8,3,4), (0,6,7,2,9)) and ((5,8), (1,3,4,0), (6,7,2,9))\n");
88     igraph_vector_init_int(&comm1, 10, 2, 0, 2, 1, 1, 0, 2, 2, 1, 2);
89     igraph_vector_init_int(&comm2, 10, 1, 1, 2, 1, 1, 0, 2, 2, 0, 2);
90     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_VI, IGRAPH_SUCCESS);
91     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_RAND, IGRAPH_SUCCESS);
92     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_ADJUSTED_RAND, IGRAPH_SUCCESS);
93     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_NMI, IGRAPH_SUCCESS);
94     compare_and_print(&comm1, &comm2, IGRAPH_COMMCMP_SPLIT_JOIN, IGRAPH_SUCCESS);
95 
96     igraph_vector_destroy(&comm1);
97     igraph_vector_destroy(&comm2);
98 
99     VERIFY_FINALLY_STACK();
100     return 0;
101 }
102