Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
latex_floyd.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
83#ifndef LATEX_FLOYD_H
84#define LATEX_FLOYD_H
85
86#include <ahFunction.H>
87#include <tpl_matgraph.H>
88#include <mat_latex.H>
89
90namespace Aleph
91{
92
104template <class AM>
106{
107 typedef typename AM::Graph_Type::Arc_Type Arc_Type;
108
109 typedef typename AM::Graph_Type GT;
110
111 typedef typename GT::Node Node;
112
113 typedef typename GT::Arc Arc;
114
115 typedef typename AM::Arc_Type::Distance_Type Distance_Type;
116
117 void operator () (AM & mat,
118 Node * src,
119 Node * tgt,
120 const long & i,
121 const long & j,
122 Distance_Type & entry,
123 void * p)
124 {
126 * reinterpret_cast<Ady_Mat<typename AM::Graph_Type, long> *>(p);
127
128 if (i == j)
129 {
130 entry = AM::Graph_Type::Arc_Type::Zero_Distance;
131 path(i, j) = j;
132
133 return;
134 }
135
136 GT & g = mat.get_list_graph();
137
138 Arc * arc = search_arc(g, src, tgt);
139
140 if (arc == nullptr)
141 {
142 entry = AM::Graph_Type::Arc_Type::Max_Distance;
143 return;
144 }
145
146 entry = arc->get_info().get_distance();
147
148 path(i, j) = j;
149 }
150};
151
170template <class GT, class Compare, class Plus>
172 GT & g,
174 Ady_Mat<GT, long> & path)
175{
177
178 typedef typename GT::Arc_Type::Distance_Type Dist_Type;
179
180 dist.
182
183 const Dist_Type & max = GT::Arc_Type::Max_Distance;
184
185 const long & n = g.get_num_nodes();
186
187 for (int i = 0; i < n; ++i)
188 for (int s = 0; s < n; ++s)
189 if (dist(s, i) < max)
190 for (int t = 0; t < n; ++t)
191 {
192 if (!(dist(i, t) < max))
193 continue;
194
195 Dist_Type new_dist = Plus () (dist(s, i), dist(i, t));
196
197 if (Compare () (new_dist, dist(s, t)))
198 {
199 path(s, t) = path(s, i);
200 dist(s, t) = new_dist;
201 }
202 }
203}
204
207template <class GT>
209 GT & g,
211 Ady_Mat<GT, long> & path)
212{
213 using Dist_T = typename GT::Arc_Type::Distance_Type;
215}
216
228template <class Mat>
230 const long src_index,
231 const long tgt_index,
233{
234 using GT = typename Mat::Graph_Type;
235 using Node = typename GT::Node;
236
237 GT & g = p.get_list_graph();
238 Node * src = p(src_index);
239 path.set_graph(g, src);
240
241 // NOTE: Initialize j properly to avoid undefined behavior
242 for (long i = src_index, j = p(i, tgt_index); i != tgt_index; i = j, j = p(i, tgt_index))
243 {
244 Node * next = p(j);
245 path.append(next);
246 }
247}
248
265template <class Mat>
267 typename Mat::Node * src_node,
268 typename Mat::Node * tgt_node,
270{
271 const long src_index = p(src_node);
272 const long tgt_index = p(tgt_node);
274}
275
276
304template <class GT, class Compare, class Plus,
305 template <class> class P_i, // Row/column index format for dist and path
306 template <class> class P_ij, // Path matrix entry format
307 template <class> class D_ij> // Distance matrix entry format
309 GT & g,
311 Ady_Mat<GT, long> & path,
312 std::ofstream & output)
313{
316
317 typedef typename GT::Arc_Type::Distance_Type Dist_Type;
318
319 dist.
321
322 const Dist_Type & max = GT::Arc_Type::Max_Distance;
323
324 const long & n = g.get_num_nodes();
325
326 output << "\\begin{figure}[H]{\\tiny " << std::endl
327 << "\\begin{tabular}{ll}" << std::endl
328 << "\\begin{tabular}{ll}" << std::endl;
330 (dist, n, n, output, "\\hskip -5mm $D_0=$", "\\\\ ");
331 output << "\\end{tabular}" << std::endl
332 << " & \\begin{tabular}{ll}" << std::endl;
334 (path, n, n, output, "\\hskip -7mm $P_0=$", "\\\\ ");
335 output << "\\end{tabular}" << std::endl
336 << "\\end{tabular}" << std::endl
337 << "}\\end{figure}" << std::endl;
338
339 for (int i = 0; i < n; ++i)
340 {
341 for (int s = 0; s < n; ++s)
342 if (dist(s, i) < max)
343 for (int t = 0; t < n; ++t)
344 {
345 if (!(dist(i, t) < max))
346 continue;
347
348 Dist_Type new_dist = Plus() (dist(s, i), dist(i, t));
349
350 if (Compare () (new_dist, dist(s, t)))
351 {
352 path(s, t) = path(s, i);
353 dist(s, t) = new_dist;
354 }
355 }
356 char buf[256];
357
358 snprintf(buf, 256, "\\hskip -5mm $D_%d=$ ", i + 1);
359
360 output << "\\begin{figure}[H]{\\tiny " << std::endl
361 << "\\begin{tabular}{ll}" << std::endl
362 << "\\begin{tabular}{ll}" << std::endl;
364 (dist, n, n, output, buf, "\\\\ ");
365 output << "\\end{tabular}" << std::endl
366 << " & \\begin{tabular}{ll}" << std::endl;
367
368 snprintf(buf, 256, "\\hskip -7mm $P_%d=$ ", i + 1);
369
371 (path, n, n, output, buf, "\\\\");
372 output << "\\end{tabular}" << std::endl
373 << "\\end{tabular}" << std::endl
374 << "}\\end{figure}" << std::endl;
375 }
376}
377
378
381template <class GT,
382 template <class> class P_i, // Row/column index format
383 template <class> class P_ij, // Path matrix entry format
384 template <class> class D_ij> // Distance matrix entry format
386 GT & g,
388 Ady_Mat<GT, long> & path,
389 std::ofstream & output)
390{
391 using Dist_T = typename GT::Arc_Type::Distance_Type;
393 P_i, P_ij, D_ij>(g, dist, path, output);
394}
395
396} // end namespace Aleph
397
398#endif // LATEX_FLOYD_H
Standard functor implementations and comparison objects.
WeightedDigraph::Node Node
Auxiliary adjacency matrix with custom entry type.
GT & get_list_graph() noexcept
Get reference to underlying graph.
Path on a graph.
Definition tpl_graph.H:2669
void set_graph(const GT &__g, Node *start_node=nullptr)
Set the graph of the path.
Definition tpl_graph.H:2797
void append(Arc *arc)
Append an arc to the path.
Definition tpl_graph.H:2865
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:595
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:695
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4110
void floyd_all_shortest_paths_latex(GT &g, Ady_Mat< GT, typename GT::Arc_Type::Distance_Type > &dist, Ady_Mat< GT, long > &path, std::ofstream &output)
Floyd-Warshall algorithm with LaTeX step-by-step output.
void find_min_path(Mat &p, const long src_index, const long tgt_index, Path< typename Mat::Graph_Type > &path)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void floyd_all_shortest_paths(GT &g, Ady_Mat< GT, typename GT::Arc_Type::Distance_Type > &dist, Ady_Mat< GT, long > &path)
Compute all-pairs shortest paths using Floyd-Warshall algorithm.
GT::Arc * search_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept
Arc filtered searching given two nodes.
Definition tpl_graph.H:2421
Matrix to LaTeX table conversion utilities.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:175
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Matrix initialization functor for Floyd-Warshall.
AM::Arc_Type::Distance_Type Distance_Type
AM::Graph_Type::Arc_Type Arc_Type
void operator()(AM &mat, Node *src, Node *tgt, const long &i, const long &j, Distance_Type &entry, void *p)
Adjacency matrix representations for graphs.
ofstream output
Definition writeHeap.C:213