I have been studying disjoint set data structures.
I've a query that while using union by rank & path compression together, if we skip using union by rank & assign precedence(parent) without any ranks comparison(of ranks of roots/representative element) of two sets(trees) will it affect the running time.
Though weighted-union heuristic is required while merging two sets,to append smaller set to larger one to make minimum updates as possible to point to representative element.
Union-by-rank(similar to weighted-union) is used while merging the two sets.But if we skip comparing ranks & randomly assign the precedence, it won't affect the running time??Then why do we use it..I am unable to see through it clearly ,please help me understand it if I'm wrong.
//no comparison for ranks
UNION(x,y)
x.parent=y;
genralized code
union(x,y){
if(x.rank>y.rank)
y.parent=x;
else
x.parent=y;
if(x.rank==y.rank)
y.rank=y.rank+1;
}
If we consider n disjoint sigleton elements and we apply the union function on them in such a way that always root of one set(i.e. of the tree formed in representation of the set) comes in union with a singleton element, then in all these union cases path compression has no effect and we may end up with creating a linked list.
In such worst cases, complexity of a single find can be θ(n), while union excluding the find for the two sets is still θ(1) as it is when including the union by rank. So, worst case complexities of individual queries on the set are improved by using union by rank which keeps it θ(lgn).
If we consider the case where finally after all queries we get 1 or a constant number of final sets then union by rank makes some difference in overall complexity if path compression is used.