next up previous
Next: Up: Previous:

Weighted-Union Heuristic

Idea: Keep track of the number of objects in a set (length of list). Append shorter list to longer list.

Theorem 22.1

A sequence of m operations, n of which are Make-Set operations, takes O(m + n lg n) time.



Proof: Since we only change rep(x) for objects in the shorter list for each Union, and lists start at length=1, then each Union at least doubles the size of x's list. Thus, we can do at most \(\lceil lg n \rceil\) Unions that require rep(x) changes, and there are n objects.

As a result, there are a total of O(n lg n) changes.

If we add the O(1) costs for the O(m) Make-Set and Find-Set operations, we get O(m + nlgn).


next up previous
Next: Up: Previous: