1
votes

If I have n single node trees and m find set operations (note: no unions, assuming unions have been done before) and use path compression only, is it true that this takes O(m) time? I've been trying to prove this but it doesn't seem that it is the case. Since the unions didn't use union by rank a find set can take up to O(n) time. But is it still possible for m find sets to be done in O(m) time?

1

1 Answers

2
votes

This in general will not take time O(m) to complete. Imagine that the n nodes have been split into √n groups and within each group all the nodes are linked together into a linked list of size √n. Now, if you do √n find operations, one per root of the linked list, the total work done will be Θ(n), because you have to update the pointers on pretty much every node in the group.

However, if you were to do the find operations in a different order (say, by starting at the ends of the linked lists and working backwards), you could do n total operations in O(n) time.

Generally speaking, the cost of union-find using only path compressions is O(m log n + n) for m operations on n nodes.