Given a connected and weighted undirected graph, construct a minimum spanning tree out of it using Kruskal’s Algorithm.

A Minimum Spanning Tree is a spanning tree of a connected, undirected graph. It connects all the vertices with minimal total weighting for its edges.

Kruskal’s Algorithm – Step 1

For example, consider the above graph. Its minimum spanning tree will be the following tree with exactly n-1 edges where n is the total number of vertices in the graph, and the sum of weights of edges is as minimum as possible:

Kruskal’s Algorithm

Practice this problem

 
Prerequisite:

Union–Find Algorithm for cycle detection in a graph

 
We can use Kruskal’s Minimum Spanning Tree algorithm, a greedy algorithm to find a minimum spanning tree for a connected weighted graph. Kruskal’s Algorithm works by finding a subset of the edges from the given graph covering every vertex present in the graph such that they form a tree (called MST), and the sum of weights of edges is as minimum as possible.

Let G = (V, E) be the given graph. Initially, our MST contains only vertices of the given graph with no edges. In other words, initially, MST has V connected components, with each vertex acting as one connected component. The goal is to add minimum weight edges to our MST such that we are left with a single connected component that comprises all the graph’s vertices. Following is the complete algorithm:

sort all edges in graph G in order of their increasing weights;
repeat V-1 times    // as MST contains V-1 edges
{
    select the next edge with minimum weight from graph G;
 
    if (no cycle is formed by adding the edge in MST, i.e., the edge connects two
            different connected components in MST)
        add the edge to MST;
}

Let’s illustrate this by taking the example of the above graph. Initially, our MST consists of only the vertices of the given graph with no edges. We start by considering the smallest weighted edge 0–3 having weight 5. As no cycle is formed, include it in our MST.

Kruskal’s Algorithm – Step 2

We next consider the smallest weighted edge 2–4 also having weight 5. As no cycle is formed, include it in our MST.

Kruskal’s Algorithm – Step 3

We next consider the smallest weighted edge 3–5 having weight 6. As no cycle is formed, include it in our MST.

Kruskal’s Algorithm – Step 4

We next consider the smallest weighted edge 0–1 having weight 7. As no cycle is formed, include it in our MST.

Kruskal’s Algorithm – Step 5

We next consider the smallest weighted edge 1–4 also having weight 7. As no cycle is formed, include it in our MST.

Kruskal’s Algorithm – Step 6

We next consider the smallest weighted edge 5–4 having weight 8. But including this edge in MST will result in a cycle 0—1—4—5—3—0, so we discard it.

Kruskal’s Algorithm – Step 7

We next consider the smallest weighted edge 1–2 also having weight 8. But including this edge in MST will result in a cycle 1—2—4—1, so we discard it.

Kruskal’s Algorithm – Step 8

We next consider the smallest weighted edge 3–1 also having weight 9. But including this edge in MST will result in a cycle 0—1—3—0, so we discard it.

Kruskal’s Algorithm – Step 9

Finally, consider the next smallest weighted edge, 4–6, also weighting 9. As no cycle is formed, include it in our MST.
Kruskal’s Algorithm – Step 10

MST is now connected (containing V-1 edges). So, we discard all remaining edges.

Kruskal’s Algorithm – Step 11

Following is the pseudocode of Kruskal’s Algorithm as per Wikipedia. It uses a disjoint–set data structure.

KRUSKAL(graph G)
 
MST = {}
 
for each vertex v belonging G.V:
    MAKE-SET(v)
 
for each (u, v) in G.E ordered by weight(u, v), increasing:
    if FIND-SET(u) != FIND-SET(v):
        add {(u, v)} to set MST
        UNION(u, v)
 
return MST

Please note that if the graph is not connected, Kruskal’s Algorithm finds a Minimum Spanning Forest, a minimum spanning tree for each connected component of the graph.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

(2, 4, 5)
(0, 3, 5)
(3, 5, 6)
(1, 4, 7)
(0, 1, 7)
(4, 6, 9)

Java


Download  Run Code

Output:

[(0, 3, 5), (2, 4, 5), (3, 5, 6), (0, 1, 7), (1, 4, 7), (4, 6, 9)]

Python


Download  Run Code

Output:

[(0, 3, 5), (2, 4, 5), (3, 5, 6), (0, 1, 7), (1, 4, 7), (4, 6, 9)]

The time complexity of the above solution is O(n2), where n is the total number of vertices in the graph. The time complexity can be improved to O(n.log(n)) by using the optimized implementation of Union and Find operations.

 
References:

1. https://en.wikipedia.org/wiki/Kruskal%27s_algorithm

2. http://lcm.csa.iisc.ernet.in/dsa/node184.html