Professional Documents
Culture Documents
Depth-First Search
A
B D E
Spanning subgraph
component of a
graph G is a maximal
connected subgraph
of G
Non connected graph with two
connected components
components of a forest
are trees
Forest
Spanning tree
2014 Goodrich, Tamassia, Goldwasser
Depth-First Search 5
Depth-First Search
Depth-first search (DFS) DFS on a graph with n
is a general technique vertices and m edges
for traversing a graph takes O(nm ) time
A DFS traversal of a DFS can be further
A A
B D E B D E
C C
B D E B D E
C C
A A
B D E B D E
C C
DFS-Iterative(G,v):
let S be a stack
S.push(v)
while S is not empty
v = S.pop()
if v is not labeled as discovered:
label v as discovered
For edges from v to w G.adjacentEdges(v) do
S.push(w)