Graph traversal visualiser: BFS, DFS and Dijkstra
Draw a graph, or start from a preset, and animate breadth-first search, depth-first search or Dijkstra’s shortest-path algorithm one step at a time. The frontier, visit order and distances update as you step, alongside highlighted pseudocode.
On this page
Graph traversal visualiserDraw your own graph or start from a preset, then pick an algorithm and a start vertex.
- Current vertex
- Discovered / in the frontier
- Finished (settled)
- Edge skipped
Edit the graph as text
One edge per line: A B 4 joins A and B with weight 4 (weight optional, default 1). A line with one name adds a lone vertex.
Pseudocode
Frontier
Visit order
Distances
How to use it
- Add vertex: choose the mode, then click empty space. Vertices are named A, B, C, … automatically; select one to rename it.
- Add edge: choose the mode, click one vertex, then another. The weight starts as the distance between them on screen, divided by 45 and rounded, so the drawing roughly matches the numbers.
- Select / move: drag vertices around; click a vertex to rename it, start from it or delete it, and click an edge to change its weight or delete it. Delete removes the selection.
- Edit as text: open “Edit the graph as text” for an edge list such as
A B 4, one edge per line. This is the quickest way to enter a graph from a textbook exercise, and it is fully keyboard-accessible. - Play: use the controls under the graph, or Space to play and pause and ← → to step. The run restarts whenever you change the graph, the algorithm or the start vertex.
The three algorithms
Breadth-first search uses a queue and visits vertices in order of their distance in edges from the start. Depth-first search uses recursion (shown as its call stack) and follows each path as deep as it goes before backtracking. Both ignore weights and take time. The BFS vs DFS explainer compares them in detail.
Dijkstra’s algorithm finds shortest paths when edges have non-negative weights. It is BFS with the queue replaced by a priority queue keyed by tentative distance. Each round it settles the closest unsettled vertex, whose distance is then final, and relaxes its outgoing edges: if going through it gives a neighbour a shorter route than any known so far, the neighbour’s distance and parent are updated.
The visualiser uses the “lazy deletion” version that most real implementations use. A vertex whose distance improves is inserted into the queue again rather than updated in place, and outdated entries are skipped when they come out. Watch for “stale entry” in the narration.
Worked example: shortest paths from S on the road map
This is the preset the tool opens with. The final distances and the edge each shortest path arrives by are:
| Vertex | Distance from S | Shortest path |
|---|---|---|
| S | 0 | S |
| A | 5 | S → B → A |
| B | 2 | S → B |
| C | 9 | S → B → A → C |
| D | 10 | S → B → D |
| T | 13 | S → B → D → T |
Notice A. The direct edge S–A has weight 7, but S → B → A costs 2 + 3 = 5, so when B is settled the edge B–A is relaxed and A’s distance drops from 7 to 5. That improvement is the whole point of the algorithm.
Why Dijkstra needs non-negative weights
Settling a vertex is a promise that no shorter path to it will turn up later. That promise relies on every edge adding a non-negative amount: any other path must leave the settled region through a vertex that is at least as far away, and can only get longer from there. One negative edge breaks the argument, and Dijkstra can then report wrong distances. The visualiser warns when it sees a negative weight. For graphs with negative weights (but no negative cycles), use the Bellman–Ford algorithm, which takes time.
Running time
With a binary heap as the priority queue, each of the relaxations may push one entry, and each push or pop costs , so Dijkstra runs in time. With a Fibonacci heap the bound improves to , though binary heaps are usually faster in practice.
References
- E. W. Dijkstra, “A note on two problems in connexion with graphs”, Numerische Mathematik 1, 1959. doi:10.1007/BF01386390
- R. E. Tarjan, “Depth-first search and linear graph algorithms”, SIAM Journal on Computing 1(2), 1972. doi:10.1137/0201010
- “Dijkstra’s algorithm”, Wikipedia.