pub fn dijkstra<N>(
graph: &Graph<N, u64>,
source: usize,
) -> Result<PathResult<u64>, GraphError>Expand description
Dijkstra’s algorithm over non-negative u64 weights.
§Examples
On a directed graph where the two-hop route 0 -> 1 -> 2 (1 + 2 = 3) beats
the direct edge 0 -> 2 (5), the shortest distance to node 2 is 3:
use sim_lib_discrete_graph::{dijkstra, Directedness, Graph};
let mut g: Graph<(), u64> = Graph::with_nodes(vec![(), (), ()], Directedness::Directed);
g.add_edge(0, 1, 1).unwrap();
g.add_edge(1, 2, 2).unwrap();
g.add_edge(0, 2, 5).unwrap();
let r = dijkstra(&g, 0).unwrap();
assert_eq!(r.distances, vec![Some(0), Some(1), Some(3)]);
assert_eq!(r.predecessors[2], Some(1)); // reached via node 1