This class provides an efficient implementation of the Bellman-Ford algorithm. The maximum time complexity of the algorithm is O(nm)
.
The Bellman-Ford algorithm solves the single-source shortest path problem when the arcs can have negative lengths, but the digraph should not contain directed cycles with negative total length. If all arc costs are non-negative, consider to use the Dijkstra algorithm instead, since it is more efficient.
The arc lengths are passed to the algorithm using a ReadMap, so it is easy to change it to any kind of length. The type of the length values is determined by the Value type of the length map.
There is also a function-type interface for the Bellman-Ford algorithm, which is convenient in the simplier cases and it can be used easier.
GR | The type of the digraph the algorithm runs on. The default type is ListDigraph. |
LEN | A readable arc map that specifies the lengths of the arcs. The default map type is GR::ArcMap<int>. |
TR | The traits class that defines various types used by the algorithm. By default, it is BellmanFordDefaultTraits<GR, LEN>. In most cases, this parameter should not be set directly, consider to use the named template parameters instead. |
#include <lemon/bellman_ford.h>
Classes | |
class | ActiveIt |
LEMON iterator for getting the active nodes. More... | |
struct | SetDistMap |
Named parameter for setting DistMap type. More... | |
struct | SetOperationTraits |
Named parameter for setting OperationTraits type. More... | |
struct | SetPredMap |
Named parameter for setting PredMap type. More... | |
Public Types | |
typedef TR::Digraph | Digraph |
The type of the underlying digraph. | |
typedef TR::LengthMap::Value | Value |
The type of the arc lengths. | |
typedef TR::LengthMap | LengthMap |
The type of the map that stores the arc lengths. | |
typedef TR::PredMap | PredMap |
The type of the map that stores the last arcs of the shortest paths. | |
typedef TR::DistMap | DistMap |
The type of the map that stores the distances of the nodes. | |
typedef PredMapPath< Digraph, PredMap > | Path |
The type of the paths. | |
typedef TR::OperationTraits | OperationTraits |
The operation traits class of the algorithm. | |
typedef TR | Traits |
The traits class of the algorithm. | |
Public Member Functions | |
BellmanFord (const Digraph &g, const LengthMap &length) | |
Constructor. More... | |
~BellmanFord () | |
Destructor. | |
BellmanFord & | lengthMap (const LengthMap &map) |
Sets the length map. More... | |
BellmanFord & | predMap (PredMap &map) |
Sets the map that stores the predecessor arcs. More... | |
BellmanFord & | distMap (DistMap &map) |
Sets the map that stores the distances of the nodes. More... | |
Execution Control | |
The simplest way to execute the Bellman-Ford algorithm is to use one of the member functions called run(). | |
void | init (const Value value=OperationTraits::infinity()) |
Initializes the internal data structures. More... | |
void | addSource (Node source, Value dst=OperationTraits::zero()) |
Adds a new source node. More... | |
bool | processNextRound () |
Executes one round from the Bellman-Ford algorithm. More... | |
bool | processNextWeakRound () |
Executes one weak round from the Bellman-Ford algorithm. More... | |
void | start () |
Executes the algorithm. More... | |
bool | checkedStart () |
Executes the algorithm and checks the negative cycles. More... | |
void | limitedStart (int num) |
Executes the algorithm with arc number limit. More... | |
void | run (Node s) |
Runs the algorithm from the given root node. More... | |
void | run (Node s, int num) |
Runs the algorithm from the given root node with arc number limit. More... | |
Query Functions | |
Path | path (Node t) const |
The shortest path to the given node. More... | |
Value | dist (Node v) const |
The distance of the given node from the root(s). More... | |
Arc | predArc (Node v) const |
Returns the 'previous arc' of the shortest path tree for the given node. More... | |
Node | predNode (Node v) const |
Returns the 'previous node' of the shortest path tree for the given node. More... | |
const DistMap & | distMap () const |
Returns a const reference to the node map that stores the distances of the nodes. More... | |
const PredMap & | predMap () const |
Returns a const reference to the node map that stores the predecessor arcs. More... | |
bool | reached (Node v) const |
Checks if a node is reached from the root(s). More... | |
lemon::Path< Digraph > | negativeCycle () const |
Gives back a negative cycle. More... | |
|
inline |
Constructor.
g | The digraph the algorithm runs on. |
length | The length map used by the algorithm. |
|
inline |
Sets the length map.
(*this)
|
inline |
|
inline |
|
inline |
Initializes the internal data structures. The optional parameter is the initial distance of each node.
|
inline |
This function adds a new source node. The optional second parameter is the initial distance of the node.
|
inline |
If the algoritm calculated the distances in the previous round exactly for the paths of at most k
arcs, then this function will calculate the distances exactly for the paths of at most k+1
arcs. Performing k
iterations using this function calculates the shortest path distances exactly for the paths consisting of at most k
arcs.
true
when the algorithm have not found more shorter paths.
|
inline |
If the algorithm calculated the distances in the previous round at least for the paths of at most k
arcs, then this function will calculate the distances at least for the paths of at most k+1
arcs. This function does not make it possible to calculate the shortest path distances exactly for paths consisting of at most k
arcs, this is why it is called weak round.
true
when the algorithm have not found more shorter paths.
|
inline |
Executes the algorithm.
This method runs the Bellman-Ford algorithm from the root node(s) in order to compute the shortest path to each node.
The algorithm computes
|
inline |
Executes the algorithm and checks the negative cycles.
This method runs the Bellman-Ford algorithm from the root node(s) in order to compute the shortest path to each node and also checks if the digraph contains cycles with negative total length.
The algorithm computes
false
if there is a negative cycle in the digraph.
|
inline |
Executes the algorithm with arc number limit.
This method runs the Bellman-Ford algorithm from the root node(s) in order to compute the shortest path distance for each node using only the paths consisting of at most num
arcs.
The algorithm computes
|
inline |
This method runs the Bellman-Ford algorithm from the given root node s
in order to compute the shortest path to each node.
The algorithm computes
|
inline |
This method runs the Bellman-Ford algorithm from the given root node s
in order to compute the shortest path distance for each node using only the paths consisting of at most num
arcs.
The algorithm computes
|
inline |
|
inline |
|
inline |
This function returns the 'previous arc' of the shortest path tree for node v
, i.e. it returns the last arc of a shortest path from a root to v
. It is INVALID
if v
is not reached from the root(s) or if v
is a root.
The shortest path tree used here is equal to the shortest path tree used in predNode() and predMap().
|
inline |
This function returns the 'previous node' of the shortest path tree for node v
, i.e. it returns the last but one node of a shortest path from a root to v
. It is INVALID
if v
is not reached from the root(s) or if v
is a root.
The shortest path tree used here is equal to the shortest path tree used in predArc() and predMap().
|
inline |
|
inline |
|
inline |
|
inline |
This function gives back a directed cycle with negative total length if the algorithm has already found one. Otherwise it gives back an empty path.