# `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 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>`.

`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

- The graph type must be a model of VertexListGraph.

- The `BinaryPredicate` type must be a model of BinaryPredicate and have argument types that match the value type of the `DistanceMap` property map.

- The `BinaryFunction` type must be a model of BinaryFunction. 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 version (1) the graph must be a PropertyGraph for `vertex_distance_t` where the property map for the graph meets the same requirements as for `DistanceMap`. The graph must also be a PropertyGraph for `edge_weight_t` where the property map for the grpah meets the same requirements for `WeightMap` below.

- In version (1) and (2) the graph must be a PropertyGraph for `vertex_color_t` and `vertex_index_t`, where the property map for the graph meet the same requirements as for `ColorMap` and `VertexIndexMap` below.

- 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 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 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.

- VertexIndexMap must be a model of ReadablePropertyMap. The value type of `VertexIndexMap` must be an integer type. The vertex descriptor type of the graph needs to be usable as the key type of the index map.

- The type `UniformCostVisitor` must be a model of UniformCostVisitor.

### 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)