Lesson 37 +40 XP

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:

  1. T is a tree (connected, no cycles);
  2. there is a unique simple path between every pair of vertices;
  3. T is connected and has n − 1 edges;
  4. T is acyclic and has n − 1 edges;
  5. T is connected, but removing any edge disconnects it (every edge is a bridge);
  6. 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)

  1. Sort all edges by increasing weight.
  2. Take the next edge; add it if it does not create a cycle (its end-points are in different components-tested with Union-Find).
  3. Stop after n − 1 edges.

5. Prim's Algorithm (vertex-centric)

  1. Start with any vertex as the tree.
  2. Repeatedly add the minimum-weight edge joining a tree vertex to a non-tree vertex.
  3. Stop when all vertices are in the tree.

Kruskal and Prim on the same 6-vertex graph: both find total weight 14Kruskal 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)

EdgeWeightAction
B-C1accept
B-D2accept
A-C3accept
E-F3accept
A-B4reject (A, B already connected via C)
C-D4reject (cycle C-B - D)
D-E5accept-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)Kruskal vs Prim summary (from the Data Structures course)

7. Complexity Analysis of MST Algorithms

AlgorithmData structureTimeBest for
Kruskalsort + Union-Find (rank + path compression)O(E log E) = O(E log V)sparse graphs
Primadjacency matrix, linear scanO(V²)dense graphs
Primbinary heap + adjacency listO((V + E) log V) = O(E log V)sparse graphs
PrimFibonacci heapO(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).

TEXT
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
🎯 END-TERM EXAM PREP

📝 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)