Class KendallTauDistance
- java.lang.Object
-
- org.cicirello.permutations.distance.KendallTauDistance
-
- All Implemented Interfaces:
NormalizedPermutationDistanceMeasurer
,NormalizedPermutationDistanceMeasurerDouble
,PermutationDistanceMeasurer
,PermutationDistanceMeasurerDouble
public final class KendallTauDistance extends Object
Kendall Tau Distance:Kendall Tau distance is sometimes also known as bubble sort distance, as it is the number of adjacent swaps necessary to transform one permutation into the other.
Another way of describing it is the number of pairs of elements whose order is inverted in one permutation relative to the other.
For example, consider p1 = [0, 1, 2, 3, 4] and p2 = [0, 3, 2, 1, 4]. The length is 5, so there are a total of 5*4/2 = 10 pairs of elements. 0 precedes all of 1, 2, 3, and 4 in both permutations. However, 1 precedes 2, 3, and 4 in p1, but only 4 in p2, so 2 adjacent swaps are needed for element 1. Elements 2 and 3 are in one order in p1, but switched in p2 relative to p1. So a total of 3 adjacent swaps are needed to transform p1 to p2. Kendall Tau distance is thus 3.
Kendall originally normalized the distance, but more recently many do not. Our implementation does not normalize.
Runtime: O(n lg n), where n is the permutation length. This runtime is achieved using a modified version of mergesort to count the inversions.
Kendall Tau distance originally described in:
M. G. Kendall, "A new measure of rank correlation," Biometrika, vol. 30, no. 1/2, pp. 81–93, June 1938.
-
-
Constructor Summary
Constructors Constructor Description KendallTauDistance()
Constructs the distance measurer as specified in the class documentation.
-
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].
-
-
-
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().
-
max
public int max(int length)
Description copied from interface:NormalizedPermutationDistanceMeasurer
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.
-
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().
-
-