This class provides an efficient implementation of the Bellman-Ford algorithm. The maximum time complexity of the algorithm is O(ne)
.
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. | |
~BellmanFord () | |
Destructor. | |
BellmanFord & | lengthMap (const LengthMap &map) |
Sets the length map. | |
BellmanFord & | predMap (PredMap &map) |
Sets the map that stores the predecessor arcs. | |
BellmanFord & | distMap (DistMap &map) |
Sets the map that stores the distances of the nodes. | |
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. | |
void | addSource (Node source, Value dst=OperationTraits::zero()) |
Adds a new source node. | |
bool | processNextRound () |
Executes one round from the Bellman-Ford algorithm. | |
bool | processNextWeakRound () |
Executes one weak round from the Bellman-Ford algorithm. | |
void | start () |
Executes the algorithm. | |
bool | checkedStart () |
Executes the algorithm and checks the negative cycles. | |
void | limitedStart (int num) |
Executes the algorithm with arc number limit. | |
void | run (Node s) |
Runs the algorithm from the given root node. | |
void | run (Node s, int num) |
Runs the algorithm from the given root node with arc number limit. | |
Query Functions | |
Path | path (Node t) const |
The shortest path to the given node. | |
Value | dist (Node v) const |
The distance of the given node from the root(s). | |
Arc | predArc (Node v) const |
Returns the 'previous arc' of the shortest path tree for the given node. | |
Node | predNode (Node v) const |
Returns the 'previous node' of the shortest path tree for the given node. | |
const DistMap & | distMap () const |
Returns a const reference to the node map that stores the distances of the nodes. | |
const PredMap & | predMap () const |
Returns a const reference to the node map that stores the predecessor arcs. | |
bool | reached (Node v) const |
Checks if a node is reached from the root(s). | |
lemon::Path< Digraph > | negativeCycle () const |
Gives back a negative cycle. |
BellmanFord | ( | const Digraph & | g, |
const LengthMap & | length | ||
) | [inline] |
Constructor.
g | The digraph the algorithm runs on. |
length | The length map used by the algorithm. |
BellmanFord& lengthMap | ( | const LengthMap & | map | ) | [inline] |
Sets the length map.
(*this)
BellmanFord& predMap | ( | PredMap & | map | ) | [inline] |
BellmanFord& distMap | ( | DistMap & | map | ) | [inline] |
void init | ( | const Value | value = OperationTraits::infinity() | ) | [inline] |
Initializes the internal data structures. The optional parameter is the initial distance of each node.
void addSource | ( | Node | source, |
Value | dst = OperationTraits::zero() |
||
) | [inline] |
This function adds a new source node. The optional second parameter is the initial distance of the node.
bool processNextRound | ( | ) | [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.bool processNextWeakRound | ( | ) | [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.void start | ( | ) | [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
bool checkedStart | ( | ) | [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.void limitedStart | ( | int | num | ) | [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
void run | ( | Node | s | ) | [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
bf.init(); bf.addSource(s); bf.start();
void run | ( | Node | s, |
int | num | ||
) | [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
bf.init(); bf.addSource(s); bf.limitedStart(num);
Path path | ( | Node | t | ) | const [inline] |
Value dist | ( | Node | v | ) | const [inline] |
Arc predArc | ( | Node | v | ) | const [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().
Node predNode | ( | Node | v | ) | const [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().
const DistMap& distMap | ( | ) | const [inline] |
const PredMap& predMap | ( | ) | const [inline] |
bool reached | ( | Node | v | ) | const [inline] |
lemon::Path<Digraph> negativeCycle | ( | ) | const [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.