ObjectUF
public class UF
The UF class represents a union-find data data structure. It supports the union and find operations, along with a method for determining the number of disjoint sets.
This implementation uses weighted quick union. Creating a data structure with N objects takes linear time. Afterwards, all operations are logarithmic worst-case time.
For additional documentation, see Section 1.5 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
| Constructor Summary | |
|---|---|
UF(int N)
Create an empty union find data structure with N isolated sets. |
|
| Method Summary | |
|---|---|
boolean |
connected(int p,
int q)
Are objects p and q in the same set? |
int |
count()
Return the number of disjoint sets. |
int |
find(int p)
Return the id of component corresponding to object p. |
static void |
main(String[] args)
|
void |
union(int p,
int q)
Replace sets containing p and q with their union. |
| Methods inherited from class Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public UF(int N)
| Method Detail |
|---|
public int find(int p)
public int count()
public boolean connected(int p,
int q)
public void union(int p,
int q)
public static void main(String[] args)