Layer-by-layer traversal that guarantees shortest paths in unweighted graphs.
Easy Explanation
BFS explores neighbors level by level, so the first time it reaches a target is the shortest unweighted path.
Renderer Mode
`Simple` uses abstract, short-form-friendly visuals. `Advanced` keeps the current detailed view.
Grid
n/a
Visited
0
Distance
n/a
Result
Searching
Safe mode: choose a tool to edit the grid.
Press Play or Step to start execution.
function bfs(graph, start, target):
queue <- [start]
visited <- { start }
parent[start] <- none
while queue is not empty:
node <- pop_front(queue)
if node == target:
return reconstruct_path(parent, target)
for neighbor in neighbors(node):
if neighbor not in visited:
visited.add(neighbor)
parent[neighbor] <- node
push_back(queue, neighbor)
return no path