Short answer: Dijkstra’s algorithm grows a set of settled vertices whose distances from the source are final. The next vertex settled is the unsettled vertex with the smallest tentative distance, and its outgoing edges relax their heads. With a binary heap the time is O((V+E) log V) on nonnegative weights. A negative edge can make a settled distance wrong, because a later path could undercut it. A* with a zero heuristic is this algorithm.
What the problem actually asks
A tentative distance is the length of some path from the source, not necessarily the best path. The nonnegative hypothesis says that extending a path cannot make it shorter. Therefore the smallest tentative distance in the frontier is already the true distance: every other path still out in the graph is at least that long before it even arrives. Settling that vertex is safe.
A negative edge removes the therefore. A path that looks long now can still cross a negative edge and arrive cheaper. The vertex would have been settled too soon.
What the algorithm guarantees
On a directed or undirected graph with nonnegative edge weights, each settled distance is the shortest-path distance, and the parent pointers form a shortest-path tree. A binary heap implements the frontier in O((V+E) log V). A Fibonacci heap improves the asymptotic bound to O(E + V log V), which matters in the proof of other algorithms and rarely in a straightforward implementation. Extra memory is Θ(V).
Decrease-key is the heap operation that updates a tentative distance. If the heap does not support it, the usual substitute pushes a new copy of the vertex and ignores a popped copy whose distance is worse than the best known. That substitute is correct and can use O(E) heap entries. Settling a vertex the first time it is popped, and skipping later copies, requires that the first pop is the best distance, which is again the nonnegative hypothesis.
The working steps
- Set dist[source] = 0 and every other distance to infinity. Insert the source in a min-heap keyed by distance.
- Pop the vertex u with the smallest tentative distance. If u was already settled, skip it. Otherwise settle u.
- For each edge u→v of weight w, if dist[u]+w < dist[v], set dist[v] = dist[u]+w, set parent[v] = u, and push the improved record.
- Stop early if the popped vertex is a single target. Its distance is final.
- If the heap empties, unsettled vertices are unreachable. A negative weight anywhere on a reachable edge voids the settle step.
Nonnegative weights are the hypothesis. Negative weights with no negative cycle belong to Bellman-Ford. Weights in {0, 1} can be handled in linear time by 0-1 BFS. A heuristic lower bound on the remaining distance, added into the heap key, is A* and reduces to this algorithm when the heuristic is identically zero.
Approaches that look simpler and fail
| Approach | Why it fails when a settled vertex must stay settled |
|---|---|
| A negative edge out of a settled vertex | A later extension can undercut dist[u]+w for a vertex u that was already popped. The algorithm will not reopen it. |
| A heap keyed by anything other than tentative distance, unless it is an A* f-score with a consistent heuristic | The pop order is the proof. Popping by vertex id, or by hop count while weights differ, settles the wrong vertex. |
| Use the Fibonacci-heap bound for a binary-heap implementation | A binary heap pays an extra log factor on decrease-key or on duplicate pushes. The O(E + V log V) bound is the Fibonacci heap’s bound. |
| Run it once from every vertex to get all pairs, and ignore Johnson’s reweighting when negatives exist | Nonnegative all-pairs can be V runs of Dijkstra. Any negative edge, even with no negative cycle, makes those runs unsafe until potentials reweight them. |
Negative edges belong to Bellman-Ford, and all-pairs with negatives and a reweighting belong to Johnson. Unit weights belong to breadth-first search, which is the degenerate case with a queue instead of a heap.
A working example
This Python fragment grows a settled shortest-path tree. Every edge weight is nonnegative, so the first time a vertex leaves the heap its distance is final. It prints 0 1 2 3.
import heapq
adj = {0: [(1, 1), (2, 4)], 1: [(2, 1), (3, 2)], 2: [(3, 1)], 3: []}
dist = [10**9] * 4
dist[0] = 0
pq = [(0, 0)]
while pq:
d, u = heapq.heappop(pq)
if d != dist[u]:
continue
for v, w in adj[u]:
if d + w < dist[v]:
dist[v] = d + w
heapq.heappush(pq, (dist[v], v))
assert dist == [0, 1, 2, 3]
print(*dist)
Where it is used
OSPF and IS-IS compute routes with Dijkstra on nonnegative link weights. Navigation graphs use the same settled-set growth when the cost is a distance or a travel time. A negative toll breaks the argument, and Bellman-Ford is the algorithm that still applies.
When you can drop this algorithm
Drop Dijkstra when any edge weight may be negative. Bellman-Ford handles that graph and also reports a negative cycle. Keep Dijkstra for nonnegative weights, with a binary heap unless a measured bottleneck says the heap is not the cost.
Why this stays published
We publish this so a search for Dijkstra returns the settled-set argument and the nonnegative hypothesis that makes the first pop final. Dijkstra settles the unsettled vertex of smallest tentative distance, and that distance is final exactly when every edge weight is nonnegative.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
