Skip to main content

shortest_path

Function shortest_path 

Source
pub fn shortest_path<N: Clone>(
    graph: &Graph<N, i64>,
    source: usize,
    goal: usize,
) -> Result<ShortestPath<N>, GraphError>
Expand description

Return one shortest path and its reusable certificate.

The helper delegates search to Bellman-Ford and verifies the produced ShortestPathCertificate before returning. The graph may be directed or undirected and may contain negative edges, but negative cycles are rejected.

use sim_lib_discrete_graph::{Directedness, Graph, shortest_path};

let mut g = Graph::with_nodes(vec!["start", "via", "goal"], Directedness::Directed);
g.add_edge(0, 1, 1).unwrap();
g.add_edge(1, 2, 1).unwrap();
g.add_edge(0, 2, 5).unwrap();

let path = shortest_path(&g, 0, 2).unwrap();
assert_eq!(path.nodes, vec!["start", "via", "goal"]);
assert_eq!(path.distance, Some(2));
assert_eq!(path.certificate.predecessors[2], Some(1));