Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_gen_tree.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32# include <iostream>
33# include <fstream>
34# include <memory>
35# include <string>
36# include <tpl_graph.H>
37# include <graph_to_tree.H>
38# include <generate_tree.H>
39
40using namespace std;
41
42# define INDENT " "
43
44using namespace Aleph;
45
46struct Ciudad
47{
49
50 string nombre;
51
53
54 Ciudad() : tipo(DESCONOCIDO) { /* empty */ }
55
56 Ciudad(const Ciudad & c) : nombre(c.nombre), tipo(c.tipo) { /* empty */ }
57
58 Ciudad(const char * nom) : nombre(nom), tipo(DESCONOCIDO) { /* empty */ }
59
60 Ciudad(char * nom) : nombre(nom), tipo(DESCONOCIDO) { /* empty */ }
61
62 Ciudad(const string & str) : nombre(str), tipo(DESCONOCIDO) { /* empty */ }
63
64 bool operator == (const Ciudad & c) const
65 {
66 return nombre == c.nombre;
67 }
68};
69
70struct Via
71{
74
75 string nombre;
78
79 Via() : nombre("Desconocido"), distancia(0), tipo(DESCONOCIDO) {}
80
81 Via(int d)
82 : nombre("Desconocido"), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
83
84 Via(char * nom, int d)
85 : nombre(nom), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
86
87 Via(const string& nom, int d)
88 : nombre(nom), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
89
90 typedef int Distance_Type;
91
93
94 static const Distance_Type Zero_Distance = 0;
95};
96
98
100
101
103
105
106
107
109{
110 bool operator () (const Ciudad & c1, const Ciudad & c2)
111 {
112 return c1.nombre == c2.nombre;
113 }
114};
115
116
117Mapa::Node * buscar_ciudad(Mapa& mapa, const string & nombre)
118{
119 return mapa.search_node([&nombre] (Mapa::Node * p)
120 {
121 return p->get_info().nombre == nombre;
122 });
123}
124
126 const string & c1, const string & c2,
127 int distancia)
128{
130
131 if (n1 == nullptr)
132 n1 = mapa.insert_node(c1);
133
135
136 if (n2 == nullptr)
137 n2 = mapa.insert_node(c2);
138
139 string nombre_arco = n1->get_info().nombre + "--" + n2->get_info().nombre;
140
141 mapa.insert_arc(n1, n2, Via(nombre_arco, distancia));
142}
143
144
146{
147 cout << endl
148 << "Camino: ";
149 for (Path<Mapa>::Iterator itor(path); itor.has_curr(); itor.next())
150 cout << itor.get_current_node()->get_info().nombre << "-";
151
152 cout << endl;
153}
154
155
157{
158 cout << endl
159 << "Listado de nodos (" << g.get_num_nodes() << ")" << endl;
160
161 for (Mapa::Node_Iterator node_itor(g); node_itor.has_curr();
162 node_itor.next())
163 cout << INDENT << node_itor.get_current_node()->get_info().nombre << endl;
164
165 cout << endl
166 << endl
167 << "Listado de arcos (" << g.get_num_arcs() << ")"
168 << endl;
169
171 arc_itor.next())
172 {
173 Mapa::Arc * arc = arc_itor.get_current_arc();
174 cout << arc->get_info().nombre << " " << arc->get_info().distancia
175 << " de " << g.get_src_node(arc)->get_info().nombre
176 << " a " << g.get_tgt_node(arc)->get_info().nombre << endl;
177 }
178
179 cout << endl
180 << endl
181 << "Listado del grafo por nodos y en cada nodo por arcos"
182 << endl;
183 for (Mapa::Node_Iterator node_itor(g); node_itor.has_curr();
184 node_itor.next())
185 {
186 Mapa::Node * src_node = node_itor.get_current_node();
187 cout << src_node->get_info().nombre << endl;
188 for (Mapa::Node_Arc_Iterator itor(node_itor.get_current_node());
189 itor.has_curr(); itor.next())
190 {
191 Mapa::Arc * arc = itor.get_current_arc();
192 cout << INDENT << arc->get_info().distancia << " "
193 << g.get_connected_node(arc, src_node)->get_info().nombre
194 << endl;
195 }
196 }
197 cout << endl;
198}
199
201{
202 insert_via(g, "San Cristobal", "La Fria", 69);
203 insert_via(g, "San Cristobal", "Sacramento", 113);
204 insert_via(g, "San Cristobal", "San Antonio", 36);
205 insert_via(g, "Rubio", "Caparo", 150);
206 insert_via(g, "La Fria", "El Vigia", 86);
207 insert_via(g, "El Vigia", "Santa Barbara", 59);
208 insert_via(g, "El Vigia", "Merida", 79);
209 insert_via(g, "La Fria", "Machiques", 252);
210 insert_via(g, "Valera", "Merida", 167);
211 insert_via(g, "Valera", "Carora", 120);
212 insert_via(g, "Carora", "Barquisimeto", 102);
213 insert_via(g, "Merida", "Barinas", 180);
214 insert_via(g, "Barinas", "Guanare", 94);
215}
216
217
218 template <typename GT>
220{
222 {
223 t->get_key() = g->get_info().nombre;
224 }
225};
226
227
229{
231 {
232 return p->get_key();
233 }
234};
235
236
237int main()
238{
239 Mapa tree;
240
241 construir_mapa(tree);
242
243 imprimir_mapa(tree);
244
245 Mapa::Node * c = buscar_ciudad(tree, "Merida");
246 if (c == nullptr)
247 {
248 cerr << "Error: buscar_ciudad(tree, \"Merida\") returned nullptr" << endl;
249 return 1;
250 }
251
252 std::unique_ptr<Tree_Node<string>> t(
253 Graph_To_Tree_Node <Mapa, string, GT_Tree<Mapa> > () (tree, c));
254
255 ofstream test("prueba.Tree", ios::trunc);
256 if (not test)
257 {
258 cerr << "Error: could not open prueba.Tree for writing" << endl;
259 return 1;
260 }
261
263}
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3960
Functor class to convert a tree graph to Tree_Node structure.
Iterator on the arcs of a graph.
Definition tpl_graph.H:831
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:429
_Graph_Node Node
The graph type.
Definition tpl_graph.H:433
_Graph_Arc Arc
The node class type.
Definition tpl_graph.H:434
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:3311
Path on a graph.
Definition tpl_graph.H:2772
Forward declaration used by CRTP helpers before the full node definition.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:536
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:779
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:820
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:826
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:785
Tree visualization and output generation.
Convert spanning tree graphs to Tree_Node structures.
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
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
STL namespace.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:223
Node belonging to a graph implemented with a double linked adjacency list.
Definition tpl_graph.H:122
Iterator on all arcs of a graph.
Definition tpl_graph.H:918
bool operator()(const Ciudad &c1, const Ciudad &c2)
Ciudad(const char *nom)
Tipo_Ciudad tipo
string nombre
Ciudad(const Ciudad &c)
bool operator==(const Ciudad &c) const
Ciudad(const string &str)
Ciudad(char *nom)
void operator()(typename GT::Node *g, Tree_Node< string > *t)
string nombre
static const Distance_Type Zero_Distance
Via(const string &nom, int d)
int Distance_Type
Tipo_Via tipo
@ CARRETERA1
@ CHALANA
@ CARRETERA3
@ AUTOPISTA
@ CARRETERA2
@ DESCONOCIDO
@ GRANZON
Distance_Type & get_distance()
int distancia
Via(int d)
Via(char *nom, int d)
const string operator()(Tree_Node< string > *p)
void test()
Definition test-comb.C:40
void imprimir_camino(Path< Mapa > &path)
void imprimir_mapa(Mapa &g)
List_Graph< Nodo_Ciudad, Arco_Via > Mapa
List_Digraph< Nodo_Ciudad, Arco_Via > Dimapa
Graph_Node< Ciudad > Nodo_Ciudad
#define INDENT
Mapa::Node * buscar_ciudad(Mapa &mapa, const string &nombre)
Graph_Arc< Via > Arco_Via
void insert_via(Mapa &mapa, const string &c1, const string &c2, int distancia)
void construir_mapa(Mapa &g)
int main()
Generic graph and digraph implementations.