|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Floyd-Warshall algorithm with LaTeX output generation. More...
#include <limits>#include <iostream>#include <tpl_matgraph.H>#include <mat_latex.H>#include <tpl_graph_utils.H>#include <latex_floyd.H>Go to the source code of this file.
Classes | |
| struct | C_i< M > |
| struct | C_ij< M > |
| struct | Di_ij< M > |
| struct | Nodo |
| struct | Arco |
Macros | |
| #define | INDENT " " |
Typedefs | |
| typedef Graph_Node< Nodo > | Node_Nodo |
| typedef Graph_Arc< Arco > | Arco_Arco |
| typedef List_Digraph< Node_Nodo, Arco_Arco > | Grafo |
Functions | |
| void | insertar_arco (Grafo &grafo, const string &src_name, const string &tgt_name, const double &distancia) |
| void | build_test_graph (Grafo &g) |
| void | copia_Arco_Arco (Arco_Arco *arc, const long &, const long &, double &value) |
| int | main () |
Variables | |
| const Arco | Arco_Zero (0) |
Floyd-Warshall algorithm with LaTeX output generation.
This example demonstrates the Floyd-Warshall algorithm for finding shortest paths between all pairs of vertices in a weighted graph. It generates step-by-step LaTeX matrices showing the algorithm's progression, making it ideal for educational purposes and algorithm visualization.
All-pairs shortest paths: Find shortest paths between every pair of vertices in a weighted graph.
Floyd-Warshall is a dynamic programming algorithm that finds shortest paths between all pairs of vertices in a single run. Unlike Dijkstra (single-source) or Johnson (all-pairs using Dijkstra), Floyd-Warshall works directly with the adjacency matrix.
The algorithm uses dynamic programming with the recurrence:
Where:
At iteration k, we consider whether going through vertex k gives a shorter path than the current best path:
| Algorithm | Time | Space | Best For |
|---|---|---|---|
| Floyd-Warshall | O(V³) | O(V²) | Dense graphs |
| Johnson | O(V² log V + VE) | O(V²) | Sparse graphs |
| V × Dijkstra | O(V(V log V + E)) | O(V²) | Non-negative only |
Generates mat-floyd.tex containing:
The LaTeX output shows:
Uses a predefined 9-node directed graph (labeled A through I) with:
The algorithm maintains a next-hop matrix P:
P[i][j] stores the next vertex index to move to when going from i to j along a shortest path.A simple reconstruction procedure is:
Time: O(path_length) to reconstruct one path
In general, a negative cycle implies that at least one diagonal entry becomes negative (some D[i][i] < 0) after relaxation.
Note: this example generates the LaTeX trace but does not perform an explicit negative-cycle check.
Definition in file write_floyd.C.
| #define INDENT " " |
Definition at line 259 of file write_floyd.C.
Definition at line 350 of file write_floyd.C.
| typedef List_Digraph<Node_Nodo, Arco_Arco> Grafo |
Definition at line 352 of file write_floyd.C.
| typedef Graph_Node<Nodo> Node_Nodo |
Definition at line 348 of file write_floyd.C.
| void build_test_graph | ( | Grafo & | g | ) |
| void copia_Arco_Arco | ( | Arco_Arco * | arc, |
| const long & | , | ||
| const long & | , | ||
| double & | value | ||
| ) |
Definition at line 410 of file write_floyd.C.
References GTArcCommon< ArcInfo >::get_info().
| void insertar_arco | ( | Grafo & | grafo, |
| const string & | src_name, | ||
| const string & | tgt_name, | ||
| const double & | distancia | ||
| ) |
Definition at line 355 of file write_floyd.C.
References Aleph::maps().
Referenced by build_test_graph().
| int main | ( | ) |
Definition at line 416 of file write_floyd.C.
References build_test_graph(), and Aleph::maps().
| const Arco Arco_Zero(0) | ( | 0 | ) |