Menu
Coddy logo textTech

Kruskal's Algorithm

Last updated

Kruskal's algorithm builds a minimum spanning tree (MST) - the cheapest set of edges that connects every node with no cycles. It sorts all edges by weight, then greedily adds the next-cheapest edge as long as it joins two nodes that are not already connected. Press play above to watch the tree grow, one cheapest safe edge at a time.

The "already connected?" check is done with a union-find (disjoint-set) data structure: each edge is skipped if its two endpoints are already in the same component, because adding it would form a cycle. Sorting dominates the cost, giving O(E log E) overall. Kruskal shines on sparse graphs.

Time & space complexity

MeasureComplexityNotes
TimeO(E log E)Dominated by sorting the edges
Union-find ops≈ O(E α(V))Near-constant per check with path compression
SpaceO(V + E)Edge list + disjoint-set structure
Best forSparse graphsWorks from a global edge list

Step by step

StepWhat happens
1Sort every edge by weight, ascending.
2Put each node in its own component (union-find).
3Take the next-cheapest edge.
4If its endpoints are in different components, add it to the tree and union them.
5Otherwise skip it (it would create a cycle).
6Stop once the tree has V − 1 edges.

Worked example

Graph with nodes A, B, C, D and edges A-B(1), B-C(2), A-C(3), C-D(4), B-D(5). Sorted edges: A-B(1), B-C(2), A-C(3), C-D(4), B-D(5):

Edge (weight)Components beforeAction
A-B(1){A} {B} {C} {D}Different components - add to MST, union to {A,B}.
B-C(2){A,B} {C} {D}Different components - add to MST, union to {A,B,C}.
A-C(3){A,B,C} {D}A and C already together - skip (would form a cycle).
C-D(4){A,B,C} {D}Different components - add to MST, union to {A,B,C,D}.
Stop{A,B,C,D}Tree has V - 1 = 3 edges. MST = A-B, B-C, C-D, total weight 7.

When to use Kruskal's algorithm

Use it whenAvoid it when
The graph is sparse (few edges relative to nodes).The graph is dense - Prim's with a heap is usually faster.
Edges are already available as a global list you can sort.Edges arrive only through adjacency lists you must scan per node.
You want a simple implementation backed by union-find.You need the tree to grow from one specific start node.
You want to build a spanning forest of a disconnected graph.You must handle edges streaming in without a full sort.

Kruskal's Algorithm code

A clean, runnable Kruskal's Algorithm implementation in Python, JavaScript, Java, C++, C. Pick a language, copy the code, or open it pre-loaded in the Coddy Playground.

Kruskal's Algorithm code in Python

Python
1def find(parent, x):2    while parent[x] != x:3        parent[x] = parent[parent[x]]  # path compression4        x = parent[x]5    return x6
7
8def kruskal(vertices, edges):9    # Take the cheapest edge that does not close a cycle10    parent = {v: v for v in vertices}11    mst, total = [], 012    for w, u, v in sorted(edges):13        root_u, root_v = find(parent, u), find(parent, v)14        if root_u != root_v:15            parent[root_u] = root_v16            mst.append((u, v, w))17            total += w18    return mst, total19
20
21vertices = ["A", "B", "C", "D", "E"]22edges = [23    (4, "A", "B"), (1, "A", "C"), (3, "B", "C"), (2, "B", "D"),24    (5, "C", "D"), (6, "C", "E"), (7, "D", "E"),25]26
27mst, total = kruskal(vertices, edges)28for u, v, w in mst:29    print(f"{u} - {v} (weight {w})")30print("Total MST weight:", total)
Run this code in the Python Playground

Kruskal's Algorithm FAQ

What is a minimum spanning tree?
A minimum spanning tree (MST) of a connected weighted graph is a subset of edges that connects all the nodes with no cycles and the smallest possible total weight. It has exactly V - 1 edges for V nodes.
What is the difference between Kruskal's and Prim's algorithm?
Both build a minimum spanning tree greedily. Kruskal sorts all edges globally and adds the cheapest one that doesn't form a cycle, using union-find. Prim grows a single tree outward from a start node, always adding the cheapest edge that leaves the tree. Kruskal suits sparse graphs; Prim (with a heap) suits dense ones.
Why does Kruskal's algorithm use union-find?
Before adding an edge, Kruskal must check whether its two endpoints are already connected - adding such an edge would create a cycle. Union-find (disjoint-set) answers "are these in the same component?" and merges components in near-constant amortized time, which keeps the algorithm efficient.
When should I use Kruskal's algorithm instead of Prim's?
Prefer Kruskal when the graph is sparse and you already have all edges as a list you can sort - the O(E log E) sort is cheap when E is small. Prim's algorithm with a binary or Fibonacci heap tends to win on dense graphs where E approaches , because it avoids sorting every edge up front.
Does Kruskal's algorithm work on disconnected graphs?
Yes. If the graph is disconnected, Kruskal simply cannot reach V - 1 edges and instead produces a minimum spanning forest - one MST per connected component. This falls out naturally because union-find never merges nodes that have no path between them.
What is the most common mistake when implementing Kruskal's algorithm?
Skipping the union-find's path compression and union by rank, which drops each connectivity check from near-constant to nearly linear and can dominate the runtime. Another frequent bug is forgetting to actually union the two components after adding an edge, which lets later edges create cycles.
Coddy programming languages illustration

Master algorithms with Coddy

GET STARTED