0% found this document useful (0 votes)
2 views

DFS

This lecture covers the depth-first search (DFS) algorithm, which traverses vertices of a graph by exploring as far as possible along each branch before backtracking. It constructs a forest of trees and outputs discovery and finish times for each vertex. The running time analysis indicates that DFS operates efficiently with respect to the number of vertices and edges in the graph.

Uploaded by

f20220831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DFS

This lecture covers the depth-first search (DFS) algorithm, which traverses vertices of a graph by exploring as far as possible along each branch before backtracking. It constructs a forest of trees and outputs discovery and finish times for each vertex. The running time analysis indicates that DFS operates efficiently with respect to the number of vertices and edges in the graph.

Uploaded by

f20220831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lecture 7: Depth-First Search

CLRS, section 22.3

Outline of this Lecture

The depth-first search algorithm.


Given for digraphs but can easily be modified for
undirected graphs

Running time analysis of DFS.

1
Background

Graph Traversal Algorithms: Graph traversal algo-


rithms visit the vertices of a graph, according to some
strategy.

Example: The BFS is an example of a graph traversal


algorithm that divides graph up into connected com-
ponents and traverses each component separately. It
traverses the vertices of each component in increas-
ing order of the distances of the vertices from the ‘root’
of the component.
Can be thought of us processing ‘wide’ and then ‘deep’.

DFS will process the vertices first deep and then wide.
After processing a vertex it recursively processes all
of its descendants

2
DFS
 
Graph is . The algorithm works in discrete
time steps. Each vertex is given a “discovery” time
 when it is first processed and a “finish” time,  
when all of its descendants are finished.

The output is a collection of trees. As well as 


and   each node points to   , its parent in
the forest.

Example:

11/14
d e d e 12/13
root
2/5
b a b 1/10
6/9
f a f
root

7/8
g g
c c 3/4
original graph
Two source vertices a, d
3
What Does DFS Do

 
Given a digraph , it traverses all vertices
of and

constructs a forest (a collection of rooted trees),


together with a set of source vertices (the roots);
and

outputs two arrays,    , the two time units.

Note: Forest is stored in   array with  


pointing to parent of in the forest.    of a root
node is Nil.

4
The DFS Forest

   
DFS Forest: DFS creates a forest , a
collection of rooted trees, where
      
 where DFS calls are made

11/14
d e d e 12/13
root
2/5
b a b 1/10
6/9
f a f
root

7/8
g g
c c 3/4
original graph
Two source vertices a, d

5
Idea of the DFS Algorithm

In DFS, edges are explored out of the most recently dis-


covered vertex . Only edges to unexplored vertices are


explored.

When all of ’s edges have been explored, the search “back-




tracks” to explore edges leaving the vertex from which 

was discovered.

The process continues until we have discovered all the ver-


tices that are reachable from the original source vertex.

If any undiscovered vertices remain, then one of them is se-


lected as a new source vertex, and the search is repeated
from that source vertex.

This process is repeated until all vertices are discovered.

The strategy of the DFS is to search “deeper” in the graph when-


ever possible.

6
Idea of the DFS Algorithm by Example

Apply the Idea to the Following Graph:

11/14
d e d e 12/13
root
2/5
b a b 1/10
6/9
f a f
root

7/8
g g
c c 3/4
original graph
Two source vertices a, d

7
Four Arrays for the DFS Algorithm

To record data gathered during traversal.

  
 , the color of each vertex visited: white
means undiscovered, gray means discovered but
not finished processing, and black means finished
processing.

 
 , the predecessor pointer, pointing back to

the vertex that discovered .


 , the discovery time, a counter indicating when

vertex is discovered.


  , the finishing time, a counter indicating when

the processing of vertex (and all its descen-
dants ) is finished.

8
Depth-First Search (DFS) Algorithm

DFS(G) // Main program


for each u in V // Initialize
color[u]=W; pred[u]=NIL; 

time=0;
for each u in V
if(color[u]==W) DFSVisit(u); // Start new tree


DFSVisit(u) // Process vertex u


color[u]=G; // Vertex discovered
d[u]=++time; // Time of discovery
for each v in adj[u]
if(color[v]==W) // Visit undiscovered
pred[v]=u; DFSVisit(v);  // neighbours
color[u]=B; // Vertex finished
f[u]=++time; // Time of finish


9
Running Time Analysis of DFS

DFS(G)
for each u in V
color[u]=W; pred[u]=NIL;  
time=0; 
for each u in V
if(color[u]==W) DFSVisit(u);  (tests)
 Sum:   

DFSVisit(u)  (call)
color[u]=G; 
d[u]=++time;
for each v in adj[u]
if(color[v]==W) outdeg(u) (tests)
pred[v]=u; DFSVisit(v);   outdeg(u)
color[u]=B; 
f[u]=++time;
   
 Sum:   outdeg      outdeg

10
Running Time Analysis of DFS – Continued

The total running time is



    


We have
    
    outdeg   
 
and
    
    outdeg    
 
Hence
       
         
      
          

     
Therefore     .

11
Questions about DFS

Question: Is the DFS forest unique? Why?

Question: How do you prove that the DFS traverses


over all vertices of ?

Question: Does the DFS algorithm work for undi-


rected graphs?

12
On-line Example of DFS

Task: Apply the DFS to this digraph and compare


whether your DFS forests are the same! What about
the two arrays  and   .

a b

c d

e f

13

You might also like