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.
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.
or make one
race these, up to four
Choose two to four racers.
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.
No tick the race offers is counted by every one of these racers, so one clock cannot time them. Choose others.
kruskal's cheapest links
done in 32 weighingsprim's cheapest links
done in 15 weighingswhat 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
- 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 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 links | prim'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; A | 32 | 15 |
| a line of 1,000 places | 5,051 | 1,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.
| at 1,000 places | roads | Kruskal's method compares | Prim's method weighs up |
|---|---|---|---|
| a line, from one end | 999 | 5,051 | 1,000 |
| a star, from its middle | 999 | 5,051 | 499,501 |
| every pair joined | 499,500 | 4,787,336 | 499,501 |
Their weighings on the map the race makes in each shape.
| kruskal's cheapest links | prim'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; A | 19 | 11 |
| one long lineA-B 10, B-C 10, C-D 10, D-H 10, G-H 10, F-G 10, E-F 10; A | 10 | 8 |
| 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; A | 22 | 20 |
| 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; A | 32 | 22 |
| 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; A | 15 | 14 |