pub fn get_shortest_path(
graph: &Graph,
from: VertexId,
to: VertexId,
weights: Option<&[f64]>,
mode: DijkstraMode,
) -> IgraphResult<ShortestPath>Expand description
Calculate a single shortest path from from to to.
Returns the path as both its vertex sequence (source first, target last) and the edge sequence along it. If several shortest paths exist, an arbitrary one is returned.
The backend mirrors igraph_get_shortest_paths:
weights == None: unweighted breadth-first search,weights == Some(w)with allw[e] >= 0: Dijkstra,weights == Some(w)with anyw[e] < 0: Bellman-Ford.
On directed graphs mode selects which adjacency is followed
(DijkstraMode::Out / DijkstraMode::In / DijkstraMode::All);
it is ignored on undirected graphs.
§Errors
Returns IgraphError::VertexOutOfRange if from or to is not a
valid vertex, or IgraphError::InvalidArgument if weights is
provided and its length differs from the edge count or contains NaN.
§Examples
use rust_igraph::{Graph, get_shortest_path, DijkstraMode};
let mut g = Graph::new(4, false).unwrap();
g.add_edge(0, 1).unwrap(); // edge 0
g.add_edge(1, 2).unwrap(); // edge 1
g.add_edge(2, 3).unwrap(); // edge 2
let p = get_shortest_path(&g, 0, 3, None, DijkstraMode::Out).unwrap();
assert_eq!(p.vertices, vec![0, 1, 2, 3]);
assert_eq!(p.edges, vec![0, 1, 2]);
// Unreachable target → empty path.
let mut h = Graph::new(2, true).unwrap();
h.add_edge(0, 1).unwrap();
let q = get_shortest_path(&h, 1, 0, None, DijkstraMode::Out).unwrap();
assert!(q.vertices.is_empty());
assert!(q.edges.is_empty());