void unionEquals(vl & G, DisjointSet &ds, CmpByMask cmp) { for (uint i = 0; i < G.size() - 1; ++i) { /* if previous less than next */ if (cmp(G[i], G[i + 1])) { continue; } /* two adjacent nodes are equal */ /* check if they belong to different component and union them */ /* BUG: keep index in G */ if (ds.findSet(i) != ds.findSet(i + 1)) { ds.unionNodes(i, i + 1); } } }
int main(){ DisjointSet<int> s; /* * you fill in here to set up the disjoint set */ s.createSet(1); s.createSet(2); s.createSet(3); s.createSet(4); s.createSet(5); s.createSet(6); s.createSet(7); s.createSet(8); s.createSet(9); s.unionSets(3,5); s.unionSets(4,2); s.unionSets(1,6); s.unionSets(5,7); s.unionSets(4,8); s.unionSets(3,7); s.unionSets(8,1); assert(s.findSet(1) == s.findSet(6)); // 1 and 6 are connected. assert(s.findSet(3) != s.findSet(6)); // 3 and 6 are not connected. assert(s.findSet(1) == s.findSet(1)); // 1 and 1 are connected. assert(s.findSet(3) == s.findSet(5)); // 3 and 5 are connected. assert(s.findSet(3) != s.findSet(9)); // 3 and 9 are not connected. cout<< "Done"<<endl; }
int main() { DisjointSet djset; DisjointSet::NodePtr id[3]; id[0] = djset.makeSet(0); id[1] = djset.makeSet(1); id[2] = djset.makeSet(2); djset.linkSets(id[0], id[1]); djset.linkSets(id[1], id[2]); DisjointSet::SetIdentifier t1 = djset.findSet(id[0]); DisjointSet::SetIdentifier t2 = djset.findSet(id[2]); }