Download presentation
Presentation is loading. Please wait.
1
Disjoint Set Neil Tang 02/23/2010
CS223 Advanced Data Structures and Algorithms
2
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
3
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
4
CS223 Advanced Data Structures and Algorithms
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. a b c d g e f h i CS223 Advanced Data Structures and Algorithms
5
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
6
Linked-list Implementation
f nil head tail a b c find(b) a b c f nil tail union(f, b) CS223 Advanced Data Structures and Algorithms
7
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
8
CS223 Advanced Data Structures and Algorithms
Array Implementation CS223 Advanced Data Structures and Algorithms
9
CS223 Advanced Data Structures and Algorithms
Array Implementation CS223 Advanced Data Structures and Algorithms
10
CS223 Advanced Data Structures and Algorithms
Union Operation Time complexity: O(1) CS223 Advanced Data Structures and Algorithms
11
CS223 Advanced Data Structures and Algorithms
Find Operation Time complexity: O(N) CS223 Advanced Data Structures and Algorithms
12
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
13
Union-by-Height (Rank)
Make the shallow tree a subtree of the deeper and break ties arbitrarily. CS223 Advanced Data Structures and Algorithms
14
CS223 Advanced Data Structures and Algorithms
Size and Height -1 4 -5 6 1 2 3 4 5 6 7 -1 4 -3 6 CS223 Advanced Data Structures and Algorithms
15
Union-by-Height (Rank)
Time complexity: O(1) CS223 Advanced Data Structures and Algorithms
16
CS223 Advanced Data Structures and Algorithms
Worst-Case Tree CS223 Advanced Data Structures and Algorithms
17
Find(14) with Path Compression
CS223 Advanced Data Structures and Algorithms
18
Find with Path Compression
CS223 Advanced Data Structures and Algorithms
19
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
20
Worst-Case Time Complexity
If both union-by-size and path compression heuristics are used, the worst-case running time for any sequence of M union/find operations is O(M * (M,N)) (M, N) is the inverse Ackermann function which grows even slower than logN. CS223 Advanced Data Structures and Algorithms
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.