Skip to main content

distances_all

Function distances_all 

Source
pub fn distances_all(graph: &Graph) -> IgraphResult<Vec<Option<u32>>>
Expand description

All-pairs unweighted shortest distances.

Returns a flat Vec<Option<u32>> of length n * n in row-major order, where result[i * n + j] is the shortest-path distance from vertex i to vertex j. None means unreachable.

For undirected graphs, the matrix is symmetric. For directed graphs, follows outgoing edges by default; use distances_all_with_mode for direction control.

§Errors

Returns an error if internal BFS encounters an issue (should not happen for valid graphs).

§Examples

use rust_igraph::{Graph, distances_all};

// Triangle: all distances are 0 or 1.
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
let d = distances_all(&g).unwrap();
assert_eq!(d[0 * 3 + 1], Some(1)); // 0→1
assert_eq!(d[0 * 3 + 2], Some(1)); // 0→2
assert_eq!(d[1 * 3 + 2], Some(1)); // 1→2
assert_eq!(d[0 * 3 + 0], Some(0)); // self