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 of a connected graph that is connected, acyclic, and contains all vertices. has exactly edges.
Conversely, a connected graph with vertices and edges, or an acyclic graph with vertices and edges, is a tree. - MST: a spanning tree of minimum total weight . It exists whenever is connected. If is disconnected, the analogue is a minimum spanning forest (one MST per component).
- Cut: a partition of into two non-empty sets . 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 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; .
- Distinct weights guarantee a unique MST; a connected graph with vertices and edges is itself its own unique spanning tree.
How it works
Kruskal's algorithm
- Sort all edges in non-decreasing order of weight.
- Make a DSU set for each vertex (each vertex is its own component). Set .
- Take the next edge . If , add to and ; otherwise discard (it would close a cycle).
- Stop when has 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, ; with DSU operations costing amortised, the rest is nearly linear.
Prim's algorithm
- Choose any start vertex ; set , for others, all vertices unvisited.
- Repeatedly pick the unvisited vertex with the smallest key, mark it visited; the edge that gave its key is added to the tree (except for ).
- For every unvisited neighbour of , if , set and .
- After 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).
Drawing the diagram…
Worked examples
Example 1 (Kruskal, simple–medium). Graph on with edges , , , , , , , , .
Sorted order: 1, 2, 3, 4, 5, 6, 7, 8, 9. Take ; take ; take ; take (merges with ); is rejected because and are already connected via ; take , which completes edges.
Tree: ; weight . Note was never examined — stopping early at edges is valid.
Example 2 (Prim trace, medium). Graph: , , , , , , , , . Start at .
| Step | Tree vertices | Candidate crossing edges | Chosen |
|---|---|---|---|
| 1 | A | AB 4, AC 2 | AC = 2 |
| 2 | A,C | AB 4, CB 1, CD 8, CE 10 | CB = 1 |
| 3 | A,B,C | BD 5, CD 8, CE 10 | BD = 5 |
| 4 | A,B,C,D | DE 2, DF 6, CE 10 | DE = 2 |
| 5 | A,B,C,D,E | EF 3, DF 6 | EF = 3 |
Edges added in order: ; total . Kruskal on the same graph gives the same weight (), 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). vertices ; edge between iff ; weight . Find the MST cost.
Step 1 — list edges by weight: ranges from upward. For each sum, only pairs with exist.
| Weight | Edge | Action |
|---|---|---|
| 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 , so it is spanning. Cost .
Example 4 (statement type). Count the true statements. I: distinct edge weights unique MST — true. II: Kruskal is correct with negative weights — true (only comparisons matter).
III: every MST of a connected graph has exactly 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 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 + DSU | 1. |
| B. Prim with binary heap and adjacency list | 2. |
| C. Prim with adjacency matrix / key array | 3. |
| D. Prim with Fibonacci heap | 4. |
| E. One find/union with path compression + union by rank | 5. amortised |
Correct matching: A–3, B–1, C–2, D–4, E–5. Since , and coincide asymptotically, but the standard textbook answers are as listed.
Formulas
- for any spanning tree of a connected graph with vertices; denotes the number of edges.
- MST weight: .
- Cayley's formula: the complete graph has distinct spanning trees.
- Kruskal: , equivalently .
- Prim, adjacency matrix / simple key array: .
- Prim, binary min-heap + adjacency list: , i.e. .
- Prim, Fibonacci heap: .
- DSU operations: amortised per find/union.
- Complete graph with : MST , using the consecutive pairs .
- Minimax (bottleneck) property: the path between any two vertices in an MST minimises the maximum edge weight along that path.
Comparison
| Point | Prim | Kruskal |
|---|---|---|
| Growth | one tree expands | forest of components merges |
| Main data structure | priority queue / key array | sorting + disjoint sets |
| Best when | dense graph | sparse graph |
| Time | or | |
| Ties | output depends on start vertex and tie-breaks | output depends on sort tie-breaks |
| Edge weights negative | works | works |
| Detects disconnected graph | finishes with a tree smaller than | forest has more than one component |
| Concept | MST | Shortest-path tree (Dijkstra) |
|---|---|---|
| Optimises | total weight of all tree edges | distance from one source to each vertex |
| Number of edges | exactly | exactly (a tree) |
| Typical complexity | or | with a heap |
| Negative weights | allowed | not allowed |
| Situation | Effect |
|---|---|
| All weights distinct | MST unique |
| Equal weights present | possibly many MSTs, same weight and same sorted weight list |
| Graph is itself a tree | the tree is the unique MST |
| Heaviest edge is in the MST | that edge must be a bridge (its removal disconnects the graph) |
| Lightest edge of the graph | lies in every MST |
Revision summary
- A spanning tree always has edges; MST weight is the sum of these.
- Kruskal = sort + union–find; Prim = grow one tree + priority queue.
- Distinct weights unique MST; the converse fails (triangle ).
- 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 Prim with a matrix is best, .
- Sparse graph Kruskal or heap Prim, .
- Stop Kruskal as soon as 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 or edges only for with ) are solved by noticing that the lightest edges form a chain; write the sorted list and accept edges until .
- 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 + edges, acyclic + edges — "just no cycles" fails because it ignores connectivity.
- Trap: a larger connected graph with edges must be a tree, but a disconnected acyclic graph with 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 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 or with bounded .
- 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.