Skip to main content

Module shortest_paths

Module shortest_paths 

Source
Expand description

Single-source shortest paths: Dijkstra, Bellman-Ford and A*.

See the book entry Shortest paths.

Structs§

ShortestPaths
Distances from a source and the predecessor tree for path reconstruction.

Functions§

a_star
A* search from source to target guided by heuristic(v), an estimate of the remaining cost. With an admissible (never overestimating) and consistent heuristic the returned path is optimal; a zero heuristic reduces to Dijkstra. Returns (cost, path) or None if unreachable.
bellman_ford
The Bellman-Ford algorithm, O(V E), allowing negative weights. Returns an error if a negative cycle is reachable from the source (shortest paths are then undefined).
dijkstra
Dijkstra’s algorithm with a binary heap, O((V + E) log V). Edge weights must be non-negative.