pub fn dijkstra_path<N, E, Ix>(
graph: &Graph<N, E, Ix>,
source: &N,
target: &N,
) -> Result<Option<Path<N, E>>>Expand description
Finds the shortest path between source and target nodes using Dijkstra’s algorithm (modern API)
This function provides the same functionality as shortest_path but with a clearer name
that explicitly indicates the algorithm being used. This is the recommended function to use
going forward.
§Arguments
graph- The graph to search insource- The source nodetarget- The target node
§Returns
Ok(Some(Path))- If a path existsOk(None)- If no path existsErr(GraphError)- If the source or target node is not in the graph
§Time Complexity
O((V + E) log V) where V is the number of vertices and E is the number of edges.
§Space Complexity
O(V) for the distance array and predecessor tracking.
§Example
use scirs2_graph::{Graph, dijkstra_path};
let mut graph: Graph<String, f64> = Graph::new();
graph.add_node("A".to_string());
graph.add_node("B".to_string());
graph.add_edge("A".to_string(), "B".to_string(), 1.0).unwrap();
let path = dijkstra_path(&graph, &"A".to_string(), &"B".to_string()).unwrap();
assert!(path.is_some());