w10l2 Bfs & Dfs
w10l2 Bfs & Dfs
CSE@DIU 1
Graph Traversal
• Application example
• Given a graph representation and a vertex s in the graph
• Find paths from s to other vertices
• Two common graph traversal algorithms
• Breadth-First Search (BFS)
• Find the shortest paths in an unweighted graph
• Depth-First Search (DFS)
• Topological sort
• Find strongly connected components
2
CSE@DIU
BFS and Shortest Path Problem
• Given any source vertex s, BFS visits the other vertices at increasing distances
away from s. In doing so, BFS discovers paths from s to other vertices
• What do we mean by “distance”? The number of edges on a path from s
Example
0
Consider s=vertex 1
8
2
Nodes at distance 1?
1 2 s 9 1 2, 3, 7, 9
1
1
Nodes at distance 2?
3 7 8, 6, 5, 4
1
6 2
4 Nodes at distance 3?
5
2 2 3
CSE@DIU 0
Graph Searching
• Given: a graph G = (V, E), directed or undirected
• Goal: methodically explore every vertex and every edge
• Ultimately: build a tree on the graph
• Pick a vertex as the root
• Choose certain edges to produce a tree
• Note: might also build a forest if graph is not connected
4
CSE@DIU
Breadth-First Search
• “Explore” a graph, turning it into a tree
• One vertex at a time
• Expand frontier of explored vertices across the breadth of the frontier
• Builds a tree over the graph
• Pick a source vertex to be the root
• Find (“discover”) its children, then their children, etc.
5
CSE@DIU
Breadth-First Search
• Every vertex of a graph contains a color at every moment:
• White vertices have not been discovered
• All vertices start with white initially
• Grey vertices are discovered but not fully explored
• They may be adjacent to white vertices
• Black vertices are discovered and fully explored
• They are adjacent only to black and gray vertices
6
CSE@DIU
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 ∈
V-{s} if (color[v] == WHITE){
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);
7
CSE@DIU
Breadth-First Search: Example
z s t u
∞ ∞ ∞ ∞
∞ ∞ ∞ ∞
v w x y
Vertex z s t u v w x y
color W W W W W W W W
d ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
8
CSE@DIU
prev nil nil nil nil nil nil nil nil
Breadth-First Search: Example
z s t u
∞ 0 ∞ ∞
∞ ∞ ∞ ∞
v w x y
Q: s
vertex z s t u v w x y
Color W G W W W W W W
d ∞ 0 ∞ ∞ ∞ ∞ ∞ ∞
9
CSE@DIU
prev nil nil nil nil nil nil nil nil
Breadth-First Search: Example
z s t u
1 0 ∞ ∞
∞ 1 ∞ ∞
v w x y
Q: s w z
vertex z s t u v w x y
Color G B W W W G W W
d 1 0 ∞ ∞ ∞ 1 ∞ ∞
10
CSE@DIU
prev s nil nil nil nil s nil nil
Breadth-First Search: Example
z s t u
1 0 2 ∞
∞ 1 2 ∞
v w x y
Q: s w z t x
vertex z s t u V w X y
Color G B G W W B G W
d 1 0 2 ∞ ∞ 1 2 ∞
11
CSE@DIU
prev s nil w nil nil s w nil
Breadth-First Search: Example
z s t u
1 0 2 ∞
2 1 2 ∞
v w x y
Q: s w z t x v
vertex z s t u v w x y
Color B B G W G B G W
d 1 0 2 ∞ 2 1 2 ∞
12
CSE@DIU
prev s nil w nil r s w nil
Breadth-First Search: Example
z s t u
1 0 2 3
2 1 2 ∞
v w x y
Q: s w z t x v u
vertex z s t u v w x y
Color B B B G G B G W
d 1 0 2 3 2 1 2 ∞
13
CSE@DIU
prev s nil w t r s w nil
Breadth-First Search: Example
z s t u
1 0 2 3
2 1 2 3
v w x y
Q: s w z t x v u y
vertex z s t u v w x y
Color B B B G G B B G
d 1 0 2 3 2 1 2 3
14
CSE@DIU
prev s nil w t r s w x
Breadth-First Search: Example
z s t u
1 0 2 3
2 1 2 3
v w x y
Q: s w z t x v u y
vertex z s t u v w x y
Color B B B G B B B G
d 1 0 2 3 2 1 2 3
15
CSE@DIU
prev s nil w t r s w x
Breadth-First Search: Example
z s t u
1 0 2 3
2 1 2 3
v w x y
Q: s w z t x v u y
vertex z s t u v w x y
Color B B B B B B B G
d 1 0 2 3 2 1 2 3
16
CSE@DIU
prev s nil w t r s w x
Breadth-First Search: Example
z s t u
1 0 2 3
2 1 2 3
v w x y
Q: s w z t x v u y
vertex z s t u v w x y
Color B B B G B B B B
d 1 0 2 3 2 1 2 3
17
CSE@DIU
prev s nil w t r s w x
BFS: The Code (again)
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 ∈
V-{s} if (color[v] == WHITE){
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);
18
CSE@DIU
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);
}
}
19
CSE@DIU
Amortized Analysis
• Stack with 3 operations:
• Push, Pop, Multi-pop
• What will be the complexity if “n” operations are performed?
20
CSE@DIU
BFS: Complexity
Data: color[V], prev[V],d[V]
While(Q notu empty)
= every vertex, but only once
{ (Why?)
BFS(G) // starts from here
u = DEQUEUE(Q);
{
for each v ∈ adj[u]{
for each vertex u ∈
V-{s} if(color[v] == WHITE){
color[v] = GREY;
O(V)
{
O(V) 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);
What will be the running time?
21
CSE@DIU Total running time: O(V+E)
Breadth-First Search: Properties
• BFS calculates the shortest-path distance to the source node
• Shortest-path distance δ(s,v) = minimum number of edges from s to v, or ∞ if
v not reachable from s
• Proof given in the book (p. 472-5)
• BFS builds breadth-first tree, in which paths to root represent shortest
paths in G
• Thus can use BFS to calculate shortest path from one vertex to another in
O(V+E) time
22
CSE@DIU
Application of BFS
• Find the shortest path in an undirected/directed unweighted graph.
• Find the bipartiteness of a graph.
• Find cycle in a graph.
• Find the connectedness of a graph.
23
CSE@DIU
Depth-First Search
CSE@DIU 24
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
CSE@DIU 25
Depth-First Search
• Search “deeper” in the graph whenever possible 1 2
CSE@DIU 27
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
DFS(G) // where prog starts {
{ color[u] = GREY;
Initialize 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; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }
}
CSE@DIU 28
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
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; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }
}
CSE@DIU 29
What does u[d] represent?
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
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; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }
}
CSE@DIU 30
What does f[d] represent?
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
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; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }
B C G
CSE@DIU 32
DFS Example
source
vertex d
S D F
f
1 | | |
A
| |
E
| | |
B C G
CSE@DIU 33
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 | |
E
| | |
B C G
CSE@DIU 34
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 | |
E
3 | | |
B C G
CSE@DIU 35
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 | |
E
3 |
| |
4
B C G
CSE@DIU 36
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 | |
E
3 |
5 | |
4
B C G
CSE@DIU 37
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 | |
E
3 | 5 |
|
4 6
B C G
CSE@DIU 38
DFS Example
source
vertex d
S D F
f
1 | | |
A
2 |
|
7
E
3 | 5 |
|
4 6
B C G
CSE@DIU 39
DFS Example
source
vertex d
S D F
f
1 | 8 | |
A
2 |
|
7
E
3 | 5 |
|
4 6
B C G
CSE@DIU 40
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?
CSE@DIU 41
DFS Example
source
vertex d
S D F
f
1 | 8 | |
A
2 | 9
7 |10
E
3 | 5 |
|
4 6
B C G
CSE@DIU 42
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
CSE@DIU 43
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
CSE@DIU 44
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
CSE@DIU 45
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
CSE@DIU 46
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
CSE@DIU 47
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
CSE@DIU 48
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
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; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }
}
BUT, there is actually a tighter bound.
CSE@DIU How many times will DFS_Visit() actually be called? 51
Depth-First Search: The Code
Data: color[V], time,
prev[V],d[V], f[V] DFS_Visit(u)
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; color[u] = BLACK;
for each vertex u ∈ V time = time+1;
if (color[u] == WHITE) f[u] = time;
DFS_Visit(u); }
CSE@DIU 53
DFS Example
source
vertex
d f
1 8 13|1
|12 |11 6
2 | 9
7 |10
3 | 5 | 14|1
4 6 5
Tree edges
54
DFS: Kinds of edges
55
DFS Example
source
vertex
d f
1 8 13|1
|12 |11 6
2 | 9
7 |10
3 | 5 | 14|1
4 6 5
56
DFS: Kinds of edges
57
DFS Example
source
vertex
d f
1 8 13|1
|12 |11 6
2 | 9
7 |10
3 | 5 | 14|1
4 6 5
58
DFS: Kinds of edges
59
DFS Example
source
vertex
d f
1 8 13|1
|12 |11 6
2 | 9
7 |10
3 | 5 | 14|1
4 6 5
60
DFS: Kinds of edges
61
More about the edges
62
Textbooks & Web References
• Text Book (Chapter 22)
• www.geeksforgeeks.org
CSE@DIU 63
Thank you &
Any question?
CSE@DIU 64