CSC 413/513: Intro to Algorithms. Graph Algorithms DFS. 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.
{
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 Code{
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?
{
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?
{
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?
sourcevertex
sourcevertex
d f
1 
8 

2  7
9 
3  4
5  6

What is the structure of the grey vertices? What do they represent?
{
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?
{
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
{
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?
sourcevertex
d f
1 12
8 11
1316
2  7
9 10
3  4
5  6
1415
Tree edges
Back edges
Forward edges
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
source
F?
source
C?
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;
}
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);
}
}
If v>color == GREY and Pred(u)≠v, …
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;
}