0% found this document useful (0 votes)
12 views12 pages

Spanning Trees

Breadth first search

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)
12 views12 pages

Spanning Trees

Breadth first search

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/ 12

AA UNIT-2

Spanning Trees

Definition: A tree “T” is a spanning tree of a graph “G” is “T” is a sub graph of “G” that contains all of the
vertices of “G”.

There are two types of spanning trees. They are namely


1. Directed Spanning Tree.
2. Undirected Spanning Tree.

Directed Spanning Tree

A Tree “T” is a spanning tree of a graph “G” is “T” is a sub graph of “G” that contains all the
vertices of “G”.A spanning tree that is a dire ted tree is called a directed spanning tree of “G”.

Undirected Spanning Tree

A spanning tree that has no direction is called as a undirected Spanning tree.

Examples:

Spanning trees can be obtained by two methods. They are namely


1. BFS (Breadth First Search).
2. DFS (Depth First Search)
Generating the spanning tree

To construct a spanning tree say “T1” consists of edges e3, e2, e1


V1

E1 e3

E2

V2 v4
V3

Now to construct a new spanning tree add a chord say e4 in “T1”

1
AA UNIT-2

But by adding chord”e4” we get a circuit e3, e2, e1, e4


By deleting single branch from”T1” other than “e4” we get another spanning tree.

Branch: A branch refers to the edge present in a spanning tree but not in the graph.Similarlly we obtain
trees T2,T3,T4,T5,T6,T7,T8………………

Breadth First Search

It visits all the vertices sequentially on a given level before going on to the next level
Procedure

a b c i

d e f g

h i j

k
m
Solution:
Choose the vertex “a” as the root.

a
Step 2: Then add all the edges incident with all the vertices adjacent to a , so that edges{a,b},
{a,d}.Then vertices b,d are at level 1 in the tree

a b

c
Step 3: Next add the edges from these at level 1 to adjacent vertices not already in the tree Hence,
the edges {b, c},{d, h},{d,e} are added. Now c, h, e are at level 2

A b c

2
AA UNIT-2

d e

Step 4: Next add the edges from these vertices to adjacent vertices not already in the graph.
Hence, the edges {c, f}, {h, i}, {h, k} are added. But {e, i}, {e, f} are not added because it
forms
a cycle.
Now i, f, k are at level 3

A b c

D e
f

h i

Step 5: Add {I,j},{k,m}

Now j is at level 4 add(j,g),g at level “5” add(g,l)


a b c
l

d e f g

h I j

m k

Therefore the spanning tree


a

d b

h c
e
k i f

m j

g
l

BFS (FIRST-METHOD)
1. In this we start at a vertex v and we will visit “V1”.

3
AA UNIT-2

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

V2 V3

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
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

4
V2 V3 V4 V5
AA UNIT-2

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

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
5
AA UNIT-2

SECOND METHOD

V1

V2 V3

V4 V5

V6

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
6
AA UNIT-2

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)

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
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);

7
AA UNIT-2

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

First node is V1. V6

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

8
AA UNIT-2

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

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

B C

D E F G

ABDBEBACF (Where F is the goal)

9
AA UNIT-2

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”

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.

10
AA UNIT-2

Consider the following Graph

e b

a d

The DFS for the graph is as follows

The connected components are

• Terms
– A vertex v in a connected graph G is an articulation point if the deletion of vertex v together
with all edges incident to v disconnects the graph into two or more nonempty components.

11
AA UNIT-2

BICONNECTED COMPONENTS

A connected undirected graph “G” is said to be biconnected if it remains connected after


removal of any vertex and the edges that are incident upon that vertex.
A biconnected component of an undirected graph is a maximal biconnected sub graph that is a
biconnected sub graph not contained in any larger biconnected sub graph.

Articulation point: A vertex “v” in a connected graph “g” is an articulation point or a cut point if and only
if the deletion of vertex v together with all edges incident to v disconnects the graph into two or more non
empty components

12

You might also like