Everyone learns BFS, Dijkstra and A* as pseudocode and then forgets the one thing that actually matters: how much of the graph each one has to look at. So I built a grid visualizer that runs all four side by side, and the "cells visited" counter makes the difference impossible to miss.
▶ Live demo: https://dev48v.github.io/pathfinding-visualizer/
Source: https://github.com/dev48v/pathfinding-visualizer
Draw walls, drop a maze, drag the start/target around, and watch the search.
The one number that tells the story
Run each algorithm on the same open grid and look at the cells visited:
| Algorithm | Cells visited | Path length | Shortest? |
|---|---|---|---|
| BFS | ~700 | 28 | ✅ |
| Dijkstra | ~700 | 28 | ✅ |
| A* | ~30 | 28 | ✅ |
| DFS | varies | long | ❌ |
BFS and A* return the same shortest path. But BFS looks at ~700 cells to find it and A* looks at ~30. That gap is the entire reason A* exists.
Why A* visits so much less
BFS/Dijkstra expand outward in every direction equally — they have no idea where the goal is, so they explore a growing disk until they happen to hit it. A* adds a heuristic: for each cell it estimates the remaining distance to the target (here, Manhattan distance) and always expands the cell with the best total estimate f = g + h:
-
g= actual cost to reach this cell so far -
h= estimated cost from here to the goal
Because h pulls the frontier toward the target, A* barely wanders. And as long as the heuristic never overestimates (Manhattan distance on a 4-connected grid never does — it's "admissible"), A* is still guaranteed to find the shortest path. Best of both worlds: Dijkstra's optimality, a fraction of the work.
The other two, briefly
- DFS dives as deep as it can down one branch before backtracking. It'll find a path if one exists, but it's usually long and winding — DFS makes no shortest-path promise. Watching it snake across a maze and come back with a bad path is the best way to internalize that.
- Dijkstra vs BFS: on this grid every step costs 1, so Dijkstra degenerates into BFS — same visited set, same path. The moment you add edge weights (think terrain, tolls, latency) they diverge, and that's exactly when you need Dijkstra instead of BFS.
How it's built
Each algorithm returns two things: the ordered list of cells it visited, and a cameFrom map. The visualizer replays the visit order (that's the expanding blue region), then walks cameFrom backward from the target to draw the shortest path in yellow. Keeping the algorithm and the animation separate means the visited counts are exact and "speed" is just how fast you replay the tape.
Generate a maze, run BFS, note the count, clear the path, run A* on the same maze, and compare. Once you've watched A* thread almost straight to the goal while BFS floods the whole grid, the heuristic clicks.
If it helped, a star helps others find it: https://github.com/dev48v/pathfinding-visualizer
Top comments (0)