Minimum Spanning Trees
- Minimum spanning trees (MST) is the cheapest method (minimum edge weight) to connect all vertices with no cycles
- There can be multiple valid MSTs when edge weights are equal
- Edge weight is a value assigned to an edge
- It could represent cost, distance, time, etc.
Key Properties of MSTs
- Cycle property:
- Adding an edge
einto the MST creates a cycleC - For every other edge (
f) in that cycle:weight(f) ≤ weight(e)
- Therefore, MST edges are never the heaviest in any cycle
- Adding an edge
- Partition property:
- Split all vertices into two groups
UandV - The minimum-weight edge across partition must be in the MST
- Split all vertices into two groups
Building an MST
There are two algorithms you can use to build an MST:
- Prim-Jarník’s algorithm → grows MST one vertex at a time
- Kruskal’s algorithm → sorts all edges by weight, then adds one-by-one
Prim-Jarník’s Algorithm
-
Choose a starting vertex (e.g. vertex
A) -
Record the edge weight to every connected vertex in a min edge index
-
Expand along minimum edge weight to new vertex
- e.g. if vertex
Ais connected toB, C&Dwith edge weights7, 5&9, addA→C (w=5)to MST and move to vertexC
- e.g. if vertex
-
Repeat step 2, if edge weight to a vertex is smaller than recorded in our min edge index, update connection in index
- e.g. if index initially records
A→B (w=7), but we findC→B (w=5)then update index for vertexB
- e.g. if index initially records
-
Repeat step 3 and 4 for all vertices until there are no more changes to the min edge index for unexpanded vertices

-
Use the smallest edges from our index to connect the missing vertices
- e.g. if there are no more updates to the min index for vertex
B, our min edge would be betweenC&Bwithw = 5
- e.g. if there are no more updates to the min index for vertex
Kruskal’s Algorithm
- Sort edges by weight
- We now define each vertex as being in its own cluster
- Now going along our sorted list, merge vertices into larger clusters
- If you end up with an edge inside of a cluster, skip it
- This prevents creating a cycle
- Add your edges between merged clusters into your MST
- If you end up with an edge inside of a cluster, skip it
- Repeat until all vertices are in the same cluster
Prim vs Kruskal
Prim-Jarník grows from single source vertex
- Uses a priority queue of vertices
- Better for dense graphs (many edges)
- Running time
Kruskal merges separate clusters
- Uses a priority queue of edges + Union-Find ADT
- Better for sparse graphs (few edges)
- Running time :
- Priority queue operation
- Union-Find operations
Union-Find
- Union-Find is a structure that tracks elements split into distinct groups
- Used by Kruskal algorithm
Three main operations:
makeSet(u)→ create new cluster with vertexuat startfind(u)→ find which clusteruis in- Takes time
union(A, B)→ merge clustersAandB- Moves smaller set into larger one
- Amortised cost of
List-based Partition:
- Each set is stored in sequence
- Each element has a reference back to set