DAG Shortest Paths
Solves the single-source shortest-paths problem on a weighted, directed acyclic graph (DAG), more efficiently than either Dijkstra or Bellman-Ford.
Complexity: O(V + E)
Defined in: <boost/graph/dag_shortest_paths.hpp>
Example
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dag_shortest_paths.hpp>
#include <iostream>
#include <limits>
#include <vector>
struct Node {};
struct Edge { int weight; };
using namespace boost;
using Graph = adjacency_list<vecS, vecS, directedS, Node, Edge>;
using Vertex = graph_traits<Graph>::vertex_descriptor;
int main() {
Graph g{5};
add_edge(0, 1, Edge{2}, g);
add_edge(0, 2, Edge{6}, g);
add_edge(1, 3, Edge{5}, g);
add_edge(2, 3, Edge{1}, g);
add_edge(3, 4, Edge{3}, g);
std::vector<int> dist(num_vertices(g));
std::vector<Vertex> pred(num_vertices(g));
std::vector<default_color_type> color(num_vertices(g));
auto idx = get(vertex_index, g);
dag_shortest_paths(g, vertex(0, g),
make_iterator_property_map(dist.begin(), idx),
get(&Edge::weight, g),
make_iterator_property_map(color.begin(), idx),
make_iterator_property_map(pred.begin(), idx),
dijkstra_visitor<null_visitor>(),
std::less<int>(), std::plus<int>(),
(std::numeric_limits<int>::max)(), 0);
for (auto v : make_iterator_range(vertices(g)))
std::cout << "distance to " << v << " = " << dist[v] << "\n";
}
distance to 0 = 0
distance to 1 = 2
distance to 2 = 6
distance to 3 = 7
distance to 4 = 10
(1) Positional version
template <class VertexListGraph, class DijkstraVisitor,
class DistanceMap, class WeightMap, class ColorMap,
class PredecessorMap,
class Compare, class Combine,
class DistInf, class DistZero>
void dag_shortest_paths(
const VertexListGraph& g,
typename graph_traits<VertexListGraph>::vertex_descriptor s,
DistanceMap distance, WeightMap weight, ColorMap color,
PredecessorMap pred, DijkstraVisitor vis,
Compare compare, Combine combine, DistInf inf, DistZero zero);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
The graph object on which the algorithm will be applied.
The type |
IN |
|
The source vertex. All distances will be calculated from this vertex, and the shortest paths tree will be rooted at this vertex. |
UTIL/OUT |
|
The shortest path weight from the source vertex |
IN |
|
The weight or "length" of each edge in the graph. The type |
UTIL/OUT |
|
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 |
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
|
IN |
|
Use this to specify actions that you would like to happen during certain
event points within the algorithm. The type |
IN |
|
This function is used 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 |
(2) Named parameter version
template <class VertexListGraph, class Param, class Tag, class Rest>
void dag_shortest_paths(
const VertexListGraph& g,
typename graph_traits<VertexListGraph>::vertex_descriptor s,
const bgl_named_params<Param, Tag, Rest>& params);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
The graph object on which the algorithm will be applied.
The type |
IN |
|
The source vertex. All distances will be calculated from this vertex, and the shortest paths tree will be rooted at this vertex. |
IN |
|
Named parameters passed via |
| Direction | Named Parameter | Description / Default |
|---|---|---|
IN |
|
Edge weights (see (1) |
IN |
|
This maps each vertex to an integer in the range |
OUT |
|
Predecessor map (see (1) |
UTIL/OUT |
|
Distance map (see (1) |
IN |
|
Distance comparison function (see (1) |
IN |
|
Distance combination function (see (1) |
IN |
|
Infinity value (see (1) |
IN |
|
Identity element (see (1) |
UTIL/OUT |
|
Color map (see (1) |
OUT |
|
Visitor (see (1) |
Description
This algorithm [5] solves the single-source shortest-paths problem on a weighted, directed acyclic graph (DAG). This algorithm is more efficient for DAG’s than either the Dijkstra or Bellman-Ford algorithm. Use breadth-first search instead of this 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
dag_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 their own custom-made visitor that takes actions
during any of the algorithm’s event points.
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 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 is WHITE. Since a vertex is colored GRAY when it is discovered, each reachable vertex is discovered exactly once. -
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.
Example
See example/dag_shortest_paths.cpp
for an example of using this algorithm.