Coverage Report

Created: 2025-11-14 06:33

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