0% found this document useful (0 votes)
19 views9 pages

BFS, DFS

Spanning tree

Uploaded by

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

BFS, DFS

Spanning tree

Uploaded by

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

AA UNIT-2

Graphs can be obtained by two methods. They are namely

1. BFS (Breadth First Search).


2. DFS (Depth First Search)

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

ALGORITHM FOR BREADTH FIRST SEARCH

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

Next we have to consider adjacent vertices of v2 for v2 we have


V1, V4, V5
But V1 is a adjacent vertex but it is already visited. Therefore the visited sequence is
V1, V2, V3, V4, V5

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 BFS Traversal sequence is V1, V3, V2, V6, V4 , V5

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 .

Time Complexity and Space complexity

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

BREADTH FIRST TRAVERSAL

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.

Breadth First Graph Traversal 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

DEPTH FIRST SEARCH AND TRAVERSAL

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

The process is described recursively by following algorithm

Procedure DFS (v)


Integer w;
Visited (v)1
For each vertex w adjacent from V do
If visited (w) =0 then
Call DFS (w);
End if
Repeat
End DFS

Time Complexity and Space complexity

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

First node is V1.


The adjacent vertices are V2, V3
V1, V2, V3

Next we will visit for V2, V3


ForV2 consider the adjacent vertices
V2V4, V1, V5 among which we can visit one vertex randomly
i.e... Either V4 & V5.If we visit V4 then consider adjacent vertices of V4
V1, V2, V4
For V4, V2 and V6 are adjacent vertices but V2 is already visited.So,we have to visit V6.

Therefore the order of visited sequence is


V1, V2, V4, V6
For V6, V4, V5 and V3 are adjacent vertices and V4 is already visited.
So next we can visit either V5 or V3
Let us visit V5
For V5 V2 and V6 are the adjacent vertices but both are already visited so we move back to previous
node V6.
For V6, V3 is adjacent and not visited. Therefore V3 and adjacent vertices of V3i.e.. V1 and V6
Therefore V1, V6 are already visited.
Therefore the order of the sequence is
V1, V2, V4, V6, V5, V3
Previously we have visited V2 instead of V3
Now let us visit V3.

V3V1, V6
But V1 is already visited. Therefore visit V6
V1, V3
V1, V3, V6
For V6V4, V5, V3

V1, V3, V6, V4, V5, V3

For V4V2, V6
7
AA UNIT-2

V1, V3, V6, V4, V2

For V2V1, V4, V5


V1, V4 are already visited
Therefore visit V5.
Therefore the order of sequence is V1, V3, V6, V4, V2, V5

DFS
A

B C

D E F G

ABDBEBACF (Where F is the goal)


CONNECTED COMPONENTS

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.

You might also like