Skip to content

Prim's cheapest links

networks

Level 2 of 3

Also called Prim's algorithm and Minimum spanning tree.

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

Prim's method runs from A on this map. What do its links add up to?

5241632A0BCEFG
the answer, and why

the answer is D

E by 2, F by 4, B by 1, G by 3 and C by 2, which is 12.

if you chose another

  • That keeps A-B 5 in place of E-F 4.
  • That leaves out E-F, cutting off A and E.
  • That adds every road on the map.

question 2

On this map, from A, what does the tree add up to once it has 3 roads?

61231542A0BCEFGH
the answer, and why

the answer is C

E by 1, F by 2, then B by 3, as F-B beats A-B's 6. That is 6.

if you chose another

  • That is 4 roads, with B-C's 1 added.
  • That takes B-C's 1 for F-B's 3, but C has no road written until B joins.
  • That keeps A-B's 6 in place of F-B's 3.

question 3

Which is true of a scan on a star of nn places, from its middle?

the answer, and why

the answer is D

Joining the middle writes a road at all the others. Then the waiting falls by one each round, as with every pair.

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

if you chose another

  • The star writes all its roads in round 1.
  • A star has only n−1n - 1 roads.
  • No run weighs up more, by section 4's question 3.

question 4

From the place after the semicolon, on which map does Prim's weigh up the most places?

the answer, and why

the answer is C

Joining A writes a road at all 3 others, so 1, 3, 2 and 1, which is 7.

if you chose another

  • From B, B waits alone, then A alone, then E and F, then F, so 1, 1, 2 and 1, which is 5.
  • A line of 4 weighs up one place each round, 4.
  • C, D and H are another piece, so the run ends after A and B, 2.

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. The method runs from A.

  1. Write each place in the order joined, with its road.
  2. Which number is later beaten?
  3. What do the links add up to?
a hint

Draw the map first.

a worked answer

In the order joined: A, E by A-E 1, F by E-F 6, B by F-B 2, G by F-G 3, C by B-C 4. B's 7 becomes 2. C-G's 5 does not beat C's 4, so it changes nothing. They add up to 1 + 6 + 2 + 3 + 4, so 16.

16234ABCEFG
The cheapest links 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, F, B, G, C, each with its road.
  • B's 7 becomes 2.
  • 16.

question 6about 8 minutes

The map has roads A-B 6, A-E 1, B-E 2, B-C 4 and C-E 5. Someone offers the network A-B, A-E and B-C.

  1. What does that network cost?
  2. Split A and E from the rest. Add the cheapest road across. Which road on the loop crosses too?
  3. Swap them. What does it cost now, and what does the method give from A?
a hint

Draw the map first.

a worked answer

It costs 6 + 1 + 4, so 11. A-B 6, B-E 2 and C-E 5 cross, so B-E is cheapest. Adding it closes the loop A, B, E, A, and A-B crosses too. The swap gives 11 - 6 + 2, so 7. From A the method joins E by 1, B by 2 and C by 4, also 7.

61245ABCE
The whole map. A and E, shaded, are split from the rest.
Question 7: on a map in one piece, why are the roads joined a cheapest network? Let ww be a network's cost, not question 3's WW, and TT its cost after one swap.
  1. the move: what stays true

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

    why it is allowed: Before round 2, no road is joined, so none can lie outside a cheapest network. The plan: show that joining the next road keeps this true.

  2. the move: split the places

    the line:
    the next road: cheapestacross the split\begin{gathered} \text{the next road: cheapest} \\ \text{across the split} \end{gathered}

    why it is allowed: Split the places into those in the tree and the rest. A road across has one end in each. Each place waiting holds its cheapest road from the tree, as a cheaper one always replaces what is written. So the next road joined, the cheapest written, is the cheapest across. On the small map, with A and E in the tree, B holds 9 and F holds 2.

  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 A to E but without E to F: A to E, A to B, B to F and B to C. That costs 2 + 9 + 1 + 3, so ww is 15. Adding E to F closes a loop, which comes back across the split on A to B, of 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 joined stay

    the line:
    no road joinedis taken out\begin{gathered} \text{no road joined} \\ \text{is taken out} \end{gathered}

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

  7. the move: so it stays true

    the line:
    roads joined and the nextlie in a cheapest network\begin{gathered} \text{roads joined and the next} \\ \text{lie in a cheapest network} \end{gathered}
    111ABE
    All lengths 1, from A. A cheapest network, heavy, costs 2, and lacks A to B, the next road joined. Put A to B in and take A to E out: still 2.

    why it is allowed: The network of 15 only showed the swap. Start instead from a cheapest network holding the roads joined 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 joined.

  8. the move: at the end

    the line:
    the links are acheapest network\begin{gathered} \text{the links are a} \\ \text{cheapest network} \end{gathered}

    why it is allowed: On a map in one piece, all nn places have joined, with n−1n - 1 roads, by question 6. They lie in a cheapest network, which also has n−1n - 1 roads. So they are all of it.

  9. the move: any start

    the line:
    any start givesthe same total\begin{gathered} \text{any start gives} \\ \text{the same total} \end{gathered}

    why it is allowed: Nothing above used which place came first, and all cheapest networks cost the same. On the town of eight, every start gives 18.

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
  • 11.
  • B-E, of 2; A-B crosses too.
  • 7, and 7.