- 40 Views
- Uploaded on
- Presentation posted in: General

Depth-First Search

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

G is completely traversed

before exploring G and G .

1

2

3

Idea: Keep going forward as long as there are unseen nodes

to be visited. Backtrack when stuck.

v

G

3

G

G

2

1

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

DFS(G)

time 0 // global variable

for eachv V(G) do

disc(v) unseen

for eachv V(G) do

if disc(v) = unseen then

DFS-visit(v)

DFS-visit(v)

time time + 1

disc(v) time

for eachu Adj(v) do

ifdisc(u) = unseen then

DFS-visit(u)

8

9

4

10

6

7

5

11

12

time = 1

a

h

i

c

d

e

j

f

k

l

b

g

2

3

Recursive DFS Calls

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)

1

a

c

d

e

4

6

7

5

f

2

3

b

g

8

9

h

i

10

j

k

l

DFS-visit(v) explores every unvisited vertex

reachable from v before it returns.

12

11

Depth-First Search Forest

a

h

i

c

d

e

j

f

k

l

b

g

Edges that, during DFS, lead to an unexplored vertex form a depth-first

search forest.

DFS-visit is called exactly once for each node.

|V| such calls in total.

Each call timestamps a node and then

increments the time, which takes O(1) time.

Each edge is examined O(1) time.

Running Time of DFS

O(|V| + |E|)

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

h

j

b

c

d

i

k

g

l

f

e

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

h

b

c

g

d

e

i

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.

…

Applications of DFS

InO(|V| + |E|)time, we can

a

b

c

v has not been explored at the time of the

initial call to DFS-visit(u).

v

v will be visited before returning from

DFS-visit(u).

u

Back Edge

TheoremA directed graph G has a cycle if and only if its DFS

forest has a back edge.

A back edge leads to a cycle.

Proof

Suppose there is a cycle. Let u be the

vertex with smallest time stamp on the

cycle and v be the predecessor of u in the cycle.

Therefore at the time of visiting v, a back edge (v, u) is found.

The above sufficient and necessary condition carries over to an undirected graph.

root

u

v

Algorithm for Detecting Cycle

(v, u) is a back edge if v is a descendant of u in the DFS tree.

for eachu Vdo

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 eachu Adj(v) do

ifdisc(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

Some topological sorts:

a

b

- a, c, e, b, d, g, f
- a, b, c, d, g, f, e
- b, d, g, a, c, f, e

d

c

e

f

g

Topological Sort of Digraph

Ordering < over V(G) such that u < v whenever (u, v) E(G).

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.

Intuition: Precedence Diagram

a

b

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).

If G has a cycle, then it cannot be topologically sorted.

Proof

If G has no cycle, then it can be topologically sorted.

Constructive proof: An algorithm that sorts any dag.

Courtesy: Dr. Fernandez-Baca

Initialize a global queue L within DFS(G)

Add a line to DFS-visit

Algorithm for Topological Sort

DFS-visit-topo(v)

time time + 1

disc(v) time

for eachu Adj(v) do

ifdisc(u) = unseen then

DFS-visit(u)

L insert(v, L)// insert v in the front of L

u

v

v

u

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.

Case 2:DFS-visit-topo(v) has already started, but not yet finished.

Then (u, v) is a back-edge and G has a

cycle, contradicting that it is a dag!

u

v

Correctness (cont’d)

Case 3: DFS-visit-topo(v) has not yet started.

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).

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, L is a

topological ordering of V(G).