Function graphalgs::shortest_path::floyd_warshall
source · pub fn floyd_warshall<G, F, K>(
graph: G,
edge_cost: F
) -> Result<Vec<Vec<K>>, NegativeCycle>where
G: IntoEdgeReferences + IntoNodeIdentifiers + NodeIndexable,
F: FnMut(G::EdgeRef) -> K,
K: FloatMeasure,
Expand description
Floyd–Warshall algorithm for all pairs shortest path problem.
Compute shortest paths in a weighted graph with positive or negative edge weights, but with no negative cycles. Multiple edges and self-loops allowed.
Arguments
graph
: weighted graph.edge_cost
: closure that returns cost of a particular edge.
Returns
Err
: if graph contains negative cycle.Ok
: matrixVec<Vec<K>>
of shortest distances, in cell (i, j) of which the length of the shortest path from node i to node j is stored.
Examples
use graphalgs::shortest_path::floyd_warshall;
use petgraph::Graph;
let inf = f32::INFINITY;
let graph = Graph::<(), f32>::from_edges(&[
(0, 1, 2.0), (1, 2, 10.0), (1, 3, -5.0),
(3, 2, 2.0), (2, 3, 20.0),
]);
assert_eq!(
floyd_warshall(&graph, |edge| *edge.weight()),
Ok(vec![vec![0.0, 2.0, -1.0, -3.0],
vec![inf, 0.0, -3.0, -5.0],
vec![inf, inf, 0.0, 20.0],
vec![inf, inf, 2.0, 0.0]])
);
// Negative cycle.
let graph = Graph::<(), f32>::from_edges(&[
(0, 1, 2.0), (1, 2, 2.0), (2, 0, -10.0)]);
assert!(floyd_warshall(&graph, |edge| *edge.weight()).is_err());