The problem
Six towns, A to F, and 9 possible cables. Each edge has a weight: the cost to lay it. A spanning tree links every node with no loop, so it has V − 1 = 5 edges. We want the cheapest: the minimum spanning tree.
Kruskal step by step
Kruskal is greedy: always try the cheapest edge left. Each edge is a (w, u, v) tuple, so sorted(edges) lines them up by weight. parent starts with every node as its own one-node tree.
D–E costs 1. find(D) is D and find(E) is E: two different trees, so this edge cannot close a loop. parent[E] = D merges the trees, and D–E joins the answer.
A–D (2): find(A) is A, find(D) is D, so take it. C–F (3) touches neither tree, so it starts a second one. The forest now holds {A, D, E}, {C, F} and B alone.
A–B (4): find(A) is A and B is still alone, so take it. B joins the big tree. Four edges so far, total weight 10.
B–E (5) is next, but find(B) and find(E) both return A. B and E are already joined, by B–A–D–E. Adding B–E would close a loop, so Kruskal skips it.
B–C (6): find(B) is A, find(C) is C, so take it. The last two trees become one. Now there are 5 = V − 1 edges, every node is linked, and the total weight is 16.
The loop still tries B–D (7), E–F (8) and C–E (9). Every node now has root A, so each would close a loop. Result: D–E, A–D, C–F, A–B, B–C, weight 16.
Why it works, what it costs
Cut the nodes into two groups: {A, B, D, E} and {C, F}. Three edges cross the cut: B–C (6), E–F (8), C–E (9). The cut property: the lightest crossing edge is in a minimum spanning tree. Every edge Kruskal takes is such an edge.
Sorting E edges costs O(E log E). Then each edge needs two finds. With union by size and path compression from union-find, a find is almost O(1), so Kruskal runs in O(E log E) time, which is O(E log V).
What happens
;; step 1/10 · graph V = 6 nodes, E = 9 edges (w, u, v): A–B weight 4 B–C weight 6 A–D weight 2 B–E weight 5 C–F weight 3 D–E weight 1 E–F weight 8 B–D weight 7 C–E weight 9 spanning tree: edges that link all V nodes, no loop it always has V − 1 = 5 edges minimum spanning tree (MST): smallest total weight
A spanning tree of a connected graph is a set of its edges that links every node and contains no loop; it always has V − 1 edges. A minimum spanning tree has the smallest total weight of all spanning trees (MIT 6.046 Lecture 12; Sedgewick and Wayne §4.3). The cut property (MIT 6.046 calls it the greedy-choice property): for any split of the nodes into two groups, a lightest edge crossing between them belongs to some minimum spanning tree. Each edge Kruskal takes is the lightest edge leaving its tree, so the tree it builds is minimum.