Spanning Trees
Spanning Trees
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”.
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”.
Examples:
E1 e3
E2
V2 v4
V3
1
AA UNIT-2
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………………
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
d e f g
h I j
m k
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
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.
V1 , V2 , V3 , V4 ,V5 ,V6
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
But V1 is a adjacent vertex but it is already visited. Therefore the visited sequence is
For V3 the adjacent vertices are V1, V6 and V1 is already visited so visit V6
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 .
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)
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
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
7
AA UNIT-2
End if
Repeat
End DFS
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
V1, V2, V4
For V4, V2 and V6 are adjacent vertices but V2 is already visited.So,we have to visit 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.
V3V1, V6
For V4V2, V6
DFS
B C
D E F G
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
e b
a d
• 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
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