Skip to main content

Module shortest_path

Module shortest_path 

Source
Expand description

Shortest path algorithms: Dijkstra’s algorithm

Provides shortest path computation for weighted graphs:

  • dijkstra: Single-source shortest paths with non-negative weights
  • dijkstra_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.0

Functions§

dijkstra
Compute single-source shortest paths using Dijkstra’s algorithm
dijkstra_path
Dijkstra shortest path between source and target, returning (cost, path) if reachable.