DSA Notes
Understand spanning trees, the minimum spanning tree concept, cut property, and why MSTs matter for network design and optimization problems.
What is a Spanning Tree?
Given a connected, undirected graph G with V vertices and E edges, a spanning tree is a subset of edges that:
- Connects all V vertices (spans the graph)
- Contains exactly V-1 edges
- Has no cycles (it is a tree)
Think of it this way: a spanning tree is the minimal set of edges needed to keep everyone connected. Remove any edge and the tree disconnects. Add any edge and you create a cycle.
What is a Minimum Spanning Tree?
For a weighted graph, the minimum spanning tree is the spanning tree whose total edge weight is minimized. Among all possible spanning trees of a graph, the MST has the smallest sum of edge weights.
| Graph | MST (total weight = 7): |
| All edges | 4+2+3+5+1 = 15 |
| MST edges | 2+3+1+? We need 3 edges for 4 vertices. |
| MST: (C,D,1), (A,C,2), (A,D,3) | total = 6 |
| Or: (C,D,1), (A,C,2), (A,B,4) | total = 7 |
| Best MST | {(C,D,1), (A,C,2), (A,D,3)} with weight 6 |
Why MST Matters
The MST problem appears whenever you need to connect a set of points at minimum cost:
- Network cabling: Connect offices with minimum total cable length
- Road construction: Connect villages with minimum total road
- Circuit design: Connect components with minimum wire
- Cluster analysis: Remove heaviest edges from MST to get clusters
- Approximation algorithms: MST gives 2-approximation for Traveling Salesman Problem
The Cut Property (Why Greedy Works)
The cut property is the theoretical foundation that makes greedy MST algorithms correct:
For any cut of the graph (partition of vertices into two non-empty sets), the minimum-weight edge crossing the cut MUST be in the MST.
A cut divides vertices into two groups S and V\\S. Edges crossing the cut have one endpoint in each group. The lightest such edge is safe to include in the MST.
Properties of MST
- Uniqueness: If all edge weights are distinct, the MST is unique. If some weights are equal, multiple MSTs may exist (all with the same total weight).
- Cycle Property: The maximum-weight edge in any cycle is NOT in the MST (if weights are distinct).
- Edge count: An MST of V vertices always has exactly V-1 edges.
- Substructure: If you remove any edge from the MST, the two resulting subtrees are themselves MSTs of the subgraphs they span.
MST Algorithms Overview
Two classic algorithms find the MST:
Kruskal's Algorithm (Edge-centric)
- Sort all edges by weight
- Greedily add edges that don't create cycles
- Uses Union-Find to detect cycles efficiently
- Best for sparse graphs: O(E log E)
Prim's Algorithm (Vertex-centric)
- Grow a tree from a starting vertex
- Always add the minimum-weight edge connecting tree to non-tree vertex
- Uses priority queue
- Best for dense graphs: O(E log V) with binary heap
Both are greedy and both produce correct MSTs thanks to the cut property.
Simple Example: Building a Network
MST vs Shortest Path
MST and shortest path solve fundamentally different problems:
- Shortest path: Minimum cost to travel from A to B
- MST: Minimum total cost to connect ALL vertices
An MST does NOT guarantee shortest paths between vertices. The path from A to B in the MST might be longer than the direct edge (if that edge was not included in the MST).
Time Complexity Comparison
| Algorithm | Time | Space | Best For |
|---|---|---|---|
| Kruskal's | O(E log E) | O(V) | Sparse graphs |
| Prim's (binary heap) | O(E log V) | O(V) | Dense graphs |
| Prim's (Fibonacci heap) | O(E + V log V) | O(V) | Very dense graphs |
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Minimum Spanning Tree (MST).
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, graphs, minimum, spanning
Related Data Structures & Algorithms Topics