MCS-208 · Unit 8

Graphs

Section 6 of 8

Block 3 · Unit 8Graphs11 min · Section 6 of 8

8.6Minimum Cost Spanning Trees: Kruskal and Prim

Covers IGNOU MCS-208 §8.4, 8.4.1, 8.4.2, 8.4.3

To connect a set of islands with bridges, or offices with network cables, at the lowest total cost, we need a set of edges that connects every vertex, has no unnecessary cycles, and has minimum total weight. That set is a minimum spanning tree.

Spanning trees

A graph can have many spanning trees; an MST need not be unique when some weights are equal, but its total weight is.

Kruskal’s algorithm

  1. Start with a forest of nn single-vertex trees.
  2. Sort the edges by weight (or put them in a priority queue).
  3. Take edges in increasing order; add an edge if its ends are in different trees (joining the trees); reject it if both ends are already in the same tree (it would form a cycle).
  4. Stop after n−1n - 1 edges have been added.

Whether two vertices are in the same tree is tested with a union–find (disjoint-set) structure. Running time: O(elog⁡e)O(e \log e), dominated by sorting the edges.

Prim’s algorithm

  1. Start with TT = one vertex.
  2. While TT has fewer than nn vertices: find the cheapest edge with one end in TT and the other outside, and add it (and its outside vertex) to TT.

With an array of “cheapest edge to the tree” for each outside vertex, Prim runs in O(n2)O(n^2), which is best for dense graphs; with a binary heap it runs in O(elog⁡n)O(e \log n).

Comparison

KruskalPrim
GrowsA forest of trees that mergeOne tree, one vertex at a time
ChoosesCheapest edge overall that joins two treesCheapest edge leaving the current tree
NeedsSorted edges + union–findPriority queue or array of best edges
TimeO(elog⁡e)O(e \log e)O(n2)O(n^2) (array) or O(elog⁡n)O(e \log n) (heap)
Better forSparse graphsDense graphs (with the O(n2)O(n^2) array version)

Applications

  • Designing networks at minimum cost: cables, pipelines, roads, electrical grids.
  • Connecting nn pins on a circuit board with n−1n - 1 wires of least total length.
  • Finding an independent set of circuit equations in an electrical network.
  • Clustering: deleting the most expensive MST edges splits the points into groups.
  • A lower bound and approximation for the travelling salesman problem.

Key takeaways

  • Spanning tree: all nn vertices, n−1n - 1 edges, no cycle; MST = minimum total weight.
  • Kruskal: sort edges, add if it joins two different trees (union–find): O(elog⁡e)O(e \log e).
  • Prim: grow one tree with the cheapest crossing edge: O(n2)O(n^2) or O(elog⁡n)O(e \log n).
  • An MST is a lower bound for TSP, not a TSP tour.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many edges does a spanning tree of a connected graph with 12 vertices have?

Show answer

Answer: B. 11

Every tree on nn vertices has n−1n - 1 edges. 66 is the number of edges of the complete graph on 12 vertices.

Question 2 · multiple choice

In Kruskal’s algorithm, when is an edge rejected?

Show answer

Answer: C. When both its ends are already in the same tree

Adding such an edge would close a cycle. Joining two different trees is exactly what Kruskal wants.

Question 3 · multiple choice

Which data structure does Kruskal’s algorithm use to detect cycles efficiently?

Show answer

Answer: C. Union–find (disjoint sets)

Find tells which tree a vertex is in; union merges two trees. Prim uses a priority queue instead.

Question 4 · multiple choice

In Figure 8.10, Prim’s algorithm starts at F. Which edge is added first?

Show answer

Answer: A. E–F (7)

The only edges at F are E–F (7) and B–F (22); the cheapest crossing edge is 7. C–E is cheaper but does not touch the tree {F}.

Question 5 · multiple choice

What does this program print?

C · q8-kruskal.c
#include <stdio.h>

int parent[4] = {0, 1, 2, 3};
int find(int x) { while (parent[x] != x) x = parent[x]; return x; }

int main(void)
{
    int u[5] = {0, 1, 0, 2, 0}, v[5] = {1, 2, 2, 3, 3}, w[5] = {1, 2, 3, 4, 5};
    int total = 0, used = 0;
    for (int k = 0; k < 5; k++) {          /* edges already sorted by weight */
        int a = find(u[k]), b = find(v[k]);
        if (a != b) { parent[a] = b; total += w[k]; used++; }
    }
    printf("edges=%d weight=%d\n", used, total);
    return 0;
}
Show answer

Answer: C. edges=3 weight=7

Edges 0–1 (1) and 1–2 (2) are added; 0–2 (3) closes a cycle and is rejected; 2–3 (4) is added; 0–3 (5) is rejected. Total 1+2+4=71 + 2 + 4 = 7 with 3 edges.

Question 6 · short answer

Compare Kruskal’s and Prim’s algorithms (principle, data structures, complexity, suitable graphs).

Show worked answer

Principle: both are greedy MST algorithms. Kruskal considers edges in increasing order of weight over the whole graph and adds an edge if it joins two different trees of the growing forest; Prim grows a single tree from a start vertex, each time adding the cheapest edge with exactly one end in the tree. Data structures: Kruskal needs the edges sorted (or a priority queue) and a union–find structure; Prim needs a priority queue or an array of the cheapest edge to the tree for each vertex. Complexity: Kruskal O(elog⁡e)O(e \log e); Prim O(n2)O(n^2) with an array or O(elog⁡n)O(e \log n) with a binary heap. Suitability: Kruskal is good for sparse graphs (few edges to sort); Prim’s array version is better for dense graphs.

↑ ↓ move↵ openTab switch type