Class Permutation.CanonicalForm

java.lang.Object
eu.bandm.tools.util.uni.Permutation.CanonicalForm
All Implemented Interfaces:
Serializable
Enclosing class:
Permutation

public static class Permutation.CanonicalForm extends Object implements Serializable
Canonical form of a permutation.

The canonical form contains complete information about a permutation in a syntactically convenient data structure that holds a sequence of sequences of integers:

  1. Each subsequence specifies a cycle, starting with the highest member.
  2. The outer sequence is ordered ascendingly by starting members.
  3. 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 Details

    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • 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

      public List<Permutation> 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 each Permutation 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 each Permutation 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

      public void shuffle(Object[] seq)
      Permutes the elements of a sequence according to this canonical form.

      The element at each index i is moved to index get(i).

      Parameters:
      seq - the sequence to permute
      Throws:
      NullPointerException - if seq is 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 i is moved to index get(i).

      Parameters:
      seq - the sequence to permute
      Throws:
      NullPointerException - if seq is null
      See Also:
    • shuffle

      public <E> void shuffle(List<E> seq)
      Permutes the elements of a sequence according to this canonical form.

      The element at each index i is moved to index get(i).

      Type Parameters:
      E - the element type
      Parameters:
      seq - the sequence to permute
      Throws:
      NullPointerException - if seq is 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.