6#include <gtest/gtest.h>
19#if defined(__unix__) || defined(__APPLE__)
48 std::vector<Node *> n;
52 for (
int i = 0; i < 6; ++i)
55 const int arcs[][2] = {{0, 1}, {1, 2}, {2, 0}, {1, 3}, {3, 4}, {4, 1}, {4, 5}};
56 for (
const auto & a :
arcs)
57 g.insert_arc(n[a[0]], n[a[1]], 10 * a[0] + a[1]);
63 const std::string two_triangles_low =
66 "Node 0.0 \"2,2,0\" \n"
67 "Node 0.0 \"3,3,1\" \n"
68 "Node 0.0.1 \"4,4,1\" \n"
69 "Node 0.0.1.0 \"5,5,5\" \n"
70 "Dashed-Connexion 0.0.0 0 L\n"
71 "Dashed-Connexion 0.0.1.0 0.0 R\n";
78 bool operator () (
Arc * a)
const noexcept
80 return a->get_info() != info;
87 std::vector<Df_Low_Tree_Node *>
nodes;
91 for (
auto c = p->get_left_child(); c !=
nullptr; c = c->get_right_sibling())
101 if (p->get_key().label == label)
108 return {a.
first->get_key().label, a.second->get_key().label};
111 std::vector<std::pair<std::string, std::string>>
114 std::vector<std::pair<std::string, std::string>> ret;
119 using Label_Pairs = std::vector<std::pair<std::string, std::string>>;
122TEST(Df_Low_Tree, writes_the_tree_with_df_low_and_the_other_arcs)
125 std::ostringstream
out;
127 EXPECT_EQ(
out.str(), two_triangles_low);
130TEST(Df_Low_Tree, writes_the_labels_that_the_writer_chooses)
134 std::ostringstream
df;
135 write_df_low_tree<Write_Df_Tree_Df>(t.g, t.n[0],
df);
138 "Node 0 \"(1,1)\" \n"
139 "Node 0.0 \"(2,2)\" \n"
140 "Node 0.0 \"(3,3)\" \n"
141 "Node 0.0.1 \"(4,4)\" \n"
142 "Node 0.0.1.0 \"(5,5)\" \n"
143 "Dashed-Connexion 0.0.0 0 L\n"
144 "Dashed-Connexion 0.0.1.0 0.0 R\n");
146 std::ostringstream label;
147 write_df_low_tree<Write_Df_Tree_Label>
149 [] (
Node * p) { return std::string(1, char(
'A' + p->get_info())); });
150 EXPECT_EQ(label.str(),
155 "Node 0.0.1 \"E\" \n"
156 "Node 0.0.1.0 \"F\" \n"
157 "Dashed-Connexion 0.0.0 0 L\n"
158 "Dashed-Connexion 0.0.1.0 0.0 R\n");
161TEST(Df_Low_Tree, builds_the_tree_and_the_other_arcs)
167 std::vector<std::string>
keys;
171 keys.push_back(
k.label +
":" + std::to_string(
k.df) +
":" + std::to_string(
k.low));
173 EXPECT_EQ(
keys, (std::vector<std::string>{
"0:0:-1",
"1:1:0",
"2:2:0",
"3:3:1",
175 EXPECT_EQ(labels(others), (Label_Pairs{{
"2",
"0"}, {
"4",
"1"}}));
178TEST(Df_Low_Tree, the_tree_depends_on_the_start_node)
181 std::ostringstream
out;
185 "Node 0 \"4,1,1\" \n"
186 "Node 0.0 \"3,2,1\" \n"
187 "Node 0.0.0 \"1,3,1\" \n"
188 "Node 0.0.0.0 \"0,4,3\" \n"
189 "Node 0.0.0.0.0 \"2,5,3\" \n"
190 "Dashed-Connexion 0.0.0.0.0.0 0.0.0.0 L\n"
191 "Dashed-Connexion 0.0.0.0 0.0 R\n");
194TEST(Df_Low_Tree, a_single_node_is_only_a_root)
198 std::ostringstream
out;
200 EXPECT_EQ(
out.str(),
"Root \"7,0,-\" \n");
203TEST(Df_Low_Tree, the_default_label_writes_the_node_info)
210 std::ostringstream
out;
212 EXPECT_EQ(
out.str(),
"Root \"a,0,-\" \nNode 0 \"b,1,1\" \n");
215TEST(Df_Low_Tree, a_parallel_arc_is_another_arc)
218 t.g.insert_arc(t.n[4], t.n[5], 45);
222 EXPECT_EQ(labels(others), (Label_Pairs{{
"2",
"0"}, {
"4",
"1"}, {
"5",
"4"}}));
223 EXPECT_EQ(find_label(tree.get(),
"5")->get_key().low, 4);
226TEST(Df_Low_Tree, an_arc_from_a_node_to_itself_is_left_out)
229 t.g.insert_arc(t.n[3], t.n[3], 33);
231 std::ostringstream
out;
233 EXPECT_EQ(
out.str(), two_triangles_low);
236TEST(Df_Low_Tree, only_the_arcs_that_the_filter_shows_count)
243 EXPECT_EQ(labels(others), (Label_Pairs{{
"4",
"1"}}));
244 EXPECT_EQ(find_label(tree.get(),
"2")->get_key().low, 2);
245 EXPECT_EQ(find_label(tree.get(),
"1")->get_key().low, 1);
248TEST(Df_Low_Tree, only_the_nodes_that_the_start_node_reaches_are_in_the_tree)
251 Node * x = t.g.insert_node(10);
252 Node *
y = t.g.insert_node(11);
253 t.g.insert_arc(x,
y, 1011);
254 t.g.insert_arc(
y, x, 1110);
259 EXPECT_EQ(labels(others), (Label_Pairs{{
"2",
"0"}, {
"4",
"1"}}));
263 EXPECT_EQ(labels(others), (Label_Pairs{{
"11",
"10"}}));
266TEST(Df_Low_Tree, does_not_modify_the_graph)
274 for (
auto it = t.g.get_arc_it(); it.has_curr(); it.next_ne())
277 std::ostringstream
out;
287 for (
auto it = t.g.get_arc_it(); it.has_curr(); it.next_ne())
288 EXPECT_EQ(
ARC_COOKIE(it.get_curr()), it.get_curr());
291TEST(Df_Low_Tree, rejects_a_digraph_and_a_null_start_node)
294 auto a = dg.insert_node(0);
295 dg.insert_arc(a, dg.insert_node(1), 0);
298 std::ostringstream
out;
303 EXPECT_THROW((
void)
build_df_low_tree(t.g,
nullptr, others), std::invalid_argument);
305 EXPECT_TRUE(
out.str().empty());
308TEST(Df_Low_Tree, an_exception_leaves_nothing_behind)
312 auto label = [&calls] (
Node * p)
315 throw std::runtime_error(
"label");
316 return std::to_string(p->get_info());
320 EXPECT_THROW((
void)
build_df_low_tree(t.g, t.n[0], others, label), std::runtime_error);
324 std::ostringstream
out;
326 EXPECT_TRUE(
out.str().empty());
331TEST(Df_Low_Tree, a_tree_of_any_depth_is_written)
338 for (
size_t i = 1; i < n; ++i)
345 std::ostringstream
out;
349 const std::string s =
out.str();
351 for (
size_t pos = s.find(
"Node "); pos != std::string::npos; pos = s.find(
"Node ", pos + 1))
353 EXPECT_EQ(s.rfind(
"Root ", 0), 0u);
354 EXPECT_EQ(
nodes, n - 1);
366#if defined(__unix__) || defined(__APPLE__)
368 ASSERT_EQ(pthread_attr_init(&attr), 0);
369 ASSERT_EQ(pthread_attr_setstacksize(&attr, 256 * 1024), 0);
370 auto start = [](
void *arg) ->
void *
372 (*
static_cast<F *
>(arg))();
376 const int created = pthread_create(&thread, &attr, start, &f);
377 pthread_attr_destroy(&attr);
378 ASSERT_EQ(created, 0);
379 ASSERT_EQ(pthread_join(thread,
nullptr), 0);
390TEST(Df_Low_Tree, a_deep_tree_is_written_on_a_small_stack)
392 constexpr size_t n = 3000;
396 for (
size_t i = 1; i < n; ++i)
406 run_with_small_stack([&]
410 std::ostringstream
out;
422 for (
size_t pos = s.find(
"Node "); pos != std::string::npos; pos = s.find(
"Node ", pos + 1))
424 EXPECT_EQ(s.rfind(
"Root ", 0), 0u);
425 EXPECT_EQ(
nodes, n - 1);
427 std::string deepest =
"0";
428 for (
size_t i = 1; i < n; ++i)
430 EXPECT_NE(s.find(
"Dashed-Connexion " + deepest +
" 0 "), std::string::npos);
436TEST(Df_Low_Tree, agrees_with_cut_nodes_and_bridges)
438 std::mt19937
rng(20261020);
439 for (
int round = 0; round < 200; ++round)
443 std::vector<Node *> n;
460 std::set<std::string> cuts;
461 std::set<std::pair<std::string, std::string>> bridges;
462 for (
size_t i = 0; i <
nodes.size(); ++i)
466 ASSERT_EQ(
k.df,
long(i));
467 ASSERT_EQ(
k.low >= 0, p != tree.get());
471 for (
auto c = p->
get_left_child(); c !=
nullptr; c = c->get_right_sibling())
474 cut = cut or c->get_key().low >=
k.df;
475 if (c->get_key().low >
k.df)
476 bridges.insert(std::minmax(
k.label, c->get_key().label));
478 if (p == tree.get() ? children > 1 : cut)
479 cuts.insert(
k.label);
484 bool ancestor =
false;
485 for (
auto q = a.first->get_parent(); q !=
nullptr and not ancestor; q = q->get_parent())
486 ancestor = q == a.second;
487 EXPECT_TRUE(ancestor) << a.first->get_key().label <<
" " << a.second->get_key().label;
490 std::set<std::string> expected_cuts;
493 expected_cuts.insert(std::to_string(p->get_info()));
495 std::set<std::pair<std::string, std::string>> expected_bridges;
498 expected_bridges.insert(std::minmax(std::to_string(g.
get_src_node(a)->get_info()),
502 EXPECT_EQ(cuts, expected_cuts) <<
"round " << round;
503 EXPECT_EQ(bridges, expected_bridges) <<
"round " << round;
WeightedDigraph::Node Node
size_t size_t int32_t * out
Generic directed graph (digraph) wrapper template.
size_t size() const noexcept
Return the current dimension of array.
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
const Type * first() const
Get the first element.
constexpr bool is_empty() const noexcept
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
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.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
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.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Pictures of depth-first trees with their df and low numbers.
__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)
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
#define ARC_COOKIE(p)
Return the arc cookie
#define NODE_COUNTER(p)
Get the counter of a 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 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 write_df_low_tree(const GT &g, typename GT::Node *src, std::ostream &out, Node_Label node_label=Node_Label(), SA sa=SA())
Write, for ntreepic, the depth-first spanning tree of an undirected graph with the df and low numbers...
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.
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
Main namespace for Aleph-w library functions.
constexpr size_t Max_Tree_Node_Depth
The depth of the trees that generate_tree() wrote before 7.3.2; it writes trees of any depth now.
Tree_Node< Df_Low_Key > Df_Low_Tree_Node
Node of the depth-first tree that build_df_low_tree() returns.
std::pair< Df_Low_Tree_Node *, Df_Low_Tree_Node * > Df_Low_Non_Tree_Arc
Arc of the graph that is not in the depth-first tree.
and
Check uniqueness with explicit hash + equality functors.
static long & df(typename GT::Node *p)
Internal helper: DFS discovery time stored in NODE_COUNTER(p).
bool run_with_small_stack(F &&f, size_t kb=256)
Runs f in a thread whose stack has kb KiB, and returns false if the thread could not run; without POS...
Key of a node of a depth-first tree with df and low numbers.
Default label of a graph node: its information, written with operator <<.
Arc of graph implemented with double-linked adjacency lists.
Articulation points (cut nodes), bridges, and biconnected components.
Utility algorithms and operations for graphs.