Class ReversalDistance

java.lang.Object
org.cicirello.permutations.distance.ReversalDistance
All Implemented Interfaces:
NormalizedPermutationDistanceMeasurer, NormalizedPermutationDistanceMeasurerDouble, PermutationDistanceMeasurer, PermutationDistanceMeasurerDouble

public final class ReversalDistance extends Object implements NormalizedPermutationDistanceMeasurer
Reversal Distance is the minimum number of subpermutation reversals necessary to transform one permutation into the other. This is an NP-Hard problem.

Our original motivation for implementing this required computing reversal distance from a target permutation for all permutations of that target. Therefore, we were going to exhaust all N! permutations of an N element list regardless of how we computed this.

Therefore, the approach taken in this implementation is to compute a lookup table with N! elements, one for each of the N! possible permutations. The lookup table maps permutations to distances. The distances are computed using a breadth first enumeration outward from the permutation [0, 1, ..., (N-1)]. The starting permutation is distance 0 from itself. The N*(N-1)/2 permutations that are derived by reversing a subpermutation are a distance of 1 from the initial permutation. And we continue in this manner, computing all that are a distance of 2, and then 3, etc.

The total cost of this is: O(N! * N^3) since each permutation has N^2 neighbors, generating a neighbor is linear cost, and there are N! permutations. Since our original application required computing distance for all N! permutations, the amortized cost (if your application also has that requirement) of computing distance is O(N^3).

We have not used this for N > 10. Warning: time to construct distance measure increases factorially.

  • Constructor Details

    • ReversalDistance

      public ReversalDistance()
      Construct the distance measure. Default handles permutations of length n=5.
    • ReversalDistance

      public ReversalDistance(int n)
      Defines a distance measure for permutations of length n. n must be no greater than 12.
      Parameters:
      n - The length of the permutations supported.
      Throws:
      IllegalArgumentException - when n is greater than 12
  • Method Details