Class ReversalDistance
- java.lang.Object
-
- org.cicirello.permutations.distance.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 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 Instance Methods Concrete Methods Modifier and Type Method Description int
distance(Permutation p1, Permutation p2)
Measures the distance between two permutations.double
distancef(Permutation p1, Permutation p2)
Measures the distance between two permutationsint
max(int length)
Computes the maximum possible distance between permutations of a specified length.double
maxf(int length)
Computes the maximum possible distance between permutations of a specified length.double
normalizedDistance(Permutation p1, Permutation p2)
Measures the distance between two permutations, normalized to the interval [0.0, 1.0].
-
-
-
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.- Parameters:
p1
- first permutationp2
- 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.- 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.
-
distancef
public final double distancef(Permutation p1, Permutation p2)
Measures the distance between two permutations- Specified by:
distancef
in interfacePermutationDistanceMeasurerDouble
- Parameters:
p1
- first permutationp2
- second permutation- Returns:
- distance between p1 and p2
- Throws:
IllegalArgumentException
- if p1.length() is not equal to p2.length().
-
maxf
public final double maxf(int length)
Description copied from interface:NormalizedPermutationDistanceMeasurerDouble
Computes the maximum possible distance between permutations of a specified length.- Specified by:
maxf
in interfaceNormalizedPermutationDistanceMeasurerDouble
- Parameters:
length
- Permutation length.- Returns:
- the maximum distance between a pair of permutations of the specified length.
-
normalizedDistance
public final double normalizedDistance(Permutation p1, Permutation p2)
Measures the distance between two permutations, normalized to the interval [0.0, 1.0].
- Specified by:
normalizedDistance
in interfaceNormalizedPermutationDistanceMeasurerDouble
- Parameters:
p1
- first permutationp2
- second permutation- Returns:
- distance between p1 and p2 normalized to the interval [0.0, 1.0]
- Throws:
IllegalArgumentException
- if p1.length() is not equal to p2.length().
-
-