5 Depth First Search

Given a graph G = V , E G=(V,E) and a vertex r V G r\in V(G) , a depth-first search finds all the vertices v V G v\in V(G) for which there is a path from r r to v v . In other words, a depth-first search finds the connected component of G G that contains r r . It does so by starting at r r and systematically searching G G until no more vertices can be reached. After a vertex is reached by the search, it is referred to as visited. A vertex is explored after all adjacent vertices are visited. The vertex r r that anchors the search is referred to as its root.

When a depth-first search reaches vertex v v , it immediately suspends its exploration of v v and explores any unvisited vertex w w adjacent to v v . The exploration of v v resumes only after the exploration of w w has finished. The order in which a depth-first search visits a graph G G is a generalization of the order in which a preorder traversal visits the vertices of a tree. If G G is a tree, a depth-first search produces a preorder traversal of G G .

Depth First Search Topics