59#include <gtest/gtest.h>
84 std::set<int> arc_numbers(
const List &
l)
87 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
88 r.insert(it.get_curr()->get_info());
100 s.
arcs = {{0, 1}, {1, 2}, {1, 3}, {2, 3}};
109 s.
arcs = {{0, 1}, {2, 3}, {1, 2}};
118 s.
arcs = {{0, 1}, {1, 2}, {2, 0}};
124 std::set<int> graph_arcs(
const GT & g)
127 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
128 r.insert(it.get_curr_ne()->get_info());
134 std::set<int> path_arcs(
const Path<GT> & p)
142 template <
class List>
143 std::set<int> node_numbers(
const List &
l)
146 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
147 r.insert(
num(it.get_curr()));
154 void expect_path(
const Path<GT> & p,
int from,
int to,
bool hidden)
161 EXPECT_EQ(path_arcs(p).
count(
Hidden), 0U) <<
"the path takes the hidden arc";
171 constexpr bool default_built = std::is_default_constructible_v<SA>;
191 using GT = TypeParam;
193 auto * s = b.
nodes[0];
194 auto * t = b.
nodes[3];
195 for_each_filter<GT>([&] (
auto sa,
const char * name)
197 using SA =
decltype(sa);
198 constexpr bool hiding = hides<GT, SA>;
202 auto goal = [t] (typename GT::Node * p) { return p == t; };
205 EXPECT_TRUE(lvalue(g, s, t, p));
206 expect_path(p, 0, 3, hiding);
207 expect_path(lvalue(g, s, t), 0, 3, hiding);
208 expect_path(lvalue(g, s, goal), 0, 3, hiding);
212 expect_path(rvalue(g, s, [t] (
typename GT::Node * p) {
return p == t; }),
215 if constexpr (default_built<SA>)
224 using GT = TypeParam;
226 auto * s = b.
nodes[0];
227 auto * t = b.
nodes[3];
228 for_each_filter<GT>([&] (
auto sa,
const char * name)
230 using SA =
decltype(sa);
231 constexpr bool hiding = hides<GT, SA>;
235 auto goal = [t] (typename GT::Node * p) { return p == t; };
238 EXPECT_TRUE(lvalue(g, s, t, p));
239 expect_path(p, 0, 3, hiding);
240 expect_path(lvalue(g, s, t), 0, 3, hiding);
241 expect_path(lvalue(g, s, goal), 0, 3, hiding);
245 expect_path(rvalue(g, s, goal), 0, 3, hiding);
247 if constexpr (default_built<SA>)
256 using GT = TypeParam;
258 auto * s = b.
nodes[0];
259 auto * t = b.
nodes[3];
260 for_each_filter<GT>([&] (
auto sa,
const char * name)
262 using SA =
decltype(sa);
263 constexpr bool hiding = hides<GT, SA>;
267 auto goal = [t] (typename GT::Node * p) { return p == t; };
269 expect_path(lvalue.
dfs(s, t), 0, 3, hiding);
270 expect_path(lvalue.
bfs(s, t), 0, 3, hiding);
271 expect_path(lvalue.
dfs(s, goal), 0, 3, hiding);
272 expect_path(lvalue.
bfs(s, goal), 0, 3, hiding);
276 expect_path(rvalue.
dfs(s, [t] (
typename GT::Node * p) { return p == t; }),
278 expect_path(rvalue.
bfs(s, [t] (
typename GT::Node * p) { return p == t; }),
281 if constexpr (default_built<SA>)
289 using GT = TypeParam;
291 auto * s = b.
nodes[0];
292 auto * t = b.
nodes[3];
293 for_each_filter<GT>([&] (
auto sa,
const char * name)
295 using SA =
decltype(sa);
296 constexpr bool hiding = hides<GT, SA>;
303 BiBFS by_const(std::as_const(sa));
305 EXPECT_TRUE(by_lvalue.find_path(g, s, t, p));
306 expect_path(p, 0, 3, hiding);
307 EXPECT_TRUE(by_const(g, s, t, p));
308 expect_path(p, 0, 3, hiding);
309 expect_path(by_lvalue(g, s, t), 0, 3, hiding);
312 BiBFS rvalue(std::move(copy));
313 expect_path(rvalue(g, s, t), 0, 3, hiding);
315 if constexpr (default_built<SA>)
316 expect_path(BiBFS()(g, s, t), 0, 3, false);
323 using GT = TypeParam;
325 auto * s = b.
nodes[0];
326 auto * t = b.
nodes[3];
327 for_each_filter<GT>([&] (
auto sa,
const char * name)
329 using SA =
decltype(sa);
330 constexpr bool hiding = hides<GT, SA>;
337 BiBFS by_const(std::as_const(sa));
339 EXPECT_TRUE(by_lvalue.find_path(g, s, t, p));
340 expect_path(p, 0, 3, hiding);
341 EXPECT_TRUE(by_const(g, s, t, p));
342 expect_path(p, 0, 3, hiding);
343 expect_path(by_lvalue(g, s, t), 0, 3, hiding);
346 BiBFS rvalue(std::move(copy));
347 expect_path(rvalue(g, s, t), 0, 3, hiding);
349 if constexpr (default_built<SA>)
350 expect_path(BiBFS()(g, s, t), 0, 3, false);
357 using GT = TypeParam;
359 for_each_filter<GT>([&] (
auto sa,
const char * name)
361 using SA =
decltype(sa);
362 constexpr bool hiding = hides<GT, SA>;
364 const size_t nodes = hiding ? 2 : 4;
365 const std::set<int>
arcs = hiding ? std::set<int>{0} : std::set<int>{0, 1, 2};
369 Build_Subgraph<GT, SA> build(sa);
373 build(g, sg, b.nodes[0]);
374 EXPECT_EQ(sg.get_num_nodes(), nodes);
375 EXPECT_EQ(graph_arcs(sg), arcs);
379 const GT copy = build(g, b.nodes[0]);
380 EXPECT_EQ(copy.get_num_nodes(), nodes);
381 EXPECT_EQ(graph_arcs(copy), arcs);
385 DynList<typename GT::Node *> l;
386 build(g, l, b.nodes[0]);
387 EXPECT_EQ(l.size(), nodes);
389 if constexpr (default_built<SA>)
393 EXPECT_EQ((Build_Subgraph<GT, SA>()(g, b.nodes[0]).get_num_nodes()), 4U);
403 using GT = TypeParam;
405 for_each_filter<GT>([&] (
auto sa,
const char * name)
407 using SA =
decltype(sa);
408 constexpr bool hiding = hides<GT, SA>;
412 Unconnected_Components<GT, SA> cc(sa);
416 DynList<DynList<typename GT::Node *>> lists;
417 EXPECT_THROW_OR_FILTER(cc(g, blocks), std::domain_error);
418 EXPECT_THROW_OR_FILTER(cc(g, lists), std::domain_error);
419 EXPECT_THROW_OR_FILTER((void) cc.count_components(g), std::domain_error);
420 EXPECT_THROW_OR_FILTER((void) cc.is_connected(g), std::domain_error);
421 EXPECT_THROW_OR_FILTER(cc.template compute_blocks<DynList>(g, blocks),
423 EXPECT_THROW_OR_FILTER(cc.template compute_lists<DynList>(g, lists),
425 EXPECT_TRUE(blocks.is_empty());
426 EXPECT_TRUE(lists.is_empty());
434 const size_t count = cc.count_components(g);
435 EXPECT_EQ(blocks.size(),
count);
438 for (
auto it = blocks.get_it(); it.has_curr(); it.next_ne())
440 arcs += it.get_curr().get_num_arcs();
443 EXPECT_EQ(graph_arcs(it.get_curr()).count(
Hidden), 0U);
447 EXPECT_EQ(
count, hiding ? 2U : 1U);
448 EXPECT_EQ(cc.is_connected(g), not hiding);
449 EXPECT_EQ(
arcs, hiding ? 2U : 3U);
452 cc.template compute_blocks<DynList>(g, blocks2);
453 EXPECT_EQ(blocks2.size(),
count);
455 cc.template compute_lists<DynList>(g, lists2);
458 if constexpr (default_built<SA>)
468 using GT = TypeParam;
470 for_each_filter<GT>([&] (
auto sa,
const char * name)
472 using SA =
decltype(sa);
473 constexpr bool hiding = hides<GT, SA>;
475 const std::set<int> cut = hiding ? std::set<int>{1} : std::set<int>{};
478 Compute_Cut_Nodes<GT, SA> cc(g, sa);
479 DynDlist<typename GT::Node *> l;
481 EXPECT_EQ(node_numbers(l), cut);
483 EXPECT_EQ(node_numbers(l), cut);
484 cc.cut_nodes(b.nodes[0], l);
485 EXPECT_EQ(node_numbers(l), cut);
487 EXPECT_EQ(cc.paint_subgraphs(), hiding ? 3 : 1);
492 cc.map_subgraph(block, 1);
493 EXPECT_EQ(block.get_num_nodes(), 1U);
497 cc.map_cut_graph(cut_graph, cross);
498 EXPECT_EQ(cut_graph.get_num_nodes(), cut.size());
499 const std::set<int> crossing = hiding ? std::set<int>{0, 1} : std::set<int>{};
500 EXPECT_EQ(arc_numbers(cross), crossing);
508 EXPECT_EQ(blocks.
size(), hiding ? 2U : 1U);
510 if constexpr (default_built<SA>)
522 using GT = TypeParam;
524 for_each_filter<GT>([&] (
auto sa,
const char * name)
526 using SA =
decltype(sa);
527 constexpr bool hiding = hides<GT, SA>;
529 const std::set<int> want = hiding ? std::set<int>{0, 1} : std::set<int>{};
532 Compute_Bridges<GT, SA> cb(g, sa);
533 EXPECT_EQ(arc_numbers(cb.find_bridges(b.nodes[0])), want);
534 EXPECT_EQ(arc_numbers(cb.find_bridges()), want);
535 EXPECT_EQ(arc_numbers(cb(b.nodes[0])), want);
536 EXPECT_EQ(arc_numbers(cb()), want);
538 if constexpr (default_built<SA>)
540 EXPECT_TRUE((Compute_Bridges<GT, SA>(g).find_bridges().is_empty()));
548 using GT = TypeParam;
550 for_each_filter<GT>([&] (
auto sa,
const char * name)
552 using SA =
decltype(sa);
553 constexpr bool hiding = hides<GT, SA>;
555 const std::set<int> want = hiding ? std::set<int>{0, 1} : std::set<int>{};
558 EXPECT_EQ(arc_numbers(find_bridges(g, b.nodes[0], sa)), want);
559 if constexpr (default_built<SA>)
561 EXPECT_TRUE((find_bridges<GT, SA>(g, b.nodes[0]).is_empty()));
569 using GT = TypeParam;
576 for_each_filter<GT>([&] (
auto sa,
const char * name)
578 using SA =
decltype(sa);
579 constexpr bool hiding = hides<GT, SA>;
581 auto expect_tree = [&] (
const GT & tree,
bool hidden)
596 expect_tree(tree, hiding);
599 EXPECT_EQ(dfs(b.
g, tree),
nullptr);
601 EXPECT_NE(dfs(b.
g, b.
nodes[0], tree2),
nullptr);
602 expect_tree(tree2, hiding);
607 EXPECT_EQ(dfs(apart.
g, forest),
nullptr);
610 if constexpr (default_built<SA>)
616 EXPECT_NE(
root,
nullptr);
617 expect_tree(tree3,
false);
620 EXPECT_EQ(
root,
nullptr);
627 using GT = TypeParam;
629 for_each_filter<GT>([&] (
auto sa,
const char * name)
631 using SA =
decltype(sa);
632 constexpr bool hiding = hides<GT, SA>;
636 auto expect_tree = [&] (
const GT & tree,
size_t nodes,
bool hidden)
648 const size_t from_first = b.
g.
is_digraph() ? 0 : 4;
652 bfs(b.
g, b.
nodes[0], tree);
653 expect_tree(tree, 4, hiding);
655 EXPECT_NE(bfs(b.
g, tree2),
nullptr);
656 expect_tree(tree2, from_first, hiding);
658 if constexpr (default_built<SA>)
662 expect_tree(tree3, from_first,
false);
669 using GT = TypeParam;
671 for_each_filter<GT>([&] (
auto sa,
const char * name)
673 using SA =
decltype(sa);
674 constexpr bool hiding = hides<GT, SA>;
676 const bool acyclic = hiding;
679 EXPECT_EQ(lvalue(b.
g), acyclic);
680 EXPECT_EQ(lvalue(b.
g, acyclic ? 2 : 3), acyclic);
684 EXPECT_EQ(rvalue(b.
g), acyclic);
686 if constexpr (default_built<SA>)
695 using GT = TypeParam;
697 for_each_filter<GT>([&] (
auto sa,
const char * name)
699 using SA =
decltype(sa);
700 constexpr bool hiding = hides<GT, SA>;
702 const bool cycle = not hiding;
705 EXPECT_EQ(lvalue(b.
g), cycle);
706 EXPECT_EQ(lvalue(b.
g, cycle ? 3 : 2), cycle);
710 EXPECT_EQ(rvalue(b.
g), cycle);
712 if constexpr (default_built<SA>)
721 using GT = TypeParam;
723 for_each_filter<GT>([&] (
auto sa,
const char * name)
725 using SA =
decltype(sa);
726 constexpr bool hiding = hides<GT, SA>;
728 const bool connected = not hiding;
731 EXPECT_EQ(
test(b.
g, sa), connected);
733 EXPECT_EQ(
test(b.
g, std::move(
copy)), connected);
735 if constexpr (default_built<SA>)
737 EXPECT_TRUE(
test(b.
g));
744 using GT = TypeParam;
746 for_each_filter<GT>([&] (
auto sa,
const char * name)
748 using SA =
decltype(sa);
749 constexpr bool hiding = hides<GT, SA>;
753 const Test_For_Cycle<GT, SA> test(sa);
754 EXPECT_EQ(test(g, b.nodes[0]), not hiding);
756 if constexpr (default_built<SA>)
758 EXPECT_TRUE((Test_For_Cycle<GT, SA>()(g, b.nodes[0])));
766 using GT = TypeParam;
768 for_each_filter<GT>([&] (
auto sa,
const char * name)
770 using SA =
decltype(sa);
771 constexpr bool hiding = hides<GT, SA>;
775 Test_For_Path<GT, SA> test(sa);
776 EXPECT_EQ(test(g, b.nodes[0], b.nodes[3]), not hiding);
778 if constexpr (default_built<SA>)
780 EXPECT_TRUE((Test_For_Path<GT, SA>()(g, b.nodes[0], b.nodes[3])));
Bidirectional BFS for shortest unweighted path.
Bidirectional BFS for finding shortest unweighted paths.
Computation of cut nodes (articulation points) of a graph.
void compute_blocks(DynDlist< GT > &block_list, GT &cut_graph, DynDlist< typename GT::Arc * > &cross_arc_list)
Build mapped graphs around cut nodes.
Path search on directed graphs defined through a graph class (not a digraph).
Path< GT > dfs(typename GT::Node *start, Op &op)
Path< GT > bfs(typename GT::Node *start, Op &op)
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.
Performs a breadth-first search for a path between a pair of nodes.
Performs a depth-first search for a path between a pair of nodes.
constexpr bool is_empty() const noexcept
Determines whether a graph contains cycles.
Determines whether a graph is acyclic (contains no cycles).
Node * get_first_node() const
Return any node in the graph.
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
bool is_empty() const noexcept
Return true if the path is empty.
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
void for_each_arc(Operation op=Operation()) const
Execute an operation on each arc of path.
Compute the connected components of a graph.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
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.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Determines if a graph g is connected.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
What the files of the contract matrix of the graph templates, graph_contract_matrix_*_test....
TYPED_TEST(ContractSearchAll, FindPathDepthFirst)
TYPED_TEST_SUITE(ContractSearchAll, All_Kinds)
The kinds of arc filter and of distance that the graph algorithms must accept.
Every small multigraph, on every kind of graph, for the tests.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
void for_each_constness(GT &g, F &&f)
Calls f(g) with g as it is and as a const graph.
constexpr int Hidden
The number of the arc that the filters hide.
::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.
::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 > >, 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 > > > All_Kinds
The six kinds, with int infos.
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
A small multigraph: its nodes are 0 .
std::vector< std::array< int, 2 > > arcs
Graph connectivity and connected components.
Articulation points (cut nodes), bridges, and biconnected components.
Path finding algorithms in graphs.
Generic graph and digraph implementations.
Spanning tree generation algorithms.
DAG (acyclic) graph testing.
Graph connectivity testing.
Cycle detection in graphs.