48#include <gtest/gtest.h>
67 bool arcs_inside(
const GT & g)
70 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
72 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
74 auto a = it.get_curr_ne();
84 bool linked_as_digraph(
const GT & g)
87 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
93 bool all_degrees_even(
const GT & g)
95 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
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())
114 bool balanced(
const GT & g)
116 auto in = in_degrees(g);
117 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
126 bool hamiltonian_degrees(
const GT & g)
129 auto in = in_degrees(g);
131 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_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())
137 auto u = i.get_curr_ne(), v = j.get_curr_ne();
150 void operator ()(
GT &,
typename GT::Arc *)
152 if (left > 0
and --left == 0)
153 throw std::runtime_error(
"init_arc");
159TEST(RandomGraphReuse, AnUndirectedTypeGetsADigraphEachTime)
162 for (
int i = 0; i < 3; ++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));
176TEST(RandomGraphReuse, EachEulerianGraphIsEulerian)
180 for (
int i = 0; i < 20; ++i)
184 auto & g = graphs.
append(gen(
size_t(30),
size_t(40)));
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));
193TEST(RandomGraphReuse, EachEulerianDigraphIsBalanced)
197 for (
int i = 0; i < 20; ++i)
201 auto & g = graphs.
append(gen(
size_t(30),
size_t(40)));
203 ASSERT_TRUE(arcs_inside(e1)
and arcs_inside(g)
and arcs_inside(e2));
204 EXPECT_TRUE(balanced(e1));
205 EXPECT_TRUE(balanced(e2));
212TEST(RandomGraphReuse, AHamiltonianDigraphAfterAnEulerianOne)
215 for (
int i = 0; i < 3; ++i)
218 Digraph_Type e = gen.
eulerian(
size_t(20),
size_t(40));
219 EXPECT_TRUE(balanced(e));
221 EXPECT_TRUE(arcs_inside(
h));
222 EXPECT_TRUE(hamiltonian_degrees(
h));
229 using Throwing_Digraphs =
233 using Throwing_Graphs =
239 template <
class GT,
class Generator>
240 void a_graph_after_one_that_threw(
const bool digraph)
243 Throw_At_Arc<GT> init_arc;
247 EXPECT_THROW(gen(
size_t(20),
size_t(60),
false), std::runtime_error);
252 EXPECT_THROW(gen(
size_t(20),
size_t(60),
false), std::runtime_error);
254 GT g = gen(
size_t(20),
size_t(60),
false);
256 EXPECT_TRUE(arcs_inside(g));
261TEST(RandomGraphReuse, AGraphAfterOneThatThrew)
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);
271TEST(EulerianGraphs, ACompleteGraphOfEvenOrderIsRejected)
274 EXPECT_THROW(gen.
eulerian(
size_t(4), 1.0), std::domain_error);
277 EXPECT_TRUE(all_degrees_even(k5));
284TEST(EulerianGraphs, FromSparseToDense)
289 const size_t n = 20 +
seed % 20;
293 EXPECT_TRUE(balanced(e)
and arcs_inside(e));
298 EXPECT_TRUE(all_degrees_even(e)
and arcs_inside(e));
301 const size_t small = 4 +
seed % 10;
302 for (
double p : {0.3, 0.7, 0.95})
307 Digraph_Type e = gen.
eulerian(small, p);
308 EXPECT_TRUE(balanced(e)
and arcs_inside(e));
310 catch (
const std::domain_error &) {}
315 EXPECT_TRUE(all_degrees_even(e)
and arcs_inside(e));
317 catch (
const std::domain_error &) {}
Generic directed graph (digraph) wrapper template.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
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.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
bool is_digraph() const noexcept
Return true if the graph this is directed.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
DynList< typename GT::Node * > in_nodes(typename GT::Node *p, SA sa=SA())
Return the nodes connected to the filtered incoming arcs to p.
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.
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.