Download presentation
Presentation is loading. Please wait.
Published byChad Douglas Modified over 9 years ago
1
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G 1 2 3 G is completely traversed before exploring G and G. 1 23 From Computer Algorithms by S. Baase and A. van Gelder
2
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) time time + 1 disc(v) time for each u Adj(v) do if disc(u) = unseen then DFS-visit(u)
3
A DFS Example a l g f b ced j k ih time = 1 2 3 4 5 67 89 10 1112
4
Recursive DFS Calls a g f b ced 1 2 3 4 5 6 7 l j k ih 89 10 11 12 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.
5
Depth-First Search Forest a l g f b ced j k ih Edges that, during DFS, lead to an unexplored vertex form a depth-first search forest.
6
Running Time of DFS (|V| + |E|) DFS-visit is called exactly once for each node. Each edge is examined O(1) time. |V| such calls in total. Each call timestamps a node and then increments the time, which takes O(1) time.
7
Edge Classification – Undirected Graphs 1. Tree edges are those in the DFS forest. 2. Back edges go from a vertex to one of its ancestors. a b ef g dc i h l k j
8
Edge Classification – Directed Graphs Besides tree edges and back edges, there are also 3. Forward edges go from a vertex to one of its descendants. 4. Cross edges: all other edges. a bc de g i h
9
Applications of DFS In O(|V| + |E|) time, we can Find connected components of G. Determine if G has a cycle. Determine if removing a vertex or edge will disconnect G. Determine if G is planar. …
10
Back Edge Theorem A directed graph G has a cycle if and only if its DFS forest has a back edge. Proof A back edge leads to a cycle. a b c Suppose there is a cycle. Let u be the vertex with the smallest time stamp on the cycle and v be the predecessor of u in the cycle. v has not been explored at the time of the initial call to DFS-visit(u). v will be visited before returning from DFS-visit(u). Therefore at the time of visiting v, a back edge (v, u) will be found. v u The theorem also applies to an undirected graph.
11
Algorithm for Detecting Cycle (v, u) is a back edge if v is a descendant of u in the DFS tree. for each u V do onpath(u) false // on path from the root of the DFS tree DFS-visit(v) time time + 1 disc(v) time onpath(v) true for each u Adj(v) do if disc(u) = unseen then DFS-visit(u) else if onpath(u) then a cycle has been found; halt onpath(v) false // backtrack: v no longer on path from root u v root
12
Topological Sort of Digraphs Ordering < over V(G) such that u < v whenever (u, v) E(G). a c ef b d g Some topological sorts: 1.a, c, e, b, d, g, f 2.a, b, c, d, g, f, e 3.b, d, g, a, c, f, e
13
Intuition: Precedence Diagram Each node represents an activity; e.g., taking a class. (u, v) E(G) implies activity u must be scheduled before activity v. Topological sort schedules all activities. More than one schedule may exist.
14
Existence of Topological Sort Lemma G can be topologically sorted iff it has no cycle, that is, iff it is a dag (directed acyclic graph). Proof If G has a cycle, then it cannot be topologically sorted. ab If G has no cycle, then it can be topologically sorted. Constructive proof: An algorithm that sorts any dag.
15
Algorithm for Topological Sort Initialize a global queue L within DFS(G) Add a line to DFS-visit DFS-visit-topo(v) time time + 1 disc(v) time for each u Adj(v) do if disc(u) = unseen then DFS-visit-topo(u) L insert(v, L) // insert v in the front of L a c ef b d g
16
Correctness of the Algorithm Claim Let G be a directed acyclic graph (dag). If (u, v) E(G), then DFS-visit-topo(u) finishes after DFS-visit-topo(v). Proof Consider the time when DFS-visit-topo(u) first scans (u, v): Case 1: DFS-visit-topo(v) has already finished. Obviously, DFS-visit-topo(u) finishes afterwards. And (u, v) is a cross edge. u v Case 2: DFS-visit-topo(v) has already started, but not yet finished. u v Then (u, v) is a back-edge and G has a cycle, contradicting that it is a dag!
17
Correctness (cont’d) Case 3: DFS-visit-topo(v) has not yet started. u v Then the procedure call will start immediately. So (u, v) is a tree edge. Hence DFS-visit-topo(u) will finish after DFS-visit-topo(v). Theorem If G is a dag, then at termination of DFS, L is a topological ordering of V(G). Combining cases 1 and 3, u will always be inserted in front of v in the queue L. Courtesy: Dr. Fernandez-Baca
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.