1. Gyanastra
  2. Algorithms
  3. UGC NET: Graph Algorithms Extras
  4. Minimum Spanning Trees
Free lesson, no account needed 12 min read 10 practice questions after it
Asked in past papers1 time
  • ISRO2023

Algorithms · UGC NET: Graph Algorithms Extras

Minimum Spanning Trees

Overview

A spanning tree of a connected undirected graph is a subgraph that touches every vertex and contains no cycle; among all such trees, the one of least total edge weight is the Minimum Spanning Tree (MST). MST is the standard example of a greedy algorithm that is provably optimal, and it is the backbone of network-design problems (laying optical fibre between towns, wiring a campus, clustering).
It sits in the Graph Algorithms extras of the syllabus, next to shortest-path and traversal algorithms.
Before studying it you must be comfortable with: connected and acyclic graphs, trees and their edge count, cuts, cycles, sorting, disjoint-set (union–find), and priority queues.
Prim's and Kruskal's algorithms, the cut and cycle properties, and the running-time table are the examinable core.

Key concepts

  • Spanning tree: a subgraph TT of a connected graph GG that is connected, acyclic, and contains all nn vertices. TT has exactly n−1n-1 edges.
    Conversely, a connected graph with nn vertices and n−1n-1 edges, or an acyclic graph with nn vertices and n−1n-1 edges, is a tree.
  • MST: a spanning tree of minimum total weight ∑e∈Tw(e)\sum_{e\in T} w(e). It exists whenever GG is connected. If GG is disconnected, the analogue is a minimum spanning forest (one MST per component).
  • Cut: a partition of VV into two non-empty sets (S,V−S)(S, V-S). An edge crosses the cut if its endpoints lie on different sides.
  • Light edge: a minimum-weight edge crossing a given cut.
  • Cut property (CLRS Theorem 23.1): for any cut, a light edge crossing it is safe — it belongs to some MST. If that light edge is strictly lighter than every other crossing edge, it belongs to every MST.
  • Cycle property: for any cycle, a strictly heaviest edge of the cycle belongs to no MST. With ties, some MST avoids that edge.
  • Uniqueness: if all edge weights are distinct, the MST is unique. Equal weights may give several MSTs, but every MST has the same number of edges and the same sorted multiset of edge weights.
  • Greedy skeleton: repeatedly add an edge that does not create a cycle and is lightest possible — this generic algorithm is correct by the cut property.
  • Kruskal: sorts all mm edges and adds the next lightest edge that joins two different components. Uses a disjoint-set / union–find structure (with path compression and union by rank) to test connectivity.
  • Prim: starts from a chosen root and repeatedly adds the lightest edge leaving the current tree. Uses an array of key values (dense graphs) or a priority queue (sparse graphs).
  • Borůvka (third greedy method): every component simultaneously picks its lightest outgoing edge; O(mlog⁡n)O(m\log n).
  • Distinct weights guarantee a unique MST; a connected graph with nn vertices and n−1n-1 edges is itself its own unique spanning tree.

How it works

Kruskal's algorithm

  1. Sort all mm edges in non-decreasing order of weight.
  2. Make a DSU set for each vertex (each vertex is its own component). Set T=∅T=\varnothing.
  3. Take the next edge e=(u,v)e=(u,v). If find(u)≠find(v)\text{find}(u)\ne \text{find}(v), add ee to TT and union(u,v)\text{union}(u,v); otherwise discard ee (it would close a cycle).
  4. Stop when TT has n−1n-1 edges (or when edges run out).

Correctness rests on the cycle property: an edge that closes a cycle is the heaviest on it, so some MST avoids it. Sorting dominates the cost, O(mlog⁡m)O(m\log m); with DSU operations costing O(α(n))O(\alpha(n)) amortised, the rest is nearly linear.

Prim's algorithm

  1. Choose any start vertex rr; set key(r)=0\text{key}(r)=0, key(v)=∞\text{key}(v)=\infty for others, all vertices unvisited.
  2. Repeatedly pick the unvisited vertex uu with the smallest key, mark it visited; the edge that gave uu its key is added to the tree (except for rr).
  3. For every unvisited neighbour vv of uu, if w(u,v)<key(v)w(u,v)<\text{key}(v), set key(v)=w(u,v)\text{key}(v)=w(u,v) and parent(v)=u\text{parent}(v)=u.
  4. After nn extractions the tree is complete.

Why greedy works: at every step the tree's vertex set defines a cut; the lightest edge crossing it is the lightest edge incident to the tree, and by the cut property it is safe.
So no greedy choice can destroy optimality — this is the exchange argument: replacing an edge of an optimal tree by the chosen lighter edge keeps a spanning tree and cannot increase weight.

Handling ties and negatives: edge weights may be negative; only comparisons matter, so both algorithms stay correct (no "distance" is being accumulated).
With equal weights, tie-breaking decides which MST you output, never its weight. A maximum spanning tree is obtained by negating all weights (or reversing the sort).

Figure. Notice the cycle check: edges whose endpoints are already in the same DSU component are discarded; otherwise the edge is added to the MST and the sets are unioned. The loop stops after n-1 edges.

Worked examples

Example 1 (Kruskal, simple–medium). Graph on A,B,C,D,E,FA,B,C,D,E,F with edges (A,B)=1(A,B)=1, (C,E)=2(C,E)=2, (B,D)=3(B,D)=3, (A,C)=4(A,C)=4, (B,C)=5(B,C)=5, (E,F)=6(E,F)=6, (D,F)=7(D,F)=7, (A,D)=8(A,D)=8, (C,F)=9(C,F)=9.

Sorted order: 1, 2, 3, 4, 5, 6, 7, 8, 9. Take AB=1AB=1; take CE=2CE=2; take BD=3BD=3; take AC=4AC=4 (merges {A,B,D}\{A,B,D\} with {C,E}\{C,E\}); BC=5BC=5 is rejected because BB and CC are already connected via B−A−CB-A-C; take EF=6EF=6, which completes n−1=5n-1=5 edges.
Tree: AB,CE,BD,AC,EFAB,CE,BD,AC,EF; weight =1+2+3+4+6=16=1+2+3+4+6=16. Note DF=7DF=7 was never examined — stopping early at n−1n-1 edges is valid.

Example 2 (Prim trace, medium). Graph: A ⁣− ⁣B=4A\!-\!B=4, A ⁣− ⁣C=2A\!-\!C=2, B ⁣− ⁣C=1B\!-\!C=1, B ⁣− ⁣D=5B\!-\!D=5, C ⁣− ⁣D=8C\!-\!D=8, C ⁣− ⁣E=10C\!-\!E=10, D ⁣− ⁣E=2D\!-\!E=2, D ⁣− ⁣F=6D\!-\!F=6, E ⁣− ⁣F=3E\!-\!F=3. Start at AA.

StepTree verticesCandidate crossing edgesChosen
1AAB 4, AC 2AC = 2
2A,CAB 4, CB 1, CD 8, CE 10CB = 1
3A,B,CBD 5, CD 8, CE 10BD = 5
4A,B,C,DDE 2, DF 6, CE 10DE = 2
5A,B,C,D,EEF 3, DF 6EF = 3

Edges added in order: AC,CB,BD,DE,EFAC, CB, BD, DE, EF; total 2+1+5+2+3=132+1+5+2+3=13. Kruskal on the same graph gives the same weight (BC=1,AC=2,DE=2,EF=3,BD=5BC=1, AC=2, DE=2, EF=3, BD=5), confirming optimality. If two crossing edges tie, either may be picked, so several edge orders are possible but the total weight is fixed.

Example 3 (hard, two-step). n=10n=10 vertices v1,…,v10v_1,\dots,v_{10}; edge between vi,vjv_i,v_j iff 0<∣i−j∣≤20<|i-j|\le 2; weight =i+j=i+j. Find the MST cost.

Step 1 — list edges by weight: i+ji+j ranges from 1+2=31+2=3 upward. For each sum, only pairs with ∣i−j∣≤2|i-j|\le2 exist.

WeightEdgeAction
3(1,2)accept
4(1,3)accept
5(2,3)reject (cycle 1-2-3)
6(2,4)accept
7(3,4)reject
8(3,5)accept
9(4,5)reject
10(4,6)accept
11(5,6)reject
12(5,7)accept
13(6,7)reject
14(6,8)accept
15(7,8)reject
16(7,9)accept
17(8,9)reject
18(8,10)accept

Step 2 — count and sum: 9 accepted edges =n−1=n-1, so it is spanning. Cost =3+4+6+8+10+12+14+16+18=91=3+4+6+8+10+12+14+16+18=91.

Example 4 (statement type). Count the true statements. I: distinct edge weights ⇒\Rightarrow unique MST — true. II: Kruskal is correct with negative weights — true (only comparisons matter).
III: every MST of a connected graph has exactly n−1n-1 edges — true. IV: starting Prim from a different vertex can change the MST's total weight — false; only the tree may change.
V: a cycle with two equal light edges forces non-uniqueness — false; the triangle with weights 1,1,21,1,2 has the unique MST formed by the two weight‑1 edges. Answer: 3 true statements.

Example 5 (match the list).

List-I (method)List-II (running time)
A. Kruskal with sorting + DSU1. O(mlog⁡n)O(m\log n)
B. Prim with binary heap and adjacency list2. O(V2)O(V^2)
C. Prim with adjacency matrix / key array3. O(mlog⁡m)O(m\log m)
D. Prim with Fibonacci heap4. O(m+Vlog⁡n)O(m+V\log n)
E. One find/union with path compression + union by rank5. O(α(V))O(\alpha(V)) amortised

Correct matching: A–3, B–1, C–2, D–4, E–5. Since m≤n2m\le n^2, O(mlog⁡m)O(m\log m) and O(mlog⁡n)O(m\log n) coincide asymptotically, but the standard textbook answers are as listed.

Formulas

  • ∣T∣=n−1|T| = n-1 for any spanning tree of a connected graph with nn vertices; m=∣E∣m=|E| denotes the number of edges.
  • MST weight: w(T)=∑e∈Tw(e)w(T)=\sum_{e\in T} w(e).
  • Cayley's formula: the complete graph KnK_n has n n−2n^{\,n-2} distinct spanning trees.
  • Kruskal: O(mlog⁡m)O(m\log m), equivalently O(mlog⁡n)O(m\log n).
  • Prim, adjacency matrix / simple key array: O(n2)O(n^2).
  • Prim, binary min-heap + adjacency list: O(mlog⁡n)O(m\log n), i.e. O((n+m)log⁡n)O((n+m)\log n).
  • Prim, Fibonacci heap: O(m+nlog⁡n)O(m+n\log n).
  • DSU operations: O(α(n))O(\alpha(n)) amortised per find/union.
  • Complete graph with w(vi,vj)=4∣i−j∣w(v_i,v_j)=4|i-j|: MST =4(n−1)=4(n-1), using the n−1n-1 consecutive pairs ∣i−j∣=1|i-j|=1.
  • Minimax (bottleneck) property: the path between any two vertices in an MST minimises the maximum edge weight along that path.

Comparison

PointPrimKruskal
Growthone tree expandsforest of components merges
Main data structurepriority queue / key arraysorting + disjoint sets
Best whendense graphsparse graph
TimeO(n2)O(n^2) or O(mlog⁡n)O(m\log n)O(mlog⁡m)O(m\log m)
Tiesoutput depends on start vertex and tie-breaksoutput depends on sort tie-breaks
Edge weights negativeworksworks
Detects disconnected graphfinishes with a tree smaller than n−1n-1forest has more than one component
ConceptMSTShortest-path tree (Dijkstra)
Optimisestotal weight of all tree edgesdistance from one source to each vertex
Number of edgesexactly n−1n-1exactly n−1n-1 (a tree)
Typical complexityO(mlog⁡n)O(m\log n) or O(n2)O(n^2)O(mlog⁡n)O(m\log n) with a heap
Negative weightsallowednot allowed
SituationEffect
All weights distinctMST unique
Equal weights presentpossibly many MSTs, same weight and same sorted weight list
Graph is itself a treethe tree is the unique MST
Heaviest edge is in the MSTthat edge must be a bridge (its removal disconnects the graph)
Lightest edge of the graphlies in every MST

Revision summary

  • A spanning tree always has n−1n-1 edges; MST weight is the sum of these.
  • Kruskal = sort + union–find; Prim = grow one tree + priority queue.
  • Distinct weights ⇒\Rightarrow unique MST; the converse fails (triangle 1,1,21,1,2).
  • Lightest edge of the graph is always in the MST; heaviest edge is in the MST only if it is a bridge.
  • The heaviest edge of a cycle is never forced into an MST.
  • Negative weights are fine; no distance is being added, only edges compared.
  • Dense graph ⇒\Rightarrow Prim with a matrix is best, O(n2)O(n^2).
  • Sparse graph ⇒\Rightarrow Kruskal or heap Prim, O(mlog⁡n)O(m\log n).
  • Stop Kruskal as soon as n−1n-1 edges are accepted.
  • Every MST has the same total weight and the same multiset of edge weights.
  • Maximum spanning tree = MST after negating weights.
  • Do not confuse MST with a shortest-path tree.

Previous-year exam insights

  • Direct numeric questions give a small graph or an edge list; compute the MST weight by Kruskal, then check the options — arithmetic slips on the last two edges are the usual error.
  • Weight-formula questions (complete graph with w=4∣i−j∣w=4|i-j| or edges only for ∣i−j∣≤2|i-j|\le2 with w=i+jw=i+j) are solved by noticing that the lightest edges form a chain; write the sorted list and accept edges until n−1n-1.
  • Order-of-edges questions on Prim give a start vertex and four sequences; verify each option step by step against the smallest crossing edge — a sequence is wrong the moment a non-minimal crossing edge is taken, and ties make more than one option legal only if both use genuinely minimal edges.
  • Statement-type questions test uniqueness (distinct weights), negative weights, bridge condition for the heaviest edge, and the fact that Prim's start vertex does not change the weight.
  • Match-the-list questions pair each implementation (Kruskal, heap Prim, matrix Prim, Fibonacci Prim, DSU) with its complexity; memorise the five standard entries.
  • Tree-definition questions test the equivalences: connected + acyclic, connected + n−1n-1 edges, acyclic + n−1n-1 edges — "just no cycles" fails because it ignores connectivity.
  • Trap: a larger connected graph with n−1n-1 edges must be a tree, but a disconnected acyclic graph with n−1n-1 edges need not be.
  • Check first whether weights are distinct, whether the graph is dense or sparse, and whether the question asks for the weight, an edge order, or a complexity.

Practice at PARIKSHA

You are ready for the PARIKSHA tests of this sub-topic when you can:

  • State why every spanning tree of a connected graph has exactly n−1n-1 edges, and explain what changes for a disconnected graph.
  • Run Kruskal on a 6-vertex, 9-edge graph and write the accepted and rejected edges with reasons.
  • Run Prim from a named start vertex and list the crossing-edge candidates at every step.
  • Identify from four given sequences which one can be a valid Prim edge order, and say why each other one fails.
  • Prove or disprove a claim about unique MSTs when weights are distinct, or when an equal-weight triangle exists.
  • Compute MST weight for a weight formula such as w(vi,vj)=4∣i−j∣w(v_i,v_j)=4|i-j| or w=i+jw=i+j with bounded ∣i−j∣|i-j|.
  • Decide whether a heaviest or lightest edge must lie in the MST, and give the bridge argument.
  • Match Prim, Kruskal, DSU and Fibonacci-heap implementations to their exact complexities.
  • Explain why negative edge weights do not break either greedy algorithm, while they do break Dijkstra.
  • Distinguish an MST from a shortest-path tree by weight, structure and algorithm.

Attempt the sub-topic test first, then the subject test.

Pages 1–2 of 2