Kruskal Algorithm

 Kruskal’s Minimum Spanning Tree Algorithm


What is Minimum Spanning Tree? 

Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and  connects all the vertices together. A single graph can  have many different spanning trees. A minimum spanning tree (MST) or minimum weight spanning tree  for a weighted, connected and undirected graph is a  spanning tree with weight less than or equal to the  weight of every other spanning tree. The weight of a  spanning tree is the sum of weights given to each edge  of the spanning tree. 

How many edges does a minimum spanning tree has? A minimum spanning tree has (V – 1) edges where V is  the number of vertices in the given graph. 

Below are the steps for finding MST using Kruskal’s algorithm 

1. Sort all the edges in non-decreasing order of their weight. 2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it. 

3. Repeat step#2 until there are (V-1) edges in the spanning tree. 

The algorithm is a Greedy Algorithm. The Greedy Choice is to pick the smallest weight edge that does not cause a cycle in the MST constructed so far. Let us understand it with an example: Consider the below input graph.

The graph contains 9 vertices and 14 edges. So, the minimum spanning  tree formed will be having (9 – 1) = 8 edges. 

After sorting: 

Weight     Src     Dest 

1            7     

2            8     

2            6    

4            0     

4            2    

6            8     

7            2     

7            7     

8            0     

8            1     

9            3     

10           5     

11           1     

14           3     5 

Now pick all edges one by one from sorted list of edges

1. Pick edge 7-6: No cycle is formed, include it. 




2. Pick edge 8-2: No cycle is formed, include it.  




3. Pick edge 6-5: No cycle is formed, include it.  




4. Pick edge 0-1: No cycle is formed, include it.  




5. Pick edge 2-5: No cycle is formed, include it.  

6. Pick edge 8-6: Since including this edge results in  cycle, discard it.

7. Pick edge 2-3: No cycle is formed, include it. 

  

8. Pick edge 7-8: Since including this edge results in  cycle, discard it. 

9. Pick edge 0-7: No cycle is formed, include it. 

  

10. Pick edge 1-2: Since including this edge results in  cycle, discard it. 

11. Pick edge 3-4: No cycle is formed, include it. 

  

Since the number of edges included equals (V – 1), the  algorithm stops here. 

Example








Step 1 - Remove all loops and Parallel Edges 

Remove all loops and parallel edges from the given  graph. 



In case of parallel edges, keep the one which has  the least cost associated and remove all others.



Step 2 - Arrange all edges in their increasing order of weight 

The next step is to create a set of edges and weight, and  arrange them in an ascending order of weightage (cost). 




Step 3 - Add the edge which has the least weightage 

Now we start adding edges to the graph beginning from  the one which has the least weight. Throughout, we shall  keep checking that the spanning properties remain  intact. In case, by adding one edge, the spanning tree  property does not hold then we shall consider not to  include the edge in the graph. 



The least cost is 2 and edges involved are B,D and D,T.  We add them. Adding them does not violate spanning  tree properties, so we continue to our next edge  selection. 

Next cost is 3, and associated edges are A,C and C,D.  We add them again –




Next cost in the table is 4, and we observe that adding it  will create a circuit in the graph. – 




We ignore it. In the process we shall ignore/avoid all  edges that create a circuit. 




We observe that edges with cost 5 and 6 also create  circuits. We ignore them and move on. 



                    

Now we are left with only one node to be added.  Between the two least cost edges available 7 and  8, we shall add the edge with cost 7.




By adding edge S,A we have included all the nodes  of the graph and we now have minimum cost  spanning tree. 

Algorithm: 

MST- KRUSKAL (G, w) 

1. A ←  

2. for each vertex v V [G] 

3. do MAKE - SET (v) 

4. sort the edges of E into non decreasing order  by weight w 

5. for each edge (u, v) E, taken in non  decreasing order by weight 

6. do if FIND-SET (μ) ≠ if FIND-SET (v) 7. then A ← A {(u, v)} 

8. UNION (u, v) 

9. return A

Example:2 



Solution: First we initialize the set A to the  empty set and create |v| trees, one containing  each vertex with MAKE-SET procedure. Then sort  the edges in E into order by non-decreasing  weight. 

There are 9 vertices and 12 edges. So MST  formed (9-1) = 8 edges 




Now, check for each edge (u, v) whether  the endpoints u and v belong to the same  tree. If they do then the edge (u, v) cannot 

be supplementary. Otherwise, the two  vertices belong to different trees, and the  edge (u, v) is added to A, and the vertices  in two trees are merged in by union  procedure. 


Step1: So, first take (h, g) edge


Step 2: then (g, f) edge. 




Step 3:
then (a, b) and (i, g) edges are  considered, and the forest becomes





Step 4: Now, edge (h, i). Both h and i vertices  are in the same set. Thus it creates a cycle. So  this edge is discarded. 

Then edge (c, d), (b, c), (a, h), (d, e), (e,  f) are considered, and the forest becomes. 




Step 5: In (e, f) edge both endpoints e and f exist in  the same tree so discarded this edge. Then (b, h)  edge, it also creates a cycle. 

Step 6: After that edge (d, f) and the final spanning  tree is shown as in dark lines.



Step 7: This step will be required Minimum Spanning Tree  because it contains all the 9 vertices and (9 - 1) = 8 edges 

1. e f, b h, d f [cycle will be formed]  




Minimum Cost MST 




Solution: 

Step1: Q =[s, t, x, y, z] 

We scanned vertices one by one and find out its adjacent. Calculate the distance of each  adjacent to the source vertices.

We make a stack, which contains those vertices which are selected after computation of  shortest distance. 

Firstly we take's' in stack M (which is a source) 

1. M = [S] Q = [t, x, y, z]  

Step 2: Now find the adjacent of s that are t and y. 

1. Adj [s] t, y [Here s is u and t and y are v]  




Case - (i) s t 

d [v] > d [u] + w [u, v] 

d [t] > d [s] + w [s, t] 

∞ > 0 + 10 [false condition] 

Then d [t] 10 

 π [t] 5 

Adj [s] t, y 

Case - (ii) sy 

d [v] > d [u] + w [u, v] 

d [y] > d [s] + w [s, y] 

∞ > 0 + 5 [false condition] 

∞ > 5 

Then d [y] 5 

 π [y]

By comparing case (i) and case (ii) 

Adj [s] t = 10, y = 5 

y is shortest 

y is assigned in 5 = [s, y]








Step 3: Now find the adjacent of y that is t, x, z. 

1. Adj [y] t, x, z [Here y is u and t, x, z are v]  

Case - (i) y t 

d [v] > d [u] + w [u, v] 

d [t] > d [y] + w [y, t] 

10 > 5 + 3 

10 > 8 

Then d [t] 8 

π [t]

Case - (ii) y x 

d [v] > d [u] + w [u, v] 

d [x] > d [y] + w [y, x] 

∞ > 5 + 9 

∞ > 14 

Then d [x] 14 

π [x] 14 

Case - (iii) y z 

d [v] > d [u] + w [u, v] 

d [z] > d [y] + w [y, z] 

∞ > 5 + 2 

∞ > 7 

Then d [z] 7 

π [z]

By comparing case (i), case (ii) and case (iii) Adj [y] x = 14, t = 8, z =7 

z is shortest 

z is assigned in 7 = [s, z]




Step - 4 Now we will find adj [z] that are s, x 

1. Adj [z] [x, s] [Here z is u and s and x are v]  

Case - (i) z x 

d [v] > d [u] + w [u, v] 

d [x] > d [z] + w [z, x] 

14 > 7 + 6 

14 > 13 

Then d [x] 13 

π [x]

Case - (ii) z s 

d [v] > d [u] + w [u, v] 

d [s] > d [z] + w [z, s] 

0 > 7 + 7 

0 > 14 

This condition does not satisfy so it will be discarded. Now we have x = 13. 




Step 5: Now we will find Adj [t] 

Adj [t] [x, y] [Here t is u and x and y are v] 

Case - (i) t x 

d [v] > d [u] + w [u, v] 

d [x] > d [t] + w [t, x] 

13 > 8 + 1 

13 > 9 

Then d [x] 9 

 π [x]

Case - (ii) t y 

d [v] > d [u] + w [u, v] 

d [y] > d [t] + w [t, y] 

5 > 10 

This condition does not satisfy so it will be discarded.





Thus we get all shortest path vertex as 

Weight from s to y is 5 

Weight from s to z is 7 

Weight from s to t is 8 

Weight from s to x is 9 

These are the shortest distance from the source's' in the given graph.



Disadvantage of Dijkstra's Algorithm: 

1. It does a blind search, so wastes a lot of time while processing. 

2. It can't handle negative edges. 

3. It leads to the acyclic graph and most often cannot obtain the right shortest path. 4. We need to keep track of vertices that have been visited.


Comments