Path compression

Home > Computer Science > Algorithms and data structures > Disjoint Set Union > Path compression

In this implementation, when we traverse a path to find the representative of a set, we make all the elements on the path point directly to the representative. This flattens the tree and reduces the height of subsequent traversals to O(1) on average. The time complexity of this implementation is also O(alpha(n)).