You are on page 1of 15

Lecture 8: DFS and Topological Sort

CLRS 22.3, 22.4 Outline of this Lecture

Recalling Depth First Search.
       

The time-stamp structure.

Using the DFS for cycle detection.

Topological sort with the DFS.

1

What does DFS Do? Given a digraph tices of and
  © ¨¦¤¢ § ¥ £ ¡ 6 7©   ¥

, DFS traverses all ver-

constructs a forest, together with a set of source vertices; and
 

8

where DFS calls are made

$

DFS Forest: DFS constructs a forest collection of trees, where 
   4 2 0 ¢ 531)( ¡ & §

© '& ¨%¤¢ § ¥ £ 

    #"!  ¡

outputs two time unit arrays,
 

.

,a

2

A Depth First Search Example Example
d b a e
2/5

d b
1/10

11/14

root

e 12/13
6/9

f

a
root

f
7/8

g c original graph c 3/4

g Two source vertices a, d

Question: What can we do with the returned data?

3

Classification of the Edges of a Digraph Tree edges: those on the DFS forest. The remaining edges fall in three categories:
© ¥ ¡¢   ¡ © © 
 

Forward edges: where is a proper descen.] dent of in the tree. [ 

¢¡  

Back edges: , where vertex is an ancestor of in the tree. [ is allowed.] 

 

Cross edges: where and are not ancestors or descendents of one another (in fact, the edges may go between different trees).   

¥ ¡¢   

¥ ¡¢  

 

 

4

Example of the Classification of Edges
d b a e
2/5
11/14

d C
1/10

root

e 12/13
6/9

C C a
root

b f

f
7/8

F g c original graph c 3/4 C

B g

C: cross, F: forward, B: back edge

Remark: Since the forest obtained with the DFS is not unique, the classification is not unique.

5

Time-Stamp Structure in DFS There is also a nice structure to the time stamps, which is referred to as Parenthesis Structure. Lemma 1 Given a digraph est for , and any two vertices
 
 

is a descendent of in the DFS forest if and only if is a subinterval of : 
  #"      ¥      

                 ¥ 

       

¡ £

¡  £ ¡ £    

is unrelated to and or

in the DFS forest if and only if are disjoint intervals:

and are not possible
6

£

© § ¦¤¢ ¥ £

  

¥  

¡

 

, any DFS For,

¡ £ 

                   ¥    

¡ £

¡£  ¢#"  ¡   ¡   ¡  £   ¢    

#"                 ¥  

¡ £

¡   ¢#"
 
   

 

Cycles in digraphs: Applications of DFS Claim: Given a digraph, DFS can be used to determine whether it contains any cycles. Lemma 2: Given a digraph , and any DFS tree of , tree edges, forward edges, and cross edges all go from a vertex of higher finish time to a vertex of lower finish time. Back edges go from a vertex of lower finish time to a vertex of higher finish time. Proof: The conclusions follow from Lemma 1.
   

d b a

e
2/5

d C
1/10

11/14

root

e 12/13
6/9

C C a
root

b f

f
7/8

F g c original graph c 3/4 C

B g

C: cross, F: forward, B: back edge

7

When Is a Digraph Acyclic Lemma 3: A digraph is acyclic if and only if any DFS forest of yields no back edges. Example:
d b a e
2/5
 

d C
1/10

11/14

root

e 12/13
6/9

C C a
root

b f

f
7/8

F g c original graph c 3/4 C

B g

C: cross, F: forward, B: back edge

8

When Is a Digraph Acyclic Lemma 3: A digraph is acyclic if and only if any DFS forest of yields no back edges. Proof of : Suppose there are no back edges. By Lemma 2, all edges go from a vertex of higher finish time to a vertex of lower finish time. Hence there can be no cycles.
 

 

9

When Is a Digraph Acyclic Lemma 3: A digraph is acyclic (a DAG) if and only if any DFS forest of yields no back edges. Proof of : Assume that has no cycles. We prove that has no back edges by contradiction. Suppose there is a back edge . Then is an ancestor of in a rooted DFS tree. There is a path in the DFS tree. The back edge + the path gives a cycle. A contradiction!
v back edge u 

   
¡ ¢ ¢ ¤££¢ ¡

©

¥ ¡¢  

 

  

  

 

10

Cycle Detection with the DFS Cycle detection: becomes back edge detection by Lemma 3! Problem: Modify the DFS algorithm slightly to give an algorithm for cycle detection. This can always be done by first running the algorithm and values and then runand assigning the ning through all of the edges one more time, seeing if any of them are back edges. This would take time for the DFS and time for the scan through all of the edges. In total, this uses time. How could you solve this problem online by identifying back edges while the DFS algorithm is still running?
© 4
£ ¡ ¤¢¢  

© 4

£

¡ ¢¢

  

  

© 4 ¢

  

 

11

Topological Sorting; graphs If is a DAG then a topological sorting of is a linear ordering of such that for each edge in the DAG, appears before in the linear ordering. and have Example: Let if and only if . This is partial order, but not a linear one. There are several topological sortings of (how many?), for example:
¢ 

   ¤ #¥ "¥ ¨ ¥ £¥ £¥    ¥ !©¥ ¥ ¨ ¥ ¥ ¥    ¤¥ ©¥ §¥ ¥¥ §¥    ¢ ¦  ¦    ¢ ¤ ¥     ¨ ¦ ¤ ¢

12

©

£ 

¥ ¡¢  

 

8 

6     ¨ ¦ ¤ ¢   ¥ ©¥ §¥ ¥¥ £¥ ¡( 

£

¡

£

© § %¤¢ ¥ £
 

§

 

©

¡ 

 £¥ ¥¢

 

Topological Sorting; graphs If is a DAG then a topological sorting of is a linear ordering of such that for each edge in the DAG, appears before in the linear ordering. Idea of Topological Sorting: Run the DFS on the DAG and output the vertices in reverse order of finishing time. Correctness of the Idea: By lemma 2, for every edge in a DAG, the finishing time of is greater than that of , as there are no back edges and the remaining three classes of edges have this property.
 

13

©

£ 

¥ ¡¢   

£

© § %¤¢ ¥ £
  

©

¡ 

¥ ¡¢  

 

Topological Sort: the Algorithm The Algorithm:

2. As each vertex is finished, insert it onto the front of a list;

3. Output the list
© 4
£ ¡ ¢¢

 

Running time:

, the same as DFS. 

  

1. Run DFS(G), computing finish time vertex ; 

for each

14

Topological Sort: Example
a c e b d f g h a 1/10 c 2/9

e

3/6

d 7/8

h 4/5

Original graph

DFS forest  

Final order:

.

¡ ¤¢ ¢£  ¦

¡ ¤¥ ¥£  

$¥ #

4 ¨¥

¡ ¤¢ ¢ £  

 ¡  ¤¢ ¢£  ¦

¥ "¥ !

¡ ¤¢ ¢£  ¡ ¤¢ ¢£  ¦

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

f 11/14 b 15/16 g 12/13

¡ ¤¢ ¢£ 

¡ ¤¢ ¢£  

¥¥ 

¥

¡¢ ¤¢£    ¦  ¤¡¢  ¦¢£    ¡ ¢ ¡  ¢£  ¤¢£  ¤¢ ¦ ¡ ¡ ¤¢© £  § ¤¢  ¦¢¨ ¨ ¨ ¢£ ¦ ¨ ¨ ¢  ¤¡ ¢£
¥

¡ ¤¢ ¢£  

15