Skip to content
Part IA Lent Term

Kruskal's Algorithm

Kruskal’s algorithm builds an MST by processing edges in non-decreasing weight order, adding an edge if it connects two different trees in the growing forest. It uses a disjoint-set data structure to track components efficiently.

Algorithm

KRUSKAL(G, w):
    A = empty set
    for each vertex v in G.V:
        MAKE-SET(v)
    sort edges of G.E into non-decreasing order by weight w
    for each edge (u, v) in sorted order:
        if FIND-SET(u) != FIND-SET(v):
            A = A union {(u, v)}
            UNION(u, v)
    return A

Each vertex starts in its own set. When an edge is selected, the two sets (trees) are merged via UNION. An edge is rejected if its endpoints are already in the same set (would create a cycle).

Correctness

At any iteration, AA is a forest. The cut (Cu,VCu)(C_u, V \setminus C_u), where CuC_u is the component containing uu, respects AA. Since edges are processed in non-decreasing weight, (u,v)(u, v) is the lightest edge crossing this cut at the time of consideration (any lighter edge would have been processed earlier and either added or rejected because its endpoints were in different components then but the same component now). By the safe-edge theorem, (u,v)(u, v) is safe.

Running Time

  • Initialisation of V|V| disjoint sets: O(V)O(|V|)
  • Sorting edges: O(ElogE)=O(ElogV)O(|E| \log |E|) = O(|E| \log |V|), since EV2|E| \le |V|^2 implies logE2logV\log |E| \le 2 \log |V|
  • Processing edges: E|E| calls to FIND-SET and at most V1|V|-1 calls to UNION. With union-by-rank and path compression, each near O(α(V))O(\alpha(|V|)), where α\alpha is the inverse Ackermann function (effectively constant, 4\le 4 for any practical input)

Total: O(ElogV)O(|E| \log |V|), dominated by sorting.

Kruskal is optimal for sparse graphs where E=O(V)|E| = O(|V|).

Worked Example

Graph with vertices {A,B,C,D,E,F}\{A, B, C, D, E, F\} and edges (weight):

A-B: 4,  A-C: 1,  B-C: 3,  B-D: 2
C-D: 5,  C-E: 6,  D-E: 7,  D-F: 4,  E-F: 3

Sorted edges: (A,C,1)(A,C,1), (B,D,2)(B,D,2), (B,C,3)(B,C,3), (E,F,3)(E,F,3), (A,B,4)(A,B,4), (D,F,4)(D,F,4), (C,D,5)(C,D,5), (C,E,6)(C,E,6), (D,E,7)(D,E,7).

Processing:

  1. (A,C,1)(A,C,1): different sets, add. Components: {A,C},{B},{D},{E},{F}\{A,C\}, \{B\}, \{D\}, \{E\}, \{F\}
  2. (B,D,2)(B,D,2): different sets, add. Components: {A,C},{B,D},{E},{F}\{A,C\}, \{B,D\}, \{E\}, \{F\}
  3. (B,C,3)(B,C,3): different sets, add (connects {A,C}\{A,C\} and {B,D}\{B,D\}). Components: {A,B,C,D},{E},{F}\{A,B,C,D\}, \{E\}, \{F\}
  4. (E,F,3)(E,F,3): different sets, add. Components: {A,B,C,D},{E,F}\{A,B,C,D\}, \{E,F\}
  5. (A,B,4)(A,B,4): same set (both in {A,B,C,D}\{A,B,C,D\}), skip
  6. (D,F,4)(D,F,4): different sets, add. Components: {A,B,C,D,E,F}\{A,B,C,D,E,F\}
  7. A=5=V1|A| = 5 = |V| - 1, terminate. (Remaining edges skipped.)

MST edges: (A,C)(A,C), (B,D)(B,D), (B,C)(B,C), (E,F)(E,F), (D,F)(D,F). Total weight: 1+2+3+3+4=131 + 2 + 3 + 3 + 4 = 13.

Kruskal example step by step

Comparison with Prim

AspectKruskalPrim
StrategyForest of trees, merge by edgesSingle tree, grow by vertices
Data structureDisjoint-setPriority queue
ComplexityO(ElogV)O(\lvert E \rvert \log \lvert V \rvert)O(ElogV)O(\lvert E \rvert \log \lvert V \rvert) (binary heap)
Best forSparse graphsDense graphs
Edge sorting requiredYesNo

Summary

StepDetail
SortO(ElogE)O(\lvert E \rvert \log \lvert E \rvert)
Disjoint-set opsO(1)\sim O(1) amortised each (inverse Ackermann)
Edge selectionGreedy, always picks lightest safe edge
TotalO(ElogV)O(\lvert E \rvert \log \lvert V \rvert)
TerminationStop when A=V1\lvert A \rvert = \lvert V \rvert - 1

Past Tripos: y2024p2q7, y2023p1q9.