# `johnson_all_pairs_shortest_paths`

    // named parameter version
    template <class Graph, class DistanceMatrix, class P, class T, class R>
    bool johnson_all_pairs_shortest_paths(Graph& g, DistanceMatrix& D,
      const bgl_named_params<P, T, R>& params = all defaults)

    // there is not a non-named parameter version of this function

This algorithm finds the shortest distance between every pair of vertices in the graph. The algorithm returns false if there is a negative weight cycle in the graph and true otherwise. The distance between each pair of vertices is stored in the distance matrix `D`. This is one of the more time intensive graph algorithms, having a time complexity of O(V E log V).

### Where Defined

`boost/graph/johnson_all_pairs_shortest.hpp`

### Parameters

IN: `Graph& g`

> A directed or undirected graph. The graph type must be a model of Vertex List Graph, Edge List Graph, and Incidence Graph.

OUT: `DistanceMatrix& D`

> The length of the shortest path between each pair of vertices u,v in the graph is stored in `D[u][v]`. The set of types {`DistanceMatrix, vertex_descriptor, D`} must be a model of BasicMatrix where `D` is the value type of the `DistanceMap`.

### Named Parameters

IN: `weight_map(WeightMap w_map)`

> The weight or ``length'' of each edge in the graph. The type `WeightMap` must be a model of Readable Property Map. 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.
> Default: `get(edge_weight, g)`

UTIL: `weight_map2(WeightMap2 w2_map)`

> The algorithm requires storage for modified edge weights. The type `WeightMap2` must be a model of Read/Write Property Map. 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.
> Default: `get(edge_weight2, g)`

IN: `vertex_index_map(VertexIndexMap i_map)`

> This maps each vertex to an integer in the range `[0, num_vertices(g))`. This is necessary for efficient updates of the heap data structure in the internal call to Dijkstra's algorithm. The type `VertexIndexMap` must be a model of Readable Property Map. The value type of the map must be an integer type. The vertex descriptor type of the graph needs to be usable as the key type of the map.
> Default: `get(vertex_index, g)`

UTIL/OUT: `distance_map(DistanceMap d_map)`

> The shortest path weight from the source vertex `s` to each vertex in the graph `g` is recorded in this property map. The shortest path weight is the sum of the edge weights along the shortest path. The type `DistanceMap` must be a model of \concept{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 Less Than Comparable.
> Default: `iterator_property_map` created from a `std::vector` of the `WeightMap`'s value type of size `num_vertices(g)` and using the `i_map` for the index map.

IN: `distance_compare(CompareFunction cmp)`

> This function is use to compare distances to determine which vertex is closer to the source vertex. The `CompareFunction` type must be a model of \stlconcept{BinaryPredicate} and have argument types that match the value type of the `DistanceMap` property map.
> Default: `std::less<D>` with `D=typename property_traits<DistanceMap>::value_type`

IN: `distance_combine(CombineFunction cmb)`

> This function is used to combine distances to compute the distance of a path. The `CombineFunction` type must be a model of Binary Function. 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.
> Default: `std::plus<D>` with `D=typename property_traits<DistanceMap>::value_type`

IN: `distance_inf(InfinityGenerator inf)`

> This function is used to initialize the distance for each vertex before the start of the algorithm. The `InfinityGenerator` type must model the Generator concept. The result type of the generator must be convertible to the value type of the `DistanceMap`. The default infinity generator uses `std::numeric_limits::max()`.
> Default: `detail::generate_infinity<D>` with `D=typename property_traits<DistanceMap>::value_type`

IN: `distance_zero(ZeroGenerator zero)`

> This function is used to initialize the distance for the source vertex before the start of the algorithm. The `ZeroGenerator` type must model the Generatorconcept. The result type of the generator must be convertible to the value type of the `DistanceMap`.
> Default: `detail::generate_zero<D>` with `D=typename property_traits<DistanceMap>::value_type`

UTIL/OUT: `color_map(ColorMap c_map)`

> 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 `ColorMap` must be a model of Read/Write Property Map. 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 Color Value.
> Default: an `iterator_property_map` created from a `std::vector` of `default_color_type` of size `num_vertices(g)` and using the `i_map` for the index map.

### Complexity

The time complexity is O(V E log V).

### Example

The file `example/johnson-eg.cpp` applies Johnson's algorithm for all-pairs shortest paths to the example graph from page 568 of the CLR [8].

Copyright © 2000-2001 | Jeremy Siek, Indiana University (jsiek@osl.iu.edu)