Algorithms And Data Structures Codexery

Kruskal's algorithm

Greedy algorithm for minimum spanning trees using sorted edges.

Kruskal's algorithm works on an undirected graph where each edge has a weight. It produces a minimum spanning forest: for a connected graph, this is a minimum spanning tree; for a disconnected graph, it gives one minimum spanning tree per connected component. The algorithm is greedy, repeatedly picking the lightest edge that does not create a cycle and adding it to the growing forest. To do this efficiently, it first sorts all edges by weight, then uses a disjoint-set data structure to quickly check whether an edge’s endpoints are already in the same tree. The overall running time is dominated by the sorting step, which takes O(E log E) time (or equivalently O(E log V) for graphs without isolated vertices). If the edges are already sorted or can be sorted in linear time (e.g., with counting sort or radix sort), the disjoint-set operations become the bottleneck, adding O(E α(E,V)) time, where α is the inverse Ackermann function.

The algorithm starts by creating a forest where each vertex is its own tree. It then sorts all edges by weight. Going through the edges from lightest to heaviest, for each edge it tests whether adding it would form a cycle. If not, the edge is added, merging two trees into one. When all edges have been processed, the resulting forest is a minimum spanning forest.

In pseudocode, using a disjoint-set data structure: function Kruskal(Graph G) is F := ∅ for each v in G.Vertices do MAKE-SET(v) for each {u, v} in G.Edges ordered by increasing weight({u, v}) do if FIND-SET(u) ≠ FIND-SET(v) then F := F ∪ { {u, v} } UNION(FIND-SET(u), FIND-SET(v)) return F

The proof of correctness has two parts. First, the algorithm always produces a spanning tree (or forest). It cannot create a cycle because edges that would form one are rejected. It cannot leave the graph disconnected, because the first edge that would join two separate components will eventually be added. Second, the spanning tree is minimal. This is shown by induction: at every step, the set of chosen edges F can be extended to some minimum spanning tree that also contains none of the edges already rejected. Initially, when F is empty, any minimum spanning tree works. When a new edge e is added, if it is already in that minimum spanning tree T, the property holds. If e is not in T, then adding e to T creates a cycle. That cycle must contain an edge f that is not in F and not e, and f belongs to T. Since f was not considered earlier, its weight is at least as large as e’s weight (otherwise the algorithm would have picked f first). Replacing f with e in T yields another spanning tree with total weight no greater than T’s, so the property continues to hold.

Joseph Kruskal first published the algorithm in 1956; it was rediscovered soon after by Loberman and Weinberger in 1957. Other algorithms for the same problem include Prim’s algorithm, Borůvka’s algorithm, and the reverse-delete algorithm.

field
Computer science, graph theory
known_for
Kruskal's algorithm for minimum spanning trees
related_algorithms
Prim's algorithm, Borůvka's algorithm, reverse-delete algorithm

Lore & Background

Kruskal's algorithm operates by first creating a forest of single-vertex trees, one for each vertex in the graph. It then sorts all graph edges by weight and iterates through them in ascending order. For each edge, it tests whether adding it to the current forest would create a cycle; if not, the edge is added, combining two trees into one. The algorithm uses a disjoint-set data structure to efficiently detect cycles and manage the forest components. The algorithm's running time is dominated by the sorting step, achieving O(E log E) time (or equivalently O(E log V) for graphs with no isolated vertices). After sorting, the disjoint-set operations take amortized time O(E α(V)), where α is the extremely slowly growing inverse Ackermann function. In cases where edges are already sorted or can be sorted in linear time using integer sorting, the total time becomes O(E α(V)). A parallel variant called Filter-Kruskal has been described by Osipov et al., which partitions edges similarly to quicksort and filters out edges connecting vertices of the same tree to reduce sorting costs. This variant lends itself better to parallelization as sorting, filtering, and partitioning can be performed in parallel by distributing edges between processors.

Reader's Guide

Kruskal's algorithm is a foundational method in graph theory and computer science for constructing minimum spanning trees and forests. Its significance lies in its simplicity and efficiency, using a greedy approach that always selects the lowest-weight edge that does not create a cycle. The algorithm's reliance on sorting and disjoint-set data structures makes it a classic example of how data structures can optimize algorithmic performance. Its correctness is proven by induction, showing that the set of chosen edges is always contained within some minimum spanning tree. The algorithm is inherently sequential and hard to parallelize, but variants like Filter-Kruskal have been developed to improve parallel performance. Kruskal's algorithm remains widely taught and used in network design, clustering, and other applications requiring optimal connectivity with minimal total edge weight.

Did You Know?

Frequently Asked Questions

What are Kruskal's algorithm's powers/role?

Its signature move is to sort every edge by weight, then greedily claim the cheapest edge that won't create a cycle, tracking connectivity with a union-find structure. On a connected graph this produces a minimum spanning tree; on a disconnected one it yields a minimum spanning forest.

How does Kruskal's algorithm's story end?

The run concludes the moment exactly n − 1 edges have been accepted (n being the vertex count), completing the spanning tree. If the graph is disconnected, it simply stops when no further cycle-free edge is available, leaving behind a minimum spanning forest.

Why is Kruskal's algorithm important?

It offers a clean, provably optimal way to pick the cheapest set of links that ties a whole network together, which makes it a go-to tool in network design, clustering, and infrastructure planning. Its sorted-edge, union-find approach gives a straightforward O(E log E) bound that is easy to implement and teach.

Who else shares Kruskal's algorithm's powers?

Prim's algorithm, Borůvka's algorithm, and the reverse-delete algorithm all solve the same minimum-spanning-tree problem but from different angles. Kruskal's stands apart by working edge-by-edge over a globally sorted list rather than growing from a single vertex or contracting whole components.

More in Algorithms And Data Structures 1-24

Spotted an error? Know more?

This is a living reference — every entry is fact-audited, and reader corrections feed straight into our audit queue. Suggest an edit · See this site's audit record

Comments

Loading…
Open in the interactive codex →