|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Dial's algorithm for shortest paths with integer weights. More...
#include <Dial.H>
Classes | |
| class | Dial_Init_Guard |
| RAII guard for Dial initialization and cleanup. More... | |
| struct | Node_Info |
Public Types | |
| using | Distance_Type = typename Distance::Distance_Type |
| using | Node = typename GT::Node |
| using | Arc = typename GT::Arc |
Public Member Functions | |
| Dial_Min_Paths (Distance dist=Distance(), SA __sa=SA()) | |
| Constructor. | |
| bool | is_painted () const noexcept |
| Check if the graph has been painted. | |
| Node * | get_start_node () const noexcept |
| Get the start node of the last computation. | |
| GT * | get_graph () const noexcept |
| Get the graph of the last computation. | |
| void | paint_min_paths_tree (const GT &g, Node *start) |
| Paints the shortest paths tree on the graph from start. | |
| Distance_Type | get_distance (Node *node) |
| Gets the accumulated distance to a node after painting. | |
| Distance_Type | get_min_path (Node *end, Path< GT > &path) |
| Extracts a shortest path from a previously painted graph. | |
| Distance_Type | find_min_path (const GT &g, Node *start, Node *end, Path< GT > &path) |
| Computes the shortest path from start to end. | |
| Distance_Type | operator() (const GT &g, Node *start, Node *end, Path< GT > &path) |
| Computes shortest path (operator interface). | |
Private Member Functions | |
| void | reset_state_after_failure () noexcept |
| void | init (const GT &g) |
| void | uninit_paint () |
| void | uninit_discard () |
| Distance_Type | find_max_weight (const GT &g) |
| void | run_dial (const GT &g, Node *start, Node *end=nullptr) |
Private Attributes | |
| SA | sa |
| Distance | distance |
| bool | painted = false |
| GT * | ptr_g = nullptr |
| Node * | s = nullptr |
Static Private Attributes | |
| static constexpr size_t | Max_Sane_Buckets = 16u * 1024u * 1024u |
| static constexpr Distance_Type | Inf |
Dial's algorithm for shortest paths with integer weights.
This class computes shortest paths in graphs where every edge weight is a non-negative integer.
Dial's algorithm runs in O(V*C + E) time vs Dijkstra with a heap in O((V+E) log V), where C is the maximum edge weight. Use Dial when V*C + E << (V+E) log V (equivalently V*C << (V+E) log V). As a practical rule of thumb, Dial is especially effective on sparse graphs (small E) with small integer edge weights (small C).
The class receives the following template parameters:
GT: graph type (List_Graph, List_SGraph, Array_Graph, etc.).Distance: class reading the weight from an arc. Must export Distance_Type (which must be integral) and operator()(Arc*).Itor: arc iterator template (defaults to Node_Arc_Iterator).SA: arc filter for internal iterators.Distance_Type must be an integral type.
|
inlineprivate |
Definition at line 264 of file Dial.H.
References ah_domain_error_if, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::distance, Aleph::divide_and_conquer_partition_dp(), and w.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::run_dial().
|
inline |
Computes the shortest path from start to end.
Paints the graph (stopping early when end is found) and extracts the path.
| [in] | g | The graph. |
| [in] | start | The starting node. |
| [in] | end | The destination node. |
| [out] | path | The shortest path (empty if no path exists). |
| domain_error | If start or end is nullptr, g is empty, or any arc weight is negative. |
| overflow_error | If maximum possible distance exceeds Distance_Type range or during distance accumulation. |
| runtime_error | If bucket count exceeds safety limit. |
| bad_alloc | If there is not enough memory. |
Definition at line 547 of file Dial.H.
References ah_domain_error_if, Aleph::divide_and_conquer_partition_dp(), Aleph::Path< GT >::empty(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::get_min_path(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::Inf, Aleph::init, IS_NODE_VISITED, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::painted, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::run_dial(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::s, Aleph::Spanning_Tree, and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::uninit_paint().
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::operator()().
|
inline |
Gets the accumulated distance to a node after painting.
| [in] | node | The destination node. |
| domain_error | If the graph has not been painted, node is null, node is not reachable from the start, or path reconstruction fails. |
Definition at line 471 of file Dial.H.
References ah_domain_error_if, Aleph::and, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::distance, Aleph::divide_and_conquer_partition_dp(), GraphCommon< GT, Node, Arc >::get_connected_node(), IS_ARC_VISITED, IS_NODE_VISITED, NODE_COOKIE, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::painted, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::ptr_g, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::s, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::sa, and Aleph::Spanning_Tree.
|
inlinenoexcept |
Get the graph of the last computation.
Definition at line 425 of file Dial.H.
References Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::ptr_g.
|
inline |
Extracts a shortest path from a previously painted graph.
| [in] | end | Destination node of the path. |
| [out] | path | Path where the shortest path will be stored. |
| domain_error | If the graph has not been painted. |
| bad_alloc | If there is not enough memory. |
Definition at line 520 of file Dial.H.
References ah_domain_error_if, Aleph::divide_and_conquer_partition_dp(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::painted, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::ptr_g, and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::s.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::find_min_path().
|
inlinenoexcept |
Get the start node of the last computation.
Definition at line 420 of file Dial.H.
References Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::s.
|
inlineprivate |
Definition at line 208 of file Dial.H.
References NODE_COOKIE, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::ptr_g, GraphCommon< GT, Node, Arc >::reset_bit(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::reset_state_after_failure(), Aleph::Spanning_Tree, and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::uninit_discard().
|
inlinenoexcept |
Check if the graph has been painted.
Definition at line 415 of file Dial.H.
References Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::painted.
|
inline |
Computes shortest path (operator interface).
| [in] | g | The graph. |
| [in] | start | The starting node. |
| [in] | end | The destination node. |
| [out] | path | The shortest path. |
| domain_error | If start or end is nullptr, g is empty, or any arc weight is negative. |
| overflow_error | If maximum possible distance exceeds Distance_Type range or during distance accumulation. |
| runtime_error | If bucket count exceeds safety limit. |
| bad_alloc | If there is not enough memory. |
Definition at line 587 of file Dial.H.
References Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::find_min_path().
|
inline |
Paints the shortest paths tree on the graph from start.
After calling this method, NODE_COOKIE(p) stores the parent node in the shortest path tree, and the Spanning_Tree bit is set on nodes and arcs that belong to the tree.
| [in] | g | The graph. |
| [in] | start | The starting node. |
| domain_error | If start is nullptr, g is empty, or any arc weight is negative. |
| overflow_error | If maximum possible distance exceeds Distance_Type range or during distance accumulation. |
| runtime_error | If bucket count exceeds safety limit. |
| bad_alloc | If there is not enough memory. |
Definition at line 444 of file Dial.H.
References ah_domain_error_if, Aleph::divide_and_conquer_partition_dp(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::init, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::painted, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::run_dial(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::s, and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::uninit_paint().
Referenced by main(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivatenoexcept |
Definition at line 160 of file Dial.H.
References Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::painted, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::ptr_g, and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::s.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::Dial_Init_Guard::~Dial_Init_Guard(), and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::init().
|
inlineprivate |
Definition at line 277 of file Dial.H.
References ah_domain_error_if, ah_overflow_error_if, ah_runtime_error_if, Aleph::and, Aleph::Array< T >::append(), Aleph::DynList< T >::append(), ARC_BITS, DIAL_DIST, DIAL_PARC, DIAL_PARENT, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::distance, Aleph::divide_and_conquer_partition_dp(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::find_max_weight(), GraphCommon< GT, Node, Arc >::get_connected_node(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::HTList::is_empty(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::Max_Sane_Buckets, NODE_BITS, num_nodes, Aleph::DynList< T >::remove_first(), Aleph::Array< T >::remove_first(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::sa, Aleph::Spanning_Tree, and w.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::find_min_path(), and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::paint_min_paths_tree().
|
inlineprivate |
Definition at line 245 of file Dial.H.
References DIAL_INFO, Aleph::divide_and_conquer_partition_dp(), NODE_COOKIE, Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::ptr_g, GraphCommon< GT, Node, Arc >::reset_bit(), and Aleph::Spanning_Tree.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::Dial_Init_Guard::~Dial_Init_Guard(), and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::init().
|
inlineprivate |
Definition at line 233 of file Dial.H.
References DIAL_INFO, Aleph::divide_and_conquer_partition_dp(), NODE_COOKIE, and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::ptr_g.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::find_min_path(), and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::paint_min_paths_tree().
|
private |
Definition at line 137 of file Dial.H.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::find_max_weight(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::get_distance(), and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::run_dial().
|
staticconstexprprivate |
Definition at line 143 of file Dial.H.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::find_min_path().
|
staticconstexprprivate |
Definition at line 141 of file Dial.H.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::run_dial().
|
private |
Definition at line 138 of file Dial.H.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::find_min_path(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::get_distance(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::get_min_path(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::is_painted(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::paint_min_paths_tree(), and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::reset_state_after_failure().
|
private |
Definition at line 139 of file Dial.H.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::get_distance(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::get_graph(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::get_min_path(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::init(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::reset_state_after_failure(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::uninit_discard(), and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::uninit_paint().
|
private |
Definition at line 140 of file Dial.H.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::find_min_path(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::get_distance(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::get_min_path(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::get_start_node(), Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::paint_min_paths_tree(), and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::reset_state_after_failure().
|
private |
Definition at line 136 of file Dial.H.
Referenced by Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::get_distance(), and Aleph::Dial_Min_Paths< GT, Distance, Itor, SA >::run_dial().