You are on page 1of 24

SPANNING TREE

SPANNING TREE
SPANNING TREE
A spanning tree can be defined as the subgraph of an
undirected connected graph. It includes all the
vertices along with the least possible number of
edges. If any vertex is missed, it is not a spanning
tree. A spanning tree is a subset of the graph that
does not have cycles, and it also cannot be
disconnected.

A spanning tree consists of (n-1) edges, where 'n' is


the number of vertices (or nodes). Edges of the
spanning tree may or may not have weights assigned
to them.
EXAMPL
E
EXAMPLE
Suppose a graph be:

A A A

B E B E B E

C D C D C D
ALGORITHM
There are two types of Prim’s Algo rithm Kruska l’s Alg orithm
algorithm that can be used
to find the minimum P r i m ’s a l g o r i t h m f i n d s t h e In Kruskal's algorithm, we
spanning tree; minimum spanning tree by start from edges with the
starting with one node and lowest weight and keep
then keeps adding new nodes adding the edges until the
from its nearest neighbor of goal is reached.
minimum weight until the
number of edges is one less
than the number of vertices.
PRIM’S
PRIM’S
The steps to implement the prim's algorithm are
given as follows -

• First, we have to initialize an MST with the


randomly chosen vertex.
• N o w, w e h a v e t o f i n d a l l t h e e d g e s t h a t c o n n e c t t h e
tree in the above step with the new vertices. From
the edges found, select the minimum edge and add
it to the tree.
• Repeat step 2 until the minimum spanning tree is
formed.
PRIM’S
Suppose a weighted graph is:

10 3
B C A

4 6
2

D E
1
PRIM'S
Step 1 - First, we have to
choose a vertex from the above
graph. Let's choose B:

10 3
B C A

4 6
2

D E
1
PRIM’S
S t e p 2 - N o w, w e h a v e t o c h o o s e a n d a d d t h e s h o r t e s t e d g e
from vertex B. There are two edges from vertex B that are B
to C with weight 10 and edge B to D with weight 4. Among
the edges, the edge BD has the minimum weight. So, add it to
t h e M S T.

10 3
B C A

4 6
2

D E
1
PRIM’S
S t e p 3 - N o w, a g a i n , c h o o s e t h e e d g e w i t h t h e m i n i m u m
weight among all the other edges. In this case, the edges DE
and CD are such edges. Add them to MST and explore the
adjacent of C, i.e., E and A. So, select the edge DE and add it
t o t h e M S T.

10 3
B C A

4 6
2

D E
1
PRIM’S
S t e p 4 - N o w, s e l e c t t h e
edge CD, and add it to
t h e M S T.

10 3
B C A

4 6
2

D E
1
PRIM’S
S t e p 5 - N o w, c h o o s e t h e e d g e C A . H e r e ,
we cannot select the edge CE as it would
create a cycle to the graph. So, choose
t h e e d g e C A a n d a d d i t t o t h e M S T.

10 3
B C A

4 6
2

D E
1
PRIM’S
So, the graph produced in step 5 is the
minimum spanning tree of the given
graph. The cost of the MST is given
below -

B C A

D E

C o s t o f M S T= 4 +2 +1+3 = 1 0
KRUSKAL’
S
KRUSKAL’S
T h e s t e p s t o i m p l e m e n t t h e K r u s k a l ’s a l g o r i t h m
are given as follows -

• First, sort all the edges from low weight to


high.
• N o w, t a k e t h e e d g e w i t h t h e l o w e s t w e i g h t a n d
add it to the spanning tree. If the edge to be
added creates a cycle, then reject the edge.
• Continue to add the edges until we reach all
vertices, and a minimum spanning tree is
created.
KRUSKAL’S
Suppose a weighted graph is:

5
A E
1 10
7 2

B C D
3 4
KRUSKAL’S
Step 1 - First, we have to
choose a vertex from the above
graph. Let's choose A

5
A E
1 10
7 2

B C D
3 4
KRUSKAL’S
Step 2 - add the edge AB with
weight 1 to the MST

5
A E
1 10
7 2

B C D
3 4
KRUSKAL’S
Step 3 - Add the edge DE with
weight 2 to the MST as it is
not creating the cycle.

5
A E
1 10
7 2

B C D
3 4
KRUSKAL’S
Step 4 - Add the edge BC with
w e i g h t 3 t o t h e M S T, a s i t i s
not creating any cycle or loop.

5
A E
1 10
7 2

B C D
3 4
KRUSKAL’S
S t e p 5 - N o w, p i c k t h e e d g e C D
w i t h w e i g h t 4 t o t h e M S T, a s i t
is not forming the cycle.

5
A E
1 10
7 2

B C D
3 4
KRUSKAL’S
So, the final minimum spanning
tree obtained from the given
weighted graph by using
Kruskal's algorithm is -

A E
1
2

B C D
3 4
C o s t o f M S T= 1 +3 +4+2 = 1 0

You might also like