Loading lessons...
Trees, Spanning Trees & Minimum Spanning Tree Algorithms
1. Trees
A tree is a connected acyclic undirected graph. A forest is an acyclic graph (each component a tree).
Theorem. For a graph T with n vertices, the following are equivalent:
- T is a tree (connected, no cycles);
- there is a unique simple path between every pair of vertices;
- T is connected and has n − 1 edges;
- T is acyclic and has n − 1 edges;
- T is connected, but removing any edge disconnects it (every edge is a bridge);
- T is acyclic, but adding any edge creates exactly one cycle.
Proof that a tree has n − 1 edges (induction on n). n = 1: 0 edges ✓. A tree with n ≥ 2 vertices has a leaf (follow a longest path-its end cannot have another neighbour without creating a cycle or a longer path). Remove the leaf and its edge: a tree with n − 1 vertices, which has n − 2 edges by hypothesis. Add them back: n − 1 edges. ∎
A forest with n vertices and k components has n − k edges.
2. Spanning Trees
A spanning tree of a connected graph G is a subgraph that is a tree and contains every vertex of G. Every connected graph has one (delete edges of cycles until none remain, or run DFS/BFS-Lesson 38). A spanning tree of an n-vertex graph has exactly n − 1 edges.
Cayley's formula: Kₙ has nn-2 spanning trees-K₄ has 4² = 16.
3. Minimum Spanning Trees (MST)
In a connected weighted graph, an MST is a spanning tree of minimum total weight. Applications: laying cable or pipelines, network design, clustering, approximating TSP.
The cut property (why greedy works)
A cut splits V into S and V − S. Cut property: for any cut, a minimum-weight edge crossing it belongs to some MST. Proof (exchange argument). Let e be a lightest crossing edge and T an MST without e. Adding e to T creates a cycle, which must cross the cut again at some edge f. w(e) ≤ w(f), so T + e − f is a spanning tree of weight ≤ w(T) - an MST containing e. ∎
Cycle property: the heaviest edge on any cycle (if unique) is in no MST.
4. Kruskal's Algorithm (edge-centric)
- Sort all edges by increasing weight.
- Take the next edge; add it if it does not create a cycle (its end-points are in different components-tested with Union-Find).
- Stop after n − 1 edges.
5. Prim's Algorithm (vertex-centric)
- Start with any vertex as the tree.
- Repeatedly add the minimum-weight edge joining a tree vertex to a non-tree vertex.
- Stop when all vertices are in the tree.
Kruskal and Prim on the same 6-vertex graph: both find total weight 14
6. Worked Example
Edges: A-B 4, A-C 3, B-C 1, B-D 2, C-D 4, C-E 6, D-E 5, D-F 7, E-F 3.
Kruskal (sorted: BC 1, BD 2, AC 3, EF 3, AB 4, CD 4, DE 5, CE 6, DF 7)
| Edge | Weight | Action |
|---|---|---|
| B-C | 1 | accept |
| B-D | 2 | accept |
| A-C | 3 | accept |
| E-F | 3 | accept |
| A-B | 4 | reject (A, B already connected via C) |
| C-D | 4 | reject (cycle C-B - D) |
| D-E | 5 | accept-5 edges, stop |
MST = {BC, BD, AC, EF, DE}, total 1 + 2 + 3 + 3 + 5 = 14.
Prim from A: A-C (3) → C-B (1) → B-D (2) → D-E (5) → E-F (3). Same tree, total 14.
(If edge weights are distinct the MST is unique; here ties do not change the tree.)
Kruskal vs Prim summary (from the Data Structures course)
7. Complexity Analysis of MST Algorithms
| Algorithm | Data structure | Time | Best for |
|---|---|---|---|
| Kruskal | sort + Union-Find (rank + path compression) | O(E log E) = O(E log V) | sparse graphs |
| Prim | adjacency matrix, linear scan | O(V²) | dense graphs |
| Prim | binary heap + adjacency list | O((V + E) log V) = O(E log V) | sparse graphs |
| Prim | Fibonacci heap | O(E + V log V) | very large dense graphs |
Kruskal: sorting E edges costs O(E log E); the E find/union operations cost O(E·α(V)), which is almost linear; since E ≤ V², log E ≤ 2 log V. Prim (heap): V extract-min operations plus up to E decrease-key operations, each O(log V).
Kruskal(G):
sort edges by weight; MakeSet(v) for every v; T = φ
for each edge (u, v) in sorted order:
if Find(u) ≠ Find(v):
T = T ∪ {(u, v)}; Union(u, v)
return T📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Prove that a tree with n vertices has n − 1 edges and that there is a unique path between any two vertices. (5 Marks)
- Q2. Find the MST of a given weighted graph using Kruskal's and Prim's algorithms, showing each step. (10 Marks)
- Q3. State and prove the cut property. Compare the time complexity of Prim and Kruskal. (10 Marks)