Expand description
Shortest path algorithms: Dijkstra’s algorithm
Provides shortest path computation for weighted graphs:
dijkstra: Single-source shortest paths with non-negative weightsdijkstra_path: Shortest path between two specific nodes
§Example
use trueno_graph::{CsrGraph, NodeId, dijkstra};
// Build a weighted graph
let edges = vec![
(NodeId(0), NodeId(1), 1.0),
(NodeId(1), NodeId(2), 2.0),
(NodeId(0), NodeId(2), 5.0),
];
let graph = CsrGraph::from_edge_list(&edges).unwrap();
// Find shortest paths from node 0
let distances = dijkstra(&graph, NodeId(0));
assert_eq!(distances.get(&NodeId(0)), Some(&0.0));
assert_eq!(distances.get(&NodeId(1)), Some(&1.0));
assert_eq!(distances.get(&NodeId(2)), Some(&3.0)); // 0→1→2 = 3.0, not 0→2 = 5.0Functions§
- dijkstra
- Compute single-source shortest paths using Dijkstra’s algorithm
- dijkstra_
path - Dijkstra shortest path between
sourceandtarget, returning(cost, path)if reachable.