Depth first search

Depth first search (DFS) is a useful method of traversing all nodes and edges of a graph. The DFS paradigm is:

    Search from node x
       while there exists an edge from x not yet considered do
          consider edge (x,y)
          if y has not yet been visited then
             visit y
             Search from node y

The order in which vertices are visited is the DFS order.   The depth first number of a node is that node's rank in the order.   The depth first order corresponds to a pre-order traversal of the trees in a spanning forest.

The following DFS includes (in red) statements enabling computation of the graph's connected components.

    DFS
       T := Ø   /** will be the edges in the spanning forest **/
       Q := Λ  /** queue of new nodes **/
                    /** implemented as doubly-linked list **/
       COUNT := 0
       COMP := 0
       forall vertices v do
          FLAG[v] := "new"
          add v to Q
       while there exists v s.t. FLAG[v] = "new" do
          COMP := COMP + 1
          SEARCH(v)

    SEARCH(v)
       COUNT := COUNT + 1
       DFN[v] := COUNT
       COMPNO[v] := COMP
       FLAG[v] := "old"
       delete v from Q
       Let L[v] be the list of vertices adjacent to v
       while L[v] ≠ Λ do
          w ← L[v]
          if FLAG[w] = "new" then
             T := T union {(v,w)}
             SEARCH(w)

Time analysis:

The time spent executing SEARCH(v) is proportional to the degree of vertex v.   SEARCH is called only once for each vertex.   Since the sum of the degrees of all the vertices adds up to twice the number of edges, therefore the total time spent by DFS is O(n + e).

Note that the algorithm partitions the graph's edge set into (1) edges in the DFS forest (i.e., T) and (2) other edges (which are termed back edges).

Lemma.   If (v,w) is a backedge then, in the DFS forest, v is an ancestor of w or vice-versa.

Proof.   Without loss of generality, v is visited before w.   That is to say, SEARCH(v) is called before SEARCH(w).   Then, when v is reached, w is still labeled "new".   The statement of the lemma follows from the facts that (1) all "new" vertices visited by SEARCH(v) will become descendents of v in the spanning forest, and (2) SEARCH(v) cannot end until w is reached because w is on the list L[v].

Notation for DFS forest

In drawing diagrams of DFS forests, newly visited nodes are placed to the right of other sons of their father.   As a result, if v is an ancestor of w or if v is to the left of w in the tree then the depth-first-number (DFN) of v is less than the DFN of w.


Dan Hirschberg
Last modified: Oct 28, 2003