Depth-First Search

1/0
1.0x

Depth-First Search

O(V + E)Space: O(V)

Pseudocode

1procedure DFS(graph, start)
2 create stack S
3 push start onto S
4 while S is not empty do
5 v ← pop from S
6 if v is not visited then
7 mark v as visited
8 process v
9 for each neighbor u of v do
10 if u is not visited then
11 push u onto S