Coverage Report

Created: 2026-12-01 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/igraph/fuzzing/community.cpp
Line
Count
Source
1
/*
2
   igraph library.
3
   Copyright (C) 2024  The igraph development team
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, write to the Free Software
17
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
   02110-1301 USA
19
*/
20
21
#include <igraph.h>
22
#include <cstdlib>
23
24
// Verify that all communities induce connected subgraphs
25
6.94k
void check_communities_are_connected(const igraph_t *graph, const igraph_vector_int_t *membership) {
26
6.94k
    const igraph_int_t vcount = igraph_vcount(graph);
27
6.94k
    igraph_vector_int_t memb;
28
6.94k
    igraph_vector_int_list_t communities;
29
6.94k
    igraph_int_t no;
30
31
6.94k
    igraph_vector_int_init_copy(&memb, membership);
32
6.94k
    igraph_reindex_membership(&memb, NULL, &no);
33
34
6.94k
    igraph_vector_int_list_init(&communities, no);
35
331k
    for (igraph_int_t v=0; v < vcount; v++) {
36
324k
        igraph_vector_int_push_back(igraph_vector_int_list_get_ptr(&communities, VECTOR(memb)[v]), v);
37
324k
    }
38
39
329k
    for (igraph_int_t c=0; c < no; c++) {
40
322k
        igraph_t subgraph;
41
322k
        igraph_bool_t connected;
42
43
322k
        igraph_induced_subgraph(graph, &subgraph,
44
322k
                                igraph_vss_vector(igraph_vector_int_list_get_ptr(&communities, c)),
45
322k
                                IGRAPH_SUBGRAPH_AUTO);
46
322k
        igraph_is_connected(&subgraph, &connected, IGRAPH_WEAK);
47
322k
        IGRAPH_ASSERT(connected);
48
322k
        igraph_destroy(&subgraph);
49
322k
    }
50
51
6.94k
    igraph_vector_int_list_destroy(&communities);
52
6.94k
    igraph_vector_int_destroy(&memb);
53
6.94k
}
54
55
3.51k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
56
3.51k
    igraph_t graph;
57
3.51k
    igraph_vector_int_t edges;
58
59
3.51k
    igraph_set_warning_handler(igraph_warning_handler_ignore);
60
61
3.51k
    if (Size % 2 == 0 || Size > 512+1 || Size < 1) {
62
13
        return 0;
63
13
    }
64
65
3.50k
    igraph_vector_int_init(&edges, Size-1);
66
214k
    for (size_t i=0; i < Size-1; ++i) {
67
211k
        VECTOR(edges)[i] = Data[i+1];
68
211k
    }
69
70
3.50k
    igraph_rng_seed(igraph_rng_default(), 42);
71
72
3.50k
    if (igraph_create(&graph, &edges, Data[0], IGRAPH_DIRECTED) == IGRAPH_SUCCESS) {
73
3.50k
        igraph_matrix_int_t merges, im;
74
3.50k
        igraph_matrix_t mat;
75
3.50k
        igraph_vector_int_t membership, membership2, iv, iv2;
76
3.50k
        igraph_vector_t mv, v;
77
3.50k
        igraph_real_t m, r;
78
3.50k
        igraph_int_t i;
79
3.50k
        igraph_bool_t b;
80
81
        /* Limit graph size for the sake of performance. */
82
3.50k
        if (igraph_vcount(&graph) <= 64) {
83
3.47k
            igraph_matrix_int_init(&merges, 0, 0);
84
3.47k
            igraph_matrix_int_init(&im, 0, 0);
85
3.47k
            igraph_matrix_init(&mat, 0, 0);
86
3.47k
            igraph_vector_int_init(&membership, 0);
87
3.47k
            igraph_vector_int_init(&membership2, 0);
88
3.47k
            igraph_vector_int_init(&iv, 0);
89
3.47k
            igraph_vector_int_init(&iv2, 0);
90
3.47k
            igraph_vector_init(&mv, 0);
91
3.47k
            igraph_vector_init(&v, 0);
92
93
            // The IGRAPH_LPA_FAST version is temporarily switched to undirected until
94
            // the reason for the bad performance / infinite loop in the directed version
95
            // is debugged. See https://github.com/igraph/igraph/issues/2608
96
3.47k
            igraph_community_label_propagation(&graph, &membership, IGRAPH_ALL, NULL, NULL, NULL, IGRAPH_LPA_FAST);
97
3.47k
            igraph_community_label_propagation(&graph, &membership, IGRAPH_OUT, NULL, NULL, NULL, IGRAPH_LPA_RETENTION);
98
3.47k
            igraph_community_label_propagation(&graph, &membership, IGRAPH_OUT, NULL, NULL, NULL, IGRAPH_LPA_DOMINANCE);
99
100
3.47k
            igraph_community_walktrap(&graph, NULL, 3, &merges, &mv, &membership);
101
3.47k
            igraph_community_edge_betweenness(&graph, &iv, &v, &merges, &iv2, &mv, &membership2, IGRAPH_DIRECTED, NULL, NULL);
102
103
3.47k
            igraph_community_leiden(&graph, NULL, NULL, NULL, 1.5, 0.01, false, 2, &membership, &i, &r);
104
3.47k
            check_communities_are_connected(&graph, &membership);
105
106
            // Take the opportunity to run functions that can use the output of community detection.
107
108
3.47k
            {
109
3.47k
                igraph_community_comparison_t method[] = {
110
3.47k
                    IGRAPH_COMMCMP_VI,
111
3.47k
                    IGRAPH_COMMCMP_NMI,
112
3.47k
                    IGRAPH_COMMCMP_SPLIT_JOIN,
113
3.47k
                    IGRAPH_COMMCMP_RAND,
114
3.47k
                    IGRAPH_COMMCMP_ADJUSTED_RAND
115
3.47k
                };
116
20.8k
                for (size_t i=0; i < sizeof(method) / sizeof(method[0]); i++) {
117
17.3k
                    if ((method[i] == IGRAPH_COMMCMP_RAND ||
118
13.8k
                         method[i] == IGRAPH_COMMCMP_ADJUSTED_RAND) &&
119
6.94k
                        igraph_vcount(&graph) < 2) {
120
38
                        continue;
121
38
                    }
122
17.3k
                    igraph_compare_communities(&membership, &membership2, &r, method[i]);
123
17.3k
                }
124
3.47k
            }
125
126
3.47k
            {
127
3.47k
                igraph_t graph2;
128
3.47k
                igraph_copy(&graph2, &graph);
129
3.47k
                igraph_contract_vertices(&graph2, &membership, NULL);
130
3.47k
                igraph_destroy(&graph2);
131
3.47k
            }
132
133
3.47k
            igraph_modularity(&graph, &membership, NULL, 1.5, IGRAPH_UNDIRECTED, &m);
134
3.47k
            igraph_modularity_matrix(&graph, NULL, 0.75, &mat, IGRAPH_DIRECTED);
135
3.47k
            igraph_assortativity_nominal(&graph, NULL, &membership, &r, IGRAPH_DIRECTED, true);
136
137
3.47k
            igraph_simplify(&graph, true, true, NULL);
138
3.47k
            igraph_community_voronoi(&graph, &membership, &iv, &m, NULL, NULL, IGRAPH_OUT, 1.0);
139
140
3.47k
            igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_COLLAPSE, NULL);
141
3.47k
            igraph_community_fastgreedy(&graph, NULL, &merges, &mv, &membership);
142
143
3.47k
            igraph_community_leiden(&graph, NULL, NULL, NULL, 1.5, 0.01, false, 2, &membership, &i, &r);
144
3.47k
            check_communities_are_connected(&graph, &membership);
145
146
3.47k
            igraph_community_multilevel(&graph, NULL, 0.8, &membership, &im, &mv);
147
148
3.47k
            igraph_is_connected(&graph, &b, IGRAPH_WEAK);
149
3.47k
            if (b) {
150
273
                igraph_int_t no_comm = 4;
151
273
                if (no_comm > igraph_vcount(&graph)) {
152
95
                    no_comm = igraph_vcount(&graph);
153
95
                }
154
273
                igraph_community_fluid_communities(&graph, no_comm, &membership);
155
273
            }
156
157
3.47k
            igraph_vector_destroy(&v);
158
3.47k
            igraph_vector_destroy(&mv);
159
3.47k
            igraph_vector_int_destroy(&iv2);
160
3.47k
            igraph_vector_int_destroy(&iv);
161
3.47k
            igraph_vector_int_destroy(&membership2);
162
3.47k
            igraph_vector_int_destroy(&membership);
163
3.47k
            igraph_matrix_destroy(&mat);
164
3.47k
            igraph_matrix_int_destroy(&im);
165
3.47k
            igraph_matrix_int_destroy(&merges);
166
3.47k
        }
167
168
3.50k
        igraph_destroy(&graph);
169
3.50k
    }
170
171
3.50k
    igraph_vector_int_destroy(&edges);
172
173
3.50k
    IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY);
174
175
3.50k
    return 0;  // Non-zero return values are reserved for future use.
176
3.50k
}