help you fix your problem First, when you get keys, you need to find them in the hash table. Hash table contains entries: (key, pointertonode). code :
Algorithm(k1,k2){
int i=0,j=0;
int i1,i2;
while (i<max and A[i1 = h1(k1)+i*h2(k1) mod size(A)]>key!=k1){
i++;
}
while (j<max and A[i2 = h1(k2)+j*h2(k2) mod size(A)]>key!=k2){
j++;
}
if (A[i1]>key!=k1) return;
if (A[i2]>key!=k2) return;
pointer node1,node2,root1,root2;
node1=A[i1]>node;
node2=A[i2]>node;
root1=UpTreeFind(node1);
root2=UpTreeFind(node2);
if (root1==root2){
printf("The nodes belong to the same up tree");
return;
}
// path compression
pointer tmp,tmpParent;
tmp = node1;
while (tmp>parent!=root1) {
tmpParent=tmp>parent;
tmp>parent=root1;
tmp=tmpParent;
}
tmp = node2;
while (tmp>parent!=root2) {
tmpParent=tmp>parent;
tmp>parent=root2;
tmp=tmpParent;
}
UpTreeUnion(root1,root2);
}
Share :

dynamicset operation UNION takes two disjoint sets S1 and S2 as input
By : Jevi
Date : March 29 2020, 07:55 AM

Why is my Union Find Disjoint Sets algo for this problem not passing all test cases?
By : Vyshak
Date : March 29 2020, 07:55 AM
like below fixes the issue The for loop you put in union ruins the performance. Just take it out. In the histogram loop, you need int root = find(j,parent);

O(1) Make, Find, Union in Disjoint Sets Data Structure
By : user3910535
Date : March 29 2020, 07:55 AM
seems to work fine My intuition agrees with your colleague. You say: u.set.list.append(v.set.list); // hypothetical method, append a list in O(1)

Implementing Disjoint Sets (Union Find) in C++
By : Saif
Date : March 29 2020, 07:55 AM
it should still fix some issue Not a perfect implementation by any means (I did write it after all!), but does this help?

Detect if a graph is bipartite using union find (aka disjoint sets)
By : yamini
Date : March 29 2020, 07:55 AM
I wish this help you Given a graph represented as an adjacencylist (i.e. a list of edges), you can determine if it's bipartite as follows: Initialize a disjointset data structure SETS, with a singleton set for each vertex. (If there is an evenlength path between two vertices, then we will ultimately unify those two vertices into the same set, unless we return ꜰᴀʟꜱᴇ first.) Initialize a mapping MAP from each vertex to ɴɪʟ. (As we examine edges, we will populate MAP with a mapping from each vertex to one of its neighbors.) For each edge {u, v}: If u and v belong to the same set in SETS, then return ꜰᴀʟꜱᴇ. If MAP[u] = ɴɪʟ, set MAP[u] := v.

