Skip to content

join every place cheaply

Join every place using roads whose lengths add up to as little as can be. Race two methods on the same map.

The race below runs on a clock. Each method has a lane, named kruskal's cheapest links or prim's cheapest links. At each tick, each lane still running makes one more of whatever you choose under 'one tick is'. In the box, A-B 4 is a road from A to B, 4 long. The letter after the semicolon is where Prim's method starts. Kruskal's method needs no start.

On a map in one piece, every place can reach every other by road. There both methods end on a cheapest network: on the map below, lengths adding up to 18. Each method's own page shows why each is cheapest. Two cheapest networks cost the same, so their totals match.

A look checks one road. Kruskal's method takes each road in turn. Prim's method looks along a road only from the end that joins first, never back. So on a map in one piece each makes one look for each road, and their looks always tie: 12 each on the map below. To see the tie, press 'a look along a road'.

What differs most is the work of choosing the next road, so the race starts with one tick set to one weighing. For Kruskal's method, a weighing is comparing two roads in its sort. Its own page says 'a look at two roads' for this; here that is a weighing, not a look. For Prim's method, a weighing is checking one place waiting to join, even one waiting alone. Fewest weighings wins.

First Kruskal's method sorts every road by length, ties in letter order, with merge sort. Sorted runs of 1, then 2, then 4 and on are each merged with a neighbour. A last run with no partner stays as it is that round. A merge compares the two runs' first roads, and the shorter goes next, until one run is empty. That sort is all its weighing. So when one tick is a weighing, its lane lights the two roads being compared, through the whole sort, then jumps to its finished map. Each place starts as a group of its own, showing its own letter. In order, the method makes a look at each road. A road whose two ends show one letter would close a loop, so it is left out. A road kept joins two groups. The smaller takes the other's letter; if they are equal, the group of the road's second letter does. That road turns blue at once, and the places only at the end: press 'a look along a road' to watch.

426518242373ABCDEFGH
In the order the box lists them, its sort of 12 roads takes 4 rounds of merging: 6 + 8 + 7 + 11 = 32 comparisons, so 32 weighings. It keeps 1, 2, 2, 2, 3, 3 and 5: 18 in all, drawn heavy. It leaves out 4, 4, 6, 7 and 8. The first 4, A to B, would close a loop, as A, B, E and F all show A by then.

Prim's method starts at one place and grows one tree: roads with no loop. It writes 0 at the start, which waits alone. After that, a place waits once a length is written at it, until it joins. Each round the method weighs up every place waiting, and joins the one with the smallest number, ties in letter order. Then it makes a look along each road from that place to a place not joined yet. It writes that road's length there if none is written or this road is cheaper.

426518242373ABCDEFGH
It starts from A, shaded. Each list after the colon is one round's places waiting, with their numbers, and the smallest joins: A 0. B 4, E 2, F 6. B 4, F 3. B 1, G 7. C 5, G 7. D 8, G 2. D 4, H 3. D 2. Count them: 1 + 3 + 2 + 2 + 2 + 2 + 2 + 1 = 15 weighings. The roads joined, 2, 3, 1, 5, 2, 3 and 2, make 18 in all, the same roads, drawn heavy.

or make one

race these, up to four

one tick is

Without scripts the race cannot run, so each lane shows where its racer ends.

Tick 32: no lane has made more than 32 weighings. Kruskal's cheapest links and prim's cheapest links have finished.

kruskal's cheapest links

done in 32 weighings
426531728324AABACADAEAFAGAHA

prim's cheapest links

done in 15 weighings
426531728324A0B1C5D2E2F3G2H3

what the marks mean

  • a place or road being looked at or compared
  • a place given a new number, and the road that gave it
  • a place whose number is final, and the road to it
  • the place a lane starts from
  • a road passed by
  • any other place or road
  • how much of its race a lane has run

which suits this map

On this map of 8 places, prim's cheapest links made the fewest weighings: 15, against 32 for kruskal's cheapest links.

What each lane kept: kruskal's cheapest links 7 roads totalling 18 and prim's cheapest links 7 roads totalling 18.

each racer's own page

The same racers on one line of two-way roads, each 10 long, in letter order: weighings up, how many places across, both scales in powers of ten. Each point is counted on that line.
102104101102103104105106107101102103104101102103104105106107
  • kruskal's cheapest links
  • prim's cheapest links

the starting racers, then on one line

From one end of a line of 1,000 places, only the next place waits each round, so Prim's method weighs up each place once: 1,000. Kruskal's method sorts its 999 roads by merge sort. With the roads already in order, the left run's roads all go first, one comparison each, and then the merge stops. Round by round that is 499 x 1 + 250 x 2 + 125 x 4 + 62 x 8 + 31 x 16. Then 16 x 32, 8 x 64, 4 x 128, 2 x 256 and 1 x 512, each 512. In all, 5,051. Looks tie on the starting map, 12 each, and on the line, 999 each. Neither count is all the work. Both methods also make those looks, and Kruskal's method gives places new letters as groups join. The 7 roads kept give 1+1+1+1+2+2+4=121 + 1 + 1 + 1 + 2 + 2 + 4 = 12 new letters on the starting map.

First, their weighings on the starting map. Then on one line of 1,000 places, each road 10 long, listed in order from the first place to the last: the same map for both. Prim's method starts at the first place. The fewest in each row is in bold.

kruskal's cheapest linksprim's cheapest links
the starting mapA-B 4, A-E 2, A-F 6, B-C 5, E-F 3, B-F 1, F-G 7, C-G 2, C-D 8, G-H 3, D-H 2, D-G 4; A3215
a line of 1,000 places5,0511,000

the starting racers on a map of 8 places in each shape

At 8 places, by weighings, Prim's method wins on all five shapes in the last table, by only 1 on 'a shorter way round'. Of 100 maps of 8 places made by pressing 'a town at random', Prim's method wins 91, Kruskal's method wins 6 and 3 tie. With fewer places Kruskal's method does better: at 3 places it wins all five shapes and all 100 maps. On a star Kruskal's method wins: at 6 places in the first picture below, and in the table at 1,000 places. Prim's count grows with how many places wait at once, and Kruskal's with how many roads it sorts, as that table shows. This counts Prim's method looking over every place waiting, as its lane and its own page do. That page's question 5 instead keeps the lengths written in a heap, the smallest always on top, so it need not look over them all. Two roads of equal length can change what is kept, never the total. On 'a shorter way round', Prim's method reaches C from D first, so it keeps C to D. Kruskal's method takes equal roads in letter order, so it keeps B to C. Each is 90, and both total 90 + 90 + 5 x 10 = 230. Use Prim's method when a map has many roads. With few roads, from 8 places up, the shape decides. Few places waiting at once, as on a line from one end, suit Prim's method. Many, as on a star, suit Kruskal's method. Use Kruskal's method when a map may be in pieces.

1010101010ABCEFG
A star is one middle place with a road to every other. Try it in the box above: A-F 10, B-F 10, C-F 10, E-F 10, F-G 10; F (Prim's method starts at the middle). F waits alone. Joining F writes a length at the 5 others, then each round one fewer waits: 1 + 5 + 4 + 3 + 2 + 1 = 16 weighings. Kruskal's method sorts 5 roads in 2 + 2 + 4 = 8 comparisons, and wins.
312512ABCDEFH
Two pieces. Try them in the box above: A-B 3, A-E 1, B-F 2, E-F 5, C-D 1, D-H 2; E (Prim's method starts at E). Prim's method joins only E's piece, shaded: 3 roads totalling 1 + 3 + 2 = 6, in 1 + 2 + 2 + 1 = 6 weighings. Kruskal's method keeps a network for each piece: 5 roads totalling 6 + 1 + 2 = 9, in 3 + 3 + 4 = 10 weighings. Prim's method makes the fewest weighings but joins only one piece, so read 'What each lane kept', under 'which suits this map'.
at 1,000 placesroadsKruskal's method comparesPrim's method weighs up
a line, from one end9995,0511,000
a star, from its middle9995,051499,501
every pair joined499,5004,787,336499,501
A star has as few roads as a line, and from its middle Prim's method weighs up far more. The middle waits alone, then 999 wait, then 998, down to 1. Write 999 + 998 + ... + 1 twice, once forwards and once backwards underneath: 999 columns, each adding to 1,000. That is twice the sum, so the sum is 1,000 x 999 / 2 = 499,500. With the middle's 1, that is 499,501. With every pair joined the same places wait, so Prim's count stays the same. Each of the 1,000 places has 999 roads, and that counts each road twice: 1,000 x 999 / 2 = 499,500 roads for Kruskal's method to sort. On all three maps every road is one length, listed in letter order, so each merge compares every road of its left run. Listed in another order, the count can differ. Kruskal's counts are from running that sort.

Their weighings on the map the race makes in each shape.

kruskal's cheapest linksprim's cheapest links
a town at randomA-B 14, A-E 17, C-D 33, C-G 22, C-H 16, D-H 18, E-F 25, F-G 12, G-H 15; A1911
one long lineA-B 10, B-C 10, C-D 10, D-H 10, G-H 10, F-G 10, E-F 10; A108
two rows and rungsA-B 10, B-C 10, C-D 10, E-F 10, F-G 10, G-H 10, A-E 10, B-F 10, C-G 10, D-H 10; A2220
every road the grid allowsA-B 10, B-C 10, C-D 10, E-F 10, F-G 10, G-H 10, A-E 10, B-F 10, C-G 10, D-H 10, A-F 15, B-G 15, D-G 15; A3222
a shorter way roundA-B 90, B-C 90, C-D 90, A-E 10, E-F 10, F-G 10, G-H 10, D-H 10; A1514