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 <ah-graph-concepts.H>
87
88#include <ahFunction.H>
89#include <tpl_matgraph.H>
90#include <mat_latex.H>
91
92namespace Aleph
93{
94
106template <class AM>
108{
109 typedef typename AM::Graph_Type::Arc_Type Arc_Type;
110
111 typedef typename AM::Graph_Type GT;
112
113 typedef typename GT::Node Node;
114
115 typedef typename GT::Arc Arc;
116
117 typedef typename AM::Arc_Type::Distance_Type Distance_Type;
118
119 void operator () (AM & mat,
120 Node * src,
121 Node * tgt,
122 const long & i,
123 const long & j,
124 Distance_Type & entry,
125 void * p)
126 {
128 * reinterpret_cast<Ady_Mat<typename AM::Graph_Type, long> *>(p);
129
130 if (i == j)
131 {
132 entry = AM::Graph_Type::Arc_Type::Zero_Distance;
133 path(i, j) = j;
134
135 return;
136 }
137
138 GT & g = mat.get_list_graph();
139
140 Arc * arc = search_arc(g, src, tgt);
141
142 if (arc == nullptr)
143 {
144 entry = AM::Graph_Type::Arc_Type::Max_Distance;
145 return;
146 }
147
148 entry = arc->get_info().get_distance();
149
150 path(i, j) = j;
151 }
152};
153
172template <AlephGraph GT, class Compare, class Plus>
174 GT & g,
176 Ady_Mat<GT, long> & path)
177{
179
180 typedef typename GT::Arc_Type::Distance_Type Dist_Type;
181
182 dist.
184
185 const Dist_Type & max = GT::Arc_Type::Max_Distance;
186
187 const long & n = g.get_num_nodes();
188
189 for (int i = 0; i < n; ++i)
190 for (int s = 0; s < n; ++s)
191 if (dist(s, i) < max)
192 for (int t = 0; t < n; ++t)
193 {
194 if (!(dist(i, t) < max))
195 continue;
196
197 Dist_Type new_dist = Plus () (dist(s, i), dist(i, t));
198
199 if (Compare () (new_dist, dist(s, t)))
200 {
201 path(s, t) = path(s, i);
202 dist(s, t) = new_dist;
203 }
204 }
205}
206
209template <AlephGraph GT>
211 GT & g,
213 Ady_Mat<GT, long> & path)
214{
215 using Dist_T = typename GT::Arc_Type::Distance_Type;
217}
218
230template <class Mat>
232 const long src_index,
233 const long tgt_index,
235{
236 using GT = typename Mat::Graph_Type;
237 using Node = typename GT::Node;
238
239 GT & g = p.get_list_graph();
240 Node * src = p(src_index);
241 path.set_graph(g, src);
242
243 // NOTE: Initialize j properly to avoid undefined behavior
244 for (long i = src_index, j = p(i, tgt_index); i != tgt_index; i = j, j = p(i, tgt_index))
245 {
246 Node * next = p(j);
247 path.append(next);
248 }
249}
250
267template <class Mat>
269 typename Mat::Node * src_node,
270 typename Mat::Node * tgt_node,
272{
273 const long src_index = p(src_node);
274 const long tgt_index = p(tgt_node);
276}
277
278
306template <AlephGraph GT, class Compare, class Plus,
307 template <class> class P_i, // Row/column index format for dist and path
308 template <class> class P_ij, // Path matrix entry format
309 template <class> class D_ij> // Distance matrix entry format
311 GT & g,
313 Ady_Mat<GT, long> & path,
314 std::ofstream & output)
315{
318
319 typedef typename GT::Arc_Type::Distance_Type Dist_Type;
320
321 dist.
323
324 const Dist_Type & max = GT::Arc_Type::Max_Distance;
325
326 const long & n = g.get_num_nodes();
327
328 output << "\\begin{figure}[H]{\\tiny " << std::endl
329 << "\\begin{tabular}{ll}" << std::endl
330 << "\\begin{tabular}{ll}" << std::endl;
332 (dist, n, n, output, "\\hskip -5mm $D_0=$", "\\\\ ");
333 output << "\\end{tabular}" << std::endl
334 << " & \\begin{tabular}{ll}" << std::endl;
336 (path, n, n, output, "\\hskip -7mm $P_0=$", "\\\\ ");
337 output << "\\end{tabular}" << std::endl
338 << "\\end{tabular}" << std::endl
339 << "}\\end{figure}" << std::endl;
340
341 for (int i = 0; i < n; ++i)
342 {
343 for (int s = 0; s < n; ++s)
344 if (dist(s, i) < max)
345 for (int t = 0; t < n; ++t)
346 {
347 if (!(dist(i, t) < max))
348 continue;
349
350 Dist_Type new_dist = Plus() (dist(s, i), dist(i, t));
351
352 if (Compare () (new_dist, dist(s, t)))
353 {
354 path(s, t) = path(s, i);
355 dist(s, t) = new_dist;
356 }
357 }
358 char buf[256];
359
360 snprintf(buf, 256, "\\hskip -5mm $D_%d=$ ", i + 1);
361
362 output << "\\begin{figure}[H]{\\tiny " << std::endl
363 << "\\begin{tabular}{ll}" << std::endl
364 << "\\begin{tabular}{ll}" << std::endl;
366 (dist, n, n, output, buf, "\\\\ ");
367 output << "\\end{tabular}" << std::endl
368 << " & \\begin{tabular}{ll}" << std::endl;
369
370 snprintf(buf, 256, "\\hskip -7mm $P_%d=$ ", i + 1);
371
373 (path, n, n, output, buf, "\\\\");
374 output << "\\end{tabular}" << std::endl
375 << "\\end{tabular}" << std::endl
376 << "}\\end{figure}" << std::endl;
377 }
378}
379
380
383template <AlephGraph GT,
384 template <class> class P_i, // Row/column index format
385 template <class> class P_ij, // Path matrix entry format
386 template <class> class D_ij> // Distance matrix entry format
388 GT & g,
390 Ady_Mat<GT, long> & path,
391 std::ofstream & output)
392{
393 using Dist_T = typename GT::Arc_Type::Distance_Type;
395 P_i, P_ij, D_ij>(g, dist, path, output);
396}
397
398} // end namespace Aleph
399
400#endif // LATEX_FLOYD_H
C++20 concepts for the protocol shared by graph algorithms.
Standard functor implementations and comparison objects.
WeightedDigraph::Node Node
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
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:2772
void set_graph(const GT &__g, Node *start_node=nullptr)
Set the graph of the path.
Definition tpl_graph.H:2900
void append(Arc *arc)
Append an arc to the path.
Definition tpl_graph.H:2975
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:637
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:737
__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:4121
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.
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
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:2524
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:171
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:215