|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Base class providing common infrastructure for shortest path algorithms. More...
#include <shortest_path_common.H>
Classes | |
| struct | Arc_Info |
| Information stored in arc cookies for painting version. More... | |
| struct | Destroy_Arc |
| Arc cleanup for painting version. More... | |
| struct | Destroy_Node |
| Node cleanup for painting version. More... | |
| struct | Destroy_Tree_Arc |
| Arc cleanup and mapping for tree-building version. More... | |
| struct | Destroy_Tree_Node |
| Node cleanup and mapping for tree-building version. More... | |
| struct | Get_Potential_Arc |
| Functor to get arc potential for heap ordering. More... | |
| struct | Heap_Info |
| Functor to access heap node handle from node cookie. More... | |
| struct | Initialize_Arc |
| Arc initialization for painting version. More... | |
| struct | Initialize_Node |
| Node initialization for painting version. More... | |
| struct | Initialize_Tree_Arc |
| Arc initialization for tree-building version. More... | |
| struct | Initialize_Tree_Node |
| Node initialization for tree-building version. More... | |
| struct | Node_Info |
| Information stored in node cookies for painting version. More... | |
| struct | Painted_Arc |
| Arc filter that shows the arcs of the painted tree. More... | |
| struct | Painted_Node |
| Node filter that shows the nodes of the painted tree. More... | |
| struct | Total |
| Distance totalizer class for path copying. More... | |
| struct | Tree_Arc_Info |
| Extended arc info with tree arc mapping. More... | |
| struct | Tree_Node_Info |
| Extended node info with tree node mapping. More... | |
Public Types | |
| using | Heap = HeapT< GT, Get_Potential_Arc, Heap_Info > |
Public Member Functions | |
| 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. | |
Protected Member Functions | |
| template<class IN , class IA > | |
| void | init (const GT &g, typename GT::Node *start) |
| Initialize algorithm state. | |
| template<class DN , class DA > | |
| auto | uninit_on_exit () |
| Returns a guard that calls uninit() when it goes out of scope. | |
| template<class DN , class DA > | |
| void | uninit () |
| Cleanup algorithm state. | |
| bool | add_if_fits (const typename Distance::Distance_Type &a, const typename Distance::Distance_Type &b, typename Distance::Distance_Type &out) |
| Adds a tentative distance that may go beyond max(). | |
| void | check_beyond_max (const GT &g, typename GT::Node *start, typename GT::Node *end=nullptr) const |
After a painting in which a sum went beyond max(), throws if a node that start reaches was left out, or end if it is given. | |
| 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 | |
| SA | sa |
| Arc filter. | |
| Distance | distance |
| Arc weights. | |
| 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. | |
| bool | beyond_max = false |
| A sum of the painting went beyond max() | |
| bool | leave_out_beyond_max = false |
| Whether a painting leaves out without throwing the nodes whose distance does not fit. | |
Base class providing common infrastructure for shortest path algorithms.
This template class encapsulates shared functionality between Dijkstra's algorithm, A*, and similar shortest path algorithms. It provides:
| GT | Graph type. |
| Distance | Distance accessor functor. |
| Itor | Arc iterator template. |
| SA | Arc filter for iterators. |
| HeapT | Priority queue template. |
Definition at line 203 of file shortest_path_common.H.
| using Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::Heap = HeapT<GT, Get_Potential_Arc, Heap_Info> |
Definition at line 423 of file shortest_path_common.H.
|
inline |
Default constructor.
| dist | Distance functor for arc weights. |
| __sa | Arc filter for iterators. |
Definition at line 676 of file shortest_path_common.H.
|
inlineprotected |
Adds a tentative distance that may go beyond max().
Sets out to a + b and returns true, but for an integral type whose sum goes beyond max(): then no distance that fits goes through it, and it returns false and remembers it in beyond_max, which check_beyond_max() looks at when the painting ends.
| a | First operand. | |
| b | Second operand. | |
| [out] | out | The sum, if it fits. |
| std::overflow_error | If the sum goes below lowest(). |
Definition at line 591 of file shortest_path_common.H.
References ah_overflow_error_if, Aleph::and, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::beyond_max, and out.
Referenced by Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_partial_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_partial_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_partial_path(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_partial_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_partial_min_paths_tree(), and Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_partial_path().
|
inlineprotected |
After a painting in which a sum went beyond max(), throws if a node that start reaches was left out, or end if it is given.
Every path to such a node has a sum beyond max(), so that its distance does not fit. The walk goes through the arcs that the filter shows, without touching the painting. With leave_out_beyond_max, it throws nothing.
| shortest_path_detail::Beyond_Max_Error | If such a node exists; it is a std::overflow_error. |
| std::bad_alloc | If there is not enough memory. |
It is rare, and out of line: copied into a painting, its check of leave_out_beyond_max changed the code of the loop of the painting.
Definition at line 626 of file shortest_path_common.H.
References Aleph::DynListQueue< T >::get(), Aleph::DynSetTree< Key, Tree, Compare >::insert(), Aleph::DynListQueue< T >::is_empty(), IS_NODE_VISITED, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::leave_out_beyond_max, Aleph::DynListQueue< T >::put(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::sa, and Aleph::Spanning_Tree.
Referenced by Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_partial_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_partial_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_partial_path(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_partial_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_partial_min_paths_tree(), and Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_partial_path().
|
inlineprotected |
Checked addition to prevent integer overflow.
| a | First operand. |
| b | Second operand. |
| std::overflow_error | If overflow would occur. |
Definition at line 660 of file shortest_path_common.H.
References Aleph::shortest_path_detail::checked_add().
Referenced by Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_partial_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_partial_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_partial_path(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_partial_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_partial_min_paths_tree(), and Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_partial_path().
|
inline |
Extracts the painted shortest paths tree and puts it in tree.
Only the nodes that the painting reaches are copied, as compute_min_paths_tree() does. The nodes and arcs of g and of tree are mapped through their cookies; the cookies of the nodes that are not reached are null. Since the cookies held the painting, the painting cannot be used afterwards and is_painted() becomes false.
| [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 863 of file shortest_path_common.H.
References ah_domain_error_if, ARC_COOKIE, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::distance, Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), NODE_COOKIE, and Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::painted.
|
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 730 of file shortest_path_common.H.
References ah_domain_error_if, Aleph::and, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::distance, IS_ARC_VISITED, IS_NODE_VISITED, NODE_COOKIE, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::painted, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::s, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::sa, and Aleph::Spanning_Tree.
Referenced by TYPED_TEST().
|
inlinenoexcept |
Get the graph of the last computation.
Definition at line 710 of file shortest_path_common.H.
References Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::ptr_g.
Referenced by TYPED_TEST().
|
inline |
Extracts path from tree to end node.
| [in] | tree | The spanning tree built by compute_min_paths_tree. |
| [in] | end | Destination node (in original graph). |
| [out] | path | Path where result will be stored (in original graph), made of the arcs of g that those of the tree map to. |
g. | domain_error | If the minimum paths tree has not been computed, or end is not in the tree, nor reached in it from the start. |
| bad_alloc | If there is not enough memory. |
Definition at line 901 of file shortest_path_common.H.
References ah_domain_error_if, Aleph::Path< GT >::append(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::distance, Aleph::Path< GT >::empty(), Aleph::Path< GT >::Iterator::has_current_arc(), Aleph::Path< GT >::init(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::ptr_g, and Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::s.
|
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 781 of file shortest_path_common.H.
References ah_domain_error_if, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::distance, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::painted, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::ptr_g, and Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::s.
Referenced by TYPED_TEST().
|
inlinenoexcept |
Get the start node of the last computation.
Definition at line 705 of file shortest_path_common.H.
References Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::s.
Referenced by TYPED_TEST().
|
inlinenoexcept |
Check if a computation has been performed.
Definition at line 690 of file shortest_path_common.H.
References Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::ptr_g.
Referenced by TYPED_TEST().
|
inlineprotected |
Initialize algorithm state.
Gives every node and every arc shown by the filter a new cookie. With a filter that can throw (see nothrow_arc_filter_v), the bit Min of each arc says whether it has one, so that uninit() frees them without calling the filter again. A previous painting is lost, since its bits and cookies are reset.
| IN | Node initializer functor type. |
| IA | Arc initializer functor type. |
| g | The graph. |
| start | The starting node. |
| std::bad_alloc | If there is not enough memory. |
| anything | that the filter throws. |
If it throws, the cookies already allocated are freed and left null.
Definition at line 473 of file shortest_path_common.H.
References Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::beyond_max, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::heap, IS_ARC_VISITED, Aleph::Min, Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), num_nodes, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::painted, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::ptr_g, GraphCommon< GT, Node, Arc >::reset_bit(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::s, Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::sa, GraphCommon< GT, Node, Arc >::set_bit(), and Aleph::Spanning_Tree.
|
inlinenoexcept |
Check if the graph has been painted.
Every computation resets the bits and cookies of the previous one, so only a painting that ended normally can be used.
Definition at line 700 of file shortest_path_common.H.
References Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::painted.
Referenced by TYPED_TEST().
|
inlineprotected |
Cleanup algorithm state.
Frees the cookies that init() gave. With a filter that can throw, it finds those of the arcs by their bit Min, without calling the filter: it runs in the destructor of the guard of uninit_on_exit().
| DN | Node destroyer functor type. |
| DA | Arc destroyer functor type. |
Definition at line 562 of file shortest_path_common.H.
References Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::heap, IS_ARC_VISITED, Aleph::Min, Aleph::Filter_Iterator< Container, It, Show_Item >::next_ne(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::ptr_g, and Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::sa.
|
inlineprotected |
Returns a guard that calls uninit() when it goes out of scope.
The algorithms take one right after init(), so that the cookies are freed however they end: by returning or by an exception.
| DN | Node destroyer functor type. |
| DA | Arc destroyer functor type. |
Definition at line 547 of file shortest_path_common.H.
References Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::ptr_g.
|
protected |
A sum of the painting went beyond max()
Definition at line 437 of file shortest_path_common.H.
Referenced by Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::add_if_fits(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_partial_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::compute_partial_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::compute_partial_path(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::init(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_partial_min_paths_tree(), Aleph::Dijkstra_Min_Paths< GT, Distance, Itor, SA, HeapT >::paint_partial_min_paths_tree(), Aleph::AStar_Min_Path< GT, Distance, Heuristic, Itor, SA, HeapT >::paint_partial_path(), and Aleph::k_shortest_paths_detail::Painting_Within_Max< GT, Distance, Itor, SA >::went_beyond_max().
|
protected |
Arc weights.
Definition at line 431 of file shortest_path_common.H.
Referenced by Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::copy_painted_min_paths_tree(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_distance(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_min_path(), and Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_min_path().
|
protected |
Potential accessor.
Definition at line 432 of file shortest_path_common.H.
|
protected |
Priority queue.
Definition at line 433 of file shortest_path_common.H.
Referenced by Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::init(), and Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::uninit().
|
protected |
Whether a painting leaves out without throwing the nodes whose distance does not fit.
With it, check_beyond_max() throws nothing, so that a painting gives the nodes whose distances fit, and takes the others as nodes that the start does not reach; beyond_max says whether it may have left out some. It is false, and only a class derived from a painting sets it, for an algorithm that needs the nodes whose distances fit, as the index of Eppstein.
Definition at line 449 of file shortest_path_common.H.
Referenced by Aleph::k_shortest_paths_detail::Painting_Within_Max< GT, Distance, Itor, SA >::Painting_Within_Max(), and Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::check_beyond_max().
|
protected |
Whether graph has been painted.
Definition at line 434 of file shortest_path_common.H.
Referenced by Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::copy_painted_min_paths_tree(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_distance(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_min_path(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::init(), and Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::is_painted().
|
protected |
Pointer to the graph.
Definition at line 435 of file shortest_path_common.H.
Referenced by Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_graph(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_min_path(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_min_path(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::has_computation(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::init(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::uninit(), and Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::uninit_on_exit().
|
protected |
Start node.
Definition at line 436 of file shortest_path_common.H.
Referenced by Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_distance(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_min_path(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_min_path(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_start_node(), and Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::init().
|
protected |
Arc filter.
Definition at line 430 of file shortest_path_common.H.
Referenced by Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::check_beyond_max(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::get_distance(), Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::init(), and Aleph::Shortest_Path_Base< GT, Distance, Itor, SA, HeapT >::uninit().