Minimum Spanning Tree (MST)
A Minimum Spanning Tree (MST) is a spanning tree of a connected, weighted, undirected graph having the minimum possible total edge weight.
Before understanding Minimum Spanning Tree, it is important to understand the concepts of a graph, tree, and spanning tree. An MST connects all vertices of a graph without creating a cycle and does so with the minimum possible total cost.
Prerequisite Concepts
| Concept | Meaning |
|---|---|
| Vertex | A vertex is a node or point in a graph. |
| Edge | An edge is a connection between two vertices. |
| Weighted Graph | A graph in which every edge has an associated numerical weight or cost. |
| Connected Graph | A graph in which every vertex can be reached from every other vertex. |
| Cycle | A path that starts and ends at the same vertex without repeating intermediate vertices. |
| Tree | A connected graph that contains no cycle. |
What is a Spanning Tree?
A Spanning Tree is a tree formed from a connected graph that contains all the vertices of the original graph but only enough edges to keep them connected.
If a graph contains V vertices, every spanning tree contains exactly:
A spanning tree must satisfy two important conditions:
- It must contain all vertices.
- It must not contain any cycle.
A connected graph can have more than one spanning tree. When edge weights are present, these different spanning trees may have different total costs.
Spanning Tree Illustration
The following figure shows the difference between a weighted graph, a spanning tree, and a minimum spanning tree.
Figure 1: Graph, Spanning Tree and Minimum Spanning Tree
Understanding Spanning Tree with a Simple Example
Consider a graph containing four vertices:
| Vertex | A | B | C | D |
|---|
Suppose the graph has the following edges:
| Edge | Weight |
|---|---|
| A − B | 4 |
| A − C | 2 |
| B − C | 1 |
| B − D | 3 |
| C − D | 5 |
Since there are 4 vertices, a spanning tree must contain:
For example, the following three edges form a spanning tree:
| Selected Edge | Weight |
|---|---|
| A − C | 2 |
| C − B | 1 |
| B − D | 3 |
All four vertices are connected and no cycle is formed. Therefore, these three edges form a spanning tree.
What is a Minimum Spanning Tree?
A graph may contain several different spanning trees. The Minimum Spanning Tree is the spanning tree whose total edge weight is minimum among all possible spanning trees of the graph.
For a graph having V vertices, an MST contains exactly V − 1 edges.
Spanning Tree vs Minimum Spanning Tree
| Spanning Tree | Minimum Spanning Tree |
|---|---|
| Connects all vertices. | Connects all vertices. |
| Contains no cycle. | Contains no cycle. |
| Contains V − 1 edges. | Contains V − 1 edges. |
| May have any valid total weight. | Has the minimum possible total weight. |
| There may be many spanning trees. | There may be one or multiple MSTs. |
Important Properties of MST
| Property | Explanation |
|---|---|
| All vertices are connected | Every vertex must belong to the MST. |
| No cycle | An MST cannot contain a cycle. |
| V − 1 edges | If there are V vertices, the MST contains exactly V − 1 edges. |
| Minimum total weight | The sum of selected edge weights is minimum. |
| Connected graph required | A single MST exists only when the graph is connected. |
| Undirected graph | Standard MST algorithms are defined for undirected weighted graphs. |
Why Can We Not Simply Select the Smallest Edges?
We cannot simply select the smallest edges without checking whether they create a cycle.
For example, suppose the three smallest edges are:
| Edge | Weight |
|---|---|
| A − B | 1 |
| B − C | 2 |
| A − C | 3 |
Selecting all three edges creates the cycle:
Therefore, the third edge must be rejected because a tree cannot contain a cycle.
How is an MST Constructed?
A Minimum Spanning Tree can be constructed using greedy algorithms.
The two most important algorithms used for constructing an MST are:
- Prim's Algorithm
- Kruskal's Algorithm
Prim's Algorithm
Prim's Algorithm is a greedy algorithm that starts with one vertex and gradually grows a single tree until all vertices are included.
At every step, Prim's Algorithm selects the minimum-weight edge that connects a vertex already inside the tree to a vertex outside the tree.
Basic Idea of Prim's Algorithm
- Choose any starting vertex.
- Put the starting vertex into the MST.
- Look at all edges going from the current MST to unvisited vertices.
- Select the edge having the smallest weight.
- Add the selected vertex and edge to the MST.
- Repeat until all vertices are included.
Prim's Algorithm — Step-by-Step Example
Consider the following weighted graph:
| Edge | Weight |
|---|---|
| A − B | 4 |
| A − C | 2 |
| B − C | 3 |
| B − D | 6 |
| C − D | 1 |
| C − E | 5 |
| D − E | 2 |
Step 1: Start with A
Start Prim's Algorithm from vertex A.
| Edge | Weight |
|---|---|
| A − B | 4 |
| A − C | 2 |
The minimum edge is A − C = 2.
Select A − C.
Step 2: Current MST = {A, C}
| Edge | Weight | Decision |
|---|---|---|
| A − B | 4 | Consider |
| C − B | 3 | Consider |
| C − D | 1 | Select |
| C − E | 5 | Consider |
The smallest edge is C − D = 1.
Select C − D.
Step 3: Current MST = {A, C, D}
| Edge | Weight |
|---|---|
| C − E | 5 |
| D − E | 2 |
The smallest available edge is D − E = 2.
Select D − E.
Step 4: Add B
The remaining unvisited vertex is B.
| Edge | Weight |
|---|---|
| A − B | 4 |
| C − B | 3 |
| D − B | 6 |
The smallest edge is C − B = 3.
Select C − B.
Final MST using Prim's Algorithm
| Selected Edge | Weight |
|---|---|
| A − C | 2 |
| C − D | 1 |
| D − E | 2 |
| C − B | 3 |
| Total | 8 |
Figure 2: Prim's Algorithm — Step-by-Step Selection
Prim's Algorithm Pseudocode
Prim(G):
1. Choose any starting vertex
2. Mark the starting vertex as visited
3. Repeat until V - 1 edges are selected:
Find the minimum-weight edge
connecting a visited vertex
to an unvisited vertex
Add that edge to MST
Mark the new vertex as visited
4. Return MST
Kruskal's Algorithm
Kruskal's Algorithm is another greedy algorithm used to find a Minimum Spanning Tree.
Unlike Prim's Algorithm, Kruskal's Algorithm does not start from a particular vertex. Instead, it considers edges in increasing order of their weights.
Basic Idea of Kruskal's Algorithm
- List all edges of the graph.
- Sort the edges in increasing order of weight.
- Select the smallest edge.
- Check whether adding the edge creates a cycle.
- If no cycle is formed, add the edge to the MST.
- If a cycle is formed, reject the edge.
- Continue until V − 1 edges have been selected.
Kruskal's Algorithm — Step-by-Step Example
Consider the following graph:
| Edge | Weight |
|---|---|
| A − D | 1 |
| A − C | 2 |
| C − D | 3 |
| A − B | 4 |
| B − C | 5 |
| B − D | 6 |
Step 1: Sort All Edges
| Order | Edge | Weight |
|---|---|---|
| 1 | A − D | 1 |
| 2 | A − C | 2 |
| 3 | C − D | 3 |
| 4 | A − B | 4 |
| 5 | B − C | 5 |
| 6 | B − D | 6 |
Step 2: Select A − D
The smallest edge is A − D = 1. No cycle is possible because this is the first selected edge.
Selected.
Step 3: Select A − C
The next smallest edge is A − C = 2. Adding it does not create a cycle.
Selected.
Step 4: Check C − D
The next edge is C − D = 3.
But A, C and D are already connected through:
A → C → D → A
Therefore, adding C − D would create a cycle.
Step 5: Select A − B
The next edge is A − B = 4. B is not yet connected to the existing tree, so adding this edge does not create a cycle.
Selected.
Now all four vertices are connected and the MST contains:
V − 1 = 4 − 1 = 3 edges
Final MST using Kruskal's Algorithm
| Selected Edge | Weight |
|---|---|
| A − D | 1 |
| A − C | 2 |
| A − B | 4 |
| Total | 7 |
Figure 3: Kruskal's Algorithm — Edge Selection and Cycle Rejection
Kruskal's Algorithm Pseudocode
Kruskal(G):
1. Sort all edges in increasing order of weight
2. Initialize MST as an empty set
3. For each edge (u, v) in sorted order:
If adding (u, v) does not create a cycle:
Add (u, v) to MST
If MST contains V - 1 edges:
Stop
4. Return MST
Cycle Detection in Kruskal's Algorithm
The main challenge in Kruskal's Algorithm is determining whether adding an edge will create a cycle.
The standard data structure used for this purpose is called Disjoint Set Union (DSU), also known as Union-Find.
| Operation | Purpose |
|---|---|
| Find | Determines which component a vertex belongs to. |
| Union | Combines two different components. |
If both endpoints of an edge already belong to the same component, adding that edge would create a cycle, so the edge is rejected.
Complete Solved Example — MST
Consider the following weighted graph:
| Edge | Weight |
|---|---|
| A − B | 4 |
| A − C | 2 |
| B − C | 5 |
| B − D | 6 |
| C − E | 3 |
| D − E | 1 |
Sorted Edge Table
| Order | Edge | Weight |
|---|---|---|
| 1 | D − E | 1 |
| 2 | A − C | 2 |
| 3 | C − E | 3 |
| 4 | A − B | 4 |
| 5 | B − C | 5 |
| 6 | B − D | 6 |
Step-by-Step Selection
| Edge | Weight | Decision | Reason |
|---|---|---|---|
| D − E | 1 | Select | No cycle |
| A − C | 2 | Select | No cycle |
| C − E | 3 | Select | Joins two components |
| A − B | 4 | Select | Connects B |
Now all five vertices are connected and the MST contains exactly four edges.
| Selected Edge | Weight |
|---|---|
| D − E | 1 |
| A − C | 2 |
| C − E | 3 |
| A − B | 4 |
| Total MST Cost | 10 |
Cut Property of MST
The Cut Property is an important theoretical property of Minimum Spanning Trees.
Suppose the vertices of a graph are divided into two groups. This division is called a cut. Among the edges crossing the cut, a minimum-weight edge is a safe choice for constructing an MST.
Cycle Property of MST
The Cycle Property states that a strictly heaviest edge in a cycle cannot belong to an MST.
This principle helps explain why Kruskal's Algorithm rejects an edge when adding it would produce a cycle and a lower-weight connection is already available.
Can an MST be Unique?
Yes. An MST can be unique.
If all edge weights in a connected graph are distinct, the MST is unique.
If some edge weights are equal, a graph may have multiple different MSTs with the same minimum total weight.
Number of Edges in MST
| Number of Vertices (V) | MST Edges (V − 1) |
|---|---|
| 4 | 3 |
| 5 | 4 |
| 6 | 5 |
| 10 | 9 |
| 20 | 19 |
Time Complexity
| Algorithm / Implementation | Time Complexity |
|---|---|
| Prim's — Adjacency Matrix | O(V²) |
| Prim's — Binary Heap + Adjacency List | O(E log V) |
| Kruskal's | O(E log E) |
Space Complexity
| Algorithm | Typical Space Requirement |
|---|---|
| Prim's with adjacency matrix | O(V²) |
| Prim's with adjacency list and priority queue | O(V + E) |
| Kruskal's | O(E) for edge storage and auxiliary structures |
Applications of Minimum Spanning Tree
| Application | Use of MST |
|---|---|
| Computer Networks | Designing a network with minimum cable cost. |
| Road Construction | Connecting cities or villages with minimum construction cost. |
| Electrical Networks | Designing economical power distribution connections. |
| Telecommunication | Connecting communication stations with minimum infrastructure cost. |
| Water Pipeline Networks | Connecting locations using minimum pipeline installation cost. |
| Transportation Networks | Designing low-cost connections between locations. |
Advantages of MST
- Minimizes the total cost of connecting all vertices.
- Connects every vertex.
- Does not contain unnecessary cycles.
- Provides an efficient structure for network design.
- Prim's and Kruskal's algorithms can construct MST efficiently.
Limitations of MST
- A standard MST requires an undirected weighted graph.
- A single MST exists only when the graph is connected.
- MST minimizes total edge cost but does not automatically optimize other factors such as traffic, latency or reliability.
- A change in edge weights can change the resulting MST.
Prim's Algorithm vs Kruskal's Algorithm
| Feature | Prim's Algorithm | Kruskal's Algorithm |
|---|---|---|
| Basic approach | Vertex/tree based | Edge based |
| Starting point | Any vertex | No fixed starting vertex |
| Main choice | Minimum edge crossing the current tree boundary | Minimum edge that does not create a cycle |
| Data structure | Priority Queue | Union-Find |
| Sorting required | No global edge sorting required | Yes |
| Typical complexity | O(E log V) | O(E log E) |
| Tree formation | One tree grows continuously | Several components merge |
Summary
A Minimum Spanning Tree is a spanning tree of a connected weighted undirected graph with the minimum possible total edge weight. It contains all vertices, exactly V − 1 edges, and no cycle.
The two most important algorithms for finding an MST are Prim's Algorithm and Kruskal's Algorithm. Prim's Algorithm starts from a vertex and continuously expands a single tree by selecting the minimum-weight edge connecting the tree to a new vertex. Kruskal's Algorithm sorts all edges by weight and adds the smallest edge whenever it does not create a cycle.