Dijkstra and early termination
Dijkstra from point to point
With Dijkstra’s algorithm, we can use early termination to stop once we find the shortest path from node A to node Z.
Early termination in Dijkstra’s algorithm
Dijkstra’s algorithm finds the shortest path from the source node A to all nodes in the graph. However, if our goal is only to find the shortest path from A to a specific destination node Z, we can apply early termination.
Here’s how it works:
- Dijkstra’s algorithm processes nodes in order of increasing distance (cost) from the start node A.
- As soon as the destination node Z is removed from the priority queue and processed, Dijkstra has found the shortest path from A to Z.
- Once we know the shortest path to Z, there’s no longer any need to explore any other nodes. Any other path(s) we might find would be more expensive.
Thus, we can terminate early as soon as we dequeue Z from the priority queue, since we have a guarantee that we’ve found the shortest path to Z.
Comparison with \astar
By contrast, \astar is intended to find a shortest path from some start node to a specific goal node. So \astar has early termination built in by design.
Copyright © 2023–2026 Clayton Cafiero
No generative AI was used in producing drafts of this material. This was written the old-fashioned way. AI was used to rewrite existing pseudocode in LaTeX to produce standalone *.tex files for rendering, and for revisions toward satisfying WCAG 2.1 AA-level accessibility standards as required by UVM policy. It may also have been used to proofread selected human-written prose. Claude 2.1.150 with model Sonnet 4.6. Revisions, if any, were performed by the author. AI was not used in generating any code whatsoever. All code samples and starter code are by the author only.