BFS vs DFS vs Dijkstra vs A*: Watch the Difference
The four pathfinders explored on the same grid: rings vs deep dives vs weighted frontiers vs heuristic focus — visited-cell counts settle the argument.
Problem
BFS, DFS, Dijkstra, and A* all 'search a graph' — choosing the right one, and explaining why in an interview, requires knowing how their exploration actually differs.
What to do
Run all four on the same walls and the difference is visual, not theoretical: BFS floods in perfect rings and guarantees fewest hops; DFS snakes deep and guarantees nothing; Dijkstra's frontier respects weights, flowing around expensive sand; A* squeezes the same guarantee out of a fraction of the cells by aiming at the goal. The visited-cell counter after each run is the punchline — same maze, hundreds fewer cells for A*.
- 1Open the Pathfinding Visualizer, draw a few walls, and add some weighted sand cells.
- 2Run BFS and note the ring-shaped frontier and the visited count.
- 3Run Dijkstra on the same grid and watch it route around weights BFS ignored.
- 4Run A* last — same shortest path as Dijkstra, far fewer cells visited. That gap is the heuristic doing its work.
Use the browser tool
Pathfinding Visualizer lets you draw walls and weighted cells on a grid, then watch BFS, DFS, Dijkstra, A*, and Greedy Best-First explore it — the frontier floods outward, visited cells tint, and the shortest path draws itself when found. Comparing visit counts between runs shows exactly why A*'s heuristic matters.
People also ask
- Which algorithms guarantee the shortest path?
- BFS (for unweighted grids), Dijkstra, and A* with an admissible heuristic. DFS and Greedy Best-First trade that guarantee for speed or simplicity.
- What are the weighted cells?
- Cells that cost 5 to enter instead of 1 — sand instead of road. Dijkstra and A* route around them when it's cheaper; BFS ignores costs entirely.
- Does it work on a phone?
- Yes — drawing walls, dragging endpoints, and playback all work with touch.

