Aleph-w 3.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
63#ifndef GENERATE_DF_TREE_H
64#define GENERATE_DF_TREE_H
65
66#include <tpl_graph_utils.H>
67#include <tpl_tree_node.H>
68#include <generate_tree.H>
69
71static long global_counter = 0;
72
78struct Clave
79{
80 int key;
81 long count;
82 long low;
83};
84
90{
91 bool operator () (const Clave & c1, const Clave & c2) const
92 {
93 return c1.key == c2.key;
94 }
95};
96
103{
105 {
106 Grafo::Node * gnode = static_cast<Grafo::Node *>(NODE_COOKIE(tnode));
107
108 Clave & clave = t->get_key();
109 clave.key = tnode->get_info().clave;
110 clave.count = gnode->get_info().df;
111 clave.low = gnode->get_info().low;
112 }
113};
114
120{
121 static const size_t Buf_Size = 512;
122
124 {
125 char str[2];
126 str[0] = p->get_key().key;
127 str[1] = '\0';
128 return std::string(str);
129 }
130};
131
138{
139 static const size_t Buf_Size = 512;
140
142 {
143 char buf[Buf_Size];
144 snprintf(buf, Buf_Size, "(%c,%ld)", p->get_key().key, p->get_key().count);
145 return std::string(buf);
146 }
147};
148
156{
157 static const size_t Buf_Size = 512;
158
160 {
161 char buf[Buf_Size];
162
163 if (p->get_key().low >= 0)
164 snprintf(buf, Buf_Size, "%d,%ld,%ld",
165 p->get_key().key, p->get_key().count, p->get_key().low);
166 else
167 snprintf(buf, Buf_Size, "%d,%ld,-",
168 p->get_key().key, p->get_key().count);
169
170 return std::string(buf);
171 }
172};
173
180{
181 nodo->get_info().df = global_counter++;
182}
183
189{
190 nodo->get_info().low = reinterpret_cast<long>(nodo->cookie);
191}
192
193
217template <class GT, class Key>
218void write_df_low_tree(GT & g, typename GT::Node * src, std::ofstream & f)
219{
220 // Compute cut nodes (articulation points)
222
223 depth_first_traversal(g, src, &visitar_df); // Copy df numbers
224 depth_first_traversal(g, src, &visitar_low); // Copy low-link values
225
227
228 // Compute non-tree arcs (back edges)
229 DynDlist<No_Tree_Arc> arc_list;
230 generate_non_tree_arcs(g, arc_list);
231
232 typename GT::Node * td = static_cast<typename GT::Node *>(NODE_COOKIE(src));
233
234 Tree_Node<Key> * rd = Graph_To_Tree_Node () <Grafo, Key, Convertir>(tree, td);
235
237
238 write_non_tree_arcs(arc_list, rd, f);
239}
240
241#endif // GENERATE_DF_TREE_H
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3848
typename BaseGraph::Arc Arc
Definition graph-dry.H:3852
typename BaseGraph::Node Node
Definition graph-dry.H:3851
Dynamic doubly linked list with O(1) size and bidirectional access.
Functor class to convert a tree graph to Tree_Node structure.
Generic m-ary trees.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
static long global_counter
void visitar_df(Grafo &, Grafo::Node *nodo, Grafo::Arc *)
DFS visitor that assigns discovery numbers.
void visitar_low(Grafo &, Grafo::Node *nodo, Grafo::Arc *)
DFS visitor that copies low-link values from cookies.
Tree visualization and output generation.
size_t depth_first_traversal(const GT &g, typename GT::Node *start_node, bool(*visit)(const GT &g, typename GT::Node *, typename GT::Arc *))
Depth-first traversal starting from a given node.
void write_df_low_tree(GT &g, typename GT::Node *src, std::ofstream &f)
Generate a DFS tree picture with low-link values.
DynList< typename GT::Node * > compute_cut_nodes(const GT &g, typename GT::Node *start)
Compute articulation points (cut vertices) of an undirected graph.
#define NODE_COOKIE(p)
Return the node cookie
GT find_depth_first_spanning_tree(const GT &g, typename GT::Node *gnode)
Build a depth-first spanning tree (mapped to the original graph).
DynList< T > maps(const C &c, Op op)
Classic map operation.
Equality comparator for Clave structures.
bool operator()(const Clave &c1, const Clave &c2) const
Key structure for DFS tree nodes.
int key
Original node identifier.
long count
DFS discovery number (pre-order)
long low
Low-link value (minimum reachable via back edges)
Converter from graph node to Tree_Node<Clave>.
void operator()(Grafo::Node *tnode, Tree_Node< Clave > *t)
Writer that outputs node key and DFS number.
static const size_t Buf_Size
std::string operator()(Tree_Node< Clave > *p)
Writer that outputs node key, DFS number, and low-link value.
std::string operator()(Tree_Node< Clave > *p)
static const size_t Buf_Size
Writer that outputs only the node key.
std::string operator()(Tree_Node< Clave > *p)
static const size_t Buf_Size
Utility algorithms and operations for graphs.
General tree (n-ary tree) node.
List_Digraph< Node_Nodo, Arco_Arco > Grafo