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

后端 未结 5 561
北恋
北恋 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:17

    All the solutions seem to be correct, but the problem says that we should use indicator random variables. So here is my solution using the same:

        Let Eij be the event that i < j and A[i] > A[j].
    
        Let Xij = I{Eij} = {1 if (i, j) is an inversion of A
    
                            0 if (i, j) is not an inversion of A}
    
        Let X = Σ(i=1 to n)Σ(j=1 to n)(Xij) = No. of inversions of A.
    
        E[X] = E[Σ(i=1 to n)Σ(j=1 to n)(Xij)]
    
             = Σ(i=1 to n)Σ(j=1 to n)(E[Xij])
    
             = Σ(i=1 to n)Σ(j=1 to n)(P(Eij))
    
             = Σ(i=1 to n)Σ(j=i + 1 to n)(P(Eij)) (as we must have i < j)
    
             = Σ(i=1 to n)Σ(j=i + 1 to n)(1/2) (we can choose the two numbers in
                                                C(n, 2) ways and arrange them
                                                as required. So P(Eij) = C(n, 2) / n(n-1))
    
             = Σ(i=1 to n)((n - i)/2)
    
             = n(n - 1)/4
    

提交回复
热议问题