Mininum spanning tree algorithms

A spanning tree of a weighted, connected, undirected graph G whose total edge weight is less than or equal to the weight of every other possible spanning tree is called a Minimum Spanning Tree (MST). The weight of a spanning tree is the sum of all the weights assigned to each of its edges. The two most popular algorithms to find an MST are Kruskal's Algorithm and Prim's Algorithm.

Kruskal's Algorithm

Kruskal's algorithm is a greedy algorithm that builds the MST by picking the smallest weighted edge at each step, as long as it does not form a cycle.

Algorithm Steps

  1. Sort all edges of the graph in non-decreasing order of their weight.
  2. Pick the smallest weighted edge. Check if it forms a cycle with the spanning tree formed so far.
  3. If no cycle, include this edge in the MST. Otherwise, discard it.
  4. Repeat steps 2 and 3 until (V − 1) edges are in the spanning tree.

Problem

Find the minimum spanning tree for the following graph G using Kruskal's algorithm ?

20 9 13 1 4 5 2 3 14 a b c d e f

Solution

First, list all edges sorted by weight in ascending order −

Edge Vertex Pair Weight Action
E4 (b, c) 1 Include
E7 (c, d) 2 Include
E8 (d, e) 3 Include
E5 (b, e) 4 Discard (forms cycle b-c-d-e)
E6 (b, f) 5 Include
E2 (a, c) 9 Include

We now have 5 edges (V − 1 = 6 − 1 = 5), so the algorithm stops.

MST by Kruskal's (Total Weight = 20) 9 1 5 2 3 a b c d e f

The MST edges are (b,c)=1, (c,d)=2, (d,e)=3, (b,f)=5, and (a,c)=9. Total weight = 1 + 2 + 3 + 5 + 9 = 20.

Prim's Algorithm

Prim's algorithm, discovered in 1930 by mathematicians Vojtěch Jarník and Robert C. Prim, is a greedy algorithm that builds the MST by starting from a single vertex and growing the tree one edge at a time, always choosing the cheapest edge that connects the tree to a new vertex. Prim's algorithm is faster on dense graphs.

Algorithm Steps

  1. Initialize the MST with a single vertex, chosen from the graph.
  2. Select the minimum weight edge connecting the tree to a vertex not yet in the tree, ensuring no cycle is formed.
  3. Add the selected edge and the new vertex to the tree.
  4. Repeat steps 2 and 3 until all vertices are included.

Problem

Find the minimum spanning tree for the same graph G using Prim's algorithm, starting from vertex 'a' ?

Solution

Starting from vertex 'a', we grow the tree step by step −

Step Vertices in MST Edge Added Weight Reason
1 {a} Start vertex
2 {a, c} (a, c) 9 Smallest edge from a
3 {a, c, b} (b, c) 1 Smallest edge from {a, c}
4 {a, c, b, d} (c, d) 2 Smallest edge to new vertex
5 {a, c, b, d, e} (d, e) 3 Smallest edge to new vertex
6 {a, c, b, d, e, f} (b, f) 5 Smallest edge to new vertex

The MST contains edges (a,c)=9, (b,c)=1, (c,d)=2, (d,e)=3, and (b,f)=5. Total weight = 1 + 2 + 3 + 5 + 9 = 20.

Both algorithms produce the same MST with a total weight of 20.

Conclusion

Kruskal's algorithm sorts all edges globally and picks the smallest that doesn't form a cycle, while Prim's algorithm grows the tree from a starting vertex by always adding the cheapest adjacent edge. Both produce the same minimum spanning tree with optimal total weight.

Updated on: 2026-03-14T08:20:54+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements