Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Union-Find size Optimization

Union-Find size Optimization

Following the idea in the previous section, we perform the operation union(4,9) on the Union-Find shown below.

The structure after the merge operation is:

It can be found that the tree of this structure is relatively tall. If the number of elements increases at this point, the cost incurred will be relatively large. Solving this problem is actually simple: make a judgment before performing the actual pointing operation, and point the root of the set with fewer elements to the root of the set with more elements, which can generate a tree with fewer levels with higher probability.

When constructing the Union-Find, one more parameter is needed,sz an array,sz[i] representing the number of elements in the set rooted at i i.

// Constructor

public

UnionFind3

(

int

count

)

{

    parent

=

new

int

[

count

]

;

    sz

=

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

;

        sz

[

i

]

=

1

;

   

}

}

When performing a merge operation, the merge direction is determined based on the different numbers of elements in the trees where the two elements reside.

public

void

unionElements

(

int

p,

int

q

)

{

   

int

pRoot

=

find

(

p

)

;

   

int

qRoot

=

find

(

q

)

;

   

if

(

pRoot

==

qRoot

)

       

return

;

   

if

(

sz

[

pRoot

]

<

sz

[

qRoot

]

)

{

parent

[

pRoot

]

=

qRoot

;

        sz

[

qRoot

]

+=

sz

[

pRoot

]

;

   

}

   

else

{

        parent

[

qRoot

]

=

pRoot

;

        sz

[

pRoot

]

+=

sz

[

qRoot

]

;

   

}

}

After optimization, the merge result is as follows, with 9 pointing to parent node 8.

Java Example Code

Source code package download:Download

UnionFind3.java file code:

package

runoob.union

;

/**
 * Union-Find size Optimization
 */

public

class

UnionFind3

{

   

// parent[i] represents the parent node pointed to by the first element

   

private

int

[

]

parent

;

   

// sz[i] represents the number of elements in the set rooted at i

   

private

int

[

]

sz

;

   

// number of data items

   

private

int

count

;

   

// Constructor

   

public

UnionFind3

(

int

count

)

{

        parent

=

new

int

[

count

]

;

        sz

=

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

;

            sz

[

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

;

       

// The merge direction is determined based on the different numbers of elements in the trees where the two elements reside

       

// Merge the set with fewer elements into the set with more elements

       

if

(

sz

[

pRoot

]

<

sz

[

qRoot

]

)

{

parent

[

pRoot

]

=

qRoot

;

            sz

[

qRoot

]

+=

sz

[

pRoot

]

;

       

}

       

else

{

            parent

[

qRoot

]

=

pRoot

;

            sz

[

pRoot

]

+=

sz

[

qRoot

]

;

       

}

   

}

}