CS 3343: Analysis of Algorithms. Lecture 24: Graph searching, Topological sort. Review of MST and shortest path problem. Run Kruskal’s algorithm Run Prim’s algorithm Run Dijkstra’s algorithm. 2. f. c. 4. 7. 8. 8. a. d. g. 6. 7. 6. 1. 3. 5. 6. b. e. Graph Searching.
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.
Lecture 24: Graph searching, Topological sort
2
f
c
4
7
8
8
a
d
g
6
7
6
1
3
5
6
b
e
BFS(G, s) {
initialize vertices; // mark all vertices as white
Q = {s}; // Q is a queue; initialize to s
while (Q not empty) {
u = Dequeue(Q);
for each v adj[u]
if (v.color == WHITE) {
v.color = GREY;
v.d = u.d + 1;
v.p = u;
Enqueue(Q, v);
}
u.color = BLACK;
}
}
What does v.d represent?
What does v.p represent?
u = every vertex, but only once (Why?)
v = every vertex that appears in some other vert’s adjacency list
Total: Θ(m)
BFS: The Code AgainBFS(G, s) {
initialize vertices;
Q = {s};
while (Q not empty) {
u = Dequeue(Q);
for each v adj[u]
if (v.color == WHITE) {
v.color = GREY;
v.d = u.d + 1;
v.p = u;
Enqueue(Q, v);
}
u.color = BLACK;
}
}
What will be the running time?
Total running time: Θ(n+m)
{
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? (How about in BFS?)
{
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 CodeHow many times will DFS_Visit() be called?
{
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 CodeHow much time is needed within each DFS_Visit()?
{
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)
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?
sourcevertex
d f
1 


2 

3 


Cyclic
Acyclic
Underwear
Socks
Watch
Pants
Shoes
Shirt
Belt
Tie
Jacket
Socks
Underwear
Pants
Shoes
Watch
Shirt
Belt
Tie
Jacket
TopologicalSort()
{ // condition: the graph is a DAG
Run DFS
When a vertex is finished, output it
Vertices are output in reverse topological order
}
0
1
2
2
1
4
7
8
1
3
5
6
1
0
1
9
2
3
0
1
0
0
1
0
2
2
1
4
7
1
8
0
1
3
5
6
1
0
0
1
9
0
2
1
3
0
2
Compare to DFS: