Package org.jgrapht.alg.util
Class UnionFind<T>
java.lang.Object
org.jgrapht.alg.util.UnionFind<T>
An implementation of Union
Find data structure. Union Find is a disjoint-set data structure. It
supports two operations: finding the set a specific element is in, and
merging two sets. The implementation uses union by rank and path compression
to achieve an amortized cost of O(a(n)) per operation where a is the inverse
Ackermann function. UnionFind uses the hashCode and equals method of the
elements it operates on.
- Since:
- Feb 10, 2010
- Author:
- Tom Conerly
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddElement(T element) Adds a new element to the data structure in its own set.Returns the representative element of the set that element is in.voidMerges the sets which contain element1 and element2.
-
Constructor Details
-
UnionFind
Creates a UnionFind instance with all of the elements of elements in seperate sets.
-
-
Method Details
-
addElement
Adds a new element to the data structure in its own set.- Parameters:
element- The element to add.
-
getParentMap
- Returns:
- map from element to parent element
-
getRankMap
- Returns:
- map from element to rank
-
find
Returns the representative element of the set that element is in.- Parameters:
element- The element to find.- Returns:
- The element representing the set the element is in.
-
union
Merges the sets which contain element1 and element2.- Parameters:
element1- The first element to union.element2- The second element to union.
-