reconstruct_path

Function reconstruct_path 

Source
pub fn reconstruct_path(
    result: &DijkstraResult,
    start: NodeId,
    end: NodeId,
) -> Option<(Vec<NodeId>, f64)>
Expand description

Reconstructs the shortest path from start to end using the parent map produced by dijkstra.

Returns Some((path, total_distance)) where path[0] == start and path.last() == end, or None if end is unreachable from start.

ยงExamples

use graph_core::{AdjacencyList, Graph};
use graph_shortest_path::dijkstra;
use graph_shortest_path::dijkstra::reconstruct_path;

let mut g: AdjacencyList<()> = AdjacencyList::directed();
let a = g.add_node(());
let b = g.add_node(());
let c = g.add_node(());
g.add_edge(a, b, 1.0).unwrap();
g.add_edge(b, c, 2.0).unwrap();

let result = dijkstra(&g, a).unwrap();
let (path, dist) = reconstruct_path(&result, a, c).unwrap();
assert_eq!(path, vec![a, b, c]);
assert_eq!(dist, 3.0);