Disjoint Sets
Dynamic Connectivity
Connected Components
- Record sets that something belongs to
- Connected component = maximal set of mutually connected items
Quick Union
- Arrange in hierarchical tree structure
- Map items (array indices) to parents (parent indices)
Weighted Quick Union
- Modify quick-union to avoid tall trees
- Track tree size (number of elements), also works similarly for height
- Always link root of smaller tree to larger tree
- Max depth of any item is
- Depth of element
x
only increases when treeT1
that containsx
linked below other treeT2
- Occurs only when
weight(T2) >= weight(T1)
- Size of resulting tree is at least doubled
- Depth of element
x
incremented only whenweight(T2) >= weight(T1)
& resulting tree at least doubled in size
- Occurs only when
- Tree containing
x
can double in size at most times, when starting from just 1 element - Max depth starts at 0 for only 1 element (root) → increments at most times, max depth =
- Depth of element
Performance Summary
Implementation | Constructor | connect |
isConnected |
---|---|---|---|
QuickFindDS |
|||
QuickUnionDS |
|||
WeightedQuickUnionDS |
Path Compression
- When doing
isConnected(15, 10)
, tie all nodes seen to the root- Additional cost insignificant (same order of growth)
- Kinda memoization/dynamic programming?
- Path compression results in union/connected operations very close to amortized constant time
- operations on nodes =
- Inverse of super exponentiation
- Tighter bound: , where is the inverse Ackermann function