## Class ReversalDistance

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

```public final class ReversalDistance
extends Object
implements NormalizedPermutationDistanceMeasurer```
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 Summary

Constructors
Constructor Description
`ReversalDistance()`
Construct the distance measure.
`ReversalDistance​(int n)`
Defines a distance measure for permutations of length n.
• ### Method Summary

All Methods
Modifier and Type Method Description
`int` ```distance​(Permutation p1, Permutation p2)```
Measures the distance between two permutations.
`int` `max​(int length)`
Computes the maximum possible distance between permutations of a specified length.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Methods inherited from interface org.cicirello.permutations.distance.NormalizedPermutationDistanceMeasurer

`maxf, normalizedDistance`
• ### Methods inherited from interface org.cicirello.permutations.distance.PermutationDistanceMeasurer

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

• #### distance

```public int distance​(Permutation p1,
Permutation p2)```
Measures the distance between two permutations.
Specified by:
`distance` in interface `PermutationDistanceMeasurer`
Parameters:
`p1` - first permutation
`p2` - second permutation
Returns:
distance between p1 and p2
Throws:
`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.
Specified by:
`max` in interface `NormalizedPermutationDistanceMeasurer`
Parameters:
`length` - Permutation length.
Returns:
the maximum distance between a pair of permutations of the specified length.
Throws:
`IllegalArgumentException` - if length is not equal to the the permutation length for which this was configured at time of construction.