pub fn johnson_distances(
graph: &Graph,
weights: &[f64],
) -> IgraphResult<Vec<Vec<Option<f64>>>>Expand description
All-pairs shortest-path distances via Johnson’s algorithm.
Returns a vcount × vcount matrix where result[u][v] is the
distance from u to v: Some(d) if reachable, None
otherwise. result[u][u] is Some(0.0) for every valid u.
Use this when (a) you need many shortest paths and (b) at least one edge weight is negative. For non-negative weights this function transparently falls through to V independent Dijkstra runs — slightly wasteful but always correct.
weights[e] is the weight of edge e; length must equal
graph.ecount(). NaN weights are rejected. Positive-infinite
weights are treated as “edge ignored” (matches upstream).
Constraint: undirected graphs with any negative weight are
rejected (IgraphError::InvalidArgument) — an undirected
negative edge is itself a length-2 negative cycle.
Counterpart of igraph_distances_johnson(_, _, vss_all(), vss_all(), weights, IGRAPH_OUT).
§Examples
use rust_igraph::{Graph, johnson_distances};
// Directed diamond 0→1 (3), 0→2 (1), 1→3 (-2), 2→3 (4).
// The negative edge 1→3 makes 0→1→3 cheaper than 0→2→3.
let mut g = Graph::new(4, true).unwrap();
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(2, 3).unwrap();
let d = johnson_distances(&g, &[3.0, 1.0, -2.0, 4.0]).unwrap();
// Row 0 (distances from 0): [0, 3, 1, 1]
assert_eq!(d[0], vec![Some(0.0), Some(3.0), Some(1.0), Some(1.0)]);