Aleph-w 7.10.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
89#ifndef LATEX_FLOYD_H
90#define LATEX_FLOYD_H
91
92# include <ah-graph-concepts.H>
93
94#include <utility>
95#include <ahFunction.H>
96#include <tpl_array.H>
97#include <tpl_matgraph.H>
98#include <mat_latex.H>
99#include <mat_path.H>
100
101namespace Aleph
102{
103
125template <class AM,
128{
129 typedef typename AM::Graph_Type::Arc_Type Arc_Type;
130
131 typedef typename AM::Graph_Type GT;
132
133 typedef typename GT::Node Node;
134
135 typedef typename GT::Arc Arc;
136
137 typedef typename AM::Arc_Type::Distance_Type Distance_Type;
138
139 void operator () (AM & mat,
140 Node * src,
141 Node * tgt,
142 const long & i,
143 const long & j,
144 Distance_Type & entry,
145 void * p)
146 {
148 * reinterpret_cast<Ady_Mat<typename AM::Graph_Type, long> *>(p);
149
150 if (i == j)
151 {
152 entry = AM::Graph_Type::Arc_Type::Zero_Distance;
153 path(i, j) = j;
154
155 return;
156 }
157
158 GT & g = mat.get_list_graph();
159
160 // In a graph that is not directed, the arcs of the node with fewer
161 // arcs are searched, as search_arc() does
162 if (not g.is_digraph() and tgt->num_arcs < src->num_arcs)
163 std::swap(src, tgt);
164
165 Arc * lightest = nullptr;
166 for (typename GT::Node_Arc_Iterator it(src); it.has_curr(); it.next_ne())
167 if (it.get_tgt_node_ne() == tgt and
168 (lightest == nullptr or
169 Compare () (it.get_current_arc_ne()->get_info().get_distance(),
170 lightest->get_info().get_distance())))
171 lightest = it.get_current_arc_ne();
172
173 if (lightest == nullptr)
174 {
175 entry = AM::Graph_Type::Arc_Type::Max_Distance;
176 return;
177 }
178
179 entry = lightest->get_info().get_distance();
180
181 path(i, j) = j;
182 }
183};
184
185namespace latex_floyd_detail
186{
187 /* Sets the matrices as Initialize_Dist does, but from one pass over the
188 arcs of each node instead of a search for each pair of nodes: O(n^2 +
189 m log n) instead of O(n m). The first arc from i to j takes the entry,
190 and a lighter one, by Compare, replaces it; seen[j] == i says that an
191 arc from i to j was already found. */
192 template <AlephGraph GT, class Compare>
195 Ady_Mat<GT, long> & path)
196 {
197 using Dist_Type = typename GT::Arc_Type::Distance_Type;
198 using Node = typename GT::Node;
199
200 const long n = dist.get_num_nodes();
201 Array<long> seen(n, -1L);
202 for (long i = 0; i < n; ++i)
203 {
204 for (long j = 0; j < n; ++j)
205 dist(i, j) = i == j ? GT::Arc_Type::Zero_Distance
206 : GT::Arc_Type::Max_Distance;
207 path(i, i) = i;
208
209 Node * src = dist(i);
210 for (typename GT::Node_Arc_Iterator it(src); it.has_curr(); it.next_ne())
211 {
212 Node * tgt = it.get_tgt_node_ne();
213 if (tgt == src)
214 continue; // a loop does not count
215
216 const long j = dist(tgt);
217 const Dist_Type w = it.get_current_arc_ne()->get_info().get_distance();
218 if (seen[j] != i)
219 {
220 seen[j] = i;
221 dist(i, j) = w;
222 path(i, j) = j;
223 }
224 else if (Compare () (w, dist(i, j)))
225 dist(i, j) = w;
226 }
227 }
228 }
229} // end namespace latex_floyd_detail
230
255template <AlephGraph GT, class Compare, class Plus>
257 GT & g,
259 Ady_Mat<GT, long> & path)
260{
261 typedef typename GT::Arc_Type::Distance_Type Dist_Type;
262
263 latex_floyd_detail::initialize_matrices<GT, Compare>(dist, path);
264
265 const Dist_Type & max = GT::Arc_Type::Max_Distance;
266
267 const long & n = g.get_num_nodes();
268
269 for (int i = 0; i < n; ++i)
270 for (int s = 0; s < n; ++s)
271 if (dist(s, i) < max)
272 for (int t = 0; t < n; ++t)
273 {
274 if (!(dist(i, t) < max))
275 continue;
276
277 Dist_Type new_dist = Plus () (dist(s, i), dist(i, t));
278
279 if (Compare () (new_dist, dist(s, t)))
280 {
281 path(s, t) = path(s, i);
282 dist(s, t) = new_dist;
283 }
284 }
285}
286
289template <AlephGraph GT>
291 GT & g,
293 Ady_Mat<GT, long> & path)
294{
295 using Dist_T = typename GT::Arc_Type::Distance_Type;
296 floyd_all_shortest_paths<GT, Aleph::less<Dist_T>, Aleph::plus<Dist_T>>(g, dist, path);
297}
298
333template <AlephGraph GT, class Compare, class Plus,
334 template <class> class P_i, // Row/column index format for dist and path
335 template <class> class P_ij, // Path matrix entry format
336 template <class> class D_ij> // Distance matrix entry format
338 GT & g,
340 Ady_Mat<GT, long> & path,
341 std::ofstream & output)
342{
344 typedef Ady_Mat<GT, long> Path_Mat;
345
346 typedef typename GT::Arc_Type::Distance_Type Dist_Type;
347
348 latex_floyd_detail::initialize_matrices<GT, Compare>(dist, path);
349
350 const Dist_Type & max = GT::Arc_Type::Max_Distance;
351
352 const long & n = g.get_num_nodes();
353
354 output << "\\begin{figure}[H]{\\tiny " << std::endl
355 << "\\begin{tabular}{ll}" << std::endl
356 << "\\begin{tabular}{ll}" << std::endl;
357 mat_to_latex <Dist_Mat, P_i<Dist_Mat>, P_i<Dist_Mat>, D_ij<Dist_Mat> >
358 (dist, n, n, output, "\\hskip -5mm $D_0=$", "\\\\ ");
359 output << "\\end{tabular}" << std::endl
360 << " & \\begin{tabular}{ll}" << std::endl;
361 mat_to_latex <Path_Mat, P_i<Path_Mat>, P_i<Path_Mat>, P_ij<Path_Mat> >
362 (path, n, n, output, "\\hskip -7mm $P_0=$", "\\\\ ");
363 output << "\\end{tabular}" << std::endl
364 << "\\end{tabular}" << std::endl
365 << "}\\end{figure}" << std::endl;
366
367 for (int i = 0; i < n; ++i)
368 {
369 for (int s = 0; s < n; ++s)
370 if (dist(s, i) < max)
371 for (int t = 0; t < n; ++t)
372 {
373 if (!(dist(i, t) < max))
374 continue;
375
376 Dist_Type new_dist = Plus() (dist(s, i), dist(i, t));
377
378 if (Compare () (new_dist, dist(s, t)))
379 {
380 path(s, t) = path(s, i);
381 dist(s, t) = new_dist;
382 }
383 }
384 char buf[256];
385
386 snprintf(buf, 256, "\\hskip -5mm $D_%d=$ ", i + 1);
387
388 output << "\\begin{figure}[H]{\\tiny " << std::endl
389 << "\\begin{tabular}{ll}" << std::endl
390 << "\\begin{tabular}{ll}" << std::endl;
391 mat_to_latex <Dist_Mat, P_i<Dist_Mat>, P_i<Dist_Mat>, D_ij<Dist_Mat> >
392 (dist, n, n, output, buf, "\\\\ ");
393 output << "\\end{tabular}" << std::endl
394 << " & \\begin{tabular}{ll}" << std::endl;
395
396 snprintf(buf, 256, "\\hskip -7mm $P_%d=$ ", i + 1);
397
398 mat_to_latex <Path_Mat, P_i<Path_Mat>, P_i<Path_Mat>, P_ij<Path_Mat> >
399 (path, n, n, output, buf, "\\\\");
400 output << "\\end{tabular}" << std::endl
401 << "\\end{tabular}" << std::endl
402 << "}\\end{figure}" << std::endl;
403 }
404}
405
406
409template <AlephGraph GT,
410 template <class> class P_i, // Row/column index format
411 template <class> class P_ij, // Path matrix entry format
412 template <class> class D_ij> // Distance matrix entry format
414 GT & g,
416 Ady_Mat<GT, long> & path,
417 std::ofstream & output)
418{
419 using Dist_T = typename GT::Arc_Type::Distance_Type;
420 floyd_all_shortest_paths_latex<GT, Aleph::less<Dist_T>, Aleph::plus<Dist_T>,
421 P_i, P_ij, D_ij>(g, dist, path, output);
422}
423
424} // end namespace Aleph
425
426#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
long double w
Definition btreepic.C:153
Auxiliary adjacency matrix with custom entry type.
size_t get_num_nodes() const noexcept
Get number of nodes (matrix dimension)
GT & get_list_graph() noexcept
Get reference to underlying graph.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
size_t num_arcs
data associated to the node. Access it with get_info()
Definition graph-dry.H:713
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
__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:4188
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 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.
Matrix to LaTeX table conversion utilities.
Path reconstruction from Floyd-Warshall path matrices.
void initialize_matrices(Ady_Mat< GT, typename GT::Arc_Type::Distance_Type > &dist, Ady_Mat< GT, long > &path)
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Matrix initialization functor for Floyd-Warshall.
AM::Graph_Type::Arc_Type Arc_Type
AM::Arc_Type::Distance_Type Distance_Type
void operator()(AM &mat, Node *src, Node *tgt, const long &i, const long &j, Distance_Type &entry, void *p)
Dynamic array container with automatic resizing.
Adjacency matrix representations for graphs.
ofstream output
Definition writeHeap.C:215