BFS, DFS
BFS, DFS
BFS (FIRST-METHOD)
1. In this we start at a vertex v and we will visit “V1”.
2. A vertex is said to be explored if all the adjacent vertices of it are visited.
3. A data structure called “queue” which follows a FIFO principal, i.e.. Insertions occur at rear end
and deletions occur at front end.
Example:
V1
V3
V2
V4 V5
V6
Solution:
Initially queue is empty
F r
Step 1: Start from the vertex “v1” and place this “v1” in the queue find out all the adjacent vertices for
“v1” and insert into the queue
q V1
f r
1
AA UNIT-2
q
V1 V2 V3
f r
Now perform the delete operation on q. so the first vertex is removed from q
q
V2 V3
f r
The next unexplored vertex is v2
For v2, the adjacent vertices are v4, v5 and insert into q
q
V2 V3 V4 V5
f r
Again perform delete operation
Now q becomes
q
V3 V4 V5
f r
Next for V3 adjacent vertices are v1 and v6 but v1 is already visited so include v6 into the queue
q
V4 V5 V6
f r
Here V4, V5, V6 all are explored vertices since the adjacent vertices of these (V4, V5, V6) are visited.
Therefore perform the delete operation continuously until queue becomes empty.
Therefore the BFT Traversal is
V1 , V2 , V3 , V4 ,V5 ,V6
2
AA UNIT-2
Visited(m);
Procedure BFS(v)
Integer w, q is a queue
Visited(v)1
Initialize queue(q);
Addqueue(q,v)
While(notemptyqueue(q))do
{
Deletequeue(q,v);
For all vertices w adjacent from v do
If visited(w)=0 then
{
Addqueue(q,w);
Visited(w)1;
}
}
End BFS
SECOND METHOD
V1
V3
V2
V4 V5
V6
3
AA UNIT-2
Explanation
We start at V1, find adjacent vertices for v1 the adjacent vertices are v2,v3.
Visit v2,v3 or v3,v2 it means that you can visits v2 followed by v3 or v3 followed by v2
V1, V2, V3
For V3 the adjacent vertices are V1, V6 and V1 is already visited so visit V6
Therefore the visited sequence is
V1, V2, V3, V4, V5 , V6
Therefore the BFS sequence is
V1, V2, V3, V4, V5 , V6
So Therefore the FIRST BFS Traversal sequence is V1, V2, V3, V4, V5 , V6
So Therefore the SECOND BFS Traversal sequence is V1, V3, V2, V6, V4 , V5
NOTE: The order of visiting sequence means in what order we are visiting vertices i.e.. either V2
followed by V3 or V3 followed by V2 .
If t (n, e), s (n,e) represents the maximum time and maximum additional
space taken by DFS for an n vertex ,e edge graph then S(n,e)=O(n) and t(n,e)=O(n+e) if adjacency lists
are used and t(n,e)=O(n2) if adjac3ency matrices are used and T(n,e)= O(n2)
4
AA UNIT-2
If BFS is used on a connected undirected graph “G” then all the vertices in “G” get visited and
the graph is traversed.
A complete traversal of the graph can be made by repeatedly calling BFS each time with a new
unvisited starting vertex.
The time complexity and space complexity of BFT is same as BFS algorithm.
Procedure BFT (G , N)
Declare visited (n)
For i 1 to n do
Visited (i)0
Repeat
For i 1 to n do
If visited (i)=0 then call BFS(i) end if
Repeat
End
BFT
5
AA UNIT-2
In this searching we start at vertex V, next we find all the adjacent vertices at
V, among which we will visit one vertex ,U again we will explore U
If t (n, e), s (n,e) represents the maximum time and maximum additional
space taken by DFS for an n vertex ,e edge graph then S(n,e)=O(n) and t(n,e)=O(n+e) if adjacency lists
are used and t(n,e)=O(n2) if adjac3ency matrices are used and T(n,e)= O(n2)
V1
V2 V3
V4 V5
V6
6
AA UNIT-2
V3V1, V6
But V1 is already visited. Therefore visit V6
V1, V3
V1, V3, V6
For V6V4, V5, V3
For V4V2, V6
7
AA UNIT-2
DFS
A
B C
D E F G
1. If there are two vertices “u” and “v” and there is a path from u to v then the graph is said to be a
connected graph.
2. If a graph is not connected such a model will consist of several connected pieces that are called
as a connected components of the graph
Tree edge: in DFS whenever a new unvisited vertex is reached for the first v time it is attached
as a child to the vertex from which it is being reached. Therefore the edge is called as a “tree edge”
Back Edge: In DFS algorithm may also encounter an edge leading to a previously visited vertex
other than its immediate predecessor. Therefore the edge is called as a “Back edge”
8
AA UNIT-2
Applications of DFS
1. Checking connectivity of the paths.
2. Checking acyclicity of a graph.
3. The checking graph connectivity can be done by as follows
1. Start a DFS traversal at an arbitrary vertex and check after the algorithm halts, weather all
the graph vertices will have been visited.
1. If they have the graph is connected otherwise it is not connected.