E
- type of elements in setpublic class UnionFind<E>
extends java.lang.Object
http://algs4.cs.princeton.edu/15uf/UF.java.html
,
https://www.cs.princeton.edu/~rs/AlgsDS07/01UnionFind.pdf
Constructor and Description |
---|
UnionFind() |
Modifier and Type | Method and Description |
---|---|
void |
add(E p)
Add element p to the dataset
|
boolean |
connected(E p,
E q)
Returns true if the the two sites are in the same component.
|
int |
count()
Returns the number of components.
|
E |
find(E p)
Returns the component identifier for the component containing site
p . |
java.util.Collection<java.util.Collection<E>> |
getComponents()
Get all components
|
void |
union(E p,
E q)
Merges the component containing site
p with the
the component containing site q . |
public void add(E p)
p
- public java.util.Collection<java.util.Collection<E>> getComponents()
public E find(E p)
p
.p
- the integer representing one sitep
java.lang.IllegalArgumentException
- unless 0 <= p < n
public int count()
1
and n
)public boolean connected(E p, E q)
p
- the integer representing one siteq
- the integer representing the other sitetrue
if the two sites p
and q
are in the same component;
false
otherwisejava.lang.IllegalArgumentException
- unless
both 0 <= p < n
and 0 <= q < n