Set and UFDS Flashcards
2 implementations for sets
- HashTable -> if don’t have to union or intersect
- Union-Find Disjoint Sets (UFDS)
2 Key Ideas of UDFS
- Each set is modelled as a tree, thus a collection of disjoint sets form a forest of trees
- Each set is represented by a representative item, which is the root of the corresponding tree of that set
Main Implementation of UFDS
Arrays for Parents and Ranks
Time Complexity of findSet
O(a(N)) -> Recursively visit p[i] until p[i] = i, then compress the path
Time Complexity of isSameSet
O(a(N)) -> Check if findSet(i) == findSet(j)
Time Complexity of unionSet
O(a(N)) -> If i and j are of different disjoint sets, we can union the two sets by setting the representative item of the taller tree to be the representative item fo the new combined set
What is the Union by Rank heuristic
We use another integer array rank where rank[i] stores the upper bound of the height of tree rooted at i. It is an upper bound as path compressions can make trees shorter than its upper bound and we don’t want to waste effort maintaining the correctness of rank[i]
What is the Inverse Ackermann Function - a(N)
If both Union by Rank and Path Compression heuristics are used, then UFDS operations run in O(a(N)) time. We can assume it as O(1) for practical values of N.