|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Johnson's algorithm for all-pairs shortest paths. More...
#include <Johnson.H>
Classes | |
| struct | Reweighted_Dist |
Public Types | |
| using | Node = typename GT::Node |
| using | Arc = typename GT::Arc |
| using | Distance_Type = typename Distance::Distance_Type |
Public Member Functions | |
| Johnson (const GT &g, Distance d=Distance(), SA __sa=SA()) | |
| Construct a Johnson's all-pairs shortest paths executor. | |
| Johnson ()=default | |
| Default constructor (uninitialized state) | |
| bool | is_initialized () const noexcept |
| Check if the algorithm has been initialized. | |
| Distance_Type | get_distance (Node *src, Node *tgt) |
| Get the shortest distance from source to target. | |
| Path< GT > | find_path (Node *src, Node *tgt) |
| Find the shortest path from source to target. | |
| DynMapTree< std::pair< Node *, Node * >, Distance_Type > | compute_all_pairs_distances () |
| Compute the shortest distance for all pairs. | |
| Distance_Type | get_potential (Node *node) const |
| Get the node potential (h value) for a node. | |
| GT & | get_reweighted_graph () noexcept |
| Get access to the internal reweighted graph. | |
| const GT & | get_reweighted_graph () const noexcept |
| Get const access to the internal reweighted graph. | |
| Node * | get_copy_node (Node *orig) const |
| Get the copy of a node in the reweighted graph. | |
Private Member Functions | |
| void | reweight_arcs () |
| Reweight all arcs using node potentials. | |
| void | build_reverse_mappings (const GT &orig) |
| Path< GT > | map_copy_path_to_original (const Path< GT > ©_path) const |
Static Private Member Functions | |
| static Distance_Type | checked_add (const Distance_Type &a, const Distance_Type &b) |
| static Distance_Type | checked_sub (const Distance_Type &a, const Distance_Type &b) |
Private Attributes | |
| const GT * | orig_graph = nullptr |
| GraphCopyWithMapping< GT > | graph_copy |
| Copy of graph with node mapping. | |
| DynMapTree< Node *, Node * > | copy_to_orig_node |
| DynMapTree< Arc *, Arc * > | copy_to_orig_arc |
| DynMapTree< Node *, Distance_Type > | h |
| Node potentials from Bellman-Ford. | |
| Distance | dist |
| SA | sa |
| bool | initialized = false |
Johnson's algorithm for all-pairs shortest paths.
This class implements Johnson's algorithm for computing shortest paths between all pairs of vertices in a weighted directed graph. It handles graphs with negative edge weights (but not negative cycles).
The algorithm is efficient for sparse graphs, with complexity O(V² log V + VE), compared to Floyd-Warshall's O(V³).
| GT | Graph type (typically List_Digraph). |
| Distance | Arc weight accessor class. Must provide:
|
| Ait | Arc iterator template for traversing all arcs. |
| NAit | Node arc iterator template for outgoing arcs. |
| SA | Arc filter for internal iterators. |
Usage example:
|
inlineexplicit |
Construct a Johnson's all-pairs shortest paths executor.
The constructor:
| [in] | g | The graph to operate on. |
| [in] | d | Arc-weight accessor (default constructed). |
| [in] | __sa | Arc filter for internal iterators (default constructed). |
| std::bad_alloc | if there is not enough memory. |
| std::domain_error | if a negative cycle is detected. |
Definition at line 287 of file Johnson.H.
References Aleph::Johnson< GT, Distance, Ait, NAit, SA >::build_reverse_mappings(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::dist, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::h, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::initialized, Aleph::maps(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::reweight_arcs(), and Aleph::Johnson< GT, Distance, Ait, NAit, SA >::sa.
|
default |
Default constructor (uninitialized state)
|
inlineprivate |
Definition at line 203 of file Johnson.H.
References ah_domain_error_if, Aleph::DynList< T >::append(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::copy_to_orig_arc, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::copy_to_orig_node, Aleph::DynSetTree< Key, Tree, Compare >::empty(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::HTList::is_empty(), Aleph::maps(), Aleph::DynList< T >::remove_first(), and Aleph::DynSetTree< Key, Tree, Compare >::size().
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Johnson().
|
inlinestaticprivate |
Definition at line 154 of file Johnson.H.
References Aleph::shortest_path_detail::checked_add().
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::reweight_arcs().
|
inlinestaticprivate |
Definition at line 159 of file Johnson.H.
References ah_overflow_error_if, and Aleph::maps().
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::reweight_arcs().
|
inline |
Compute the shortest distance for all pairs.
| std::domain_error | if not initialized. |
Definition at line 391 of file Johnson.H.
References ah_domain_error_if, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::h, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::initialized, Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), IS_NODE_VISITED, Aleph::maps(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::sa, and Aleph::Spanning_Tree.
|
inline |
Find the shortest path from source to target.
| [in] | src | Source node (from original graph). |
| [in] | tgt | Target node (from original graph). |
| std::domain_error | if not initialized or nodes not found. |
Definition at line 360 of file Johnson.H.
References ah_domain_error_if, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::initialized, IS_NODE_VISITED, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::map_copy_path_to_original(), Aleph::maps(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::orig_graph, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::sa, and Aleph::Spanning_Tree.
|
inline |
Get the copy of a node in the reweighted graph.
| [in] | orig | Original node. |
Definition at line 459 of file Johnson.H.
References Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, and Aleph::maps().
|
inline |
Get the shortest distance from source to target.
| [in] | src | Source node (from original graph). |
| [in] | tgt | Target node (from original graph). |
| std::domain_error | if not initialized or nodes not found. |
Definition at line 320 of file Johnson.H.
References ah_domain_error_if, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::h, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::initialized, IS_NODE_VISITED, Aleph::maps(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::sa, and Aleph::Spanning_Tree.
|
inline |
Get the node potential (h value) for a node.
The potential h(v) represents the shortest distance from the dummy source to v, computed by Bellman-Ford.
| [in] | node | Node from the original graph. |
| std::domain_error | if not initialized or node not found. |
Definition at line 435 of file Johnson.H.
References ah_domain_error_if, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::h, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::initialized, and Aleph::maps().
|
inlinenoexcept |
Get const access to the internal reweighted graph.
Definition at line 452 of file Johnson.H.
References Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy.
|
inlinenoexcept |
Get access to the internal reweighted graph.
Useful for debugging or advanced operations.
Definition at line 448 of file Johnson.H.
References Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy.
|
inlinenoexcept |
Check if the algorithm has been initialized.
Definition at line 311 of file Johnson.H.
References Aleph::Johnson< GT, Distance, Ait, NAit, SA >::initialized.
|
inlineprivate |
Definition at line 244 of file Johnson.H.
References ah_domain_error_if, Aleph::DynList< T >::append(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::copy_to_orig_arc, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::copy_to_orig_node, LocateFunctions< Container, Type >::get_it(), Aleph::HTList::is_empty(), Aleph::maps(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::orig_graph, and Aleph::DynMapTree< Key, Data, Tree, Compare >::search().
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::find_path().
|
inlineprivate |
Reweight all arcs using node potentials.
New weight: w'(u,v) = w(u,v) + h(u) - h(v) This guarantees all weights are non-negative.
Definition at line 180 of file Johnson.H.
References Aleph::Johnson< GT, Distance, Ait, NAit, SA >::checked_add(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::checked_sub(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::copy_to_orig_arc, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::dist, Aleph::DynMapTree< Key, Data, Tree, Compare >::find(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::graph_copy, Aleph::Johnson< GT, Distance, Ait, NAit, SA >::h, Aleph::maps(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::sa, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), and w.
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Johnson().
|
private |
Definition at line 134 of file Johnson.H.
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::build_reverse_mappings(), and Aleph::Johnson< GT, Distance, Ait, NAit, SA >::map_copy_path_to_original().
|
private |
Definition at line 137 of file Johnson.H.
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Johnson(), and Aleph::Johnson< GT, Distance, Ait, NAit, SA >::reweight_arcs().
|
private |
Copy of graph with node mapping.
Definition at line 133 of file Johnson.H.
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Johnson(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::build_reverse_mappings(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::compute_all_pairs_distances(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::find_path(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_copy_node(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_distance(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_potential(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_reweighted_graph(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_reweighted_graph(), and Aleph::Johnson< GT, Distance, Ait, NAit, SA >::reweight_arcs().
|
private |
Node potentials from Bellman-Ford.
Definition at line 136 of file Johnson.H.
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Johnson(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::compute_all_pairs_distances(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_distance(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_potential(), and Aleph::Johnson< GT, Distance, Ait, NAit, SA >::reweight_arcs().
|
private |
Definition at line 139 of file Johnson.H.
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Johnson(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::compute_all_pairs_distances(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::find_path(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_distance(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_potential(), and Aleph::Johnson< GT, Distance, Ait, NAit, SA >::is_initialized().
|
private |
Definition at line 132 of file Johnson.H.
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::find_path(), and Aleph::Johnson< GT, Distance, Ait, NAit, SA >::map_copy_path_to_original().
|
private |
Definition at line 138 of file Johnson.H.
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::Johnson(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::compute_all_pairs_distances(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::find_path(), Aleph::Johnson< GT, Distance, Ait, NAit, SA >::get_distance(), and Aleph::Johnson< GT, Distance, Ait, NAit, SA >::reweight_arcs().