ITCS 6114. Graph Algorithms . DepthFirst Search. Depthfirst search is another strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the most recently discovered vertex v that still has unexplored edges
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.
26/7/2014
36/7/2014
{
for each vertex u G>V
{
u>color = WHITE;
}
time = 0;
for each vertex u G>V
{
if (u>color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u>color = GREY;
time = time+1;
u>d = time;
for each v u>Adj[]
{
if (v>color == WHITE)
DFS_Visit(v);
}
u>color = BLACK;
time = time+1;
u>f = time;
}
DepthFirst Search: The Code46/7/2014
{
for each vertex u G>V
{
u>color = WHITE;
}
time = 0;
for each vertex u G>V
{
if (u>color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u>color = GREY;
time = time+1;
u>d = time;
for each v u>Adj[]
{
if (v>color == WHITE)
DFS_Visit(v);
}
u>color = BLACK;
time = time+1;
u>f = time;
}
DepthFirst Search: The CodeWhat does u>d represent?
56/7/2014
{
for each vertex u G>V
{
u>color = WHITE;
}
time = 0;
for each vertex u G>V
{
if (u>color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u>color = GREY;
time = time+1;
u>d = time;
for each v u>Adj[]
{
if (v>color == WHITE)
DFS_Visit(v);
}
u>color = BLACK;
time = time+1;
u>f = time;
}
DepthFirst Search: The CodeWhat does u>f represent?
66/7/2014
{
for each vertex u G>V
{
u>color = WHITE;
}
time = 0;
for each vertex u G>V
{
if (u>color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u>color = GREY;
time = time+1;
u>d = time;
for each v u>Adj[]
{
if (v>color == WHITE)
DFS_Visit(v);
}
u>color = BLACK;
time = time+1;
u>f = time;
}
DepthFirst Search: The CodeWill all vertices eventually be colored black?
76/7/2014
{
for each vertex u G>V
{
u>color = WHITE;
}
time = 0;
for each vertex u G>V
{
if (u>color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u>color = GREY;
time = time+1;
u>d = time;
for each v u>Adj[]
{
if (v>color == WHITE)
DFS_Visit(v);
}
u>color = BLACK;
time = time+1;
u>f = time;
}
DepthFirst Search: The CodeWhat will be the running time?
86/7/2014
{
for each vertex u G>V
{
u>color = WHITE;
}
time = 0;
for each vertex u G>V
{
if (u>color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u>color = GREY;
time = time+1;
u>d = time;
for each v u>Adj[]
{
if (v>color == WHITE)
DFS_Visit(v);
}
u>color = BLACK;
time = time+1;
u>f = time;
}
DepthFirst Search: The CodeRunning time: O(n2) because call DFS_Visit on each vertex, and the loop over Adj[] can run as many as V times
96/7/2014
{
for each vertex u G>V
{
u>color = WHITE;
}
time = 0;
for each vertex u G>V
{
if (u>color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u>color = GREY;
time = time+1;
u>d = time;
for each v u>Adj[]
{
if (v>color == WHITE)
DFS_Visit(v);
}
u>color = BLACK;
time = time+1;
u>f = time;
}
DepthFirst Search: The CodeBUT, there is actually a tighter bound. How many times will DFS_Visit() actually be called?
106/7/2014
{
for each vertex u G>V
{
u>color = WHITE;
}
time = 0;
for each vertex u G>V
{
if (u>color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u>color = GREY;
time = time+1;
u>d = time;
for each v u>Adj[]
{
if (v>color == WHITE)
DFS_Visit(v);
}
u>color = BLACK;
time = time+1;
u>f = time;
}
DepthFirst Search: The CodeSo, running time of DFS = O(V+E)
116/7/2014
126/7/2014
sourcevertex
d f
1 
8 

2  7
9 
3  4
5  6

What is the structure of the grey vertices? What do they represent?
226/7/2014
306/7/2014
326/7/2014
346/7/2014
sourcevertex
d f
1 12
8 11
1316
2  7
9 10
3  4
5  6
1415
Tree edges
Back edges
Forward edges
356/7/2014
366/7/2014
sourcevertex
d f
1 12
8 11
1316
2  7
9 10
3  4
5  6
1415
Tree edges
Back edges
Forward edges
Cross edges
376/7/2014
386/7/2014
source
F?
396/7/2014
source
C?
406/7/2014
416/7/2014
DFS(G)
{
for each vertex u G>V
{
u>color = WHITE;
}
time = 0;
for each vertex u G>V
{
if (u>color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u>color = GREY;
time = time+1;
u>d = time;
for each v u>Adj[]
{
if (v>color == WHITE)
DFS_Visit(v);
}
u>color = BLACK;
time = time+1;
u>f = time;
}
426/7/2014
DFS(G)
{
for each vertex u G>V
{
u>color = WHITE;
}
time = 0;
for each vertex u G>V
{
if (u>color == WHITE)
DFS_Visit(u);
}
}
DFS_Visit(u)
{
u>color = GREY;
time = time+1;
u>d = time;
for each v u>Adj[]
{
if (v>color == WHITE)
DFS_Visit(v);
}
u>color = BLACK;
time = time+1;
u>f = time;
}
436/7/2014
446/7/2014