Kruskal's Minimum Spanning Tree
Sort the edges from cheapest to most expensive and walk through them. Keep an edge when its two ends are in different groups, and merge those groups. Skip an edge when both ends already share a group, because it would close a cycle. Stop after V − 1 kept edges.
An edge is safe to keep exactly when its two ends are in different groups. If they already share a group, keeping the edge would close a cycle, so it must be skipped.
Problem
You are given a connected, undirected graph with V nodes, numbered 0 to V − 1. Each edge [u, v, w] joins nodes u and v and has weight w.
Choose some edges so that every node is connected to every other node and the chosen edges contain no cycle. Such a set is a spanning tree. Return the smallest possible total weight of a spanning tree. That number is the minimum spanning tree (MST) weight.
V, the number of nodes, and edges, a list of [u, v, w] triples.
A single integer: the total weight of a minimum spanning tree.
Keeping 0–2 (1) and 1–2 (3) reaches all 3 nodes for a total of 4. The other two pairs of edges cost 8 and 6.
Only one edge can be kept, so the answer is its weight.
There are two triangles, {0,1,2} and {3,4,5}, joined by bridges 1–4 (7) and 2–3 (9). Each triangle keeps its two cheapest edges (1 + 2 = 3 and 3 + 4 = 7), and the cheapest bridge 1–4 (7) joins them: 3 + 7 + 7 = 17.
- • The graph is connected and undirected.
- • Nodes are numbered from 0 to V − 1.
- • Each edge is [u, v, w]: it joins nodes u and v with weight w.
- • 1 <= V <= 1000, 1 <= E <= V*(V-1)/2
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
A spanning tree on V nodes has how many edges? Draw a small one and count.
- 2.
If I keep the single cheapest edge in the whole graph, can it ever be the wrong choice?
- 3.
When is keeping an edge a mistake? What does it do to the groups of nodes that are already connected?
- 4.
If every node keeps its own cheapest edge, do all the nodes end up connected to each other?
- 5.
I keep asking whether two nodes are already connected, while edges keep arriving. What structure answers that quickly?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Test every set of V − 1 edges; keep the cheapest one that reaches all nodes | O(2^E · E) | O(V + E) |
| Optimal | Sort by weight; keep an edge only if Union-Find says its ends are in different groups | O(E log E) | O(V) |
Why does this belong to this pattern?
- “connect every node”The answer is a connected set of edges: exactly V − 1 edges and no cycle.
- “smallest possible total weight”Minimum spanning tree: the cheapest connected set of edges.
- “undirected weighted edges”Sort the edges, then let Union-Find say whether an edge joins two groups.
This is the anchor MST problem: sort the edges, and let Union-Find decide each one. The one rule to remember is same group means skip, because that edge would close a cycle. Later questions in this sub-pattern change where the edges come from (every pair of points, a list of cities), not this rule.
Don't make this mistake
A node's cheapest edge is not a tree. On the walkthrough graph, nodes 0 and 1 both pick 0–1 (1), node 2 picks 1–2 (2), nodes 3 and 4 both pick 3–4 (3), and node 5 picks 4–5 (4). That is only 4 distinct edges with total 10, and it leaves two separate groups {0,1,2} and {3,4,5}. The bridges 1–4 (7) and 2–3 (9) are nobody's cheapest edge, so they are never taken. The answer is 17, not 10.
Plain sort on [u, v, w] orders by the first node, not by weight. On the walkthrough graph, the edges then come in the order 0–1 (1), 0–2 (5), 1–2 (2), 1–4 (7), 2–3 (9), 3–4 (3), 3–5 (6), 4–5 (4). Kruskal keeps 0–1, 0–2, 1–4, 2–3 and 3–5, for a total of 28 instead of 17.
parent[u] = v moves node u itself, even when u is not a root, so u's old group loses its link. On the triangle 0–1 (1), 0–2 (2), 1–2 (3): keeping 0–1 sets parent[0] = 1. Then 0–2 sets parent[0] = 2 and cuts node 1 off from node 0. Now 1–2 looks like it joins two groups, so it is kept: total 6 instead of 3, and the kept edges contain a cycle. unite always works on the roots, so this cannot happen.
Want to implement this?
Write it yourself on GeeksforGeeks, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Union-Find (DSU) translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.