Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_exhaustive_generators_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
46#include <algorithm>
47#include <numeric>
48#include <set>
49#include <stdexcept>
50#include <string>
51#include <type_traits>
52#include <utility>
53#include <vector>
54
55#include <gtest/gtest.h>
56
57#include <tpl_graph.H>
58#include <tpl_sgraph.H>
59#include <tpl_agraph.H>
60#include <random_graph.H>
61
62#include "graph_harness.H"
63#include "graph_oracles.H"
64
65using namespace Aleph;
66using namespace graph_harness;
67namespace oracle = graph_oracles;
68
69namespace
70{
72 template <class GT>
73 struct Number_Nodes
74 {
75 int next = 0;
76 void operator () (GT &, typename GT::Node * p) { p->get_info() = next++; }
77 };
78
80 template <class GT>
81 struct Number_Arcs
82 {
83 int next = 0;
84 void operator () (GT &, typename GT::Arc * a) { a->get_info() = next++; }
85 };
86
88 template <class GT>
89 Spec spec_of(const GT & g)
90 {
91 Spec s;
92 s.n = int(g.get_num_nodes());
93 for (auto it = g.get_arc_it(); it.has_curr(); it.next_ne())
94 {
95 auto * a = it.get_curr();
96 s.arcs.push_back({num(g.get_src_node(a)), num(g.get_tgt_node(a))});
97 }
98 return s;
99 }
100
102 std::set<std::pair<int, int>> pairs_of(const Spec & s, bool directed)
103 {
104 std::set<std::pair<int, int>> r;
105 for (const auto & a : s.arcs)
106 r.insert(directed ? std::pair(a[0], a[1])
107 : std::pair(std::min(a[0], a[1]), std::max(a[0], a[1])));
108 return r;
109 }
110
113 std::string simple_error(const Spec & s, int n, bool directed)
114 {
115 if (s.n != n)
116 return std::to_string(s.n) + " nodes, not " + std::to_string(n);
117 for (const auto & a : s.arcs)
118 if (a[0] < 0 or a[0] >= n or a[1] < 0 or a[1] >= n)
119 return "a node is not numbered";
120 else if (a[0] == a[1])
121 return "a loop";
122 if (pairs_of(s, directed).size() != s.arcs.size())
123 return "parallel arcs";
124 return "";
125 }
126
129 bool connected(const Spec & s, bool directed)
130 {
131 const auto r = oracle::closure(s, directed);
132 for (int u = 0; u < s.n; ++u)
133 for (int v = 0; v < s.n; ++v)
134 if (not oracle::reaches(r, u, v))
135 return false;
136 return true;
137 }
138
144 bool can_be_made_eulerian(const Spec & s, bool directed)
145 {
146 if (directed)
147 return true;
148 const auto have = pairs_of(s, false);
149 Spec complement;
150 complement.n = s.n;
151 for (int u = 0; u < s.n; ++u)
152 for (int v = u + 1; v < s.n; ++v)
153 if (not have.count({u, v}))
154 complement.arcs.push_back({u, v});
155 const auto comp = oracle::component_of(complement);
156 std::vector<int> odd(size_t(s.n), 0);
157 for (int x = 0; x < s.n; ++x)
158 odd[size_t(comp[size_t(x)])] += oracle::degree(s, x) % 2;
159 return std::all_of(odd.begin(), odd.end(), [] (int k) { return k % 2 == 0; });
160 }
161
163 bool ore(const Spec & s)
164 {
165 if (s.n < 3)
166 return false;
167 const auto have = pairs_of(s, false);
168 for (int u = 0; u < s.n; ++u)
169 for (int v = u + 1; v < s.n; ++v)
170 if (not have.count({u, v}) and oracle::degree(s, u) + oracle::degree(s, v) < s.n)
171 return false;
172 return true;
173 }
174
176 bool hamiltonian(const Spec & s)
177 {
178 if (s.n < 3)
179 return false;
180 const auto have = pairs_of(s, false);
181 std::vector<int> order(static_cast<size_t>(s.n));
182 std::iota(order.begin(), order.end(), 0);
183 do
184 {
185 bool ok = true;
186 for (int i = 0; i < s.n and ok; ++i)
187 {
188 const int u = order[size_t(i)], v = order[size_t((i + 1) % s.n)];
189 ok = have.count({std::min(u, v), std::max(u, v)}) > 0;
190 }
191 if (ok)
192 return true;
193 }
194 while (std::next_permutation(order.begin() + 1, order.end()));
195 return false;
196 }
197
201 template <class GT, template <class, class, class> class Gen>
202 void check_generators(bool directed, int max_nodes, int seeds)
203 {
204 using Make = Gen<GT, Number_Nodes<GT>, Number_Arcs<GT>>;
205 Check sparse("operator()(n, m, connected)");
206 Check dense("operator()(n, p, connected)");
207 // connect() and eulerian() picked among the nodes in the order of their
208 // addresses, which List_SGraph keeps, so that on it the same seed gave
209 // another graph in another run
210 Check same("the same seed gives the same graph");
211 Check eulerian_m("eulerian(n, m)");
212 Check eulerian_p("eulerian(n, p)");
213 // The graph that eulerian() starts from, which these judge it by, is
214 // the one that the seed gives
215 Check kept_m("eulerian(n, m), from the graph that the seed gives");
216 Check kept_p("eulerian(n, p), from the graph that the seed gives");
217 Check hamilton("sufficient_hamiltonian(n, p)");
218
219 // A graph made with a seed, by a generator of its own
220 auto make = [] (unsigned long seed, auto && f)
221 {
222 Number_Nodes<GT> nodes;
223 Number_Arcs<GT> arcs;
224 Make gen(seed, nodes, arcs);
225 return spec_of(f(gen));
226 };
227
228 for (int n = 2; n <= max_nodes; ++n)
229 {
230 const int pairs = directed ? n * (n - 1) : n * (n - 1) / 2;
231 for (int m = 0; m <= pairs; m += 1 + pairs / 8)
232 for (unsigned long seed = 1; seed <= unsigned(seeds); ++seed)
233 for (bool conn : {false, true})
234 {
235 const std::string what = "n=" + std::to_string(n) + " m=" + std::to_string(m)
236 + " seed=" + std::to_string(seed) + (conn ? " connected" : "") + ": ";
237 Spec at;
238 at.n = n;
239
240 sparse.run();
241 const Spec s = make(seed, [&] (Make & g) { return g(size_t(n), size_t(m), conn); });
242 if (auto e = simple_error(s, n, directed); not e.empty())
243 sparse.fail(at, what + e);
244 else if (conn and not connected(s, directed))
245 sparse.fail(at, what + "not connected");
246
247 same.run();
248 if (pairs_of(make(seed, [&] (Make & g) { return g(size_t(n), size_t(m), conn); }),
249 directed) != pairs_of(s, directed))
250 same.fail(at, what + "another graph");
251 }
252
253 for (double p : {0.1, 0.3, 0.5, 0.8, 1.0})
254 for (unsigned long seed = 1; seed <= unsigned(seeds); ++seed)
255 {
256 const std::string what = "n=" + std::to_string(n) + " p=" + std::to_string(p)
257 + " seed=" + std::to_string(seed) + ": ";
258 Spec at;
259 at.n = n;
260
261 dense.run();
262 const Spec s = make(seed, [&] (Make & g) { return g(size_t(n), p, true); });
263 if (auto e = simple_error(s, n, directed); not e.empty())
264 dense.fail(at, what + e);
265 else if (not connected(s, directed))
266 dense.fail(at, what + "not connected");
267 else if (p == 1.0 and int(s.arcs.size()) != (directed ? n * (n - 1) : n * (n - 1) / 2))
268 dense.fail(at, what + "p = 1 without every arc");
269
270 if (not directed and n >= 3)
271 {
272 hamilton.run();
273 const Spec h = make(seed, [&] (Make & g) { return g.sufficient_hamiltonian(size_t(n), p); });
274 if (auto e = simple_error(h, n, false); not e.empty())
275 hamilton.fail(at, what + e);
276 else if (not ore(h) or not hamiltonian(h))
277 hamilton.fail(at, what + "neither Ore's condition nor a Hamilton cycle");
278 }
279 }
280
281 // eulerian() starts from the connected graph that the seed gives,
282 // which the test can make again if the seed gives the same graph
283 auto check_eulerian = [&] (Check & check, Check & kept, const std::string & what,
284 const Spec & base, bool known_base, auto && f)
285 {
286 Spec at;
287 at.n = n;
288 check.run();
289 try
290 {
291 const Spec e = f();
292 if (auto err = simple_error(e, n, directed); not err.empty())
293 check.fail(at, what + err);
294 else if (oracle::euler_class(e, directed) != oracle::Euler::Cycle
295 or not connected(e, directed))
296 check.fail(at, what + "not Eulerian");
297 else if (known_base)
298 {
299 kept.run();
300 const auto have = pairs_of(e, directed);
301 for (const auto & pr : pairs_of(base, directed))
302 if (not have.count(pr))
303 {
304 kept.fail(at, what + "an arc of the graph it starts from is gone");
305 break;
306 }
307 }
308 }
309 catch (const std::domain_error &)
310 {
311 if (known_base)
312 {
313 kept.run();
314 if (can_be_made_eulerian(base, directed))
315 kept.fail(at, what + "throws, but arcs added would do");
316 }
317 }
318 };
319
320 for (int m = 0; m <= pairs; m += 1 + pairs / 8)
321 for (unsigned long seed = 1; seed <= unsigned(seeds); ++seed)
322 {
323 const std::string what = "n=" + std::to_string(n) + " m=" + std::to_string(m)
324 + " seed=" + std::to_string(seed) + ": ";
325 const Spec base = make(seed, [&] (Make & g) { return g(size_t(n), size_t(m), true); });
326 const bool known_base = pairs_of(base, directed) == pairs_of
327 (make(seed, [&] (Make & g) { return g(size_t(n), size_t(m), true); }), directed);
328 check_eulerian(eulerian_m, kept_m, what, base, known_base, [&]
329 {
330 return make(seed, [&] (Make & g) { return g.eulerian(size_t(n), size_t(m)); });
331 });
332 }
333
334 for (double p : {0.1, 0.3, 0.5, 0.8, 1.0})
335 for (unsigned long seed = 1; seed <= unsigned(seeds); ++seed)
336 {
337 const std::string what = "n=" + std::to_string(n) + " p=" + std::to_string(p)
338 + " seed=" + std::to_string(seed) + ": ";
339 const Spec base = make(seed, [&] (Make & g) { return g(size_t(n), p, true); });
340 const bool known_base = pairs_of(base, directed) == pairs_of
341 (make(seed, [&] (Make & g) { return g(size_t(n), p, true); }), directed);
342 check_eulerian(eulerian_p, kept_p, what, base, known_base, [&]
343 {
344 return make(seed, [&] (Make & g) { return g.eulerian(size_t(n), p); });
345 });
346 }
347 }
348
349 sparse.expect();
350 dense.expect();
351 same.expect();
352 eulerian_m.expect();
353 eulerian_p.expect();
354 kept_m.expect();
355 kept_p.expect();
356 hamilton.expect();
357 }
358}
359
360template <class GT>
361class ExhaustiveGenerators : public ::testing::Test {};
362
364
365TYPED_TEST(ExhaustiveGenerators, RandomGraphsKeepTheirPromises)
366{
367 check_generators<TypeParam, Random_Graph>(false, heavy() ? 9 : 8, heavy() ? 60 : 12);
368}
369
370template <class GT>
371class ExhaustiveDigraphGenerators : public ::testing::Test {};
372
374
375TYPED_TEST(ExhaustiveDigraphGenerators, RandomDigraphsKeepTheirPromises)
376{
377 check_generators<TypeParam, Random_Digraph>(true, heavy() ? 8 : 7, heavy() ? 60 : 12);
378}
long double h
Definition btreepic.C:154
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Definition graph-dry.H:3144
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
The failures of one property over a domain.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4189
TYPED_TEST(ExhaustiveGenerators, RandomGraphsKeepTheirPromises)
TYPED_TEST_SUITE(ExhaustiveGenerators, Undirected_Kinds)
Every small multigraph, on every kind of graph, for the tests.
Answers computed from a Spec of graph_harness.H, without Aleph.
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
and
Check uniqueness with explicit hash + equality functors.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
::testing::Types< Aleph::List_Digraph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SDigraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Digraph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Directed_Kinds
The directed kinds, with int infos.
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Undirected_Kinds
The kinds of graph that are not directed, with int infos.
int num(Node *p)
The number of a node of a built graph.
bool heavy()
Whether the mode is heavy, which asks for the larger domains.
Euler euler_class(const Spec &s, bool directed)
The Eulerian class of s: the arcs must be in one component (weakly, in a digraph),...
std::vector< std::vector< bool > > closure(const Spec &s, bool directed)
reach[u][v]: v is reached from u by a directed path of one arc or more (in a graph that is not direct...
std::vector< int > component_of(const Spec &s, int skip_node=-1, int skip_arc=-1)
The number of the component of each node, as that of its root, of the graph without the node skip_nod...
bool reaches(const std::vector< std::vector< bool > > &r, int u, int v)
Whether there is a path, of zero arcs or more, from u to v.
int degree(const Spec &s, int x)
The degree of x: a loop counts 2.
STL namespace.
Random graph generation utilities.
A small multigraph: its nodes are 0 .
std::vector< std::array< int, 2 > > arcs
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
bool check(GT &g)
ValueArg< size_t > seed
Definition testHash.C:53
static int * k
Gsl_Compatible_Rng r
Array-based graph implementation.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.