




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





14-0: Disjoint Sets
14-1: Disjoint Sets
14-2: Disjoint Sets
14-3: Implementing Disjoint Sets
14-4: Implementing Disjoint Sets
14-5: Implementing Disjoint Sets
void CreateSets(n) { for (i=0; i 14-7: Implementing Disjoint Sets
int Find(x) { return Set[x]; }
14-8: Implementing Disjoint Sets
14-9: Implementing Disjoint Sets
void Union(x,y) { set1 = Set[x]; set2 = Set[y];
for (i=0; i < n; i=+) if (Set[i] == set2) Set[i] = set1; }
14-10: Disjoint Sets Θ()
14-11: Disjoint Sets Θ()
14-12: Disjoint Sets Θ()
We can do better! (At least for Union ...) 14-13: Implementing Disjoint Sets II
14-18: Implementing Disjoint Sets II
14-19: Implementing Disjoint Sets II
14-20: Implementing Disjoint Sets II
int Find(x) { Node tmp = Sets[x]; while (tmp.parent != null) tmp = tmp.parent; return tmp.element; }
14-21: Implementing Disjoint Sets II
14-22: Implementing Disjoint Sets II
14-23: Implementing Disjoint Sets II
void Union(x,y) { rootx = Find(x); rooty = Find(y); Sets[rootx].parent = Sets[rooty]; }
14-24: Removing pointers
14-25: Removing pointers
14-26: Removing pointers
14-27: Implementing Disjoint Sets III
int Find(x) { while (Parent[x] != -1) x = Parent[x] return x }
14-28: Implementing Disjoint Sets II
void Union(x,y) { rootx = Find(x); rooty = Find(y); Parent[rootx] = rooty; }
14-37: Union by Rank
void Union(x,y) { rootx = Find(x); rooty = Find(y); if (Parent[rootx] < Parent[rooty]) { Parent[rooty] = x; } else { if Parent[rootx] == Parent[rooty] Parent[rooty]--; Parent[rootx] = rooty; } }
14-38: Path Compression
14-39: Implementing Disjoint Sets III
int Find(x) { if (Parent[x] < 0) return x; else { Parent[x] = Find(Parent[x]); return Parent[x]; } }
14-40: Disjoint Set Θ
14-41: Disjoint Set Θ