Discrete MathematicsTrees4 of 5
Trees · 4

Minimum Spanning Trees: Kruskal and Prim

When every link in a network costs the same, Breadth-First Search or Depth-First Search can connect all the vertices using any spanning tree, but when each route has its own cost, we need the tree with the lowest total cost.

1

Why the Cheapest Network Is a Tree

We want to connect six islands A,B,C,D,E,FA, B, C, D, E, F using underwater cables so that every island can reach every other island for the lowest total cost.

AA
BB
CC
DD
EE
FF
11
22
33
44
55
66
77
88
99
Why the Cheapest Network Is a Tree
AA
BB
CC
DD
EE
FF
connect all 6, lowest total cost\text{connect all 6, lowest total cost}

A connected network with no loops is a tree, so on nn islands our cheapest network, the Minimum Spanning Tree, always uses n−1\bought{n - 1} cables:

Tree on n islands⟶n−1 cables\displaystyle \text{Tree on } n \text{ islands} \longrightarrow \bought{n - 1} \text{ cables}
6 islands⟶6−1\displaystyle \text{6 islands} \longrightarrow 6 - 1 =5 cables\displaystyle = \bought{5} \text{ cables}
2

Kruskal's Algorithm

To find which 5\bought{5} cables make up the Minimum Spanning Tree on our six-island map, the most direct approach is to scan the entire map from the cheapest cable to the most expensive, buying each cable unless it closes a loop with cables we already bought.

Kruskal's Algorithm
AA
BB
CC
DD
EE
FF
11
22
33
44
55
66
77
88
99
Text solution
Buy the three cheapest cablesScanning from lowest cost up, the first three cables connect islands that cannot reach each other yet, forming two separate groups {A,B,C}\{A, B, C\} and {D,E}\{D, E\}.
Buy D–E (1),buy A–B (2),buy A–C (3)\displaystyle \begin{aligned} &\text{Buy } \bought{D\text{--}E\ (1)}, \\ &\text{buy } \bought{A\text{--}B\ (2)}, \\ &\text{buy } \bought{A\text{--}C\ (3)} \end{aligned}
Skip loops and connect the groupsWe cross out B–C (4)\struck{B\text{--}C\ (4)} because BB and CC already connect through AA, then buy C–D (5)\bought{C\text{--}D\ (5)} to link the two groups into {A,B,C,D,E}\{A, B, C, D, E\}, and cross out B–D (6)\struck{B\text{--}D\ (6)} because BB can now reach DD through AA and CC.
Cross out B–C (4),buy C–D (5),cross out B–D (6)\displaystyle \begin{aligned} &\text{Cross out } \struck{B\text{--}C\ (4)}, \\ &\text{buy } \bought{C\text{--}D\ (5)}, \\ &\text{cross out } \struck{B\text{--}D\ (6)} \end{aligned}
Stop at n−1=5n - 1 = \bought{5} cablesBuying E–F (7)\bought{E\text{--}F\ (7)} connects the last island and gives our 5th cable, so we stop without checking C–E (8)C\text{--}E\ (8) or D–F (9)D\text{--}F\ (9).
Minimum total cost\displaystyle \text{Minimum total cost} =1+2+3+5+7\displaystyle = 1 + 2 + 3 + 5 + 7 =18\displaystyle = \bought{18}

So the Minimum Spanning Tree uses cables 1,2,3,5,\bought{1}, \bought{2}, \bought{3}, \bought{5}, and 7\bought{7} for a minimum total cost of 18\bought{18}.

3

Prim's Algorithm

Kruskal's algorithm jumps across the map, building separate pieces at D–E\bought{D\text{--}E} and A–B\bought{A\text{--}B} that only join up later. If we want to start at a single starting island and grow one connected network outward step by step, we only look at cables crossing the border of our current territory.

Prim's Algorithm
AA
BB
CC
DD
EE
FF
11
44
55
66
77
88
99
2\htmlClass{lc-pink}{2}
3\htmlClass{lc-pink}{3}
Text solution
Grow the territory from AAStarting at {A}\{A\}, we buy the cheapest cable leaving AA, then the cheapest cable crossing out of {A,B}\{A, B\} to reach CC.
{A}→buy A–B (2){A,B}→buy A–C (3){A,B,C}\displaystyle \begin{aligned} &\{A\} \\ &\xrightarrow{\text{buy } \bought{A\text{--}B\ (2)}} \{A, B\} \\ &\xrightarrow{\text{buy } \bought{A\text{--}C\ (3)}} \{A, B, C\} \end{aligned}
Cross out interior cables and expand to DD and EEBecause B–C (4)\struck{B\text{--}C\ (4)} now has both endpoints inside {A,B,C}\{A, B, C\}, we cross it out and take the cheapest border cable C–D (5)\bought{C\text{--}D\ (5)}, which puts B–D (6)\struck{B\text{--}D\ (6)} inside our territory and puts D–E (1)\candidate{D\text{--}E\ (1)} on the new border.
{A,B,C}→buy C–D (5)cross out B–C (4){A,B,C,D}→buy D–E (1)cross out B–D (6){A,B,C,D,E}\displaystyle \begin{aligned} &\{A, B, C\} \\ &\xrightarrow[\text{buy } \bought{C\text{--}D\ (5)}]{\text{cross out } \struck{B\text{--}C\ (4)}} \{A, B, C, D\} \\ &\xrightarrow[\text{buy } \bought{D\text{--}E\ (1)}]{\text{cross out } \struck{B\text{--}D\ (6)}} \{A, B, C, D, E\} \end{aligned}
Connect the final island FFIgnoring C–E (8)\struck{C\text{--}E\ (8)} inside our territory, we pick the cheaper border cable to FF and get the exact same five cables as Kruskal's algorithm.
Minimum total cost\displaystyle \text{Minimum total cost} =2+3+5+1+7\displaystyle = 2 + 3 + 5 + 1 + 7 =18\displaystyle = \bought{18}

Starting at a different island changes the order in which Prim's algorithm discovers and buys cables (for instance, starting at FF buys the 7\bought{7}-dollar cable first instead of last), but it always finds the exact same five cables and total cost of 18\bought{18}.

Starting at a different island changes the order you buy the cables, but always builds the exact same tree.
4

Why Greedy Works: The Cut Property

Both Kruskal's and Prim's algorithms are greedy: at every step, they pick the single cheapest available cable right now without looking ahead. On other problems, like finding the shortest path between two specific islands, blindly taking the cheapest next cable can lead you into an expensive detour later, so why is it guaranteed to work for a Minimum Spanning Tree?

Why Greedy Works: The Cut Property
AA
BB
CC
DD
EE
FF
11
22
33
44
55
66
77
88
99
Us\text{Us}
Them\text{Them}

For the split between Us={A,B,C}\text{Us} = \{A, B, C\} and Them={D,E,F}\text{Them} = \{D, E, F\}, three cables cross the dividing line:

Crossing cables: C–D (5)⏟cheapest across cut,B–D (6),C–E (8)\displaystyle \begin{aligned} &\text{Crossing cables: } \\ &\underbrace{\bought{C\text{--}D\ (5)}}_{\text{cheapest across cut}}, \quad \candidate{B\text{--}D\ (6)}, \quad \candidate{C\text{--}E\ (8)} \end{aligned}

Any tree that skips C–D (5)\bought{C\text{--}D\ (5)} must cross the line on B–D (6)\candidate{B\text{--}D\ (6)} or C–E (8)\candidate{C\text{--}E\ (8)}, and swapping that cable for C–D (5)\bought{C\text{--}D\ (5)} around the resulting loop saves money:

Swap B–D (6) or C–E (8) for C–D (5)⟶saves 6−5=1 or 8−5=3\displaystyle \begin{aligned} &\text{Swap } \struck{B\text{--}D\ (6)} \text{ or } \struck{C\text{--}E\ (8)} \text{ for } \bought{C\text{--}D\ (5)} \\ &\longrightarrow \text{saves } \struck{6} - \bought{5} = 1 \text{ or } \struck{8} - \bought{5} = 3 \end{aligned}
No matter how you split the islands into two groups, the cheapest cable across the dividing line belongs to the Minimum Spanning Tree, because any costlier crossing cable forms a loop where swapping saves money.
5

Ties and Multiple Minimum Spanning Trees

On our original map, Kruskal's and Prim's algorithms found the exact same tree because all nine cable costs were different, so there was never a tie across any border. Let's see what changes when we lower A–BA\text{--}B to 11 and B–CB\text{--}C to 33, creating a tie at 11 and a tie at 33.

Ties and Multiple Minimum Spanning Trees
AA
BB
CC
DD
EE
FF
11
11
33
33
55
66
77
88
99
Text solution
A tie that does not close a loop (11 and 11)Both D–E (1)\bought{D\text{--}E\ (1)} and A–B (1)\bought{A\text{--}B\ (1)} connect separate pairs of islands, so buying one does not block the other and we buy both in either order.
Buy D–E (1) and A–B (1)\displaystyle \text{Buy } \bought{D\text{--}E\ (1)} \text{ and } \bought{A\text{--}B\ (1)}
A tie that competes on the same loop (33 and 33)Both A–C (3)\candidate{A\text{--}C\ (3)} and B–C (3)\candidate{B\text{--}C\ (3)} connect CC to {A,B}\{A, B\}, so buying one immediately makes the other close the triangle A–B–CA\text{--}B\text{--}C, giving two valid Minimum Spanning Trees with the same minimum cost.
Choice 1 (buy A–C first):D–E (1), A–B (1), A–C (3), C–D (5), E–F (7)  ⟹  Cost=17Choice 2 (buy B–C first):D–E (1), A–B (1), B–C (3), C–D (5), E–F (7)  ⟹  Cost=17\displaystyle \begin{aligned} &\text{Choice 1 (buy } \candidate{A\text{--}C}\text{ first):} \\ &\quad \bought{D\text{--}E\ (1)},\ \bought{A\text{--}B\ (1)},\ \candidate{A\text{--}C\ (3)},\ \\ &\quad \bought{C\text{--}D\ (5)},\ \bought{E\text{--}F\ (7)} \\ &\quad \implies \text{Cost} = \bought{17} \\[8pt] &\text{Choice 2 (buy } \candidate{B\text{--}C}\text{ first):} \\ &\quad \bought{D\text{--}E\ (1)},\ \bought{A\text{--}B\ (1)},\ \candidate{B\text{--}C\ (3)},\ \\ &\quad \bought{C\text{--}D\ (5)},\ \bought{E\text{--}F\ (7)} \\ &\quad \implies \text{Cost} = \bought{17} \end{aligned}

When solving a problem by hand on paper, Kruskal's algorithm is usually the fastest to spot by eye because you can scan the whole map for 11s, then 22s, then 33s. When a graph is dense with many edges or you are programming it, Prim's algorithm avoids sorting every edge on the map by only checking cables across your local border.

Key concepts

To connect nn islands for the lowest total cost, we need a Minimum Spanning Tree of n−1\bought{n - 1} cables with no loops. Because any dividing line between two groups of islands must be crossed somewhere, the Cut Property guarantees that the cheapest cable crossing a border is always safe to buy:

Kruskal's algorithm (scan globally by cost, best by hand)

Sort or scan all cables from cheapest to most expensive, buying each cable that joins two separate groups, such as D–E (1)\bought{D\text{--}E\ (1)} then A–B (2)\bought{A\text{--}B\ (2)}, and crossing out any cable whose endpoints can already reach each other, like B–C (4)\struck{B\text{--}C\ (4)} closing the triangle on {A,B,C}\{A, B, C\}. Stop as soon as you have n−1\bought{n - 1} cables.

Prim's algorithm (grow locally from one island, best for dense graphs or code)

Pick any starting island as your territory and repeatedly buy the cheapest cable with one endpoint inside your territory and one outside, such as growing {A}→{A,B}→{A,B,C}\{A\} \to \{A, B\} \to \{A, B, C\} via A–B (2)\bought{A\text{--}B\ (2)} and A–C (3)\bought{A\text{--}C\ (3)}, while crossing out any cable whose endpoints are both inside your territory.

Uniqueness vs. ties

If all cable costs are distinct, there is only one Minimum Spanning Tree. If cables tie in cost, you get multiple Minimum Spanning Trees (all with the same minimum cost) only when the tied cables compete to close the same loop, like A–C (3)\candidate{A\text{--}C\ (3)} and B–C (3)\candidate{B\text{--}C\ (3)}.

When not to just pick the cheapest next cable

This greedy rule builds a network connecting all islands; do not use it to find the shortest path between two specific islands AA and FF, where a cheap 11-dollar cable can lead into a 100\struck{100}-dollar dead end and you must track the total distance from the start instead.

6

Practice

1

A communications company plans to connect 99 islands. There are 1414 possible underwater cable routes, all with different costs. (a) How many cables will be in the Minimum Spanning Tree, and how many of the 1414 routes will be left unused? (b) If an engineer builds a connected network of 9\theirs{9} cables on those 99 islands, explain why you can always delete at least one cable to save money while keeping every island connected.

How many edges does a tree on nn vertices always have, and what must happen if a connected network has more edges than a tree?

(a) A tree on n=9n = 9 islands always uses n−1=8n - 1 = \bought{8} cables (be careful not to use 99 cables for 99 islands, which would leave 14−9=514 - 9 = 5 unused). Subtracting from the 1414 available routes leaves 14−8=614 - \bought{8} = \struck{6} routes unused. (b) Because a tree on 99 islands uses only 8\bought{8} cables, a connected network with 9\theirs{9} cables cannot be a tree and must contain a loop. Erasing the most expensive cable in that loop breaks the loop, lowers the total cost, and leaves all 99 islands connected through the remaining path around the loop.
2

Five islands A,B,C,D,EA, B, C, D, E have eight possible cable routes with costs: A–B (1)A\text{--}B\ (1), D–E (2)D\text{--}E\ (2), B–C (3)B\text{--}C\ (3), A–C (4)A\text{--}C\ (4), C–D (5)C\text{--}D\ (5), B–E (6)B\text{--}E\ (6), C–E (7)C\text{--}E\ (7), and A–D (8)A\text{--}D\ (8). Run Kruskal's algorithm to find the Minimum Spanning Tree: list the cables bought in order, state which cable is crossed out before the tree is finished and why, and give the minimum total cost.

After buying the first three cables, trace which islands are already in the same group before deciding on A–C (4)A\text{--}C\ (4).

Scanning from cheapest to most expensive, buy A–B (1)\bought{A\text{--}B\ (1)}, buy D–E (2)\bought{D\text{--}E\ (2)}, and buy B–C (3)\bought{B\text{--}C\ (3)}. Next, check A–C (4)\struck{A\text{--}C\ (4)}: because AA and CC are already connected through BB, adding A–C (4)\struck{A\text{--}C\ (4)} would close the loop A–B–CA\text{--}B\text{--}C, so cross out A–C (4)\struck{A\text{--}C\ (4)} (do not just sum the four smallest numbers 1+2+3+4=101 + 2 + 3 + 4 = 10, which closes a triangle on {A,B,C}\{A, B, C\} and leaves {D,E}\{D, E\} disconnected). Next, buy C–D (5)\bought{C\text{--}D\ (5)} to join the two groups {A,B,C}\{A, B, C\} and {D,E}\{D, E\}. That gives 5−1=45 - 1 = \bought{4} cables, so we stop without checking 6,7,6, 7, or 88. The minimum total cost is 1+2+3+5=111 + 2 + 3 + 5 = \bought{11}.
3

Five islands P,Q,R,S,TP, Q, R, S, T have seven cable routes with costs: P–Q (4)P\text{--}Q\ (4), P–R (5)P\text{--}R\ (5), Q–R (6)Q\text{--}R\ (6), R–S (7)R\text{--}S\ (7), Q–S (8)Q\text{--}S\ (8), S–T (1)S\text{--}T\ (1), and R–T (9)R\text{--}T\ (9). Run Prim's algorithm starting at island PP: list the four cables in the exact order Prim buys them, state which cables get crossed out as the territory grows, and explain why the 11-dollar cable is bought last instead of first.

At each step, list only the cables that have one endpoint inside your current territory and one endpoint outside.

Starting at {P}\{P\}, the border cables are P–Q (4)\candidate{P\text{--}Q\ (4)} and P–R (5)\candidate{P\text{--}R\ (5)}, so buy P–Q (4)\bought{P\text{--}Q\ (4)} first, and from {P,Q}\{P, Q\} buy P–R (5)\bought{P\text{--}R\ (5)} second. Now our territory is {P,Q,R}\{P, Q, R\}, so both endpoints of Q–R (6)\struck{Q\text{--}R\ (6)} are inside our territory; cross out Q–R (6)\struck{Q\text{--}R\ (6)} and buy the cheapest border cable R–S (7)\bought{R\text{--}S\ (7)} (watch out for two common slips here: do not pick S–T (1)S\text{--}T\ (1) first as in Kruskal's algorithm, and do not buy Q–R (6)\struck{Q\text{--}R\ (6)} to get 4+5+6+1=164 + 5 + 6 + 1 = 16, which forms a loop inside {P,Q,R}\{P, Q, R\}). Next, cross out Q–S (8)\struck{Q\text{--}S\ (8)} since both QQ and SS are now inside {P,Q,R,S}\{P, Q, R, S\}. Finally, buy S–T (1)\bought{S\text{--}T\ (1)} and cross out R–T (9)\struck{R\text{--}T\ (9)}, since both RR and TT are now inside. Even though S–T (1)\bought{S\text{--}T\ (1)} is the cheapest cable on the map, Prim's algorithm only looks at cables crossing the border of its current territory, and that border does not reach SS or TT until the final step. The minimum total cost is 4+5+7+1=174 + 5 + 7 + 1 = \bought{17}.
4

On the six-island map with costs D–E (1)D\text{--}E\ (1), A–B (2)A\text{--}B\ (2), A–C (3)A\text{--}C\ (3), B–C (4)B\text{--}C\ (4), C–D (5)C\text{--}D\ (5), B–D (6)B\text{--}D\ (6), E–F (7)E\text{--}F\ (7), C–E (8)C\text{--}E\ (8), and D–F (9)D\text{--}F\ (9), draw a dividing line splitting the islands into Us={A,B,C,D}\text{Us} = \{A, B, C, D\} and Them={E,F}\text{Them} = \{E, F\}. (a) List every cable that crosses this dividing line and state which one the Cut Property guarantees is safe to buy. (b) Suppose someone proposes the spanning tree using A–B (2)\theirs{A\text{--}B\ (2)}, A–C (3)\theirs{A\text{--}C\ (3)}, C–D (5)\theirs{C\text{--}D\ (5)}, C–E (8)\theirs{C\text{--}E\ (8)}, and E–F (7)\theirs{E\text{--}F\ (7)}. If you add the cheapest crossing cable into their network, what loop does it create, which cable in their loop crosses back over the dividing line, and how much money do you save by swapping them?

A cable crosses the dividing line only if one of its endpoints is in {A,B,C,D}\{A, B, C, D\} and the other is in {E,F}\{E, F\}.

(a) The cables with one endpoint in {A,B,C,D}\{A, B, C, D\} and one in {E,F}\{E, F\} are D–E (1)\candidate{D\text{--}E\ (1)}, C–E (8)\candidate{C\text{--}E\ (8)}, and D–F (9)\candidate{D\text{--}F\ (9)} (do not include E–F (7)E\text{--}F\ (7), which has both endpoints inside "Them", or C–D (5)C\text{--}D\ (5), which has both endpoints inside "Us"). The Cut Property guarantees the cheapest crossing cable, D–E (1)\bought{D\text{--}E\ (1)}. (b) Adding D–E (1)\bought{D\text{--}E\ (1)} into their tree creates the three-island loop C–D–E–CC\text{--}D\text{--}E\text{--}C made of cables C–D (5)\theirs{C\text{--}D\ (5)}, C–E (8)\theirs{C\text{--}E\ (8)}, and D–E (1)\bought{D\text{--}E\ (1)}. On that loop, C–E (8)\struck{C\text{--}E\ (8)} is the cable that crosses back over the dividing line between {A,B,C,D}\{A, B, C, D\} and {E,F}\{E, F\}. Deleting C–E (8)\struck{C\text{--}E\ (8)} and keeping D–E (1)\bought{D\text{--}E\ (1)} lowers the total cost from 25\theirs{25} to 18\bought{18}, saving 8−1=7\struck{8} - \bought{1} = 7.
5

Five islands A,B,C,D,EA, B, C, D, E have six cable routes with two ties in cost: A–B (2)A\text{--}B\ (2), D–E (2)D\text{--}E\ (2), B–C (3)B\text{--}C\ (3), C–D (4)C\text{--}D\ (4), B–D (4)B\text{--}D\ (4), and A–E (6)A\text{--}E\ (6). Does having two pairs of tied weights give four different Minimum Spanning Trees? Find every Minimum Spanning Tree of this graph and its total cost.

Run Kruskal's algorithm on the two 22-dollar cables first, then check whether the two 44-dollar cables connect the same two groups.

No, there are only 22 Minimum Spanning Trees (do not assume two ties automatically multiply to 2×2=42 \times 2 = 4 trees). The first tie is between A–B (2)\bought{A\text{--}B\ (2)} and D–E (2)\bought{D\text{--}E\ (2)}; because those two cables share no islands and do not close a loop, both are bought in every Minimum Spanning Tree, followed by B–C (3)\bought{B\text{--}C\ (3)}. The second tie is between C–D (4)\candidate{C\text{--}D\ (4)} and B–D (4)\candidate{B\text{--}D\ (4)}, which both connect {A,B,C}\{A, B, C\} to {D,E}\{D, E\} and compete to close the loop B–C–DB\text{--}C\text{--}D. Breaking that tie gives 22 Minimum Spanning Trees, one using {A–B,D–E,B–C,C–D}\{\bought{A\text{--}B, D\text{--}E, B\text{--}C}, \candidate{C\text{--}D}\} and the other using {A–B,D–E,B–C,B–D}\{\bought{A\text{--}B, D\text{--}E, B\text{--}C}, \candidate{B\text{--}D}\}, both with minimum total cost 2+2+3+4=112 + 2 + 3 + 4 = \bought{11}.
6

On the six-island map with costs D–E (1)D\text{--}E\ (1), A–B (2)A\text{--}B\ (2), A–C (3)A\text{--}C\ (3), B–C (4)B\text{--}C\ (4), C–D (5)C\text{--}D\ (5), B–D (x)B\text{--}D\ (x), E–F (7)E\text{--}F\ (7), C–E (8)C\text{--}E\ (8), and D–F (9)D\text{--}F\ (9), suppose the cost xx of cable B–DB\text{--}D is still being negotiated. For what values of xx is B–DB\text{--}D guaranteed to be in every Minimum Spanning Tree, for what value of xx are there two different Minimum Spanning Trees, and for what values of xx is B–DB\text{--}D never used?

Find which cable across the cut between {A,B,C}\{A, B, C\} and {D,E,F}\{D, E, F\} competes with B–D (x)B\text{--}D\ (x) to close a loop.

Regardless of xx, cables D–E (1)\bought{D\text{--}E\ (1)}, A–B (2)\bought{A\text{--}B\ (2)}, and A–C (3)\bought{A\text{--}C\ (3)} connect {A,B,C}\{A, B, C\} and {D,E}\{D, E\}, while B–C (4)\struck{B\text{--}C\ (4)} is always crossed out because BB and CC already connect through AA for cost 2+32 + 3. To connect {A,B,C}\{A, B, C\} and {D,E,F}\{D, E, F\} across the cut, the two crossing cables that compete are B–D (x)\candidate{B\text{--}D\ (x)} and C–D (5)\candidate{C\text{--}D\ (5)}, which compete to close the four-island loop A–B–D–C–AA\text{--}B\text{--}D\text{--}C\text{--}A (be careful not to compare xx against B–C (4)\struck{B\text{--}C\ (4)} and require x<4x < 4, since B–C (4)\struck{B\text{--}C\ (4)} stays inside {A,B,C}\{A, B, C\} and does not cross the cut). Therefore, B–DB\text{--}D is in every Minimum Spanning Tree for x<5\bought{x < 5}, produces two different Minimum Spanning Trees when x=5\candidate{x = 5}, and is never used when x>5\struck{x > 5}.