200 likes | 369 Views
Disjoint Set Neil Tang 02/26/2008. Class Overview. Disjoint Set and An Application Basic Operations Linked-list Implementation Array Implementation Union-by-Size and Union-by-Height(Rank) Find with Path Compression Worst-Case Time Complexity. Disjoint Set.
E N D
Disjoint Set Neil Tang02/26/2008 CS223 Advanced Data Structures and Algorithms
Class Overview • Disjoint Set and An Application • Basic Operations • Linked-list Implementation • Array Implementation • Union-by-Size and Union-by-Height(Rank) • Find with Path Compression • Worst-Case Time Complexity CS223 Advanced Data Structures and Algorithms
Disjoint Set • Given a set of elements, we can have a collection S = {S1, S2, ... Sk} of disjoint dynamic (sub) sets. • Representative of a set: We choose one element of a set to identify the set, e.g., we use the root of a tree to identify a tree, or the head element of a linked list to access the linked list. • Usually, we want to find out if two elements belong to the same set. CS223 Advanced Data Structures and Algorithms
a d f h c b e g i An Application • Given an undirected graph G = (V, E) • We may want to find all connected components, whether the graph is connected or whether two given nodes belong to the same connected component. CS223 Advanced Data Structures and Algorithms
Basic Operations • find(x): find which disjoint set x belongs to • Union(x,y): Union set x and set y. CS223 Advanced Data Structures and Algorithms
head f tail nil a b c tail nil find(b) a b c f tail nil Linked-list Implementation union(f, b) CS223 Advanced Data Structures and Algorithms
Array Implementation • Assume that all the elements are numbered sequentially from 0 to N-1. CS223 Advanced Data Structures and Algorithms
Array Implementation CS223 Advanced Data Structures and Algorithms
Array Implementation CS223 Advanced Data Structures and Algorithms
Union Operation Time complexity: O(1) CS223 Advanced Data Structures and Algorithms
Find Operation Time complexity: O(N) CS223 Advanced Data Structures and Algorithms
Union-by-Size • Make the smaller tree a subtree of the larger and break ties arbitrarily. CS223 Advanced Data Structures and Algorithms
Union-by-Height (Rank) • Make the shallow tree a subtree of the deeper and break ties arbitrarily. CS223 Advanced Data Structures and Algorithms
Size and Height 0 1 2 3 4 5 6 7 CS223 Advanced Data Structures and Algorithms
Union-by-Height (Rank) Time complexity: O(logN) CS223 Advanced Data Structures and Algorithms
Worst-Case Tree CS223 Advanced Data Structures and Algorithms
Find with Path Compression CS223 Advanced Data Structures and Algorithms
Find with Path Compression CS223 Advanced Data Structures and Algorithms
Find with Path Compression • Fully compatible with union-by-size. • Not compatible with union-by-height. • Union-by-size is usually as efficient as union-by-height. CS223 Advanced Data Structures and Algorithms
Worst-Case Time Complexity • If both union-by-rank and path compression heuristics are used, the worst-case running time for any sequence of M union/find operations is O(M * (M,N)), where (M, N) is the inverse Ackermann function which grows even slower than logN. • For any practical purposes, (M, N) < 4. CS223 Advanced Data Structures and Algorithms