Time Complexity of the Kruskal Algorithm?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Kruskal's algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a connected, undirected graph with weighted edges. Named after Joseph Kruskal, it is one of the most popular algorithms in graph theory and is often used in network design and other applications. A central aspect of evaluating the efficiency of this algorithm lies in understanding its time complexity.
Basics of Kruskal's Algorithm
To execute Kruskal's algorithm, follow these general steps:
- Sort all the edges in non-decreasing order of their weight.
- Initialize a forest with
(n)trees, each containing one of the(n)vertices. - Iteratively add edges in increasing order of their weight, ensuring that no cycles are formed by adding an edge. If adding an edge creates a cycle, discard that edge.
- Repeat step 3 until there are
(n-1)edges in the forest (which will now become a single tree).
Time Complexity Analysis
The time complexity of the Kruskal algorithm is determined by the following steps:
1. Sorting the Edges
Sorting the edges is the most computationally expensive operation in the Kruskal algorithm. Given E
edges, a typical sorting algorithm such as Merge Sort or Python's sorted()
function has a complexity of .
2. Creating a Forest
Creating a forest with a separate tree for each vertex is straightforward. This operation is O(V)
, where V
is the number of vertices.
3. Union-Find Operations
Union-Find (or Disjoint Set Union, DSU) is used to efficiently manage the connected components and check for cycle formation. If implemented with path compression and union by rank, the operations find
and union
can be performed in near constant time, given by , where is the inverse Ackermann function, making its time complexity almost for most practical purposes.
The total time complexity for performing these operations over all E
edges is .
Summary
Given the steps above, the overall time complexity of Kruskal's algorithm is dominated by the sorting step:
This is because will significantly surpass for large graphs.
Example
Let's look at a simple example graph to illustrate Kruskal's algorithm:
4 | 5 |
Related reading
- Time Complexity Of This Code Snippet
- Time Complexity of two for loops
- Time complexity to generate all pairs in an array
- Time/Space Complexity of Depth First Search
- To print the boundary of Binary Tree
- .toArraynew MyClass0 or .toArraynew MyClassmyList.size?
- Time cost of training with pytorch DDP with multi-GPUs
- Timed annotation in spring metrics

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.