algo·visual

DFS (Grid)

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

Start at (7, 2): it is the only cell on the stack

1 / 270
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

Depth-first search keeps going in one direction (up first, then right, down and left) and only backtracks to the most recent unexplored cell on its stack when it hits a dead end. It always finds a path if one exists, but usually a long, winding one rather than the shortest.

Complexity

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

Search algorithms

Jump to any algorithm or page