ENع
→Kruskal's MST
الدرس 36 · Shortest paths and spanning trees

Minimum spanning trees with Kruskal

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 36 · تفاعلي
01 / 10

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. Sort 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 root
  3. Cheapest 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 = 1
  4. Two 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}
  5. 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}
  6. 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 = 10
  7. B–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 nodes
  8. The 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 = 16
  9. Why 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 tree
  10. Sorting 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

اختبار يتاح بعد الخطوة الأخيرة.