Unit 1 BFS DFS
Unit 1 BFS DFS
Presented By:
Dr. Parvinder Singh
Assistant Professor
Central University of Punjab, Bathinda
This hand-notes/e-lecture is only for academic purpose and learning, don’t use
the contents for any profit/ research purpose without acknowledge the authors.
Don’t upload this hand note/e-lecture on any website like slide share etc.
References
• I am very thankful to :-
• Giving credit where credit is due:
• https://www.slideshare.net/KevinJadiya/breadth-first-search-depth-first-search
• https://www.slideshare.net/shimulsakhawat/breadth-first-search-and-depth-first-search
• https://www.cs.princeton.edu/courses/archive/spr10/cos226/demo/51demo-bfs.ppt
• https://visualgo.net/en/sorting
• Books
– Cormen, T.H., Leiserson, C. E., Rivest, R.L., and Stein, C. (2015).
Introduction to Algorithms. New Delhi: PHI Learning Private Limited.
Topic Covered – BFS and DFS Algorithms
Graph Search (traversal)
▪ How do we search a graph?
– At a particular vertices, where shall we go next?
▪ Two common framework:
▪ the depth-first search (DFS)
▪ the breadth-first search (BFS) and
▪ In DFS, go as far as possible along a single path until reach
a dead end (a vertex with no edge out or no neighbor
unexplored) then backtrack
▪ In BFS, one explore a graph level by level away (explore
all neighbors first and then move on)
Breadth First Search
Given
a graph G=(V,E) – set of vertices and edges
a distinguished source vertex s
Given a graph G=(V, E) and a distinguished source vertex s.
Breadth first search systematically explores the edges of
graph G to discover every vertex that is reachable from s.
The algorithm finds the distance of each vertex from
the source vertex. It finds the minimum distance
(smallest number of edges) from source s to each
reachable vertex.
It also produces a breadth first tree with root s that
contains all reachable vertices.
Breadth first search is so named because the algorithm
discovers all vertices at distance k from s before
discovering any vertices at distance k+1
What is BFS ?????
• B F S stands for Breadth First Search.
Step 3: loop:
repeat step 4 and step 5 until queue gets empty.
v w x y
BFS: Example
r s t u
0
v w x y
Q: s
BFS: Example
r s t u
1 0
1
v w x y
Q: w r
BFS: Example
r s t u
1 0 2
1 2
v w x y
Q: r t x
BFS: Example
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
BFS: Example
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
BFS: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
BFS: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
BFS: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
BFS: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: Ø
Breadth First Search
-
A B C D
E F G H
front
FIFO Queue
Breadth First Search
-
A B C D
E F G H
-
A B C D
E F G H
-
A B C D
E F G H
FIFO Queue
Breadth First Search
-
A B C D
E F G H
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
B discovered front B
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
- A
A B C D
E F G H
I discovered front B I
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
F discovered front I F
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
- A
A B C D
E F G H
I B
E discovered front F E
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
I B
- A
A B C D
E F G H
I B
- A
A B C D
E F G H
I B
I finished front F E
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
I B
- A
A B C D
E F G H
I B
- A
A B C D
E F G H
I B F
G discovered front E G
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
I B F
F finished front E G
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
I B F
- A
A B C D
E F G H
I B F
- A
A B C D
E F G H
I B F
E finished front G
FIFO Queue
Breadth First Search
- A
A B C D
E F G H
I B F
- A
A B C D
E F G H
I B F
FIFO Queue
Breadth First Search
- A G
A B C D
E F G H
I B F
C discovered front C
FIFO Queue
Breadth First Search
- A G
A B C D
E F G H
I B F
- A G
A B C D
E F G H
I B F G
H discovered front C H
FIFO Queue
Breadth First Search
- A G
A B C D
E F G H
I B F G
G finished front C H
FIFO Queue
Breadth First Search
- A G
A B C D
E F G H
I B F G
- A G
A B C D
E F G H
I B F G
- A G C
A B C D
E F G H
I B F G
D discovered front H D
FIFO Queue
Breadth First Search
- A G C
A B C D
E F G H
I B F G
C finished front H D
FIFO Queue
Breadth First Search
- A G C
A B C D
E F G H
I B F G
- A G C
A B C D
E F G H
I B F G
- A G C
A B C D
E F G H
I B F G
finished H front D
FIFO Queue
Breadth First Search
- A G C
A B C D
E F G H
I B F G
- A G C
A B C D
E F G H
I B F G
FIFO Queue
Breadth First Search
- A G C
A B C D
E F G H
I B F G
D finished front
FIFO Queue
Breadth First Search
- A G C
A B C D
E F G H
I B F G
FIFO Queue
Breadth First Search
- A G C
A B C D
E F G H
I B F G
STOP front
FIFO Queue
Analysis of Time Complexity
▪ BFS takes O(V) time for initialization
▪ The operations of enqueuing and dequeuing take O(1) time
for each vertex. So the total time devoted to queue
operation for v vertices is O(V).
▪ Now the adjacency list of each vertex is scanned only when
the vertex is dequeued. Then the sum of the lengths of all
the adjacency lists is ɵ(E). So the total time spent in
scanning adjacency lists is O(E).
▪ So the total time complexity of BFS is O(V+E)
BFS: Time Complexity
• In DFS one starts at the root and explores as far as possible along
each branch before backtracking.
Algorithm of DFS
[1]-- Initialize all nodes with status=1.(ready state white)
[2]– Put starting node in the stack and change status
to status=2(waiting state grey).
[3]– L oop:-
Repeat step- 4 and step- 5 until stack get empty.
[4]– Remove top node N from stack process them and
change the status of N processed state (status=3 black).
[5]–Add all the neighbours of N to the top of stack and
change status to waiting status-2.
Working of DFC : Stack data structure
B D F E
B C
A C C C C C
output
D E A A A A A A
B B B B B
D D D D
F F F F
E E
C
Depth First Search
▪ To keep track of progress DFS colors each vertex white, gray or
black. Initially all the vertices are colored white. Then they are
colored gray when discovered. Finally colored black when
finished.
▪ Besides creating depth first forest DFS also timestamps each
vertex. Each vertex goes through two time stamps:
▪ Discover time d[u]: when u is first discovered
▪ Finish time f[u]: when backtrack from u or finished u
▪ f[u] > d[u]
Depth first search
It searches ‘deeper’ the graph when possible.
Starts at the selected node and explores as far as
possible along each branch before backtracking.
Vertices go through white, gray and black stages of
color.
White – initially
Gray – when discovered first
Black – when finished i.e. the adjacency list of the vertex is
completely examined.
Also records timestamps for each vertex
d[v] when the vertex is first discovered
f[v] when the vertex is finished
DFS: Algorithm
DFS(G)
1 for each vertex u ∈ V [G]
2 do color[u] ← WHITE // color all vertices white, set their parents NIL
3 π[u] ← NIL
4 time ← 0 // zero out time
5 for each vertex u ∈ V [G] // call only for unexplored vertices
6 do if color[u] = WHITE // this may result in multiple sources
7 then DFS-VISIT(u)
DFS-VISIT(u)
1 color[u] ← GRAY ▹White vertex u has just been discovered.
2 time ← time +1
3 d[u]= time // record the discovery time
4 for each v ∈ Adj[u] ▹Explore edge(u, v).
5 do if color[v] = WHITE
6 then π[v] ← u // set the parent value
7 DFS-VISIT(v) // recursive call
8 color[u] ← BLACK ▹ Blacken u; it is finished.
9 time ← time +1
10 f [u] = time
DFS Example
source
vertex
DFS Example
source
vertex
d | f
1 | | |
| |
| | |
DFS Example
source
vertex
d | f
1 | | |
2 | |
| | |
DFS Example
source
vertex
d | f
1 | | |
2 | |
3 | | |
DFS Example
source
vertex
d | f
1 | | |
2 | |
3|4 | |
DFS Example
source
vertex
d | f
1 | | |
2 | |
3|4 5 | |
DFS Example
source
vertex
d | f
1 | | |
2 | |
3|4 5|6 |
DFS Example
source
vertex
d | f
1 | | |
2|7 |
3|4 5|6 |
DFS Example
source
vertex
d | f
1 | 8 | |
2|7 |
3|4 5|6 |
DFS Example
source
vertex
d | f
1 | 8 | |
2|7 9 |
3|4 5|6 |
DFS Example
source
vertex
d | f
1 | 8 | |
2|7 9|10
3|4 5|6 |
DFS Example
source
vertex
d | f
1 | 8|11 |
2|7 9|10
3|4 5|6 |
DFS Example
source
vertex
d | f
1|12 8|11 |
2|7 9|10
3|4 5|6 |
DFS Example
source
vertex
d | f
1|12 8|11 13|
2|7 9|10
3|4 5|6 |
DFS Example
source
vertex
d | f
1|12 8|11 13|
2|7 9|10
2|7 9|10
2|7 9|10