Algorithms And Data Structures Codexery

Kruskal's algorithm

Greedy algorithm for minimum spanning trees using sorted edges.

Kruskal's algorithm

Kruskal's algorithm is a greedy algorithm for finding a minimum spanning forest of an undirected edge-weighted graph. If the graph is connected, it finds a minimum spanning tree.

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 25-38

Elsewhere in the Algorithms And Data Structures universe

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 →