Dijkstra’s Shortest Paths (No Color Map)
Example
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths_no_color_map.hpp>
#include <iostream>
#include <vector>
struct EdgeProps { int weight; };
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::no_property, EdgeProps>;
using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
int main() {
Graph g{4};
boost::add_edge(0, 1, EdgeProps{1}, g);
boost::add_edge(1, 2, EdgeProps{2}, g);
boost::add_edge(0, 2, EdgeProps{10}, g);
boost::add_edge(2, 3, EdgeProps{1}, g);
std::vector<Vertex> pred(num_vertices(g));
std::vector<int> dist(num_vertices(g));
auto idx = get(boost::vertex_index, g);
auto wt = get(&EdgeProps::weight, g);
boost::dijkstra_shortest_paths_no_color_map(g, vertex(0, g),
boost::predecessor_map(boost::make_iterator_property_map(pred.begin(), idx))
.distance_map(boost::make_iterator_property_map(dist.begin(), idx))
.weight_map(wt));
for (auto v : boost::make_iterator_range(vertices(g)))
std::cout << "dist[" << v << "] = " << dist[v] << "\n";
}
dist[0] = 0
dist[1] = 1
dist[2] = 3
dist[3] = 4
// named parameter version
template <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 version
template <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 maps
template <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);
Description
This algorithm [7,5] 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. Note
that this means that edges with infinite weight will not work correctly
in this algorithm.
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.
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 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.
|
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 |
Parameters
IN |
|
The graph object on which the algorithm will be applied. The type |
IN |
|
The source vertex. All distance will be calculated from this vertex, and the shortest paths tree will be rooted at this vertex. |
IN |
|
The weight or "length" of each edge in the graph. The weights must all be non-negative and non-infinite. The algorithm will throw a |
IN |
|
This maps each vertex to an integer in the range |
OUT |
|
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 |
UTIL/OUT |
|
The shortest path weight from the source vertex |
IN |
|
This function is use to compare distances to determine which vertex is closer to the source vertex. The |
IN |
|
This function is used to combine distances to compute the distance of a path. The |
IN |
|
The |
IN |
|
The |
OUT |
|
Use this to specify actions that you would like to happen during certain event points within the algorithm. The type |
Visitor Event Points
-
vis.initialize_vertex(u, g)is invoked on each vertex in the graph before the start of the algorithm. -
vis.examine_vertex(u, g)is invoked on a vertex as it is removed from the priority queue and added to set 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[u1] ⇐ d[u2] ⇐ d[un]. -
vis.examine_edge(e, g)is invoked on each out-edge of a vertex immediately after it has been added to set S. -
vis.edge_relaxed(e, g)is invoked on edge (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. -
vis.discover_vertex(v, g)is invoked on vertex 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. -
vis.edge_not_relaxed(e, g)is invoked if the edge is not relaxed (see above). -
vis.finish_vertex(u, g)is invoked on a vertex after all of its out edges have been examined.