Step through Dijkstra's algorithm on your own weighted edge list: each heap pop, relaxation and stale entry, then every shortest path from the source.
Most Dijkstra animations hide the part that trips people up in contests: the priority queue holds old, longer distances that have to be thrown away. This Dijkstra Shortest Path Visualizer runs the same lazy-deletion loop you would write with std::priority_queue and shows every pop, every relaxation and every stale entry it skips.
Paste a weighted edge list, pick a source, and step forward or press Play. The graph, the queue and the distance table move in lockstep, and the last step draws the shortest-path tree.
The tool opens on 0 1 4, 0 2 2, 1 3 5, 2 1 1, 2 3 8, 3 4 3, 4 0 7, read as a directed graph from node 0:
Five settle steps, two stale pops. Switch to Undirected and node 4 drops to 7, because the edge 4 0 7 can now be walked backwards. Choose a node under Highlight path to to colour its route in the final step.
Instead of decreasing a key inside the heap, each improvement pushes a new (distance, node) pair. When a pair comes off the top with a distance larger than the one already recorded, it is stale and skipped. That gives O((V + E) log V) with at most E pushes. Ties are broken by node label, so a run always replays the same way. Copy C++ Template gives the same loop as contest code, and Copy Distances exports every distance with its path.
A negative weight breaks the greedy step Dijkstra relies on, so the input is refused with a pointer to the right algorithm: “Line 2 has weight -2. Dijkstra's algorithm needs non-negative weights; use Bellman–Ford for graphs with negative edges.” Nodes the source cannot reach keep a distance of ∞ and are listed as unreachable. Duplicate edges and self-loops are allowed. The drawing handles up to 50 nodes and 300 edges, with labels from 0 to 9999 and weights up to one billion.
Start: dist[0] = 0, every other node is ∞, and the queue holds (0, 0).
queue: (0, 0)
| Node | dist (now) | prev | Shortest path (final) |
|---|---|---|---|
| 0 | 0 | — | 0 (0) |
| 1 | ∞ | — | 0 → 2 → 1 (3) |
| 2 | ∞ | — | 0 → 2 (2) |
| 3 | ∞ | — | 0 → 2 → 1 → 3 (8) |
| 4 | ∞ | — | 0 → 2 → 1 → 3 → 4 (11) |
Orange outline: source and the node being settled. Shaded: settled. At the last step, dark edges form the shortest-path tree. O((V + E) log V) with a binary heap.