These are part of my notes for the Coursera course: Algorithms, Pt I (Princeton).
The QuickUnion algorithm is explained here.
##QuickUnion Path Compression
QuickUnion mostly takes time when finding the roots. While finding the root of a point, we may touch multiple nodes on the way to the root.
If we make all of these nodes (including our initial point) connect to the root once we find it, it will save time later on.
In this way, we make QuickUnion behave a bit more like QuickFind (where all points in a tree point to the root).
This allows us to reduce root-finding and thus speeds up connection checks.
So we still keep the fast union property of QuickUnion, while avoiding the large number id-array changes that plagues QuickFind;
We only optimize the tree when root-finding - so an an "as-needed" basis.