What is Breadth-First Pathfinding?
Spread out from the start ring by ring to find the shortest path on a grid.
Type a tool name, format or task
Spread out from the start ring by ring to find the shortest path on a grid. Interactive step-by-step animation with JavaScript code, complexity and front-end use cases.
Loading animation...
Spread out from the start ring by ring to find the shortest path on a grid. Put the start cell in a queue. Take a cell, mark it explored and add its unvisited neighbours. Stop when the goal is taken; follow the recorded parents back to build the path. Time complexity: O(cells). Space complexity: O(cells). Games, maze and route features, link graphs (shortest click path) and any shortest-path problem with equal steps.
Spread out from the start ring by ring to find the shortest path on a grid.
Time: O(cells). Space: O(cells).
Games, maze and route features, link graphs (shortest click path) and any shortest-path problem with equal steps.
No. This converter runs in your browser for ordinary use, so the input is not sent to a backend by this tool.
Keep working without searching again.
Breadth-first search spreads out from the start one ring at a time, so the first time it reaches the goal it has found a shortest path.
function bfs(grid, start, end) {const queue = [start];const prev = new Map([[key(start), null]]);while (queue.length) {const cur = queue.shift();if (same(cur, end)) return buildPath(prev, cur);for (const next of neighbours(grid, cur)) {if (prev.has(key(next))) continue;prev.set(key(next), cur);queue.push(next);}}return null;}
Tip: use the arrow keys to step and the space bar to play or pause.