BFS N DFS
BFS N DFS
Graph Searching
Algorithms
Dr. B.B.Biswal
S
Breadth-First Search (BFS)
Like Tree traversals, Graph supports two
types of traversals:-
1. Breadth First Traversals (BFS)
2. Depth First Traversals (DFS)
whit Not
u 0 ∞ x e discovere
d
Discovere
gray d,
v ∞ y
∞ adjacent
white
Discovere
blac nodes
d,
w k no
∞ ∞ z
adjacent
white
nodes
Breadth-First Search (BFS)
u 0 ∞ x
BFS(G, u):
1. Initialize the
v ∞ y
∞ graph
color[u] gray
π[u] Nil
d[u] 0
w ∞ ∞ z for each other
vertex
color[u] white
Breadth-First Search (BFS)
u 0 ∞ x Q u
BFS(G, u):
2. Initialize the
v ∞ y
∞ queue
QØ
Enqueue(Q, u)
w ∞ ∞ z
Breadth-First Search (BFS)
t=u
u 0 ∞ x Q
BFS(G, u):
3. While Q ≠ Ø
v ∞ y
∞ 1) t
Dequeue(Q)
w ∞ ∞ z
Breadth-First Search (BFS)
t=u
u Q v x r = x,
0 1 x
v
BFS(G, u):
3. While Q ≠ Ø
v 1 ∞ y 2) for each r adj
to t
if color[r] =
white
w ∞ ∞ z color[r] gray
π[r] t
d[r] d[t] + 1
Breadth-First Search (BFS)
t=u
u Q v x r = x,
0 1 x
v
BFS(G, u):
3. While Q ≠ Ø
v 1 ∞ y 3) color[t] black
w ∞ ∞ z
Breadth-First Search (BFS)
t=v
u 0 1 x Q x
BFS(G, u):
3. While Q ≠ Ø
v 1 ∞ y 1) t
Dequeue(Q)
2) for each r adj
to t
w ∞ ∞ z …
3) color[t] black
Breadth-First Search (BFS)
t=v
u Q x y r=y
0 1 x
BFS(G, u):
3. While Q ≠ Ø
v 1 2 y 1) t
Dequeue(Q)
2) for each r adj
to t
w ∞ ∞ z …
3) color[t] black
Breadth-First Search (BFS)
t=v
u Q x y r=y
0 1 x
BFS(G, u):
3. While Q ≠ Ø
v 1 2 y 1) t
Dequeue(Q)
2) for each r adj
to t
w ∞ ∞ z …
3) color[t] black
Breadth-First Search (BFS)
t=x
u Q y r=
0 1 x
BFS(G, u):
3. While Q ≠ Ø
v 1 2 y 1) t
Dequeue(Q)
2) for each r adj
to t
w ∞ ∞ z …
3) color[t] black
Breadth-First Search (BFS)
t=y
u Q w r=w
0 1 x
BFS(G, u):
3. While Q ≠ Ø
v 1 2 y 1) t
Dequeue(Q)
2) for each r adj
to t
w 3 ∞ z …
3) color[t] black
Breadth-First Search (BFS)
t=w
u Q z r=z
0 1 x
BFS(G, u):
3. While Q ≠ Ø
v 1 2 y 1) t
Dequeue(Q)
2) for each r adj
to t
w 3 4 z …
3) color[t] black
Breadth-First Search (BFS)
t=z
u Q r=
0 1 x
BFS(G, u):
3. While Q ≠ Ø
v 1 2 y 1) t
Dequeue(Q)
2) for each r adj
to t
w 3 4 z …
3) color[t] black
Breadth-First Search (BFS)
u 0 1 x BFS(G, u):
- the shortest-path
distance
v 1 2 y from u
w 3 4 z
Breadth-First Search (BFS)
u 0 1 x BFS(G, u):
- the shortest-path
distance
v 1 2 y from u
- construct a tree
w 3 4 z
Breadth-First Search (BFS)
u 0 1 x BFS(G, u):
- Initialization: |V|
- Enqueuing/dequeuing:
v 1 2 y |V|
- Scanning adj vertices:
|E|
w 3 4 z
Breadth-First Search (BFS)
u 0 1 x BFS(G, u):
- Initialization: O(|V|)
- Enqueuing/dequeuing:
v 1 2 y O(|V|)
- Scanning adjacent
vertices:
O(|E|)
w 3 4 z => total running time:
O(|V| + |E|)
BFS
Depth-First Search (DFS)
Depth-First Search (DFS)
v w
Depth-First Search (DFS)
timestamp:
t
d[u]: when u is discovered
f[u]: when searching adj
d[u] = u of u
t
is finished
v w
Depth-First Search (DFS)
timestamp:
t+1
d[u]: when u is discovered
f[u]: when searching adj
d[u] = u of u
t
is finished
v w
d[v] =
t+1
Depth-First Search (DFS)
timestamp:
t+2
d[u]: when u is discovered
f[u]: when searching adj
d[u] = u of u
t
is finished
v w
d[v] =
t+1
f[v] =
t+2
Depth-First Search (DFS)
timestamp:
t+3
d[u]: when u is discovered
f[u]: when searching adj
d[u] = u of u
t
is finished
v w
d[v] = d[w] =
t+1 t+3
f[v] =
t+2
Depth-First Search (DFS)
timestamp:
t+4
d[u]: when u is discovered
f[u]: when searching adj
d[u] = u of u
t
is finished
v w
d[v] = d[w] =
t+1 t+3
f[v] = f[v] = t+4
t+2
Depth-First Search (DFS)
timestamp:
t+5
d[u]: when u is discovered
f[u]: when searching adj
d[u] = t u of u
f[u] =
is finished
t+5
v w
d[v] = d[w] =
t+1 t+3
f[v] = f[w] =
t+2 t+4
Depth-First Search (DFS)
whit Not
u x e discovere
d
Discovere
gray d,
v y adjacent
white
Discovere
blac nodes
d,
w k no
z
adjacent
white
nodes
Depth-First Search (DFS)
Not
u x discovere
d
Discovere
d/ d,
v y adjacent
white
Discovere
nodes
d,
d/f
w z no
adjacent
white
nodes
Depth-First Search (DFS)
u x DFS(G):
1. Initialization
for each u V[G],
color[u] white
v y
π[u] Nil
time 0
w z
Depth-First Search (DFS)
u 1/ x DFS(G):
1. Initialization
2. For each u V[G]
if color[u] = white
v y
DFS-Visit(u)
DFS-Visit(u):
1. Initial Setting
w z color[u] gray
d[u] time time +
1
Depth-First Search (DFS)
u 1/ x DFS(G):
1. Initialization
2. For each u V[G]
if color[u] = white
v 2/
y
DFS-Visit(u)
DFS-Visit(u):
1. Initial Setting
w z 2. for each adj v of
white
π[v] u
Depth-First Search (DFS)
u 1/ x DFS(G):
1. Initialization
2. For each u V[G]
if color[u] = white
v 2/ 3/ y
DFS-Visit(u)
DFS-Visit(u):
1. Initial Setting
w z 2. for each adj v of
white
π[v] u
Depth-First Search (DFS)
u 1/ 4/ x DFS(G):
1. Initialization
2. For each u V[G]
if color[u] = white
v 2/ 3/ y
DFS-Visit(u)
DFS-Visit(u):
1. Initial Setting
w z 2. for each adj v of
white
π[v] u
Depth-First Search (DFS)
u 1/ 4/5 x DFS(G):
1. Initialization
2. For each u V[G]
if color[u] = white
v 2/ 3/ y
DFS-Visit(u)
DFS-Visit(u):
1. Initial Setting
w z 2. Handling adj
vertices
3. color[u] black
Depth-First Search (DFS)
u 1/ 4/5 x DFS(G):
1. Initialization
2. For each u V[G]
if color[u] = white
v 2/ 3/6 y
DFS-Visit(u)
DFS-Visit(u):
1. Initial Setting
w z 2. Handling adj
vertices
3. color[u] black
Depth-First Search (DFS)
u 1/ 4/5 x DFS(G):
1. Initialization
2. For each u V[G]
if color[u] = white
v 2/7 3/6 y
DFS-Visit(u)
DFS-Visit(u):
1. Initial Setting
w z 2. Handling adj
vertices
3. color[u] black
Depth-First Search (DFS)
v 2/7 3/6 y
w 10/1 z
9/12
1
Depth-First Search (DFS)
m n o
q r s
t u
m n q o s r u t
Topological Sorting
m n o Brute-Force way
1. Find a vertex
without edges.
q r s 2. Put it onto the
front
t u of the list, and
remove it from G.
O(|V|2 + |V||E|) 3. Remove all
Or edges
O(|V|2) to the removed
edge.
Topological Sorting
m n o Using DFS
1. Call DFS(G)
2. When a vertex is
q r s finished, put it
onto
t u the front of the
list.
O(|V| + |E|)
Topological Sorting
v enters
the list u
before u? Using DFS
1. Call DFS(G)
2. When a vertex is
finished, put it
v onto
the front of the
1) v is white: d[u] < d[v]
list.
At d[u]: < f[u]
2) v is black: f[v] < d[u]
3) v is gray: d[v] < d[u] <
f[v]
Topological Sorting
v enters
the list u
before u? Using DFS
t 1. Call DFS(G)
2. When a vertex is
finished, put it
v onto
the front of the
1) v is white: d[u] < d[v]list.
At d[u]: t is gray: d[t] < d[u] < f[t]
t is black: f[t] < d[u]
Topological Sorting
1. If v is a descendant,
d[u] < d[v] < f[v] <
f[u]
u 2. If v is an anscestor,
d[v] < d[u] < f[u] <
f[v]
3. Otherwise,
v w
d[v] < f[v] < d[u] <
f[u]
or
d[u] < f[u] < d[v] <
Depth-First Search (DFS)
1. If v is a descendant, Contradiction
d[u] < d[v] < f[v] < f[u]
2. If v is an anscestor,
:
d[v] < d[u] < f[u] < f[v] G is not
3. Otherwise, acyclic.
d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <
d[v] < f[v]
u At d[u]:
t 1) v is white: d[u] < d[v]
t is gray: d[t] < d[u] < f[t]
t is black: f[t] < d[u]
v
Topological Sorting
1. If v is a descendant, Contradiction:
d[u] < d[v] < f[v] < f[u]
2. If v is an anscestor,
v should be
d[v] < d[u] < f[u] < f[v] black.
3. Otherwise,
d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <
d[v] < f[v]
u At d[u]:
t 1) v is white: d[u] < d[v]
t is gray: d[t] < d[u] < f[t]
t is black: f[t] < d[u]
v
Topological Sorting
1. If v is a descendant,
d[u] < d[v] < f[v] < f[u]
2. If v is an anscestor,
d[v] < d[u] < f[u] < f[v]
3. Otherwise,
d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <
d[v] < f[v]
u At d[u]:
t 1) v is white: d[u] < d[v]
t is white.
v
Topological Sorting
1. If v is a descendant,
d[u] < d[v] < f[v] < f[u]
2. If v is an anscestor,
d[v] < d[u] < f[u] < f[v]
3. Otherwise,
d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <
d[v] < f[v]
u At d[u]:
t 1) v is white: d[u] < d[v]
< f[u]
t is white.
v
Topological Sorting
1. If v is a descendant,
d[u] < d[v] < f[v] < f[u] v will enter
2. If v is an anscestor, the list
d[v] < d[u] < f[u] < f[v] before u.
3. Otherwise,
d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <
d[v] < f[v]
u At d[u]:
1) v is white: d[u] < d[v]
< f[u]
2) v is black: f[v] < d[u]
3) v is gray: d[v] < d[u] <
v
f[v]
Topological Sorting
1. If v is a descendant,
d[u] < d[v] < f[v] < f[u] v is already
2. If v is an anscestor, in the list.
d[v] < d[u] < f[u] < f[v]
3. Otherwise,
d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <
d[v] < f[v]
u At d[u]:
1) v is white: d[u] < d[v]
< f[u]
2) v is black: f[v] < d[u]
3) v is gray: d[v] < d[u] <
v
f[v]
Topological Sorting
1. If v is a descendant,
d[u] < d[v] < f[v] < f[u] Contradiction
2. If v is an anscestor, :
d[v] < d[u] < f[u] < f[v] G is not
3. Otherwise, acyclic.
d[v] < f[v] < d[u] < f[u] or d[u] < f[u] <
d[v] < f[v]
u At d[u]:
1) v is white: d[u] < d[v]
< f[u]
2) v is black: f[v] < d[u]
3) v is gray: d[v] < d[u] <
v
f[v]
Strongly Connected
Component
Strongly Connected
Component
Strongly Connected
Component
Strongly Connected
Component
3 1 2
4 5
1. Call DFS(G)
2. Arrange the vertices
in order of 9 8 6 7
decreasing f(u)
Strongly Connected
Component
3 1 2
4 5
1. Call DFS(G)
2. Arrange the vertices
in order of 9 8 6 7
decreasing f(u)
3. Compute GT
Strongly Connected
Component
3 1 2
4 5
1. Call DFS(G)
2. Arrange the vertices
in order of 9 8 6 7
decreasing f(u)
3. Compute GT
4. Run DFS(GT)
Strongly Connected
Component
3 1 2
4 5
1. Call DFS(G)
2. Arrange the vertices
in order of 9 8 6 7
decreasing f(u)
3. Compute GT
4. Run DFS(GT)
Topological Sorting
n o
r s
n o s r
Strongly Connected
Component
Strongly Connected
Component
1. Call DFS(G)
2. Arrange the vertices
in order of 9 8 6 7
decreasing f(u)
Strongly Connected
Component
2
1
4 3
Strongly Connected
Component
2
1
4 3
Adjacency Matrix of
Graphs
A= AT =