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

Union-Find Quick Union

Union-Find Quick Union

For a set of data, Union-Find mainly supports two operations:

In the previous section, we used id an array to represent Union-Find; in actual operation the time complexity of finding is O(1), but the efficiency of connecting is not high.

In this section, we will implement Union-Find in another way. Treat each element as a node that points to its parent, with the root pointing to itself. As shown in the figure below, node 3 points to node 2, indicating that 3 and 2 are connected; node 2 is itself the root, so it points to itself.

Union-Find is also represented by an array, but here the following set of elements uses parent to represent the parent node that the current element points to; each element points to itself and is independent.

If the operation union(4,3) is performed at this point, pointing element 4 to element 3:

The array changes accordingly:

To determine whether two elements are connected, we only need to check whether their root nodes are the same.

As shown in the figure, the roots of node 4 and node 9 are both 8, so they are connected.

To connect two elements, we only need to find their corresponding root nodes and connect the roots; then they are connected nodes.

Suppose we want to connect 6 and 4 in the figure above; we only need to point 6's root node 5 to 4's root node 8.

To build this tree structure that points to parents, use an array to construct a tree pointing to parent nodes, where parent[i] represents the parent node that element i points to.

...

private

int

[

]

parent

;

private

int

count

;

 

// number of data items

...

The find process: find the set number corresponding to element p, continuously querying one's parent node until reaching the root; the root's characteristic is parent[p] == p, with O(h) complexity, where h is the height of the tree.

...

private

int

find

(

int

p

)

{

   

assert

(

p

=

0

&&

p

<

count

)

;

   

while

(

p

!=

parent

[

p

]

)

        p

=

parent

[

p

]

;

   

return

p

;

}

...

To merge the sets to which elements p and q belong, query the root of each element separately and make one root point to the other; the two sets are then merged. This operation has O(h) time 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

;

    parent

[

pRoot

]

=

qRoot

;

}

Java Example Code

Source code package download:Download

UnionFind2.java file code:

package

runoob.union

;

/**
 * Second version of unionFind
 */

public

class

UnionFind2

{

   

// our second version of Union-Find, using an array to build a tree that points to parent nodes

   

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

   

private

int

[

]

parent

;

   

private

int

count

;

 

// number of data items

   

// Constructor

   

public

UnionFind2

(

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

;

   

}

   

// 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

;

        parent

[

pRoot

]

=

qRoot

;

   

}

}