1. Gyanastra
  2. Algorithms
  3. Minimum Spanning Trees: Kruskal, Prim and Union-Find
  4. Kruskal's Algorithm with a Full Trace
Free lesson, no account needed 11 min read 10 practice questions after it
Asked in past papers9 times
  • GATE CSE201120092006200319921991
  • UGC NET201920182008

Algorithms · Minimum Spanning Trees: Kruskal, Prim and Union-Find

Kruskal's Algorithm with a Full Trace

3.1 Idea

Sort all edges by weight ascending. Scan the sorted list; accept an edge if it does not create a cycle with the edges already accepted; otherwise reject it. Stop when n−1n-1 edges are accepted.

Cycle detection is done with a disjoint-set (union-find) structure: two vertices are in the same set iff they are already connected by accepted edges. An edge (u,v)(u,v) creates a cycle iff find(u) == find(v).

3.2 Pseudocode

KRUSKAL(G, w):
  A = empty set
  for each vertex v in V: MAKE-SET(v)
  sort E into nondecreasing order by w
  for each edge (u,v) in sorted order:
      if FIND(u) != FIND(v):
          A = A ∪ {(u,v)}
          UNION(u,v)
  return A

3.3 Correctness (one paragraph)

Every accepted edge is the lightest edge crossing the cut between its own component and the rest at the moment of acceptance (because edges are processed in sorted order, all cheaper crossing edges were already considered and either accepted — merging the components — or rejected as internal). By the cut property it is safe. Every rejected edge closes a cycle in which it is the heaviest edge (all other edges on that cycle were accepted earlier, hence are lighter or equal), so by the cycle property it is safe to discard. Hence the final AA is an MST.

3.4 Complexity

  • Sorting: O(mlog⁡m)O(m\log m).
  • Union-find: O(m α(n))O(m\,\alpha(n)) with union by rank + path compression, where α\alpha is the inverse Ackermann function (α(n)≤4\alpha(n)\le 4 for all practical nn).
  • Total: O(mlog⁡m)=O(mlog⁡n)O(m\log m) = O(m\log n) since m≤n2m\le n^2 implies log⁡m≤2log⁡n\log m\le 2\log n.

GATE convention: write O(Elog⁡E)O(E\log E) or O(Elog⁡V)O(E\log V); both are accepted and equal up to constant factor. For a dense graph m=Θ(n2)m=\Theta(n^2), Kruskal is O(n2log⁡n)O(n^2\log n); for a sparse graph m=O(n)m=O(n), it is O(nlog⁡n)O(n\log n).

3.5 Full Trace on a 7-Vertex Graph

Take V={1,2,3,4,5,6,7}V=\{1,2,3,4,5,6,7\} and the following 11 edges (weights are not all distinct: there are ties at 5, 7, 8 and 9; the MST is nevertheless unique, which was checked by enumerating all spanning trees):

#EdgeWeight
1(1,2)7
2(1,4)5
3(2,3)8
4(2,4)9
5(2,5)7
6(3,5)5
7(4,5)15
8(4,6)6
9(5,6)8
10(5,7)9
11(6,7)11

Step 0 — sort edges ascending by weight:

PositionEdgeWeight
1(1,4)5
2(3,5)5
3(4,6)6
4(1,2)7
5(2,5)7
6(2,3)8
7(5,6)8
8(2,4)9
9(5,7)9
10(6,7)11
11(4,5)15

(Ties at weight 5, 7, 8, 9 broken by edge label; any tie-break gives the same total weight here because the MST is unique.)

Step 1 — process each edge. Initially each vertex is its own set: {1}{2}{3}{4}{5}{6}{7}\{1\}\{2\}\{3\}\{4\}\{5\}\{6\}\{7\}.

StepEdgewfind(u), find(v)Same set?ActionSets afterAccepted count
1(1,4)51, 4NoAccept{1,4}{2}{3}{5}{6}{7}1
2(3,5)53, 5NoAccept{1,4}{2}{3,5}{6}{7}2
3(4,6)61, 6NoAccept{1,4,6}{2}{3,5}{7}3
4(1,2)71, 2NoAccept{1,2,4,6}{3,5}{7}4
5(2,5)71, 3NoAccept{1,2,3,4,5,6}{7}5
6(2,3)81, 1YesReject (cycle 2–3–5–2)unchanged5
7(5,6)81, 1YesReject (cycle 5–6–4–1–2–5)unchanged5
8(2,4)91, 1YesReject (cycle 2–4–1–2)unchanged5
9(5,7)91, 7NoAccept{1,2,3,4,5,6,7}6
10(6,7)111, 1YesReject (cycle 6–7–5–2–1–4–6)unchanged6
11(4,5)151, 1YesReject (cycle 4–5–2–1–4)unchanged6

We stop after step 9 because n−1=6n-1 = 6 edges are accepted; steps 10–11 are shown only for completeness (they would be rejected anyway).

MST edges: {(1,4),(3,5),(4,6),(1,2),(2,5),(5,7)}\{(1,4), (3,5), (4,6), (1,2), (2,5), (5,7)\}.

Total weight: 5+5+6+7+7+9=395+5+6+7+7+9 = 39.

Recheck: 5+5=105+5=10; 10+6=1610+6=16; 16+7=2316+7=23; 23+7=3023+7=30; 30+9=3930+9=39. ✓

Recheck edge count: 6 edges for 7 vertices. ✓

Recheck connectivity: 1–4–6, 1–2, 2–5, 5–3, 5–7. All 7 vertices reached. ✓

Recheck acyclicity: the accepted edges form a tree (6 edges, 7 vertices, connected). ✓

Recheck rejections against the cycle property:

  • (2,3) w=8: cycle 2–3–5–2 has weights 8, 5, 7; heaviest is 8 = (2,3). ✓
  • (5,6) w=8: cycle 5–6–4–1–2–5 has weights 8, 6, 5, 7, 7; heaviest is 8 = (5,6). ✓
  • (2,4) w=9: cycle 2–4–1–2 has weights 9, 5, 7; heaviest is 9 = (2,4). ✓
  • (6,7) w=11: cycle 6–7–5–2–1–4–6 has weights 11, 9, 7, 7, 5, 6; heaviest is 11 = (6,7). ✓
  • (4,5) w=15: cycle 4–5–2–1–4 has weights 15, 7, 7, 5; heaviest is 15 = (4,5). ✓

Every rejection is justified by the cycle property. ✓

Pages 1–2 of 2