95_96
95_96

Reputation: 361

What is importance of 'rank' in union by rank and path compression algorithm?

I have an understanding of the algorithm in this way...

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

Answers (1)

mcdowella
mcdowella

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

Related Questions