Coverage Report

Created: 2025-07-11 06:24

/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
3.13k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
25
3.13k
    igraph_t graph;
26
3.13k
    igraph_vector_int_t edges;
27
28
3.13k
    igraph_set_warning_handler(igraph_warning_handler_ignore);
29
30
3.13k
    if (Size % 2 == 0 || Size > 512+1 || Size < 1) {
31
14
        return 0;
32
14
    }
33
34
3.11k
    igraph_vector_int_init(&edges, Size-1);
35
204k
    for (size_t i=0; i < Size-1; ++i) {
36
201k
        VECTOR(edges)[i] = Data[i+1];
37
201k
    }
38
39
3.11k
    igraph_rng_seed(igraph_rng_default(), 42);
40
41
3.11k
    if (igraph_create(&graph, &edges, Data[0], IGRAPH_DIRECTED) == IGRAPH_SUCCESS) {
42
3.11k
        igraph_matrix_int_t merges, im;
43
3.11k
        igraph_matrix_t mat;
44
3.11k
        igraph_vector_int_t membership, membership2, iv, iv2;
45
3.11k
        igraph_vector_t mv, v;
46
3.11k
        igraph_real_t m, r;
47
3.11k
        igraph_integer_t i;
48
3.11k
        igraph_bool_t b;
49
50
        /* Limit graph size for the sake of performance. */
51
3.11k
        if (igraph_vcount(&graph) <= 64) {
52
3.08k
            igraph_matrix_int_init(&merges, 0, 0);
53
3.08k
            igraph_matrix_int_init(&im, 0, 0);
54
3.08k
            igraph_matrix_init(&mat, 0, 0);
55
3.08k
            igraph_vector_int_init(&membership, 0);
56
3.08k
            igraph_vector_int_init(&membership2, 0);
57
3.08k
            igraph_vector_int_init(&iv, 0);
58
3.08k
            igraph_vector_int_init(&iv2, 0);
59
3.08k
            igraph_vector_init(&mv, 0);
60
3.08k
            igraph_vector_init(&v, 0);
61
62
            // The IGRAPH_LPA_FAST version is temporarily switched to undirected until
63
            // the reason for the bad performance / infinite loop in the directed version
64
            // is debugged. See https://github.com/igraph/igraph/issues/2608
65
3.08k
            igraph_community_label_propagation(&graph, &membership, IGRAPH_ALL, NULL, NULL, NULL, IGRAPH_LPA_FAST);
66
3.08k
            igraph_community_label_propagation(&graph, &membership, IGRAPH_OUT, NULL, NULL, NULL, IGRAPH_LPA_RETENTION);
67
3.08k
            igraph_community_label_propagation(&graph, &membership, IGRAPH_OUT, NULL, NULL, NULL, IGRAPH_LPA_DOMINANCE);
68
69
3.08k
            igraph_community_walktrap(&graph, NULL, 3, &merges, &mv, &membership);
70
3.08k
            igraph_community_edge_betweenness(&graph, &iv, &v, &merges, &iv2, &mv, &membership2, IGRAPH_DIRECTED, NULL, NULL);
71
72
            // Take the opportunity to run functions that can use the output of community detection.
73
74
3.08k
            {
75
3.08k
                igraph_community_comparison_t method[] = {
76
3.08k
                    IGRAPH_COMMCMP_VI,
77
3.08k
                    IGRAPH_COMMCMP_NMI,
78
3.08k
                    IGRAPH_COMMCMP_SPLIT_JOIN,
79
3.08k
                    IGRAPH_COMMCMP_RAND,
80
3.08k
                    IGRAPH_COMMCMP_ADJUSTED_RAND
81
3.08k
                };
82
18.4k
                for (size_t i=0; i < sizeof(method) / sizeof(method[0]); i++) {
83
15.4k
                    if ((method[i] == IGRAPH_COMMCMP_RAND ||
84
15.4k
                         method[i] == IGRAPH_COMMCMP_ADJUSTED_RAND) &&
85
15.4k
                        igraph_vcount(&graph) < 2) {
86
22
                        continue;
87
22
                    }
88
15.3k
                    igraph_compare_communities(&membership, &membership2, &r, method[i]);
89
15.3k
                }
90
3.08k
            }
91
92
3.08k
            {
93
3.08k
                igraph_t graph2;
94
3.08k
                igraph_copy(&graph2, &graph);
95
3.08k
                igraph_contract_vertices(&graph2, &membership, NULL);
96
3.08k
                igraph_destroy(&graph2);
97
3.08k
            }
98
99
3.08k
            igraph_modularity(&graph, &membership, NULL, 1.5, IGRAPH_UNDIRECTED, &m);
100
3.08k
            igraph_modularity_matrix(&graph, NULL, 0.75, &mat, IGRAPH_DIRECTED);
101
3.08k
            igraph_assortativity_nominal(&graph, &membership, &r, IGRAPH_DIRECTED, true);
102
103
3.08k
            igraph_simplify(&graph, true, true, NULL);
104
3.08k
            igraph_community_voronoi(&graph, &membership, &iv, &m, NULL, NULL, IGRAPH_OUT, 1.0);
105
106
3.08k
            igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_COLLAPSE, NULL);
107
3.08k
            igraph_community_fastgreedy(&graph, NULL, &merges, &mv, &membership);
108
3.08k
            igraph_community_leiden(&graph, NULL, NULL, 1.5, 0.01, false, 2, &membership, &i, &r);
109
3.08k
            igraph_community_multilevel(&graph, NULL, 0.8, &membership, &im, &mv);
110
111
3.08k
            igraph_is_connected(&graph, &b, IGRAPH_WEAK);
112
3.08k
            if (b) {
113
176
                igraph_integer_t no_comm = 4;
114
176
                if (no_comm > igraph_vcount(&graph)) {
115
40
                    no_comm = igraph_vcount(&graph);
116
40
                }
117
176
                igraph_community_fluid_communities(&graph, no_comm, &membership);
118
176
            }
119
120
3.08k
            igraph_vector_destroy(&v);
121
3.08k
            igraph_vector_destroy(&mv);
122
3.08k
            igraph_vector_int_destroy(&iv2);
123
3.08k
            igraph_vector_int_destroy(&iv);
124
3.08k
            igraph_vector_int_destroy(&membership2);
125
3.08k
            igraph_vector_int_destroy(&membership);
126
3.08k
            igraph_matrix_destroy(&mat);
127
3.08k
            igraph_matrix_int_destroy(&im);
128
3.08k
            igraph_matrix_int_destroy(&merges);
129
3.08k
        }
130
131
3.11k
        igraph_destroy(&graph);
132
3.11k
    }
133
134
3.11k
    igraph_vector_int_destroy(&edges);
135
136
3.11k
    IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY);
137
138
3.11k
    return 0;  // Non-zero return values are reserved for future use.
139
3.11k
}