How to use the Dijkstra Algorithm Visualizer
- 1. Enter the graphOne edge per line, like A-B 4. Tick directed if edges go one way.
- 2. Pick a start nodeChoose where the shortest paths begin.
- 3. ReplayWatch the closest unfinished node get picked and its edges relaxed, with distances updated under each node.
Worked example
The sample graph has six nodes and nine undirected edges. From A, the algorithm first finalises A at 0, then C at 2. Relaxing C–B gives B a distance of 2 + 1 = 3, better than the direct edge A–B of 4. B then improves D to 3 + 5 = 8, D gives E 8 + 2 = 10 (beating C–E's 12), and E gives F 10 + 2 = 12 (beating D–F's 14). The final paths include A → C → B → D → E → F for F. The replay has 17 steps.
How it works
Dijkstra keeps a tentative distance for every node, starting with 0 for the source and ∞ for the rest. Each round it takes the unfinished node with the smallest distance, which is now final, and checks each edge from it: if going through this node is shorter, the neighbour's distance and previous node are updated. With a binary heap this runs in O((V + E) log V). Ties are broken alphabetically here so the replay is predictable.
Negative weights
Dijkstra assumes a finalised distance can never improve, which is false when edges can be negative. The tool refuses negative weights for that reason; use Bellman-Ford, which handles them and detects negative cycles. For unweighted graphs, plain BFS finds shortest paths faster.
Frequently asked questions
Can I use directed graphs?
Yes. Tick directed edges and A-B means only A → B.
How many nodes can I use?
Up to 14, so the drawing stays readable.
What if a node can't be reached?
Its distance stays ∞ and it's listed as unreachable.
Is the Dijkstra visualizer free?
Yes. Everything in the Dijkstra visualizer works without signing up. The first time you ask for a result, we ask once for your name and WhatsApp number; after that the Dijkstra visualizer and every other Uncodemy tool open straight away.