Package net.cscott.jutil
Class DisjointSet<E>
java.lang.Object
net.cscott.jutil.DisjointSet<E>
DisjointSet is an implementation of disjoint-set forests
using the path compression and union-by-rank heuristics to achieve
O(m * alpha(m, n)) runtime, where 'm' is the total number of
operations, 'n' is the total number of elements in the set, and
'alpha' denotes the *extremely* slowly-growing inverse Ackermann
function.- Author:
- C. Scott Ananian <cananian@alumni.princeton.edu>
- Tests:
- {@link #union union()} merges the sets of two elements, and {@link #find find()} returns the representative of an element's set, which is the same for every element of the set:
js> ds = new DisjointSet() {} js> ds.union("a", "b"); ds.union("c", "d"); ds.union("b", "c") js> ds.find("a") == ds.find("d") true js> ds.find("e") // an element in no union is its own set e js> ds.find("e") == ds.find("a") false
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasMap()Returns an unmodifiableMapview of the disjoint set, where every element is mapped to its canonical representative.booleanDetermines if there is a set of more than one element containingo.Returns the representative of the (unique) set containingo.toString()Returns a human-readable representation of the DisjointSet.voidUnites the dynamic sets that containo1ando2, say S1 and S2, into a new set that is the union of these two sets.
-
Constructor Details
-
DisjointSet
public DisjointSet()Creates aDisjointSet.
-
-
Method Details
-
union
Unites the dynamic sets that containo1ando2, say S1 and S2, into a new set that is the union of these two sets. The two sets are assumed to be disjoint prior to the operation. The representative of the resulting set is the representative of either S1 or S2; if both S1 and S2 were previously singletons, the representative of S1 union S2 is the representative of S2. -
find
Returns the representative of the (unique) set containingo. -
contains
Determines if there is a set of more than one element containingo. -
asMap
Returns an unmodifiableMapview of the disjoint set, where every element is mapped to its canonical representative. -
toString
Returns a human-readable representation of the DisjointSet.
-