Union-find
how it works
The idea, then one run, moment by moment.
Union-find answers one question fast: are two places in one group? Think of towns joined by new roads over time, asked again and again whether towns 3 and 7 are joined yet. It keeps places in groups, each with one leader. Every other place points at another in its group, and following the roads up always ends at the leader. Two places share a group when their finds end at one leader. To join two places, find each one's leader. If they differ, hang the smaller group's leader under the bigger's; for two groups the same size, the leader of the place named first under the other's. On the way up, straighten: point every place passed straight at the leader. Each road followed is one link, this page's count. The run below joins places 1 to 7 by 1-2, 3-4, 1-3, 1-5, 6-7 and 5-7; places 8 and 9 stay alone.
step 11 of 32
1 points at 2, so follow the road up.
roads followed: 1
step 13 of 32
3 points at 4, so follow the road up.
roads followed: 2
step 15 of 32
Both groups have 2 places, so hang 2 under 4.
roads followed: 2
step 17 of 32
1 points at 2, so follow the road up.
roads followed: 1
step 18 of 32
2 points at 4, so follow the road up.
roads followed: 2
step 20 of 32
Point every place passed straight at 4, so the next find is shorter.
roads followed: 2
step 22 of 32
5's group has one place and 4's has 4 places, so hang the smaller group's leader, 5, under 4.
roads followed: 2
step 1 of 32
Every place is a group of its own, and 6 joins to make.
what it remembers to know where it is
- doing join
- not yet
- looking at
- not yet
- groups
- 9
Where it says not yet, nothing is held there at that moment.
step 5 of 32
Both groups have one place, so hang 1 under 2.
what it remembers to know where it is
- doing join
- 1
- looking at
- not yet
- groups
- 8
Where it says not yet, nothing is held there at that moment.
step 9 of 32
Both groups have one place, so hang 3 under 4.
what it remembers to know where it is
- doing join
- 2
- looking at
- not yet
- groups
- 7
Where it says not yet, nothing is held there at that moment.
step 26 of 32
Both groups have one place, so hang 6 under 7.
what it remembers to know where it is
- doing join
- 5
- looking at
- not yet
- groups
- 4
Where it says not yet, nothing is held there at that moment.
step 28 of 32
5 points at 4, so follow the road up.
what it remembers to know where it is
- doing join
- 6
- looking at
- 5
- groups
- 4
step 30 of 32
7 points at nothing, so it leads the group 7 is in.
what it remembers to know where it is
- doing join
- 6
- looking at
- 7
- groups
- 4
step 31 of 32
7's group has 2 places and 4's has 5 places, so hang the smaller group's leader, 7, under 4.
what it remembers to know where it is
- doing join
- 6
- looking at
- not yet
- groups
- 3
Where it says not yet, nothing is held there at that moment.
step 32 of 32
Done. The places are in 3 groups.
what it remembers to know where it is
- doing join
- not yet
- looking at
- not yet
- groups
- 3
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, pointing at nothingPython lines 7 and 8
- to join two places, find each one's leaderPython lines 19 and 20
- follow the roads up until a place points at nothing: that is the leaderPython lines 10, 11, 12, 13 and 14
- then point every place passed straight at the leaderPython lines 15, 16 and 17
- if the leaders differ, hang the smaller group's leader under the other'sPython lines 23, 24, 25 and 26
- two groups of one size: either onePython line 25
- if they are the same, the two places are already joinedPython lines 21 and 22
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 union_find(joins, n=9):
"""Join places 1 to n into groups, one join at a time.
Each place points at another in its group, or at itself when it leads
the group. Returns where each place points, place 1 first.
"""
parent = list(range(n + 1)) # every place leads a group of its own
size = [1] * (n + 1)
def find(p):
passed = []
while parent[p] != p: # follow the roads up until a place points at itself, as a leader does
passed.append(p)
p = parent[p]
for q in passed: # then point every place passed straight at the leader
parent[q] = p
return p
for a, b in joins: # to join two places, find each one's leader
ra, rb = find(a), find(b)
if ra == rb: # the same leader: already joined
continue
if size[ra] > size[rb]: # hang the smaller group's leader under the other's
ra, rb = rb, ra
parent[ra] = rb # two groups of one size: a's leader goes under b's
size[rb] += size[ra]
return parent[1:]
how it scales
Links counted for 10 to 100,000 places. Places start as groups of one, and each join makes two groups one, so they need one join fewer than the places: 9 places, 8 joins. The plot draws joins picked at random.
Each mark across or up is ten times the one before, a log-log plot. Picked at random, the joins follow about 99 links at 100 places and about 1,090 at 1,000. That is one mark up for each mark across, so the links grow in step with the places. Each point is the mean of 20 orders. Pair by pair, counted exactly in section 4, follows 146 and 1,496.
roads followed, ten times more at each mark up
, how many places there are, ten times more at each mark to the right
- joins picked at randompoints only, each measured by a real run, since no single formula for the roads followed of this row is worked out here
| roads followed | time | |
|---|---|---|
| 1,000 | 1,090 | about 1.1 microseconds |
| 100,000 | 111,863 | about 112 microseconds |
The roads followed 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 union-find's links yourself, as the plot on this page does.
Run it with Python 3.12 or later. For each size n it joins places 1 to n
into one group by n - 1 joins, and prints the links followed for the
pair-by-pair order and the average over 20 orders picked at random. A link is
one road followed while finding a leader. The counts are the same on any
computer; only the time differs.
"""
import random
def union_find(joins, n=9):
"""Join places 1 to n into groups, one join at a time.
Each place points at another in its group, or at itself when it leads
the group. Returns where each place points, place 1 first.
"""
parent = list(range(n + 1)) # every place leads a group of its own
size = [1] * (n + 1)
def find(p):
passed = []
while parent[p] != p: # follow the roads up until a place points at itself, as a leader does
passed.append(p)
p = parent[p]
for q in passed: # then point every place passed straight at the leader
parent[q] = p
return p
for a, b in joins: # to join two places, find each one's leader
ra, rb = find(a), find(b)
if ra == rb: # the same leader: already joined
continue
if size[ra] > size[rb]: # hang the smaller group's leader under the other's
ra, rb = rb, ra
parent[ra] = rb # two groups of one size: a's leader goes under b's
size[rb] += size[ra]
return parent[1:]
def pair_by_pair(n):
"""1-2, 3-4 and 1-3, then each new pair joined, and joined to the place left two roads deep."""
if n < 4:
return [(1, 2), (1, 3)][: n - 1]
joins = [(1, 2), (3, 4), (1, 3)]
deep, p = 1, 5
while p + 1 <= n:
joins += [(p, p + 1), (deep, p)]
deep, p = p, p + 2
if p <= n:
joins.append((deep, p))
return joins
def picked(n):
"""n - 1 joins, each of two places picked at random from different groups."""
group = list(range(n + 1))
def top(p):
while group[p] != p:
group[p] = group[group[p]] # skip a step on the way: same leader, shorter roads next time
p = group[p]
return p
joins = []
while len(joins) < n - 1:
a, b = random.randint(1, n), random.randint(1, n)
if top(a) != top(b):
group[top(a)] = top(b)
joins.append((a, b))
return joins
def count(joins):
"""Join as union_find() does, counting each road followed: the links the plot counts."""
n = max([9] + [p for join in joins for p in join])
parent = list(range(n + 1))
size = [1] * (n + 1)
links = 0
def find(p):
nonlocal links
passed = []
while parent[p] != p:
links += 1
passed.append(p)
p = parent[p]
for q in passed:
parent[q] = p
return p
for a, b in joins:
ra, rb = find(a), find(b)
if ra == rb:
continue
if size[ra] > size[rb]:
ra, rb = rb, ra
parent[ra] = rb
size[rb] += size[ra]
return links
if __name__ == "__main__":
random.seed(1)
print(f"{'n':>7} {'pair by pair':>13} {'picked at random':>18}")
for n in [10, 100, 1000, 10000]:
average = sum(count(picked(n)) for _ in range(20)) / 20
print(f"{n:>7} {count(pair_by_pair(n)):>13} {average:>18.1f}")
best, worst and average
Joins that name two leaders every time follow no road: 0 links. That case is not plotted, as a log-log plot's marks go 1, 10, 100, each ten times the last, and never reach 0. The dearest order is known only for small sizes. Every order was tried for 2 to 9 places, and pair by pair always follows the most; at 9 places it is 1-2, 3-4, 1-3, 5-6, 1-5, 7-8, 5-7, 7-9. At 10 places it no longer does: 1-2, 3-4, 1-3, 5-6, 7-8, 5-7, 1-5, 1-9, 2-10 follows 12 links to its 11. So there is no worst column. Section 4 bounds the dearest order: at least pair by pair's , at most . The books cite a lower ceiling still, proved in research and beyond this page (volume three, chapter 33).
A case is one kind of input, the thing the method is given, such as joins picked at random, however many places. A bound says how fast that case's cost grows as more places join.
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.
| averagejoins picked at random | |
|---|---|
| roads followed | measured, at most : about 99 at 100 |
| how the roads followed grow in each case | |
| tight, | not worked out here |
| ceiling, | , as every order |
| floor, | not worked out here |
extra memory: two numbers a place, where it points and its group's size
Extra memory is the room it needs on top of the input itself, while it runs.
step 27 of 30
4 points at nothing, so it leads the group 4 is in.
roads followed: 0
step 28 of 30
8 points at nothing, so it leads the group 8 is in.
roads followed: 0
step 29 of 30
Both groups have 4 places, so hang 4 under 8.
roads followed: 0
| places | the most links any order follows | pair by pair | pairs, then pairs of pairs |
|---|---|---|---|
| 5 | 4 | 4 | 4 |
| 6 | 5 | 5 | 5 |
| 7 | 7 | 7 | 6 |
| 8 | 8 | 8 | 8 |
| 9 | 10 | 10 | 10 |
step 39 of 46
1 points at 4, so follow the road up.
roads followed: 1
step 41 of 46
5 points at 6, so follow the road up.
roads followed: 2
step 42 of 46
6 points at 8, so follow the road up.
roads followed: 3
step 44 of 46
Point every place passed straight at 8, so the next find is shorter.
roads followed: 3
step 45 of 46
Both groups have 4 places, so hang 4 under 8.
roads followed: 3
where the formula comes from
Each count above, worked out line by line. Each reason names the line it uses.
the move: the first four places
the line:step 11 of 28
1 points at 2, so follow the road up.
roads followed: 1
step 13 of 28
3 points at 4, so follow the road up.
roads followed: 2
step 15 of 28
Both groups have 2 places, so hang 2 under 4.
roads followed: 2
Place 1 is now two roads below 4, the deep place. why it is allowed: 1-2 and 3-4 join leaders, 0 links. 1-3 follows one road from each, 2 links. Place 2 hangs under 4, so place 1 is now two roads deep.
the move: each new pair
the line:step 21 of 28
1 points at 2, so follow the road up.
roads followed: 1
step 22 of 28
2 points at 4, so follow the road up.
roads followed: 2
step 24 of 28
Point every place passed straight at 4, so the next find is shorter.
roads followed: 2
step 25 of 28
5 points at 6, so follow the road up.
roads followed: 3
step 27 of 28
6's group has 2 places and 4's has 4 places, so hang the smaller group's leader, 6, under 4.
roads followed: 3
1-5 follows 3 roads in all, and leaves place 5 two roads below 4. after the first four joins links the deep place after the pair 5 and 6 5-6, then 1-5 3 5 the pair 7 and 8 7-8, then 5-7 3 7 9 alone 7-9 2 none Pair by pair at 9 places, after 1-2, 3-4 and 1-3. Each pair hands the deep place on. why it is allowed: Joining the pair, 5-6, follows nothing. Then 1-5: 1 follows two roads, to 2 and 4, and 5 one, to 6. 4's group always holds 4 places or more, more than the pair's 2, so 6 hangs under 4 every time, and 5 is the new deep place. The table follows each pair to 9 places.
guess it, thenthe move: a last place alone
the line:step 41 of 47
7 points at 8, so follow the road up.
roads followed: 1
step 42 of 47
8 points at 4, so follow the road up.
roads followed: 2
step 46 of 47
9's group has one place and 4's has 8 places, so hang the smaller group's leader, 9, under 4.
roads followed: 2
At 9 places the last join is 7-9. Place 7 follows 2 roads, to 8 and then 4, and 9 leads itself, 2 links. why it is allowed: When one place is left over, join the deep place to it. The deep place follows 2 roads, and the place alone leads itself.
guess it, thenthe move: add them up
the line:why it is allowed: is the links pair by pair follows. By lines 1 to 3: after the first four, the other places make pairs, and one is left alone when is odd. At 6 places, one pair: 2 + 3 = 5.
guess it, thenthe move: even n, over 2
the line:why it is allowed: For even , the places make pairs and none is alone, so line 4 reads as the left side: at 6, 2 + 3 × 1 = 5. 2 is 4 halves, and 3 times is . At 6, both sides are 5.
guess it, thenthe move: even n, the top
the line:why it is allowed: The top, , is . At 6, 10 halves, 5.
guess it, thenthe move: odd n, over 2
the line:why it is allowed: For odd , one of the places is alone, so the other make pairs, and line 4 reads as the left side. The two 2s are 8 halves, and 3 times is . At 7, 3 places: 1 pair and 1 alone, both sides 7.
guess it, thenthe move: odd n, the top
the line:why it is allowed: The top, , is . At 7, 14 halves, 7.
guess it, thenthe move: one formula
the line:places line 4 line 9 4 2 2 2.5 2 5 4 3.5 4 4 6 5 5 5.5 5 7 7 6.5 7 7 8 8 8 8.5 8 9 10 9.5 10 10 Rounding the fourth column down always gives line 4. why it is allowed: For even , is line 6's whole count plus a half, so it rounds down to line 6. For odd , line 8's is a count, whole already. The table shows 4 to 9 places.
guess it, thenthe move: drop the rounding and the 7
the line:why it is allowed: Line 9 rounds down, which is less than . At 6, 5 is at most 9.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 10, with the fixed multiple 1.5.
guess it, thenthe move: a floor
the line:why it is allowed: By lines 6 and 8, even gives exactly , and odd gives , a half more.
guess it, thenthe move: take the tops away
the line:why it is allowed: less is . At 6, 5 less 1 is 4.
guess it, thenthe move: a simpler floor
the line:why it is allowed: Line 12 less , which is not below 0 for 4 or more places, and taking away a number not below 0 keeps a floor. By line 13 what is left is , which is . At 6, 5 is at least 4.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 14. is at least half of for 4 or more places.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 11 and 15 bound by the same growth, .
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
the move: when a place sinks
the line:step 28 of 32
5 points at 4, so follow the road up.
roads followed: 1
step 31 of 32
7's group has 2 places and 4's has 5 places, so hang the smaller group's leader, 7, under 4.
roads followed: 1
The group of 6 and 7 holds 2, the group of 4 holds 5. So 7 hangs under 4, and 6 sinks one road, to two roads below 4. why it is allowed: It sinks one road only when its leader is hung, and the one hung is the smaller group's leader, or in a tie, the leader of the place named first.
the move: roads at most sinkings
the line:why it is allowed: A place starts 0 roads below its leader, each sinking adds one, and straightening only brings places closer. Place 6 sank twice, under 7 and then with 7 under 4, and sits 2 roads below 4.
guess it, thenthe move: each sinking doubles
the line:times sunk its group holds at least 0 1 1 2 2 4 3 8 4 16 With 9 places, no place sinks a fourth time, as that needs 16. why it is allowed: By line 1, each time it sinks, its group joins one at least as big, so it at least doubles. 6's group of 2 joined one of 5, and now holds 7.
guess it, thenthe move: a group holds at most n
the line:why it is allowed: By line 3, a place sunk times has at least places in its group, and no group holds more than all .
guess it, thenthe move: so d is small
the line:why it is allowed: By line 4, 1 doubled times is at most , and is how many times 1 doubles to reach , by the page on halving and logarithms. With 9 places, 1 doubled 3 times is 8, at most 9, and 4 times is 16, too many; counts sinkings, a whole number, so at most 3.
guess it, thenthe move: a find is short
the line:why it is allowed: By line 2, a place sits at most roads below its leader, and by line 5, is at most .
guess it, thenthe move: price a join
the line:why it is allowed: By line 6. Its two finds each follow at most roads.
guess it, thenthe move: add the joins
the line:places the most links any order follows ceiling 5 4 18.58 6 5 25.85 7 7 33.69 8 8 42.00 9 10 50.72 Every order tried, against the ceiling. why it is allowed: is joins times line 7's most, so no order follows more. At 6 places, 2 times 5 joins is 10. The of 6 is between 2 and 3, as 6 lies between 4 and 8: about 2.585 by a calculator, so about 25.85.
guess it, thenthe move: a simpler ceiling
the line:why it is allowed: By line 8, as is less than .
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 9, with the fixed multiple 2.
guess it, thenthe move: read it
the line:why it is allowed: By lines 8 and 10: every order follows at most , so the dearest does, and so does their average. The dearest follows at least pair by pair's , by the first question, so it is as well.
guess it, then
try it with a small : at , line 8 gives , about 25.85
count it yourself
Count every link as places 1 to 6 are joined pair by pair, 1-2, 3-4, 1-3, 5-6 and 1-5. Each place's road is written as where it points: 1 → 2 means place 1 points at place 2. Places 7, 8 and 9 stay alone.
| step | where each place points | what happens | roads followed |
|---|---|---|---|
| 1 | Every place is a group of its own, and 5 joins to make. | 0 | |
| 2 | To join 1 and 2, find each one's leader. | 0 | |
| 3 | 1 points at nothing, so it leads the group 1 is in. | 0 | |
| 4 | 2 points at nothing, so it leads the group 2 is in. | 0 | |
| 5 | 1 → 2 | Both groups have one place, so hang 1 under 2.1 → 2 | 0 |
| 6 | 1 → 2 | To join 3 and 4, find each one's leader.1 → 2 | 0 |
| 7 | 1 → 2 | 3 points at nothing, so it leads the group 3 is in.1 → 2 | 0 |
| 8 | 1 → 2 | 4 points at nothing, so it leads the group 4 is in.1 → 2 | 0 |
| 9 | 1 → 2, 3 → 4 | Both groups have one place, so hang 3 under 4.1 → 2, 3 → 4 | 0 |
| 10 | 1 → 2, 3 → 4 | To join 1 and 3, find each one's leader.1 → 2, 3 → 4 | 0 |
| 11 | 1 → 2, 3 → 4 | 1 points at 2, so follow the road up.1 → 2, 3 → 4 | 1 |
| 12 | 1 → 2, 3 → 4 | 2 points at nothing, so it leads the group 1 is in.1 → 2, 3 → 4 | 1 |
| 13 | 1 → 2, 3 → 4 | 3 points at 4, so follow the road up.1 → 2, 3 → 4 | 2 |
| 14 | 1 → 2, 3 → 4 | 4 points at nothing, so it leads the group 3 is in.1 → 2, 3 → 4 | 2 |
| 15 | 1 → 2, 2 → 4, 3 → 4 | Both groups have 2 places, so hang 2 under 4.1 → 2, 2 → 4, 3 → 4 | 2 |
| 16 | 1 → 2, 2 → 4, 3 → 4 | To join 5 and 6, find each one's leader.1 → 2, 2 → 4, 3 → 4 | 2 |
| 17 | 1 → 2, 2 → 4, 3 → 4 | 5 points at nothing, so it leads the group 5 is in.1 → 2, 2 → 4, 3 → 4 | 2 |
| 18 | 1 → 2, 2 → 4, 3 → 4 | 6 points at nothing, so it leads the group 6 is in.1 → 2, 2 → 4, 3 → 4 | 2 |
| 19 | 1 → 2, 2 → 4, 3 → 4, 5 → 6 | Both groups have one place, so hang 5 under 6.1 → 2, 2 → 4, 3 → 4, 5 → 6 | 2 |
| 20 | 1 → 2, 2 → 4, 3 → 4, 5 → 6 | To join 1 and 5, find each one's leader.1 → 2, 2 → 4, 3 → 4, 5 → 6 | 2 |
| 21 | 1 → 2, 2 → 4, 3 → 4, 5 → 6 | 1 points at 2, so follow the road up.1 → 2, 2 → 4, 3 → 4, 5 → 6 | 3 |
| 22 | 1 → 2, 2 → 4, 3 → 4, 5 → 6 | 2 points at 4, so follow the road up.1 → 2, 2 → 4, 3 → 4, 5 → 6 | 4 |
| 23 | 1 → 2, 2 → 4, 3 → 4, 5 → 6 | 4 points at nothing, so it leads the group 1 is in.1 → 2, 2 → 4, 3 → 4, 5 → 6 | 4 |
| 24 | 1 → 4, 2 → 4, 3 → 4, 5 → 6 | Point every place passed straight at 4, so the next find is shorter.1 → 4, 2 → 4, 3 → 4, 5 → 6 | 4 |
| 25 | 1 → 4, 2 → 4, 3 → 4, 5 → 6 | 5 points at 6, so follow the road up.1 → 4, 2 → 4, 3 → 4, 5 → 6 | 5 |
| 26 | 1 → 4, 2 → 4, 3 → 4, 5 → 6 | 6 points at nothing, so it leads the group 5 is in.1 → 4, 2 → 4, 3 → 4, 5 → 6 | 5 |
| 27 | 1 → 4, 2 → 4, 3 → 4, 5 → 6, 6 → 4 | 6's group has 2 places and 4's has 4 places, so hang the smaller group's leader, 6, under 4.1 → 4, 2 → 4, 3 → 4, 5 → 6, 6 → 4 | 5 |
| 28 | 1 → 4, 2 → 4, 3 → 4, 5 → 6, 6 → 4 | Done. The places are in 4 groups.1 → 4, 2 → 4, 3 → 4, 5 → 6, 6 → 4 | 5 |
In all, 5 roads followed. The formula from section 4, , gives at : the same.
where you meet it
Two widely used Python libraries keep groups with union-find.
networkx's cheapest lines
networkx, a library for measuring networks of things, finds the cheapest lines that join every town by Kruskal's method. It tries lines cheapest first, and keeps one only if its two towns are in different groups, their leaders different. Its union-find hangs by size and straightens.
source: the networkx source, networkx/utils/union_find.py (opens in a new tab), used in networkx/algorithms/tree/mst.py (opens in a new tab)
scikit-image's blobs
scikit-image, a library for pictures, numbers every blob in a picture whose pixels hold numbers. Touching pixels of one number are joined, side by side or, unless told otherwise, at a corner. Every pixel's leader names its blob, numbered from 1, and the background is 0.
source: the scikit-image source, src/skimage/measure/_ccomp.pyx (opens in a new tab) at version 0.26.0, behind its label function for pictures of numbers
| line | price | its two towns | kept |
|---|---|---|---|
| 1-2 | 1 | different groups | yes, 1 and 2 joined |
| 3-4 | 2 | different groups | yes, 3 and 4 joined |
| 1-3 | 3 | different groups | yes, all four joined |
| 2-4 | 4 | one group | no |
| row | a | b | c | d |
|---|---|---|---|---|
| 1 | 1 | 1 | 0 | 2 |
| 2 | 1 | 0 | 0 | 2 |
| 3 | 0 | 0 | 2 | 2 |
what is used instead
To ask once whether two towns are joined, walk the lines between them. Union-find is worth it when joins and questions, such as whether towns 3 and 7 share a group, come mixed, many of each.
how it compares
Two ways to keep places in groups, set side by side by what each step costs.
| way | do two places share a group | join two groups | memory |
|---|---|---|---|
| union-find | find both leaders, at most roads each | find both leaders, then hang one | two numbers a place |
| a group name on every place | compare the two names, no road | rename every place of the smaller group, up to half the places | one name a place |
| place | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| names | A | A | B | B | B |
| after joining 1 and 3 | B | B | B | B | B |