Dijkstra finds shortest paths from a source in weighted graphs with non-negative edge weights. The greedy approach: always relax the minimum-distance unvisited node. .NET PriorityQueue<TElement, TPriority> supports this directly with O(log n) Enqueue/Dequeue. The skip-stale optimisation avoids a separate "visited" set. A* adds a heuristic to guide search toward the destination. Bellman-Ford handles negative weights at O(VE) cost.
Why skip-stale avoids a separate visited[] array.
The skip-stale pattern allows multiple entries per node in PriorityQueue. Stale entries are cheap to skip, and no visited HashSet is needed.
Negative edges break the greedy invariant. Use Bellman-Ford (O(VE)) for graphs with negative-weight edges. Johnson's algorithm for all-pairs.
With a Manhattan or Euclidean distance heuristic, A* explores far fewer nodes than Dijkstra on grid-based maps.
Sign in to share your feedback and join the discussion.