The goal is to efficiently obtain representatives of every set. Obtaining the root as representative is better since it gives more information about the set; nodes are often augmented with properties of their subtrees.
For example, given the sequence of operations
make_set(1)
make_set(2)
make_set(3)
union_sets(2,3)
we want to efficiently obtain the parents <1, 2>
.
A possible solution:
s = empty set
for (every node v):
s.insert(find_parent(v))
There are n
nodes, finding parent has constant amortized runtime, inserting into a set Theta(log n)
. The worst-case time is Theta(n log n)
. How could this solution be improved?
If disjoint-set union is ill-suited for this purpose, is there a better approach?
Edit: Matt Timmermans pointed out that finding parent takes constant amortized time. And that using the constant time
is_root(v):
return v == parent[v]
we can also obtain Theta(n log n)
worst-case time. Nonetheless, is there an augmentation that would allow for better runtime?