YouTip LogoYouTip

Union Find Compress

Union-Find Path Compression \n\n

Union-Find Path Compression

\n\n

The find function in a Union-Find data structure can incorporate path compression to speed up the process of finding the root node of an element. In a tree representing a set, the root node can have many nodes attached below it. Therefore, during the find operation, we can attempt to move a node upwards as much as possible, from bottom to top, if it is not the root node. This reduces the number of levels in the tree, and this process is called path compression.

\n\n

As shown in the diagram below, the find(4) process can be path-compressed to reduce the tree's height.

\n\n

Image 1

\n\n

When node 4 searches upwards for the root node, the first step of compression reduces the tree's height by one level:

\n\n

Image 2

\n\n

Node 2 then searches upwards and is also not the root node. So, we point element 2 to its original grandparent (the parent of its parent). After this operation, the tree's height is reduced by one level, and the root node 0 is returned.

\n\n

Image 3

\n\n

The find process code is modified to:

\n\n
// Find process, Find the set number corresponding to element p\n\n// O(h)Complexity, hIs the height of the tree\n\nprivate int find(int p){\n\n    assert( p >=0&& p < count );\n\n    // path compression 1\n\n    while( p != parent){\n\n        parent= parent[parent];\n\n        p = parent;\n\n    }\n\n    return p;\n\n}
\n\n

The path compression described above is not the most optimal method. We can compress the initial tree into the form shown below, which has the minimum possible height.

\n\n

Image 4

\n\n

This find process can be represented as:

\n\n
...\n\n// Find process, Find the set number corresponding to element p\n\n// O(h)Complexity, hIs the height of the tree\n\nprivate int find(int p){\n\n    assert(p >=0&& p < count);\n\n    //The second path compression algorithm\n\n    if(p != parent)\n\n        parent= find(parent);\n\n    return parent;\n\n}\n\n...
\n\n

Java Example Code

\n\n

Source Package Download: Download

\n\n

UnionFind3.java File Code:

\n\n
package .union;\n\n/**\n\n * Optimization based on rank\n\n */\n\npublic class UnionFind4 {\n\n    private int[] rank;// rankRepresents the number of layers in the tree represented by the set with i as the root\n\n    private int[] parent;// parentRepresents the parent node pointed to by the i-th element\n\n    private int count;// Number of data elements\n\n    // Constructor\n\n    public UnionFind4(int count){\n\n        rank =new int;\n\n        parent =new int;\n\n        this.count= count;\n\n        // Initialization, Each parentPoints to itself, Indicates that each element forms a set by itself\n\n        for(int i =0; i =0&& p < count );\n\n        // Continuously query its own parent node, Until reaching the root node\n\n        // Characteristics of the root node: parent == p\n\n        while( p != parent)\n\n            p = parent;\n\n        return p;\n\n        //The second path compression algorithm\n\n        //if( p != parent )\n\n        //    parent = find( parent );\n\n        //return parent;\n\n    }\n\n    // Check if element p and element q belong to the same set\n\n    // O(h)Complexity, hIs the height of the tree\n\n    public boolean isConnected(int p , int q ){\n\n        return find(p)== find(q);\n\n    }\n\n    // Merge the sets containing element p and element q\n\n    // O(h)Complexity, hIs the height of the tree\n\n    public void unionElements(int p, int q){\n\n        int pRoot = find(p);\n\n        int qRoot = find(q);\n\n        if( pRoot == qRoot )\n\n            return;\n\n        if( rank< rank){\n\n            parent= qRoot;\n\n        }\n\n        else if( rank< rank){\n\n            parent= pRoot;\n\n        }\n\n        else{// rank == rank\n\n            parent= qRoot;\n\n            rank+=1;// Maintain the value of rank\n\n        }\n\n    }\n\n}
← Graph TheoryPython3 Mysql β†’