...

/

Kruskal's Algorithm

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:

g a a b b a--b 7 c c c--a 2 b--c 8 d d d--a 8 d--b 3 e e e--a 4 e--c 6
The example graph for the MST problem

The edge with the lowest weight is (a,c)(a, c) with weight 22. We’ll add it as the first edge to our subgraph TT (marked in blue below).

g a a b b a--b 7 c c c--a 2 b--c 8 d d d--a 8 d--b 3 e e e--a 4 e--c 6
Picking the first edge for T

The next edges are (b,d)(b, d) of weight ...