2Brute2Best
🔥0

Kruskal's Minimum Spanning Tree

Medium·GeeksforGeeksBasic Anchor
GeeksforGeeks
30-Second Mental ModelUnion-Find + Kruskal (Minimum Spanning Tree)
Complexity:O(2^E · E)→O(E log E)
Key Insight

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.

Core Validity Invariant

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.

01

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.

Input

V, the number of nodes, and edges, a list of [u, v, w] triples.

Output

A single integer: the total weight of a minimum spanning tree.

Example 1
Input: V = 3, edges = [[0,1,5],[1,2,3],[0,2,1]]
Output: 4

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.

Example 2
Input: V = 2, edges = [[0,1,5]]
Output: 5

Only one edge can be kept, so the answer is its weight.

Example 3
Input: V = 6, edges = [[0,1,1],[1,2,2],[0,2,5],[3,4,3],[4,5,4],[3,5,6],[1,4,7],[2,3,9]]
Output: 17

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.

Constraints
  • • 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
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.

  1. 1.

    A spanning tree on V nodes has how many edges? Draw a small one and count.

  2. 2.

    If I keep the single cheapest edge in the whole graph, can it ever be the wrong choice?

  3. 3.

    When is keeping an edge a mistake? What does it do to the groups of nodes that are already connected?

  4. 4.

    If every node keeps its own cheapest edge, do all the nodes end up connected to each other?

  5. 5.

    I keep asking whether two nodes are already connected, while edges keep arriving. What structure answers that quickly?

03

Brute force approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceTest every set of V − 1 edges; keep the cheapest one that reaches all nodesO(2^E · E)O(V + E)
OptimalSort by weight; keep an edge only if Union-Find says its ends are in different groupsO(E log E)O(V)
09

Why does this belong to this pattern?

Clues in the problem statement
  • “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.
Edges sorted by weight+keep an edge if its ends are in different groups+stop at V − 1 kept edges→Union-Find + Kruskal (Minimum Spanning Tree)

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.

10

Don't make this mistake

Taking each node's cheapest edge (pattern confusion)
✗ // each node picks its own cheapest edge for (int u = 0; u < V; u++) total += cheapest[u];
✓ sort the edges by weight; keep an edge only if dsu.unite(u, v) succeeds

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.

Sorting by the wrong key
✗ sort(edges.begin(), edges.end()); // compares u first, then v, then w
✓ sort by e[2] (the weight) only

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.

Uniting the nodes instead of their roots
✗ parent[u] = v;
✓ dsu.unite(u, v); // works on find(u) and find(v)

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.

11

Want to implement this?

Write it yourself on GeeksforGeeks, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Union-Find (DSU) translate into production architectures and low-level memory mechanics.