Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




KRUSKAL's ALGORITHM - MINIMUM SPANNING TREE


Kruskal's Algorithm is one of the simplest algorithm that gives us an efficient solution for constructing a Minimum Spanning Tree.


And Kruskal's Algorithm uses the Greedy approach to construct a Minimum Spanning Tree.


What is Greedy approach ?


Just imagine, a person who is Greedy for food, will eat anything he gets. And the same logic applies here.


We will start picking whatever edge we get first.


Let us take the below Graph with lengths/weights.


java_Collections

Let us see the edge and its corresponding length.


java_Collections

Now, according to Kruskal's algorithm.


  1. The first thing we will do is, sort the edges according to its length.

    java_Collections
  2. Then, we will start taking the sorted edges and start placing them, exactly in the same way they were placed in the Graph.

1st Step


At first, comes the edge a,b. With the length 1.


java_Collections

2nd Step


Then, we have the edge d,e. With a length 3.


java_Collections

3rd Step


Next, comes the edge a,d. With length 4.


java_Collections

4th Step


Then, comes the edge a,c. With length 5.


java_Collections

5th Step


Next, comes the edge b,e with length 8.


Now, if we place the edge b,e. It forms a cycle ( i.e. b,e,d,a,b).


And a spanning tree shouldn't be having cycles.


So, we do not include the edge b,e.


6th Step


Now, we need to include the edge c,e with length 9. But it also forms a cycle ( i.e. a,c,e,d,a).


So, we cannot include the edge c,e as well.


7th Step


And lastly, we come to the edge c,d with length 12.


But, including the edge c,d also forms a cycle in the graph( i.e. a,c,d,a).


And finally we have the Minimum Spanning Tree using Kruskal's Algorithm.


java_Collections

So, Kruskal's Algorithm is a three step process :


  1. Take the Graph and sort the Edges based on lengths/weights.
  2. Start including the edges based on their sorted length, one by one.
  3. If including an edge forms a cycle. Do not include the edge.

So far so good !


Now, the only concern is, how do you determine, if including an edge forms a cycle or not?


Let us understand it in detail.


How to determine, if including an edge forms a cycle or not?


Let us relook at the above example again.


We have the below Graph :


java_Collections

And we have listed the edges and its corresponding lengths :


java_Collections

The first thing we have done is, sorted the edges according to its length/weight.


java_Collections

Now, the fun begins here,


We will take all the 5 vertices and put them in individual groups.


1st Group - {a}
2nd Group - {b}
3rd Group - {c}
4th Group - {d}
5th Group - {e}

java_Collections

Next, we will start taking the sorted edges and start placing them, one by one.


1st Step


And the first edge is a,b with length 1.


So, the first thing we do is, check if the vertex 'a' and 'b' are in different groups.


And, we can see that vertex 'a' and 'b' are in different groups. So, let us put vertex 'a' and 'b' in one group.


1st Group - {a,b}
2nd Group - {c}
3rd Group - {d}
4th Group - {e}

And let's connect 'a' and 'b'.


java_Collections

2nd Step


And the next edge is d,e with length 3.


Similarly, we check if the vertex 'd' and 'e' are in different groups.


And, we can see that vertex 'd' and 'e' are in different groups. So, let us put vertex 'd' and 'e' in one group.


1st Group - {a,b}
2nd Group - {c}
3rd Group - {d,e}

So, let's connect vertex 'd' and 'e'.


java_Collections

3rd Step


Then, we come to the next edge a,d of length 4.


And, even in this case, we can see that vertex 'a' and 'd' are in different groups.


So, we combine the group of 'a' with the group of 'd'.


And we get,


1st Group - {a,b,d,e}
2nd Group - {c}

So, we connect vertex 'a' and 'd'.


java_Collections

4th Step


Next, comes the edge a,c with length 5.


So, we check if the vertex 'a' and 'c' are in different groups or not.


And found, they are in different groups. So, we place them in the same group.


1st Group - {a,b,d,e,c}

And, connect 'a' and 'c'.


java_Collections

5th Step


Next, comes the edge b,e with length 8.


So, we check if vertices 'b' and 'e' are in the same group or not.


And found that they are in the same group. So, we do not include the edge b,e.


Now, if you look at the graph.


If we place the edge b,e. It forms a cycle (i.e. b,e,d,a,b).


And a spanning tree shouldn't be having cycles.


So, we do not include the edge b,e.


6th Step


Similarly, we check if the next edge c,e with length 9, is in the same group or not.


And they are in the same group and it forms a cycle (i.e. a,c,e,d,a).


So, we do not include them as well.


7th Step


And lastly, we come to the edge c,d with length 12. But they are also in the same group and including the edge c,d also forms a cycle in the graph(i.e. a,c,d,a).


So, we don't include this edge as well.


And the minimum spanning tree using Kruskal's Algorithm is,


java_Collections

Note :We need to understand about Union by Rank and Path Compression. As we would be needing them while writing the code for Kruskal's algorithm.

Complexity of Kruskal's algorithm for Minimum Spanning Tree


Running time of Kruskal's Algorithm : O(n log n)