让A [1 .. N]为N 不同的数字数组。如果I< j和A [1]> A [J],则对(I,J)被称为A的逆(见问题2-4更多的倒置。)假设A的每个元素是随机选择,独立并均匀从范围1至n。使用指标随机变量来计算反转的预期数量。
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 inversions.) Suppose that each element of A is chosen randomly, independently, and uniformly from the range 1 through n. Use indicator random variables to compute the expected number of inversions.
现在的问题是,从运动5.2-5中的算法导论的由Cormen。这是我的递归解决方案:
The problem is from exercise 5.2-5 in Introduction to Algorithms by Cormen. Here is my recursive solution:
假定x(i)是反转的在[1..i],和E(ⅰ)的数量为x的(ⅰ)的预期值,则E(i + 1的)可以计算如下: 图片我们有 I + 1 位置把所有的数字,如果我们把的 I + 1 的第一个位置,那么x(I + 1 )= I + X(ⅰ);如果我们把的 i + 1的的在第二位置,则x第(i + 1)= I-1 + X(i)中,...,所以E(i + 1的)= 1 /( I + 1)*和(k)的+ E(i)中,其中,k = [0,1]。最终我们得到E(I + 1)= I / 2 + E(I)。 因为我们知道,E(2)= 0.5,所以递归我们得到:E(N)=(N-1 + N-2 + ... + 2)/ 2 + 0.5 = N *(N-1)/ 4
Suppose x(i) is the number of inversions in a[1..i], and E(i) is the expected value of x(i), then E(i+1) can be computed as following: Image we have i+1 positions to place all the numbers, if we place i+1 on the first position, then x(i+1) = i + x(i); if we place i+1 on the second position, then x(i+1) = i-1 + x(i),..., so E(i+1) = 1/(i+1)* sum(k) + E(i), where k = [0,i]. Finally we get E(i+1) = i/2 + E(i). Because we know that E(2) = 0.5, so recursively we get: E(n) = (n-1 + n-2 + ... + 2)/2 + 0.5 = n* (n-1)/4.
虽然扣除上述似乎是正确的,但我仍然不是很确信这一点。所以,我在这里分享。
Although the deduction above seems to be right, but I am still not very sure of that. So I share it here.
如果有什么不对,请大家指正。
If there is something wrong, please correct me.
推荐答案我认为这是正确的,但我认为正确的方式来证明它是使用conditionnal预期:
I think it's right, but I think the proper way to prove it is to use conditionnal expectations :
对于所有的X和Y,我们有:E [X] = E [E [X | Y]]
for all X and Y we have : E[X] =E [E [X|Y]]
然后在您的情况:
E(I + 1)= E [X(I + 1)] = E [E [X(I + 1)| X(ⅰ)] = E [SUM(K)/(1 + I)+ X(I)] = I / 2 + E [X(ⅰ)] = I / 2 + E(ⅰ)
E(i+1) = E[x(i+1)] = E[E[x(i+1) | x(i)]] = E[SUM(k)/(1+i) + x(i)] = i/2 + E[x(i)] = i/2 + E(i)
有关第二条语句:
如果:
E(N)= N *(N-1)/ 4。
E(n) = n* (n-1)/4.
则E(N + 1)=(N + 1)* N / 4 =(N-1)* N / 4 + 2 * N / 4 =(N-1)* N / 4 + N / 2 = E(N)+ N / 2
then E(n+1) = (n+1)*n/4 = (n-1)*n/4 + 2*n/4 = (n-1)*n/4 + n/2 = E(n) +n/2
所以,N *(N-1)/ 4。验证对所有的n> = 2的递推关系,并验证其在n = 2
So n* (n-1)/4. verify the recursion relation for all n >=2 and it verifies it for n=2
那么E(N)= N *(N-1)/ 4
So E(n) = n*(n-1)/4
希望我理解你的问题,它可以帮助
Hope I understood your problem and it helps