Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_to_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
96# ifndef GRAPH_TO_TREE_H
97# define GRAPH_TO_TREE_H
98
99# include <memory>
100
101# include <ah-graph-concepts.H>
102
103# include <tpl_tree_node.H>
104# include <tpl_dfs_stack.H>
105# include <tpl_graph.H>
106# include <tpl_graph_utils.H>
107# include <tpl_test_acyclique.H>
108# include <ah-errors.H>
109
110namespace Aleph {
111
112 namespace graph_to_tree_detail
113 {
117 template <class GT, typename Key, class SA>
118 struct Frame
119 {
121 typename GT::Arc * from;
123
124 template <class... Args>
125 [[gnu::always_inline]] ALEPH_FLATTEN
126 Frame(Tree_Node<Key> * t, typename GT::Arc * a, Args &&... args)
127 : troot(t), from(a), it(std::forward<Args>(args)...) {}
128
129 [[gnu::always_inline]] ALEPH_FLATTEN
130 void next_arc() noexcept(noexcept(it.next_ne())) { it.next_ne(); }
131
132 // Moves the iterator to the next arc that is not that through which
133 // the node was reached, and gives its target, or nullptr when the
134 // arcs are done. In a tree, which the graph is, the other arcs go
135 // to nodes not reached yet, so that the arcs need no marks.
136 [[gnu::always_inline]] ALEPH_FLATTEN typename GT::Node * next_target()
137 noexcept(noexcept(it.next_ne()))
138 {
139 for (; it.has_curr(); it.next_ne())
140 if (it.get_current_arc_ne() != from)
141 return it.get_tgt_node_ne();
142 return nullptr;
143 }
144 };
145
146 // Builds below troot the tree of what groot reaches. For each node that
147 // it reaches, it gives a new tree node and the graph node to
148 // convert(gnode, tnode), and puts the tree node as the rightmost child
149 // of that of the node from which it was reached. A path can have as
150 // many nodes as the graph, too many for recursion, so that the search
151 // keeps its own stack; the order of the visits is that of a recursion.
152 // If something throws, the nodes already put below troot stay there.
153 template <class GT, typename Key, class SA, class Convert, class... Args>
154 inline void build(Tree_Node<Key> * troot, Convert && convert,
155 typename GT::Node * groot, Args &&... args)
156 {
158
159 // Gives the tree node of gtgt, the rightmost child of that of f
160 auto child_of = [&convert](Frame & f, typename GT::Node * gtgt)
161 {
162 // freed if convert throws
163 std::unique_ptr<Tree_Node<Key>> ttgt(new Tree_Node<Key>);
164
165 convert(gtgt, ttgt.get()); // assign its key
166
167 f.troot->insert_rightmost_child(ttgt.get()); // insert it as a child
168 return ttgt.release();
169 };
170
171 // The node being visited keeps its iterator in here until it visits
172 // a node; then it waits in frames, where it goes on when it comes
173 // back. here is optional, so that the frame of each node is built in
174 // its place: a frame keeps the filter, which may not be assignable
175 Dfs_Stack<Frame> frames;
176 std::optional<Frame> here(std::in_place, troot, nullptr, groot, args...);
177 while (true)
178 {
179 if (auto gtgt = here->next_target())
180 { // the iterator stays on the arc to gtgt, as in a recursion
181 auto child = child_of(*here, gtgt);
182 auto arc = here->it.get_current_arc_ne();
183 frames.push(std::move(*here));
184 here.emplace(child, arc, gtgt, args...);
185 continue;
186 }
187
188 while (true) // back to the nodes that wait
189 {
190 if (frames.is_empty())
191 return;
192
193 Frame & f = frames.top();
194 f.next_arc(); // past the arc to the node that was visited
195 if (auto gtgt = f.next_target())
196 {
197 here.emplace(child_of(f, gtgt), f.it.get_current_arc_ne(),
198 gtgt, args...);
199 break;
200 }
201 frames.pop();
202 }
203 }
204 }
205 } // namespace graph_to_tree_detail
206
207 template <AlephGraph GT, typename Key, typename Convert, ArcFilter<GT> SA> static inline
208void __graph_to_tree_node(GT & g, typename GT::Node * groot,
209 Tree_Node<Key> * troot, const SA & sa);
210
258 template <AlephGraph GT, typename Key,
259 class Convert, ArcFilter<GT> SA = Dft_Show_Arc<GT>> inline
261 SA sa = SA())
262{
263 // acyclic by the arcs of the traversal, those that sa shows
265 << "Graph is not a tree (not acyclique)";
266
267 Tree_Node<Key> * troot = new Tree_Node<Key>; // the root
268 try
269 {
270 Convert () (groot, troot); // convert groot into troot
271
272 __graph_to_tree_node <GT, Key, Convert, SA> (g, groot, troot, sa);
273 }
274 catch (...)
275 {
276 destroy_tree(troot); // what was built
277 throw;
278 }
279
280 return troot;
281}
282
317 template <AlephGraph GT, typename Key,
318 class Convert,
319 ArcFilter<GT> SA = Dft_Show_Arc<GT> >
321{
322 SA sa;
323 Convert * convert = nullptr;
324
325 void graph_to_tree(typename GT::Node * groot, Tree_Node<Key> * troot)
326 {
327 // go through the arcs of groot and build the tree
328 graph_to_tree_detail::build<GT, Key, SA>(troot, *convert, groot, sa);
329 }
330
332 typename GT::Node * groot,
333 Convert & conv)
334 {
335 // acyclic by the arcs that the filter shows, which the tree takes
337 << "Graph is not a tree (not acyclique)";
338
339 Tree_Node<Key> * troot = new Tree_Node<Key>; // the root
340
341 convert = &conv;
342 try
343 {
344 (*convert) (groot, troot); // convert groot into troot
345
346 graph_to_tree(groot, troot);
347 }
348 catch (...)
349 {
350 destroy_tree(troot); // what was built
351 throw;
352 }
353
354 return troot;
355 }
356
357public:
358
359 Graph_To_Tree_Node(SA __sa = SA()) : sa(__sa) { /* empty */ }
360
378 operator () (GT & g, typename GT::Node * groot, Convert && conv = Convert())
379 {
380 return graph_to_tree(g, groot, conv);
381 }
382
384 operator () (GT & g, typename GT::Node * groot, Convert & conv)
385 {
386 return graph_to_tree(g, groot, conv);
387 }
388};
389
390
391 template <AlephGraph GT, typename Key, typename Convert, ArcFilter<GT> SA> static inline
392void __graph_to_tree_node(GT & g, typename GT::Node * groot,
393 Tree_Node<Key> * troot, const SA & sa)
394{
395 (void) g;
396
397 // go through the arcs of groot and build the tree
398 graph_to_tree_detail::build<GT, Key, SA>
399 (troot, [](typename GT::Node * gtgt, Tree_Node<Key> * ttgt)
400 {
401 Convert () (gtgt, ttgt);
402 }, groot, sa);
403}
404
405} // end namespace Aleph
406
407# endif // GRAPH_TO_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
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
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.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Functor class to convert a tree graph to Tree_Node structure.
Tree_Node< Key > * operator()(GT &g, typename GT::Node *groot, Convert &&conv=Convert())
Convert a tree graph to Tree_Node structure.
void graph_to_tree(typename GT::Node *groot, Tree_Node< Key > *troot)
Graph_To_Tree_Node(SA __sa=SA())
Tree_Node< Key > * graph_to_tree(GT &g, typename GT::Node *groot, Convert &conv)
Determines whether a graph is acyclic (contains no cycles).
Forward declaration used by CRTP helpers before the full node definition.
void insert_rightmost_child(Tree_Node *p) noexcept
Inserts p as the rightmost child of this.
Tree_Node< Key > * graph_to_tree_node(GT &g, typename GT::Node *groot, SA sa=SA())
Convert a tree graph to a Tree_Node structure.
#define ALEPH_FLATTEN
[[gnu::flatten]], or nothing where it would only make the compilation slower.
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
void build(Tree_Node< Key > *troot, Convert &&convert, typename GT::Node *groot, Args &&... args)
Main namespace for Aleph-w library functions.
static void __graph_to_tree_node(GT &g, typename GT::Node *groot, Tree_Node< Key > *troot, const SA &sa)
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).
Definition ah-zip.H:252
STL namespace.
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
A node of the tree being built that waits for the one that it visits, its image in the tree,...
Node_Arc_Iterator< GT, SA > it
Frame(Tree_Node< Key > *t, typename GT::Arc *a, Args &&... args)
GT::Node * next_target() noexcept(noexcept(it.next_ne()))
void next_arc() noexcept(noexcept(it.next_ne()))
Stack of the frames of a depth-first search that does not recurse.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
DAG (acyclic) graph testing.
General tree (n-ary tree) node.