algo·visual

BFS (Grid)

Graphs
avgO(V + E)worstO(V + E)spaceO(V)

Start at (7, 2): it is the only cell in the queue

1 / 376
Draw
Size

Drag on the grid to draw. Grab the start or end cell to move it. Edits re-run the search from step 1.

How it works

Breadth-first search spreads out from the start one ring at a time using a FIFO queue, so cells are reached in order of their distance in moves. The first time it dequeues the end it has found a shortest path, which it rebuilds by following each cell's prev link back to the start.

Complexity

Best
O(1)
Average
O(V + E)
Worst
O(V + E)
Space
O(V)

Search algorithms

Jump to any algorithm or page