public final class ReversalDistance extends Object
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 and Description |
|---|
ReversalDistance()
Construct the distance measure.
|
ReversalDistance(int n)
Defines a distance measure for permutations of length n.
|
| Modifier and Type | Method and Description |
|---|---|
int |
distance(Permutation p1,
Permutation p2)
Measures the distance between two permutations.
|
double |
distancef(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.
|
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].
|
public ReversalDistance()
public ReversalDistance(int n)
n - The length of the permutations supported.IllegalArgumentException - when n is greater than 12public int distance(Permutation p1, Permutation p2)
p1 - first permutationp2 - second permutationpublic int max(int length)
length - Permutation length.public final double distancef(Permutation p1, Permutation p2)
distancef in interface PermutationDistanceMeasurerDoublep1 - first permutationp2 - second permutationpublic final double maxf(int length)
maxf in interface NormalizedPermutationDistanceMeasurerDoublelength - Permutation length.public final double normalizedDistance(Permutation p1, Permutation p2)
Measures the distance between two permutations, normalized to the interval [0.0, 1.0].
normalizedDistance in interface NormalizedPermutationDistanceMeasurerDoublep1 - first permutationp2 - second permutationCopyright © 2005-2020 Vincent A. Cicirello. All rights reserved.