52#include <gtest/gtest.h>
100 std::vector<Spec> sweep_specs(
bool digraph)
104 a.
arcs = {{0, 1}, {1, 2}, {2, 0}, {2, 3}, {3, 3}, {1, 2}};
107 b.
arcs = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}};
110 c.
arcs = digraph ? std::vector<std::array<int, 2>>{{0, 1}, {1, 0}, {1, 2}, {2, 3}, {3, 1}}
111 : std::vector<std::array<int, 2>>{{0, 1}, {2, 3}, {3, 2}};
112 for (
Spec * s : {&a, &b, &c})
113 for (
size_t i = 0; i < s->arcs.size(); ++i)
114 s->w.push_back(
static_cast<long long>(s->arcs.size() - i));
118 std::string text(std::vector<int> v,
bool sorted =
true)
121 std::sort(v.begin(), v.end());
123 for (
size_t i = 0; i < v.size(); ++i)
128 template <
class List>
129 std::vector<int> numbers(
const List &
l)
132 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
133 r.push_back(
num(it.get_curr()));
138 template <
class Lists>
139 std::string text_of_lists(
const Lists & lists)
141 std::vector<std::string>
r;
142 for (
auto it = lists.get_it(); it.has_curr(); it.next_ne())
143 r.push_back(text(numbers(it.get_curr())));
144 std::sort(
r.begin(),
r.end());
146 for (
const auto & x :
r)
152 std::string text_of_graphs(
const DynList<GT> & graphs)
154 std::vector<std::string>
r;
155 for (
auto it = graphs.
get_it(); it.has_curr(); it.next_ne())
158 for (
auto j = it.get_curr().get_node_it(); j.has_curr(); j.next_ne())
160 for (
auto j = it.get_curr().get_arc_it(); j.has_curr(); j.next_ne())
161 arcs.push_back(
num(j.get_curr()));
164 std::sort(
r.begin(),
r.end());
166 for (
const auto & x :
r)
172 std::string text_of_path(
const Path<GT> & p)
177 r += std::to_string(
num(it.get_current_node_ne()));
178 if (it.has_current_arc())
179 r +=
"-" + std::to_string(
num(it.get_current_arc_ne())) +
"-";
187 std::string closed_walk_text(
const Path<GT> & p,
const Spec & s)
194 nodes.push_back(
num(it.get_current_node_ne()));
195 if (it.has_current_arc())
196 arcs.push_back(
num(it.get_current_arc_ne()));
200 for (
size_t i = 0; ok
and i <
arcs.size(); ++i)
202 return ok ?
"cycle" :
"not a cycle: " + text_of_path(p);
207 std::string text_of_tree(
const GT & tree)
210 for (
auto it = tree.
get_node_it(); it.has_curr(); it.next_ne())
211 nodes.push_back(
num(it.get_curr()));
212 for (
auto it = tree.
get_arc_it(); it.has_curr(); it.next_ne())
213 arcs.push_back(
num(it.get_curr()));
219 std::string painted(
const GT & g,
unsigned b)
221 std::vector<int>
arcs;
222 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
223 if (
ARC_BITS(it.get_curr()).get_bit(b))
224 arcs.push_back(
num(it.get_curr()));
231 std::string
r =
k.label +
"," + std::to_string(
k.df) +
"," + std::to_string(
k.low) +
"[";
232 for (
auto * c = t->
get_left_child(); c !=
nullptr; c = c->get_right_sibling())
233 r += text_of_df_tree(c);
256 std::vector<Algorithm<GT>> algorithms()
261 std::vector<Algorithm<GT>>
r;
262 auto add = [&
r] (std::string name,
auto f) {
r.push_back({std::move(name), f}); };
272 return text_of_lists(
l);
278 return text_of_graphs(
l);
302 return text(numbers(
l)) + text(numbers(rr));
304 catch (
const std::domain_error &)
306 return std::string(
"not bipartite");
309 add(
"blossom_maximum_cardinality_matching()", [] (
Built<GT> & b,
const Spec &)
313 return text(numbers(
m));
319 return text_of_tree(tree);
325 return text_of_tree(tree);
334 return text_of_tree(tree);
336 add(
"Kruskal_Min_Spanning_Tree, painted", [] (
Built<GT> & b,
const Spec & s)
350 prim(b.
g, b.
nodes[0], tree);
351 return text_of_tree(tree);
368 return text_of_df_tree(t.get()) + std::to_string(non_tree.
size());
373 return text(numbers(res.path),
false);
386 return text_of_lists(
l);
393 return text_of_graphs(blk) + text(numbers(
arcs));
398 add(
"Tarjan_Connected_Components::compute_cycle()", [] (
Built<GT> & b,
const Spec & s)
402 return closed_walk_text(p, s);
404 add(
"kosaraju_connected_components(), lists", [] (
Built<GT> & b,
const Spec &)
408 add(
"kosaraju_connected_components(), blocks", [] (
Built<GT> & b,
const Spec &)
413 return text_of_graphs(blk) + text(numbers(
arcs));
419 for (
auto it =
ranks.
get_it(); it.has_curr(); it.next_ne())
420 r += text(numbers(it.get_curr()));
425 std::vector<int> idoms;
427 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
428 idoms.push_back(
num(it.get_curr().first) * 100
429 + (it.get_curr().second ?
num(it.get_curr().second) : 99));
440 std::vector<int> idoms;
442 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
443 idoms.push_back(
num(it.get_curr().first) * 100
444 + (it.get_curr().second ?
num(it.get_curr().second) : 99));
447 add(
"warshall_compute_transitive_clausure()", [] (
Built<GT> & b,
const Spec & s)
452 for (
int u = 0; u < s.
n; ++u)
453 for (
int v = 0; v < s.
n; ++v)
454 r +=
char(
'0' +
int(mat(b.
nodes[
size_t(u)], b.
nodes[
size_t(v)])));
464 return text_of_path(p);
470 return text_of_path(p);
485 return text_of_tree(c);
493 std::string structure_error(
const Built<GT> & b,
const Spec & s)
496 return "the graph lost nodes or arcs";
497 for (
size_t i = 0; i < s.
arcs.size(); ++i)
500 return "the arc " + std::to_string(i) +
" lost its ends";
501 for (
int x = 0; x < s.
n; ++x)
504 for (
typename GT::Node_Arc_Iterator it(b.
nodes[
size_t(x)]); it.has_curr(); it.next_ne())
507 return "the list of the node " + std::to_string(x) +
" is broken";
518 std::string sweep(
const Algorithm<GT> & algo,
Built<GT> & b,
const Spec & s)
520 if (std::getenv(
"ALEPH_GRAPH_HARNESS_TRACE") !=
nullptr)
521 std::cerr << algo.name <<
" on " << s.
str() << std::endl;
522 const std::string want = algo.run(b, s);
523 for (
size_t k = 1;
k < 1000000; ++
k)
532 got = algo.run(b, s);
534 catch (
const std::bad_alloc &)
538 refused = refused_allocations;
540 if (
auto e = structure_error(b, s); not e.empty())
541 return "at the allocation " + std::to_string(
k) +
": " + e;
544 if (
const auto again = algo.run(b, s); again != want)
545 return "after a failure at the allocation " + std::to_string(
k)
546 +
", it gives " + again +
", not " + want;
552 return (refused > 0 ?
"it caught the failure of the allocation " + std::to_string(
k)
553 +
" and went on, and gives " :
"it gives ") + got +
", not " + want;
557 return "it never ends";
568 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
576 using GT = TypeParam;
577 const auto algos = algorithms<GT>();
578 std::vector<Check> checks;
579 for (
const auto & a : algos)
580 checks.emplace_back(a.name);
582 for (
const Spec & s : sweep_specs(
GT().is_digraph()))
583 for (
size_t i = 0; i < algos.size(); ++i)
587 if (
auto e = sweep(algos[i], b, s); not e.empty())
588 checks[i].fail(s, e);
591 for (
const auto & c : checks)
598 alignas(64)
unsigned char left_over[4096] = {};
603 void dirty(
const GT & g)
605 auto soil = [] (
auto * x)
608 x->attrs.control_bits.set_bit(bit, 1);
609 x->attrs.counter = 1000003;
610 x->attrs.cookie = left_over;
612 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
614 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
626 using GT = TypeParam;
627 const auto algos = algorithms<GT>();
628 std::vector<Check> checks;
629 for (
const auto & a : algos)
630 checks.emplace_back(a.name);
632 for (
const Spec & s : sweep_specs(
GT().is_digraph()))
633 for (
size_t i = 0; i < algos.size(); ++i)
637 const std::string want = algos[i].run(b, s);
642 if (
const std::string got = algos[i].run(b, s); got != want)
643 checks[i].fail(s,
"gives " + got +
", not " + want);
646 for (
const auto & c : checks)
Bidirectional BFS for shortest unweighted path.
Edmonds' Blossom algorithm for maximum matching in general graphs.
Dominator and post-dominator tree computation via the Lengauer-Tarjan algorithm.
Kruskal's minimum spanning tree algorithm.
Prim's algorithm for minimum spanning trees.
Tarjan's algorithm for strongly connected components.
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
WeightedDigraph::Node Node
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Bidirectional BFS for finding shortest unweighted paths.
Bit matrix for graph connectivity.
Node belonging to a double circular linked list with header node.
Dynamic doubly linked list with O(1) size and bidirectional access.
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.
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.
Computes the minimum spanning tree of a graph using Kruskal's algorithm.
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
Iterator on nodes and arcs of a path.
bool has_current_node() const noexcept
Return true if the iterator has a current node.
bool is_empty() const noexcept
Return true if the path is empty.
Computes the minimum spanning tree of a graph using Prim's algorithm.
Computes topological ordering using breadth-first search (Kahn's algorithm).
RankList< List< typename GT::Node * > > ranks(const GT &g)
Compute rank-based topological ordering.
Computes strongly connected components (SCCs) in a directed graph using Tarjan's algorithm.
void connected_components(const GT &g, DynList< GT > &blk_list, DynList< typename GT::Arc * > &arc_list)
Computes the strongly connected components (SCCs) of a digraph.
bool compute_cycle(const GT &g, Path< GT > &path)
Finds and constructs a cycle in the digraph, if one exists.
Tests whether a graph or digraph is Eulerian.
Checks whether a path exists between two nodes.
Forward declaration used by CRTP helpers before the full node definition.
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
Compute the connected components of a graph.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
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)
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_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.
Eulerian graph detection and path/cycle finding.
Pictures of depth-first trees with their df and low numbers.
TYPED_TEST_SUITE(GraphFailAt, All_Kinds)
TYPED_TEST(GraphFailAt, EveryAllocationOfEveryAlgorithmMayFail)
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.
Convert spanning tree graphs to Tree_Node structures.
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 kosaraju_connected_components(const GT &g, DynList< GT > &blk_list, DynList< typename GT::Arc * > &arc_list)
Compute strongly connected components using Kosaraju's algorithm.
#define NODE_COUNTER(p)
Get the counter of a node.
bool test_for_cycle(const GT &g, typename GT::Node *src)
Search for a cycle reachable from a given node.
Df_Low_Tree_Node * build_df_low_tree(const GT &g, typename GT::Node *src, DynList< Df_Low_Non_Tree_Arc > &non_tree_arcs, Node_Label node_label=Node_Label(), SA sa=SA())
Build the depth-first spanning tree of an undirected graph, with the df and low numbers of its nodes.
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
void build_dominator_tree(GT &g, typename GT::Node *root, GT &tree, SA sa=SA())
Build the dominator tree of a digraph.
#define ARC_BITS(p)
Return the control bits of arc p.
#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.
void warshall_compute_transitive_clausure(GT &g, Bit_Mat_Graph< GT, SA > &mat, SA sa=SA())
Computes the transitive closure of a graph using Warshall's algorithm.
DynList< std::pair< typename GT::Node *, typename GT::Node * > > compute_dominators(GT &g, typename GT::Node *root, SA sa=SA())
Compute immediate dominators of a digraph from a root node.
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< std::pair< typename GT::Node *, typename GT::Node * > > compute_post_dominators(const GT &g, typename GT::Node *exit_node, SA sa=SA())
Compute immediate post-dominators of a digraph from an exit node.
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.
void copy_graph(GT >gt, const GT &gsrc, bool cookie_map=false)
Explicit copy of graph.
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
Kosaraju's algorithm for finding strongly connected components.
Main namespace for Aleph-w library functions.
Tree_Node< Df_Low_Key > Df_Low_Tree_Node
Node of the depth-first tree that build_df_low_tree() returns.
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.
Array< size_t > ranks(const Array< T > &array)
Computes the rank of each element in an Array.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
double add(double a, double b)
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.
The weight of an arc, read by its number from a vector.
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 .
std::string str() const
The spec as text, ready to be written back as a test case.
std::vector< long long > w
std::vector< std::array< int, 2 > > arcs
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Topological sorting algorithms for directed acyclic graphs (DAGs).
Bipartite graph detection and 2-coloring.
Graph connectivity and connected components.
Articulation points (cut nodes), bridges, and biconnected components.
Path finding algorithms in graphs.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
Spanning tree generation algorithms.
DAG (acyclic) graph testing.
Warshall's algorithm for transitive closure.