Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Chapter 9 GRAPH

DFS algorithm Its applications

Depth-first traversal
<void> DFS() Traverses the digraph in depth-first order
Uses 1.

Auxiliary function DFSVisit()

loop( more vertex v in digraph) 1. color[v] WHITE 2. father[v] null 2. time 0 3. loop( more vertex v in digraph) 1. if (color[v]= WHITE) 1. DFSVisit(v)

end DFS

<void> DFSVisit(vertex v) 1. color[v] GRAY 2. time time +1 3. d[v] time 4. loop (more vertex u adjacent to v) 1. if (color[u]= WHITE) 1. father[u] v 2. DFSVisit(u) 5. color[v] BLACK 6. time time +1 7. f[v] time

end DFSVisit

Depth-first traversal
<void> DFS() Traverses the digraph in depth-first order
Uses 1. <void> DFSVisit(vertex v) Auxiliary function DFSVisit() 1. 2. 3. 4. color[v] GRAY time time +1 d[v] time loop (more vertex u adjacent to v) 1. if (color[u]= WHITE) 1. father[u] v 2. DFSVisit(u) 5. color[v] BLACK 6. time time +1 7. f[v] time

loop( more vertex v in digraph) 1. color[v] WHITE 2. father[v] null 2. time 0 3. loop( more vertex v in digraph) 1. if (color[v]= WHITE) 1. DFSVisit(v)

end DFS

end DFSVisit
4

How to modify DFS algorithm to


a) determine whether there exists a cycle in graph b) calculate number of connected components c) determine topological order d) identify articulation points

You might also like