# Depth-First Search

Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck.

G1 is completely traversed before exploring G2 and G 3 .

v G3

G1

G2

From Computer Algorithms by S. Baase and A. van Gelder

The DFS Algorithm DFS(G) time  0 // global variable for each v  V(G) do disc(v)  unseen for each v  V(G) do if disc(v) = unseen then DFS-visit(v) DFS-visit(v) time  time + 1 disc(v)  time for each u  Adj(v) do if disc(u) = unseen then DFS-visit(u) .

A DFS Example time = 1 8 4 9 a c 6 h 10 i d 7 5 e j g 3 f b 2 k 11 l 12 .

k 11 l 12 .Recursive DFS Calls 1 a 6 2b 8 9 c f d 7 5 e 4 g 3 h 10 i j DFS(G) DFS-visit(a) DFS-visit(b) DFS-visit(g) DFS-visit(e) DFS-visit(f) DFS-visit(c) DFS-visit(d) DFS-visit(h) DFS-visit(i) DFS-visit(j) DFS-visit(k) DFS-visit(l) DFS-visit(v) explores every unvisited vertex reachable from v before it returns.

Depth-First Search Forest a c d e h i j f b g k l Edges that. during DFS. . lead to an unexplored vertex form a depth-first search forest.

which takes O(1) time. . |V| such calls in total. Each edge is examined O(1) time.Running Time of DFS (|V| + |E|) DFS-visit is called exactly once for each node. Each call timestamps a node and then increments the time.

a b c d i k h j g l f e . 2. Back edges go from a vertex to one of its ancestors. Tree edges are those in the DFS forest.Edge Classification – Undirected Graphs 1.

there are also 3.Edge Classification – Directed Graphs Besides tree edges and back edges. Cross edges: all other edges. Forward edges go from a vertex to one of its descendants. 4. a b c g d e i h .

Applications of DFS In O(|V| + |E|) time. Determine if G has a cycle. … . we can Find connected components of G. Determine if removing a vertex or edge will disconnect G. Determine if G is planar.

.Back Edge Theorem A directed graph G has a cycle if and only if its DFS forest has a back edge. Let u be the vertex with the smallest time stamp on the cycle and v be the predecessor of u in the cycle. u) will be found. b c  Suppose there is a cycle. v has not been explored at the time of the initial call to DFS-visit(u). v u Therefore at the time of visiting v. a back edge (v. The theorem also applies to an undirected graph. v will be visited before returning from DFS-visit(u). a Proof  A back edge leads to a cycle.

u) is a back edge if v is a descendant of u in the DFS tree.Algorithm for Detecting Cycle (v. for each u  V do onpath(u)  false // on path from the root of the DFS tree DFS-visit(v) root time  time + 1 disc(v)  time onpath(v)  true u for each u  Adj(v) do if disc(u) = unseen then DFS-visit(u) else if onpath(u) then v a cycle has been found. halt onpath(v)  false // backtrack: v no longer on path from root .

e. v)  E(G). a. b. c. f. d. f. g.Topological Sort of Digraphs Ordering < over V(G) such that u < v whenever (u. b. c. e c e f d g . a b Some topological sorts: 1. d. c. b. a. g. e 3. f 2. d. g. a.

Topological sort schedules all activities. taking a class. v)  E(G) implies activity u must be scheduled before activity v.. e. (u. More than one schedule may exist.g. .Intuition: Precedence Diagram Each node represents an activity.

a b  If G has no cycle. iff it is a dag (directed acyclic graph). Proof  If G has a cycle. then it can be topologically sorted. that is. .Existence of Topological Sort Lemma G can be topologically sorted iff it has no cycle. then it cannot be topologically sorted. Constructive proof: An algorithm that sorts any dag.

L) // insert v in the front of L d g .Algorithm for Topological Sort Initialize a global queue L    within DFS(G) Add a line to DFS-visit a b DFS-visit-topo(v) time  time + 1 c disc(v)  time for each u  Adj(v) do if disc(u) = unseen then e f DFS-visit-topo(u) L  insert(v.

but not yet finished. contradicting that it is a dag! . If (u. v): Case 1: DFS-visit-topo(v) has already finished.Correctness of the Algorithm Claim Let G be a directed acyclic graph (dag). Proof Consider the time when DFS-visit-topo(u) first scans (u. DFS-visit-topo(u) finishes afterwards. u v Obviously. v)  E(G). v) is a cross edge. Case 2: DFS-visit-topo(v) has already started. v) is a back-edge and G has a cycle. then DFS-visit-topo(u) finishes after DFS-visit-topo(v). And (u. v u Then (u.

v) is a tree edge. u Then the procedure call will start immediately. So (u. Hence DFS-visit-topo(u) will finish after DFS-visit-topo(v). Fernandez-Baca .Correctness (cont’d) Case 3: DFS-visit-topo(v) has not yet started. L is a topological ordering of V(G). v Combining cases 1 and 3. u will always be inserted in front of v in the queue L. Theorem If G is a dag. then at termination of DFS. Courtesy: Dr.