Union of Disjoint Sets with at Most k Sets - Algorithm

  • Context:
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Sets
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Smirk)Consider an implementation of disjoint sets with union, where there can be at most $k$ disjoint sets.
The implementation uses a hash table $A[0.. \text{ max }-1]$ at which there are stored keys based on the method ordered double hashing.
Let $h1$ and $h2$ be the primary and the secondary hash function, respectively. $A$ contains the keys of the nodes of all of the above trees and also a pointer to the corresponding node for each of them.
I want to write an algorithm that takes as parameters the keys of two nodes and merges the upper trees to which the nodes belong (the nodes can belong to any upper trees, even at the same in which case it appears an appropriate message). At merging, we have to apply techniques of path compression and height reduction.How could we do this? (Thinking)
 
Last edited:
Physics news on Phys.org
This is the algorithm that merges two up trees, applying the technique of path compression:
Code:
pointer Union(S,T){
  if (S==NULL or T==NULL) return;
  if (S->count>=T->count){
      S->count=S->count+T->count;
      T->parent=S;
      return S;
  }
  else {
      T->count=T->count+S->count;
      S->parent=T;
      return T;
  }
}
 
Last edited:
What do we have to do in this case, where we have a hash table? (Thinking)