9

Consider this problem:

Let's define a list of numbers {1, 2, ..., n} in the right order. Let's make a random permutation by swapping numbers randomly. Take k random pairs of different elements and swap them.

For example with k = 1 I might get the following list {2, 1, ..., n} or {1, n, ..., 2}, etc ...

Compute the expected number of numbers that are at the right place (in the two examples above, only n-2 numbers are at the right place).

The edge cases are:

  • If $ k \rightarrow \infty $, then we expect 1 number at the right place (the shuffling will be perfect so we have 1/n chance per position, so n/n chance in total). I am not sure this proof is good because I assume everything is independent by summing right? and it's definitely not independent (if n-1 first numbers are at the right place, the last one doesn't have 1/n probability to be at the right place, it has 1 probability to be at the right place). This has been proven as false (with the counter example n = 2 it's easy to see why). Thank you Calvin Lin.
  • If $k = 0$, we expect n.
  • If $k = 1$, we expect n-2.

I tried simulating some stuff with k = 30000 and k = 70001 for n = 10000 in Python:

import numpy as np

def count_fixed_points(perm): return sum(1 for i, x in enumerate(perm, 1) if i == x)

def random_permutation(n, num_swaps): perm = list(range(1, n+1)) for _ in range(num_swaps): i, j = np.random.choice(n, 2, replace=False) perm[i], perm[j] = perm[j], perm[i] return perm

n = 10000

perm30000 = random_permutation(n, 30000) fixed_points_30000 = count_fixed_points(perm30000)

perm70000 = random_permutation(n, 70000) fixed_points_70000 = count_fixed_points(perm70000)

fixed_points_30000, fixed_points_70000

I get (23, 1), (31, 2), and (23, 0) on three runs which are consistent with intuition (the more with swaps, the smaller the amount of fixed points we will get).

Can we easily compute the expected value? Is it possible with not-too-advanced stuff (undergrad level proof let's say).

RobPratt
  • 50,938
  • Recursive ideas seem encouraging. Look at the first $k-1$ swaps, recursively we know the number of fixed points. Then the final swap reduces the number of fixed points except in the cases where the next swap correctly places two elements of the non-fixed set. It's still messy, I expect. – lulu Aug 29 '24 at 14:32
  • I'm not too sure I understand, I was looking for a general answer, involving the expectation operator of a random variable, not how to count in a specific case where we know the swaps in advance. If I misunderstood your message, sorry. – FluidMechanics Potential Flows Aug 29 '24 at 14:35
  • You misunderstood. I am proposing a recursive method for computing the expected number. You don't need to know the form of the fixed set under the first $k-1$ swaps,. you only need to know the expected value (which, recursively, we know). Of course, you would prefer a simple closed formula, but I don't see any reason to imagine that there is one. The recursive method (messy or not) is worth implementing in any case as it will at least give you a lot of numerical values you can use to test a proposed formula or asymptotic. – lulu Aug 29 '24 at 14:39
  • @lulu I believe you need to know the form of the set, and not just the distribution. EG Say we have a distribution with 4 non-fixed points, what's the expected value of the number of non-fixed points after a swap? It would depend on whether we have $(abcd)$ or $(ab)(cd)$. – Calvin Lin Aug 29 '24 at 15:08
  • @CalvinLin Oh, absolutely. My error. That would seem to make the problem a lot harder though. – lulu Aug 29 '24 at 15:11
  • @FluidMechanicsPotentialFlows To stress : the point CalvinLin makes invalidates my recursive method in a strong way. That is, there's no obvious way to put a distribution on the cycle structure for the complement so there's no cheap repair. – lulu Aug 29 '24 at 15:12
  • @FluidMechanicsPotentialFlows FYI Your edge case of $ k \rightarrow \infty$ ignores the parity of the swap. Instead of $S_n$, we have to look at the action of $A_n$. EG In the case of $n = 2$, we have $k$ odd gives 0 fixed points (always), $k$ even gives 2 fixed points. $\quad$ Interestingly, for $n = 3$, both $k$ odd or even leads to an expected 1 fixed point. So maybe that added flexibility is sufficient to push through an $A_n$ argument like what you had. – Calvin Lin Aug 29 '24 at 15:20
  • I'm not too sure I'm familiar with the $S_n$ and $A_n$ notations you use @CalvinLin – FluidMechanics Potential Flows Aug 29 '24 at 15:21
  • The counter example of my k→∞ with n = 2 is very valid and totally invalidates my initial intuition, thanks – FluidMechanics Potential Flows Aug 29 '24 at 15:23
  • $S_n$ is the group of all permutations. $A_n$ is the alternating group of permutations, which are those with even parity. – Calvin Lin Aug 29 '24 at 15:29
  • For $n=3$, the only odd permutations are $(12), (23), (13)$, which all have 1 fixed point. As such, we can conclude that when $n \rightarrow \infty$, the even permutations also only have 1 fixed point on average. Note that even though the even permutations are $(123), (132), \emptyset$, we don't necessarily know what their distribution would be after $k$ swaps (though this analysis suggests that it approaches $(1/3, 1/3, 1/3)$. $\quad$ For this reason, I didn't try to calculate what happens with $n = 4$. Can you check your code to see? – Calvin Lin Aug 29 '24 at 15:33
  • @CalvinLin with n = 4, with 7000 permutations (which should be enough for k→∞), running it 100 times gives me those frequencies: 1: 65, 0: 31, 4: 4 – FluidMechanics Potential Flows Aug 29 '24 at 15:39
  • And with 7001 permutations I get 0: 53, 2: 47 – FluidMechanics Potential Flows Aug 29 '24 at 15:40

1 Answers1

12

Let $P(k)$ be the probability that a given number returns to its position after $k$ swaps. Then $P(0) = 1$, and for $k \geq 0$, \begin{aligned} P(k + 1) &= \frac{n-2}{n} P(k) + \frac{2}{n^2-n} \big(1 - P(k)\big) \\ &= \frac{2}{n^2-n} + \left(\frac{n-2}{n}-\frac{2}{n^2-n}\right)P(k). \end{aligned} This recurrence is solved by $$ P(k) = \frac{1}{n} \left[1 + \left(1-\frac{2}{n-1}\right)^{k-1} (n- 3)\right]. $$ Hence $$ E(k) = 1 + \left(1-\frac{2}{n-1}\right)^{k-1} (n-3) . $$

  • To check, can you elaborate on the first equation? It seems to me that you're considering $k$ swaps involving that number. $\quad$ Also, for $k = 0, 1$ and $n$ large, $E(0) > n$, E(1) >> 2$. which doesn't make sense to me. – Calvin Lin Aug 29 '24 at 15:37
  • $E(1)$ should be equal to $n - 2$, which should be greater than two. The first equation says that if the given number is in its original position, then the next swap shouldn't disturb it (probability $\frac{n-2}{n}$). Otherwise, the next swap should return it to its original position (probability $\binom{n}{2}^{-1}$). –  Aug 29 '24 at 15:40
  • I made a typo or off-by-one error when writing the exponent, I think. Should be fixed. –  Aug 29 '24 at 15:43
  • Ah, I was counting non-fixed points instead, hence my confusion. Sorry. $\quad$ K, your edit of the exponent results in $E(0) = n, E(1) = n-2$, so this is fine now. – Calvin Lin Aug 29 '24 at 15:43