codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

25 / 65 topics
25Kruskal's Algorithm26Prim's Algorithm
Tutorials/Data Structures & Algorithms/Kruskal's Algorithm
🧮Data Structures & Algorithms

Kruskal's Algorithm

Updated 2026-04-20
3 min read

Introduction

Kruskal's Algorithm is a well-known algorithm used to find the Minimum Spanning Tree (MST) of a connected, undirected graph. An MST is a subset of the edges that connects all vertices together, without any cycles and with the minimum possible total edge weight. This algorithm is particularly useful in network design, such as designing efficient road networks or electrical grids.

Prerequisites

Before diving into Kruskal's Algorithm, it's essential to understand the following concepts:

  • Graph Theory: Basic understanding of graphs, vertices, edges, and cycles.
  • Data Structures: Familiarity with data structures like adjacency lists, priority queues (min-heaps), and disjoint sets (union-find).
  • Sorting Algorithms: Knowledge of sorting algorithms, as Kruskal's Algorithm involves sorting edges.

Overview of Kruskal's Algorithm

Kruskal's Algorithm works by iteratively adding the next lightest edge that does not form a cycle with the spanning tree formed so far. The algorithm can be broken down into the following steps:

  1. Sort all the edges in non-decreasing order of their weight.
  2. Initialize a forest (a set of trees), where each vertex is a separate tree.
  3. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If not, include this edge in the MST and perform union operation on two sets.
  4. Repeat step 3 until there are (V-1) edges in the spanning tree, where V is the number of vertices.

Implementation Details

Step-by-Step Explanation

  1. Sort Edges: First, sort all the edges based on their weights. This can be done using any efficient sorting algorithm like quicksort or mergesort.
  2. Initialize Disjoint Sets: Create a disjoint set for each vertex. Each set will represent a tree in the forest.
  3. Iterate and Union-Find: Iterate through the sorted edges, and for each edge, check if the two vertices it connects belong to different sets (i.e., they are not part of the same tree). If they do not belong to the same set, add the edge to the MST and perform a union operation on the two sets.
  4. Stop Condition: The algorithm stops when there are (V-1) edges in the MST.

Code Example

Below is a Python implementation of Kruskal's Algorithm using an adjacency list representation for the graph and a disjoint set data structure:

class Graph:
    def __init__(self, vertices):
        self.V = vertices
        self.graph = []

    # Function to add an edge to the graph
    def add_edge(self, u, v, w):
        self.graph.append([u, v, w])

    # A utility function to find set of an element i (uses path compression)
    def find(self, parent, i):
        if parent[i] == i:
            return i
        return self.find(parent, parent[i])

    # A function that does union of two sets of x and y (uses union by rank)
    def union(self, parent, rank, x, y):
        xroot = self.find(parent, x)
        yroot = self.find(parent, y)

        # Attach smaller rank tree under root of high rank tree
        if rank[xroot] < rank[yroot]:
            parent[xroot] = yroot
        elif rank[xroot] > rank[yroot]:
            parent[yroot] = xroot

        # If ranks are same, then make one as root and increment its rank by one
        else:
            parent[yroot] = xroot
            rank[xroot] += 1

    # The main function to construct MST using Kruskal's algorithm
    def kruskal_mst(self):
        result = []  # This will store the resultant MST
        i = 0  # An index variable, used for sorted edges
        e = 0  # An index variable, used for result[]

        # Step 1: Sort all the edges in non-decreasing order of their weight
        self.graph = sorted(self.graph, key=lambda item: item[2])

        parent = []
        rank = []

        # Create V subsets with single elements
        for node in range(self.V):
            parent.append(node)
            rank.append(0)

        # Number of edges to be taken is equal to V-1
        while e < self.V - 1:

            # Step 2: Pick the smallest edge and increment the index for next iteration
            u, v, w = self.graph[i]
            i += 1
            x = self.find(parent, u)
            y = self.find(parent, v)

            # If including this edge does't cause cycle, include it in result
            if x != y:
                e = e + 1
                result.append([u, v, w])
                self.union(parent, rank, x, y)

        # Print the contents of result[] to display the built MST
        print("Following are the edges in the constructed MST")
        for u, v, weight in result:
            print(f"{u} -- {v} == {weight}")

# Driver code
if __name__ == "__main__":
    g = Graph(4)
    g.add_edge(0, 1, 10)
    g.add_edge(0, 2, 6)
    g.add_edge(0, 3, 5)
    g.add_edge(1, 3, 15)
    g.add_edge(2, 3, 4)

    g.kruskal_mst()

Explanation of the Code

  • Graph Class: Represents the graph with vertices and edges.
  • add_edge Method: Adds an edge to the graph.
  • find Method: Implements path compression for finding the root of a set.
  • union Method: Implements union by rank for merging two sets.
  • kruskal_mst Method: Implements Kruskal's Algorithm, including sorting edges and using union-find operations.

Best Practices

  1. Efficient Sorting: Use efficient sorting algorithms like quicksort or mergesort to sort the edges.
  2. Path Compression and Union by Rank: These techniques in the disjoint set data structure help keep the operations fast.
  3. Edge Cases: Ensure the algorithm handles edge cases, such as graphs with no edges or disconnected components.

Conclusion

Kruskal's Algorithm is a powerful tool for finding the MST of a graph. Its simplicity and efficiency make it suitable for a wide range of applications. By understanding its implementation and best practices, you can effectively use Kruskal's Algorithm in your projects involving network design and optimization problems.


PreviousFloyd-Warshall AlgorithmNext Prim's Algorithm

Recommended Gear

Floyd-Warshall AlgorithmPrim's Algorithm