algo·visual

DFS (Graph)

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

Start the depth-first search at A

1 / 38
Nodes8
Source
Edges

How it works

Depth-first search follows one branch as deep as it can before backing up to the most recent node that still has an unexplored neighbour. The recursion's call stack remembers the way back, and the edges that lead to new nodes form a depth-first tree.

Complexity

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

Search algorithms

Jump to any algorithm or page