Aleph-w 3.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 <ah-graph-concepts.H>
100
101# include <tpl_tree_node.H>
102# include <tpl_graph.H>
103# include <tpl_graph_utils.H>
104 # include <ah-errors.H>
105
106namespace Aleph {
107
108 template <AlephGraph GT, typename Key, class Convert> static
110 template <AlephGraph GT, typename Key, class Convert> static void
113
114 template <AlephGraph GT, typename Key, typename Convert, ArcFilter<GT> SA> static inline
115void __graph_to_tree_node(GT & g, typename GT::Node * groot,
117
160 template <AlephGraph GT, typename Key,
161 class Convert, ArcFilter<GT> SA = Dft_Show_Arc<GT>> inline
163{
165 << "Graph is not a tree (not acyclique)";
166
167 Tree_Node<Key> * troot = new Tree_Node<Key>; // apartar memoria raíz
168
169 Convert () (groot, troot); //convertir de groot y copiar a troot
170
172
173 return troot;
174}
175
210 template <AlephGraph GT, typename Key,
211 class Convert,
214{
215 SA sa;
216 Convert * convert = nullptr;
217
219 {
220 typedef typename GT::Node Node;
221 typedef typename GT::Arc Arc;
222
223 // recorrer arcos de groot y construir recursivamente
224 for (Node_Arc_Iterator<GT, SA> it(groot, sa); it.has_curr(); it.next_ne())
225 {
226 Arc * arc = it.get_current_arc_ne();
227 if (IS_ARC_VISITED(arc, Convert_Tree))
228 continue;
229
230 ARC_BITS(arc).set_bit(Convert_Tree, true); // arc visitado
231 Node * gtgt = it.get_tgt_node();
233
234 (*convert) (gtgt, ttgt); // asignarle la clave
235
236 troot->insert_rightmost_child(ttgt); // insertarlo como hijo
237
239 }
240 }
241
243 typename GT::Node * groot,
244 Convert & conv)
245 {
247 << "Graph is not a tree (not acyclique)";
248
249 Tree_Node<Key> * troot = new Tree_Node<Key>; // apartar memoria raíz
250
251 convert = &conv;
252
253 (*convert) (groot, troot); //convertir de groot y copiar a troot
254
256
257 return troot;
258 }
259
260public:
261
262 Graph_To_Tree_Node(SA __sa = SA()) : sa(__sa) { /* empty */ }
263
276 {
277 return graph_to_tree(g, groot, conv);
278 }
279
282 {
283 return graph_to_tree(g, groot, conv);
284 }
285};
286
287
288 template <AlephGraph GT, typename Key, typename Convert, ArcFilter<GT> SA> static inline
291{
292 typedef typename GT::Node Node;
293 typedef typename GT::Arc Arc;
294
295 // recorrer arcos de groot y construir recursivamente
296 for (Node_Arc_Iterator<GT, SA> it(groot); it.has_curr(); it.next_ne())
297 {
298 Arc * arc = it.get_current_arc_ne();
299 if (IS_ARC_VISITED(arc, Convert_Tree))
300 continue;
301
302 ARC_BITS(arc).set_bit(Convert_Tree, true); // arc visitado
303 Node * gtgt = it.get_tgt_node();
305
306 Convert () (gtgt, ttgt); // asignarle la clave
307
308 troot->insert_rightmost_child(ttgt); // insertarlo como hijo
309
311 }
312}
313
314} // end namespace Aleph
315
316# 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:527
C++20 concepts for the protocol shared by graph algorithms.
WeightedDigraph::Node Node
WeightedDigraph::Arc Arc
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
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)
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.
A graph usable by the graph algorithms.
An arc filter (SA) for graph GT.
#define ARC_BITS(p)
Return the control bits of arc p.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
@ Convert_Tree
Definition aleph-graph.H:81
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
static Tree_Node< Key > * graph_to_tree_node(GT &g, typename GT::Node *groot)
static void __graph_to_tree_node(GT &g, typename GT::Node *groot, Tree_Node< Key > *troot)
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1001
Filtered iterator of adjacent arcs of a node.
Definition tpl_graph.H:1120
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
General tree (n-ary tree) node.