Python 3D Academyবাংলা

Why the cheapest crossing edge is always safe

🌍 In real life. A river splits a district: some villages on the east bank, the rest on the west. However the best network is drawn, it has to cross the river somewhere, with at least one cable from bank to bank. So take the cheapest cable across the river. It can never be a mistake: if the best network used a dearer crossing instead, swapping in the cheaper one would make it no dearer.

The idea in one sentence: for any split of the vertices into two sides, the cheapest edge crossing between the sides belongs to some minimum spanning tree (the cut property).

Its words:

  • cut: a split of the vertices into two non-empty sides
  • crossing edge: an edge with one end on each side
  • cut property: the cheapest crossing edge of any cut is safe to put in the tree
  • exchange argument: a proof that swaps one edge for another and shows the total doesn't grow

The proof, step by step:

  1. pick any cut, and let e be its cheapest crossing edge
  2. take any minimum spanning tree T; if e is in T, there is nothing to prove
  3. otherwise, adding e to T closes exactly one loop, through e
  4. that loop leaves one side by e, so it must come back by some other crossing edge f
  5. f costs at least as much as e, because e is the cheapest crossing; remove f, keep e: still a spanning tree, and no dearer. So a minimum spanning tree with e exists

Both algorithms only ever take such an edge. Prim's cut is "the tree" against "the rest". When Kruskal takes a cable, its cut is "one end's group" against "everyone else": every cheaper cable was tried and none crosses that cut. Prim from D builds the same total, 19, in yet another order:

When there is no spanning tree. If the villages can't all be joined, every cut between the separate pieces has no crossing cable at all. Kruskal then builds the cheapest tree inside each piece (a minimum spanning forest):

What it costs, by counting. There can be a huge number of spanning trees: when every pair of 10 villages can be joined, there are 10⁸ = 100,000,000 of them (Cayley's formula, n to the power n − 2). The cut property is why Kruskal and Prim never compare them: each cheap local choice is safe, so O(E log E) work is enough.

def cheapest_crossing(cables, side):
    crossing = [(w, a, b) for a, b, w in cables if (a in side) != (b in side)]   # exactly one end inside
    if not crossing:
        return None
    w, a, b = min(crossing)          # cheapest; equal prices go by the villages' names
    return (a, b, w)

cables = [("A", "B", 4), ("A", "C", 3), ("B", "C", 2), ("B", "D", 5), ("C", "D", 6), ("C", "E", 7), ("D", "E", 1), ("D", "F", 8), ("E", "F", 9)]
print(cheapest_crossing(cables, {"A", "B", "C"}))
print(cheapest_crossing(cables, {"F"}))
print(cheapest_crossing(cables, {"A"}))
print(cheapest_crossing(cables, {"A", "B", "C", "D", "E", "F"}))

Mistakes and edge cases.

  • "the dearest cable is never in an MST": not if it is the only cable to some village; that cable crosses the cut around that village alone, so it is the cheapest crossing there
  • the cheapest cable at each village: it is always safe too, using the cut of that village alone against the rest
  • equal prices: with ties, the cheapest crossing edge is in some MST, not necessarily every one; when every price is different, the MST is unique
  • a maximum spanning tree: the same argument with "dearest crossing" gives it; just sort dearest first

Quiz

  1. What is a cut?

    • A split of the vertices into two non-empty sides
    • Removing the dearest edge
    • An edge that closes a loop
  2. In the proof, why must the loop made by adding e contain another crossing edge f?

    • A loop that leaves a side by e has to come back to it
    • Every loop has exactly two edges
    • f is always the dearest edge
  3. What does this print?

    def cheapest_crossing(cables, side):
        crossing = [(w, a, b) for a, b, w in cables if (a in side) != (b in side)]   # exactly one end inside
        if not crossing:
            return None
        w, a, b = min(crossing)          # cheapest; equal prices go by the villages' names
        return (a, b, w)
    
    print(cheapest_crossing([("A", "B", 4), ("A", "C", 3), ("B", "C", 2), ("B", "D", 5), ("C", "D", 6), ("C", "E", 7), ("D", "E", 1), ("D", "F", 8), ("E", "F", 9)], {"D", "E"}))
  4. Can the dearest cable of all be in every minimum spanning tree?

    • Yes, when it is the only cable to some village
    • Never
    • Only if all prices are equal
  5. How many spanning trees are there when every pair of 10 villages can be joined?

    • 100,000,000, that is 10 to the power 8
    • 45
    • 9
  6. For the side {"A", "B", "C"} the cheapest crossing cable is ('B', 'D', 5), but this answers ('D', 'E', 1), a cable with both ends outside. Which line is wrong?

    def cheapest_crossing(cables, side):
        crossing = [(w, a, b) for a, b, w in cables if (a in side) == (b in side)]
        if not crossing:
            return None
        w, a, b = min(crossing)
        return (a, b, w)

Coding tasks

  1. Write cheapest_crossing(cables, side): side is a set of villages. Return the cheapest cable (a, b, price) with exactly one end in side, or None if none crosses. For equal prices, take the one with the smallest (a, b).

  2. DSU is written for you. A road authority wants the network of roads with the largest total capacity that still connects every village with no loop. Write max_spanning(vertices, cables): the total of a maximum spanning tree.