Kendall Tau Distance: Find the number of reverse order between two permutations

Source: Internet
Author: User
Definition of Kendall tau distance

The following definitions are taken from the Wiki encyclopedia Kendall tau Distance:

The Kendall tau rank distance is a metric that counts the number of pairwise disagreements between the ranking lists. The larger the distance, the more dissimilar the lists is.

That is, the Kendall tau distance is the number of reverse order between the two permutations, which reflects the similarity of the two permutations. For example two permutations in the interval [0, 6]:

A = {0, 3, 1, 6, 2, 5, 4}
b = {1, 0, 3, 6, 4, 2, 5}

The Kendall tau distance for a, B, is to find two permutations between the reverse {0,1},{3,1},{2,4},{}, altogether 4 pairs, so 5,4 tau distance of 4. The method of finding the Kendall tau distance

As can be seen from the above example, the number of reverse order between the two permutations can be considered as a standard, and B arranges its own reverse order number. In order to arrange the criteria in a, first we need to extract the index of a arranged into the new permutation aindex, i.e. aindex[a[i] = I. The index of B is then determined by the index of a, i.e. bindex[i] = aindex[b[i]]. Thus the inverse number of the index in the Bindex is the number of reverse order between A and B.
A special case is: a[i] = I natural sequence, then aindex[i] = i, bindex[i] = b[i], then A is arranged by the standard, B is arranged in reverse order number of the number of itself B. Find the number of reverse order in a permutation

The

First considers the simple squared order algorithm. In an insert sort or bubble sort, the number of elements exchanged is equal to the number of reverse order of the permutation, so the number of exchanges is counted during sorting. But this method is less efficient.  
more efficient methods inspire self-efficient sorting algorithms, such as merge ordering, which can make the algorithm linear to the order of magnitude. When merging two ordered permutations, the first element of the first sub-array is less than the first element of the second sub-array, the reverse number is 0, and conversely, the inverse number is the number of the current element in the previous sub-array. The implementation of Kendall tau distance is obtained

public class Kendalltau {private static long counter = 0; public static long Distance (int[] A, int[] b) {if (a.length! = b.length) {throw new Illegalargumente
        Xception ("Array dimensions Disagree");
        } int N = A.length;
        int[] Aindex = new int[n];//records the index of an array for (int i = 0; i < N; i++) {Aindex[a[i]] = i; } int[] Bindex = new int[n];//b array referencing the index of a array for (int i = 0; i < N; i++) {Bindex[i] = Aindex
        [B[i]];
    } return Mergecount (Bindex);
            }//Use the Insert Sort method to find the inverse number public static long Insertioncount (int[] a) {for (int i = 1; i < a.length; i++) {
                for (int j = i; j > 0 && a[j] < a[j-1]; j--) {int temp = a[j];
                A[J] = a[j-1];
                A[J-1] = temp;
    counter++;//Insert sort once per interchange, there is a pair of reverse order}} return counter; }//Using the merge Sort method to find the inverse number private Static int[] aux;
        public static long Mergecount (int[] a) {aux = new int[a.length];
        MergeSort (A, 0, a.length-1);
    return counter;
        } private static void MergeSort (int[] A, int lo, int hi) {if (Hi <= lo) {return;
        } int mid = lo + (Hi-lo)/2;
        MergeSort (A, lo, mid);
        MergeSort (A, mid + 1, HI);
    Merge (A, lo, Mid, hi);
        public static void merge (int[] A, int lo, int mid, int hi) {int i = lo, j = mid + 1;
        for (int k = lo; k <= hi; k++) {aux[k] = a[k];
            } for (int k = lo; k <= hi; k++) {if (i > Mid) {a[k] = aux[j++];
            } else if (J > Hi) {a[k] = aux[i++];
                } else if (Aux[j] < Aux[i]) {a[k] = aux[j++];
            Counter + = Mid-i + 1;//Each sub-array element smaller than the former sub-array, the inverse number is the existing length of the former Subarray} else {a[k] = aux[i++];
    }    }} public static void Main (string[] args) {int[] a = new int[] {0, 3, 1, 6, 2, 5, 4};
        Int[] B = new int[] {1, 0, 3, 6, 4, 2, 5};
        for (int i = 0; i < a.length; i++) {System.out.println (A[i] + "" + b[i]);
    } System.out.println ("Inversions:" + distance (A, b)); }} Original

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.