Class ReversalDistance

  • All Implemented Interfaces:
    NormalizedPermutationDistanceMeasurer, NormalizedPermutationDistanceMeasurerDouble, PermutationDistanceMeasurer, PermutationDistanceMeasurerDouble

    public final class ReversalDistance
    extends Object
    Reversal Distance:

    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 Detail

      • 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.
        n - The length of the permutations supported.
        IllegalArgumentException - when n is greater than 12
    • Method Detail

      • distance

        public int distance​(Permutation p1,
                            Permutation p2)
        Measures the distance between two permutations.
        p1 - first permutation
        p2 - second permutation
        distance between p1 and p2
        IllegalArgumentException - if p1.length() is not equal to p2.length().
        IllegalArgumentException - if length of the permutations is not equal to the the permutation length for which this was configured at time of construction.
      • max

        public int max​(int length)
        Computes the maximum possible distance between permutations of a specified length.
        length - Permutation length.
        the maximum distance between a pair of permutations of the specified length.
        IllegalArgumentException - if length is not equal to the the permutation length for which this was configured at time of construction.