...one of the most highly
regarded and expertly designed C++ library projects in the
world.

— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards

// named paramter versiontemplate <class EdgeListGraph, class Size, class P, class T, class R> bool bellman_ford_shortest_paths(const EdgeListGraph& g, Size N, const bgl_named_params<P, T, R>& params =all defaults); template <class VertexAndEdgeListGraph, class P, class T, class R> bool bellman_ford_shortest_paths(const VertexAndEdgeListGraph& g, const bgl_named_params<P, T, R>& params =all defaults);// non-named parameter versiontemplate <class EdgeListGraph, class Size, class WeightMap, class PredecessorMap, class DistanceMap, class BinaryFunction, class BinaryPredicate, class BellmanFordVisitor> bool bellman_ford_shortest_paths(EdgeListGraph& g, Size N, WeightMap weight, PredecessorMap pred, DistanceMap distance, BinaryFunction combine, BinaryPredicate compare, BellmanFordVisitor v)

The Bellman-Ford algorithm [4,11,20,8] solves the single-source shortest paths problem for a graph with both positive and negative edge weights. For the definition of the shortest paths problem see Section Shortest-Paths Algorithms. If you only need to solve the shortest paths problem for positive edge weights, Dijkstra's algorithm provides a more efficient alternative. If all the edge weights are all equal to one then breadth-first search provides an even more efficient alternative.

Before calling the `bellman_ford_shortest_paths()` function,
the user must assign the source vertex a distance of zero and all
other vertices a distance of infinity *unless* you are providing
a starting vertex. The Bellman-Ford algorithm
proceeds by looping through all of the edges in the graph, applying
the relaxation operation to each edge. In the following pseudo-code,
*v* is a vertex adjacent to *u*, *w* maps edges to
their weight, and *d* is a distance map that records the length
of the shortest path to each vertex seen so far. *p* is a
predecessor map which records the parent of each vertex, which will
ultimately be the parent in the shortest paths tree

RELAX( |
relax edge |

The algorithm repeats this loop *|V|* times after which it is
guaranteed that the distances to each vertex have been reduced to the
minimum possible unless there is a negative cycle in the graph. If
there is a negative cycle, then there will be edges in the graph that
were not properly minimized. That is, there will be edges *(u,v)* such
that *w(u,v) + d[u] < d[v]*. The algorithm loops over the edges in
the graph one final time to check if all the edges were minimized,
returning `true` if they were and returning `false`
otherwise.

BELLMAN-FORD( |
examine edge |

A directed or undirected graph whose type must be a model of Edge List Graph. If a root vertex is provided, then the graph must also model Vertex List Graph.IN:

Python: The parameter is namedgraph.

The number of vertices in the graph. The typeSizemust be an integer type.

Default:num_vertices(g).

Python: Unsupported parameter.

The weight (also know as ``length'' or ``cost'') of each edge in the graph. TheOUT:WeightMaptype must be a model of Readable Property Map. The key type for this property map must be the edge descriptor of the graph. The value type for the weight map must beAddablewith the distance map's value type.

Default:get(edge_weight, g)

Python: Must be anedge_double_mapfor the graph.

Python default:graph.get_edge_double_map("weight")

The predecessor map records the edges in the minimum spanning tree. Upon completion of the algorithm, the edgesIN/OUT:(p[u],u)for allu in Vare in the minimum spanning tree. Ifp[u] = uthenuis either the source vertex or a vertex that is not reachable from the source. ThePredecessorMaptype must be a Read/Write Property Map which key and vertex types the same as the vertex descriptor type of the graph.

Default:dummy_property_map

Python: Must be avertex_vertex_mapfor the graph.

The shortest path weight from the source vertex to each vertex in the graphIN:gis recorded in this property map. The typeDistanceMapmust be a model of Read/Write Property Map. The key type of the property map must be the vertex descriptor type of the graph, and the value type of the distance map must be Less Than Comparable.

Default:get(vertex_distance, g)

Python: Must be avertex_double_mapfor the graph.

The starting (or "root") vertex from which shortest paths will be computed. When provided, the distance map need not be initialized (the algorithm will perform the initialization itself). However, the graph must model Vertex List Graph when this parameter is provided.IN:

Default:None; if omitted, the user must initialize the distance map.

The visitor object, whose type must be a model of Bellman-Ford Visitor. The visitor object is passed by value [1].IN:

Default:bellman_visitor<null_visitor>

Python: The parameter should be an object that derives from theBellmanFordVisitortype of the graph.

This function object replaces the role of addition in the relaxation step. The first argument type must match the distance map's value type and the second argument type must match the weight map's value type. The result type must be the same as the distance map's value type.IN:

Default:std::plus<D>withD=typename property_traits<DistanceMap>::value_type.

Python: Unsupported parameter.

This function object replaces the role of the less-than operator that compares distances in the relaxation step. The argument types must match the distance map's value type.

Default:std::less<D>withD=typename property_traits<DistanceMap>::value_type.

Python: Unsupported parameter.

The time complexity is *O(V E)*.

is invoked on every edge in the graph`vis.examine_edge(e, g)`*|V|*times.is invoked when the distance label for the target vertex is decreased. The edge`vis.edge_relaxed(e, g)`*(u,v)*that participated in the last relaxation for vertex*v*is an edge in the shortest paths tree.is invoked if the distance label for the target vertex is not decreased.`vis.edge_not_relaxed(e, g)`is invoked during the second stage of the algorithm, during the test of whether each edge was minimized. If the edge is minimized then this function is invoked.`vis.edge_minimized(e, g)`is also invoked during the second stage of the algorithm, during the test of whether each edge was minimized. If the edge was not minimized, this function is invoked. This happens when there is a negative cycle in the graph.`vis.edge_not_minimized(e, g)`

An example of using the Bellman-Ford algorithm is in `examples/bellman-example.cpp`.

[1]
Since the visitor parameter is passed by value, if your visitor
contains state then any changes to the state during the algorithm
will be made to a copy of the visitor object, not the visitor object
passed in. Therefore you may want the visitor to hold this state by
pointer or reference.

Copyright © 2000 | Jeremy Siek, Indiana University (jsiek@osl.iu.edu) |