/src/igraph/fuzzing/linear_algos_undirected.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 | | #include "fuzz_utilities.h" |
25 | | |
26 | 2.40k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
27 | 2.40k | igraph_t graph; |
28 | 2.40k | igraph_vector_int_t edges; |
29 | | |
30 | 2.40k | igraph_set_warning_handler(igraph_warning_handler_ignore); |
31 | | |
32 | 2.40k | if (Size % 2 == 0 || Size > 512+1 || Size < 1) { |
33 | 14 | return 0; |
34 | 14 | } |
35 | | |
36 | 2.38k | igraph_vector_int_init(&edges, Size-1); |
37 | 155k | for (size_t i=0; i < Size-1; ++i) { |
38 | 153k | VECTOR(edges)[i] = Data[i+1]; |
39 | 153k | } |
40 | | |
41 | | /* Undirected */ |
42 | 2.38k | if (igraph_create(&graph, &edges, Data[0], IGRAPH_UNDIRECTED) == IGRAPH_SUCCESS) { |
43 | 2.38k | igraph_vector_int_list_t ivl1, ivl2; |
44 | 2.38k | igraph_vector_int_t iv1, iv2, iv3, iv4, iv5; |
45 | 2.38k | igraph_graph_list_t gl; |
46 | 2.38k | igraph_vector_bool_t bv; |
47 | 2.38k | igraph_matrix_t m; |
48 | 2.38k | igraph_integer_t i, i2; |
49 | 2.38k | igraph_bool_t b, b2, loop, multi, graphical; |
50 | 2.38k | igraph_real_t r; |
51 | 2.38k | igraph_t g; |
52 | | |
53 | 2.38k | igraph_vector_int_list_init(&ivl1, 0); |
54 | 2.38k | igraph_vector_int_list_init(&ivl2, 0); |
55 | 2.38k | igraph_vector_int_init(&iv1, 0); |
56 | 2.38k | igraph_vector_int_init(&iv2, 0); |
57 | 2.38k | igraph_vector_int_init(&iv3, 0); |
58 | 2.38k | igraph_vector_int_init(&iv4, 0); |
59 | 2.38k | igraph_vector_int_init(&iv5, 0); |
60 | 2.38k | igraph_vector_bool_init(&bv, 0); |
61 | 2.38k | igraph_matrix_init(&m, 0, 0); |
62 | | |
63 | 2.38k | igraph_find_cycle(&graph, &iv1, &iv2, IGRAPH_ALL); |
64 | 2.38k | igraph_biconnected_components(&graph, &i, NULL, &ivl1, &ivl2, &iv1); |
65 | 2.38k | igraph_maximum_cardinality_search(&graph, &iv1, &iv2); |
66 | 2.38k | igraph_coreness(&graph, &iv1, IGRAPH_ALL); |
67 | 2.38k | igraph_girth(&graph, &r, &iv1); |
68 | 2.38k | igraph_bridges(&graph, &iv1); |
69 | 2.38k | igraph_assortativity_degree(&graph, &r, IGRAPH_UNDIRECTED); |
70 | 2.38k | igraph_count_multiple(&graph, &iv1, igraph_ess_all(IGRAPH_EDGEORDER_FROM)); |
71 | 2.38k | igraph_is_loop(&graph, &bv, igraph_ess_all(IGRAPH_EDGEORDER_TO)); |
72 | 2.38k | igraph_is_multiple(&graph, &bv, igraph_ess_all(IGRAPH_EDGEORDER_ID)); |
73 | 2.38k | igraph_maxdegree(&graph, &i, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS); |
74 | 2.38k | igraph_mean_degree(&graph, &r, IGRAPH_NO_LOOPS); |
75 | | |
76 | | /* Graphicality and graph realization based on the degrees of 'graph'. */ |
77 | 2.38k | igraph_has_loop(&graph, &loop); |
78 | 2.38k | igraph_has_multiple(&graph, &multi); |
79 | 2.38k | igraph_degree(&graph, &iv1, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS); |
80 | 2.38k | igraph_is_graphical(&iv1, NULL, IGRAPH_SIMPLE_SW, &b); |
81 | 2.38k | if (!loop && !multi) { |
82 | 1.26k | IGRAPH_ASSERT(b); |
83 | 1.26k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST); |
84 | 1.26k | igraph_destroy(&g); |
85 | 1.26k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST); |
86 | 1.26k | igraph_destroy(&g); |
87 | 1.26k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_INDEX); |
88 | 1.26k | igraph_destroy(&g); |
89 | 1.26k | } |
90 | 2.38k | igraph_is_graphical(&iv1, NULL, IGRAPH_LOOPS_SW, &b); |
91 | 2.38k | if (!multi) { |
92 | 1.55k | IGRAPH_ASSERT(b); |
93 | | /* Undirected realization is not yet implemented. */ |
94 | 1.55k | } |
95 | 2.38k | igraph_is_graphical(&iv1, NULL, IGRAPH_MULTI_SW, &b); |
96 | 2.38k | if (!loop) { |
97 | 1.51k | IGRAPH_ASSERT(b); |
98 | 1.51k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST); |
99 | 1.51k | igraph_destroy(&g); |
100 | 1.51k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST); |
101 | 1.51k | igraph_destroy(&g); |
102 | 1.51k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_INDEX); |
103 | 1.51k | igraph_destroy(&g); |
104 | 1.51k | } |
105 | 2.38k | igraph_is_graphical(&iv1, NULL, IGRAPH_LOOPS_SW | IGRAPH_MULTI_SW, &b); |
106 | 2.38k | IGRAPH_ASSERT(b); |
107 | 2.38k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_LOOPS_SW | IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST); |
108 | 2.38k | igraph_destroy(&g); |
109 | 2.38k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_LOOPS_SW | IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST); |
110 | 2.38k | igraph_destroy(&g); |
111 | 2.38k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_LOOPS_SW | IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_INDEX); |
112 | 2.38k | igraph_destroy(&g); |
113 | | |
114 | | /* Graphicality and graph realization based on the degrees of 'graph'. */ |
115 | 2.38k | igraph_has_loop(&graph, &loop); |
116 | 2.38k | igraph_has_multiple(&graph, &multi); |
117 | 2.38k | igraph_degree(&graph, &iv1, igraph_vss_all(), IGRAPH_ALL, IGRAPH_LOOPS); |
118 | 2.38k | igraph_is_graphical(&iv1, NULL, IGRAPH_SIMPLE_SW, &graphical); |
119 | 2.38k | if (!loop && !multi) { |
120 | 1.26k | IGRAPH_ASSERT(graphical); |
121 | 1.26k | } |
122 | 2.38k | if (graphical) { |
123 | 1.60k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST); |
124 | 1.60k | igraph_destroy(&g); |
125 | 1.60k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST); |
126 | 1.60k | igraph_destroy(&g); |
127 | 1.60k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_INDEX); |
128 | 1.60k | igraph_destroy(&g); |
129 | 1.60k | } else { |
130 | 781 | CHECK_ERROR( |
131 | 781 | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST), |
132 | 781 | IGRAPH_EINVAL); |
133 | 781 | CHECK_ERROR( |
134 | 781 | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST), |
135 | 781 | IGRAPH_EINVAL); |
136 | 781 | CHECK_ERROR( |
137 | 781 | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_INDEX), |
138 | 781 | IGRAPH_EINVAL); |
139 | 781 | } |
140 | 2.38k | igraph_is_graphical(&iv1, NULL, IGRAPH_LOOPS_SW, &graphical); |
141 | 2.38k | if (!multi) { |
142 | 1.55k | IGRAPH_ASSERT(graphical); |
143 | | /* Undirected realization is not yet implemented. */ |
144 | 1.55k | } |
145 | 2.38k | igraph_is_graphical(&iv1, NULL, IGRAPH_MULTI_SW, &graphical); |
146 | 2.38k | if (!loop) { |
147 | 1.51k | IGRAPH_ASSERT(graphical); |
148 | 1.51k | } |
149 | 2.38k | if (graphical) { |
150 | 1.92k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST); |
151 | 1.92k | igraph_destroy(&g); |
152 | 1.92k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST); |
153 | 1.92k | igraph_destroy(&g); |
154 | 1.92k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_INDEX); |
155 | 1.92k | igraph_destroy(&g); |
156 | 1.92k | } else { |
157 | 462 | CHECK_ERROR( |
158 | 462 | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST), |
159 | 462 | IGRAPH_EINVAL); |
160 | 462 | CHECK_ERROR( |
161 | 462 | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST), |
162 | 462 | IGRAPH_EINVAL); |
163 | 462 | CHECK_ERROR( |
164 | 462 | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_INDEX), |
165 | 462 | IGRAPH_EINVAL); |
166 | 462 | } |
167 | 2.38k | igraph_is_graphical(&iv1, NULL, IGRAPH_LOOPS_SW | IGRAPH_MULTI_SW, &graphical); |
168 | 2.38k | IGRAPH_ASSERT(graphical); |
169 | 2.38k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_LOOPS_SW | IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST); |
170 | 2.38k | igraph_destroy(&g); |
171 | 2.38k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_LOOPS_SW | IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST); |
172 | 2.38k | igraph_destroy(&g); |
173 | 2.38k | igraph_realize_degree_sequence(&g, &iv1, NULL, IGRAPH_LOOPS_SW | IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_INDEX); |
174 | 2.38k | igraph_destroy(&g); |
175 | | |
176 | | // These algorithms require a starting vertex, |
177 | | // so we require the graph to have at least one vertex. |
178 | 2.38k | if (igraph_vcount(&graph) >= 1) { |
179 | 2.38k | igraph_distances(&graph, &m, igraph_vss_1(0), igraph_vss_all(), IGRAPH_ALL); |
180 | 2.38k | igraph_get_shortest_paths(&graph, &ivl1, &ivl2, 0, igraph_vss_all(), IGRAPH_ALL, &iv1, &iv2); |
181 | 2.38k | igraph_pseudo_diameter(&graph, NULL, &r, 0, &i, &i2, false, true); |
182 | 2.38k | igraph_bfs(&graph, 0, NULL, IGRAPH_ALL, true, NULL, &iv1, &iv2, &iv3, &iv4, NULL, &iv5, NULL, NULL); |
183 | 2.38k | igraph_dfs(&graph, 0, IGRAPH_ALL, true, &iv1, &iv2, &iv3, &iv4, NULL, NULL, NULL); |
184 | 2.38k | igraph_bfs_simple(&graph, 0, IGRAPH_ALL, &iv1, &iv2, &iv3); |
185 | 2.38k | igraph_subcomponent(&graph, &iv1, 0, IGRAPH_ALL); |
186 | 2.38k | igraph_degree_1(&graph, &i, 0, IGRAPH_OUT, IGRAPH_LOOPS); |
187 | 2.38k | igraph_degree_1(&graph, &i, 0, IGRAPH_OUT, IGRAPH_NO_LOOPS); |
188 | 2.38k | } |
189 | | |
190 | 2.38k | if (igraph_vcount(&graph) >= 2) { |
191 | 2.36k | igraph_get_all_eids_between(&graph, &iv2, 1, 0, IGRAPH_UNDIRECTED); |
192 | 2.36k | igraph_get_all_eids_between(&graph, &iv2, 0, 0, IGRAPH_UNDIRECTED); |
193 | | |
194 | 2.36k | igraph_edges(&graph, igraph_ess_all(IGRAPH_EDGEORDER_FROM), &iv1, 0); |
195 | 2.36k | igraph_vector_int_push_back(&iv1, 0); |
196 | 2.36k | igraph_vector_int_push_back(&iv1, 1); |
197 | 2.36k | igraph_vector_int_push_back(&iv1, 1); |
198 | 2.36k | igraph_vector_int_push_back(&iv1, 1); |
199 | 2.36k | igraph_get_eids(&graph, &iv2, &iv1, IGRAPH_UNDIRECTED, false); |
200 | 2.36k | } |
201 | | |
202 | 2.38k | igraph_is_eulerian(&graph, &b, &b2); |
203 | 2.38k | if (b) igraph_eulerian_path(&graph, &iv1, &iv2); |
204 | 2.38k | if (b2) igraph_eulerian_cycle(&graph, &iv1, &iv2); |
205 | | |
206 | 2.38k | igraph_vertex_coloring_greedy(&graph, &iv1, IGRAPH_COLORING_GREEDY_COLORED_NEIGHBORS); |
207 | 2.38k | igraph_vertex_coloring_greedy(&graph, &iv1, IGRAPH_COLORING_GREEDY_DSATUR); |
208 | | |
209 | 2.38k | igraph_connected_components(&graph, &iv1, &iv2, &i, IGRAPH_WEAK); |
210 | 2.38k | igraph_minimum_spanning_tree(&graph, &iv1, NULL, IGRAPH_MST_UNWEIGHTED); |
211 | 2.38k | igraph_subgraph_from_edges(&graph, &g, igraph_ess_vector(&iv1), false); |
212 | 2.38k | if (i == 1 && igraph_vcount(&g) >= 2) { |
213 | | // 'g' is a tree (not a forest) when 'graph' had exactly one |
214 | | // connected component. |
215 | 138 | igraph_to_prufer(&g, &iv1); |
216 | | |
217 | 138 | igraph_t t; |
218 | 138 | igraph_from_prufer(&t, &iv1); |
219 | 138 | igraph_destroy(&t); |
220 | 138 | } |
221 | 2.38k | igraph_destroy(&g); |
222 | | |
223 | 2.38k | if (igraph_vcount(&graph) >= 1) { |
224 | 2.38k | igraph_vector_int_resize(&iv1, 1); |
225 | 2.38k | VECTOR(iv1)[0] = 0; |
226 | 2.38k | igraph_unfold_tree(&graph, &g, IGRAPH_ALL, &iv1, &iv2); |
227 | 2.38k | igraph_destroy(&g); |
228 | 2.38k | } |
229 | | |
230 | 2.38k | igraph_graph_list_init(&gl, 0); |
231 | 2.38k | igraph_decompose(&graph, &gl, IGRAPH_WEAK, -1, 4); |
232 | 2.38k | igraph_graph_list_destroy(&gl); |
233 | | |
234 | 2.38k | igraph_simplify(&graph, true, true, NULL); |
235 | | |
236 | 2.38k | if (igraph_vcount(&graph) >=1) { |
237 | | // Run only on the simplified graph to avoid a very large number of |
238 | | // shortest paths due to multi-edges. |
239 | 2.38k | igraph_get_all_shortest_paths(&graph, &ivl1, &ivl2, &iv1, 0, igraph_vss_all(), IGRAPH_ALL); |
240 | 2.38k | } |
241 | | |
242 | | /* Basic graph modification */ |
243 | 2.38k | igraph_add_vertices(&graph, 3, NULL); |
244 | 2.38k | igraph_degree_1(&graph, &i, 0, IGRAPH_ALL, IGRAPH_NO_LOOPS); |
245 | 2.38k | igraph_delete_vertices(&graph, igraph_vss_1(0)); |
246 | 2.38k | igraph_add_edge(&graph, 0, 1); |
247 | 2.38k | igraph_count_multiple_1(&graph, &i, 0); |
248 | 2.38k | igraph_delete_edges(&graph, igraph_ess_1(0)); |
249 | | |
250 | 2.38k | if (igraph_vcount(&graph) >= 4) { |
251 | 2.36k | igraph_rewire(&graph, igraph_ecount(&graph) + 1, IGRAPH_REWIRING_SIMPLE); |
252 | 2.36k | } |
253 | | |
254 | 2.38k | igraph_matrix_destroy(&m); |
255 | 2.38k | igraph_vector_bool_destroy(&bv); |
256 | 2.38k | igraph_vector_int_destroy(&iv5); |
257 | 2.38k | igraph_vector_int_destroy(&iv4); |
258 | 2.38k | igraph_vector_int_destroy(&iv3); |
259 | 2.38k | igraph_vector_int_destroy(&iv2); |
260 | 2.38k | igraph_vector_int_destroy(&iv1); |
261 | 2.38k | igraph_vector_int_list_destroy(&ivl2); |
262 | 2.38k | igraph_vector_int_list_destroy(&ivl1); |
263 | | |
264 | 2.38k | igraph_destroy(&graph); |
265 | 2.38k | } |
266 | | |
267 | 2.38k | igraph_vector_int_destroy(&edges); |
268 | | |
269 | 2.38k | IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY); |
270 | | |
271 | 2.38k | return 0; // Non-zero return values are reserved for future use. |
272 | 2.38k | } |