Minimum spanning trees with Kruskal
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
You need to link six towns with cables and pay as little as possible. Kruskal's algorithm sorts the nine possible cables by cost, then tries them from cheapest to dearest: it keeps a cable when its two ends are still in different trees, and skips it when it would close a loop. Watch a union-find answer that question edge by edge, see the cut property prove the greedy choice safe, and count why the whole run costs O(E log E). The lessons "Union-find: merging groups fast" and "Greedy choices: the exchange argument and when greedy fails" cover the two tools this one uses.
A network to build
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.
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 network to build
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.
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 weightSort edges, start apart
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.
sorted(edges), tuples compare weight first: D–E 1, A–D 2, C–F 3, A–B 4, B–E 5, B–C 6, B–D 7, E–F 8, C–E 9 parent = {v: v for v in nodes} A→A B→B C→C D→D E→E F→F 6 trees of one node find(x) climbs parent links to the rootCheapest first: D–E
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.
try the lightest edge: D–E 1 find(D) = D, find(E) = E different → take, parent[E] = D len(tree) = 1, total = 1Two trees grow
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.
next two edges: A–D 2 find(A) = A, find(D) = D different → take, parent[D] = A C–F 3 find(C) = C, find(F) = F different → take, parent[F] = C len(tree) = 3, total = 6 trees: {A, D, E} {C, F} {B}A–B brings in B
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.
next edge: A–B 4 find(A) = A, find(B) = B different → take, parent[B] = A len(tree) = 4, total = 10 trees: {A, B, D, E} {C, F}B–E would close a loop
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.
next edge: B–E 5 find(B) = A, find(E) = A same root → skip (loop) find(B): B → A find(E): E → D → A same root: B–E would make the loop B–A–D–E–B len(tree) = 4, total = 10B–C joins the last trees
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.
next edge: B–C 6 find(B) = A, find(C) = C different → take, parent[C] = A len(tree) = 5, total = 16 5 edges = V − 1: one tree spans all 6 nodesThe rest all loop
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.
the last three edges: B–D 7 find(B) = A, find(D) = A same root → skip (loop) E–F 8 find(E) = A, find(F) = A same root → skip (loop) C–E 9 find(C) = A, find(E) = A same root → skip (loop) return tree: ('D', 'E', 1) ('A', 'D', 2) ('C', 'F', 3) ('A', 'B', 4) ('B', 'C', 6) total weight 1 + 2 + 3 + 4 + 6 = 16Why greedy is safe
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.
cut: {A, B, D, E} | {C, F} crossing edges: B–C 6 lightest → in the MST E–F 8 C–E 9 when Kruskal takes u–v, the cut around u's tree has no lighter crossing edge: lighter ones were tried first and joined nodes already in one treeSorting sets the cost
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).
sorted(edges) O(E log E) E times: 2 finds O(E α(V)) with union-find tricks V − 1 unions O(V α(V)) total O(E log E) E ≤ V², so log E ≤ 2 log V: O(E log E) = O(E log V) this short find has no tricks: its chains can grow
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A connected graph has V = 8 nodes and E = 20 edges. How many edges does any spanning tree of it have?
A tree on V nodes always has V − 1 edges: start with 8 one-node trees, and each edge that joins two trees cuts the count by one, so 7 edges leave one tree.
02Kruskal tries edge B–E. find(B) returns A and find(E) returns A. What happens?
The same root means B and E are already joined by tree edges. Adding B–E would make a loop, and a tree has no loops, so Kruskal skips it and moves to the next edge.
03Why does the Python code sort a list of (w, u, v) tuples rather than (u, v, w)?
Tuples compare lexicographically: the first items decide unless they are equal. With w first, sorted(edges) lines the edges up from lightest to heaviest.
04Split the nodes into two groups. Three edges cross between them, with weights 6, 8 and 9. What does the cut property say?
The lightest edge crossing any cut belongs to a minimum spanning tree. Kruskal only ever takes such an edge, which is why its greedy choices are safe.
05Which part of Kruskal's algorithm sets its O(E log E) running time?
Sorting E edges costs O(E log E). With union by size and path compression, the 2E finds and V − 1 unions are almost O(1) each, so the sort dominates.
المسرد
- Weighted graph
- A graph whose edges each carry a number, here the cost of laying a cable between two nodes.
- Spanning tree
- A set of a connected graph's edges that links every node and has no loop. It always has V − 1 edges.
- Minimum spanning tree (MST)
- A spanning tree whose total edge weight is as small as possible.
- Forest
- A set of trees that do not touch. Kruskal starts with V one-node trees and merges them.
- find
- The union-find operation that climbs parent links from a node to the root of its tree. Two nodes are in the same tree exactly when their roots match.
- Cut
- A split of the nodes into two groups. An edge crosses the cut when its two ends are in different groups.
- Cut property
- For any cut, a lightest edge crossing it belongs to some minimum spanning tree.
- Greedy algorithm
- An algorithm that makes the locally best choice at each step and never undoes it.
الملاحظات والمصادر
- 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.
- All nine weights here are different, so this graph has exactly one minimum spanning tree. With equal weights there can be several, all with the same total weight.
- The find on the card has neither union by size nor path compression, to keep it short. On a large graph add both tricks from the union-find lesson; otherwise a chain of parent links can grow long and each find slows down.
- Sedgewick and Wayne's KruskalMST stops as soon as the tree holds V − 1 edges. A min-heap (Python's heapq) gives the edges in the same order and lets you stop early without sorting them all; the worst case is still O(E log E).
- If the graph is not connected, no spanning tree exists. The same code then returns a minimum spanning forest: one tree per connected part, with fewer than V − 1 edges.
- Sedgewick and Wayne, Algorithms 4th ed., §4.3 Minimum Spanning Trees
- Sedgewick and Wayne, KruskalMST.java
- MIT 6.046J Design and Analysis of Algorithms (Spring 2015), Lecture 12 notes: Greedy Algorithms and Minimum Spanning Tree
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), ch. 21 Minimum Spanning Trees
- Python built-in functions: sorted()
- Python language reference: value comparisons (tuples compare lexicographically)
اختبار يتاح بعد الخطوة الأخيرة.