Class Permutation.CanonicalForm
- All Implemented Interfaces:
Serializable
- Enclosing class:
Permutation
The canonical form contains complete information about a permutation in a syntactically convenient data structure that holds a sequence of sequences of integers:
- Each subsequence specifies a cycle, starting with the highest member.
- The outer sequence is ordered ascendingly by starting members.
- Trivial cycles that contain only one element are omitted.
These rules ensure that the canonical form is finite and uniquely determined.
The permutation can be reconstructed from the canonical form; the
following expression is equivalent to p:
p.canonicalForm().decompose().stream()
.reduce(Permutation.identity(), Permutation::compose)
Several algebraic operations on permutations are algorithmically easier on their canonical forms. Therefore canonical forms may be computed internally as a preliminary step.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionReturns the decomposition of this canonical form into nontrivial cycles.booleanintReturns the number of nontrivial cycles of this canonical form.inthashCode()voidshuffle(int[] seq) Permutes the elements of a sequence according to this canonical form.voidPermutes the elements of a sequence according to this canonical form.<E> voidPermutes the elements of a sequence according to this canonical form.sqrt()Finds some canonical form that composed with itself is equivalent to this canonical form.toString()
-
Method Details
-
equals
-
hashCode
public int hashCode() -
toString
-
getCycleCount
public int getCycleCount()Returns the number of nontrivial cycles of this canonical form.- Returns:
- the number of nontrivial cycles of this canonical form; the result is never negative, and zero if and only if the permutation is identity.
- See Also:
-
decompose
Returns the decomposition of this canonical form into nontrivial cycles.The resulting list contains permutations that each cover one nontrivial cycle:
cf.decompose().size() == cf.getCycleCount()p.getCycleCount() == 1, for eachPermutation p : cf.decompose()The cycles do not overlap, and are hence pairwise commutative, and together cover the whole permutation:
p.compose(q).equals(q.compose(p)), for eachPermutation p, q : cf.decompose()cf.decompose().stream().reduce(identity(), Permutation::compose).equals(Permutation.of(cf))Because cycles are commutative, their order in the result list does not matter semantically. However, for convenient processing, the order is always ascending in the highest cycle member, as specified in the class description.
- Returns:
- the decomposition of this canonical form into nontrivial cycles
- See Also:
-
shuffle
Permutes the elements of a sequence according to this canonical form.The element at each index
iis moved to indexget(i).- Parameters:
seq- the sequence to permute- Throws:
NullPointerException- ifseqis null- See Also:
-
shuffle
public void shuffle(int[] seq) Permutes the elements of a sequence according to this canonical form.The element at each index
iis moved to indexget(i).- Parameters:
seq- the sequence to permute- Throws:
NullPointerException- ifseqis null- See Also:
-
shuffle
Permutes the elements of a sequence according to this canonical form.The element at each index
iis moved to indexget(i).- Type Parameters:
E- the element type- Parameters:
seq- the sequence to permute- Throws:
NullPointerException- ifseqis null- See Also:
-
sqrt
Finds some canonical form that composed with itself is equivalent to this canonical form.The implemented algorithm is stable; for equivalent canonical forms, equivalent results are obtained always. It is also complete; if there are any solutions, one will be found. It is not unique; if there are several solutions, only a single, arbitrary one is found.
- Returns:
- some canonical form that composed with itself is equivalent to
this canonical form; or
Optional.empty()if none exists.
-