Lukas' Notes

Definition

Kruskal's Algorithm

Let be a finite, connected, undirected graph with edge weights . Kruskal’s algorithm is a greedy algorithm that constructs a minimum spanning tree: a tree spanning all vertices and minimising

Starting with the forest , scan the edges in non-decreasing weight order. For each edge ,

where is the component containing in . Accepting means adding to : two trees merge into one. An edge within one tree is rejected because it would create a cycle.

Solid cyan edges belong to ; the dashed orange edge is under consideration.

Disconnected graphs and tied weights

On a disconnected network, the same procedure returns a minimum spanning forest: one minimum spanning tree per connected component. Equal-weight edges may be processed in any order; different orders can produce different optimal forests.

Algorithm

A union-find data structure stores the components of . Its sets represent connectivity, not the actual edges of the output tree; those edges are stored separately in .

Kruskal(V, E, w):
    F ← ∅
    for each vertex v in V:
        MakeSet(v)
 
    for each edge {u, v} in E, sorted by non-decreasing weight:
        ru ← Find(u)
        rv ← Find(v)
        if ru ≠ rv:
            F ← F ∪ {{u, v}}
            Union(ru, rv)
 
    return F

For a connected graph, the scan may stop as soon as . Negative weights need no special treatment: only the order of weights matters.

Correctness

Forest invariant

After every processed edge, is acyclic and is contained in some minimum spanning tree of . Consequently, on a connected graph, Kruskal’s algorithm returns a minimum spanning tree.

Proof

Initially, is contained in every spanning tree. Rejecting an edge leaves the invariant unchanged. Suppose the next accepted edge is .

The accepted edge is cheapest across a component boundary

Let . Since , adding merges two components and cannot create a cycle.

Every edge crossing from to was also between different components earlier: components only merge. If such an edge had already been processed, it would have been accepted, contradicting the current boundary of .

Therefore no crossing edge was processed before . By the sorted order,

Exchange an edge without increasing the weight

By the invariant, choose a minimum spanning tree containing . If , nothing needs to be changed. Otherwise, the unique – path in crosses the boundary of through some edge .

The solid path illustrates the – path in . Adding closes a cycle; removing breaks that cycle while preserving connectivity. Moreover, , since no edge of crosses its own component boundary. Hence

Since was already minimum, is minimum too. This preserves the invariant.

The final forest spans the graph

If two components remained after the scan, connectivity of would provide an edge between final components. Its endpoints were also separate when it was processed, so it would have been accepted: a contradiction.

Thus is a spanning tree. Since it is contained in a minimum spanning tree and both have edges, it is itself minimum. For disconnected graphs, the same argument applies separately to each connected component.

Complexity

Write and . With comparison sorting and union-find using both path compression and union by rank or size:

WorkTime
Initialise singleton sets
Sort edges
At most finds and successful unions amortised

Here is the inverse Ackermann function, which grows extremely slowly. The total is , taking the sorting cost as zero for .

For a connected simple graph with , , giving the usual bound. Union-find and the output forest need space; storing the edge list needs space, with additional sorting space depending on the implementation.

Kruskal is a natural choice for an edge-list representation, particularly for sparse graphs. It is not universally faster than Prim’s algorithm: the comparison depends on the graph representation and priority-queue implementation.

Example

Three accepted edges, then two cycle edges

Consider the edges in processing order:

Each panel shows the same graph after an acceptance. Orange edges belong to ; thin cyan edges have not been accepted.

The component partition changes as follows:

We may now stop: the three accepted edges form a spanning tree with total weight .

If the scan continues, is rejected because and are already joined by . Likewise, is rejected because the path already exists.

Further walkthrough: video explanation of Kruskal’s algorithm.