Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
generate_df_tree.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
75#ifndef GENERATE_DF_TREE_H
76#define GENERATE_DF_TREE_H
77
78# include <algorithm>
79# include <memory>
80# include <ostream>
81# include <sstream>
82# include <string>
83# include <utility>
84
85# include <ah-errors.H>
86# include <ah-graph-concepts.H>
87# include <generate_tree.H>
88# include <htlist.H>
89# include <tpl_dfs_stack.H>
90# include <tpl_dynMapTree.H>
91# include <tpl_graph.H>
92# include <tpl_tree_node.H>
93
94namespace Aleph
95{
103 {
104 std::string label;
105 long df = -1;
106 long low = -1;
107 };
108
111
117 using Df_Low_Non_Tree_Arc = std::pair<Df_Low_Tree_Node *, Df_Low_Tree_Node *>;
118
126 template <AlephGraph GT>
128 {
130 std::string operator () (typename GT::Node * p) const
131 {
132 std::ostringstream s;
133 s << p->get_info();
134 return s.str();
135 }
136 };
137
143 {
145 std::string operator () (Df_Low_Tree_Node * p) const
146 {
147 return p->get_key().label;
148 }
149 };
150
157 {
159 std::string operator () (Df_Low_Tree_Node * p) const
160 {
161 const Df_Low_Key & key = p->get_key();
162 return "(" + key.label + "," + std::to_string(key.df) + ")";
163 }
164 };
165
174 {
176 std::string operator () (Df_Low_Tree_Node * p) const
177 {
178 const Df_Low_Key & key = p->get_key();
179 return key.label + "," + std::to_string(key.df) + "," +
180 (key.low >= 0 ? std::to_string(key.low) : std::string("-"));
181 }
182 };
183
184 namespace df_tree_detail
185 {
187 template <AlephGraph GT, class Node_Label, ArcFilter<GT> SA>
189 {
190 using Node = typename GT::Node;
191 using Arc = typename GT::Arc;
192
193 Node_Label & node_label;
194 SA & sa;
196
197 // Tree node of each visited graph node; it owns them until build()
198 // returns
200 long curr_df = 0;
201
202 // Create the tree node of p, with the next df number. tree_nodes
203 // takes it; if its insertion throws, the guard frees it. The guard is
204 // not a unique_ptr, whose release() would say that the caller takes it
206 {
207 struct Guard
208 {
209 Df_Low_Tree_Node * tp;
210 ~Guard() { delete tp; }
212
213 Df_Low_Tree_Node * tp = guard.tp;
214 tree_nodes.insert(p, tp);
215 guard.tp = nullptr; // tree_nodes took it
216 ++curr_df;
217 return tp;
218 }
219
220 // A node whose arcs the traversal goes through: the graph node, its
221 // tree node, the arc through which it was reached, and the next arc
222 // to look at
223 struct Frame
224 {
229
230 Frame(Node * n, Df_Low_Tree_Node * t, Arc * a, SA & filter)
231 : p(n), tp(t), tree_arc(a), it(n, filter) {}
232 };
233
234 // Traverse from src, whose tree node is root. A path can have as many
235 // nodes as the graph, too many for recursion, so that the traversal
236 // keeps its own stack; the order of the visits is that of a
237 // recursion.
239 {
240 Dfs_Stack<Frame> frames;
241 frames.push(src, root, nullptr, sa);
242 while (true)
243 {
244 Frame & f = frames.top();
245 Df_Low_Key & key = f.tp->get_key();
246 Node * next = nullptr;
247 for (; f.it.has_curr(); f.it.next_ne())
248 {
249 Arc * a = f.it.get_curr();
250 if (a == f.tree_arc)
251 continue;
252
253 Node * t = f.it.get_tgt_node();
254 if (auto pair = tree_nodes.search(t); pair != nullptr)
255 { // a visited t is an ancestor of p, a descendant or p itself
256 Df_Low_Tree_Node * tt = pair->second;
257 if (tt->get_key().df < key.df)
258 {
259 key.low = std::min(key.low, tt->get_key().df);
261 }
262 continue;
263 }
264
265 next = t; // the iterator stays on the arc, as in a recursion
266 break;
267 }
268
269 if (next != nullptr)
270 {
271 // a child is linked while it is still a leaf, as Tree_Node
272 // needs
274 f.tp->insert_rightmost_child(child);
275 frames.push(next, child, f.it.get_curr(), sa); // f may move
276 continue;
277 }
278
279 // the arcs of f.p are done: back to the node that reached it
280 const long low = key.low;
281 frames.pop();
282 if (frames.is_empty())
283 return;
284
285 Frame & parent = frames.top();
286 Df_Low_Key & parent_key = parent.tp->get_key();
287 parent_key.low = std::min(parent_key.low, low);
288 parent.it.next_ne(); // past the arc to the child
289 }
290 }
291
292 public:
295 Df_Low_Builder(Node_Label & label, SA & filter,
298 {
299 // empty
300 }
301
304 {
305 try
306 {
308 visit(src, root);
309 root->get_key().low = -1; // as Compute_Cut_Nodes leaves it
310 return root;
311 }
312 catch (...)
313 {
314 for (auto it = tree_nodes.get_it(); it.has_curr(); it.next_ne())
315 delete it.get_curr().second;
317 throw;
318 }
319 }
320 };
321
326 {
328 size_t index;
329 };
330
339 {
340 for (auto it = arcs.get_it(); it.has_curr(); it.next_ne())
341 {
342 (void) numbers.insert(it.get_curr().first, std::string());
343 (void) numbers.insert(it.get_curr().second, std::string());
344 }
345
346 size_t missing = numbers.size(); // the nodes that wait for a number
347 if (missing == 0)
348 return;
349
350 std::string number = "0"; // that of p
353 while (true)
354 {
355 if (auto pair = numbers.search(p); pair != nullptr)
356 {
357 pair->second = number;
358 if (--missing == 0)
359 return;
360 }
361
362 if (auto child = p->get_left_child(); child != nullptr)
363 {
364 path.push(Dewey_Level{number.size(), 0});
365 number += ".0";
366 p = child;
367 continue;
368 }
369
370 // the right sibling of p, or of its lowest ancestor below root
371 // that has one; the parent of a last child is at once
372 while (p != root and p->is_rightmost())
373 {
374 p = p->get_parent();
375 number.resize(path.top().parent_length);
376 path.pop();
377 }
378
379 if (p == root)
380 return;
381
382 p = p->get_right_sibling();
383 Dewey_Level & level = path.top();
384 number.resize(level.parent_length);
385 number += '.';
386 number += std::to_string(++level.index);
387 }
388 }
389
392 {
395 {
397 }
398 };
399 } // namespace df_tree_detail
400
444 template <AlephGraph GT, class Node_Label = Dft_Df_Tree_Label<GT>,
445 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
446 [[nodiscard]] Df_Low_Tree_Node *
447 build_df_low_tree(const GT & g, typename GT::Node * src,
448 DynList<Df_Low_Non_Tree_Arc> & non_tree_arcs,
449 Node_Label node_label = Node_Label(), SA sa = SA())
450 {
452 << "build_df_low_tree(): g is a digraph";
453 ah_invalid_argument_if(src == nullptr)
454 << "build_df_low_tree(): src must be non-null";
455
456 non_tree_arcs.empty();
458 (node_label, sa, non_tree_arcs).build(src);
459 }
460
497 template <class Write = Write_Df_Tree_Low, AlephGraph GT,
498 class Node_Label = Dft_Df_Tree_Label<GT>,
499 ArcFilter<GT> SA = Dft_Show_Arc<GT>>
500 void write_df_low_tree(const GT & g, typename GT::Node * src,
501 std::ostream & out,
502 Node_Label node_label = Node_Label(), SA sa = SA())
503 {
504 DynList<Df_Low_Non_Tree_Arc> non_tree_arcs;
505 std::unique_ptr<Df_Low_Tree_Node, df_tree_detail::Tree_Deleter> root
506 (build_df_low_tree(g, src, non_tree_arcs, node_label, sa));
507
508 generate_tree<Df_Low_Tree_Node, Write>(root.get(), out);
509
511 df_tree_detail::number_tree(root.get(), non_tree_arcs, numbers);
512
513 size_t i = 0;
514 for (auto it = non_tree_arcs.get_it(); it.has_curr(); it.next_ne(), ++i)
515 {
516 const Df_Low_Non_Tree_Arc & arc = it.get_curr();
517 out << "Dashed-Connexion " << numbers.find(arc.first) << " "
518 << numbers.find(arc.second) << (i % 2 == 0 ? " L" : " R") << '\n';
519 }
520 }
521} // namespace Aleph
522
523#endif // GENERATE_DF_TREE_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
size_t size_t int32_t * out
Definition ca-c-api.h:120
Stack of the frames of a depth-first search that does not recurse.
T & push(Args &&...args)
Builds a frame on top of the stack.
bool is_empty() const noexcept
Whether the stack has no frames.
T & top() noexcept
The top frame; the stack must not be empty.
void pop() noexcept
Destroys the top frame; the stack must not be empty.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
void empty() noexcept
empty the list
Definition htlist.H:1576
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
const size_t & size() const
Returns the cardinality of the set.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Forward declaration used by CRTP helpers before the full node definition.
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
constexpr bool is_rightmost() const noexcept
Returns true if this is the rightmost node among its siblings.
Tree_Node * get_right_sibling() const noexcept
Returns the right sibling of this.
void insert_rightmost_child(Tree_Node *p) noexcept
Inserts p as the rightmost child of this.
Tree_Node * get_parent() const noexcept
Returns the parent of this.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
Depth-first traversal that builds the tree of build_df_low_tree().
DynMapTree< Node *, Df_Low_Tree_Node * > tree_nodes
Df_Low_Builder(Node_Label &label, SA &filter, DynList< Df_Low_Non_Tree_Arc > &arcs)
Prepare a traversal that labels the nodes with label, sees only the arcs that filter accepts and appe...
Df_Low_Tree_Node * build(Node *src)
Build the tree from src; on an exception, free what it built.
Df_Low_Tree_Node * new_tree_node(Node *p)
DynList< Df_Low_Non_Tree_Arc > & non_tree_arcs
void visit(Node *src, Df_Low_Tree_Node *root)
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
Tree visualization and output generation.
__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)
Definition gmpfrxx.h:4138
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
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.
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...
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
Singly linked list implementations with head-tail access.
void number_tree(Df_Low_Tree_Node *root, const DynList< Df_Low_Non_Tree_Arc > &arcs, DynMapTree< Df_Low_Tree_Node *, std::string > &numbers)
Give to each node that an arc of arcs names its Dewey number, as generate_tree() writes them.
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.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
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.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
Key of a node of a depth-first tree with df and low numbers.
long df
Depth-first number: 0 for the start node.
std::string label
Label of the graph node.
long low
Low number; -1 for the start node, which has none.
Default label of a graph node: its information, written with operator <<.
std::string operator()(typename GT::Node *p) const
Return the information of p, written with operator <<.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1155
Writer for generate_tree() that shows the label and the df number of each node, as (label,...
std::string operator()(Df_Low_Tree_Node *p) const
Return (label,df) for p.
Writer for generate_tree() that shows the label of each node.
std::string operator()(Df_Low_Tree_Node *p) const
Return the label of p.
Writer for generate_tree() that shows the label, the df and the low numbers of each node,...
std::string operator()(Df_Low_Tree_Node *p) const
Return label,df,low for p, or label,df,- if it has no low number.
A level of the path that number_tree() follows: the length of the Dewey number of the parent,...
Frame(Node *n, Df_Low_Tree_Node *t, Arc *a, SA &filter)
Deleter that frees a whole tree.
void operator()(Df_Low_Tree_Node *root) const
Free root and every node below it.
Stack of the frames of a depth-first search that does not recurse.
Dynamic key-value map based on balanced binary search trees.
Generic graph and digraph implementations.
General tree (n-ary tree) node.