Kruskal's Algorithm

Learn to compute minimum spanning trees using Kruskal's algorithm.

In this lesson, we’ll study Kruskal’s algorithm, which makes use of a surprisingly straightforward strategy to assemble a minimum spanning tree.

Computing MSTs with Kruskal’s algorithm

The input to Kruskal’s algorithm is a connected, weighted, undirected graph G=(V,E,w)G = (V, E, w). The algorithm iteratively builds up an acyclic graph T=(V,F,w)T = (V, F, w) with FEF \subseteq E. In the beginning, FF is empty. The edges will be added until TT is a spanning tree of GG.

First, we’ll sort all edges of EE by their weight w(e)w(e) in increasing order. Next, we’ll inspect each edge e=(u,v)e = (u, v) in this order, one by one. If uu and vv are in different connected components of TT, we’ll add ee to FF. Otherwise, we’ll skip ee, as it would introduce a cycle in TT.

We’ll continue this strategy until we have processed all edges, at which point TT is a spanning tree of GG.

Let’s illustrate the algorithm on the example graph from the previous lesson:

Get hands-on with 1400+ tech skills courses.