public class UnionFind extends Object
Constructor and Description |
---|
UnionFind(int size)
Creates a new Union-Find data structure with
size nodes. |
Modifier and Type | Method and Description |
---|---|
int |
find(int n)
Finds the root of the tree with node
n and does path
contraction. |
int[][] |
getComponents()
Returns the connected components of the graph.
|
boolean |
union(int k1,
int k2)
This method unions the tree that includes the node
k1 with
the tree that includes node k2 . |
public UnionFind(int size)
size
nodes.size
- the number of nodespublic int[][] getComponents()
public int find(int n)
n
and does path
contraction. The implemented path contraction is strict, that means it
hooks every node on the way to the root directly to the root.n
- the noden
public boolean union(int k1, int k2)
k1
with
the tree that includes node k2
. If both nodes are already in
the same tree nothing is done.k1
- one nodek2
- another nodetrue
if a real union is done, false
if
nothing is done