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