0% found this document useful (0 votes)
11 views4 pages

Union Find

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Union Find

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Partitions with Union-Find

Operations

makeSet(x): Create a singleton set containing


Union-Find Partition Structures the element x and return the position storing x
in this set
union(A,B ): Return the set A U B, destroying
the old A and B
find(p): Return the set containing the element
at position p

© 2004 Goodrich, Tamassia Union-Find 1 © 2004 Goodrich, Tamassia Union-Find 2

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).

© 2004 Goodrich, Tamassia Union-Find 3 © 2004 Goodrich, Tamassia Union-Find 4


Tree-based Implementation Union-Find Operations
5
Each element is stored in a node, which contains a To do a union, simply
pointer to a set name make the root of one tree
2

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

Each set is a tree, rooted at a node with a self- 11


referencing set pointer 9 12
For example: The sets “1”, “2”, and “5”:

1 2 5 To do a find, follow set- 5


name pointers from the 2

4 7 3 6 8 10 starting node until 8 10


reaching a node whose 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

Union-Find Heuristic 1 Union-Find Heuristic 2


Union by size: Path compression:
 After performing a find, compress all the pointers on the path
 When performing a union,
just traversed so that they all point to the root
make the root of smaller tree
point to the root of the larger 5 5 5

Implies O(n log n) time for 2 8 10 8 10


performing n union-find 8 10
operations: 3 6 11
12
11
12
2 2
 Each time we follow a pointer, 11
we are going to a subtree of 9 12 3 6 3 6
size at least double the size of
9 9
the previous subtree
 Thus, we will follow at most Implies O(n log* n) time for performing n union-find
O(log n) pointers for any find. operations:
 Proof is somewhat involved… (and not in the book)
© 2004 Goodrich, Tamassia Union-Find 7 © 2004 Goodrich, Tamassia Union-Find 8
Proof of log* n Amortized Time Proof of log* n Amortized Time (2)
For each node v that is a root For each node v with parent w:
 define n(v) to be the size of the subtree rooted at v  r (v ) > r (w )
(including v)
 identified a set with the root of its associated tree. Claim: There are at most n/ 2s nodes of rank s.
We update the size field of v each time a set is Proof:
unioned into v. Thus, if v is not a root, then n(v) is  Since r (v) < r (w), for any node v with parent w, ranks are
the largest the subtree rooted at v can be, which monotonically increasing as we follow parent pointers up
occurs just before we union v into some other node any tree.
whose size is at least as large as v ’s.  Thus, if r (v) = r (w) for two nodes v and w, then the nodes
For any node v, then, define the rank of v, which we counted in n(v) must be separate and distinct from the
denote as r (v), as r (v) = [log n(v)]: nodes counted in n(w).
Thus, n(v) ≥ 2r(v).  If a node v is of rank s, then n(v) ≥ 2s.
Therefore, since there are at most n nodes total, there can
Also, since there are at most n nodes in the tree of v,

be at most n/ 2s that are of rank s.
r (v) = [log n], for each node v.

© 2004 Goodrich, Tamassia Union-Find 9 © 2004 Goodrich, Tamassia Union-Find 10

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

You might also like