I'm studying time complexity of Union-Find data structure.

I saw time complexity of union and find function depends on some conditions.

  1. without anything: $O(N)$
  2. with Union by Rank: $O(\log N)$
  3. with Path Compression: $O(\alpha(N))$ which is inverse Ackermann function.

I understood $O(N)$ because of skewed tree of disjoint set. But, I cannot understand $O(\log N)$. I looked on Wikipedia and Stack Overflow, but I couldn't find it. How can I prove second time complexity?


1 Answer 1


Firstly, here is the precise statement of the case 2 in OP's question.

A sequence of m MAKE-SET, UNION, and FIND operations, n of which are MAKE-SET operations, can be performed on a disjoint-set forest with union by rank in worst-case time $O(m\log_2 n)$.

In fact, it is trivial to see that the MAKE-SET and UNION operations run in $O(1)$ time. So the question is really about the time-complexity of the FIND operation.

Here are the properties of the disjoint-set forest you can prove.

  1. Given any node $u$ that has been added to the set of union-find data structure, the ranks of the nodes in a path from $u$ to its root is strictly increasing.
  2. A node $u$ which is root of a sub-tree with rank $r$ has at least $2^r$ nodes. (This fact can be proved by induction on $r$).
  3. Every node has rank at most $\log_2 n$.

With properties 1 and 3, it should be easy to prove the desired time complexity.

The above explanation follows roughly the section 21.4 in Introduction To Algorithms, third edition by CLRS. Readers are encouraged to read the chapter 21 of that book.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.