Weighted Quick Union with Path Compression
Weighted Quick Union is pretty good, but we can do even better!
The clever insight is realizing that whenever we call
find(x) we have to traverse the path from
x to root. So, along the way we can connect all the items we visit to their root at no extra asymptotic cost.
Connecting all the items along the way to the root will help make our tree shorter with each call to
Recall that both
connect(x, y) and
isConnected(x, y) always call
find(y). Thus, after calling
isConnected enough, essentially all elements will point directly to their root.
By extension, the average runtime of
isConnected becomes almost constant in the long term! This is called the amortized runtime (from amortized analysis, ch. 8.4).
N: number of elements in Disjoint Set
|Weighted Quick Union (WQU)||O(log N)||O(log N)|
|WQU with Path Compression||O(α(N))*||O(α(N))*|
*behaves as constant in long term.
Code? This is your lab 6!
1. Students interested in understanding where the iterated logarithm comes from can read this proof or page 9 from these 170 notes. Path compression is actually even better than iterated log - it's bounded by the inverse Ackermann function α which is comically out of scope. ↩