template <class VertexListGraph, class P, class T, class R> void dijkstra_shortest_paths(VertexListGraph& g, typename graph_traits<VertexListGraph>::vertex_descriptor s, const bgl_named_params<P, T, R>& params);
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.
There are two main options for obtaining output from the dijkstra_shortest_paths() function. If you provide a distance property map through the distance_map() parameter then the shortest distance from the source 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 there own custom-made visitor that can takes actions during any of the algorithm's event points.
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 algorithm uses color markers (white, gray, and black) to keep track of which set each vertex is in. Vertices colored black are in S. Vertices colored white or gray are in V-S. White vertices have not yet been discovered and gray vertices are in the priority queue. By default, the algorithm will allocate an array to store a color marker for each vertex in the graph. You can provide you own storage and access for colors with the color_map() parameter.
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(G, s, w) for each vertex u in V d[u] := infinity p[u] := u color[u] := WHITE end for color[s] := GRAY d[s] := 0 INSERT(Q, s) while (Q != Ø) u := EXTRACT-MIN(Q) S := S U { u } for each vertex v in Adj[u] if (w(u,v) + d[u] < d[v]) d[v] := w(u,v) + d[u] p[v] := u if (color[v] = WHITE) color[v] := GRAY INSERT(Q, v) else if (color[v] = GRAY) DECREASE-KEY(Q, v) else ... end for color[u] := BLACK end while return (d, p) |
initialize vertex u discover vertex s examine vertex u examine edge (u,v) edge (u,v) relaxed discover vertex v edge (u,v) not relaxed finish vertex u |
The graph object on which the algorithm will be applied. The type VertexListGraph must be a model of \concept{VertexListGraph}.IN: vertex_descriptor s
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 type WeightMap must 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 the map must be Addable with the value type of the distance map.IN: vertex_index_map(VertexIndexMap i_map)
Default: get(edge_weight, g)
This maps each vertex to an integer in the range [0, num_vertices(g)). This is necessary for efficient updates of the heap data structure when an edge is relaxed. The type VertexIndexMap must 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.OUT: predecessor_map(PredecessorMap p_map)
Default: get(vertex_index, g)
The predecessor map records the edges in the minimum spanning tree. Upon completion of the algorithm, the edges (p[u],u) for all u in V are in the minimum spanning tree. If p[u] = u then u is either the source vertex or a vertex that is not reachable from the source. The PredecessorMap type must be a Read/Write Property Map which key and vertex types the same as the vertex descriptor type of the graph.UTIL/OUT: distance_map(DistanceMap d_map)
Default: dummy_property_map
The shortest path weight from the source vertex s to each vertex in the graph g is recorded in this property map. The shortest path weight is the sum of the edge weights along the shortest path. The type DistanceMap must 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 must be Less Than Comparable.IN: distance_compare(CompareFunction cmp)
Default: iterator_property_map created from a std::vector of the WeightMap's value type of size num_vertices(g) and using the i_map for the index map.
This function is use to compare distances to determine which vertex is closer to the source vertex. The CompareFunction type must be a model of Binary Predicate and have argument types that match the value type of the DistanceMap property map.IN: distance_combine(CombineFunction cmb)
Default: std::less<D> with D=typename property_traits<DistanceMap>::value_type
This function is used to combine distances to compute the distance of a path. The CombineFunction type must be a model of Binary Function. The first argument type of the binary function must match the value type of the DistanceMap property map and the second argument type must match the value type of the WeightMap property map. The result type must be the same type as the distance value type.IN: distance_inf(InfinityGenerator inf)
Default: std::plus<D> with D=typename property_traits<DistanceMap>::value_type
This function is used to initialize the distance for each vertex before the start of the algorithm. The InfinityGenerator type must model the Generator concept. The result type of the generator must be convertible to the value type of the DistanceMap. The default infinity generator uses std::numeric_limits::max().IN: distance_zero(ZeroGenerator zero)
Default: detail::generate_infinity<D> with D=typename property_traits<DistanceMap>::value_type
This function is used to initialize the distance for the source vertex before the start of the algorithm. The ZeroGenerator type must model the Generatorconcept. The result type of the generator must be convertible to the value type of the DistanceMap.UTIL/OUT: color_map(ColorMap c_map)
Default: detail::generate_zero<D> with D=typename property_traits<DistanceMap>::value_type
This is used during the execution of the algorithm to mark the vertices. The vertices start out white and become gray when they are inserted in the queue. They then turn black when they are removed from the queue. At the end of the algorithm, vertices reachable from the source vertex will have been colored black. All other vertices will still be white. The type ColorMap must be a model of Read/Write Property Map. A vertex descriptor must be usable as the key type of the map, and the value type of the map must be a model of Color Value.OUT: visitor(Vis v)
Default: an iterator_property_map created from a std::vector of default_color_type of size num_vertices(g) and using the i_map for the index map.
Use this to specify actions that you would like to happen during certain event points within the algorithm. The type Vis must be a model of Dijkstra Visitor.
Default: dijkstra_visitor<null_visitor>
The time complexity is O((V + E) log V), or just O(E log V) if all vertices are reachable from the source.
See examples/dijkstra.cpp for an example of using Dijkstra's algorithm.
Copyright © 2000 | Jeremy Siek, Univ.of Notre Dame (jsiek@lsc.nd.edu) |