You are on page 1of 5

KRUSKAL’S ALGORITHM

In this algorithm, arrange the list of edges in ascending order based
on their weights.
Choose the edges one-by-one which does not form any cycle.
It is not necessary that selected edge is adjacent.
For example consider the following graph.
28
1 2
10 16
14

6 7 3
24 18
25 12

5 4
22
KRUSKAL’S ALGORITHM
Now, arrange the list of edges in ascending order based on their
weights. Hence
(1,6), (4,3), (7,2), (3,2), (4,7), (5,4), (5,7), (6,5)
Next, select the edges one-by-one until all the vertices are visited
exactly once and ignore the edge which forms cycle. Hence
1 2 1 2 1 2 1 2
10 10 10 14 10 14 16

6 7 3 6 7 3 6 7 3 6 7 3
12 12 12

5 4 5 4 5 4 5 4

Select (1,6) Select (4,3) Select (7,2) Select (3,2)

Since edge (4,7) causes cycle, ignore it.


KRUSKAL’S ALGORITHM
1 2
10 14 16

6 7 3
12
5 4
22
Select (5,4)
Since edge (5,7) causes cycle, ignore it.

1 2
10 14 16

6 7 3 Total cost = 10 + 25 + 22 + 12 + 16 + 14 = 99.


25 12

5 4
22
Select (6,5)
KRUSKAL’S ALGORITHM
Algorithm Kruskals(E, cost, n, t) if(j≠k) then
//E is the set of edges in G. G has ‘n’ vertices. {
Cost [u, v] is the cost of edge (u, v ). ‘t’ is set i:=i+1;
of edges in Minimum Cost Spanning Tree.
t[i,1]:= u;
{
t[i,2]:=v;
Construct a heap out of the edge costs using
heapify; mincost:=mincost+cost[u, v];
for i:=1 to n do Union(j, k);
parent[i]:= -1; }
i:=0; }
mincost:=0.0; if(i≠n-1) then write “no spanning tree”;
while((i<n-1) and (heap not empty)) do else return mincost;
{ }
delete a minimum cost edge (u, v) from
the heap and reheapify;
j:=Find(u);
k:=Find(v);
What is a Minimum Cost Spanning tree? Explain Kruskal’s
Minimum cost spanning tree algorithm with a suitable example.
[7M] [R16 III-II SET-4 Regular April/May - 2019]

You might also like