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 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.
The central idea
To find an MST of a connected graph ,
-
Start with a forest consisting of components, each containing exactly one vertex.
-
At each step, add an edge between two vertices of that meets the following criteria:
-
The edge has minimum weight among all edges of that are not already in .
-
The end-vertices of belong to different components of .
This second criterion ensures that, at each step, remains acyclic.
-
The edge addition at each step reduces the number of components by one. So, once edges have been added, consists of just a single component.
Once all edges have been considered, the algorithm must terminate.
After the last round is finished, is acyclic, connected, and contains all vertices of . Therefore, by definition, is a spanning tree of . 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.
Let’s look at the bookkeeping required to implement the algorithm.
Implementation details
To check if an edge addition to creates a cycle, we want to keep track of the vertices belonging to each connected component. For this, we represent the connected components of as
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 vertexv
. -
Find(v)
: Returns the name of the set containing the vertexv
. -
Union(x, y)
: Returns union of sets namedx
andy
.
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 of edges that constitute that minimum spanning tree.
Running time
The running time of Kruskal’s algorithm depends on the choice and implementation of the data structure.
- Line 2 is .
- Line 3 takes