Graph-2 Dfs
Graph-2 Dfs
DFS
1
Last Class’s Topic
● Graph Representation
■ Adjacency Matrix
■ Adjacency List
● BFS – Breadth First Search
2
Breadth-First Search: The Code
Data: color[V], prev[V],d[V] While(Q not empty)
{
BFS(G) // starts from here u = DEQUEUE(Q);
{ for each v ∈ adj[u]{
for each vertex u ∈ if (color[v] == WHITE){
V-{s} color[v] = GREY;
{ d[v] = d[u] + 1;
color[u]=WHITE; prev[v] = u;
prev[u]=NIL; Enqueue(Q, v);
d[u]=inf; }
} }
color[s]=GRAY; color[u] = BLACK;
d[s]=0; prev[s]=NIL; }
Q=empty; }
ENQUEUE(Q,s);
3
Breadth-First Search: Print Path
Data: color[V], prev[V],d[V]
Print-Path(G, s, v)
{
if(v==s)
print(s)
else if(prev[v]==NIL)
print(No path);
else{
Print-Path(G,s,prev[v]);
print(v);
}
}
4
BFS – Questions
● Find the shortest path between “A” and “B” (with
path)? When will it fail?
● Find the most distant node from start node “A”
● How can we detect that there exists no path between
A and B using BFS?
● Print all of those nodes that are at distance 2 from
source vertex “S”.
● How can we modify BFS algorithm to check the
bipartiteness of a graph?
● Is it possible to answer that there exists more than one
path from “S” to “T” with minimum path cost?
5
Depth-First Search
● Input: 1 2
■ G = (V, E) (No source vertex given!) 3
● Goal: 5 4
■ Explore the edges of G to “discover” every vertex in V starting at the most
current visited node
■ Search may be repeated from multiple sources
● Output:
■ 2 timestamps on each vertex:
○ d[v] = discovery time
○ f[v] = finishing time (done with examining v’s adjacency list)
■ Depth-first forest
Depth-First Search
B C G
13
DFS Example
source
vertex d
S D F
f
1 | | |
A
| |
E
| | |
B C G
14
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 | |
E
| | |
B C G
15
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 | |
E
3 | | |
B C G
16
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 | |
E
3 |
| |
4
B C G
17
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 | |
E
3 |
5 | |
4
B C G
18
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 | |
E
3 | 5 |
|
4 6
B C G
19
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 |
|
7
E
3 | 5 |
|
4 6
B C G
20
DFS Example
source
vertex d
S D F
f
1 | 8 | |
A
2 |
|
7
E
3 | 5 |
|
4 6
B C G
21
DFS Example
source
vertex d
S D F
f
1 | 8 | |
A
2 |
9 |
7
E
3 | 5 |
|
4 6
B C G
What is the structure of the grey vertices?
What do they represent?
22
DFS Example
source
vertex d
S D F
f
1 | 8 | |
A
2 | 9
7 |10
E
3 | 5 |
|
4 6
B C G
23
DFS Example
source
vertex d
S D F
f
8
1 | |
|11
A
2 | 9
7 |10
E
3 | 5 |
|
4 6
B C G
24
DFS Example
source
vertex d
S D F
f
1 8
|
|12 |11
A
2 | 9
7 |10
E
3 | 5 |
|
4 6
B C G
25
DFS Example
source
vertex d
S D F
f
1 8
13|
|12 |11
A
2 | 9
7 |10
E
3 | 5 |
|
4 6
B C G
26
DFS Example
source
vertex d
S D F
f
1 8
13|
|12 |11
A
2 | 9
7 |10
E
3 | 5 |
14|
4 6
B C G
27
DFS Example
source
vertex d
S D F
f
1 8
13|
|12 |11
A
2 | 9
7 |10
E
3 | 5 | 14|
4 6 15
B C G
28
DFS Example
source
vertex d
S D F
f
1 8 13|
|12 |11 16
A
2 | 9
7 |10
E
3 | 5 | 14|
4 6 15
B C G
29
Depth-First Search: The Code
Data: color[V], time, DFS_Visit(u)
prev[V],d[V], f[V] {
DFS(G) // where prog starts color[u] = GREY;
{ time = time+1;
for each vertex u ∈ V d[u] = time;
{ for each v ∈ Adj[u]
color[u] = WHITE; {
prev[u]=NIL; if (color[v] == WHITE)
f[u]=inf; d[u]=inf; prev[v]=u;
} DFS_Visit(v);
time = 0; }
for each vertex u ∈ V color[u] = BLACK;
if (color[u] == WHITE) time = time+1;
DFS_Visit(u); f[u] = time;
} }
What will be the running time?
30
Depth-First Search: The Code
Data: color[V], time, DFS_Visit(u)
prev[V],d[V], f[V] {
DFS(G) // where prog starts color[u] = GREY;
{ time = time+1;
for each vertex u ∈ V d[u] = time;
{ for each v ∈ Adj[u] O(V)
color[u] = WHITE; {
prev[u]=NIL; O(V) if (color[v] == WHITE)
f[u]=inf; d[u]=inf; prev[v]=u;
} DFS_Visit(v);
time = 0; }
for each vertex u ∈ VO(V) color[u] = BLACK;
if (color[u] == WHITE) time = time+1;
DFS_Visit(u); f[u] = time;
} 2 }
Running time: O(V ) because call DFS_Visit on each vertex,
and the31loop over Adj[] can run as many as |V| times
Depth-First Search: The Code
Data: color[V], time, DFS_Visit(u)
prev[V],d[V], f[V] {
DFS(G) // where prog starts color[u] = GREY;
{ time = time+1;
for each vertex u ∈ V d[u] = time;
{ for each v ∈ Adj[u]
color[u] = WHITE; {
prev[u]=NIL; if (color[v] == WHITE)
f[u]=inf; d[u]=inf; prev[v]=u;
} DFS_Visit(v);
time = 0; }
for each vertex u ∈ V color[u] = BLACK;
if (color[u] == WHITE) time = time+1;
DFS_Visit(u); f[u] = time;
} BUT, there is actually
} a tighter bound.
How many times will DFS_Visit() actually be called?
32
Depth-First Search: The Code
Data: color[V], time, DFS_Visit(u)
prev[V],d[V], f[V] {
DFS(G) // where prog starts color[u] = GREY;
{ time = time+1;
for each vertex u ∈ V d[u] = time;
{ for each v ∈ Adj[u]
color[u] = WHITE; {
prev[u]=NIL; if (color[v] == WHITE)
f[u]=inf; d[u]=inf; prev[v]=u;
} DFS_Visit(v);
time = 0; }
for each vertex u ∈ V color[u] = BLACK;
if (color[u] == WHITE) time = time+1;
DFS_Visit(u); f[u] = time;
} }
So, running time of DFS = O(V+E)
33
Depth-First Sort Analysis
34
DFS: Kinds of edges
35
DFS Example
source
vertex d
f
1 8 13|
|12 |11 16
2 | 9
7 |10
3 | 5 | 14|
4 6 15
Tree edges
36
DFS: Kinds of edges
37
DFS Example
source
vertex d
f
1 8 13|
|12 |11 16
2 | 9
7 |10
3 | 5 | 14|
4 6 15
38
DFS: Kinds of edges
39
DFS Example
source
vertex d
f
1 8 13|
|12 |11 16
2 | 9
7 |10
3 | 5 | 14|
4 6 15
40
DFS: Kinds of edges
41
DFS Example
source
vertex d
f
1 8 13|
|12 |11 16
2 | 9
7 |10
3 | 5 | 14|
4 6 15
42
DFS: Kinds of edges
43
More about the edges
44
Depth-First Search - Timestamps
a b s c
1/1 11/
3/6 2/9
0 16
B F C
B
12/ 14/
4/5 7/8
C C 13 C 15
d e f g
45
Depth-First Search - Timestamps
s c
C B
F
b f g
C
a e C
B
C
d
46
Depth-First Search: Detect Edge
Data: color[V], time, DFS_Visit(u)
prev[V],d[V], f[V] {
DFS(G) // where prog starts color[u] = GREY;
{ time = time+1;
for each vertex u ∈ V d[u] = time;
{ for each v ∈ Adj[u]
color[u] = WHITE; {
prev[u]=NIL; detect edge type using
f[u]=inf; d[u]=inf; “color[v]”
} if(color[v] == WHITE){
time = 0; prev[v]=u;
for each vertex u ∈ V DFS_Visit(v);
if (color[u] == WHITE) }}
DFS_Visit(u); color[u] = BLACK;
} time = time+1;
f[u] = time;
47
DFS: Kinds Of Edges
48
DFS: Kinds Of Edges
53
DFS And Cycles
54
DFS And Cycles
55
Reference
● Cormen –
■ Chapter 22 (Elementary Graph Algorithms)
● Exercise –
■ 22.3-4 –Detect edge using d[u], d[v], f[u], f[v]
■ 22.3-11 – Connected Component
■ 22.3-12 – Singly connected
56