Union-Find Path Compression
Path compression can be performed in the find function of Union-Find to locate the root of a node more quickly. For a set tree, many nodes may be attached beneath its root, so during the find process we can try, from the bottom up, to move a visited node upward as much as possible if it is not the root, reducing the depth of the tree; this process is called path compression.
As shown in the figure below, the process of find(4) can apply path compression, reducing the tree's depth.

When node 4 searches upward for the root, the first compression step reduces the tree's depth by one level:

Node 2 also searches upward and is not the root either, so point element 2 to the parent of its original parent; the tree's depth is correspondingly reduced by one level, and root node 0 is returned.

The code for the find process is modified as follows:
// 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
);
// path compression 1
while
(
p
!=
parent
[
p
]
)
{
parent
[
p
]
=
parent
[
parent
[
p
]
]
;
p
=
parent
[
p
]
;
}
return
p
;
}
The path compression above is not the optimal approach; we can compress the initial tree into the form shown in the figure below, where the number of levels is minimal.

This find process is represented as:
...
// 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
);
// the second path compression algorithm
if
(
p
!=
parent
[
p
]
)
parent
[
p
]
=
find
(
parent
[
p
]
)
;
return
parent
[
p
]
;
}
...
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
;
// the second path compression algorithm
//if( p != parent[p] )
//parent[p] = find( parent[p] );
//return parent[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
}
}
}