Disjoint set union

void union(int x,int y)
{
int super_parent_x=get_superparent(x);
int super_parent_y=get_superparent(y);
if (super_parent_x!=super_parent_y)
{
par[super_parent_x]=super_parent_y;
}
}

We had to connect x and y, but according to the code written in this video it is connecting superparent of x and superparent of y…

Why is he connecting superparent of x and superparent of y instead of simply connecting x and y ??

first of all both the logic will work.
connecting superparent is bit efficent ,because it will take less time find parent due to some compression of path.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.