|
Aleph-w 7.10.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 |
| Get an owning copy of the graph nodes array. | |
| const DynArray< Node * > & | get_nodes_ref () const noexcept |
| Get the array of graph nodes as a constant reference. | |
| DynArray< Node * > | get_nodes_copy () const |
| Get an owning copy of the graph nodes array. | |
| 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. | |
| template<class D , std::enable_if_t< std::is_convertible_v< D, Distance >, int > = 0> | |
| Floyd_All_Shortest_Paths (const GT &__g, D &&__distance, SA __sa=SA()) | |
| Construct Floyd-Warshall solver for all-pairs shortest paths. | |
| Floyd_All_Shortest_Paths (const GT &__g, SA &__sa) | |
Construct Floyd-Warshall solver with a distance accessor built by Distance(). | |
| Floyd_All_Shortest_Paths (const GT &g, SA &&sa=SA()) | |
Construct Floyd-Warshall solver with rvalue arc filter and a distance accessor built by Distance(). | |
| Floyd_All_Shortest_Paths (const Floyd_All_Shortest_Paths &other)=default | |
| Copy the matrices of another solver, on the same graph. | |
| Floyd_All_Shortest_Paths (Floyd_All_Shortest_Paths &&other) noexcept(std::is_nothrow_move_constructible_v< SA > and std::is_nothrow_move_constructible_v< Distance > and std::is_nothrow_copy_constructible_v< Distance_Type >) | |
| Take the matrices of another solver, which is left without nodes, on the same graph. | |
| 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 Member Functions | |
| decltype(auto) | path_distance () const |
| template<class D > | |
| void | append_lightest_arc (Path< GT > &path, Node *p, Node *q, D &d) const |
| bool | has_negative_diagonal () const |
Private Attributes | |
| DynArray< Node * > | nodes |
| GT & | g |
| long | n |
| const Distance_Type | Inf |
| bool | negative_cycle = false |
| DynMatrix< long > | path_mat |
| DynMatrix< Distance_Type > | dist |
| SA | sa |
| Distance | distance |
Related Symbols | |
(Note that these are not member symbols.) | |
| template<AlephGraph GT, ArcDistance< GT > Distance, ArcFilter< GT > SA = Dft_Show_Arc<GT>> | |
| Floyd_All_Shortest_Paths (const GT &, Distance, SA=SA()) -> Floyd_All_Shortest_Paths< GT, Distance, SA > | |
| The type of a distance given to the constructor is deduced from it: that of the constructor is not, since it is a parameter of its own. | |
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. With a negative cycle there are no shortest distances: the solver stops as soon as it finds one, has_negative_cycle() is then true, the matrices do not hold distances, and get_min_path() throws.
Of the parallel arcs between two nodes, the lightest counts, and get_min_path() takes it. A loop counts only if it is negative: it is then a negative cycle. In a graph that is not directed, each arc counts in both directions.
The solver keeps copies of the distance accessor and of the arc filter. A distance passed to the constructor may have state, or no default constructor; the constructors that do not take one build it with Distance(). get_min_path(), which is const, measures the parallel arcs with the copy of the solver if the call operator of the distance is const, and with a copy of it otherwise, so that concurrent calls do not share the state of the distance.
Template parameters:
GT: graph type.Distance<GT>: arc weight accessor that must provide:
SA: arc filter.Definition at line 167 of file Floyd_Warshall.H.
|
private |
Definition at line 170 of file Floyd_Warshall.H.
|
private |
Definition at line 171 of file Floyd_Warshall.H.
|
private |
Definition at line 169 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.
The type of the distance is a parameter of the constructor, so that braces, as in (g, {}), which give it no type, take the constructor with the filter.
| D | Type of the distance, which converts to Distance. |
| [in] | __g | Input graph (will be treated as const). |
| [in] | __distance | Distance accessor for the arc weights. |
| [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. |
| std::overflow_error | if, with an integer distance type, the sum of two distances is below lowest() before a negative cycle is found, which needs a weight below lowest() / (2 (n - 1)), with n nodes. A sum above max() does not count. |
Definition at line 362 of file Floyd_Warshall.H.
References Aleph::DynArray< T >::access(), ah_invalid_argument_if, ah_overflow_error_if, Aleph::DynMatrix< T >::allocate(), Aleph::and, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::dist, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::distance, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::g, GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::has_negative_diagonal(), Aleph::in_place_sort(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::index_node(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Inf, GraphCommon< GT, Node, Arc >::is_digraph(), k, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::n, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::negative_cycle, Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::path_mat, Aleph::DynArray< T >::reserve(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::sa, and w.
|
inline |
Construct Floyd-Warshall solver with a distance accessor built by Distance().
| [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. |
| std::overflow_error | as the first constructor. |
Definition at line 479 of file Floyd_Warshall.H.
|
inline |
Construct Floyd-Warshall solver with rvalue arc filter and a distance accessor built by Distance().
| [in] | g | Input graph. |
| [in] | sa | Arc filter, which may be a temporary. |
| std::invalid_argument | if graph has no nodes. |
| std::bad_alloc | if memory allocation fails. |
| std::overflow_error | as the first constructor. |
Definition at line 492 of file Floyd_Warshall.H.
|
default |
Copy the matrices of another solver, on the same graph.
| [in] | other | solver to copy. |
| std::bad_alloc | if there is no memory for the copy. |
|
inlinenoexcept |
Take the matrices of another solver, which is left without nodes, on the same graph.
The implicit move took the nodes and the matrices but copied their number: get_min_path() then passed its checks in the moved-from solver and read a matrix that it no longer had.
| [in,out] | other | solver to move from. |
| what | copying the infinity, which is const, or moving the filter or the distance throws; then other is left without nodes. |
Definition at line 514 of file Floyd_Warshall.H.
|
inlineprivate |
Definition at line 198 of file Floyd_Warshall.H.
References Aleph::and, Aleph::Path< GT >::append(), Aleph::Path< GT >::append_directed(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::g, GraphCommon< GT, Node, Arc >::is_digraph(), Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::sa.
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path().
|
inline |
Convert a distance value to string representation.
| [in] | e | Distance value to convert. |
Definition at line 530 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Inf.
Referenced by TEST().
|
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. With a negative cycle it holds no distances: see has_negative_cycle().
Definition at line 252 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::dist.
Referenced by compare_with_floyd(), Moved_From_Case< Floyd_All_Shortest_Paths< Dgraph > >::is_empty(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
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. |
| std::domain_error | if the graph has a negative cycle. |
Definition at line 578 of file Floyd_Warshall.H.
References ah_domain_error_if, ah_out_of_range_error_if, Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::append_lightest_arc(), 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, k, 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_distance(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::path_mat.
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_min_path(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TYPED_TEST(), TYPED_TEST(), and Moved_From_Case< Floyd_All_Shortest_Paths< Dgraph > >::use().
|
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, or if the graph has a negative cycle. |
Definition at line 623 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().
|
inline |
Get an owning copy of the graph nodes array.
Returns a copy of the sorted array of node pointers. This method is equivalent to get_nodes_copy() and provides a safe snapshot for long-term storage or when the Floyd_All_Shortest_Paths instance may be destroyed.
Definition at line 274 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_nodes_copy().
|
inline |
Get an owning copy of the graph nodes array.
Returns a copy of the sorted array of node pointers. This is safer for long-term storage or if the Floyd_All_Shortest_Paths instance is destroyed, but incurs an O(n) copy cost.
Definition at line 306 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes.
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_nodes(), and TEST().
|
inlinenoexcept |
Get the array of graph nodes as a constant reference.
Returns the sorted array of node pointers used for matrix indexing. This provides O(1) access to the underlying storage but requires the caller to ensure the Floyd_All_Shortest_Paths instance remains alive.
Definition at line 289 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes.
Referenced by Moved_From_Case< Floyd_All_Shortest_Paths< Dgraph > >::is_empty(), TEST(), and Moved_From_Case< Floyd_All_Shortest_Paths< Dgraph > >::use().
|
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. With a negative cycle it holds no paths: see has_negative_cycle().
Definition at line 241 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::path_mat.
Referenced by Moved_From_Case< Floyd_All_Shortest_Paths< Dgraph > >::is_empty(), and TEST().
|
inlineconstexprnoexcept |
Check if the graph contains a negative cycle.
Definition at line 230 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::negative_cycle.
Referenced by Moved_From_Case< Floyd_All_Shortest_Paths< Dgraph > >::is_empty(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TYPED_TEST().
|
inlineprivate |
Definition at line 214 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::dist, and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::n.
Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Floyd_All_Shortest_Paths().
|
inline |
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 324 of file Floyd_Warshall.H.
References ah_domain_error_if, ah_invalid_argument_if, Aleph::binary_search(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes, and Aleph::DynArray< T >::size().
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(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivate |
Definition at line 187 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::distance.
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 548 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 314 of file Floyd_Warshall.H.
References Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::nodes.
Referenced by TEST(), and Moved_From_Case< Floyd_All_Shortest_Paths< Dgraph > >::use().
|
related |
The type of a distance given to the constructor is deduced from it: that of the constructor is not, since it is a parameter of its own.
|
private |
Definition at line 179 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(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::has_negative_diagonal(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::print().
|
private |
Definition at line 182 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 >::path_distance().
|
private |
Definition at line 176 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().
|
private |
Definition at line 175 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 >::has_negative_diagonal(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::print().
|
private |
|
private |
Definition at line 173 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_copy(), Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::get_nodes_ref(), 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 180 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 >::append_lightest_arc().