VOOZH about

URL: https://www.geeksforgeeks.org/dsa/kruskals-minimum-spanning-tree-algorithm-greedy-algo-2/

⇱ Kruskal’s Minimum Spanning Tree (MST) Algorithm - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Kruskal’s Minimum Spanning Tree (MST) Algorithm

Last Updated : 20 Dec, 2025

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.  

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.  It uses the Disjoint Sets to detect cycles.
  • Repeat step 2 until there are (V-1) edges in the spanning tree.

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.

Illustration:

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



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

Comment