48#include <gtest/gtest.h>
70 std::set<int> numbers(
const List &
l)
73 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
74 r.insert(
num(it.get_curr()));
78 std::string text(
const std::set<int> & s)
87 std::vector<int> starts(
const Spec & s)
101 std::string subgraph_error(
Built<GT> & b,
GT & sg,
const std::set<int> & want_nodes,
102 const std::set<int> & want_arcs)
104 std::set<int> got_nodes, got_arcs;
105 for (
auto it = sg.
get_node_it(); it.has_curr(); it.next_ne())
107 auto q = it.get_curr();
108 const int x =
num(q);
110 if (x < 0 or
size_t(x) >= b.
nodes.size() or mapped_node<GT>(q) != b.
nodes[
size_t(x)])
111 return "the copy of " + std::to_string(x) +
" maps to another node";
112 if (mapped_node<GT>(b.
nodes[
size_t(x)]) != q)
113 return "the node " + std::to_string(x) +
" does not map to its copy";
115 for (
auto it = sg.
get_arc_it(); it.has_curr(); it.next_ne())
117 auto c = it.get_curr();
118 const int i = c->get_info();
120 if (i < 0 or
size_t(i) >= b.
arcs.size() or mapped_arc<GT>(c) != b.
arcs[size_t(i)])
121 return "the copy of the arc " + std::to_string(i) +
" maps to another arc";
122 auto a = b.
arcs[size_t(i)];
123 if (mapped_arc<GT>(a) != c)
124 return "the arc " + std::to_string(i) +
" does not map to its copy";
129 if (not ((src == g_src
and tgt == g_tgt) or (src == g_tgt
and tgt == g_src)))
130 return "the copy of the arc " + std::to_string(i) +
" joins other nodes";
132 if (got_nodes != want_nodes or sg.
get_num_nodes() != want_nodes.size())
133 return "the nodes are " + text(got_nodes) +
", not " + text(want_nodes);
134 if (got_arcs != want_arcs or sg.
get_num_arcs() != want_arcs.size())
135 return "the arcs are " + text(got_arcs) +
", not " + text(want_arcs);
147 using GT = TypeParam;
148 Check count(
"Unconnected_Components::count_components()");
149 Check lists(
"Unconnected_Components::operator()");
150 Check inconnected(
"inconnected_components()");
151 Check connectivity(
"test_connectivity()");
161 count.fail(s, std::to_string(c) +
" components, not " + std::to_string(want));
163 auto total_arcs = [] (
const auto &
l)
166 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
167 arcs += it.get_curr().get_num_arcs();
175 lists.
fail(s, std::to_string(
l.
size()) +
" components, "
176 + std::to_string(total_arcs(
l)) +
" arcs");
180 if (
int(ic.size()) != want or total_arcs(ic) != s.
num_arcs())
181 inconnected.
fail(s, std::to_string(ic.size()) +
" components, "
182 + std::to_string(total_arcs(ic)) +
" arcs");
186 if (connected != (s.
n > 0
and want == 1))
187 connectivity.
fail(s, std::string(
"says ") + (connected ?
"" :
"not ") +
"connected");
201 using GT = TypeParam;
202 Check clear(
"Build_Subgraph");
203 Check stale(
"Build_Subgraph, with cookies toward a graph that went");
204 Check listed(
"Build_Subgraph, into a list of the nodes");
209 for (
int start : starts(s))
211 std::set<int> want_nodes, want_arcs;
212 for (
int x = 0; x < s.
n; ++x)
213 if (comp[
size_t(x)] == comp[size_t(start)])
214 want_nodes.insert(x);
215 for (
size_t i = 0; i < s.
arcs.size(); ++i)
216 if (comp[
size_t(s.
arcs[i][0])] == comp[size_t(start)])
217 want_arcs.insert(
int(i));
219 for (
const bool old_cookies : {
false,
true})
229 for (
auto p : b.
nodes)
231 for (
auto a : b.
arcs)
236 if (
auto e = subgraph_error(b, sg, want_nodes, want_arcs); not e.empty())
237 check.fail(s,
"from " + std::to_string(start) +
": " + e);
244 if (
const auto got = numbers(
l); got != want_nodes or
l.
size() != want_nodes.size())
245 listed.
fail(s,
"from " + std::to_string(start) +
" gives " + text(got) +
", not "
257 using GT = TypeParam;
258 Check function(
"compute_cut_nodes()");
259 Check object(
"Compute_Cut_Nodes");
260 Check bridges(
"find_bridges()");
266 for (
int start : starts(s))
269 auto * src = b.
nodes[size_t(start)];
274 function.
fail(s,
"from " + std::to_string(start) +
" gives " + text(got_f)
275 +
", not " + text(want));
281 const auto got_o = numbers(
l);
283 object.fail(s,
"from " + std::to_string(start) +
" gives " + text(got_o)
284 +
", not " + text(want));
289 for (
auto it = lb.get_it(); it.has_curr(); it.next_ne())
290 got_b.insert(
num(it.get_curr()));
291 if (got_b != want_bridges)
292 bridges.fail(s,
"from " + std::to_string(start) +
" gives " + text(got_b)
293 +
", not " + text(want_bridges));
304 using GT = TypeParam;
305 Check acyclique(
"is_graph_acyclique()");
307 Check through(
"test_for_cycle(g, start)");
316 acyclique.
fail(s, cycle ?
"says acyclic" :
"says cyclic");
320 has_cycle.fail(s, cycle ?
"finds no cycle" :
"finds a cycle");
322 for (
int start : starts(s))
327 through.
fail(s,
"from " + std::to_string(start)
328 + (want ?
" finds no cycle" :
" finds a cycle"));
339 using GT = TypeParam;
340 Check bipartite(
"compute_bipartite()");
341 Check hopcroft_karp(
"hopcroft_karp_matching()");
342 Check by_flow(
"compute_maximum_cardinality_bipartite_matching()");
343 Check blossom(
"blossom_maximum_cardinality_matching()");
358 const auto L = numbers(
l), R = numbers(
r);
359 bool ok = want_bipartite
and int(L.size() + R.size()) == s.
n;
361 ok = ok
and R.count(x) == 0;
362 for (
const auto & a : s.
arcs)
363 ok = ok
and L.count(a[0]) != L.count(a[1]);
365 bipartite.
fail(s, want_bipartite ?
"gives a wrong partition"
366 :
"does not throw on a graph that is not bipartite");
368 catch (
const std::domain_error &)
371 bipartite.
fail(s,
"throws on a bipartite graph");
376 std::vector<int>
arcs;
377 for (
auto it =
m.
get_it(); it.has_curr(); it.next_ne())
378 arcs.push_back(
num(it.get_curr()));
380 check.fail(s, std::to_string(
arcs.size()) +
" arcs, not "
381 + std::to_string(want_size)
390 check_matching(hopcroft_karp,
m);
397 check_matching(by_flow, mf);
399 catch (
const std::exception & e)
401 by_flow.
fail(s, std::string(
"throws ") + e.what());
408 check_matching(blossom, mb);
410 blossom.
fail(s,
"returns " + std::to_string(
size) +
" for "
411 + std::to_string(mb.
size()) +
" arcs");
422 using GT = TypeParam;
423 Check depth(
"Find_Depth_First_Spanning_Tree");
424 Check breadth(
"Find_Breadth_First_Spanning_Tree");
430 for (
int start : starts(s))
433 for (
int x = 0; x < s.
n; ++x)
434 size += comp[
size_t(x)] == comp[size_t(start)];
450 for (
typename GT::Node_Iterator it(tree); it.has_curr()
and ok; it.next_ne())
452 auto * t = it.get_curr_ne();
455 and comp[size_t(
num(p))] == comp[size_t(start)]
459 check->fail(s,
"from " + std::to_string(start) +
": a tree of "
462 + std::to_string(
size) +
" nodes");
473 using GT = TypeParam;
474 Check euler(
"Test_Eulerian::compute()");
475 Check walk(
"Find_Eulerian_Path");
476 Check nodes(
"Find_Eulerian_Path::find_node_sequence()");
478 Check empty(
"Find_Eulerian_Path, without arcs");
482 return (got == Eulerian_Type::Cycle) == (want == oracle::Euler::Cycle)
483 and (got == Eulerian_Type::Path) == (want == oracle::Euler::Path);
492 euler.
fail(s,
"a wrong class");
495 if (want == oracle::Euler::None)
497 const bool closed = want == oracle::Euler::Cycle;
503 if (
r.type != Eulerian_Type::Cycle or not
r.path.is_empty())
504 empty.
fail(s,
"says another class, or gives arcs");
510 std::vector<int>
arcs;
511 for (
auto it =
r.path.get_it(); it.has_curr(); it.next_ne())
512 arcs.push_back(
num(it.get_curr()));
514 walk.
fail(s,
"a wrong class or walk");
518 std::vector<int> seq;
520 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
521 seq.push_back(
num(it.get_curr()));
522 bool ok = seq.
size() ==
arcs.size() + 1;
523 for (
size_t i = 0; ok
and i <
arcs.size(); ++i)
525 const auto & a = s.
arcs[size_t(
arcs[i])];
526 ok = (a[0] == seq[i]
and a[1] == seq[i + 1]) or (a[1] == seq[i]
and a[0] == seq[i + 1]);
528 if (not ok or (closed
and seq.front() != seq.back()))
529 nodes.fail(s,
"the nodes do not follow the arcs");
Edmonds' Blossom algorithm for maximum matching in general graphs.
Build a mapped subgraph from a graph starting at a given node.
Computation of cut nodes (articulation points) of a graph.
void insert(Dlink *node) noexcept
Insert node after this.
Node belonging to a double circular linked list with header node.
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
Compute a breadth-first spanning tree of a graph.
Compute a depth-first spanning tree of a graph.
Finds and constructs an Eulerian path or cycle using Hierholzer's algorithm.
DynList< typename GT::Node * > find_node_sequence(GT &g)
Get the node sequence of the Eulerian path/cycle.
Determines whether a graph contains cycles.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Tests whether a graph or digraph is Eulerian.
Compute the connected components of a graph.
size_t count_components(const GT &g)
Count the number of connected components in a graph.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
The failures of one property over a domain.
void expect() const
Expects what the mark says, and prints the first failures.
void fail(const Spec &s, const std::string &what)
Counts a failure on spec, with what went wrong.
void run() noexcept
Counts a run of the property.
Eulerian graph detection and path/cycle finding.
TYPED_TEST(ExhaustiveConnectivity, ComponentsMatchTheOracle)
TYPED_TEST_SUITE(ExhaustiveConnectivity, 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
DynArray< Graph::Arc * > arcs
void compute_bipartite(const GT &g, DynDlist< typename GT::Node * > &l, DynDlist< typename GT::Node * > &r, SA sa=SA())
Computes the partition sets of a bipartite graph.
void compute_maximum_cardinality_bipartite_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Computes the maximum cardinality matching of a bipartite graph.
#define ARC_COOKIE(p)
Return the arc cookie
bool has_cycle(const GT &g)
Return true if an undirected graph has at least one cycle.
DynList< GT > inconnected_components(const GT &g)
Forward declaration for inconnected_components().
bool test_for_cycle(const GT &g, typename GT::Node *src)
Search for a cycle reachable from a given node.
bool test_connectivity(const GT &g)
Connectivity test for undirected graphs.
void hopcroft_karp_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Computes a maximum cardinality matching on a bipartite graph using the Hopcroft-Karp algorithm.
#define NODE_COOKIE(p)
Return the node cookie
DynList< typename GT::Node * > compute_cut_nodes(const GT &g, typename GT::Node *start)
Compute articulation points (cut vertices) of an undirected graph.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
DynList< typename GT::Arc * > find_bridges(const GT &g, typename GT::Node *start, SA sa=SA())
Find all bridge edges in a connected undirected graph.
bool is_graph_acyclique(const GT &g, typename GT::Node *start_node)
Return true if an undirected graph is acyclic.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Eulerian_Type
Enumeration for Eulerian graph classification.
and
Check uniqueness with explicit hash + equality functors.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
::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.
size_t for_each_spec(const Domain &d, bool directed, F &&f)
Calls f(spec) with every spec of d, smallest first, and returns how many there were.
int num(Node *p)
The number of a node of a built graph.
Domain domain(int max_nodes, int max_arcs)
The domain of max_nodes nodes and max_arcs arcs, of one more of each in heavy mode,...
bool is_matching(const Spec &s, const std::vector< int > &arcs)
Whether the arcs, by number, form a matching of s.
bool has_cycle(const Spec &s)
Whether the multigraph, not directed, has a cycle.
int max_matching(const Spec &s)
The size of a maximum matching, by brute force over the subsets of the arcs; loops never match.
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< 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...
std::set< int > cut_nodes(const Spec &s)
The nodes whose removal splits their component.
std::set< int > bridges(const Spec &s)
The arcs, by number, whose removal splits their component.
int components(const Spec &s, int skip_node=-1, int skip_arc=-1)
The number of components, without the node skip_node and the arc skip_arc.
bool on_cycle(const Spec &s, int x)
Whether the node x lies on a cycle of the multigraph, not directed.
bool is_bipartite(const Spec &s)
Whether the multigraph, not directed, has two colors with no arc inside one (a loop is an odd cycle)
Euler
What kind of Eulerian walk the multigraph has.
bool is_euler_walk(const Spec &s, const std::vector< int > &arcs, bool directed, bool closed)
Whether arcs, by number, walk every arc of s once, each from the node where the one before ended (fro...
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
std::vector< typename GT::Arc * > arcs
A small multigraph: its nodes are 0 .
size_t num_arcs() const noexcept
std::vector< std::array< int, 2 > > arcs
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Bipartite graph detection and 2-coloring.
Graph connectivity and connected components.
Articulation points (cut nodes), bridges, and biconnected components.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
Spanning tree generation algorithms.
DAG (acyclic) graph testing.