Introduction
A spanning tree of a connected undirected graph is a subset of that connects all vertices and forms a tree ( edges, no cycle). A minimum spanning tree (MST) is a spanning tree of minimum total edge weight. Kruskal's and Prim's algorithms both grow an MST by the greedy cut property and are the GATE syllabus algorithms for this topic.
Prerequisites
- Greedy Algorithms: greedy-choice property and safe edges.
- Graph Representations: adjacency lists for Prim.
- Union-Find (Disjoint Set) for Kruskal: makeset, find, union in nearly constant amortized time.
Core concepts
Cut property
A cut partitions the vertices. An edge crosses the cut if its ends lie in different sides. A light edge for the cut is a crossing edge of minimum weight. If is a subset of some MST and the cut respects (no edge of crosses), then every light edge crossing the cut is safe to add to (CLRS 3e, Theorem 23.1).
Cycle property
If an edge is a maximum-weight edge on some cycle, then some MST avoids (CLRS Exercise 23.1-5). If is the unique heaviest edge on that cycle, then no MST contains : any spanning tree that uses can swap it for a lighter edge of the cycle that crosses the same cut. Kruskal's rejections are exactly this rule in action: a rejected edge closes a cycle whose other edges were all accepted earlier and are no heavier.
Cut versus cycle. The cut property certifies an edge in an MST (lightest across some cut); the cycle property certifies an edge out of it (heaviest on some cycle). Every edge of an MST is a light edge for some cut (CLRS Exercise 23.1-3).
Algorithms and pseudocode
Pseudocode uses <- for assignment. Kruskal uses a disjoint-set structure; Prim uses a min-priority queue keyed by key.
Kruskal
MST-KRUSKAL(G, w)
1 A <- empty set
2 for each vertex v in G.V
3 MAKE-SET(v)
4 sort the edges of G.E into nondecreasing order by weight w
5 for each edge (u, v) in that order
6 if FIND-SET(u) != FIND-SET(v) then
7 A <- A union {(u, v)}
8 UNION(u, v)
9 return ATime. Sorting ; the Union-Find loop is . Overall (or since ).
Prim
MST-PRIM(G, w, r)
1 for each u in G.V
2 u.key <- infinity
3 u.pi <- NIL
4 r.key <- 0
5 Q <- G.V // min-priority queue on key
6 while Q is not empty
7 u <- EXTRACT-MIN(Q)
8 for each v in Adj[u]
9 if v in Q and w(u, v) < v.key then
10 v.pi <- u
11 v.key <- w(u, v) // DECREASE-KEY
12 return {(v.pi, v) : v in G.V - {r}}Time. Binary heap: . Fibonacci heap: . Adjacency-matrix Prim: .
| Algorithm and data structure | Time | Best for |
|---|---|---|
| Kruskal, comparison sort plus union-find | sparse graphs, edges given as a list | |
| Prim, array of keys (scan for the minimum) | dense graphs, | |
| Prim, binary heap | , that is for connected graphs | sparse graphs |
| Prim, Fibonacci heap | theory; dense and sparse |
On a dense graph, array-based Prim's beats the binary-heap version's .
Edge cases. Disconnected graphs have spanning forests; both algorithms extend naturally (Kruskal already produces a forest; Prim needs a loop over unused roots). Equal weights: MSTs need not be unique; both algorithms are correct for any tie-break. Negative weights are allowed (unlike Dijkstra).
Union-Find cost in Kruskal
With path compression and union by rank the disjoint-set operations run in amortized time, where is the inverse Ackermann function (at most 4 for all practical ). The bottleneck is therefore the initial sort of the edges. If weights are integers in a small range, counting sort or radix sort can replace the comparison sort and the Union-Find term may dominate, giving nearly linear time in .
Prim versus Dijkstra
Both algorithms maintain a priority queue of unsettled vertices and repeatedly extract the minimum key. The difference is the key:
| Prim | Dijkstra | |
|---|---|---|
| Key of | weight of the lightest edge from the tree to | tentative distance |
| Relaxation | key[v] <- min(key[v], w(u,v)) | d[v] <- min(d[v], d[u]+w(u,v)) |
| Requires | undirected graph | directed or undirected; nonnegative weights |
Replacing Prim's key update by Dijkstra's yields a shortest-path tree, not an MST. Triangle with and : every MST contains and one of the two edges at , total 3. The shortest-path tree from uses and (distances 2 and 2), total 4, so it is not an MST.
Transforming the weights
Every spanning tree has exactly edges, and Kruskal looks only at the order of the weights. Hence:
- Adding a constant to every weight keeps every MST an MST; each tree's weight grows by . Shortest paths behave differently: paths with more edges gain more, so shortest paths can change (Single-Source Shortest Paths).
- Multiplying by a positive constant, or applying any strictly increasing function (squaring positive weights, for example), keeps the same MSTs.
- Negative weights need no special handling.
- Maximum spanning tree: negate all weights (or sort in decreasing order) and run either algorithm.
- Bottleneck. An MST also minimises the largest edge weight among all spanning trees.
- Singleton cuts. For each vertex, a lightest incident edge belongs to some MST; if it is the unique lightest, it belongs to every MST.
Mathematical explanation
Correctness via the cut property
Kruskal's accepted edge is light for the cut separating the two components it joins, so it is safe. Prim's extracted vertex is joined by a light edge across the cut , so that edge is safe. Induction shows the final set is an MST.
Uniqueness
If every cut has a unique light edge, the MST is unique. The converse is false: a graph can have a unique MST while some cut has several light edges of equal weight (CLRS Exercise 23.1-6). Path with both edges of weight 1 is its own unique spanning tree, yet the cut has two light edges.
Distinct weights give a unique MST (CLRS Problem 23-1), because then every cut has a unique light edge. With ties, several MSTs can exist: with all six weights equal has spanning trees, and every one is an MST. All MSTs of a graph share the same sorted list of edge weights (CLRS Exercise 23.1-8).
Second-best MST
A second-best MST is a spanning tree of least weight among those that are not the (unique) MST. It differs from the MST by a single swap: add one non-tree edge and delete the heaviest MST edge on the tree path from to . Its weight is the minimum over non-tree edges of , and it need not be unique (CLRS Problem 23-1).
Worked examples
One graph, both algorithms
Vertices . Edges with weights: , , , , , .
Kruskal (sorted): .
- Take (components ).
- Take (components ).
- Take (connects ). Stop. Reject as cycles.
MST edges , weight .
Prim from , keys of the vertices still in the queue after each EXTRACT-MIN (a blank cell means the vertex has already been extracted):
| Extracted | key | key | key | Edge added |
|---|---|---|---|---|
| 1 | 4 | 3 | none (root) | |
| 2 | 3 | (1) | ||
| 3 | (2) | |||
| (3) |
Extracting lowers 's key from 4 to 2 (edge ) but leaves at 3, since . Same tree as Kruskal.
Second-best tree for this graph. Try each non-tree edge: (4) closes , whose heaviest tree edge is (2), giving ; (5) closes , dropping (3), giving 8; (6) closes , dropping , giving 9. The second-best weight is 8, reached by two different trees, and . The graph has 16 spanning trees in all, and the MST of weight 6 is unique because the weights are distinct.


Implementation checklist for MST questions
Before coding Kruskal, confirm the graph is undirected and that the Union-Find structure initialises one set per vertex. Sort edges stably only if the problem asks for a particular MST among ties; any total order by weight is correct for the weight optimum. Before coding Prim, initialise every key to infinity except the root, and treat the priority queue as containing all vertices (decrease-key) rather than inserting neighbours lazily, unless the lazy version is analysed carefully for duplicate entries.
For hand traces, draw/save the cut immediately before accepting each edge: for Kruskal use a pre-union component versus the rest; for Prim use the previously grown tree versus the rest. The accepted edge must be light across that cut. This picture is the fastest way to catch an off-by-one in a weight table during an exam.
Important properties and observations
- MST weight is unique even when several MSTs exist; the sorted list of MST edge weights is the same for every MST (CLRS Exercise 23.1-8).
- Prim resembles Dijkstra but keys are edge weights to the tree, not path weights from the root.
- Kruskal needs sorting globally; Prim needs a dynamic priority queue.
- Updating an MST after decreasing a tree edge keeps the same tree; after decreasing a non-tree edge, add it and delete the heaviest edge on the created cycle.
Common misconceptions.
- "Prim requires nonnegative weights." False; Prim and Kruskal allow negatives.
- "Dijkstra's tree is an MST." False in general: shortest-path trees minimise path weights from , not total edge weight.
- "The MST is unique." Only under stricter conditions (for example distinct weights, or a unique light edge for every cut).
- "Adding a constant to all weights can change the MST." It cannot, because every spanning tree has edges. It can change shortest paths.
GATE examination perspective
- TRACE KRUSKAL. Sort edges; skip those that unite the same component.
- TRACE PRIM. Show
keyand after each extract-min. - CUT ARGUMENT. Name a cut and its light edge to justify an MST edge.
- COMPLEXITY. Kruskal ; Prim binary-heap ; matrix Prim .
- MST VERSUS SHORTEST PATHS. Different objectives; the triangle with weights 2, 2, 1 separates them.
- DISTINCT WEIGHTS. Unique MST. With ties, count trees carefully; the MST weight is still unique.
- WEIGHT TRANSFORMS. Adding a constant or applying an increasing function preserves the MST.
- SECOND-BEST. One edge swap from the MST; compute on the cycle for each non-tree edge .
- DATA STRUCTURE. Dense graph: array Prim . Sparse graph: Kruskal or heap Prim .
Summary
Sources
- T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009: Chapter 23, Sections 23.1 (growing an MST) and 23.2 (Kruskal and Prim); Exercises 23.1-3, 23.1-5, 23.1-6 and 23.1-8; Problem 23-1 (second-best minimum spanning tree).