Greedy settlement
The unvisited vertex with the smallest tentative distance becomes settled. With non-negative weights, its shortest distance can no longer improve.
Dijkstra's algorithm finds shortest paths from one source vertex in a graph with non-negative edge weights. This simulator uses an undirected weighted graph and a min-priority queue.
Example weighted graph loaded. Drag vertices or run Dijkstra.
| Vertex | Distance | Previous | State |
|---|
candidate =
distance[current] + edgeWeight
if candidate < distance[neighbor]:
distance[neighbor] = candidate
previous[neighbor] = current
push updated entry
into the min-priority queue
Stale queue entries are ignored.The unvisited vertex with the smallest tentative distance becomes settled. With non-negative weights, its shortest distance can no longer improve.
Each outgoing edge tests whether reaching a neighbor through the current vertex gives a shorter route.
The predecessor table stores the previous vertex on each best-known route. Following those links backward reconstructs the shortest path.
A vertex outside the source's connected component keeps distance infinity and has no shortest path from that source.
| Condition | Result |
|---|---|
| All edge weights are non-negative | Dijkstra is correct. |
| A negative edge exists | Use Bellman–Ford or another suitable algorithm. |
| All weights are equal | BFS is sufficient. |
| Need all-pairs shortest paths | Run from each source or use an all-pairs algorithm. |