|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Compute the all-pairs shortest path distance matrix and the path reconstruction matrix for a graph g using the Floyd-Warshall algorithm.
More...
#include <Floyd_Warshall.H>
Public Member Functions | |
| constexpr bool | has_negative_cycle () const noexcept |
| Check if the graph contains a negative cycle. | |
| const DynMatrix< long > & | get_path_mat () const noexcept |
| Get the path reconstruction matrix. | |
| const DynMatrix< Distance_Type > & | get_dist_mat () const noexcept |
| Get the distance matrix. | |
| DynArray< Node * > | get_nodes () const noexcept |
| Get the array of graph nodes. | |
| Node * | select_node (long i) const noexcept |
| Get the graph node at a given matrix index. | |
| long | index_node (Node *p) const |
Return the adjacency-matrix index corresponding to node p. | |
| Floyd_All_Shortest_Paths (const GT &__g, SA &__sa) | |
| Construct Floyd-Warshall solver for all-pairs shortest paths. | |
| Floyd_All_Shortest_Paths (const GT &g, SA &&sa=SA()) | |
| Construct Floyd-Warshall solver with rvalue arc filter. | |
| std::string | entry (const Distance_Type &e) const |
| Convert a distance value to string representation. | |
| Path< GT > | get_min_path (const long src_idx, const long tgt_idx) const |
| Reconstruct the shortest path between two nodes by matrix indices. | |
| Path< GT > | get_min_path (typename GT::Node *src, typename GT::Node *tgt) const |
| Reconstruct the shortest path between two nodes. | |
Static Public Member Functions | |
| static void | print (const DynMatrix< Distance_Type > &dist) |
| Print a distance matrix to standard output. | |
Private Types | |
| typedef GT::Node | Node |
| typedef GT::Arc | Arc |
| typedef Distance::Distance_Type | Distance_Type |
Private Attributes | |
| DynArray< Node * > | nodes |
| GT & | g |
| const long | n |
| const Distance_Type | Inf |
| bool | negative_cycle = false |
| DynMatrix< long > | path_mat |
| DynMatrix< Distance_Type > | dist |
| SA & | sa |
Compute the all-pairs shortest path distance matrix and the path reconstruction matrix for a graph g using the Floyd-Warshall algorithm.
This class computes two matrices:
dist: matrix of shortest-path costs between all node pairs. Each entry dist(i,j) stores the minimum total cost to go from the node with index i to the node with index j.path: path reconstruction matrix. Each entry path(i,j) stores the next node index on a shortest path from i to j. By iterating through path(k,j) you can reconstruct the path. The helper get_min_path() performs this reconstruction.Floyd-Warshall supports negative edge weights, but it does not work correctly if the graph contains negative cycles. Use Bellman-Ford based algorithms if you suspect negative cycles.
Template parameters:
GT: graph type.Distance<GT>: arc weight accessor that must provide:
SA: arc filter.Definition at line 149 of file Floyd_Warshall.H.
Definition at line 152 of file Floyd_Warshall.H.
|
private |
Definition at line 153 of file Floyd_Warshall.H.
Definition at line 151 of file Floyd_Warshall.H.
|
inline |
Construct Floyd-Warshall solver for all-pairs shortest paths.
Computes shortest distances and path reconstruction matrices for all pairs of nodes in the graph using the Floyd-Warshall algorithm. Handles negative weights but detects negative cycles.
| [in] | __g | Input graph (will be treated as const). |
| [in] | __sa | Arc filter for considering which arcs to include. |
| std::invalid_argument | if graph has no nodes. |
| std::bad_alloc | if memory allocation fails. |
Definition at line 242 of file Floyd_Warshall.H.
References Aleph::DynArray< T >::access(), ah_invalid_argument_if, Aleph::DynMatrix< T >::allocate(), arcs, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::dist, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::g, Aleph::in_place_sort(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Inf, Aleph::maps(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::n, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::negative_cycle, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::path_mat, Aleph::DynArray< T >::reserve(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::sa.
|
inline |
Construct Floyd-Warshall solver with rvalue arc filter.
| [in] | g | Input graph. |
| [in] | sa | Arc filter (moved). |
Definition at line 351 of file Floyd_Warshall.H.
|
inline |
Convert a distance value to string representation.
| [in] | e | Distance value to convert. |
Definition at line 360 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Inf, and Aleph::maps().
|
inlinenoexcept |
Get the distance matrix.
The distance matrix contains shortest distances between all pairs of nodes. Entry (i,j) contains the shortest distance from node i to node j.
Definition at line 190 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::dist.
|
inline |
Reconstruct the shortest path between two nodes by matrix indices.
Uses the path reconstruction matrix to build the shortest path from source to target node.
| std::out_of_range | if indices are invalid. |
Definition at line 405 of file Floyd_Warshall.H.
References ah_out_of_range_error_if, Aleph::Path< GT >::append_directed(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::dist, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::g, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Inf, Aleph::maps(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::n, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes, and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::path_mat.
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path().
|
inline |
Reconstruct the shortest path between two nodes.
Convenience method that accepts node pointers instead of indices.
| [in] | src | Pointer to source node. |
| [in] | tgt | Pointer to target node. |
| std::invalid_argument | if either node pointer is null. |
| std::domain_error | if either node is not found in the graph. |
Definition at line 446 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::index_node().
|
inlinenoexcept |
Get the array of graph nodes.
Returns the sorted array of node pointers used for matrix indexing.
Definition at line 202 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes.
|
inlinenoexcept |
Get the path reconstruction matrix.
The path matrix contains intermediate nodes for shortest path reconstruction. Entry (i,j) contains the next node index on the shortest path from i to j.
Definition at line 180 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::path_mat.
|
inlineconstexprnoexcept |
Check if the graph contains a negative cycle.
Definition at line 170 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::negative_cycle.
Return the adjacency-matrix index corresponding to node p.
Find the matrix index corresponding to a graph node.
| [in] | p | Pointer to the node to find. |
| std::domain_error | if the node is not found. |
Definition at line 220 of file Floyd_Warshall.H.
References ah_domain_error_if, ah_invalid_argument_if, Aleph::binary_search(), Aleph::maps(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes, and Aleph::DynArray< T >::size().
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path().
|
inlinestatic |
Print a distance matrix to standard output.
Prints the distance matrix in a readable format, showing "inf" for infinite distances and numeric values otherwise.
| [in] | dist | Distance matrix to print. |
Definition at line 378 of file Floyd_Warshall.H.
References Aleph::DynMatrix< T >::access(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::dist, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Inf, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::n, and Aleph::DynMatrix< T >::rows().
|
inlinenoexcept |
Get the graph node at a given matrix index.
| [in] | i | Matrix index (must be in range [0, n)). |
Definition at line 210 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes.
|
private |
Definition at line 161 of file Floyd_Warshall.H.
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Floyd_All_Shortest_Paths(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_dist_mat(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::print().
|
private |
Definition at line 156 of file Floyd_Warshall.H.
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Floyd_All_Shortest_Paths(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path().
|
private |
Definition at line 158 of file Floyd_Warshall.H.
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Floyd_All_Shortest_Paths(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::entry(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::print().
Definition at line 159 of file Floyd_Warshall.H.
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Floyd_All_Shortest_Paths(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::has_negative_cycle().
Definition at line 155 of file Floyd_Warshall.H.
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Floyd_All_Shortest_Paths(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_nodes(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::index_node(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::select_node().
|
private |
Definition at line 162 of file Floyd_Warshall.H.
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Floyd_All_Shortest_Paths().