Floyd-Warshall All Pairs Shortest Paths
Finds the shortest distance between every pair of vertices in a graph using the Floyd-Warshall algorithm.
Complexity: O(V3)
Defined in: <boost/graph/floyd_warshall_shortest.hpp>
Example
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/floyd_warshall_shortest.hpp>
#include <iostream>
#include <limits>
#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));
floyd_warshall_all_pairs_shortest_paths(g, D,
get(&Edge::weight, g),
std::less<int>{},
closed_plus<int>{(std::numeric_limits<int>::max)()},
(std::numeric_limits<int>::max)(),
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) floyd_warshall_initialized_all_pairs_shortest_paths (positional parameter version)
template <typename VertexListGraph, typename DistanceMatrix,
typename BinaryPredicate, typename BinaryFunction,
typename Infinity, typename Zero>
bool floyd_warshall_initialized_all_pairs_shortest_paths(
const VertexListGraph& g, DistanceMatrix& d,
const BinaryPredicate& compare, const BinaryFunction& combine,
const Infinity& inf, const Zero& zero);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
A directed or undirected graph. The graph must be a model of Vertex List Graph. |
OUT |
|
The length of the shortest path between each pair of vertices |
IN |
|
The function used to compare distances to determine which target
vertex is closer to the source vertex. The argument types must match the
value type of the |
IN |
|
The function used to combine distances to compute the distance of a
path. The argument types must match the value type of the |
IN |
|
The value used to initialize the distance for each vertex before
starting the algorithm, and to represent the distance between vertices
for which there is no path. Should be larger than any possible valid
path length. The argument type must match the value type of the |
IN |
|
The value used to represent the distance from a vertex to itself, and
to determine if a value is negative. The argument type must match the
value type of the |
(2) floyd_warshall_all_pairs_shortest_paths (positional parameter version)
template <typename VertexAndEdgeListGraph, typename DistanceMatrix,
typename WeightMap, typename BinaryPredicate,
typename BinaryFunction, typename Infinity, typename Zero>
bool floyd_warshall_all_pairs_shortest_paths(
const VertexAndEdgeListGraph& g, DistanceMatrix& d,
const WeightMap& w, const BinaryPredicate& compare,
const BinaryFunction& combine,
const Infinity& inf, const Zero& zero);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
A directed or undirected graph. The graph must be a model of Vertex And Edge List Graph. |
OUT |
|
The length of the shortest path between each pair of vertices |
IN |
|
The weight or length of each edge in the graph. The |
IN |
|
The function used to compare distances to determine which target
vertex is closer to the source vertex. The argument types must match the
value type of the |
IN |
|
The function used to combine distances to compute the distance of a
path. The argument types must match the value type of the |
IN |
|
The value used to initialize the distance for each vertex before
starting the algorithm, and to represent the distance between vertices
for which there is no path. Should be larger than any possible valid
path length. The argument type must match the value type of the |
IN |
|
The value used to represent the distance from a vertex to itself, and
to determine if a value is negative. The argument type must match the
value type of the |
(3) floyd_warshall_initialized_all_pairs_shortest_paths (named parameter version)
template <class VertexListGraph, class DistanceMatrix,
class P, class T, class R>
bool floyd_warshall_initialized_all_pairs_shortest_paths(
const VertexListGraph& g, DistanceMatrix& d,
const bgl_named_params<P, T, R>& params);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
A directed or undirected graph. The graph must be a model of Vertex List Graph. |
OUT |
|
The length of the shortest path between each pair of vertices |
IN |
|
Named parameters passed via |
| Direction | Named Parameter | Description / Default |
|---|---|---|
IN |
|
The weight or length of each edge in the graph. The |
IN |
|
The function used to compare distances to determine which target
vertex is closer to the source vertex. The |
IN |
|
The function used to combine distances to compute the distance of a
path. The |
IN |
|
The value used to initialize the distance for each vertex before
starting the algorithm, and to represent the distance between vertices
for which there is no path. Should be larger than any possible valid
path length. The argument type must match the value type of the
|
IN |
|
The value used to represent the distance from a vertex to itself, and
to determine if a value is negative. The argument type must match the
value type of the |
(4) floyd_warshall_all_pairs_shortest_paths (named parameter version)
template <class VertexAndEdgeListGraph, class DistanceMatrix,
class P, class T, class R>
bool floyd_warshall_all_pairs_shortest_paths(
const VertexAndEdgeListGraph& g, DistanceMatrix& d,
const bgl_named_params<P, T, R>& params);
| Direction | Parameter | Description |
|---|---|---|
IN |
|
A directed or undirected graph. The graph must be a model of Vertex And Edge List Graph. |
OUT |
|
The length of the shortest path between each pair of vertices |
IN |
|
Named parameters passed via |
| Direction | Named Parameter | Description / Default |
|---|---|---|
IN |
|
The weight or length of each edge in the graph. The |
IN |
|
The function used to compare distances to determine which target
vertex is closer to the source vertex. The |
IN |
|
The function used to combine distances to compute the distance of a
path. The |
IN |
|
The value used to initialize the distance for each vertex before
starting the algorithm, and to represent the distance between vertices
for which there is no path. Should be larger than any possible valid
path length. The argument type must match the value type of the
|
IN |
|
The value used to represent the distance from a vertex to itself, and
to determine if a value is negative. The argument type must match the
value type of the |
Description
These algorithms implement the
Floyd-Warshall algorithm
to find the shortest distance between every pair of vertices in the graph. The shortest distance between each pair of
vertices is stored in the distance matrix d. The difference between the
two algorithms is in whether the distance matrix is assumed to be
initialized or not: floyd_warshall_initialized_all_pairs_shortest_paths
requires the caller to initialize the distance matrix before calling the
function, while floyd_warshall_all_pairs_shortest_paths initializes the
matrix automatically.
This algorithm should be used to compute shortest paths between every
pair of vertices for dense graphs. For sparse graphs, use
johnson_all_pairs_shortest_paths.