Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
random_graph_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
48#include <gtest/gtest.h>
49
50#include <stdexcept>
51
52#include <random_graph.H>
53#include <tpl_dynMapTree.H>
54#include <tpl_dynSetTree.H>
55#include <tpl_graph.H>
56
57using namespace Aleph;
58
59namespace
60{
62 using Digraph_Type = List_Digraph<Graph_Node<int>, Graph_Arc<int>>;
63
64 // The ends of each arc are nodes of g: a node of another graph that a
65 // generator kept was taken as an end
66 template <class GT>
67 bool arcs_inside(const GT & g)
68 {
70 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
71 nodes.insert(it.get_curr_ne());
72 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
73 {
74 auto a = it.get_curr_ne();
75 if (not nodes.contains(g.get_src_node(a)) or
76 not nodes.contains(g.get_tgt_node(a)))
77 return false;
78 }
79 return true;
80 }
81
82 // A directed arc is only in its source
83 template <class GT>
84 bool linked_as_digraph(const GT & g)
85 {
86 size_t in_nodes = 0;
87 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
88 in_nodes += g.get_num_arcs(it.get_curr_ne());
89 return g.is_digraph() and in_nodes == g.get_num_arcs();
90 }
91
92 template <class GT>
93 bool all_degrees_even(const GT & g)
94 {
95 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
96 if (g.get_num_arcs(it.get_curr_ne()) % 2 != 0)
97 return false;
98 return true;
99 }
100
101 template <class GT>
102 DynMapTree<typename GT::Node *, size_t> in_degrees(const GT & g)
103 {
105 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
106 in.insert(it.get_curr_ne(), 0);
107 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
108 ++in.find(g.get_tgt_node(it.get_curr_ne()));
109 return in;
110 }
111
112 // The in-degree of each node is its out-degree
113 template <class GT>
114 bool balanced(const GT & g)
115 {
116 auto in = in_degrees(g);
117 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
118 if (in.find(it.get_curr_ne()) != g.get_num_arcs(it.get_curr_ne()))
119 return false;
120 return true;
121 }
122
123 // What sufficient_hamiltonian() ensures of a digraph: out(u) + in(v) >= n
124 // for each pair without the arc u --> v
125 template <class GT>
126 bool hamiltonian_degrees(const GT & g)
127 {
128 const size_t n = g.get_num_nodes();
129 auto in = in_degrees(g);
131 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
132 arcs.insert({g.get_src_node(it.get_curr_ne()),
133 g.get_tgt_node(it.get_curr_ne())});
134 for (typename GT::Node_Iterator i(g); i.has_curr(); i.next_ne())
135 for (typename GT::Node_Iterator j(g); j.has_curr(); j.next_ne())
136 {
137 auto u = i.get_curr_ne(), v = j.get_curr_ne();
138 if (u != v and not arcs.contains({u, v}) and
139 g.get_num_arcs(u) + in.find(v) < n)
140 return false;
141 }
142 return true;
143 }
144
145 template <class GT>
146 struct Throw_At_Arc
147 {
148 int left = 0; // the arc that throws; 0 for none
149
150 void operator ()(GT &, typename GT::Arc *)
151 {
152 if (left > 0 and --left == 0)
153 throw std::runtime_error("init_arc");
154 }
155 };
156}
157
158// The second digraph, and the next ones, were undirected
159TEST(RandomGraphReuse, AnUndirectedTypeGetsADigraphEachTime)
160{
162 for (int i = 0; i < 3; ++i)
163 {
164 SCOPED_TRACE(i);
165 Graph sparse = gen(size_t(20), size_t(60), false);
166 EXPECT_TRUE(linked_as_digraph(sparse));
167 Graph dense = gen(size_t(12), 0.3, false);
168 EXPECT_TRUE(linked_as_digraph(dense));
169 EXPECT_EQ(dense.get_num_nodes(), 12u);
170 }
171}
172
173// The parities of the nodes of the former graphs, and the graphs built
174// after an Eulerian one, went into the next Eulerian graphs, whose arcs
175// then reached nodes of other graphs; all the graphs stay alive
176TEST(RandomGraphReuse, EachEulerianGraphIsEulerian)
177{
178 Random_Graph<Graph> gen(7);
179 DynList<Graph> graphs;
180 for (int i = 0; i < 20; ++i)
181 {
182 SCOPED_TRACE(i);
183 auto & e1 = graphs.append(gen.eulerian(size_t(30), size_t(40)));
184 auto & g = graphs.append(gen(size_t(30), size_t(40)));
185 auto & e2 = graphs.append(gen.eulerian(size_t(16), 0.2));
186 ASSERT_TRUE(arcs_inside(e1) and arcs_inside(g) and arcs_inside(e2));
187 EXPECT_TRUE(all_degrees_even(e1));
188 EXPECT_TRUE(all_degrees_even(e2));
189 EXPECT_EQ(g.get_num_nodes(), 30u);
190 }
191}
192
193TEST(RandomGraphReuse, EachEulerianDigraphIsBalanced)
194{
197 for (int i = 0; i < 20; ++i)
198 {
199 SCOPED_TRACE(i);
200 auto & e1 = graphs.append(gen.eulerian(size_t(30), size_t(40)));
201 auto & g = graphs.append(gen(size_t(30), size_t(40)));
202 auto & e2 = graphs.append(gen.eulerian(size_t(16), 0.2));
203 ASSERT_TRUE(arcs_inside(e1) and arcs_inside(g) and arcs_inside(e2));
204 EXPECT_TRUE(balanced(e1));
205 EXPECT_TRUE(balanced(e2));
206 EXPECT_EQ(g.get_num_nodes(), 30u);
207 }
208}
209
210// eulerian() left the tracking of the parities on, and then the
211// Hamiltonian digraph counted each in-degree twice
212TEST(RandomGraphReuse, AHamiltonianDigraphAfterAnEulerianOne)
213{
215 for (int i = 0; i < 3; ++i)
216 {
217 SCOPED_TRACE(i);
218 Digraph_Type e = gen.eulerian(size_t(20), size_t(40));
219 EXPECT_TRUE(balanced(e));
220 Digraph_Type h = gen.sufficient_hamiltonian(size_t(14), 0.2);
221 EXPECT_TRUE(arcs_inside(h));
222 EXPECT_TRUE(hamiltonian_degrees(h));
223 }
224}
225
226namespace
227{
228 template <class GT>
229 using Throwing_Digraphs =
231
232 template <class GT>
233 using Throwing_Graphs =
235
236 // A graph that throws halfway stays in the generator: it removed the
237 // arcs of a digraph as those of an undirected graph when it was
238 // destroyed, and gave the nodes to the next graph
239 template <class GT, class Generator>
240 void a_graph_after_one_that_threw(const bool digraph)
241 {
242 Dft_Init_Rand_Node<GT> init_node;
243 Throw_At_Arc<GT> init_arc;
244 {
245 Generator gen(7, init_node, init_arc);
246 init_arc.left = 10;
247 EXPECT_THROW(gen(size_t(20), size_t(60), false), std::runtime_error);
248 }
249
250 Generator gen(7, init_node, init_arc);
251 init_arc.left = 10;
252 EXPECT_THROW(gen(size_t(20), size_t(60), false), std::runtime_error);
253 init_arc.left = 0;
254 GT g = gen(size_t(20), size_t(60), false);
255 EXPECT_EQ(g.get_num_nodes(), 20u);
256 EXPECT_TRUE(arcs_inside(g));
257 EXPECT_EQ(g.is_digraph(), digraph);
258 }
259}
260
261TEST(RandomGraphReuse, AGraphAfterOneThatThrew)
262{
263 using D = Digraph_Type;
264 a_graph_after_one_that_threw<D, Throwing_Digraphs<D>>(true);
265 a_graph_after_one_that_threw<Graph, Throwing_Digraphs<Graph>>(true);
266 a_graph_after_one_that_threw<Graph, Throwing_Graphs<Graph>>(false);
267}
268
269// The odd nodes of K4 are all adjacent, and no arc can go in: the pairs
270// had no end
271TEST(EulerianGraphs, ACompleteGraphOfEvenOrderIsRejected)
272{
273 Random_Graph<Graph> gen(7);
274 EXPECT_THROW(gen.eulerian(size_t(4), 1.0), std::domain_error);
275 Graph k5 = gen.eulerian(size_t(5), 1.0);
276 EXPECT_EQ(k5.get_num_arcs(), 10u);
277 EXPECT_TRUE(all_degrees_even(k5));
278}
279
280// Graphs from a new generator each: a sparse one is always made Eulerian;
281// a small or dense one may not be by adding arcs, as when a node of odd
282// degree is adjacent to all the others, and then it throws, but it does
283// not abort nor search with no end
284TEST(EulerianGraphs, FromSparseToDense)
285{
286 for (unsigned long seed = 1; seed <= 150; ++seed)
287 {
288 SCOPED_TRACE(seed);
289 const size_t n = 20 + seed % 20;
290 {
292 Digraph_Type e = gen.eulerian(n, n + seed % n);
293 EXPECT_TRUE(balanced(e) and arcs_inside(e));
294 }
295 {
297 Graph e = gen.eulerian(n, n + seed % n);
298 EXPECT_TRUE(all_degrees_even(e) and arcs_inside(e));
299 }
300
301 const size_t small = 4 + seed % 10;
302 for (double p : {0.3, 0.7, 0.95})
303 {
304 try
305 {
307 Digraph_Type e = gen.eulerian(small, p);
308 EXPECT_TRUE(balanced(e) and arcs_inside(e));
309 }
310 catch (const std::domain_error &) {}
311 try
312 {
314 Graph e = gen.eulerian(small, p);
315 EXPECT_TRUE(all_degrees_even(e) and arcs_inside(e));
316 }
317 catch (const std::domain_error &) {}
318 }
319 }
320}
long double h
Definition btreepic.C:154
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Generic key-value map implemented on top of a binary search tree.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Lazy, single-pass sequence of T values produced by a coroutine.
Random directed graph (digraph) generator.
GT eulerian(const size_t &__num_nodes, const size_t &__num_arcs)
Create a random Eulerian graph (sparse version).
Random undirected graph generator.
GT eulerian(const size_t &__num_nodes, const size_t &__num_arcs)
Create a random Eulerian graph (sparse version).
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
#define TEST(name)
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
DynList< typename GT::Node * > in_nodes(typename GT::Node *p, SA sa=SA())
Return the nodes connected to the filtered incoming arcs to p.
Definition tpl_graph.H:2606
GT sufficient_hamiltonian(const size_t &__num_nodes, const double &p=0.5)
Create a random Hamiltonian graph.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Random graph generation utilities.
Default node initializer for random graph generation.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
ValueArg< size_t > seed
Definition testHash.C:53
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.