Skip to content

Union-find

storing

Level 3 of 3

Also called Disjoint-set forest and Merge-find set.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Places 1 to 9 start alone. How many links do the joins 1-2, 3-4 and 1-3 follow in all?

the answer, and why

the answer is D

1-2 and 3-4 join leaders, 0 links. 1-3 follows one road from 1 to 2, and one from 3 to 4. That is 2.

if you chose another

  • That counts only the first two joins, which follow nothing.
  • That counts each join, not each road followed.
  • That counts one find of 1-3, but it makes two.

question 2

Starting from places alone, which joins follow the most links?

the answer, and why

the answer is D

1-3 follows 2 links, and 1-5 follows 2 links from place 1 up to 4. That is 4. The others follow 1, 0 and 2.

if you chose another

  • 2-4 joins two leaders, so only 2-5 follows a road.
  • Place 1 leads every join, so nothing is followed.
  • 2-3 joins two leaders. 3-4 and 4-5 each follow one link, to place 2, so 2 in all.

question 3

Which is true of joining nn places into one group with union-find?

the answer, and why

the answer is D

Section 4's question on the ceiling shows a find follows at most log⁡2 n\log_2\, n roads, so a join at most 2 log⁡2 n2\,\log_2\, n, and the joins at most 2(n−1) log⁡2 n2(n - 1)\,\log_2\, n.

this tests where the formula comes from, on the learn page

if you chose another

  • Joins naming leaders follow none, and pair by pair follows 1,496 at 1,000 places.
  • In pair by pair, a find from the deep place follows 2.
  • When its leader is hung under another, a straightened place sinks one road.

question 4

Places 1 to 9 start alone. After 1-2, 3-4, 2-4, 1-3 and 5-6, how many groups are there?

the answer, and why

the answer is C

1-2, 3-4, 2-4 and 5-6 each join two groups, so 9 become 5. 1-3 finds one leader for both and joins nothing.

if you chose another

  • That takes one group away for 1-3 as well, but 1 and 3 are already joined.
  • 2-4 joins the two pairs, so they make one group.
  • Places 7, 8 and 9 are each a group of their own too.

pen and paper

question 5about 8 minutes

Places 1 to 9 start alone. Join 1-2, 3-4, 1-3 and 1-5, in that order.

  1. Draw the roads after each join.
  2. How many links does each join follow?
  3. Where does place 1 point at the end, and why?
a hint

In join 3, two groups the same size meet, so the leader of the place named first goes under the other's.

a worked answer

1-2 and 3-4 follow nothing: 1 points at 2, 3 at 4. 1-3 follows 1 road each, 2 links, and 2 hangs under 4. 1-5 follows 2 roads from 1, 2 links, then straightens 1 to point at 4, and 5 hangs under 4. 4 links in all, and 1 points at 4.

  1. step 17 of 23

    123456789

    1 points at 2, so follow the road up.

    roads followed: 1

  2. step 18 of 23

    123456789

    2 points at 4, so follow the road up.

    roads followed: 2

  3. step 20 of 23

    123456789

    Point every place passed straight at 4, so the next find is shorter.

    roads followed: 2

  4. step 22 of 23

    123456789

    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

Join 4. Place 1 follows 2 roads to 4, then points straight at 4. 5 hangs under 4.

this tests how it works, on the learn page

what a full answer includes
  • Roads 1 to 2, 3 to 4, then 2 to 4, then 1 to 4 and 5 to 4.
  • 0, 0, 2 and 2 links.
  • At 4: straightening pointed it at the leader it found.

question 6about 6 minutes

Places are joined by size, the smaller group's leader hung under the bigger's, and in a tie the leader of the place named first.

  1. Why does a place sink only when its group at least doubles?
  2. Why is no place more than log⁡2 n\log_2\, n roads below its leader?
  3. Why does no order of n−1n - 1 joins follow more than 2(n−1) log⁡2 n2(n - 1)\,\log_2\, n links?
a hint

Count how many times a group of 1 can double before it holds more than nn places.

a worked answer

A place sinks only when its leader is hung. The hung group is the smaller or, in a tie, the same size, so the group it joins is at least as big. After dd sinkings its group holds at least 2d2^d, at most nn, so dd is at most log⁡2 n\log_2\, n. Straightening only brings places closer, so a place is at most dd roads down. Each join makes two finds: n−1n - 1 joins follow at most 2(n−1) log⁡2 n2(n - 1)\,\log_2\, n.

Why do no n−1n - 1 joins into one group follow more than 2(n−1) log⁡2 n2(n - 1)\,\log_2\, n links?
  1. the move: when a place sinks

    the line:
    a place sinks only whenits group is hung underone as big or bigger\begin{gathered} \text{a place sinks only when} \\ \text{its group is hung under} \\ \text{one as big or bigger} \end{gathered}
    1. step 28 of 32

      123456789

      5 points at 4, so follow the road up.

      roads followed: 1

    2. step 31 of 32

      123456789

      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.

  2. the move: roads at most sinkings

    the line:
    its roads below the leaderare at most its sinkings\begin{gathered} \text{its roads below the leader} \\ \text{are at most its sinkings} \end{gathered}

    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.

  3. the move: each sinking doubles

    the line:
    after sinking d times, itsgroup holds at least 2d\begin{gathered} \text{after sinking } d \text{ times, its} \\ \text{group holds at least } 2^d \end{gathered}
    times sunkits group holds at least
    01
    12
    24
    38
    416
    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.

  4. the move: a group holds at most n

    the line:
    2d is at most n\begin{gathered} 2^d \text{ is at most } n \end{gathered}

    why it is allowed: By line 3, a place sunk dd times has at least 2d2^d places in its group, and no group holds more than all nn.

  5. the move: so d is small

    the line:
    d is at most  log⁡2 n\begin{gathered} d \text{ is at most } \,\log_2\, n \end{gathered}

    why it is allowed: By line 4, 1 doubled dd times is at most nn, and log⁡2 n\log_2\, n is how many times 1 doubles to reach nn, 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; dd counts sinkings, a whole number, so at most 3.

  6. the move: a find is short

    the line:
    a find follows at mostlog⁡2 n roads\begin{gathered} \text{a find follows at most} \\ \log_2\, n \text{ roads} \end{gathered}

    why it is allowed: By line 2, a place sits at most dd roads below its leader, and by line 5, dd is at most log⁡2 n\log_2\, n.

  7. the move: price a join

    the line:
    a join follows at most2 log⁡2 n links\begin{gathered} \text{a join follows at most} \\ 2\,\log_2\, n \text{ links} \end{gathered}

    why it is allowed: By line 6. Its two finds each follow at most log⁡2 n\log_2\, n roads.

  8. the move: add the joins

    the line:
    U=2(n−1) log⁡2 nU = 2(n - 1)\,\log_2\, n
    placesthe most links any order followsceiling UU
    5418.58
    6525.85
    7733.69
    8842.00
    91050.72
    Every order tried, against the ceiling.

    why it is allowed: UU is n−1n - 1 joins times line 7's most, so no order follows more. At 6 places, 2 times 5 joins is 10. The log⁡2\log_2 of 6 is between 2 and 3, as 6 lies between 4 and 8: about 2.585 by a calculator, so about 25.85.

  9. the move: a simpler ceiling

    the line:
    U≤2n log⁡2 nU \le 2n\,\log_2\, n

    why it is allowed: By line 8, as n−1n - 1 is less than nn.

  10. the move: read it as a bound

    the line:
    U∈O(n log⁡2 n)U \in O(n \,\log_2\, n)

    why it is allowed: By line 9, with the fixed multiple 2.

  11. the move: read it

    the line:
    the dearest order and theaverage are O(n log⁡2 n)\begin{gathered} \text{the dearest order and the} \\ \text{average are } O(n \,\log_2\, n) \end{gathered}

    why it is allowed: By lines 8 and 10: every order follows at most UU, so the dearest does, and so does their average. The dearest follows at least pair by pair's WW, by the first question, so it is Ω(n)\Omega(n) as well.

try it with a small nn: at n=6n = 6, line 8 gives UU, about 25.85

this tests where the formula comes from, on the learn page

what a full answer includes
  • Hung only under a group as big or bigger.
  • 2d2^d at most nn, so dd at most log⁡2 n\log_2\, n; straightening only brings places closer, so at most dd roads.
  • Two finds a join, n−1n - 1 joins.