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,F using underwater cables so that every island can reach every other island for the lowest total cost.
Why the Cheapest Network Is a Tree
A connected network with no loops is a tree, so on n islands our cheapest network, the Minimum Spanning Tree, always uses n−1 cables:
Tree on n islands⟶n−1 cables
6 islands⟶6−1=5 cables
2
Kruskal's Algorithm
To find which 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
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} and {D,E}.
Buy D–E(1),buy A–B(2),buy A–C(3)
Skip loops and connect the groupsWe cross out B–C(4) because B and C already connect through A, then buy C–D(5) to link the two groups into {A,B,C,D,E}, and cross out B–D(6) because B can now reach D through A and C.
Cross out B–C(4),buy C–D(5),cross out B–D(6)
Stop at n−1=5 cablesBuying E–F(7) connects the last island and gives our 5th cable, so we stop without checking C–E(8) or D–F(9).
Minimum total cost=1+2+3+5+7=18
So the Minimum Spanning Tree uses cables 1,2,3,5, and 7 for a minimum total cost of 18.
3
Prim's Algorithm
Kruskal's algorithm jumps across the map, building separate pieces at D–E and A–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
Text solution
Grow the territory from AStarting at {A}, we buy the cheapest cable leaving A, then the cheapest cable crossing out of {A,B} to reach C.
{A}buy A–B(2){A,B}buy A–C(3){A,B,C}
Cross out interior cables and expand to D and EBecause B–C(4) now has both endpoints inside {A,B,C}, we cross it out and take the cheapest border cable C–D(5), which puts B–D(6) inside our territory and puts D–E(1) on the new border.
{A,B,C}cross out B–C(4)buy C–D(5){A,B,C,D}cross out B–D(6)buy D–E(1){A,B,C,D,E}
Connect the final island FIgnoring C–E(8) inside our territory, we pick the cheaper border cable to F and get the exact same five cables as Kruskal's algorithm.
Minimum total cost=2+3+5+1+7=18
Starting at a different island changes the order in which Prim's algorithm discovers and buys cables (for instance, starting at F buys the 7-dollar cable first instead of last), but it always finds the exact same five cables and total cost of 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
For the split between Us={A,B,C} and Them={D,E,F}, three cables cross the dividing line:
Crossing cables: cheapest across cutC–D(5),B–D(6),C–E(8)
Any tree that skips C–D(5) must cross the line on B–D(6) or C–E(8), and swapping that cable for C–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
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–B to 1 and B–C to 3, creating a tie at 1 and a tie at 3.
Ties and Multiple Minimum Spanning Trees
Text solution
A tie that does not close a loop (1 and 1)Both D–E(1) and A–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)
A tie that competes on the same loop (3 and 3)Both A–C(3) and B–C(3) connect C to {A,B}, so buying one immediately makes the other close the triangle A–B–C, giving two valid Minimum Spanning Trees with the same minimum cost.
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 1s, then 2s, then 3s. 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 n islands for the lowest total cost, we need a Minimum Spanning Tree of 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) then A–B(2), and crossing out any cable whose endpoints can already reach each other, like B–C(4) closing the triangle on {A,B,C}. Stop as soon as you have 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} via A–B(2) and A–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) and B–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 A and F, where a cheap 1-dollar cable can lead into a 100-dollar dead end and you must track the total distance from the start instead.
6
Practice
1
A communications company plans to connect 9 islands. There are 14 possible underwater cable routes, all with different costs. (a) How many cables will be in the Minimum Spanning Tree, and how many of the 14 routes will be left unused? (b) If an engineer builds a connected network of 9 cables on those 9 islands, explain why you can always delete at least one cable to save money while keeping every island connected.
Be careful not to use 9 cables for 9 islands, which would leave 14−9=5 unused.
How many edges does a tree on n vertices always have, and what must happen if a connected network has more edges than a tree?
(a) A tree on n=9 islands always uses n−1=8 cables (be careful not to use 9 cables for 9 islands, which would leave 14−9=5 unused). Subtracting from the 14 available routes leaves 14−8=6 routes unused. (b) Because a tree on 9 islands uses only 8 cables, a connected network with 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 9 islands connected through the remaining path around the loop.
2
Five islands A,B,C,D,E have eight possible cable routes with costs: A–B(1),D–E(2),B–C(3),A–C(4),C–D(5),B–E(6),C–E(7), and A–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).
Scanning from cheapest to most expensive, buy A–B(1), buy D–E(2), and buy B–C(3). Next, check A–C(4): because A and C are already connected through B, adding A–C(4) would close the loop A–B–C, so cross out A–C(4) (do not just sum the four smallest numbers 1+2+3+4=10, which closes a triangle on {A,B,C} and leaves {D,E} disconnected). Next, buy C–D(5) to join the two groups {A,B,C} and {D,E}. That gives 5−1=4 cables, so we stop without checking 6,7, or 8. The minimum total cost is 1+2+3+5=11.
3
Five islands P,Q,R,S,T have seven cable routes with costs: P–Q(4),P–R(5),Q–R(6),R–S(7),Q–S(8),S–T(1), and R–T(9). Run Prim's algorithm starting at island P: 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 1-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}, the border cables are P–Q(4) and P–R(5), so buy P–Q(4) first, and from {P,Q} buy P–R(5) second. Now our territory is {P,Q,R}, so both endpoints of Q–R(6) are inside our territory; cross out Q–R(6) and buy the cheapest border cable R–S(7) (watch out for two common slips here: do not pick S–T(1) first as in Kruskal's algorithm, and do not buy Q–R(6) to get 4+5+6+1=16, which forms a loop inside {P,Q,R}). Next, cross out Q–S(8) since both Q and S are now inside {P,Q,R,S}. Finally, buy S–T(1) and cross out R–T(9), since both R and T are now inside. Even though S–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 S or T until the final step. The minimum total cost is 4+5+7+1=17.
4
On the six-island map with costs D–E(1),A–B(2),A–C(3),B–C(4),C–D(5),B–D(6),E–F(7),C–E(8), and D–F(9), draw a dividing line splitting the islands into Us={A,B,C,D} and 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),A–C(3),C–D(5),C–E(8), and E–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} and the other is in {E,F}.
(a) The cables with one endpoint in {A,B,C,D} and one in {E,F} are D–E(1),C–E(8), and D–F(9) (do not include E–F(7), which has both endpoints inside "Them", or C–D(5), which has both endpoints inside "Us"). The Cut Property guarantees the cheapest crossing cable, D–E(1). (b) Adding D–E(1) into their tree creates the three-island loop C–D–E–C made of cables C–D(5),C–E(8), and D–E(1). On that loop, C–E(8) is the cable that crosses back over the dividing line between {A,B,C,D} and {E,F}. Deleting C–E(8) and keeping D–E(1) lowers the total cost from 25 to 18, saving 8−1=7.
5
Five islands A,B,C,D,E have six cable routes with two ties in cost: A–B(2),D–E(2),B–C(3),C–D(4),B–D(4), and A–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.
Do not assume two ties automatically multiply to 2×2=4 trees.
Run Kruskal's algorithm on the two 2-dollar cables first, then check whether the two 4-dollar cables connect the same two groups.
No, there are only 2 Minimum Spanning Trees (do not assume two ties automatically multiply to 2×2=4 trees). The first tie is between A–B(2) and D–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). The second tie is between C–D(4) and B–D(4), which both connect {A,B,C} to {D,E} and compete to close the loop B–C–D. Breaking that tie gives 2 Minimum Spanning Trees, one using {A–B,D–E,B–C,C–D} and the other using {A–B,D–E,B–C,B–D}, both with minimum total cost 2+2+3+4=11.
6
On the six-island map with costs D–E(1),A–B(2),A–C(3),B–C(4),C–D(5),B–D(x),E–F(7),C–E(8), and D–F(9), suppose the cost x of cable B–D is still being negotiated. For what values of x is B–D guaranteed to be in every Minimum Spanning Tree, for what value of x are there two different Minimum Spanning Trees, and for what values of x is B–D never used?
Be careful not to compare x against B–C(4) and require x<4, since B–C(4) stays inside {A,B,C} and does not cross the cut.
Find which cable across the cut between {A,B,C} and {D,E,F} competes with B–D(x) to close a loop.
Regardless of x, cables D–E(1),A–B(2), and A–C(3) connect {A,B,C} and {D,E}, while B–C(4) is always crossed out because B and C already connect through A for cost 2+3. To connect {A,B,C} and {D,E,F} across the cut, the two crossing cables that compete are B–D(x) and C–D(5), which compete to close the four-island loop A–B–D–C–A (be careful not to compare x against B–C(4) and require x<4, since B–C(4) stays inside {A,B,C} and does not cross the cut). Therefore, B–D is in every Minimum Spanning Tree for x<5, produces two different Minimum Spanning Trees when x=5, and is never used when x>5.