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.
CSC 413/513: Intro to Algorithms
Graph Algorithms
DFS
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;
}
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;
}
What does u>d represent?
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;
}
What does u>f represent?
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;
}
Will all vertices eventually be colored black?
sourcevertex
sourcevertex
d f
1 







sourcevertex
d f
1 


2 




sourcevertex
d f
1 


2 

3 


sourcevertex
d f
1 


2 

3  4


sourcevertex
d f
1 


2 

3  4
5 

sourcevertex
d f
1 


2 

3  4
5  6

sourcevertex
d f
1 
8 

2  7

3  4
5  6

sourcevertex
d f
1 
8 

2  7

3  4
5  6

sourcevertex
d f
1 
8 

2  7
9 
3  4
5  6

What is the structure of the grey vertices? What do they represent?
sourcevertex
d f
1 
8 

2  7
9 10
3  4
5  6

sourcevertex
d f
1 
8 11

2  7
9 10
3  4
5  6

sourcevertex
d f
1 12
8 11

2  7
9 10
3  4
5  6

sourcevertex
d f
1 12
8 11
13
2  7
9 10
3  4
5  6

sourcevertex
d f
1 12
8 11
13
2  7
9 10
3  4
5  6
14
sourcevertex
d f
1 12
8 11
13
2  7
9 10
3  4
5  6
1415
sourcevertex
d f
1 12
8 11
1316
2  7
9 10
3  4
5  6
1415
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;
}
What will be the running 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);
}
}
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;
}
Running time: O(n2) because call DFS_Visit on each vertex, and the loop over Adj[] can run as many as V times
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;
}
BUT, 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
sourcevertex
d f
1 12
8 11
1316
2  7
9 10
3  4
5  6
1415
Tree edges
Back edges
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;
}