...

/

Kruskal’s Algorithm

Kruskal’s Algorithm

Discover Kruskal’s algorithm for building a minimum spanning tree.

Kruskal’s algorithm was discovered in 1956 by Joesph Kruskal. However, it’s almost identical to an earlier algorithm, called Borůvka’s algorithm, which was discovered by Otakar Borůvka in 1926.

A different growing strategy

Kruskal’s algorithm, like Prim’s algorithm, is an algorithm for finding minimum spanning trees. It begins with a collection of nn isolated vertices—each vertex, by itself, a tree.

In each round of Kruskal’s algorithm, an edge is added between two vertices in a way that brings two of the trees together into a single bigger tree. Each step leads to a smaller collection of trees. Kruskal’s algorithm essentially grows a tree out of a forest.

Press + to interact

The central idea

To find an MST of a connected graph GG,

  1. Start with a forest FF consisting of nn components, each containing exactly one vertex.

  2. At each step, add an edge ee between two vertices of FF that meets the following criteria:

    • The edge ee has minimum weight among all edges of GG that are not already in FF.

    • The end-vertices of ee belong to different components of FF.

    This second criterion ensures that, at each step, FF remains acyclic.

The edge addition at each step reduces the number of components by one. So, once n1n−1 edges have been added, FF consists of just a single component.

Press + to interact
A forest containing three trees
A forest containing three trees

Once all edges have been considered, the algorithm must terminate.

After the last round is finished, FF is acyclic, connected, and contains all vertices of GG. Therefore, by definition, FF is a spanning tree of GG. We’ll see later why this spanning tree is a minimum spanning tree.

Visualization

Let’s see this in action before we fill out the details.

Press + to interact
Input weighted graph for Kruskal’s algorithm
1 / 9
Input weighted graph for Kruskal’s algorithm

Let’s look at the bookkeeping required to implement the algorithm.

Implementation details

To check if an edge addition to FF creates a cycle, we want to keep track of the vertices belonging to each connected component. For this, we represent the connected components of FF as disjoint setsdisjoint of vertices, with support for the following types of operations:

Operation

Purpose

Find name of the set to which a vertex belongs.

To check if end-vertices of an edge belong to different components

Perform set unions.

To represent the merging of two components into one as a result of edge addition

Disjoint-set data structure

The disjoint-set data structure (also known as the union-find data structure) is a nifty data structure that satisfies these requirements. We assume the disjoint-set data structure used by our algorithms supports the following methods:

  • DisjointSet(v): Creates a set containing the vertex v.

  • Find(v): Returns the name of the set containing the vertex v.

  • Union(x, y): Returns union of sets named x and y.

A set receives the name of one of its member vertices. When different sets are merged, the name of the set may change to the name of a different vertex.

We discuss this data structure in greater detail in the next lesson.

Algorithm

There is more than one way to represent a minimum-spanning tree. Here, we just represent the minimum spanning tree as a simple collection SS of edges that constitute that minimum spanning tree.

Press + to interact
Kruskal’s algorithm for finding minimum spanning trees
Kruskal’s algorithm for finding minimum spanning trees

Running time

The running time of Kruskal’s algorithm depends on the choice and implementation of the data structure.

  • Line 2 is O(1)O(1).
  • Line 3 takes
...