# Kruskal's algorithm

Kruskal's algorithm is a general-purpose algorithm for the minimum spanning tree problem, based on the disjoint sets data structure. The existence of very simple algorithms to maintain disjoint sets in almost constant time gives rise to simple implementations of Kruskal's algorithm whose running times are close to linear, usually outperforming Prim's algorithm in sparse graphs.

## Theory of the algorithm

Kruskal's may be characterized as a greedy algorithm, which builds the MST one edge at a time. As befits a MST algorithm, the greedy strategy is to continually add the remaining edge of lowest weight. Unlike Prim's, however, Kruskal's adds edges without regard to the connectivity of the partially built MST. We shall assume that a spanning tree exists for the following sections. (If you find them too difficult, skip them.)

### Lemma

Suppose that a spanning tree $T$ is given of some graph $G$. Then, the addition of any edge $\notin E(T)$ to $E(T)$, followed by the removal of any edge from the resulting cycle, yields a spanning tree of $G$.

Proof: Before the operation, the number of vertices is one more than the number of edges. After the operation, this is again true. As the addition of the new edge generates exactly one simple cycle, there are no longer any cycles after an edge on this cycle is removed. So the new $T$ has a vertex count which exceeds its edge count by one and contains no cycles; it must therefore be a tree.

### The algorithm

We are now ready to present the algorithm. We begin with no knowledge of the edges of the MST, and add them one by one until the MST is complete. To do so we consider edges in increasing order of weight. When considering an edge, if adding it would create a cycle, we skip it; otherwise we add it. Once $V-1$ edges have been added, we have constructed a MST. Kruskal's is both correct (Theorem 2) and complete (Theorem 1).

#### Remark

If adding an edge to the partially built MST generates a cycle, it will also do so if the edge is added to the partially built MST later on in the algorithm, since we only add edges and never remove them. The contrapositive is also true: if adding an edge does not generate a cycle, it wouldn't have generated a cycle if it were added earlier, either.

#### Theorem 1

Kruskal's algorithm will never fail to find a spanning tree in a connected graph.

Proof: By contradiction. Assume that all edges have been considered and the partially built tree is still not complete. Then, there exists some edge that connects two vertices in different connected components of the partially built tree. But this edge must have been considered at some point and discarded as adding it would have created a cycle. This is a contradiction, as adding it now would not create a cycle, per the Remark above.

#### Theorem 2

Kruskal's algorithm will never produce a non-minimal spanning tree.

Proof:
We first proceed by induction on the number of edges so far added.

• When no edges have yet been added, the set of edges so far added, being empty, is obviously a subset of the edges of some MST.
• Suppose that $k$ edges have been added in this manner ( $1 \le k < V$), and we know that they form a subset $S$ of the edges of some MST. All further edges to be considered are of greater or equal weight. If adding an edge generates a cycle at this point, it can be discarded, as it can never be added per the Remark above. According to our algorithm, then, the next edge to be added is the edge $e$ with minimal weight which does not generate a cycle. Consider now any MST $T$ containing the edges so far added as a subset of its edge set. It it contains $e$, we are done; we know that when $e$ is added our edge set is still a subset of the edge set of some MST. Otherwise, add the edge $e$ to $E(T)$. A cycle is now generated. This cycle must contain some edge not yet considered, because we know that the edge currently under consideration, in conjunction with the edges previously added, do not form a cycle. This edge not yet considered has a weight greater than or equal to that of $e$. Then, deleting this edge yields a new spanning tree (per the Lemma above). If the edge deleted was of greater weight than $e$, the resulting spanning tree has lower weight than $T$, a contradiction as $T$ was assumed to be minimal. Otherwise, it must have equal weight to $e$, and deleting it and adding $e$ gives a new spanning tree of equal weight, that is, another MST. So we have proven that given that there is any MST at all containing $S$, we can add $e$ and find some MST containing this new set as a subset of its own edge set.

Thus, if a spanning tree is built, then its edges form a subset of the edge set of some MST of the graph, and therefore the spanning tree must be that MST itself.

## Implementation

We have so far glossed over a crucial detail: we must have a means of efficiently deciding whether an edge can be added without generating a cycle, and of adding that edge if it can. To do so, we note that a cycle is created if and only if the two endpoints of the edge are in the same connected component. We could, of course, answer this query through any graph search algorithm such as DFS or BFS. However, that would make the algorithm quadratic time overall, which is undesirable. Instead, we notice that a data structure already exists which can efficiently identify the component containing a vertex and add new edges (joining together components). Assuming that we know how to implement it, then, here is Kruskal's algorithm:

input G
let T = (V(G),∅)
for each {u,v} ∈ E(G) in increasing order of wt(u,v)
if find(u) ≠ find(v)
unite(u,v)

It would make sense to stop the loop after $V-1$ edges have been added (instead of processing every edge, which is often unnecessary). Nevertheless, it does not affect the asymptotic complexity (see Analysis)
The usual implementation of Kruskal's sorts the edges by weight first, which takes $\mathcal{O}(E\lg E)$ time. Following this, $\mathcal{O}(E)$ union-find operations are performed. As each can be done in almost-constant time (see the article itself), this step requires approximately $\mathcal{O}(E)$ time to complete. The cost of the sort then dominates the running time. As typical implementations of quicksort, usually used here, are faster than those of heapsort, which may be said to operate implicitly in Prim's algorithm, a well-coded Kruskal's will outperform Prim's in sparse graphs. In dense graphs, Prim's non-heap implementation, taking $\mathcal{O}(E+V^2)$ time, is likely to outperform Kruskals, with runtime close to $\mathcal{O}(V^2 \lg V)$. In-place quicksort takes expected linear stack space, and the disjoint set data structure takes linear extra space, so Kruskal's has a linear memory complexity.