Johnson All Pairs Shortest Paths
Finds the shortest distance between every pair of vertices in a graph, suitable for sparse graphs with positive or negative edge weights.
Complexity: O(V E log V)
Defined in: <boost/graph/johnson_all_pairs_shortest.hpp>
Example
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/johnson_all_pairs_shortest.hpp>
#include <iostream>
#include <vector>
struct Node {};
struct Edge { int weight; };
using namespace boost;
using Graph = adjacency_list<vecS, vecS, directedS, Node, Edge>;
int main() {
Graph g{3};
add_edge(0, 1, Edge{2}, g);
add_edge(1, 2, Edge{3}, g);
add_edge(0, 2, Edge{7}, g);
auto n = num_vertices(g);
std::vector<std::vector<int>> D(n, std::vector<int>(n));
johnson_all_pairs_shortest_paths(g, D,
get(vertex_index, g), get(&Edge::weight, g), int{0});
for (std::size_t i = 0; i < n; ++i) {
for (std::size_t j = 0; j < n; ++j)
std::cout << D[i][j] << (j + 1 < n ? " " : "");
std::cout << "\n";
}
}
0 2 5
2147483647 0 3
2147483647 2147483647 0
(1) Positional version
template <typename Graph, typename DistanceMatrix,
typename VertexIndex, typename WeightMap, typename DT>
bool johnson_all_pairs_shortest_paths(
VertexAndEdgeListGraph& g1,
DistanceMatrix& D,
VertexIndex i_map, WeightMap w_map, DT zero);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
A directed or undirected graph. The graph type must be a model of Vertex List Graph, Edge List Graph, and Incidence Graph. |
OUT |
|
The length of the shortest path between each pair of vertices u,v in the
graph is stored in |
IN |
|
Maps each vertex to an integer in the range |
IN |
|
The weight or "length" of each edge in the graph. The type |
IN |
|
The value used to initialize the distance for the source vertex before the
start of the algorithm. The type |
(2) Positional parameter version
template <class VertexAndEdgeListGraph, class DistanceMatrix,
class VertexID, class Weight, class BinaryPredicate,
class BinaryFunction, class Infinity, class DistanceZero>
bool johnson_all_pairs_shortest_paths(
VertexAndEdgeListGraph& g1,
DistanceMatrix& D,
VertexID id1, Weight w1, const BinaryPredicate& compare,
const BinaryFunction& combine, const Infinity& inf,
DistanceZero zero);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
A directed or undirected graph. The graph type must be a model of Vertex List Graph, Edge List Graph, and Incidence Graph. |
OUT |
|
The length of the shortest path between each pair of vertices u,v in the
graph is stored in |
IN |
|
Maps each vertex to an integer in the range |
IN |
|
The weight or "length" of each edge in the graph. 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 |
|
This value is used to initialize the distance for each vertex before the start
of the algorithm. The type |
IN |
|
This value is used to initialize the distance for the source vertex before the
start of the algorithm. The type |
(3) Named parameter version
template <typename Graph, typename DistanceMatrix,
typename P, typename T, typename R>
bool johnson_all_pairs_shortest_paths(
Graph& g, DistanceMatrix& D,
const bgl_named_params<P, T, R>& params = all defaults);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
A directed or undirected graph. The graph type must be a model of Vertex List Graph, Edge List Graph, and Incidence Graph. |
OUT |
|
The length of the shortest path between each pair of vertices u,v in the
graph is stored in |
IN |
|
Named parameters passed via |
| Direction | Named Parameter | Description / Default |
|---|---|---|
IN |
|
The weight or "length" of each edge in the graph. The type |
UTIL |
|
This parameter is no longer needed, and will be ignored. |
IN |
|
This maps each vertex to an integer in the range |
UTIL |
|
This parameter is no longer needed, and will be ignored. |
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 |
|
This value is used to initialize the distance for each vertex before the start
of the algorithm. The type |
IN |
|
This value is used to initialize the distance for the source vertex before the
start of the algorithm. 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 |
Description
This algorithm
finds the shortest distance between every pair of vertices in the
graph. The distance between each pair of vertices is stored in the distance
matrix D. The algorithm returns false if there is a negative weight cycle in
the graph and true otherwise.
This algorithm should be used to compute shortest paths between every pair of
vertices for sparse graphs. For dense graphs, use
floyd_warshall_all_pairs_shortest_paths.
Returns
Returns true if all shortest paths were successfully computed (no negative
weight cycle exists). Returns false if a negative weight cycle is detected in
the graph.
Example
See example/johnson-eg.cpp for an example that
applies Johnson’s algorithm for all-pairs shortest paths to the example graph
from page 568 of the CLR [5].