The expected number of inversions--From Introduction to Algorithms by Cormen

后端 未结 5 571
北恋
北恋 2020-12-30 03:38

Let A[1 .. n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. (See Problem 2-4 for more on inv

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 04:04

    Using indicator random variables:

    1. Let X = random variable which is equal to the number of inversions.
    2. Let Xij = 1 if A[i] and A[j] form an inversion pair, and Xij = 0 otherwise.
    3. Number of inversion pairs = Sum over 1 <= i < j <= n of (Xij)
    4. Now P[Xij = 1] = P[A[i] > A[j]] = (n choose 2) / (2! * n choose 2) = 1/2
    5. E[X] = E[sum over all ij pairs such that i < j of Xij] = sum over all ij pairs such that i < j of E[Xij] = n(n - 1) / 4

提交回复
热议问题