Reputation: 361
I have an understanding of the algorithm in this way...
path compression helps lower time for find operation and overtime the time complexity for path compression averages out to be O(1).
we decide which of the two is a parent node (during union operation) looking at the rank.
But I never could understand what union by rank does, tbh I don't believe I correctly understand what rank means here. I also don't understand why, during union, rank of parent is incremented by 1 if rank of the two sets to be unioned are same.
Upvotes: 0
Views: 2263
Reputation: 19601
The paragraph in https://en.wikipedia.org/wiki/Disjoint-set_data_structure starting "The first way, called union by rank, is to always attach the smaller tree to the root of the larger tree" shows that even without path compression, union by rank is good enough to reduce the cost of a join operation to worst case O(log n).
It also explains that, without path compression, the rank reflects the maximum depth of the tree produced so far, which explains why this is only incremented if the ranks are the same - because the smaller tree is added to the root of the larger tree, this is the only case in which the maximum depth actually increases.
Upvotes: 3