KRUSKAL’S ALGORITHM FOR FINDING MST

Advait Kumar
4 min readApr 10, 2022

And I’m here to discuss about my favourite algorithm for finding the Minimum Cost Spanning Tree.
And yes, you guessed it right.
In this blog, we will discuss about Kruskal’s Algorithm.
But before moving directly towards the Kruskal’s algorithm, we should first understand basic terms such as spanning tree and minimum spanning tree.

Spanning tree — A spanning tree is the subgraph of an undirected complete graph.

Here all the spanning trees which are possible for the given complete graph.

Minimum Spanning tree — Minimum spanning tree can be defined as the spanning tree in which the sum of the weights of the edge is minimum.
Here is an example of a minimum spanning tree.

Here the cost of the Minimum spanning tree is 7.

Now let’s move to our main topic
That is Kruskal’s Algorithm.
Kruskal’s algorithm uses the greedy approach for finding a minimum spanning tree.
Kruskal’s algorithm treats every node as an independent tree and connects one with another only if it has the lowest cost compared to all other options available.

Steps to Kruskal’s algorithm:

Sort the graph edges with respect to their weights.

Start adding edges to the minimum spanning tree from the edge with the smallest weight until the edge of the largest weight.

Only add edges which don’t form a cycle — edges which connect only disconnected components.

To understand Kruskal’s Algorithm more clearly, and in depth,
Let’s take an example.

Write down the weights of all the edges in the above graph

Now, sort the edges in the ascending order of their weights.

Now start Adding edges with least weight sequentially ;

We begin with the edges with least weight/cost.

Step -3

Now, Add the edge BC with weight 3 to the MST, as it is not creating any cycle or loop.

Step-4
Now, pick the edge CD with weight 4 to the MST, as it is not forming the cycle

So, the final minimum spanning tree obtained from the given weighted graph by using Kruskal’s algorithm is

Minimum cost of the spanning tree:

  • The cost of the MST is = AB + DE + BC + CD = 1 + 2 + 3 + 4 = 10.

Complexity of Kruskal’s Algorithm

Kruskal’s algorithm’s time complexity is O(E log V)

where :

E : no. of edges

V : no. of vertices\nodes

A program to implement kruskal’s algorithm in C++.

  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. const int MAX = 1e4 + 5;
  5. int id[MAX], nodes, edges;
  6. pair <long long, pair<int, int> > p[MAX];
  7. void init()
  8. {
  9. for(int i = 0;i < MAX;++i)
  10. id[i] = i;
  11. }
  12. int root(int x)
  13. {
  14. while(id[x] != x)
  15. {
  16. id[x] = id[id[x]];
  17. x = id[x];
  18. }
  19. return x;
  20. }
  21. void union1(int x, int y)
  22. {
  23. int p = root(x);
  24. int q = root(y);
  25. id[p] = id[q];
  26. }
  27. long long kruskal(pair<long long, pair<int, int> > p[])
  28. {
  29. int x, y;
  30. long long cost, minimumCost = 0;
  31. for(int i = 0;i < edges;++i)
  32. {
  33. x = p[i].second.first;
  34. y = p[i].second.second;
  35. cost = p[i].first;
  36. if(root(x) != root(y))
  37. {
  38. minimumCost += cost;
  39. union1(x, y);
  40. }
  41. }
  42. return minimumCost;
  43. }
  44. int main()
  45. {
  46. int x, y;
  47. long long weight, cost, minimumCost;
  48. init();
  49. cout <<”Enter Nodes and edges”;
  50. cin >> nodes >> edges;
  51. for(int i = 0;i < edges;++i)
  52. {
  53. cout<<”Enter the value of X, Y and edges”;
  54. cin >> x >> y >> weight;
  55. p[i] = make_pair(weight, make_pair(x, y));
  56. }
  57. sort(p, p + edges);
  58. minimumCost = kruskal(p);
  59. cout <<”Minimum cost is “<< minimumCost << endl;
  60. return 0;
  61. }

Applications of Minimum Spanning Tree Problem :

Network design.
– telephone, electrical, hydraulic, TV cable, computer, road

Approximation algorithms for NP-hard problems.
– traveling salesperson problem, Steiner tree

Indirect applications.
– max bottleneck paths
– LDPC codes for error correction
– image registration with Renyi entropy
– learning salient features for real-time face verification
– reducing data storage in sequencing amino acids in a protein
– model locality of particle interactions in turbulent fluid flows
– autoconfig protocol for Ethernet bridging to avoid cycles in a network.

--

--