In this article I show you the implementation of Kruskal’s Algorithm using C and C++ Programming Languages. This a popular computer science algorithm which is directly based on the generic Minimum Spanning Tree (MST) algorithm. A minimum spanning tree is a subgraph of the graph (a tree) with the minimum sum of edge weights.

Kruskal’s algorithm is a greedy algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph. It finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized.

Table of Contents

Key Concepts

  1. Graph: A collection of vertices (or nodes) and edges connecting pairs of vertices.
  2. Weighted Graph: A graph where each edge has a numerical value (weight) associated with it.
  3. Minimum Spanning Tree (MST): A spanning tree of a graph whose total weight is minimized.
minimum spanning tree (MST)
Left Image: The original graph, where the numbers on the edges represent their weights. Right Image: The MST of the graph, highlighting the subset of edges that connect all vertices with the minimum total weight.

Kruskal’s algorithm addresses two problems as mentioned below.

  • PROBLEM 1. Give a practical method for constructing a spanning subtree of minimum length.
  • PROBLEM 2. Give a practical method for constructing an unbranched spanning subtree of minimum length.

Kruskal’s algorithm is most suitable for sparse graphs (low number of edges).  This algorithm is practically used in many fields such as Traveling Salesman Problem, Creating Mazes and Computer Networks etc. It is also helpful in cluster analysis in machine learning as well as geographical mapping.

This algorithm initially appeared in “On the Shortest Spanning Subtree of a Graph and the Traveling Salesman Problem” research paper by Joseph B. Kruskal, Jr. in the proceedings of the American Mathematical Society Vol. 7, No. 1 (Feb., 1956), pp. 48-50.

Kruskal’s Algorithm Steps

  1. Sort all the edges in the graph in non-decreasing order of their weight.
  2. Initialize a forest (a set of trees), where each vertex in the graph is a separate tree.
  3. Initialize an empty set for the MST.
  4. Process each edge in the sorted order:
    • Check if the current edge forms a cycle with the spanning tree formed so far.
    • If it does not form a cycle, include this edge in the MST.
    • If it forms a cycle, discard the edge.
  5. Repeat until there are V−1V−1 edges in the MST (where VV is the number of vertices in the graph).

Pseudo code of the Kruskal’s Algorithm

Complexity of Kruskal’s Algorithm

Time Complexity: The time complexity Of Kruskal’s Algorithm is: O(ElogE+ElogV) where E is number of edges and V is the number of vertices. Sorting the edges takes 0(ElogE)O(ElogE) and the union-find operations take 0(ElogV)O(ElogV).

Space Complexity: 0(V+E)O(V+E), due to the storage required for the graph representation and the Union-Find data structure.

Explanation:

Kruskal’s algorithm takes o(e log e) time in sorting of the edges. Here e is numbers of edges and v is the number of vertices in the graph. Further, it iterates all edges and runs a subroutine to find the cycles in the graph which is called union-find algorithm. The union-find algorithm requires o(log v) time and is applied after sorting of edges is completed.

So, Overall Kruskal’s algorithm requires o(e log v) time to run.

Applications of Kruskal’s Algorithm

Kruskal’s algorithm is significant in data processing and data management because it efficiently finds the Minimum Spanning Tree (MST) of a weighted graph. This has various applications, including:

1. Network Design and Optimization

  • Used in designing efficient communication networks (e.g., laying out fiber-optic cables, telephone networks, and electrical grids).
  • Helps in reducing costs by ensuring minimal total weight (cost/distance) for connections.

2. Clustering in Data Mining

  • Kruskal’s algorithm is used in hierarchical clustering, particularly in single linkage clustering.
  • Helps group similar data points together based on the shortest connection.

3. Image Processing

  • Applied in segmentation of images by grouping similar regions based on edge weights.
  • Used in graph based segmentation techniques.

4. Geographic Information Systems (GIS)

  • Helps in computing optimal road maps and route planning.
  • Assists in finding minimum cost paths for transportation networks.

5. Data Management and Distributed Systems

  • Helps in reducing redundancy in database relationships and query optimization.
  • Assists in structuring distributed databases to optimize communication.

6. Social Network Analysis

  • Used to identify strongly connected components with minimum connection costs.

C Programming Implementation of Kruskal’s Algorithm

Here is the C program that implements this algorithm.

Output of the C Program

Implementation of Kruskal’s Algorithm

Enter the no. of vertices:3

Enter the cost adjacency matrix:

9

8

7

6

5

4

3

2

3

The edges of Minimum Cost Spanning Tree are

1 edge (3,2) =2

2 edge (3,1) =3

Minimum cost = 5

C++ Programming Implementation of Kruskal’s Algorithm

Python Implementation of Kruskal’s Algorithm

Here is a sample implementation of Kruskal’s Algorithm in Python using the Disjoint Set (Union-Find) data structure for efficiency. The edges are sorted by weight to ensure we always pick the smallest edge. It iterates through the edges and adds them to the MST if they don’t form a cycle.

Elon Musk
Discover the mind behind the innovations – Elon Musk by Walter Isaacson, now on Audible. Dive into the life of a visionary shaping our future!
View on Amazon

You can also view implementation of sorting algorithms in C here: Shell SortQuick SortInsertion SortSelection Sort