# `dijkstra_shortest_paths`

Graphs: | directed and undirected
Properties: | color, distance, weight, vertex index
Complexity: | O((V + E) log V)
Where Defined: | `boost/graph/dijkstra_shortest_paths.hpp`

    (1)
    template <class VertexListGraph>
    void
    dijkstra_shortest_paths(VertexListGraph& g,
      typename graph_traits<VertexListGraph>::vertex_descriptor s);

    (2)
    template <class VertexListGraph, class DistanceMap>
    void
    dijkstra_shortest_paths(VertexListGraph& g,
      typename graph_traits<VertexListGraph>::vertex_descriptor s,
      DistanceMap d);

    (3)
    template <class VertexListGraph, class DistanceMap, class UniformCostVisitor>
    void
    dijkstra_shortest_paths(VertexListGraph& g,
      typename graph_traits<VertexListGraph>::vertex_descriptor s,
      DistanceMap d, UniformCostVisitor visit);

    (4)
    template <class VertexListGraph, class UniformCostVisitor,
              class DistanceMap, class WeightMap, class ColorMap, class VertexIndexMap>
    void
    dijkstra_shortest_paths(VertexListGraph& g,
      typename graph_traits<VertexListGraph>::vertex_descriptor s,
      DistanceMap distance, WeightMap weight, ColorMap color, VertexIndexMap id,
      UniformCostVisitor vis);

This is the modified Dijkstra algorithm [10,8] which solves the single-source shortest-paths problem on a weighted, directed graph for the case where all edge weights are nonnegative. See Section Shortest-Paths Algorithms for some background to the shortest-path problem. The priority queue used inside the algorithm is implemented with a heap for efficiency.

There are four versions of the algorithm to accommodate whether the necessary graph properties will be supplied by the graph object or externally via a argument to this function. The properties needed by the algorithm are distance, weight, color, and vertex index. Version 3 and 4 of the algorithm also include a visitor argument for added extensibility.

### Requirements on Types

- The type `VertexListGraph` must be a model of VertexListGraph.
- In version (1) of the algorithm, the graph must be a PropertyGraph with respect to `vertex_color_t`, `vertex_distance_t`, `edge_weight_t`, and `vertex_index_t`
- In version (2) of the algorithm, the graph must be a PropertyGraph with respect to `vertex_color_t`, `edge_weight_t`, and `vertex_index_t`
- In version (3) of the algorithm, the graph must be a PropertyGraph with respect to `vertex_color_t`, `edge_weight_t`, and `vertex_index_t`
- The type `UniformCostVisitor` must be a model of UniformCostVisitor.
- The type `DistanceMap` must be a model of ReadWritePropertyMap. 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 LessThanComparable.
- The type `WeightMap` must be a model of ReadablePropertyMap. 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.
- The type `ColorMap` must be a model of ReadWritePropertyMap. 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 ColorValue.
- The type `VertexIndexMap` must be a model of ReadablePropertyMap. The value type of `VertexIndexMap` must be an integer type. The integers must map vertex descriptors to the integers zero through `num_vertices(g) - 1`. The vertex descriptor type of the graph needs to be usable as the key type of the vertex index map.

### Complexity

The time complexity is O((V + E) log V), or just O(E log V) if all vertices are reachable from the source.

### Example

The source code for this example is in `examples/dijkstra.cpp`.

    int
    main(int , char* [])
    {
      using namespace boost;

      typedef property<edge_weight_t, int> weightp;
      typedef adjacency_list< listS, vecS, directedS,
        property<vertex_color_t,default_color_type>, weightp > Graph;
      typedef graph_traits<Graph>::vertex_descriptor Vertex;

      typedef std::pair<int,int> E;

      const int num_nodes = 5;
      E edges[] = { E(0,2),
                    E(1,1), E(1,3), E(1,4),
                    E(2,1), E(2,3),
                    E(3,4),
                    E(4,0), E(4,1) };
      int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1};

      Graph G(num_nodes, edges, edges + sizeof(edges)/sizeof(E), weights);

      std::vector<Vertex> p(num_vertices(G));
      std::vector<int> d(num_vertices(G));

      Vertex s = *(vertices(G).first);
      p[s] = s;
      dijkstra_shortest_paths(G, s, &d[0],
        make_ucs_visitor(record_predecessors(&p[0], on_edge_relaxed())));

      std::cout << "distances from start vertex:" << std::endl;
      graph_traits<Graph>::vertex_iterator vi, vend;
      for(tie(vi,vend) = vertices(G); vi != vend; ++vi)
        std::cout << "distance(" << *vi << ") = " << d[*vi] << std::endl;
      std::cout << std::endl;

      std::cout << "shortest paths tree" << std::endl;
      adjacency_list<> tree(num_nodes);

      for(tie(vi,vend) = vertices(G); vi != vend; ++vi)
        if (*vi != p[*vi])
          add_edge(p[*vi], *vi, tree);

      print_graph(tree);

      return 0;
    }

The output is:

      distances from start vertex:
      distance(0) = 0
      distance(1) = 6
      distance(2) = 1
      distance(3) = 4
      distance(4) = 5

      shortest paths tree
      0 --> 2
      1 -->
      2 --> 3
      3 --> 4
      4 --> 1

Copyright © 2000 | Jeremy Siek, Univ.of Notre Dame (jsiek@lsc.nd.edu)