Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
write_floyd.C
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
248# include <limits>
249# include <iostream>
250# include <tpl_matgraph.H>
251# include <mat_latex.H>
252# include <tpl_graph_utils.H>
253# include <latex_floyd.H>
254
255using namespace std;
256
257using namespace Aleph;
258
259# define INDENT " "
260
261 template <typename M>
262struct C_i
263{
264 string operator () (M & m, const long & i) const
265 {
266 return m(i)->get_info().nombre;
267 }
268};
269
270
271 template <typename M>
272struct C_ij
273{
274 string operator () (M & m, const long & i, const long & j)
275 {
276 return m(m(i, j))->get_info().nombre;
277 }
278};
279
280
281 template <typename M>
282struct Di_ij
283{
284 string operator () (M & mat, const long & i, const long & j)
285 {
286 typename M::Arc_Type::Distance_Type value = mat(i, j);
287
288 if (value == numeric_limits<double>::infinity())
289 return string("{$\\infty$}");
290
291 if (value == 0)
292 return string("{$0$}");
293
294 char buf[256];
295
296 snprintf(buf, 256, "{$%d$}", (int) value);
297
298 return string(buf);
299 }
300};
301
302struct Nodo
303{
304 string nombre;
305
306 Nodo() {}
307
308 Nodo(const string & str) : nombre(str) { /* empty */ }
309
310 Nodo(const char * str) : nombre(str) { /* empty */ }
311
312 bool operator == (const Nodo & der) const
313 {
314 return nombre == der.nombre;
315 }
316};
317
318
319struct Arco
320{
321 typedef double Distance_Type;
322
323 static const double Max_Distance;
324
325 static const double Zero_Distance;
326
327 double distancia;
328
329 Arco() : distancia(Max_Distance) { /* empty */ }
330
331 Arco(const double & dist) : distancia(dist)
332 {
333 /* empty */
334 }
335
336 const double & get_distance() const { return distancia; }
337
338 operator double & () { return distancia; }
339};
340
342
343const double Arco::Max_Distance = numeric_limits<double>::infinity();
344
345const double Arco::Zero_Distance = 0;
346
347
349
351
353
354
356 const string & src_name,
357 const string & tgt_name,
358 const double & distancia)
359{
360 Grafo::Node * n1 = grafo.find_node(Nodo(src_name));
361
362 if (n1 == nullptr)
363 n1 = grafo.insert_node(src_name);
364
365 Grafo::Node * n2 = grafo.find_node(Nodo(tgt_name));
366
367 if (n2 == nullptr)
368 n2 = grafo.insert_node(tgt_name);
369
370 grafo.insert_arc(n1, n2, Arco(distancia));
371}
372
373
374
376{
377 g.insert_node("A");
378 g.insert_node("B");
379 g.insert_node("C");
380 g.insert_node("D");
381 g.insert_node("E");
382 g.insert_node("F");
383 g.insert_node("G");
384 g.insert_node("H");
385 g.insert_node("I");
386
387 insertar_arco(g, "A", "B", 2);
388 insertar_arco(g, "A", "F", 5);
389 insertar_arco(g, "B", "F", 1);
390 insertar_arco(g, "B", "D", 3);
391 insertar_arco(g, "C", "A", 1);
392 insertar_arco(g, "C", "E", 4);
393 insertar_arco(g, "F", "D", -2);
394 insertar_arco(g, "F", "C", -1);
395 insertar_arco(g, "F", "E", 2);
396 insertar_arco(g, "D", "F", 2);
397 insertar_arco(g, "D", "H", 4);
398 insertar_arco(g, "E", "G", 2);
399 insertar_arco(g, "E", "I", -2);
400 insertar_arco(g, "G", "D", 3);
401 insertar_arco(g, "G", "F", -1);
402 insertar_arco(g, "G", "H", 2);
403 insertar_arco(g, "H", "D", -2);
404 insertar_arco(g, "H", "G", -1);
405 insertar_arco(g, "I", "G", 4);
406 insertar_arco(g, "I", "H", 3);
407
408}
409
410void copia_Arco_Arco(Arco_Arco * arc, const long&, const long&, double& value)
411{
412 value = arc->get_info().get_distance();
413}
414
415
416int main()
417{
419
421
423
425
426 ofstream out("mat-floyd.tex", ios::trunc);
427
429 (list_graph, cost_mat, path_mat, out);
430}
Auxiliary adjacency matrix with custom entry type.
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3848
typename BaseGraph::Node Node
Definition graph-dry.H:3851
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:595
Floyd-Warshall algorithm with LaTeX output generation.
Matrix to LaTeX table conversion utilities.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
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
static const double Zero_Distance
Arco(const double &dist)
double Distance_Type
double distancia
static const double Max_Distance
const double & get_distance() const
string operator()(M &m, const long &i) const
string operator()(M &m, const long &i, const long &j)
string operator()(M &mat, const long &i, const long &j)
Nodo(const char *str)
string nombre
Nodo(const string &str)
bool operator==(const Nodo &der) const
Utility algorithms and operations for graphs.
Adjacency matrix representations for graphs.
void copia_Arco_Arco(Arco_Arco *arc, const long &, const long &, double &value)
void insertar_arco(Grafo &grafo, const string &src_name, const string &tgt_name, const double &distancia)
List_Digraph< Node_Nodo, Arco_Arco > Grafo
const Arco Arco_Zero(0)
void build_test_graph(Grafo &g)
Graph_Arc< Arco > Arco_Arco
int main()
Graph_Node< Nodo > Node_Nodo