C++ Boost

uniform_cost_search

Graphs: directed
Properties: color, distance, weight, vertex index
Complexity: time: O((V + E) log V)
Where Defined: boost/graph/uniform_cost_search.hpp

  (1)
  template <class VertexListGraph, class BinaryPredicate, 
            class BinaryFunction>
  void
  uniform_cost_search(VertexListGraph& g, 
     typename graph_traits<VertexListGraph>::vertex_descriptor s, 
     BinaryPredicate compare, BinaryFunction combine);

  (2)
  template <class VertexListGraph, class DistanceMap, class WeightMap,
            class BinaryPredicate, class BinaryFunction>
  void
  uniform_cost_search(VertexListGraph& g,
    typename graph_traits<VertexListGraph>::vertex_descriptor s, 
    DistanceMap distance, WeightMap weight,
    BinaryPredicate compare, BinaryFunction combine);

  (3)
  template <class VertexListGraph,
            class DistanceMap, class WeightMap, class ColorMap, class VertexIndexMap,
            class BinaryPredicate, class BinaryFunction,
            class UniformCostVisitor>
  void
  uniform_cost_search(VertexListGraph& g,
    typename graph_traits<VertexListGraph>::vertex_descriptor  s,
    DistanceMap distance, WeightMap weight, ColorMap color, VertexIndexMap id, 
    BinaryPredicate compare, BinaryFunction combine,
    UniformCostVisitor vis);

This function solves a generalized version of the single-source shortest paths problem. In particular, the algorithm used is a generalized version of Dijkstra's single-source shortest paths algorithm. In Dijkstra's algorithm the ``cost'' is the edge weight. With uniform_cost_search() the user supplies the meaning of ``cost'' through the compare and combine function objects. For Dijkstra's algorithm compare is std::less<D> and combine is std::plus<W>.

If you want to solve the single-source shortest paths problem for integer edge weights, where all edge weights are equal to 1, then use breadth_first_search() instead (it is faster) with a distance_recorder.

uniform_cost_search() does not initialize the distance properties of the graph, so they must be pre-initialized to a maximum value (with respect to the predicate). Typically std::numeric_limits<D>::max() is a good value. Also, the source vertex s must be initialized to have zero distance. The following code is an example of how one might initialize the distance properties prior to calling uniform_cost_search() .

  typedef typename property_traits<DistanceMap>::value_type D;
  typename boost::graph_traits<Graph>::vertex_iterator ui, ui_end;
  for (tie(ui, ui_end) = vertices(G); ui != ui_end; ++ui)
    put(distance, *ui, std::numeric_limits<D>::max());

  put(distance, s, D());

The ColorMap is used to keep track of which vertices have been visited. Traditionally, implementation of Dijkstra's algorithm do not use a color map and instead use the distance map for this purpose. When generalizing to a uniform-cost search, it is better to separate out the responsibilities.

The implementation of uniform_cost_search() consists mainly of a call to version (3) of breadth_first_search().

Requirements on Types

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

See the Parallel Compilation section of the File Dependency Example.



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