Krushkal
Krushkal
1 Introduction
Kruskal’s algorithm is another classic method for finding a Minimum Spanning Tree
(MST) in a connected, undirected, weighted graph. Whereas Prim’s algorithm grows the
MST by starting from a single vertex and repeatedly selecting the minimum-weight edge
connecting the tree to an external vertex, Kruskal’s algorithm takes an edge-centric ap-
proach:
• Examine edges from lightest to heaviest, adding each edge to the MST if it does not
create a cycle.
This approach is often paired with a disjoint set (union–find) data structure to quickly
detect and prevent cycles.
2 Key Ideas
• Spanning Tree: For a connected, undirected graph G = (V, E), a spanning tree is a
subset of edges that connects all vertices together without any cycle.
• Minimum Spanning Tree (MST): Of all possible spanning trees, the MST has the
smallest total sum of edge weights.
3 Step-by-Step Description
Let G = (V, E) be a connected, undirected, weighted graph with |V | = n. Each edge (u, v)
has a weight w(u, v).
1
1. Sort all edges: Sort the edge set E by weight in non-decreasing order. Let the sorted
list of edges be e1 , e2 , . . . , em where m = |E| and w(e1 ) ≤ w(e2 ) ≤ · · · ≤ w(em ).
2. Initialize: Let MST be initially empty (no edges). We will also maintain a disjoint
set (union–find) data structure, where each vertex v ∈ V is initially in its own set.
3. Process edges in ascending weight order: For each edge ei (from lightest to
heaviest):
• When an edge connects two vertices in different components, the components are
merged. Because there was no existing path between them in the MST, this cannot
create a cycle.
• If an edge connects two vertices already in the same component, a cycle would form,
so the edge is skipped.
5 Example
Consider a graph with vertices {A, B, C, D, E} and the following edge weights:
A − B : 2, A − C : 3, B − D : 3, B − C : 5,
C − D : 6, C − E : 4, D − E : 2.
2
1. Sort edges by weight:
2. Initialize union–find: Each vertex is in its own set: {A}, {B}, {C}, {D}, {E}.
Hence the selected edges form a spanning tree ({(A, B), (A, C), (B, D), (D, E)} or some
slight variation depending on ties). No cycles are formed because the union–find structure
prevents adding any edge that would connect vertices already within the same set.
6 Conclusion
• Kruskal’s Algorithm is an edge-based strategy for finding an MST. Sorting edges
by weight and then selectively adding them (if they do not create a cycle) will yield a
minimum spanning tree.
• Cycle Avoidance is handled naturally via the disjoint set (union–find) data structure.
• Time Complexity: Sorting the edges takes O(m log m), and each union–find opera-
tion can be made almost O(1) with path compression and union by rank. Hence, the
overall complexity is O(m log m), where m = |E|.