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 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27# include <iostream>
28# include <fstream>
29# include <memory>
30# include <string>
31# include <tpl_graph.H>
32# include <graph_to_tree.H>
33# include <generate_tree.H>
34
35using namespace std;
36
37# define INDENT " "
38
39using namespace Aleph;
40
41struct Ciudad
42{
44
45 string nombre;
46
48
49 Ciudad() : tipo(DESCONOCIDO) { /* empty */ }
50
51 Ciudad(const Ciudad & c) : nombre(c.nombre), tipo(c.tipo) { /* empty */ }
52
53 Ciudad(const char * nom) : nombre(nom), tipo(DESCONOCIDO) { /* empty */ }
54
55 Ciudad(char * nom) : nombre(nom), tipo(DESCONOCIDO) { /* empty */ }
56
57 Ciudad(const string & str) : nombre(str), tipo(DESCONOCIDO) { /* empty */ }
58
59 bool operator == (const Ciudad & c) const
60 {
61 return nombre == c.nombre;
62 }
63};
64
65struct Via
66{
69
70 string nombre;
73
74 Via() : nombre("Desconocido"), distancia(0), tipo(DESCONOCIDO) {}
75
76 Via(int d)
77 : nombre("Desconocido"), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
78
79 Via(char * nom, int d)
80 : nombre(nom), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
81
82 Via(const string& nom, int d)
83 : nombre(nom), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
84
85 typedef int Distance_Type;
86
88
89 static const Distance_Type Zero_Distance = 0;
90};
91
93
95
96
98
100
101
102
104{
105 bool operator () (const Ciudad & c1, const Ciudad & c2)
106 {
107 return c1.nombre == c2.nombre;
108 }
109};
110
111
112Mapa::Node * buscar_ciudad(Mapa& mapa, const string & nombre)
113{
114 return mapa.search_node([&nombre] (Mapa::Node * p)
115 {
116 return p->get_info().nombre == nombre;
117 });
118}
119
121 const string & c1, const string & c2,
122 int distancia)
123{
125
126 if (n1 == nullptr)
127 n1 = mapa.insert_node(c1);
128
130
131 if (n2 == nullptr)
132 n2 = mapa.insert_node(c2);
133
134 string nombre_arco = n1->get_info().nombre + "--" + n2->get_info().nombre;
135
136 mapa.insert_arc(n1, n2, Via(nombre_arco, distancia));
137}
138
139
141{
142 cout << endl
143 << "Camino: ";
144 for (Path<Mapa>::Iterator itor(path); itor.has_curr(); itor.next())
145 cout << itor.get_current_node()->get_info().nombre << "-";
146
147 cout << endl;
148}
149
150
152{
153 cout << endl
154 << "Listado de nodos (" << g.get_num_nodes() << ")" << endl;
155
156 for (Mapa::Node_Iterator node_itor(g); node_itor.has_curr();
157 node_itor.next())
158 cout << INDENT << node_itor.get_current_node()->get_info().nombre << endl;
159
160 cout << endl
161 << endl
162 << "Listado de arcos (" << g.get_num_arcs() << ")"
163 << endl;
164
166 arc_itor.next())
167 {
168 Mapa::Arc * arc = arc_itor.get_current_arc();
169 cout << arc->get_info().nombre << " " << arc->get_info().distancia
170 << " de " << g.get_src_node(arc)->get_info().nombre
171 << " a " << g.get_tgt_node(arc)->get_info().nombre << endl;
172 }
173
174 cout << endl
175 << endl
176 << "Listado del grafo por nodos y en cada nodo por arcos"
177 << endl;
178 for (Mapa::Node_Iterator node_itor(g); node_itor.has_curr();
179 node_itor.next())
180 {
181 Mapa::Node * src_node = node_itor.get_current_node();
182 cout << src_node->get_info().nombre << endl;
183 for (Mapa::Node_Arc_Iterator itor(node_itor.get_current_node());
184 itor.has_curr(); itor.next())
185 {
186 Mapa::Arc * arc = itor.get_current_arc();
187 cout << INDENT << arc->get_info().distancia << " "
188 << g.get_connected_node(arc, src_node)->get_info().nombre
189 << endl;
190 }
191 }
192 cout << endl;
193}
194
196{
197 insert_via(g, "San Cristobal", "La Fria", 69);
198 insert_via(g, "San Cristobal", "Sacramento", 113);
199 insert_via(g, "San Cristobal", "San Antonio", 36);
200 insert_via(g, "Rubio", "Caparo", 150);
201 insert_via(g, "La Fria", "El Vigia", 86);
202 insert_via(g, "El Vigia", "Santa Barbara", 59);
203 insert_via(g, "El Vigia", "Merida", 79);
204 insert_via(g, "La Fria", "Machiques", 252);
205 insert_via(g, "Valera", "Merida", 167);
206 insert_via(g, "Valera", "Carora", 120);
207 insert_via(g, "Carora", "Barquisimeto", 102);
208 insert_via(g, "Merida", "Barinas", 180);
209 insert_via(g, "Barinas", "Guanare", 94);
210}
211
212
213 template <typename GT>
215{
217 {
218 t->get_key() = g->get_info().nombre;
219 }
220};
221
222
224{
226 {
227 return p->get_key();
228 }
229};
230
231
232int main()
233{
234 Mapa tree;
235
236 construir_mapa(tree);
237
238 imprimir_mapa(tree);
239
240 Mapa::Node * c = buscar_ciudad(tree, "Merida");
241 if (c == nullptr)
242 {
243 cerr << "Error: buscar_ciudad(tree, \"Merida\") returned nullptr" << endl;
244 return 1;
245 }
246
247 std::unique_ptr<Tree_Node<string>> t(
248 Graph_To_Tree_Node <Mapa, string, GT_Tree<Mapa> > () (tree, c));
249
250 ofstream test("prueba.Tree", ios::trunc);
251 if (not test)
252 {
253 cerr << "Error: could not open prueba.Tree for writing" << endl;
254 return 1;
255 }
256
258}
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3854
Functor class to convert a tree graph to Tree_Node structure.
Iterator on the arcs of a graph.
Definition tpl_graph.H:830
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:428
_Graph_Node Node
The graph type.
Definition tpl_graph.H:432
_Graph_Arc Arc
The node class type.
Definition tpl_graph.H:433
Iterator on nodes and arcs of a path.
Definition tpl_graph.H:3208
Path on a graph.
Definition tpl_graph.H:2669
Generic m-ary trees.
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:494
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:737
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Definition graph-dry.H:778
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:784
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:743
Tree visualization and output generation.
Convert spanning tree graphs to Tree_Node structures.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Node belonging to a graph implemented with a double linked adjacency list.
Definition tpl_graph.H:121
Iterator on all arcs of a graph.
Definition tpl_graph.H:917
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:35
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.