|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Bellman-Ford algorithm for shortest paths with negative weights. More...
#include <Bellman_Ford.H>
Classes | |
| struct | Ni |
| struct | Sni |
Public Member Functions | |
| Bellman_Ford (GT &__g, Distance d=Distance(), SA __sa=SA()) | |
| Construct a Bellman-Ford executor. | |
| void | clear () noexcept(nothrow_arc_filter_v< GT, SA >) |
| Clear the painted state and reset internal data structures. | |
| bool | has_computation () const noexcept |
| Check if a shortest-path tree has been computed or painted. | |
| bool | is_painted () const noexcept |
| Check if a shortest-path tree has been painted. | |
| Node * | get_start_node () const noexcept |
| Get the start node of the last computation. | |
| const GT & | get_graph () const noexcept |
| Get reference to the graph. | |
| bool | paint_spanning_tree (Node *start) |
Paint the shortest paths tree from a start node. | |
| bool | faster_paint_spanning_tree (Node *start) |
Faster shortest paths tree painting from a start node. | |
| bool | has_negative_cycle (Node *start) |
| Test if a negative cycle exists starting from a specific node. | |
| bool | has_negative_cycle () |
| Test if a negative cycle exists anywhere in the graph. | |
| Path< GT > | test_negative_cycle (Node *start) |
Search a negative cycle on all possible paths starting from start node. | |
| Path< GT > | test_negative_cycle () |
| Searches and returns a negative cycle (if it exists). | |
| std::tuple< Path< GT >, size_t > | search_negative_cycle (Node *start, double it_factor, const size_t step) |
| Searches a negative cycle using the faster version of Bellman-Ford algorithm and iteratively searching the cycle before finishing up. | |
| Path< GT > | search_negative_cycle (Node *start) |
| Searches a negative cycle using the faster version of Bellman-Ford algorithm (SPFA variant). | |
| std::tuple< Path< GT >, size_t > | search_negative_cycle (double it_factor, const size_t step) |
| Searches a negative cycle using the faster version of Bellman-Ford algorithm and iteratively searching the cycle before finishing up. | |
| Path< GT > | search_negative_cycle () |
See search_negative_cycle(Node * start). | |
| DynArray< Arc * > | extract_min_spanning_tree () |
| Extract the shortest paths tree in a compressed form. | |
| Distance_Type | get_distance (typename GT::Node *node) |
| Get the accumulated distance to a node from a previously painted tree. | |
| void | build_tree (GT &tree, bool with_map=true) |
| Extract from the graph a previously painted shortest paths tree. | |
| bool | test_negative_cycle (typename GT::Node *s, Path< GT > &cycle) |
| Test for negative cycle and return it if found. | |
| bool | test_negative_cycle (Path< GT > &cycle) |
| Test for negative cycle anywhere in the graph. | |
| Distance_Type | get_min_path (typename GT::Node *end, Path< GT > &path) |
| Extract the shortest path to a node from a previously painted tree. | |
| DynMapTree< Node *, Distance_Type > | compute_nodes_weights () |
| Returns a mapping with the shortest distances to each node obtained after executing the Bellman-Ford algorithm from a virtual source connected to all nodes with zero-weight edges: every node starts at 0, and the graph gets no node. | |
Private Types | |
| typedef Distance::Distance_Type | Distance_Type |
| using | Node = typename GT::Node |
| using | Arc = typename GT::Arc |
Private Member Functions | |
| Distance_Type | checked_add (const Distance_Type &a, const Distance_Type &b) const |
| bool | add_if_fits (const Distance_Type &a, const Distance_Type &b, Distance_Type &out) |
| void | check_beyond_max (Node *start) |
| bool | relaxes (const Distance_Type &sum, const Distance_Type &accum) const |
| Whether sum relaxes the distance accum: it is below it, by more than the tolerance of the distance, if it gives one (see the class). | |
| void | init_with_indexes (Node *start) |
| Initialize node cookies with predecessor tracking for path reconstruction. | |
| void | abandon () noexcept |
| Leave the object without a computation, and the graph without the data of the nodes, after an operation that throws. | |
| template<class Info_Type > | |
| void | uninit () noexcept |
| Release the memory associated with the node cookies, and the bits Depth_First that SPFA leaves on the nodes still in its queue. | |
| bool | check_painted_arcs () noexcept(nothrow_arc_filter_v< GT, SA >) |
| Check that painted arcs form a valid spanning tree structure. | |
| void | relax_arcs () |
| Relax all arcs up to n-1 times (standard Bellman-Ford). | |
| void | relax_arcs (typename GT::Node *src, DynListQueue< typename GT::Node * > &q) |
| Relax outgoing arcs from a source node (SPFA variant). | |
| void | paint_tree () noexcept |
| Paint the spanning tree nodes and arcs with the Spanning_Tree bit. | |
| bool | last_relax_and_prepare_check_negative_cycle () |
| Perform one more relaxation pass and check for negative cycle. | |
| bool | last_relax_and_test_negative_cycle () |
| Perform one more relaxation pass to detect (but not prepare) negative cycle. | |
| void | link_cookies_and_free (typename GT::Node *start) noexcept |
| Free node cookies and set up predecessor pointers for path reconstruction. | |
| void | put_sources_in_queue (DynListQueue< typename GT::Node * > &q) |
| Put in q the start, or, without one, every node, as the virtual source reaches them. | |
| Node * | pred_node (Node *p) noexcept |
| The source of the arc by which the last relaxation of p reached it, or nullptr. | |
| Path< GT > | cycle_of_predecessors () |
| A cycle of the graph of the predecessors, the arcs of the last relaxations, as a path of its arcs; empty if there is none. | |
| Path< GT > | negative_cycle_witness () |
| The witness of a negative cycle that the last pass found. | |
| bool | detect_negative_cycle (Node *start) |
| Path< GT > | find_negative_cycle (Node *start) |
| Without a start, every node is a source. | |
| std::tuple< Path< GT >, size_t > | faster_find_negative_cycle (Node *start, double it_factor, size_t step) |
| SPFA, which, from it_factor * n rounds, and then every step rounds, looks for a cycle among the predecessors; without a start, every node is a source. | |
| Path< GT > | faster_find_negative_cycle (Node *start) |
| SPFA; without a start, every node is a source. | |
Static Private Member Functions | |
| static Distance_Type & | accum (Node *p) noexcept |
| static int & | idx (Node *p) noexcept |
| static size_t & | mark (Node *p) noexcept |
| static void | throw_not_finite () |
| static void | check_finite (const Distance_Type &sum) |
| Throw domain_error if a floating sum of the last pass is -inf or NaN, from a weight or from an overflow: -inf plus a weight is not below -inf, so that a negative cycle that it reaches could be missed. | |
| static void | put_in_queue (DynListQueue< typename GT::Node * > &q, typename GT::Node *p) |
| Insert a node into the queue if not already present (SPFA optimization). | |
| static GT::Node * | get_from_queue (DynListQueue< typename GT::Node * > &q) |
| Remove a node from the queue and clear its in-queue flag. | |
Private Attributes | |
| Array< typename GT::Arc * > | arcs_ |
| GT & | g_ |
| const Distance_Type | Inf |
| bool | painted_ = false |
| bool | parents_ = false |
| bool | beyond_max_ = false |
| Node * | s_ = nullptr |
| Node * | relaxed_ = nullptr |
| SA | sa_ |
| Distance | dist_ |
Bellman-Ford algorithm for shortest paths with negative weights.
This class implements the Bellman-Ford algorithm for finding shortest paths from a single source node. Unlike Dijkstra's algorithm, Bellman-Ford can handle graphs with negative edge weights and can detect negative cycles.
Two versions are provided:
Template parameters:
GT: Graph type (typically List_Digraph).Distance: Arc weight accessor. Must define Distance_Type and provide Distance_Type operator()(typename GT::Arc *a). It should have no side effects: how many times it is called per arc is unspecified (the relaxation passes stop as soon as one relaxes nothing). It may give Distance_Type tolerance() const, which must not be negative: a relaxation must then improve a distance by more than it, so that the rounding of floating sums does not make a cycle of weight zero negative, as an arc and its residual, of opposite weights, in a residual network. A node without a distance takes any sum that is below max(), as without a tolerance.Ait: Arc iterator template for traversing all arcs.NAit: Node arc iterator template for traversing arcs from a node.SA: Arc filter for the internal iterators.Features:
This class maintains internal state between operations:
painted: Whether a spanning tree is currently painted on the graphs: The start node of the last computationarcs: Predecessor array for path reconstruction
Important usage notes:**
A negative cycle is a cycle of the graph of the predecessors, the arcs of the last relaxations, whose cycles are all negative. It is given by its arcs: with parallel arcs, the one that was relaxed, and only arcs that the filter shows. A floating distance that the last pass finds -inf or NaN, from a weight or from an overflow, throws domain_error.
With an integer distance, a sum beyond max() is no path, and one below lowest() throws overflow_error at once. If a node that the start reaches is left without a distance, as its distances do not fit, the paintings and the searches from a start throw overflow_error, unless they find a negative cycle: the one that such a node could reach would be missed. Without a start every node begins at 0, and no sum goes beyond max().
Definition at line 188 of file Bellman_Ford.H.
|
private |
Definition at line 193 of file Bellman_Ford.H.
|
private |
Definition at line 190 of file Bellman_Ford.H.
|
private |
Definition at line 192 of file Bellman_Ford.H.
|
inline |
Construct a Bellman-Ford executor.
| [in] | __g | The graph to operate on. Note: the graph will be temporarily modified (node/arc bits and cookies) during algorithm execution. The modifications are cleaned up after each operation completes. |
| [in] | d | Arc-weight accessor. |
| [in] | __sa | Arc filter for internal iterators. |
Definition at line 474 of file Bellman_Ford.H.
|
inlineprivatenoexcept |
Leave the object without a computation, and the graph without the data of the nodes, after an operation that throws.
Definition at line 383 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::arcs_, Aleph::Array< T >::empty(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::painted_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::parents_, and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::compute_nodes_weights(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::detect_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_spanning_tree().
|
inlinestaticprivatenoexcept |
Definition at line 206 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::accum(), and NODE_COOKIE.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::accum(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_beyond_max(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::compute_nodes_weights(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_test_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relaxes().
|
inlineprivate |
Definition at line 241 of file Bellman_Ford.H.
References ah_overflow_error_if, Aleph::and, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::beyond_max_, and out.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_test_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs().
|
inline |
Extract from the graph a previously painted shortest paths tree.
This method requires that the shortest paths tree has been computed with paint_spanning_tree() or faster_paint_spanning_tree().
| [in] | tree | a graph where the shortest paths tree will be stored. |
| [in] | with_map | if true the extracted tree is mapped through the cookies with the graph; then the cookies no longer hold the parents, and the tree must be painted again for the methods that read them. |
| domain_error | if the tree has not been painted, or if a former build_tree() mapped it. |
Definition at line 1266 of file Bellman_Ford.H.
References ah_domain_error_if, ah_logic_error_unless, Aleph::and, ARC_COOKIE, Aleph::clear_graph(), FunctionalMethods< Container, T >::for_each(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Digraph_Iterator< GT, Filter >::has_curr(), Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_arc(), Aleph::List_Graph< _Graph_Node, _Graph_Arc >::insert_node(), IS_ARC_VISITED, GraphCommon< GT, Node, Arc >::map_arcs(), GraphCommon< GT, Node, Arc >::map_nodes(), NODE_COOKIE, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::painted_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::parents_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::sa_, Aleph::DynMapTree< Key, Data, Tree, Compare >::search(), and Aleph::Spanning_Tree.
|
inlineprivate |
Definition at line 262 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::accum(), ah_overflow_error_if, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, Aleph::DynListQueue< T >::get(), GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::Inf, Aleph::DynSetTree< Key, Tree, Compare >::insert(), Aleph::DynListQueue< T >::is_empty(), Aleph::DynListQueue< T >::put(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::sa_.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::detect_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_spanning_tree().
|
inlinestaticprivate |
Throw domain_error if a floating sum of the last pass is -inf or NaN, from a weight or from an overflow: -inf plus a weight is not below -inf, so that a negative cycle that it reaches could be missed.
One comparison for each arc of the last pass, inlined.
Definition at line 330 of file Bellman_Ford.H.
References Aleph::sum(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::throw_not_finite().
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_test_negative_cycle().
|
inlineprivatenoexcept |
Check that painted arcs form a valid spanning tree structure.
Verifies the following invariants:
For disconnected graphs, only the component reachable from s is verified.
| anything | that the arc filter throws. |
Definition at line 418 of file Bellman_Ford.H.
References Aleph::and, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, GraphCommon< GT, Node, Arc >::get_tgt_node(), IS_ARC_VISITED, IS_NODE_VISITED, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::sa_, and Aleph::Spanning_Tree.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_spanning_tree().
|
inlineprivate |
Definition at line 222 of file Bellman_Ford.H.
References ah_overflow_error_if.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_distance().
|
inlinenoexcept |
Clear the painted state and reset internal data structures.
This method should be called if you want to reuse the same Bellman_Ford object for a different computation, or if you want to clear the Spanning_Tree bits that were painted on the graph.
After calling clear():
| anything | that the arc filter throws, while it clears the bits of a painting. |
Definition at line 500 of file Bellman_Ford.H.
References ARC_BITS, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::arcs_, Aleph::Array< T >::empty(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, NODE_BITS, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::painted_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::parents_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::sa_, and Aleph::Spanning_Tree.
|
inline |
Returns a mapping with the shortest distances to each node obtained after executing the Bellman-Ford algorithm from a virtual source connected to all nodes with zero-weight edges: every node starts at 0, and the graph gets no node.
This routine is specifically designed for Johnson's algorithm for computing all-pairs shortest paths.
| std::domain_error | if negative cycles are detected. |
Definition at line 1371 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::abandon(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::accum(), ah_domain_error_if, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_from_queue(), GraphCommon< GT, Node, Arc >::get_node_it(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::DynMapTree< Key, Data, Tree, Compare >::insert(), Aleph::DynListQueue< T >::is_empty(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_in_queue(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_sources_in_queue(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs().
Referenced by Aleph::Johnson< GT, Distance, Ait, NAit, SA >::narrow_potentials(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlineprivate |
A cycle of the graph of the predecessors, the arcs of the last relaxations, as a path of its arcs; empty if there is none.
Every cycle of that graph is negative: the relaxation of its last arc made its end lower than the sum of the weights from it. The search goes back from relaxed_ first, and then from each node, each walk marking the nodes that it reaches.
Definition at line 761 of file Bellman_Ford.H.
References Aleph::and, Aleph::Path< GT >::append_directed(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::arcs_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, LocateFunctions< Container, Type >::get_it(), GraphCommon< GT, Node, Arc >::get_src_node(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::idx(), Aleph::DynList< T >::insert(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::mark(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::pred_node(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relaxed_.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::negative_cycle_witness().
|
inlineprivate |
Definition at line 945 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::abandon(), Aleph::and, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::beyond_max_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_beyond_max(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_test_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::has_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::has_negative_cycle().
|
inline |
Extract the shortest paths tree in a compressed form.
On a previously painted shortest paths tree starting from a source node, this method extracts the tree and loads it in an array of arcs.
| domain_error | if the shortest paths tree has not been previously built. |
Definition at line 1199 of file Bellman_Ford.H.
References ah_domain_error_if, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::arcs_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::painted_, Aleph::DynArray< T >::reserve(), Aleph::Array< T >::size(), and Aleph::DynArray< T >::touch().
|
inlineprivate |
SPFA; without a start, every node is a source.
Definition at line 1035 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::abandon(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::beyond_max_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_beyond_max(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_from_queue(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::DynListQueue< T >::is_empty(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::link_cookies_and_free(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::negative_cycle_witness(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_in_queue(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_sources_in_queue(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_.
|
inlineprivate |
SPFA, which, from it_factor * n rounds, and then every step rounds, looks for a cycle among the predecessors; without a start, every node is a source.
Definition at line 981 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::abandon(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::beyond_max_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_beyond_max(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::cycle_of_predecessors(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_from_queue(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::DynListQueue< T >::is_empty(), Aleph::Path< GT >::is_empty(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::link_cookies_and_free(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::negative_cycle_witness(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_in_queue(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_sources_in_queue(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::search_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::search_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::search_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::search_negative_cycle().
|
inline |
Faster shortest paths tree painting from a start node.
This method executes a faster version of Bellman-Ford algorithm (SPFA variant) which is often more efficient in practice.
| [in] | start | source node from which the shortest paths will be computed. |
false is returned and the shortest paths tree is painted with the bit Spanning_Tree. Definition at line 868 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::abandon(), ah_domain_error_if, Aleph::and, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::beyond_max_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_beyond_max(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_painted_arcs(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_from_queue(), GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::DynListQueue< T >::is_empty(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::link_cookies_and_free(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_in_queue(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_.
Referenced by example_spfa_comparison(), TEST(), TEST(), TEST(), and TEST_F().
|
inlineprivate |
Without a start, every node is a source.
Definition at line 961 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::abandon(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::beyond_max_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_beyond_max(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::link_cookies_and_free(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::negative_cycle_witness(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::test_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::test_negative_cycle().
|
inline |
Get the accumulated distance to a node from a previously painted tree.
This method is useful when you only need the distance without reconstructing the full path.
| [in] | node | The destination node. |
| std::domain_error | if the graph has not been previously painted or if the node is unreachable. |
Definition at line 1220 of file Bellman_Ford.H.
References ah_domain_error_if, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::checked_add(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::dist_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, GraphCommon< GT, Node, Arc >::get_tgt_node(), IS_ARC_VISITED, Aleph::Dfs_Stack< T >::is_empty(), NODE_COOKIE, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::painted_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::parents_, Aleph::Dfs_Stack< T >::pop(), Aleph::Dfs_Stack< T >::push(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::sa_, Aleph::Spanning_Tree, and Aleph::Dfs_Stack< T >::top().
Referenced by example_basic_negative_weights(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlinestaticprivate |
Remove a node from the queue and clear its in-queue flag.
Definition at line 598 of file Bellman_Ford.H.
References Aleph::Depth_First, Aleph::DynListQueue< T >::get(), IS_NODE_VISITED, and NODE_BITS.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::compute_nodes_weights(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree().
|
inlinenoexcept |
Get reference to the graph.
Definition at line 541 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_.
Referenced by TEST().
|
inline |
Extract the shortest path to a node from a previously painted tree.
| [in] | end | The destination node. |
| [out] | path | Path where the shortest path will be stored. |
| std::domain_error | if the graph has not been previously painted. |
Definition at line 1353 of file Bellman_Ford.H.
References ah_domain_error_if, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::dist_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::painted_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::parents_, and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_.
Referenced by example_basic_negative_weights(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), and TYPED_TEST().
|
inlinenoexcept |
Get the start node of the last computation.
Definition at line 534 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_.
|
inlinenoexcept |
Check if a shortest-path tree has been computed or painted.
Definition at line 520 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_.
Referenced by TEST().
|
inline |
Test if a negative cycle exists anywhere in the graph.
Every node is a source, at distance 0, as if a virtual source reached each one by an arc of weight 0; the graph gets no node.
Definition at line 939 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::detect_negative_cycle().
|
inline |
Test if a negative cycle exists starting from a specific node.
| [in] | start | Source node from which to check for negative cycles. |
| std::domain_error | if start is nullptr. |
Definition at line 925 of file Bellman_Ford.H.
References ah_domain_error_if, and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::detect_negative_cycle().
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), and TYPED_TEST().
|
inlinestaticprivatenoexcept |
Definition at line 211 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::idx(), and NODE_COOKIE.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::cycle_of_predecessors(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::idx(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::pred_node(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs().
|
inlineprivate |
Initialize node cookies with predecessor tracking for path reconstruction.
Without a start, every node starts at 0, as if a virtual source reached each one by an arc of weight 0.
Definition at line 340 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::Sni::accum, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::accum(), Aleph::Array< T >::append(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::arcs_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::beyond_max_, Aleph::Depth_First, Aleph::Array< T >::empty(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, GraphCommon< GT, Node, Arc >::get_num_nodes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::Inf, NODE_BITS, NODE_COOKIE, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::painted_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::parents_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relaxed_, Aleph::Array< T >::reserve(), GraphCommon< GT, Node, Arc >::reset_arcs(), GraphCommon< GT, Node, Arc >::reset_bit(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_, and Aleph::Spanning_Tree.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::compute_nodes_weights(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::detect_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_spanning_tree().
|
inlinenoexcept |
Check if a shortest-path tree has been painted.
Definition at line 527 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::painted_.
|
inlineprivate |
Perform one more relaxation pass and check for negative cycle.
Also updates predecessor array for cycle detection, and keeps the last node that it relaxes, on a negative cycle or reached from one.
Definition at line 659 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::accum(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::add_if_fits(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::arcs_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_finite(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::dist_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::idx(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::Inf, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relaxed_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relaxes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::sa_, and Aleph::sum().
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::compute_nodes_weights(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_spanning_tree().
|
inlineprivate |
Perform one more relaxation pass to detect (but not prepare) negative cycle.
Definition at line 690 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::accum(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::add_if_fits(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_finite(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::dist_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::Inf, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relaxes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::sa_, and Aleph::sum().
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::detect_negative_cycle().
|
inlineprivatenoexcept |
Free node cookies and set up predecessor pointers for path reconstruction.
Definition at line 714 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::arcs_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), NODE_COOKIE, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::parents_, and GraphCommon< GT, Node, Arc >::vsize().
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_spanning_tree().
|
inlinestaticprivatenoexcept |
Definition at line 216 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::mark(), and NODE_COOKIE.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::cycle_of_predecessors(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::mark().
|
inlineprivate |
The witness of a negative cycle that the last pass found.
Definition at line 805 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::cycle_of_predecessors(), Aleph::Path< GT >::is_empty(), and WARNING.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle().
|
inline |
Paint the shortest paths tree from a start node.
| [in] | start | source node from which the shortest paths will be computed. |
false is returned and the shortest paths tree is painted with the bit Spanning_Tree. Definition at line 829 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::abandon(), ah_domain_error_if, Aleph::and, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::beyond_max_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_beyond_max(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_painted_arcs(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::link_cookies_and_free(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_.
Referenced by example_basic_negative_weights(), example_negative_cycle(), example_spfa_comparison(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TYPED_TEST(), and TYPED_TEST().
|
inlineprivatenoexcept |
Paint the spanning tree nodes and arcs with the Spanning_Tree bit.
Definition at line 636 of file Bellman_Ford.H.
References ARC_BITS, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::arcs_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), NODE_BITS, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::painted_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_, Aleph::Spanning_Tree, and GraphCommon< GT, Node, Arc >::vsize().
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_spanning_tree().
|
inlineprivatenoexcept |
The source of the arc by which the last relaxation of p reached it, or nullptr.
Definition at line 748 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::arcs_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, GraphCommon< GT, Node, Arc >::get_src_node(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::idx().
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::cycle_of_predecessors().
|
inlinestaticprivate |
Insert a node into the queue if not already present (SPFA optimization).
Definition at line 589 of file Bellman_Ford.H.
References Aleph::Depth_First, IS_NODE_VISITED, NODE_BITS, and Aleph::DynListQueue< T >::put().
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::compute_nodes_weights(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_sources_in_queue(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs().
|
inlineprivate |
Put in q the start, or, without one, every node, as the virtual source reaches them.
Definition at line 737 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_in_queue(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::s_.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::compute_nodes_weights(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle().
|
inlineprivate |
Relax all arcs up to n-1 times (standard Bellman-Ford).
Stops as soon as a pass relaxes no arc: the distances have reached their fixed point and no later pass (including the extra negative-cycle check) can relax anything.
| overflow_error | if a sum leaves the range of an integer type. |
Definition at line 552 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::accum(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::add_if_fits(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::arcs_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::dist_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, GraphCommon< GT, Node, Arc >::get_src_node(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::idx(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::Inf, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relaxes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::sa_, Aleph::sum(), GraphCommon< GT, Node, Arc >::vsize(), and w.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::compute_nodes_weights(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::detect_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_spanning_tree().
|
inlineprivate |
Relax outgoing arcs from a source node (SPFA variant).
Inlined: it is the step of the loops of SPFA.
Definition at line 608 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::accum(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::add_if_fits(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::arcs_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::dist_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, GraphCommon< GT, Node, Arc >::get_src_node(), GraphCommon< GT, Node, Arc >::get_tgt_node(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::idx(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::Inf, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_in_queue(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relaxes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::sa_, Aleph::sum(), and w.
|
inlineprivate |
Whether sum relaxes the distance accum: it is below it, by more than the tolerance of the distance, if it gives one (see the class).
With an integer distance, accum - tolerance() is not computed below lowest(), where no sum is, and a node without a distance, at Inf, takes any sum below Inf, as without a tolerance
Definition at line 306 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::accum(), Aleph::and, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::dist_, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::Inf, and Aleph::sum().
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_test_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs().
|
inline |
See search_negative_cycle(Node * start).
Definition at line 1185 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle().
|
inline |
Searches a negative cycle using the faster version of Bellman-Ford algorithm and iteratively searching the cycle before finishing up.
This overloaded version searches for negatives cycles in all the graph: every node is a source, at distance 0, as if a virtual source reached each one by an arc of weight 0. The graph gets no node.
Definition at line 1177 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle().
|
inline |
Searches a negative cycle using the faster version of Bellman-Ford algorithm (SPFA variant).
| [in] | start | source node from which the shortest paths tree is built. |
Definition at line 1161 of file Bellman_Ford.H.
References ah_domain_error_if, and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle().
|
inline |
Searches a negative cycle using the faster version of Bellman-Ford algorithm and iteratively searching the cycle before finishing up.
Normally, the Bellman-Ford algorith certainly detects a negative cycle if during an additional arcs scanning an arc is relaxed. However, very frequently the cycle appears in graph used for representing the partial spanning tree.
This version could be seen as thus:
threshold = it_factor*|V|;
for (int i = 0; i < |V|; ++i)
{
for (e in Arcs) // for each arc e
relax e;
if (i >= threshold)
{
search a cycle in the graph representing the spanning tree threshold += step; if negative cycle is found return it; } }
So, from the threshold-th iteration, the algorithm tries to find a negative cycle on the hope that if this exists, then the algorithm will finish much before the normal completion.
| [in] | start | node from which the spanning tree is built. |
| [in] | it_factor | iterative factor since then the negative cycle is searched. |
| [in] | step | next step from which the next negative cycle will be done. |
get<0> value corresponds to the found negative cycle (if this one exists) and get<1> value is the external iteration when the cycle was found. The idea of this second field is to give feedback in order to eventually refine the it_factor value. Definition at line 1147 of file Bellman_Ford.H.
References ah_domain_error_if, and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle().
Referenced by Aleph::Bellman_Ford_Negative_Cycle< GT, Distance, Ait, NAit, SA >::operator()(), Aleph::Bellman_Ford_Negative_Cycle< GT, Distance, Ait, NAit, SA >::operator()(), Aleph::Bellman_Ford_Negative_Cycle< GT, Distance, Ait, NAit, SA >::operator()(), Aleph::Bellman_Ford_Negative_Cycle< GT, Distance, Ait, NAit, SA >::operator()(), TEST(), TEST(), TEST(), TEST(), TEST(), and TYPED_TEST().
|
inline |
Searches and returns a negative cycle (if it exists).
Every node is a source, at distance 0, as if a virtual source reached each one by an arc of weight 0; the graph gets no node.
Definition at line 1099 of file Bellman_Ford.H.
References Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle().
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::test_negative_cycle(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::test_negative_cycle().
|
inline |
Search a negative cycle on all possible paths starting from start node.
If a negative cycle is found, it is a cycle of the graph of the predecessors, given by its arcs. Otherwise (there is no negative cycle), the returned path is empty.
| [in] | start | starting node |
Definition at line 1084 of file Bellman_Ford.H.
References ah_domain_error_if, and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle().
Referenced by example_negative_cycle(), Aleph::Bellman_Ford_Negative_Cycle< GT, Distance, Ait, NAit, SA >::operator()(), Aleph::Bellman_Ford_Negative_Cycle< GT, Distance, Ait, NAit, SA >::operator()(), Aleph::Bellman_Ford_Negative_Cycle< GT, Distance, Ait, NAit, SA >::operator()(), Aleph::Bellman_Ford_Negative_Cycle< GT, Distance, Ait, NAit, SA >::operator()(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), and TYPED_TEST().
|
inline |
Test for negative cycle anywhere in the graph.
| [out] | cycle | Path where the cycle is stored if found. |
Definition at line 1340 of file Bellman_Ford.H.
References Aleph::Path< GT >::is_empty(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::test_negative_cycle().
|
inline |
Test for negative cycle and return it if found.
| [in] | s | The starting node. |
| [out] | cycle | Path where the cycle is stored if found. |
Definition at line 1329 of file Bellman_Ford.H.
References Aleph::Path< GT >::is_empty(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::test_negative_cycle().
|
inlinestaticprivate |
Definition at line 295 of file Bellman_Ford.H.
References ah_domain_error.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_finite().
|
inlineprivatenoexcept |
Release the memory associated with the node cookies, and the bits Depth_First that SPFA leaves on the nodes still in its queue.
Definition at line 395 of file Bellman_Ford.H.
References Aleph::Depth_First, Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::g_, NODE_BITS, and NODE_COOKIE.
|
private |
Definition at line 284 of file Bellman_Ford.H.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::abandon(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::clear(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::cycle_of_predecessors(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::extract_min_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::link_cookies_and_free(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::pred_node(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs().
|
private |
Definition at line 289 of file Bellman_Ford.H.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::add_if_fits(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::detect_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_spanning_tree().
|
private |
Definition at line 293 of file Bellman_Ford.H.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_distance(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_min_path(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_test_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relaxes().
|
private |
Definition at line 285 of file Bellman_Ford.H.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::build_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_beyond_max(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_painted_arcs(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::clear(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::compute_nodes_weights(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::cycle_of_predecessors(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_distance(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_graph(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_test_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::link_cookies_and_free(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::pred_node(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_sources_in_queue(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::uninit().
|
private |
Definition at line 286 of file Bellman_Ford.H.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_beyond_max(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_test_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relaxes().
|
private |
Definition at line 287 of file Bellman_Ford.H.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::abandon(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::build_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::clear(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::extract_min_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_distance(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_min_path(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::is_painted(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_tree().
|
private |
Definition at line 288 of file Bellman_Ford.H.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::abandon(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::build_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::clear(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_distance(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_min_path(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::link_cookies_and_free().
|
private |
Definition at line 291 of file Bellman_Ford.H.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::cycle_of_predecessors(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle().
|
private |
Definition at line 290 of file Bellman_Ford.H.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::abandon(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_painted_arcs(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::clear(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::detect_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::faster_paint_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::find_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_distance(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_min_path(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_start_node(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::has_computation(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::init_with_indexes(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_spanning_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::paint_tree(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::put_sources_in_queue().
|
private |
Definition at line 292 of file Bellman_Ford.H.
Referenced by Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::build_tree(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_beyond_max(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::check_painted_arcs(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::clear(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::get_distance(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_prepare_check_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::last_relax_and_test_negative_cycle(), Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs(), and Aleph::Bellman_Ford< GT, Distance, Ait, NAit, SA >::relax_arcs().