DisjointSet Slide
DisjointSet Slide
DisjointSet Slide
• Performance parameters:
– m = number of calls to FIND-SET and UNION operations
disjoint sets =
– n = number of elements = number of calls to MAKE-SET
connected components
• Application: Dynamic connectivity over initially empty graph
– ADD-NODE(u): add node u (1 MAKE-SET operation)
– ADD-EDGE(u, v): add an edge between nodes u and v (1 UNION operation)
– IS-CONNECTED(u, v): is there a path between u and v ? (2 FIND-SET operations)
Disjoint-Set Operations: Implementation (1)
Linked List Implementation Set A: {c, h ,e, b}
• MAKE-SET(x): O(1)
– need to create only one
node created with
appropriate pointers Set B: {f, g, d}
• FIND-SET(x): O(n)
– need to traverse entire
linked list to find x
Set (A U B): {c, h, e, b, f, g, d}
• UNION(x,y): O(n)
– need to point back all
back-pointers of second
list to head of first list
Disjoint-Set Operations: Implementation (2)
• Array Representation UNION(3,5) or
– Represent each set as tree of elements UNION(8,6) or
UNION(8,7)
– Allocate an array of parent[] of length n
– parent[i]=j (parent of element i is j)
/ /
0 0 • Analysis of Operations:
– Total zeros in array = Disjoint-sets
X X
– FIND-SET(x): O(n) worst-case
– UNION(x,y): O(n) worst-case
• UNION(FIND-SET(x), FIND-SET(y))
• O(n) due to FIND-SET operation
Suppress self-loops for root for brevity! Solution: Smart Union-Find Algorithms !!
Smart Disjoint-Set Operations: Union-by-Size
• Union-by-Size UNION(x,y) {
– Maintain a tree size r ß FIND-SET(x);
(number of nodes) for s ß FIND-SET(y);
each root node if(r == s) return r;
– Link root of smaller else if(size[r] > size[s]) {
tree to root of larger parent[s] ß r;
tree (break tries size[r] = size[r] + size[s];
arbitrarily) return r;
}
FIND-SET(x) { else { UNION(3,5)
while(x is not parent) parent[r] ß s;
x ß parent[x]; size[s] = size[r] + size[s];
return x; return s;
} MAKE-SET(x) {
}
parent[x] ß 0;
}
size[x] ß 1;
return x;
}
Analysis of Union-by-Size Heuristic (1)
Property: Using union-by-size, for every root node r, we have size[r] ≥ 2height(r)
Proof: [ by induction on number of links ]
– Base case: singleton tree has size 1 and height 0
– Inductive hypothesis: assume true after first i links
• The UNION operation takes O(1) time except for its two calls to FIND-SET
– FIND-SET required to find out the set representative (which is the root)
• m number of UNION and FIND-SET operations takes a total of O(m log2 n) time
Smart Disjoint-Set Operations: Union-by-Rank
• Union-by-Rank rank = height
– Maintain an integer
rank for each node, UNION(x,y) {
initially 0 r ß FIND-SET(x);
s ß FIND-SET(y);
– Link root of smaller
if (r == s) return r;
rank to root of larger
else if (rank[r] ≥ rank[s]) {
rank; if tie, increase
rank of larger root by 1 parent[s] ß r;
if(rank[r] == rank[s])
FIND-SET(x) { rank[r] = rank[r] + 1; UNION(3,5)
while(x is not parent) return r;
x ß parent[x]; }
return x; else {
} MAKE-SET(x) {
parent[r] ß s;
parent[x] ß 0;
return s;
rank[x] ß 0;
}
return x;
}
}
Analysis of Union-by-Rank Heuristic (1)
Property-1: If x is not a root node, then rank[x] < rank[parent[x]]
Proof: A node of rank k is created only by linking two roots of rank k – 1.
Property-2: If x is not a root node, then rank[x] will never change again
Proof: Rank changes only for roots; a non-root never becomes a root.
FIND-SET(x) {
if(x is not parent)
parent[x] ß FIND-SET(parent[x]);
return x;
}
Properties of Union-by-Rank + Path Compression (1)
Property-0: The tree roots, node ranks, and elements within a tree are the same with or
without path compression.
Property-1: If x is not a root node, then rank[x] < rank[parent[x]]
Proof: Path compression can make x point to only an ancestor of parent[x]
Property-2: If x is not a root node, then rank[x] will never change again
Property-3: If parent[x] changes, then rank[parent[x]] strictly increases.
Proof: Path compression doesn’t change any ranks, but it can change parents
If parent[x] doesn’t change during a path compression the inequality continues to hold
if parent[x] changes, then rank[parent[x]] strictly increases
Property-4: Any root node of rank k has ≥ 2k nodes in its tree
Property-5: The highest rank of a node is ≤ ⎣log2 n⎦
Property-6: For any integer k ≥ 0, there are ≤ n / 2k nodes with rank k
Properties of Union-by-Rank + Path Compression (2)
• Definitions: Rank Groups
• Complexity Analysis:
– Time Complexity = (Number of nodes in group g) x (Movement charges across
groups) x (Movement charges with groups) = (n/F(g)) x (log* n) x [F(g) – F(g-1)]
≤ n log* n [ since, (n/F(g))x[F(g) – F(g-1)] ≤ n ]