Coverage Report

Created: 2026-10-16 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/igraph/fuzzing/weighted_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 are connected subgraphs
25
13.5k
void check_communities_are_connected(const igraph_t *graph, const igraph_vector_int_t *membership) {
26
13.5k
    igraph_vector_int_t memb, comp, map;
27
13.5k
    igraph_int_t vcount = igraph_vcount(graph);
28
13.5k
    igraph_int_t no;
29
30
13.5k
    igraph_vector_int_init_copy(&memb, membership);
31
13.5k
    igraph_reindex_membership(&memb, NULL, &no);
32
33
13.5k
    igraph_vector_int_init(&map, no);
34
13.5k
    igraph_vector_int_fill(&map, -1);
35
36
13.5k
    igraph_vector_int_init(&comp, 0);
37
38
13.5k
    igraph_connected_components(graph, &comp, NULL, NULL, IGRAPH_WEAK);
39
40
638k
    for (igraph_int_t v=0; v < vcount; v++) {
41
625k
        if (VECTOR(map)[ VECTOR(memb)[v] ] == -1) {
42
617k
            VECTOR(map)[ VECTOR(memb)[v] ] = VECTOR(comp)[v];
43
617k
        } else {
44
7.52k
            IGRAPH_ASSERT(VECTOR(map)[ VECTOR(memb)[v] ] == VECTOR(comp)[v]);
45
7.52k
        }
46
625k
    }
47
48
13.5k
    igraph_vector_int_destroy(&comp);
49
13.5k
    igraph_vector_int_destroy(&map);
50
13.5k
    igraph_vector_int_destroy(&memb);
51
13.5k
}
52
53
3.44k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
54
3.44k
    igraph_t graph;
55
3.44k
    igraph_vector_int_t edges;
56
3.44k
    igraph_vector_t weights;
57
58
3.44k
    igraph_set_warning_handler(igraph_warning_handler_ignore);
59
60
3.44k
    if (Size % 3 == 0 || Size % 3 == 2 || Size > (3 * 256) + 1 || Size < 1) {
61
15
        return 0;
62
15
    }
63
64
3.43k
    igraph_vector_int_init(&edges, ((Size-1) / 3) * 2);
65
3.43k
    igraph_vector_init(&weights, (Size-1) / 3);
66
95.1k
    for (size_t i=0; i < ((Size-1) / 3); ++i) {
67
91.7k
        VECTOR(edges)[i * 2] = Data[i * 3 + 1];
68
91.7k
        VECTOR(edges)[i * 2 + 1] = Data[i * 3 + 2];
69
        // We keep the weights strictly positive, as this is required by some algorithms.
70
91.7k
        VECTOR(weights)[i] = ((double) Data[i * 3 + 3] + 1.0) / 105.0;
71
91.7k
    }
72
73
    // Turn on attribute handling.
74
3.43k
    igraph_set_attribute_table(&igraph_cattribute_table);
75
76
3.43k
    igraph_rng_seed(igraph_rng_default(), 42);
77
78
3.43k
    if (igraph_create(&graph, &edges, Data[0], IGRAPH_DIRECTED) == IGRAPH_SUCCESS) {
79
3.43k
        igraph_matrix_int_t merges, im;
80
3.43k
        igraph_matrix_t mat;
81
3.43k
        igraph_vector_int_t membership, membership2, iv, iv2;
82
3.43k
        igraph_vector_t mv, v;
83
3.43k
        igraph_real_t m, r;
84
3.43k
        igraph_int_t i;
85
3.43k
        igraph_bool_t b;
86
87
        /* Limit graph size for the sake of performance. */
88
3.43k
        if (igraph_vcount(&graph) <= 64) {
89
3.40k
            igraph_attribute_combination_t comb;
90
91
3.40k
            igraph_matrix_int_init(&merges, 0, 0);
92
3.40k
            igraph_matrix_int_init(&im, 0, 0);
93
3.40k
            igraph_matrix_init(&mat, 0, 0);
94
3.40k
            igraph_vector_int_init(&membership, 0);
95
3.40k
            igraph_vector_int_init(&membership2, 0);
96
3.40k
            igraph_vector_int_init(&iv, 0);
97
3.40k
            igraph_vector_int_init(&iv2, 0);
98
3.40k
            igraph_vector_init(&mv, 0);
99
3.40k
            igraph_vector_init(&v, 0);
100
101
3.40k
            SETEANV(&graph, "weight", &weights);
102
103
            // Currently "strength" is used only used as a vertex attribute to exercise the
104
            // attribute handling code durig vertex contraction, which is convenient to do
105
            // using the output from community detection. It is not used as input to any
106
            // community detection methods.
107
3.40k
            igraph_strength(&graph, &v, igraph_vss_all(), IGRAPH_OUT, IGRAPH_LOOPS, &weights);
108
3.40k
            SETVANV(&graph, "strength", &v);
109
110
3.40k
            igraph_attribute_combination(&comb,
111
3.40k
                                         "weight", IGRAPH_ATTRIBUTE_COMBINE_SUM,
112
3.40k
                                         "strength", IGRAPH_ATTRIBUTE_COMBINE_MAX,
113
3.40k
                                         IGRAPH_NO_MORE_ATTRIBUTES);
114
115
            // Ignore edge directions in label propagation for now, as on some weighted graphs the
116
            // algorithm seems to never complete. See https://github.com/igraph/igraph/issues/2561
117
3.40k
            igraph_community_label_propagation(&graph, &membership, IGRAPH_ALL, &weights, NULL, NULL, IGRAPH_LPA_FAST);
118
3.40k
            igraph_community_label_propagation(&graph, &membership, IGRAPH_ALL, &weights, NULL, NULL, IGRAPH_LPA_RETENTION);
119
3.40k
            igraph_community_label_propagation(&graph, &membership, IGRAPH_ALL, &weights, NULL, NULL, IGRAPH_LPA_DOMINANCE);
120
121
3.40k
            igraph_community_walktrap(&graph, &weights, 3, &merges, &mv, &membership);
122
3.40k
            igraph_community_edge_betweenness(&graph, &iv, &v, &merges, &iv2, &mv, &membership2, IGRAPH_DIRECTED, &weights, NULL);
123
124
3.40k
            igraph_community_leiden(&graph, &weights, NULL, NULL, 1.5, 0.01, false, 2, &membership, &i, &r);
125
3.40k
            check_communities_are_connected(&graph, &membership);
126
127
            // Take the opportunity to run functions that can use the output of community detection,
128
            // potentially with weights.
129
130
3.40k
            {
131
3.40k
                igraph_community_comparison_t method[] = {
132
3.40k
                    IGRAPH_COMMCMP_VI,
133
3.40k
                    IGRAPH_COMMCMP_NMI,
134
3.40k
                    IGRAPH_COMMCMP_SPLIT_JOIN,
135
3.40k
                    IGRAPH_COMMCMP_RAND,
136
3.40k
                    IGRAPH_COMMCMP_ADJUSTED_RAND
137
3.40k
                };
138
20.4k
                for (size_t i=0; i < sizeof(method) / sizeof(method[0]); i++) {
139
17.0k
                    if ((method[i] == IGRAPH_COMMCMP_RAND ||
140
13.6k
                         method[i] == IGRAPH_COMMCMP_ADJUSTED_RAND) &&
141
6.80k
                        igraph_vcount(&graph) < 2) {
142
30
                        continue;
143
30
                    }
144
16.9k
                    igraph_compare_communities(&membership, &membership2, &r, method[i]);
145
16.9k
                }
146
3.40k
            }
147
148
3.40k
            {
149
3.40k
                igraph_t graph2;
150
3.40k
                igraph_copy(&graph2, &graph);
151
3.40k
                igraph_contract_vertices(&graph2, &membership, &comb);
152
3.40k
                igraph_destroy(&graph2);
153
3.40k
            }
154
155
3.40k
            igraph_modularity(&graph, &membership, &weights, 1.5, IGRAPH_UNDIRECTED, &m);
156
3.40k
            igraph_modularity_matrix(&graph, &weights, 0.75, &mat, IGRAPH_DIRECTED);
157
158
            // NOTE: Currently Infomap dominates this fuzz target due to being
159
            // slower and more complex than the other algorithms.
160
            // TODO: Currently disabled because of extremely bad performance.
161
            // igraph_community_infomap(&graph, &weights, NULL, 1, &membership, &r);
162
163
3.40k
            igraph_simplify(&graph, true, true, &comb);
164
3.40k
            EANV(&graph, "weight", &weights);
165
166
            // Compute 'length' vector for community_voronoi()
167
3.40k
            igraph_vector_update(&v, &weights);
168
58.6k
            for (igraph_int_t i=0; i < igraph_vector_size(&v); i++) {
169
55.2k
                VECTOR(v)[i] = 1 / (1 + VECTOR(v)[i]);
170
55.2k
            }
171
3.40k
            igraph_community_voronoi(&graph, &membership, &iv, &m, &v, &weights, IGRAPH_OUT, 1.0);
172
173
3.40k
            igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_COLLAPSE, &comb);
174
3.40k
            EANV(&graph, "weight", &weights);
175
176
3.40k
            igraph_community_fastgreedy(&graph, &weights, &merges, &mv, &membership);
177
178
3.40k
            igraph_community_leiden(&graph, &weights, NULL, NULL, 1.5, 0.01, false, 2, &membership, &i, &r);
179
3.40k
            check_communities_are_connected(&graph, &membership);
180
181
3.40k
            igraph_community_multilevel(&graph, &weights, 0.8, &membership, &im, &mv);
182
183
            // community_spinglass() only works on connected graphs
184
3.40k
            igraph_is_connected(&graph, &b, IGRAPH_WEAK);
185
3.40k
            if (b) {
186
477
                igraph_community_spinglass(&graph, &weights, &r, NULL, &membership, NULL, 10, false, 1.0, 0.01, 0.99, IGRAPH_SPINCOMM_UPDATE_CONFIG, 1.0, IGRAPH_SPINCOMM_IMP_ORIG, 1.0);
187
477
            }
188
189
3.40k
            DELALL(&graph);
190
191
3.40k
            igraph_attribute_combination_destroy(&comb);
192
193
3.40k
            igraph_vector_destroy(&v);
194
3.40k
            igraph_vector_destroy(&mv);
195
3.40k
            igraph_vector_int_destroy(&iv2);
196
3.40k
            igraph_vector_int_destroy(&iv);
197
3.40k
            igraph_vector_int_destroy(&membership2);
198
3.40k
            igraph_vector_int_destroy(&membership);
199
3.40k
            igraph_matrix_destroy(&mat);
200
3.40k
            igraph_matrix_int_destroy(&im);
201
3.40k
            igraph_matrix_int_destroy(&merges);
202
3.40k
        }
203
204
3.43k
        igraph_destroy(&graph);
205
3.43k
    }
206
207
3.43k
    igraph_vector_int_destroy(&edges);
208
3.43k
    igraph_vector_destroy(&weights);
209
210
3.43k
    IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY);
211
212
3.43k
    return 0;  // Non-zero return values are reserved for future use.
213
3.43k
}