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

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

// named parameter versiontemplate <typename Graph, typename Param, typename Tag, typename Rest> void dijkstra_shortest_paths_no_color_map (const Graph& graph, typename graph_traits<Graph>::vertex_descriptor start_vertex, const bgl_named_params& params);// non-named parameter versiontemplate <typename Graph, typename DijkstraVisitor, typename PredecessorMap, typename DistanceMap, typename WeightMap, typename VertexIndexMap, typename DistanceCompare, typename DistanceWeightCombine, typename DistanceInfinity, typename DistanceZero> void dijkstra_shortest_paths_no_color_map (const Graph& graph, typename graph_traits<Graph>::vertex_descriptor start_vertex, PredecessorMap predecessor_map, DistanceMap distance_map, WeightMap weight_map, VertexIndexMap index_map, DistanceCompare distance_compare, DistanceWeightCombine distance_weight_combine, DistanceInfinity distance_infinity, DistanceZero distance_zero);// version that does not initialize the property mapstemplate <typename Graph, typename DijkstraVisitor, typename PredecessorMap, typename DistanceMap, typename WeightMap, typename VertexIndexMap, typename DistanceCompare, typename DistanceWeightCombine, typename DistanceInfinity, typename DistanceZero> void dijkstra_shortest_paths_no_color_map_no_init (const Graph& graph, typename graph_traits<Graph>::vertex_descriptor start_vertex, PredecessorMap predecessor_map, DistanceMap distance_map, WeightMap weight_map, VertexIndexMap index_map, DistanceCompare distance_compare, DistanceWeightCombine distance_weight_combine, DistanceInfinity distance_infinity, DistanceZero distance_zero);

This algorithm [10,8] solves the single-source shortest-paths problem on a weighted, directed or undirected graph for the case where all edge weights are nonnegative. Use the Bellman-Ford algorithm for the case when some edge weights are negative. Use breadth-first search instead of Dijkstra's algorithm when all edge weights are equal to one. For the definition of the shortest-path problem see Section Shortest-Paths Algorithms for some background to the shortest-path problem.

`dijkstra_shortest_paths_no_color_map` differs from the original `dijkstra_shortest_paths` algorithm by not using a color map to identify vertices as discovered or undiscovered. Instead, this is done with the distance map: a vertex *u* such that *distance_compare(distance_map[u], distance_infinity) == false* is considered to be undiscovered.

There are two main options for obtaining output from the
`dijkstra_shortest_paths_no_color_map()` function. If you provide a
distance property map through the `distance_map()` parameter
then the shortest distance from the start vertex to every other
vertex in the graph will be recorded in the distance map. Also you can
record the shortest paths tree in a predecessor map: for each vertex
*u in V*, *p[u]* will be the predecessor of *u* in
the shortest paths tree (unless *p[u] = u*, in which case *u* is
either the source or a vertex unreachable from the source). In
addition to these two options, the user can provide their own
custom-made visitor that takes actions during any of the
algorithm's event points [4].

Dijkstra's algorithm finds all the shortest paths from the source
vertex to every other vertex by iteratively "growing" the set of
vertices *S* to which it knows the shortest path. At each step of
the algorithm, the next vertex added to *S* is determined by a
priority queue. The queue contains the vertices in *V - S*[1] prioritized by their distance label, which is the
length of the shortest path seen so far for each vertex. The vertex
*u* at the top of the priority queue is then added to *S*,
and each of its out-edges is relaxed: if the distance to *u* plus
the weight of the out-edge *(u,v)* is less than the distance
label for *v* then the estimated distance for vertex *v* is
reduced. The algorithm then loops back, processing the next vertex at
the top of the priority queue. The algorithm finishes when the
priority queue is empty.

The following is the pseudo-code for Dijkstra's single-source shortest
paths algorithm. *w* is the edge weight, *d* is the distance label,
and *p* is the predecessor of each vertex which is used to encode
the shortest paths tree. *Q* is a priority queue that supports the
DECREASE-KEY operation. The visitor event points for the algorithm are
indicated by the labels on the right.

DIJKSTRA( |
initialize vertex |

The graph object on which the algorithm will be applied. The typeIN:Graphmust be a model of Vertex List Graph and Incidence Graph.

The source vertex. All distance will be calculated from this vertex, and the shortest paths tree will be rooted at this vertex.

The weight or ``length'' of each edge in the graph. The weights must all be non-negative and non-infinite [3]. The algorithm will throw aIN:negative_edgeexception is one of the edges is negative. The typeWeightMapmust be a model of Readable Property Map. The edge descriptor type of the graph needs to be usable as the key type for the weight map. The value type for this map must be the same as the value type of the distance map.

Default:get(edge_weight, graph)

This maps each vertex to an integer in the rangeOUT:[0, num_vertices(graph)). This is necessary for efficient updates of the heap data structure [61] when an edge is relaxed. The typeVertexIndexMapmust be a model of Readable Property Map. The value type of the map must be an integer type. The vertex descriptor type of the graph needs to be usable as the key type of the map.

Default:get(vertex_index, graph). Note: if you use this default, make sure your graph has an internalvertex_indexproperty. For example,adjacenty_listwithVertexList=listSdoes not have an internalvertex_indexproperty.

The predecessor map records the edges in the minimum spanning tree. Upon completion of the algorithm, the edgesUTIL/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 whose key and value types are 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 vertexIN:start_vertexto each vertex in the graphgraphis recorded in this property map. The shortest path weight is the sum of the edge weights along the shortest path. The typeDistanceMapmust be a model of Read/Write Property Map. The vertex descriptor type of the graph needs to be usable as the key type of the distance map. The value type of the distance map is the element type of a Monoid formed with thedistance_weight_combinefunction object and thedistance_zeroobject for the identity element. Also the distance value type must have a StrictWeakOrdering provided by thedistance_comparefunction object.

Default:iterator_property_mapcreated from astd::vectorof theWeightMap's value type of sizenum_vertices(graph)and using theindex_mapfor the index map.

This function is use to compare distances to determine which vertex is closer to the source vertex. TheIN:DistanceCompareFunctiontype must be a model of Binary Predicate and have argument types that match the value type of theDistanceMapproperty map.

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

This function is used to combine distances to compute the distance of a path. TheIN:DistanceWeightCombineFunctiontype must be a model of Binary Function. The first argument type of the binary function must match the value type of theDistanceMapproperty map and the second argument type must match the value type of theWeightMapproperty map. The result type must be the same type as the distance value type.

Default:boost::closed_plus<D>withD=typename property_traits<DistanceMap>::value_type

TheIN:distance_infinityobject must be the greatest value of anyDobject. That is,distance_compare(d, distance_infinity) == truefor anyd != distance_infinity. The typeDis the value type of theDistanceMap.

Default:std::numeric_limits<D>::max()

TheOUT:distance_zerovalue must be the identity element for the Monoid formed by the distance values and thedistance_weight_combinefunction object. The typeDis the value type of theDistanceMap.

Default:D()withD=typename property_traits<DistanceMap>::value_type

Use this to specify actions that you would like to happen during certain event points within the algorithm. The typeDijkstraVisitormust be a model of the Dijkstra Visitor concept. The visitor object is passed by value [2].

Default:dijkstra_visitor<null_visitor>

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

is invoked on each vertex in the graph before the start of the algorithm.`vis.initialize_vertex(u, g)`is invoked on a vertex as it is removed from the priority queue and added to set`vis.examine_vertex(u, g)`*S*. At this point we know that*(p[u],u)*is a shortest-paths tree edge so*d[u] = delta(s,u) = d[p[u]] + w(p[u],u)*. Also, the distances of the examined vertices is monotonically increasing*d[u*._{1}] <= d[u_{2}] <= d[u_{n}]is invoked on each out-edge of a vertex immediately after it has been added to set`vis.examine_edge(e, g)`*S*.is invoked on edge`vis.edge_relaxed(e, g)`*(u,v)*if*d[u] + w(u,v) < d[v]*. The edge*(u,v)*that participated in the last relaxation for vertex*v*is an edge in the shortest paths tree.is invoked on vertex`vis.discover_vertex(v, g)`*v*when the edge*(u,v)*is examined and*v*has not yet been discovered (i.e. its distance was infinity before relaxation was attempted on the edge). This is also when the vertex is inserted into the priority queue.is invoked if the edge is not relaxed (see above).`vis.edge_not_relaxed(e, g)`is invoked on a vertex after all of its out edges have been examined.`vis.finish_vertex(u, g)`

See
`example/dijkstra-no-color-map-example.cpp` for an example of using Dijkstra's algorithm.

Based on the documentation for dijkstra_shortest_paths.

[1]
The algorithm used here saves a little space by not putting all *V -
S* vertices in the priority queue at once, but instead only those
vertices in *V - S* that are discovered and therefore have a
distance less than infinity.

[2] 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.

[3] The algorithm will not work correctly if any of the edge weights are equal to infinity since the infinite distance value is used to determine if a vertex has been discovered.

[4]
Calls to the visitor events occur in the same order as `dijkstra_shortest_paths` (i.e. *discover_vertex(u)* will always be called after *examine_vertex(u)* for an undiscovered vertex *u*). However, the vertices of the graph given to *dijkstra_shortest_paths_no_color_map* will **not** necessarily be visited in the same order as *dijkstra_shortest_paths*.

Copyright © 2009 | Trustees of Indiana University |