Skip to main content

get_all_shortest_paths

Function get_all_shortest_paths 

Source
pub fn get_all_shortest_paths(
    graph: &Graph,
    source: VertexId,
) -> IgraphResult<AllShortestPaths>
Expand description

Find all shortest paths from source to every reachable vertex.

Uses BFS (unweighted edges). For directed graphs, follows outgoing edges by default. Use get_all_shortest_paths_with_mode for direction control.

§Errors

  • InvalidArgument if source >= vcount().

§Examples

use rust_igraph::{Graph, get_all_shortest_paths};

// Diamond: 0-1, 0-2, 1-3, 2-3. Two shortest paths from 0 to 3.
let mut g = Graph::with_vertices(4);
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 r = get_all_shortest_paths(&g, 0).unwrap();
assert_eq!(r.paths[3].len(), 2); // two paths to vertex 3
assert_eq!(r.nrgeo[3], 2);
assert_eq!(r.paths[0], vec![vec![0]]); // self-path