Skip to main content

Module yen

Module yen 

Source
Expand description

Yen’s K-shortest loopless paths algorithm.

Finds up to K distinct loopless paths between a source and destination node, ordered by ascending cost. Uses Dijkstra as the inner shortest-path routine.

The implementation follows Yen (1971) with standard spur-node iteration.

§Example

use rustsim_pathfinding::yen::{yen_k_shortest, YenPath};

// Diamond graph: 0→1 (cost 1), 0→2 (cost 2), 1→3 (cost 2), 2→3 (cost 1)
let neighbors = |node: &usize| -> Vec<(usize, f64)> {
    match *node {
        0 => vec![(1, 1.0), (2, 2.0)],
        1 => vec![(3, 2.0)],
        2 => vec![(3, 1.0)],
        _ => vec![],
    }
};

let paths = yen_k_shortest(0, 3, 3, neighbors);
assert_eq!(paths.len(), 2);
assert_eq!(paths[0].nodes, vec![0, 1, 3]);
assert_eq!(paths[1].nodes, vec![0, 2, 3]);

§References

Yen, J. Y. (1971). “Finding the K Shortest Loopless Paths in a Network.” Management Science, 17(11), 712–716.

Structs§

YenPath
A single path with its node sequence and total cost.

Functions§

yen_k_shortest
Find up to k shortest loopless paths from src to dest.