|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm. More...
#include <Dijkstra.H>
Public Member Functions | |
| Dijkstra_Min_Paths (Distance dist=Distance(), SA __sa=SA()) | |
| Constructor. | |
| GT::Node * | compute_min_paths_tree (const GT &g, typename GT::Node *start, GT &tree) |
| Computes the spanning tree of all shortest paths from the start node. | |
| void | compute_partial_min_paths_tree (const GT &g, typename GT::Node *start, typename GT::Node *end, GT &tree) |
| Computes the partial spanning tree of all shortest paths from start that contains the path from start to end. | |
| bool | paint_partial_min_paths_tree (const GT &g, typename GT::Node *start, typename GT::Node *end) |
| Paints on graph g the partial shortest paths tree starting from start and stopping when the end node is found. | |
| void | paint_min_paths_tree (const GT &g, typename GT::Node *start) |
| Paints on graph g the spanning tree of all shortest paths starting from start. | |
| Distance::Distance_Type | find_min_path (const GT &g, typename GT::Node *start, typename GT::Node *end, Path< GT > &min_path) |
| Computes the shortest path between start and end by painting the graph. | |
| void | operator() (const GT &g, typename GT::Node *s, GT &tree) |
| Computes the spanning tree of all shortest paths from a given node according to Dijkstra's algorithm. | |
| Distance::Distance_Type | operator() (const GT &g, typename GT::Node *s, typename GT::Node *e, Path< GT > &path) |
| bool | has_computation () const noexcept |
| Check if a computation has been performed. | |
| bool | is_painted () const noexcept |
| Check if the graph has been painted. | |
| GT::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. | |
| Distance::Distance_Type | get_distance (typename GT::Node *node) |
| Gets the accumulated distance to a node after painting. | |
| Distance::Distance_Type | get_min_path (typename GT::Node *end, Path< GT > &path) |
| Extracts a shortest path to end from a previously painted graph. | |
| Distance::Distance_Type | get_min_path (const GT &tree, typename GT::Node *end, Path< GT > &path) |
| Extracts path from tree to end node. | |
| Distance::Distance_Type | copy_painted_min_paths_tree (GT &g, GT &tree) |
| Extracts the painted shortest paths tree and puts it in tree. | |
Public Member Functions inherited from Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT > | |
| Shortest_Path_Base (Distance dist=Distance(), SA __sa=SA()) | |
| Default constructor. | |
| bool | has_computation () const noexcept |
| Check if a computation has been performed. | |
| bool | is_painted () const noexcept |
| Check if the graph has been painted. | |
| GT::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. | |
| Distance::Distance_Type | get_distance (typename GT::Node *node) |
| Gets the accumulated distance to a node after painting. | |
| Distance::Distance_Type | get_min_path (typename GT::Node *end, Path< GT > &path) |
| Extracts a shortest path to end from a previously painted graph. | |
| Distance::Distance_Type | copy_painted_min_paths_tree (GT &g, GT &tree) |
| Extracts the painted shortest paths tree and puts it in tree. | |
| Distance::Distance_Type | get_min_path (const GT &tree, typename GT::Node *end, Path< GT > &path) |
| Extracts path from tree to end node. | |
Private Types | |
| using | Base = Shortest_Path_Base< GT, Distance, Itor, SA, HeapT > |
Private Attributes | |
| SA | sa |
| Arc filter. | |
| Heap | heap |
| Priority queue. | |
| bool | painted |
| Whether graph has been painted. | |
| GT * | ptr_g |
| Pointer to the graph. | |
| GT::Node * | s |
| Start node. | |
Additional Inherited Members | |
Public Types inherited from Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT > | |
| using | Heap = HeapT< GT, Get_Potential_Arc, Heap_Info > |
Protected Member Functions inherited from Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT > | |
| template<class IN , class IA > | |
| void | init (const GT &g, typename GT::Node *start) |
| Initialize algorithm state. | |
| template<class DN , class DA > | |
| void | uninit () |
| Cleanup algorithm state. | |
| Distance::Distance_Type | checked_add (const typename Distance::Distance_Type &a, const typename Distance::Distance_Type &b) const |
| Checked addition to prevent integer overflow. | |
Protected Attributes inherited from Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT > | |
| SA | sa |
| Arc filter. | |
| Get_Potential_Arc | get_pot |
| Potential accessor. | |
| Heap | heap |
| Priority queue. | |
| bool | painted = false |
| Whether graph has been painted. | |
| GT * | ptr_g = nullptr |
| Pointer to the graph. | |
| GT::Node * | s = nullptr |
| Start node. | |
Spanning tree calculation of all shortest paths from a given node according to Dijkstra's algorithm.
This class handles Dijkstra's algorithm to compute a tree containing of all shortest paths from a node s of a graph g.
The algorithm uses an internal queue whose length is proportional to the number of nodes in the graph.
Dijkstra's algorithm does not work for graphs with negative weights.
The class receives the following template parameters:
GT: graph type.Distance<GT>: class reading the weight. It should export the following attributes:typedef Distance<GT>::Distance_Type: Data type. It represents a weight on an arc.Distance<GT>::Distance_Type operator()(typename GT::Arc *a): returns the weight of arc a.Itor<GT, SA>: arc iterator type (defaults to Node_Arc_Iterator).SA: arc filter for internal iterators.HeapT<GT, Distance, Access>: priority-queue adapter used by the algorithm (defaults to ArcHeap). It must provide put_arc, get_min_arc, is_empty, and empty, and accept an access functor that maps nodes to heap handles.Definition at line 99 of file Dijkstra.H.
|
private |
Definition at line 102 of file Dijkstra.H.
|
inline |
Constructor.
| [in] | dist | Distance functor for arc weights. |
| [in] | __sa | Arc filter for iterators. |
Definition at line 142 of file Dijkstra.H.
|
inline |
Computes the spanning tree of all shortest paths from the start node.
| [in] | g | The graph. |
| [in] | start | The starting node for all shortest paths. |
| [out] | tree | The spanning tree of all shortest paths starting from start, mapped via cookies to the graph g. |
| bad_alloc | If there is not enough memory. |
| domain_error | If start is nullptr or g is empty. |
Definition at line 170 of file Dijkstra.H.
References ACC, ah_domain_error_if, ARC_BITS, ARC_DIST, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::checked_add(), Aleph::clear_graph(), GTNodeCommon< NodeInfo >::get_info(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::heap, Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), IS_ARC_VISITED, Aleph::HTList::is_empty(), IS_NODE_VISITED, Aleph::maps(), NODE_BITS, NODE_COOKIE, POT, Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::sa, Aleph::Spanning_Tree, TREEARC, and TREENODE.
Referenced by main(), and Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::operator()().
|
inline |
Computes the partial spanning tree of all shortest paths from start that contains the path from start to end.
| [in] | g | The graph. |
| [in] | start | The starting node for all shortest paths. |
| [in] | end | The destination node. |
| [out] | tree | The spanning tree of all shortest paths starting from start, mapped via cookies to the graph g. |
| bad_alloc | If there is not enough memory. |
| domain_error | If start or end is nullptr, or if g is empty. |
Definition at line 254 of file Dijkstra.H.
References ACC, ah_domain_error_if, ARC_BITS, ARC_DIST, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::checked_add(), Aleph::clear_graph(), GTNodeCommon< NodeInfo >::get_info(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::heap, Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), IS_ARC_VISITED, Aleph::HTList::is_empty(), IS_NODE_VISITED, Aleph::maps(), NODE_BITS, NODE_COOKIE, POT, Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::sa, Aleph::Spanning_Tree, TREEARC, and TREENODE.
|
inline |
Extracts the painted shortest paths tree and puts it in tree.
| [in] | g | The previously painted graph. |
| [out] | tree | The graph where the spanning tree will be copied. |
| bad_alloc | If there is not enough memory. |
| domain_error | If the graph has not been previously painted. |
Definition at line 566 of file shortest_path_common.H.
|
inline |
Computes the shortest path between start and end by painting the graph.
This is the version with the lowest memory consumption and probably also the fastest.
| [in] | g | The graph. |
| [in] | start | The starting node of the path. |
| [in] | end | The destination node of the path. |
| [out] | min_path | The shortest path. |
| bad_alloc | If there is not enough memory. |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 502 of file Dijkstra.H.
References Aleph::DynList< T >::empty(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::get_min_path(), Aleph::maps(), and Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_partial_min_paths_tree().
Referenced by Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::operator()().
|
inline |
Gets the accumulated distance to a node after painting.
After painting the graph with a shortest path tree, this method traverses the path from the start node to the given node and computes the total distance.
| [in] | node | The destination node. |
| domain_error | If the graph has not been painted or if node is not reachable from the start node. |
Definition at line 480 of file shortest_path_common.H.
|
inlinenoexcept |
Get the graph of the last computation.
Definition at line 460 of file shortest_path_common.H.
|
inline |
Extracts path from tree to end node.
| [in] | tree | The spanning tree built by compute_min_paths_tree. |
| [in] | end | Destination node in the original graph. |
| [out] | path | Path where result will be stored (in original graph). |
Definition at line 585 of file shortest_path_common.H.
|
inline |
Extracts a shortest path to end from a previously painted graph.
Takes a graph on which shortest paths have been painted and extracts the shortest path to end, storing it in path.
| [in] | end | Destination node of the path. |
| [out] | path | Path where the shortest path will be stored. |
| bad_alloc | If there is not enough memory. |
| domain_error | If the graph has not been previously painted. |
Definition at line 531 of file shortest_path_common.H.
Referenced by Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::find_min_path().
|
inlinenoexcept |
Get the start node of the last computation.
Definition at line 455 of file shortest_path_common.H.
|
inlinenoexcept |
Check if a computation has been performed.
Definition at line 445 of file shortest_path_common.H.
|
inlinenoexcept |
Check if the graph has been painted.
Definition at line 450 of file shortest_path_common.H.
|
inline |
Computes the spanning tree of all shortest paths from a given node according to Dijkstra's algorithm.
| [in] | g | The graph. |
| [in] | s | The starting node. |
| [out] | tree | The spanning tree. |
| bad_alloc | If there is not enough memory. |
Definition at line 521 of file Dijkstra.H.
References Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_min_paths_tree(), and Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::s.
|
inline |
Definition at line 527 of file Dijkstra.H.
References Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::find_min_path(), and Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::s.
|
inline |
Paints on graph g the spanning tree of all shortest paths starting from start.
| [in] | g | The graph. |
| [in] | start | The starting node for shortest paths. |
| bad_alloc | If there is not enough memory. |
| domain_error | If start is nullptr or g is empty. |
Definition at line 424 of file Dijkstra.H.
References ACC, ah_domain_error_if, ARC_BITS, ARC_DIST, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::checked_add(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::heap, IS_ARC_VISITED, Aleph::HTList::is_empty(), IS_NODE_VISITED, Aleph::maps(), NODE_BITS, Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::painted, PARENT, POT, Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::sa, and Aleph::Spanning_Tree.
|
inline |
Paints on graph g the partial shortest paths tree starting from start and stopping when the end node is found.
| [in] | g | The graph. |
| [in] | start | The starting node for shortest paths. |
| [in] | end | The destination node. |
| bad_alloc | If there is not enough memory. |
| domain_error | If start or end is nullptr, or if g is empty. |
Definition at line 339 of file Dijkstra.H.
References ACC, ah_domain_error_if, ARC_BITS, ARC_DIST, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::checked_add(), GraphCommon< GT, Node, Arc >::get_num_nodes(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::heap, IS_ARC_VISITED, Aleph::HTList::is_empty(), IS_NODE_VISITED, Aleph::maps(), NODE_BITS, Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::painted, PARENT, POT, Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::sa, and Aleph::Spanning_Tree.
Referenced by Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::find_min_path().
|
private |
Priority queue.
Definition at line 363 of file shortest_path_common.H.
Referenced by Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_partial_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_min_paths_tree(), and Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_partial_min_paths_tree().
|
private |
Whether graph has been painted.
Definition at line 364 of file shortest_path_common.H.
Referenced by Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_min_paths_tree(), and Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_partial_min_paths_tree().
|
private |
Pointer to the graph.
Definition at line 365 of file shortest_path_common.H.
|
private |
Start node.
Definition at line 366 of file shortest_path_common.H.
Referenced by Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::operator()(), and Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::operator()().
|
private |
Arc filter.
Definition at line 361 of file shortest_path_common.H.
Referenced by Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_partial_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_min_paths_tree(), and Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_partial_min_paths_tree().