Skip to main content

get_shortest_paths

Function get_shortest_paths 

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

Returns one shortest path from source to every vertex in the graph.

Uses BFS (unweighted edges). The result is a Vec of length vcount(), where result[v] is the vertex sequence of a shortest path from source to v (inclusive at both ends). If v is unreachable from source, result[v] is empty.

For directed graphs, follows edges in the outgoing direction by default. Use get_shortest_paths_with_mode to control direction.

§Errors

  • InvalidArgument if source >= vcount().

§Examples

use rust_igraph::{Graph, get_shortest_paths};

// Path 0-1-2-3
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
let paths = get_shortest_paths(&g, 0).unwrap();
assert_eq!(paths[0], vec![0]);
assert_eq!(paths[1], vec![0, 1]);
assert_eq!(paths[2], vec![0, 1, 2]);
assert_eq!(paths[3], vec![0, 1, 2, 3]);