Union Find
Union Find
Operations
Analysis of List-based
List-based Implementation Representation
Each set is stored in a sequence represented When doing a union, always move
with a linked-list elements from the smaller set to the
Each node should store an object containing larger set
the element and a reference to the set name Each time an element is moved it goes to a
set of size at least double its old set
Thus, an element can be moved at most
O(log n) times
Total time needed to do n unions and
finds is O(n log n).
A node v whose set pointer points back to v is also a point to the root of the
8 10
set name other
3 6
9 11
set-name pointer refers 11
back to itself 9 12
12
© 2004 Goodrich, Tamassia Union-Find 5 © 2004 Goodrich, Tamassia Union-Find 6
Proof of log* n Amortized Time (3) Proof of log* n Amortized Time (4)
Definition: Tower of two’s function: Charge 1 cyber-dollar per pointer hop during
t(i) = 2t(i-1) a find:
If w is the root or if w is in a different rank group
Nodes v and u are in the same rank than v, then charge the find operation one cyber-
group g if dollar.
g = log*(r(v)) = log*(r(u)): Otherwise (w is not a root and v and w are in the
same rank group), charge the node v one cyber-
Since the largest rank is log n, the dollar.
largest rank group is Since there are most (log* n)-1 rank groups,
log*(log n) = (log* n) - 1 this rule guarantees that any find operation is
charged at most log* n cyber-dollars.
© 2004 Goodrich, Tamassia Union-Find 11 © 2004 Goodrich, Tamassia Union-Find 12
Proof of log* n Amortized Time (5) Proof of log* n Amortized Time (end)
After we charge a node v then v will get a new
parent, which is a node higher up in v ’s tree. Bounding n(g): Returning to C:
The rank of v ’s new parent will be greater than the t(g)
n log*n −1
rank of v ’s old parent w. n( g ) ≤ ∑
s = t ( g −1) +1 2
s C< ∑
n
⋅ (t ( g ) − t ( g − 1))
Thus, any node v can be charged at most the g =1 t(g)
t ( g ) −t ( g −1) −1
number of different ranks that are in v ’s rank group. =
n
∑
1 log*n −1
n
If v is in rank group g > 0, then v can be charged at 2t ( g −1) +1 s =0 2s ≤ ∑g =1 t(g)
⋅ t(g)
most t(g)-t(g-1) times before v has a parent in a n
< t ( g −1) +1 ⋅ 2 log*n −1
higher rank group (and from that point on, v will
never be charged again). In other words, the total
2
n
= ∑n
g =1
number, C, of cyber-dollars that can ever be charged = t ( g −1)
2 ≤ n log* n
to nodes can be bounded by n
log*n −1
=
C≤ ∑ n( g ) ⋅ (t ( g ) − t ( g − 1))
g =1
t(g)
© 2004 Goodrich, Tamassia Union-Find 13 © 2004 Goodrich, Tamassia Union-Find 14