Union-Find rank Optimization
The previous section introduced the size-based optimization of Union-Find, but in some scenarios there are still issues, as shown in the figure below, for the operation union(4,2).

According to the size optimization in the previous section, the root of the set with fewer elements points to the root of the set with more elements. After the operation, the number of levels becomes 4, one more than before, as shown below:

From this we can see that judging the direction based on the size of a set is not entirely correct. More accurately, the direction of the root should be determined based on the number of levels of the two sets: the root of the set with fewer levels points to the root of the set with more levels, as shown below. This is the rank-based optimization.

We add a rank array to the properties of the Union-Find. rank[i] represents the number of levels of the tree represented by the set rooted at i.
...
private
int
[
]
rank
;
// rank[i] represents the number of levels in the tree of the set rooted at i
private
int
[
]
parent
;
// parent[i] represents the parent node pointed to by the i-th element
private
int
count
;
// number of data items
...
The constructor is modified accordingly:
...
// Constructor
public
UnionFind4
(
int
count
)
{
rank
=
new
int
[
count
]
;
parent
=
new
int
[
count
]
;
this
.
count
=
count
;
// initialize; each parent[i] points to itself, indicating each element forms its own set
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
parent
[
i
]
=
i
;
rank
[
i
]
=
1
;
}
}
...
When merging two elements, we need to compare the number of levels of the sets at the roots. The whole process is O(h) complexity, where h is the height of the tree.
...
public
void
unionElements
(
int
p,
int
q
)
{
int
pRoot
=
find
(
p
)
;
int
qRoot
=
find
(
q
)
;
if
(
pRoot
==
qRoot
)
return
;
if
(
rank
[
pRoot
]
<
rank
[
qRoot
]
)
{
parent
[
pRoot
]
=
qRoot;
}
else
if
(
rank
[
qRoot
]
<
rank
[
pRoot
]
)
{
parent
[
qRoot
]
=
pRoot;
}
else
{
// rank[pRoot] == rank[qRoot]
parent
[
pRoot
]
=
qRoot
;
rank
[
qRoot
]
+=
1
;
// At this point, I maintain the value of rank
}
}
...
Java Example Code
Source code package download:Download
UnionFind3.java file code:
package
runoob.union
;
/**
* Optimization based on rank
*/
public
class
UnionFind4
{
private
int
[
]
rank
;
// rank[i] represents the number of levels in the tree of the set rooted at i
private
int
[
]
parent
;
// parent[i] represents the parent node pointed to by the i-th element
private
int
count
;
// number of data items
// Constructor
public
UnionFind4
(
int
count
)
{
rank
=
new
int
[
count
]
;
parent
=
new
int
[
count
]
;
this
.
count
=
count
;
// initialize; each parent[i] points to itself, indicating each element forms its own set
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
parent
[
i
]
=
i
;
rank
[
i
]
=
1
;
}
}
// search process: find the set number corresponding to element p
// O(h) complexity, where h is the height of the tree
private
int
find
(
int
p
)
{
assert
(
p
=
0
&&
p
<
count
);
// keep querying one's parent node until reaching the root
// characteristic of the root node: parent[p] == p
while
(
p
!=
parent
[
p
]
)
p
=
parent
[
p
]
;
return
p
;
}
// check whether elements p and q belong to the same set
// O(h) complexity, where h is the height of the tree
public
boolean
isConnected
(
int
p ,
int
q
)
{
return
find
(
p
)
==
find
(
q
)
;
}
// merge the sets to which elements p and q belong
// O(h) complexity, where h is the height of the tree
public
void
unionElements
(
int
p,
int
q
)
{
int
pRoot
=
find
(
p
)
;
int
qRoot
=
find
(
q
)
;
if
(
pRoot
==
qRoot
)
return
;
if
(
rank
[
pRoot
]
<
rank
[
qRoot
]
)
{
parent
[
pRoot
]
=
qRoot;
}
else
if
(
rank
[
qRoot
]
<
rank
[
pRoot
]
)
{
parent
[
qRoot
]
=
pRoot;
}
else
{
// rank[pRoot] == rank[qRoot]
parent
[
pRoot
]
=
qRoot
;
rank
[
qRoot
]
+=
1
;
// maintain the value of rank
}
}
}