Minimum Spanning Tree
๐ What is a Spanning Tree?
Take a connected graph (a set of points joined by lines).
A spanning tree is:
- A set of edges
- That connects all vertices
- With no cycles
- And uses only the edges of the original graph
Think of it like placing just enough wires between rooms of a house so that electricity reaches everywhere, but you donโt waste wire on loops.
๐ฐ So What Makes It โMinimumโ?
A Minimum Spanning Tree (MST) is a spanning tree where the total weight (sum of edge costs) is as small as possible.
If weights represent:
- distance โ shortest cable length
- cost โ cheapest network
- time โ fastest connection
then MST helps you build the most efficient system.
๐ฏ A Simple Weighted Graph
Letโs take a small graph as an example:
(2)
A ------- B
| \ |
(3| \5) (1)
| \ |
C ---- D
(4)
Each number is a weight โ think of it as kilometers, cost, or effort.
Your goal:
Pick a set of edges that connects A, B, C, and D, avoids loops, and keeps the total weight as low as possible.
๐ณ An MST of the Above Graph
One possible MST for the graph looks like this:
(2)
A ------- B
|
(3|
|
C ------- D
(4)
Selected edges: 2, 3, 4
Total weight = 2 + 3 + 4 = 9
Notice:
- All nodes are connected
- No cycles are formed
- Total weight is minimal
๐ง How Do We Find an MST?
There are two famous methods, and both are surprisingly intuitive.
โญ 1. Kruskalโs Algorithm โ โPick the cheapest firstโ
Kruskal feels like shopping during a sale.
You sort all edges from cheapest to costliest, and then:
- Pick the smallest edge.
- Add the next smallest edge.
- Skip an edge if it creates a cycle.
- Keep going until all vertices are connected.
Super simple idea: Buy the cheap stuff first, but donโt take duplicates.
โญ 2. Primโs Algorithm โ โGrow like a treeโ
Prim works like youโre slowly spreading out from one town.
- Start at any vertex.
- Choose the smallest edge that connects to a new vertex.
- Repeat โ always attach the next nearest vertex.
- Stop when all vertices are included.
Itโs like expanding a network step-by-step, always choosing the most affordable connection.
โจ Why Minimum Spanning Trees Matter
MSTs appear in many real-world problems:
- Designing computer networks
- Planning telecom towers
- Laying railway tracks or roads
- Electricity grid design
- Clustering algorithms in machine learning
- Social network analysis
Anywhere you want everything connected efficiently, MSTs are extremely helpful.
