(Translated by https://www.hiragana.jp/)
Kruskal’s Minimum Spanning Tree (MST) Algorithm - GeeksforGeeks
Open In App

Kruskal’s Minimum Spanning Tree (MST) Algorithm

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected, and undirected graph is a spanning tree (no cycles and connects all vertices) that has minimum weight. The weight of a spanning tree is the sum of all edges in the tree.  

In Kruskal's algorithm, we sort all edges of the given graph in increasing order. Then it keeps on adding new edges and nodes in the MST if the newly added edge does not form a cycle. It picks the minimum weighted edge at first and the maximum weighted edge at last. Thus we can say that it makes a locally optimal choice in each step in order to find the optimal solution. Hence this is a Greedy Algorithm.

How to find MST using Kruskal's algorithm?

Below are the steps for finding MST using Kruskal's algorithm:

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

Kruskal's Algorithm uses the Disjoint Set Data Structure to detect cycles. 

Illustration:

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


C++
#include <bits/stdc++.h>
using namespace std;

// Disjoint set data struture
class DSU {
    vector<int> parent, rank;

public:
    DSU(int n) {
        parent.resize(n);
        rank.resize(n);
        for (int i = 0; i < n; i++) {
            parent[i] = i;
            rank[i] = 1;
        }
    }

    int find(int i) {
        return (parent[i] == i) ? i : (parent[i] = find(parent[i]));
    }

    void unite(int x, int y) {
        int s1 = find(x), s2 = find(y);
        if (s1 != s2) {
            if (rank[s1] < rank[s2]) parent[s1] = s2;
            else if (rank[s1] > rank[s2]) parent[s2] = s1;
            else parent[s2] = s1, rank[s1]++;
        }
    }
};
bool comparator(vector<int> &a,vector<int> &b){
    if(a[2]<=b[2])return true;
    return false;
}
int kruskalsMST(int V, vector<vector<int>> &edges) {
    
    // Sort all edhes
    sort(edges.begin(), edges.end(),comparator);
    
    // Traverse edges in sorted order
    DSU dsu(V);
    int cost = 0, count = 0;
    
    for (auto &e : edges) {
        int x = e[0], y = e[1], w = e[2];
        
        // Make sure that there is no cycle
        if (dsu.find(x) != dsu.find(y)) {
            dsu.unite(x, y);
            cost += w;
            if (++count == V - 1) break;
        }
    }
    return cost;
}

int main() {
    
    // An edge contains, weight, source and destination
    vector<vector<int>> edges = {
        {0, 1, 10}, {1, 3, 15}, {2, 3, 4}, {2, 0, 6}, {0, 3, 5}
    };
    
    cout<<kruskalsMST(4, edges);
    
    return 0;
}
C Java Python C# JavaScript

Output
Following are the edges in the constructed MST
2 -- 3 == 4
0 -- 3 == 5
0 -- 1 == 10
Minimum Cost Spanning Tree: 19

Time Complexity: O(E * log E) or O(E * log V) 

  • Sorting of edges takes O(E*logE) time. 
  • After sorting, we iterate through all edges and apply the find-union algorithm. The find and union operations can take at most O(logV) time.
  • So overall complexity is O(E*logE + E*logV) time. 
  • The value of E can be at most O(V2), so O(logV) and O(logE) are the same. Therefore, the overall time complexity is O(E * logE) or O(E*logV)

Auxiliary Space: O(E+V), where V is the number of vertices and E is the number of edges in the graph.

Problems based on Minimum Spanning Tree


Kruskal’s Algorithm for Minimum Spanning Tree
Article Tags :
Practice Tags :

Similar Reads