How the Maze Solver Works: Breadth-First Search Explained
Generate a maze in the maze solver and it draws the shortest route from start to finish — not just a route. The algorithm behind that guarantee is breadth-first search, one of the most dependable ideas in computer science: explore outward in rings of equal distance, and the first time you touch the goal you have necessarily arrived by the shortest possible path.
Breadth-First Search, in One Idea
Think of the start cell dropping a stone in water. BFS visits every cell one step away, then every cell two steps away, then three — a wave expanding evenly across the maze. It keeps a queue of cells to visit and, for each, remembers which neighbour it came from. Because it never looks at a distance-k+1 cell until every distance-k cell is done, the moment the wave reaches the finish, the recorded trail back to the start is a fewest-step path. Follow those "came-from" pointers backward and you have the route the solver highlights.
Why shortest is guaranteed. Every move in a maze costs the same one step, and BFS processes cells strictly in order of distance. So it is impossible to reach the exit by a longer path before a shorter one exists — the first arrival is always optimal. That even-cost property is exactly why BFS, not a fancier search, is the right tool for an unweighted maze.
How Much of the Maze It Explores
Guaranteeing the shortest path has a cost: the solver has to rule out shorter alternatives, so it explores a good chunk of the maze before it stops. We ran breadth-first search on random mazes and recorded the path length and how many cells it visited:
| Maze | Cells | Shortest path | Cells explored | Explored |
|---|---|---|---|---|
| 15×15 | 225 | 93 steps | 138 | 61% |
| 25×25 | 625 | 223 steps | 371 | 59% |
| 40×40 | 1,600 | 509 steps | 935 | 58% |
Averages over 200 random mazes per size. BFS visits roughly 60% of the cells to certify the path it returns is the shortest — and it scales linearly, so even large mazes solve instantly.
The full per-size sweep (open cells, shortest path, cells visited and the ~56–65% explored band) is published as the LK Forge Maze Solver Benchmark dataset on Hugging Face.
Reproduce It Yourself
BFS for a grid maze is a dozen lines — a queue, a visited set, and a table of where each cell was reached from:
function bfs(neighbors, start, goal) {
const queue = [start], cameFrom = new Map([[start, null]]);
while (queue.length) {
const cell = queue.shift();
if (cell === goal) break; // first arrival = shortest
for (const n of neighbors(cell))
if (!cameFrom.has(n)) { cameFrom.set(n, cell); queue.push(n); }
}
const path = []; // walk the trail back
for (let c = goal; c != null; c = cameFrom.get(c)) path.unshift(c);
return path;
}
Frequently Asked Questions
Is there a maze solver that finds the shortest path?
Yes — this page's maze solver is a free in-browser tool. Generate a maze or draw your own and it traces the shortest path from start to finish. No sign-up.
What algorithm does the maze solver use?
Breadth-first search. It explores the maze in rings of equal distance from the start, so the first time it reaches the finish it has arrived by a shortest route. Because every step costs the same, BFS is guaranteed to return a fewest-step path. Not a generative-AI or LLM wrapper.
Is the path it finds guaranteed to be the shortest?
Yes — BFS visits cells in order of distance from the start, so it cannot reach the finish by a longer route before a shorter one. Any path it returns has the minimum number of steps.
How much of the maze does it explore?
Roughly 60% of all cells — about 138 of 225 on a 15×15 maze, 935 of 1,600 on a 40×40 — because it must rule out shorter alternatives to be certain the path it found is shortest.
Is the maze solver free?
Completely free, no account required. It runs client-side in your browser, so nothing you enter is uploaded.