0% found this document useful (0 votes)
11 views80 pages

BFS N DFS

The document discusses graph traversal algorithms, specifically Breadth-First Search (BFS) and Depth-First Search (DFS). BFS involves visiting adjacent vertices level-wise using a queue, while DFS explores as far as possible along each branch before backtracking. Both algorithms utilize color-coding for vertex states and have specific initialization and traversal procedures.

Uploaded by

subratsahu016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views80 pages

BFS N DFS

The document discusses graph traversal algorithms, specifically Breadth-First Search (BFS) and Depth-First Search (DFS). BFS involves visiting adjacent vertices level-wise using a queue, while DFS explores as far as possible along each branch before backtracking. Both algorithms utilize color-coding for vertex states and have specific initialization and traversal procedures.

Uploaded by

subratsahu016
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 80

GRAPH TRAVERSAL

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)

BFS: One node is selected as the start


position.
• Visit all the adjacent vertices of the
start vertex and then visit all the
unvisited vertices adjacent to these
BFS

 In BFS, we need a Queue to


implement it.
 In Queue implementation, add all
the unvisited vertices to the rear
end and delete the visited vertex
from the front of the queue.
 Find the Next node and follow the
same procedure until the queue is
empty.
 BFS is called as Level wise
BFS
 Graph traversal uses three colors. Each vertex
has the following attributes.
 Before processing color is white(undiscovered)

 When we are putting in queue it is Gray

3. When deleting from the queue make it Black

4. Predecessor of a vertex, can be NILL

5. distance from the source vertex computed


BFS
 Initially color of all vertex is white, the
depth of all vertex= Inf, the parent of all
vertex is Nil and the source vertex depth
is =0, parent= Nil and put it into QUEUE
Tree
Breadth-First Search (BFS)
Breadth-First Search (BFS)

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)

d[u]: when u is discovered


f[u]: when searching adj
u of u
is finished

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)

d[u]: when u is discovered


f[u]: when searching adj
d[u] = t u of u
f[u] =
is finished
t+5 1. d[u] < f[u]
v w 2. [ d[u], f[u] ] entirely
d[v] = d[w] = contains [ d[v], f[v] ]
t+1 t+3 3. [ d[v], f[v] ] and [ d[w],
f[v] = f[w] = f[w] ] are entirely
t+2 t+4 disjoint
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)

u 1/8 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)

u 1/8 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 9/ z 2. Handling adj
vertices
3. color[u]  black
Depth-First Search (DFS)

u 1/8 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 9/ 10/ z 2. Handling adj
vertices
3. color[u]  black
Depth-First Search (DFS)

u 1/8 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 10/1 z 2. Handling adj
9/
1 vertices
3. color[u]  black
Depth-First Search (DFS)

u 1/8 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 10/1 z 2. Handling adj
9/12
1 vertices
3. color[u]  black
Depth-First Search (DFS)

u 1/8 4/5 x DFS(G):


- construct a forest

v 2/7 3/6 y

w 10/1 z
9/12
1
Depth-First Search (DFS)

u 1/8 4/5 x DFS(G):


- Initialization: O(|V|)
- Traversing vertices:
v 2/7 3/6 y O(|V|)
- Scanning adjacent
vertices:
O(|E|)
w 10/1 z => total running time:
9/12
1 O(|V| + |E|)
Topological Sorting

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. d[u] < f[u]


2. [ d[u], f[u] ] entirely
contains [ d[v], f[v] ]
u 3. [ d[v], f[v] ] and [ d[w],
f[w] ] are entirely
disjoint
In a depth-first forest,
v w v is a descendant of u
if and only if
d[u] < d[v] < f[v] <
f[u]
Topological Sorting

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

Last > Last


f[u] 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)
Strongly Connected
Component

2
1
4 3
Strongly Connected
Component

2
1
4 3
Adjacency Matrix of
Graphs

Directed graph Adjacency-


Matrix
1 2 3 4
1 2 1 5
2
5 3
3 4 4
5
Adjacency Matrix of
Graphs

A= AT =

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy