Skip to content

Kruskal's cheapest links

networks

Level 2 of 3

Also called Kruskal's algorithm and Minimum spanning forest.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Kruskal's method runs on this map. What do the roads kept add up to?

4362715AABBCCEEFFGG
the answer, and why

the answer is B

C-G 1, B-F 2, A-E 3, A-B 4 and F-G 5 are kept, which is 15. E-F and B-C would close loops.

if you chose another

  • That adds every road on the map.
  • That stops after 4 roads, leaving C and G cut off from the rest.
  • That takes E-F 6 for F-G 5, but E-F closes a loop, and C and G stay cut off from the rest.

question 2

On this map, what do the roads kept add up to once 3 are kept?

5143267AABBCCEEFFGG
the answer, and why

the answer is A

A-E 1, B-C 2 and B-F 3 come first, and each joins two groups. That is 6.

if you chose another

  • That is 4 roads, with E-F's 4 added.
  • That is A-B's 5 alone, which comes later and is left out.
  • That takes A-B's 5 in place of B-C's 2.

question 3

Which is true of a line and of every pair of nn places, from 3 places on, every length 1?

the answer, and why

the answer is C

Each road kept makes one group fewer, from nn groups to 1, on either map.

this tests best, worst and average, on the learn page

if you chose another

  • That is how many roads it reads, not how many it keeps.
  • The sort reads every road, and every pair has more.
  • Both keep the same number.

question 4

The roads come in the order written. On which map does the sort make the most comparisons?

the answer, and why

the answer is C

Five roads in order: left runs 1 and 1, so 2; then 2, so 2; then 4, so 4. That is 8.

if you chose another

  • Three roads in order compare 3.
  • One road needs no comparison.
  • Four roads in order compare 4. Pieces change nothing in the sort.

pen and paper

question 5about 8 minutes

The map has roads A-B 7, A-E 1, E-F 6, B-F 2, B-C 4, F-G 3 and C-G 5.

  1. Write the roads in order, each kept or left out.
  2. Which is left out first, and why?
  3. What do the roads kept add up to?
a hint

Write each place's name as you go.

a worked answer

In order: A-E 1, B-F 2, F-G 3 and B-C 4 are kept. C-G 5 is left out, as C and G share a name. E-F 6 is kept. A-B 7 is left out. They add up to 1 + 2 + 3 + 4 + 6, so 16.

12346ABCEFG
The roads kept alone, 16. A-B and C-G are left out.

this tests how it works, on the learn page

what a full answer includes
  • A-E, B-F, F-G, B-C and E-F kept; C-G and A-B left out.
  • C-G: C and G share a name.
  • 16.

question 6about 8 minutes

The map has roads A-B 3, A-E 6, B-E 4, B-F 8 and E-F 5. Someone offers the network A-E, B-E and B-F.

  1. What does that network cost?
  2. Split A alone from the rest. Add the cheapest road across. Which road on the loop crosses too?
  3. Swap them. Now split F alone from the rest, add the cheapest road across, and swap again. What does the network cost now, and what do the roads Kruskal's method keeps add up to?
a hint

Draw the map first.

a worked answer

It costs 6 + 4 + 8, so 18. A-B 3 and A-E 6 cross, so A-B is cheapest. Adding it closes the loop A, B, E, A, and A-E crosses too. The swap gives 18 - 6 + 3, so 15. For F alone, E-F 5 and B-F 8 cross, so E-F is cheapest. Adding it closes the loop B, E, F, B, and B-F crosses too. The swap gives 15 - 8 + 5, so 12. The method keeps A-B 3, B-E 4 and E-F 5, also 12.

36485ABEF
The whole map. A, shaded, is split from the rest.
Question 6: why are the roads kept a cheapest network, one for each piece? Let ww be a network's cost and TT its cost after one swap.
  1. the move: what stays true

    the line:
    the roads kept so far liein some cheapest network\begin{gathered} \text{the roads kept so far lie} \\ \text{in some cheapest network} \end{gathered}

    why it is allowed: Take one piece, and only its roads. Two networks can tie for cheapest. At the start no road is kept, so there is nothing to check. The plan is to show that keeping the next road keeps this true.

  2. the move: split the places

    the line:
    a road kept: cheapestacross its split\begin{gathered} \text{a road kept: cheapest} \\ \text{across its split} \end{gathered}
    92213ABCEF
    A and E, shaded, are split from the rest. E to F, length 2, and A to B, length 9, cross.

    why it is allowed: Take the next road kept. Split the group at one of its ends from the rest of the piece. Its ends have different names, so it crosses. An earlier road was kept or left out, so its ends now share a name, and it does not cross. A later road is no shorter. So this road is cheapest across. On the small map, with A and E split off, it is E to F.

  3. the move: swap a road on the loop

    the line:
    T=w−9+2T = w - 9 + 2
    29132ABCEF
    The network of 15 with E to F added. The loop, heavy, crosses the split on E to F and on A to B.

    why it is allowed: Take a network with the roads kept so far, B to F and A to E, without E to F: those two, plus A to B and B to C. That costs 1 + 2 + 9 + 3, so ww is 15. Adding E to F closes a loop, which comes back across the split on A to B, length 9. Take A to B out.

  4. the move: add them up

    the line:
    T=w−7T = w - 7
    where does this come from?
    question 2 of the page greedy choices, and when they failSplit the places in two. Why does some cheapest network hold the cheapest road across? Let ww be a network's cost, and TT its cost after one swap.
    1. the move: swap a road on the loop

      the line:
      T=w−6+4T = w - 6 + 4
      36214ABCDE
      A network of 12, A to B, B to D, C to D and D to E, with A to C added. The loop A, C, D, B, A, heavy, crosses back on B to D, of 6.

      why it is allowed: The plan is to show a swap on a network that is not the cheapest, then use it on a cheapest one. Take a network without A to C, the cheapest road across. A to B, B to D, C to D and D to E cost 3 + 6 + 2 + 1, so here ww is 12. They already join A to C another way, so adding A to C closes a loop, roads leading round and back to A. The loop leaves the shaded group on A to C, so it comes back on another road across, B to D, of 6. Take B to D out.

    2. the move: add them up

      the line:
      T=w−2T = w - 2

      why it is allowed: Taking out 6 and putting in 4 is 2 less.

    3. the move: no dearer

      the line:
      T≤wT \le w
      3421ABCDE
      After the swap, B to D out and A to C in, at 12 less 2, which is 10.

      why it is allowed: ww less 2 is at most ww, so the swap costs no more. Before, 12; after, 10.

    4. the move: still one piece

      the line:
      every placestill joined\begin{gathered} \text{every place} \\ \text{still joined} \end{gathered}

      why it is allowed: A trip that used the road taken out goes the other way round the loop. From B to D, that is B, A, C, D.

    5. the move: on any map

      the line:
      cost after:w−f+e\begin{gathered} \text{cost after:} \\ w - f + e \end{gathered}

      why it is allowed: On any map, let ee be the cheapest road across, and ff a road where the loop crosses back. Each letter also stands for its road's length. Here ee is A to C, of 4, and ff is B to D, of 6. The swap takes ff out and puts ee in.

    6. the move: f costs at least e

      the line:
      w−f+eis at most w\begin{gathered} w - f + e \\ \text{is at most } w \end{gathered}

      why it is allowed: ff crosses too, and ee is the cheapest road across, so ff is at least ee. Taking ff out saves at least what putting ee in adds.

    7. the move: start from a cheapest one

      the line:
      a cheapest networkswaps to one with e\begin{gathered} \text{a cheapest network} \\ \text{swaps to one with } e \end{gathered}

      why it is allowed: The network of 12 only showed the swap. Start instead from a cheapest network without ee. The swap costs no more, and no network costs less than a cheapest one, so it costs the same. So it is cheapest too, and holds ee. A cheapest network that holds ee already needs no swap.

    the whole page: greedy choices, and when they fail

    why it is allowed: Taking out 9 and putting in 2 is 7 less. The proof of the swap on any map is under this line.

  5. the move: no dearer

    the line:
    T≤wT \le w

    why it is allowed: The road taken out crosses too, so it costs at least the cheapest across. Before, 15; after, 8.

  6. the move: roads kept stay

    the line:
    no road keptis taken out\begin{gathered} \text{no road kept} \\ \text{is taken out} \end{gathered}

    why it is allowed: The road taken out crosses the split. A road kept has both ends in one group, so it does not cross.

  7. the move: so it stays true

    the line:
    roads kept and the nextlie in a cheapest network\begin{gathered} \text{roads kept and the next} \\ \text{lie in a cheapest network} \end{gathered}

    why it is allowed: The network of 15 only showed the swap. Start from a cheapest network holding the roads kept so far, by line 1. If it holds the next road too, nothing changes. If not, the swap leaves a network and costs no more, and no network costs less, so it is cheapest too. It holds the next road, and by line 6 it keeps the roads kept.

  8. the move: as many roads

    the line:
    a piece of p places:p−1 roads kept\begin{gathered} \text{a piece of } p \text{ places:} \\ p - 1 \text{ roads kept} \end{gathered}
    road keptgroups left
    start5
    B-F4
    A-E3
    E-F2
    B-C1
    The small map, 5 places and 4 roads kept.

    why it is allowed: Each road kept makes one group fewer. After its turn a road's ends share a name, kept or left out. A piece is joined, so two or more groups left in it would have a road between two of them. So a piece of pp places goes from pp groups to 1.

  9. the move: a cheapest network has them too

    the line:
    a cheapest network:p−1 roads\begin{gathered} \text{a cheapest network:} \\ p - 1 \text{ roads} \end{gathered}

    why it is allowed: Take any road off a loop. A trip that used it goes the other way round, so every place stays joined. Every length here is at least 1, so the cost falls. So it has no loop. Travel out from any place. Each other place is first reached along one road, p−1p - 1 in all, and any other road would close a loop.

  10. the move: at the end

    the line:
    the roads kept are acheapest network\begin{gathered} \text{the roads kept are a} \\ \text{cheapest network} \end{gathered}
    13212ABCDEFH
    The roads kept on two parts of town, the watch page's fifth map, cost 6 and 3, which is 9, with 2 names at the end.

    why it is allowed: By line 7, after every road kept they lie in a cheapest network of the piece, and by lines 8 and 9 both have p−1p - 1 roads. So they are all of it, one for each piece.

  11. the move: ties change nothing

    the line:
    ties broken any way:the same total\begin{gathered} \text{ties broken any way:} \\ \text{the same total} \end{gathered}

    why it is allowed: Nothing above used letter order, and all cheapest networks cost the same. On the watch page's third map, every road 5, ties broken backwards keep other roads, and still cost 35.

try it with a small ww: at w=15w = 15, line 3 gives T=8T = 8, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 18.
  • A-B, of 3; A-E crosses too.
  • 15, then E-F in and B-F out: 12, and the method's 12.