at 8:20 bhaiya told us about rank ensures path ccmpression ?? can you tell me how.
they are two different thing na?
Rank ensures path compression?
and one more thing https://ide.codingblocks.com/s/4959 i want to ask when the rank is not equal , then after doing union operation why one of set the rank is not increased??
path compression is a different thing.
inline int getparent(int x) {
return (x == -1 ? x : (p[x] = getparent(p[x])));
}
if you use this function to get parent of a node , the path is compressed.
because it flattens the tree.
But…
using rank also compresses the path, earlier in union find method the path may lead to n nodes on worst cases, but now using rank path may go to max logn nodes , hence path is compressed!
and one more thing https://ide.codingblocks.com/s/4959 i want to ask when the rank is not equal , then after doing union operation why one of set the rank is not increased??
bro just tell me one thing , why are we using rank , just to know ki which subtree is bigger…
now when both the trees are not equal then we need no change because we are adding a small subtree in the larger subtree…now when both the subtrees have equal rank then make any one one them with higher rank , i.e. increase rank by 1.