Connecting a set of towns by roads, cables or pipes at the least total cost is the minimum spanning tree problem.

Spanning Trees

Given a connected, weighted, undirected graph G = (V, E), a spanning tree is a subgraph that includes all V vertices, is connected and has no cycle; it therefore has exactly |V| − 1 edges. A minimum spanning tree (MST) is the spanning tree of least total weight. A graph can have several spanning trees; MSTs are used in network design, clustering and approximation of harder problems. Both algorithms below are greedy and rely on the cut property: for any cut of the graph, the lightest edge crossing it belongs to some MST.

Kruskal's Algorithm

Kruskal's algorithm works on edges. Sort all edges by increasing weight. Take the edges in order, and add an edge to the tree unless it forms a cycle with the edges already chosen; stop when |V| − 1 edges are chosen. Cycle detection uses the union-find (disjoint set) structure: an edge is accepted only if its end points lie in different sets, and then the sets are merged. The time is Θ(E log E), which equals Θ(E log V), dominated by sorting the edges. It is efficient for sparse graphs.

Prim's Algorithm

Prim's algorithm grows a single tree from a starting vertex. Repeatedly choose the lightest edge that connects a vertex in the tree to a vertex outside it and add that vertex. With an adjacency matrix and a simple array scan the time is Θ(V²), good for dense graphs; with an adjacency list and a binary heap it is Θ(E log V). At every step the partial result is a tree, whereas in Kruskal's algorithm the partial result is a forest.

Definition: Minimum spanning tree. A spanning tree of a connected weighted graph whose total edge weight is the smallest among all spanning trees.

Table 2.7: Prim versus Kruskal

Basis Prim Kruskal
Grows One tree from a vertex A forest, merged by edges
Selection Cheapest edge leaving the tree Cheapest edge overall, no cycle
Data structure Priority queue or array Sorting and union-find
Time Θ(V²) or Θ(E log V) Θ(E log E)
Best for Dense graphs Sparse graphs

Example 2.7: Kruskal and Prim on a five-vertex graph

Problem: Edges (weight): A-B 2, A-C 3, B-C 1, B-D 4, C-D 5, C-E 6, D-E 7. Find the MST.

Solution:

  1. Kruskal: sorted B-C 1, A-B 2, A-C 3, B-D 4, C-D 5, C-E 6, D-E 7.
  2. Accept B-C (1), A-B (2); reject A-C (cycle A-B-C); accept B-D (4); reject C-D (cycle); accept C-E (6): 4 edges for 5 vertices.
  3. Prim from A: cheapest edge from {A} is A-B 2; from {A, B} is B-C 1; then B-D 4; then C-E 6.
  4. Both give edges A-B, B-C, B-D, C-E.

Result: MST weight = 2 + 1 + 4 + 6 = 13.

edges = [(2,"A","B"), (3,"A","C"), (1,"B","C"), (4,"B","D"),
         (5,"C","D"), (6,"C","E"), (7,"D","E")]
parent = {v: v for v in "ABCDE"}
def find(x):
    while parent[x] != x:
        x = parent[x]
    return x
mst, total = [], 0
for w, u, v in sorted(edges):
    ru, rv = find(u), find(v)
    if ru != rv:
        parent[ru] = rv
        mst.append(u + v)
        total += w
print("Kruskal edges:", mst, "weight", total)

Output

Kruskal edges: ['BC', 'AB', 'BD', 'CE'] weight 13

Common mistakes

  • Adding an edge that closes a cycle in Kruskal's algorithm.
  • Choosing the cheapest edge anywhere in Prim's algorithm instead of the cheapest edge leaving the tree.
  • Expecting an MST to be unique when several edges have equal weight.
  • Forgetting that an MST has exactly V − 1 edges.

Key takeaways

  • An MST connects all V vertices with V − 1 edges and minimum total weight.
  • Kruskal: sort edges, add if no cycle; Θ(E log E).
  • Prim: grow one tree by the cheapest crossing edge; Θ(V²) or Θ(E log V).
  • Both are greedy and rely on the cut property.

Practice questions

  1. How many edges does a spanning tree of 8 vertices have?
  2. Which data structure detects cycles in Kruskal's algorithm?
  3. Which algorithm suits dense graphs better?

Answers to the practice questions

  1. Union-find (disjoint sets).
  2. Prim's algorithm with an adjacency matrix, Θ(V²).

Sources

  • Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit II