Kruskal's cheapest links
how it works
The idea on a small map, then the whole method on the town of eight.
Kruskal's method joins every place it can with the least road in all, as when wiring a town, and has no starting place. Each place starts as a group of its own, named by its letter. Put the roads in order, shortest first, two equal in letter order. Then take each in turn. If its ends have different names, keep it, and the smaller group takes the other's name. If the two groups are the same size, the group of the road's second letter does. If its ends share a name, they are joined already, so the road would close a loop: leave it out. A map in pieces ends with a name for each piece. Putting the roads in order is the sort, here merge sort. This page counts its comparisons, each a look at two roads to see which goes first. The town of eight makes 32, against 12 new names and 24 names read, 2 for each of the 12 roads. The code below, in the language Python, keeps the same names, and leaves the sort to Python's own sorted.
| road | its ends' names | does | names of A, B, C, E, F |
|---|---|---|---|
| start | A B C E F | ||
| B-F 1 | B, F | keep | A B C E B |
| A-E 2 | A, E | keep | A B C A B |
| E-F 2 | A, B | keep | A A C A A |
| B-C 3 | A, C | keep | A A A A A |
| A-B 9 | A, A | leave out | A A A A A |
step 1 of 15
Every one of the 8 places starts as a group of its own, named by its letter.
what it remembers to know where it is
- road taken
- not yet
- road kept
- 0
Where it says not yet, nothing is held there at that moment.
step 2 of 15
Put the 12 roads in order from the shortest up, ties in letter order: that takes 32 comparisons. Now take each in turn.
what it remembers to know where it is
- road taken
- not yet
- road kept
- 0
Where it says not yet, nothing is held there at that moment.
step 3 of 15
The road from B to F is 1 long and joins two groups, so keep it. F takes the name B, since that group is at least as big.
what it remembers to know where it is
- road taken
- B-F
- road kept
- 1
step 7 of 15
The road from E to F is 3 long and joins two groups, so keep it. B and F take the name A, since that group is at least as big.
what it remembers to know where it is
- road taken
- E-F
- road kept
- 10
step 9 of 15
The road from A to B is 4 long, but both are in the group named A already, so it would close a loop. Leave it out.
what it remembers to know where it is
- road taken
- A-B
- road kept
- 13
step 11 of 15
The road from B to C is 5 long and joins two groups, so keep it. C, D, G and H take the name A, since that group is at least as big.
what it remembers to know where it is
- road taken
- B-C
- road kept
- 18
step 14 of 15
The road from C to D is 8 long, but both are in the group named A already, so it would close a loop. Leave it out.
what it remembers to know where it is
- road taken
- C-D
- road kept
- 18
step 15 of 15
Done: every road has been taken. The 7 roads kept are 18 long in all, and the places make one group.
what it remembers to know where it is
- road taken
- not yet
- road kept
- 18
Where it says not yet, nothing is held there at that moment.
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- every place starts as a group of its own, named by its letterPython lines 12, 13, 14, 15, 16 and 17
- put the roads in order from the shortest up, ties in letter order, and take each in turnPython lines 20 and 21
- a road between two groups: keep it; the smaller group, or on a tie the second place's, takes the other's namePython lines 25, 26, 27, 28, 29 and 30
- a road inside one group would close a loop: leave it outPython lines 22, 23 and 24
- the roads kept are the cheapest that join every place that can be joinedPython lines 31 and 36
the same in Python
The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.
def kruskal(roads):
"""Find the cheapest links that join every place that can be joined.
roads is a list of (place, place, length), each road running both
ways. Returns each place's name at the end: places with one name are
joined, so a map in pieces ends with a name for each piece.
"""
return take_in_order(roads)[0]
def take_in_order(roads):
names = {} # each place's name: every place starts as a group of its own
groups = {} # each name's places
for a, b, _ in roads:
for place in (a, b):
names[place] = place
groups[place] = [place]
kept = []
# the shortest first, two equal in letter order
in_order = sorted(roads, key=lambda road: (road[2], road[0] + road[1]))
for a, b, length in in_order:
keep, lose = names[a], names[b] # on a tie, lose is the second place's
if keep == lose: # one group already: it would close a loop
continue # so leave it out
if len(groups[lose]) > len(groups[keep]): # the smaller takes a new name
keep, lose = lose, keep
for place in groups[lose]:
names[place] = keep
groups[keep] += groups.pop(lose)
kept.append((a, b, length))
return names, kept
def cheapest_links(roads):
"""The roads kept, each as (place, place, length), shortest first."""
return take_in_order(roads)[1]
how it scales
The plot counts the sort's comparisons on two maps, every length 1 and listed in letter order, so the roads start in order. One is a line of places, each joined to the next, and the other has every pair joined. is how many places, measured from 10 to 1,000.
Each mark across or up is ten times the one before. A line climbs a little over one mark up for each mark across: 371 at 100 places and 5,051 at 1,000, about 14 times. Every pair joined climbs a little over two: 33,081 at 100 places and 4,787,336 at 1,000, about 145 times. The plot's note reads every pair further out, on the dashed part, a million to ten million places: about 118 times.
comparisons to order the roads, ten times more at each mark up
, n, ten times more at each mark to the right
- every pair, every length 1solid where measured by running it; dashed where its formula carries it on to ten millionslope 2.1: ten times n, 118 times the work
- a line, every length 1solid where measured by running it; dashed where its formula carries it on to ten million
| comparisons to order the roads | time | |
|---|---|---|
| 1,000 | 4,787,336 | about 4.8 milliseconds |
| 100,000 | 83,738,380,752 | about 1.4 minutes |
| 10,000,000 | 1,154,820,797,287,168 | about 13 days |
The comparisons to order the roads are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.
make these counts yourself
Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.
"""Count Kruskal's comparisons yourself, as the plot on this page does.
Run it with Python 3.12 or later. For each size n it runs two maps of n
places, every length 1: a line, the fewest roads that join them, and every
two places joined, the most. It prints the comparisons the sort makes for
each, by count below, the watch page's merge in place of Python's own
sorted. The counts are the same on any computer; only the time differs.
"""
def kruskal(roads):
"""Find the cheapest links that join every place that can be joined.
roads is a list of (place, place, length), each road running both
ways. Returns each place's name at the end: places with one name are
joined, so a map in pieces ends with a name for each piece.
"""
return take_in_order(roads)[0]
def take_in_order(roads):
names = {} # each place's name: every place starts as a group of its own
groups = {} # each name's places
for a, b, _ in roads:
for place in (a, b):
names[place] = place
groups[place] = [place]
kept = []
# the shortest first, two equal in letter order
in_order = sorted(roads, key=lambda road: (road[2], road[0] + road[1]))
for a, b, length in in_order:
keep, lose = names[a], names[b] # on a tie, lose is the second place's
if keep == lose: # one group already: it would close a loop
continue # so leave it out
if len(groups[lose]) > len(groups[keep]): # the smaller takes a new name
keep, lose = lose, keep
for place in groups[lose]:
names[place] = keep
groups[keep] += groups.pop(lose)
kept.append((a, b, length))
return names, kept
def cheapest_links(roads):
"""The roads kept, each as (place, place, length), shortest first."""
return take_in_order(roads)[1]
def count(roads):
"""How many comparisons the watch page's sort makes: runs of 1, 2, 4
and on, merged in neighbouring pairs, comparing their front roads."""
runs = [[(length, a + b)] for a, b, length in roads]
compared = 0
while len(runs) > 1:
merged = []
for k in range(0, len(runs) - 1, 2):
left, right = runs[k], runs[k + 1]
run = []
i = j = 0
while i < len(left) and j < len(right):
compared += 1
if right[j] < left[i]:
run.append(right[j])
j += 1
else:
run.append(left[i])
i += 1
merged.append(run + left[i:] + right[j:])
if len(runs) % 2 == 1:
merged.append(runs[-1])
runs = merged
return compared
def line(n):
"""n places in a line, p0001 onward, each road 1 long: n - 1 roads."""
return [(f"p{k:04}", f"p{k + 1:04}", 1) for k in range(1, n)]
def every_pair(n):
"""n places, every two joined, each road 1 long: n(n - 1) / 2 roads."""
return [(f"p{a:04}", f"p{b:04}", 1) for a in range(1, n + 1) for b in range(a + 1, n + 1)]
if __name__ == "__main__":
print(f"{'n':>6} {'a line':>8} {'every pair':>12}")
for n in [10, 100, 1000]:
print(f"{n:>6} {count(line(n)):>8} {count(every_pair(n)):>12}")
best, worst and average
Here best just names a line and worst names every pair: kinds of map, not the fewest and most a sort can compare. The same roads in another order can compare differently: section 4, question 1. No average is worked out. With 8 places the sort compares 11 on a line, 32 on the town of eight and 72 with every pair joined.
A case is one kind of map, such as a line. A bound says how fast that case's cost grows as places are added.
The counts are in the grid. A count is often the sum of pieces that grow at different speeds, and on long inputs the fastest-growing piece swamps the rest. For example, a count of half of n times n, less half of n: at 2,000 items, 2,000,000 less 1,000. So how a cost grows is what tells one method from another, and the bounds keep just that.
| besta line, every length 1 | worstevery pair, every length 1 | |
|---|---|---|
| comparisons to order the roads | on roads, each round's left runs | on roads, each round's left runs |
| roads looked along | , each road once | , each road once |
| roads kept | ||
| how the comparisons to order the roads grow in each case | ||
| tight, | ||
| ceiling, | ||
| floor, | ||
extra memory: A name and a spot in a group's list for each place, a second row of roads for the sort, and the roads kept.
Extra memory is the room it needs on top of the input itself, while it runs.
| map | roads | comparisons | kept |
|---|---|---|---|
| a line of 5 | 4 | 4 | 4 |
| every pair of 5 | 10 | 21 | 4 |
| a line of 8 | 7 | 11 | 7 |
| the town of eight | 12 | 32 | 7 |
| every pair of 8 | 28 | 72 | 7 |
where the formula comes from
Questions 1 to 3 count the comparisons, 4 and 5 the whole method, and 6 shows why the roads kept are cheapest.
the move: how the sort goes
the line:8 numbers stand for 8 roads. Round 2's first merge, 1, 3 with 2, 5, compares 1 with 2, then 3 with 2, then 3 with 5. That is 3 comparisons. The 5 follows with no comparison. round run length merges comparisons 1 1 6 6 2 2 3 8 3 4 1 7 4 8 1 11 all 11 32 The town of eight, in the watch page's order: A-B, A-E, A-F, B-C, E-F, B-F, F-G, C-G, C-D, G-H, D-H, D-G. In round 3 the last run of 4 waits. why it is allowed: This sort is merge sort. Each road starts as a run of 1. Each round merges neighbouring runs, comparing their front roads and taking the one that goes first. A last run with no partner waits.
the move: a round's most
the line:why it is allowed: Each comparison puts one road into the new run, and a round moves each road once at most. The town's rounds are all at most 12.
guess it, thenthe move: how many rounds
the line:Top, numbers, each double the last; bottom, their logs, each 1 more. 5 lies between 4 and 8, so its log, about 2.3, lies between 2 and 3. 12 lies between 8 and 16, so its log, about 3.6, lies between 3 and 4. why it is allowed: Run lengths double until one run holds all . For 12 roads, 1, 2, 4 and 8, so 4 rounds. The log of 12 is about 3.6, and rounded up, 4.
guess it, thenthe move: a ceiling
the line:why it is allowed: is lines 2 and 3 multiplied. For 12 roads, 48.
guess it, thenthe move: a merge's least
the line:8 roads comparisons in order 12 back to front 12 the dearest order 17 Order can change the count. Every order of 8 costs 12 to 17, between the floor of 6 and the ceiling of 24 this question reaches. why it is allowed: A merge stops when a run is used up, and each road of that run took one comparison. The right run is never the longer. So 1, 3 with 2, 5 compares 3 times, at least 2.
guess it, thenthe move: a round's right runs
the line:why it is allowed: At least one full pair fits, and what is left over is shorter than a pair, so the full pairs hold at least half the roads. Half of each pair is its right run. For 12 roads in runs of 4, one pair holds 8, so 4 in right runs, at least 3.
guess it, thenthe move: how many such rounds
the line:why it is allowed: Run lengths 1, 2, 4 and on, while twice the length is at most . For 12 roads, 1, 2 and 4, so 3, which is the log of 12 rounded down.
guess it, thenthe move: a floor
the line:why it is allowed: By lines 5 and 6, each such round compares at least . Line 7 counts those rounds, so multiply. For 12 roads, 9. The town's 32 lies between 9 and 48.
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
the move: a line's roads
the line:why it is allowed: The first place needs no road, and each place after it adds one. So is .
the move: each merge compares its left run
the line:run length left runs comparisons 1 4 of 1 4 2 2 of 2 4 4 1 of 4 4 all 12 A line of 9, so 8 roads. why it is allowed: Every length is 1 and the roads come in letter order, so the list is in order already. A merge takes all its left run first, one comparison each, then stops. adds up the left runs of length 1, those of length 2, and so on. Lengths double, so there is no . Here 4 + 4 + 4.
guess it, thenthe move: a ceiling
the line:why it is allowed: Question 1's line 4, with as . For 5 places, is 4, at most 8.
guess it, thenthe move: a simpler ceiling
the line:why it is allowed: is below , both times it appears. Rounding up adds less than 1, and from 2 places on 1 is at most , so the rounded log is at most . For 5 places, is 4, at most 23.2.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 4, with the fixed multiple 2.
guess it, thenthe move: a floor
the line:why it is allowed: Question 1's line 8. For 5 places, is 4, at least 2.
guess it, thenthe move: n - 1 is at least half of n
the line:why it is allowed: From 2 places on. For 5 places, is 4, at least 1.25.
guess it, thenthe move: the rounded log against the exact one
the line:why it is allowed: Call line 7's rounded-down log . From 3 places on is at least 1, so is at most . is below , so is at most it. For 5 places, is 2, and the log of 5, about 2.3, is at most 3, at most 4.
guess it, thenthe move: drop the rounding
the line:why it is allowed: By line 8, is at least half of . Put that into line 7. For 5 places, is 4, at least 0.73.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 9, with the fixed multiple a sixteenth.
guess it, thenthe move: both together
the line:why it is allowed: Lines 5 and 10 bound by the same growth.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: as on a line of places
the line:run length left runs comparisons 1 5 of 1 5 2 2 of 2 4 4 1 of 4 4 8 1 of 8 8 all 21 Every pair of 5, so 10 roads. At run length 2 the last run waits, and at 4 the last 2 roads wait. why it is allowed: As in question 2, the list is in order already, so each merge compares its left run.
the move: how many roads
the line:where does this come from?
question 2 of the page graphs and their words places, every two joined by a road. How many roads? the move: count place by place
the line:Every two of 5 places joined. A has 4 roads, B 3 more, C 2 more, D 1 more, E none new. 4 + 3 + 2 + 1 = 10. why it is allowed: Let be the roads, for 2 places or more. List the places. The first has roads to the after it. The next adds one fewer new road, as its road to the first is counted already. Each adds one fewer, and the last adds none.
the move: use adding up 1 to n
the line:place new roads A 4 B 3 C 2 D 1 E 0 all 10 The new roads each place adds on line 1's map, in turn. 4 + 3 + 2 + 1 + 0 = 10. where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: Read from right to left, line 1 is 1 + 2 + up to . The box under this line adds 1 up to a last number with two staircases, that number times one more, halved. Here the last number is . One more than it is , so is times , halved. For 5, 1 + 2 + 3 + 4 is 4 times 5, halved, which is 10.
the move: one way, each pair holds two
the line:why it is allowed: Let be the most one-way roads. A to B and B to A are two roads, one each way, so each pair holds two.
the move: double line 2
the line:why it is allowed: Line 3 says is , and twice line 2 is . For 5 places, 20.
the move: read it
the line:why it is allowed: 1,000 times 999, halved, is 499,500. A map of 1,000 places with 3,000 roads has 3 times as many roads as places, a few, so it is sparse. It holds 3,000 of the 499,500 it could.
why it is allowed: How many roads every pair has, shown under this line. For 5 places, 10.
guess it, thenthe move: a ceiling
the line:why it is allowed: Question 1's line 4. For 5 places, is 21, at most 40.
guess it, thenthe move: a log of n
the line:why it is allowed: Line 2 gives at most this. Squaring doubles a log: the log of 4 is 2, and of 16, its square, 4. Halving takes 1 off, and rounding up adds back less than 1. For 5 places, is 10, whose log rounded up is 4, at most 4.6.
guess it, thenthe move: use it
the line:why it is allowed: By lines 3 and 4. For 5 places, is 21, at most 46.4.
guess it, thenthe move: roads by places
the line:why it is allowed: is , at most . For 5 places, is 21, at most 58.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 6, with the fixed multiple 1.
guess it, thenthe move: a floor
the line:why it is allowed: Question 1's line 8. For 5 places, is 21, at least 7.5.
guess it, thenthe move: m is at least a quarter of n squared
the line:why it is allowed: From 2 places on, is at least half of , so is at least a quarter of , and a quarter of at least . For 5 places, is 21, at least 4.7.
guess it, thenthe move: the rounded log against the exact one
the line:why it is allowed: From 3 places on, is at least . Call line 9's rounded-down log , at least 1. is below , so is too. Then as in question 2's line 8. For 5 places, is 10 and is 3, and the log of 5, about 2.3, is at most 6.
guess it, thenthe move: drop the rounding
the line:why it is allowed: By line 10, is at least half of . Put that into line 9. For 5 places, is 21, at least 1.8.
guess it, thenthe move: read the bound
the line:From 3 places on, stays between its floor and its ceiling. why it is allowed: By line 11, with the fixed multiple a thirty-second.
guess it, thenthe move: both together
the line:why it is allowed: Lines 7 and 12 bound by the same growth.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: a new name, a bigger group
the line:road kept take a new name how many B-F F 1 A-E E 1 C-G G 1 D-H H 1 E-F B, F 2 G-H D, H 2 B-C C, D, G, H 4 all 12 The town of eight. H is renamed most, 3 times, its group growing 1, 2, 4, 8. So 8 places times 3 is a ceiling of 24, and the town writes 12. why it is allowed: Only the smaller group takes a new name, or if the two are the same size, the second letter's. So the other group is at least as big, and together they are at least double.
the move: each place's most
the line:why it is allowed: Its group starts at 1, at least doubles each time, and never holds more than . For 8 places, 1, 2, 4 and 8, so 3.
guess it, thenthe move: every place
the line:why it is allowed: Line 2 times the places. The letters at the start do not count. For 8, 24.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: three costs
the line:the town of eight count comparisons 32 new names 12 names read 24 all 68 The town of eight's three costs. why it is allowed: Comparisons by question 1 and new names by question 4. Each of the roads then reads 2 names, to test them.
the move: the sort
the line:why it is allowed: Comparisons are at most , by question 1's line 4. Two places share one road at most, so question 3's line 4 holds on any map. For the town, 48 is at most 72.
guess it, thenthe move: the new names
the line:why it is allowed: By question 4, at most . Every place here is on a road, and a road has 2 ends, so is at most , even in pieces. For the town, 24 is at most 72.
guess it, thenthe move: the names read
the line:why it is allowed: is the names read to test the roads, 2 for each. From 2 places on is at least 1, so is at most . For the town, 24 is at most 72.
guess it, thenthe move: add the three
the line:why it is allowed: Lines 2, 3 and 4 each allow , and three lots make 6. For the town, 6 times 12 times 3 is 216, and its three cost 68.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 5, with the fixed multiple 6 for these three. The method also writes each place's starting letter, at most , and in merges moves each road at most once a round, so again at most line 2's ceiling. The code keeps each name's places in a list, so finding a group looks at no other place. What remains is a fixed few steps for each road or new name. Those are only more lots of , so the whole method has a larger multiple, still fixed, on any map, even in pieces.
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
the move: what stays true
the line: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.
the move: split the places
the line: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.
guess it, thenthe move: swap a road on the loop
the line: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 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.
guess it, thenthe move: add them up
the line: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 be a network's cost, and its cost after one swap. the move: swap a road on the loop
the line: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 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.
the move: add them up
the line:why it is allowed: Taking out 6 and putting in 4 is 2 less.
the move: no dearer
the line:After the swap, B to D out and A to C in, at 12 less 2, which is 10. why it is allowed: less 2 is at most , so the swap costs no more. Before, 12; after, 10.
the move: still one piece
the line: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.
the move: on any map
the line:why it is allowed: On any map, let be the cheapest road across, and a road where the loop crosses back. Each letter also stands for its road's length. Here is A to C, of 4, and is B to D, of 6. The swap takes out and puts in.
the move: f costs at least e
the line:why it is allowed: crosses too, and is the cheapest road across, so is at least . Taking out saves at least what putting in adds.
the move: start from a cheapest one
the line:why it is allowed: The network of 12 only showed the swap. Start instead from a cheapest network without . 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 . A cheapest network that holds already needs no swap.
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.
guess it, thenthe move: no dearer
the line:why it is allowed: The road taken out crosses too, so it costs at least the cheapest across. Before, 15; after, 8.
guess it, thenthe move: roads kept stay
the line: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.
guess it, thenthe move: so it stays true
the line: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.
guess it, thenthe move: as many roads
the line:road kept groups left start 5 B-F 4 A-E 3 E-F 2 B-C 1 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 places goes from groups to 1.
guess it, thenthe move: a cheapest network has them too
the line: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, in all, and any other road would close a loop.
guess it, thenthe move: at the end
the line: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 roads. So they are all of it, one for each piece.
guess it, thenthe move: ties change nothing
the line: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.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
count it yourself
Every pair of 3 places: A, B and E. The watch page takes this map as A-B 1, A-E 1, B-E 1. Round 1 makes 1 comparison, A-B with A-E, and B-E waits. Round 2 makes 2, A-B with B-E, then A-E with B-E. So is 1 and is 2: 3 in all, shown together at step 2. Then A-B and A-E are kept, and B-E would close a loop. The last columns are running totals.
| step | what happens | comparisons to order the roads | roads looked along | roads kept |
|---|---|---|---|---|
| 1 | Every one of the 3 places starts as a group of its own, named by its letter. | 0 | 0 | 0 |
| 2 | Put the 3 roads in order from the shortest up, ties in letter order: that takes 3 comparisons. Now take each in turn. | 3 | 0 | 0 |
| 3 | The road from A to B is 1 long and joins two groups, so keep it. B takes the name A, since that group is at least as big. | 3 | 1 | 1 |
| 4 | The road from A to E is 1 long and joins two groups, so keep it. E takes the name A, since that group is at least as big. | 3 | 2 | 2 |
| 5 | The road from B to E is 1 long, but both are in the group named A already, so it would close a loop. Leave it out. | 3 | 3 | 2 |
| 6 | Done: every road has been taken. The 2 roads kept are 2 long in all, and the places make one group. | 3 | 3 | 2 |
In all, 3 comparisons to order the roads. The formula from section 4, , gives at : the same.
where you meet it
A 1956 paper set it out. Ready-made code gives it to you, keeping groups with union-find, where a join rewrites no names.
Kruskal's 1956 paper
Joseph Kruskal set out this method in a paper of under three pages. It is one of three the paper gives: go through the roads by length, and pass over any that would close a loop.
source: J. B. Kruskal, On the shortest spanning subtree of a graph and the traveling salesman problem (opens in a new tab), Proceedings of the American Mathematical Society 7(1), and the books' volume three, chapter 33
networkx's kruskal_mst_edges
networkx, ready-made Python code for maps of places and roads, uses this method by default for a cheapest network. It sorts the roads, and keeps its groups with union-find.
source: the networkx source, mst.py (opens in a new tab), its kruskal_mst_edges and minimum_spanning_tree
SciPy's minimum_spanning_tree
SciPy, ready-made Python code for science, sorts a map's roads by length, then keeps its groups with union-find too.
source: the SciPy source, _min_spanning_tree.pyx (opens in a new tab), its minimum_spanning_tree
what is used instead
When you want the shortest way between two places, use Dijkstra's method. When every pair is joined, Prim's method with a scan, looking through the places each round, grows more slowly than this sort. It grows as the square of on Prim's own page, and this sort as the square of times in question 3.
how it compares
Prim's method starts at one place and grows outward, so it finds a cheapest network of that place's piece. Dijkstra's also grows from a start, and finds shortest ways.
| method | picks next by | finds |
|---|---|---|
| Kruskal's method | the cheapest road left, unless it closes a loop | a cheapest network for each piece |
| Prim's method | the cheapest road out of its one group | a cheapest network of the start's piece |
| Dijkstra's method | the least total length from the start | shortest ways from the start |