Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_search_deway.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 <fstream>
33# include <iostream>
34# include <memory>
35# include <string>
36# include <ah-errors.H>
37# include <tpl_graph.H>
38# include <graph_to_tree.H>
39# include <generate_tree.H>
40
41using namespace std;
42
43using namespace Aleph;
44
45# define INDENT " "
46
47struct Ciudad
48{
50
51 string nombre;
52
54
55 Ciudad() : tipo(DESCONOCIDO) { /* empty */ }
56
57 Ciudad(const Ciudad & c) : nombre(c.nombre), tipo(c.tipo) { /* empty */ }
58
59 Ciudad(const char * nom) : nombre(nom), tipo(DESCONOCIDO) { /* empty */ }
60
61 Ciudad(char * nom) : nombre(nom), tipo(DESCONOCIDO) { /* empty */ }
62
63 Ciudad(const string & str) : nombre(str), tipo(DESCONOCIDO) { /* empty */ }
64
65 bool operator == (const Ciudad & c) const
66 {
67 return nombre == c.nombre;
68 }
69};
70
71struct Via
72{
75
76 string nombre;
77 int distancia;
79
80 Via() : nombre("Desconocido"), distancia(0), tipo(DESCONOCIDO) {}
81
82 Via(int d)
83 : nombre("Desconocido"), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
84
85 Via(char * nom, int d)
86 : nombre(nom), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
87
88 Via(const string& nom, int d)
89 : nombre(nom), distancia(d), tipo(DESCONOCIDO) { /* empty */ }
90
91 typedef int Distance_Type;
92
94
95 static constexpr Distance_Type Zero_Distance = 0;
96};
97
99
101
102
104
106
107
108
109struct Ciudad_Igual
110{
111 bool operator () (const Ciudad & c1, const Ciudad & c2) const
112 {
113 return c1.nombre == c2.nombre;
114 }
115};
116
117
118Mapa::Node * buscar_ciudad(Mapa& mapa, const string & nombre)
119{
120 return mapa.search_node([&nombre] (Mapa::Node * p)
121 {
122 return p->get_info().nombre == nombre;
123 });
124}
125
127 const string & c1, const string & c2,
128 int distancia)
129{
131
132 if (n1 == nullptr)
133 n1 = mapa.insert_node(c1);
134
136
137 if (n2 == nullptr)
138 n2 = mapa.insert_node(c2);
139
140 string nombre_arco = n1->get_info().nombre + "--" + n2->get_info().nombre;
141
142 mapa.insert_arc(n1, n2, Via(nombre_arco, distancia));
143}
144
145
147{
148 cout << endl
149 << "Path: ";
150 for (Path<Mapa>::Iterator itor(path); itor.has_curr(); itor.next())
151 cout << itor.get_current_node()->get_info().nombre << "-";
152
153 cout << endl;
154}
155
156
158{
159 cout << endl
160 << "Node list (" << g.get_num_nodes() << ")" << endl;
161
162 for (Mapa::Node_Iterator node_itor(g); node_itor.has_curr();
163 node_itor.next())
164 cout << INDENT << node_itor.get_current_node()->get_info().nombre << endl;
165
166 cout << endl
167 << endl
168 << "Arc list (" << g.get_num_arcs() << ")"
169 << endl;
170
172 arc_itor.next())
173 {
174 Mapa::Arc * arc = arc_itor.get_current_arc();
175 cout << arc->get_info().nombre << " " << arc->get_info().distancia
176 << " from " << g.get_src_node(arc)->get_info().nombre
177 << " to " << g.get_tgt_node(arc)->get_info().nombre << endl;
178 }
179
180 cout << endl
181 << endl
182 << "Graph listing by nodes and for each node by arcs"
183 << endl;
184 for (Mapa::Node_Iterator node_itor(g); node_itor.has_curr();
185 node_itor.next())
186 {
187 Mapa::Node * src_node = node_itor.get_current_node();
188 cout << src_node->get_info().nombre << endl;
189 for (Mapa::Node_Arc_Iterator itor(node_itor.get_current_node());
190 itor.has_curr(); itor.next())
191 {
192 Mapa::Arc * arc = itor.get_current_arc();
193 cout << INDENT << arc->get_info().distancia << " "
194 << g.get_connected_node(arc, src_node)->get_info().nombre
195 << endl;
196 }
197 }
198 cout << endl;
199}
200
202{
203 insert_via(g, "San Cristobal", "La Fria", 69);
204 insert_via(g, "San Cristobal", "Sacramento", 113);
205 insert_via(g, "San Cristobal", "San Antonio", 36);
206 insert_via(g, "Rubio", "Caparo", 150);
207 insert_via(g, "La Fria", "El Vigia", 86);
208 insert_via(g, "El Vigia", "Santa Barbara", 59);
209 insert_via(g, "El Vigia", "Merida", 79);
210 insert_via(g, "La Fria", "Machiques", 252);
211 insert_via(g, "Valera", "Merida", 167);
212 insert_via(g, "Valera", "Carora", 120);
213 insert_via(g, "Carora", "Barquisimeto", 102);
214 insert_via(g, "Merida", "Barinas", 180);
215 insert_via(g, "Barinas", "Guanare", 94);
216}
217
218
219 template <typename GT>
220struct GT_Tree
221{
223 {
224 t->get_key() = g->get_info().nombre;
225 }
226};
227
228
229struct Write_Ciudad
230{
232 {
233 return p->get_key();
234 }
235};
236
237void escribir_deway(int deway[], const size_t & n)
238{
239 for (size_t i = 0; i < n; ++i)
240 {
241 cout << deway[i];
242
243 if (i < n - 1)
244 cout << ".";
245 }
246}
247
248int main()
249{
250 Mapa tree;
251
252 construir_mapa(tree);
253
254 imprimir_mapa(tree);
255
256 Mapa::Node * c = buscar_ciudad(tree, "Merida");
257 ah_runtime_error_if(c == nullptr)
258 << "buscar_ciudad(tree, \"Merida\") returned nullptr";
259
260 std::unique_ptr<Tree_Node<string>> t(
261 Aleph::Graph_To_Tree_Node <Mapa, string, GT_Tree<Mapa> > () (tree, c));
262
263 while (true)
264 {
265 cout << "Enter key to search (type \"quit\"): ";
266 string clave;
267 if (not std::getline(std::cin, clave))
268 break;
269
270 // Remove trailing CR if present (e.g. on Windows)
271 if (not clave.empty() and clave.back() == '\r')
272 clave.pop_back();
273
274 if (clave.empty() or clave == "quit")
275 break;
276
277 const size_t Buf_Size = 512;
278 int deway[Buf_Size];
279 size_t dw_size = 0;
280
281 Tree_Node<string> * p =
282 search_deway <Tree_Node<string> >(t.get(), clave, deway, Buf_Size,
283 dw_size);
284
285 if (p == nullptr)
286 cout << clave << " was not found in the tree" << endl;
287 else
288 {
289 cout << clave << " has Deway number: ";
291 cout << endl;
292 }
293 }
294
295 cout << "Exiting ... " << endl;
296
297 ofstream test("prueba.Tree", ios::trunc);
299 << "could not open prueba.Tree for writing";
300
302}
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:271
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
void deway(Tree_Node< int > *p, int prefix[], const int &len, const size_t &dim)
Recursively compute and print Deway numbering for a tree node.
Definition deway.C:118
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
and
Check uniqueness with explicit hash + equality functors.
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)
void escribir_deway(int deway[], const size_t &n)
int main()
Generic graph and digraph implementations.