Skip to main content

floyd_warshall_distances

Function floyd_warshall_distances 

Source
pub fn floyd_warshall_distances(
    graph: &Graph,
    weights: Option<&[f64]>,
) -> IgraphResult<Vec<Vec<Option<f64>>>>
Expand description

All-pairs shortest distances via Floyd-Warshall.

weights = None collapses to unit weights and reproduces unweighted BFS distances (modulo cost: BFS is O(V·(V+E)) vs FW’s O(V³)). When weights = Some(w), w.len() must equal graph.ecount().

Returns a vcount × vcount matrix where out[i][j] = Some(d) if there is a path i → j of weighted length d, and None if j is unreachable from i. The diagonal is always Some(0.0) (a non-trivial cycle through i of total weight 0 is not surfaced — this matches igraph’s C contract; only negative cycles are flagged as errors).

Errors:

  • weights.len() != ecountInvalidArgument.
  • any w == NaNInvalidArgument.
  • undirected graph with any w < 0InvalidArgument (negative 2-cycle).
  • directed graph with self-loop of w < 0InvalidArgument.
  • negative cycle detected during relaxation → InvalidArgument.

§Examples

use rust_igraph::{Graph, floyd_warshall_distances};

// Triangle 0-1-2 with weights 1, 4, 2 → dist(0,2) = 1+2 = 3.
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(1, 2).unwrap();
let d = floyd_warshall_distances(&g, Some(&[1.0, 4.0, 2.0])).unwrap();
assert_eq!(d[0][2], Some(3.0));
assert_eq!(d[0][0], Some(0.0));