0

Let $2 \leq m \leq n$. Let $S_n^m$ be the subset of $S_n$ consisting of all permutations $a \in S_n$ such that for all $1 \leq i < m$, $a^i$ is a derangement, i.e. $a$ has no cycles of length less than $m$.

How many elements are there in $S_n^m$?

Furthermore, is there an (efficient) algorithm which, given an integer $0 \leq I < N$, with $N$ the number of elements in $S_n^m$, will return an element of $S_n^m$ such that the algorithm outputs distinct permutations if and only if it is given distinct inputs?

Mel
  • 196
  • For the first question the answer is extremely close to $e^{-H_{m-1}} n!$ where $H_{m-1}$ are the harmonic numbers; see https://math.stackexchange.com/questions/4915816/why-does-this-connection-of-increasingly-exclusive-partitions-p-n-k-to-the-h/4924601#4924601 – Qiaochu Yuan Jun 20 '24 at 19:01

1 Answers1

0

You can count the number of elements of $S_n^m$ via a dynamic programming. Let $dp[n]$ be a number of permutations in $S_n^m$. Let's look at the cycle $a_1 \to a_2 \to a_3 \to \dots a_s \to a_1$, where $a_1 =1 $, of a premutation in $S_n^m$. There are $(n-1)\cdot (n-2) \dots (n-s+1) = \frac{(n-1)!}{(n-s)!}$ ways to form this cycle and $dp[n-s]$ ways to form other cycles. So we have $$dp[n] = \sum_{s=m}^n \frac{(n-1)!}{(n-s)!} \cdot dp[n-s].$$

Once you have counted all $dp[n]$, you can enumerate all permutations in $S_n^m$ via the standart trick.

Let we want to find a permutation, corresponding to a number $M$. Again, let's concentrate on the cycle $a_1 \to a_2 \to a_3 \to \dots a_s \to a_1$, where $a_1 =1 $. Where are exactly $$f(t) = \sum_{s = 0}^t \frac{(n-1)!}{(n-s)!} \cdot dp[n-s] $$ permutations, for which $s$ is not less than $t$. So, we can find a number $t'$, such that $f(t') \le M < f(t'+1)$ and then conclude that for our permutation $s$ equals $t'$. Then let $M' = M - f(t')$, so we have to find the $M'$-th permutation with cycle $a_1 \to a_2 \to a_3 \to \dots a_s \to a_1$, where $a_1 =1 $. There are $dp[n-s]$ permutations with each of such cycle, so, in fact, we want to find a $M'' = \frac{M'}{dp[n-s]}$-th permutation in $S_{n-s}^m$ and $M''' = M' \mod dp[n-s]$-th cycle $a_1 \to a_2 \to a_3 \to \dots a_s \to a_1$ (here $a \mod b$ means the remainder when dividing $a$ by $b$). The first part can be done recursively, while the second part (enumerating the cycle) is trivial.

The asymptotics of this algorithm is $\mathcal O(n^2)$ if you precalc all factorials.

UPD: We can improve the asyptotics by noting that $$dp[n+1] = \sum_{s=m}^{n+1} \frac{n!}{(n + 1-s)!} \cdot dp[n + 1 -s] = \sum_{s=m-1}^{n} \frac{n!}{(n-s)!} \cdot dp[n -s] = \\ n \cdot \sum_{s=m}^{n} \frac{(n-1)!}{(n-s)!} \cdot dp[n -s] + \frac{n!}{(n+1-m)!}\cdot dp[n+1-m] =\\ n\cdot dp[n] + \frac{n!}{(n+1-m)!} \cdot dp[n+1-m] . $$ If we assume that multiplication can be done in $\mathcal O(1)$ time, then assymptotics to calculate all $dp[i]$ becames only $\mathcal O(n)$.

The assymptotics of finding $I$-th permutation in $S_n^m$ can be also slightly improved: we can find $t'$ by binary search and then asymptotics becames only $\mathcal O(n\log n)$. Moreover, if we notice that throughout our algoritm $M$ only decreases, so $t'$ also decreases and thus we can achieve the overall asymptotics $\mathcal O(n)$.

But this works only if we assume that we can multiply any two numbers in $\mathcal O(1)$ time (for example if we know that $I$ is bounded by some not so big value). Unfortunately, in general case, we need time $O(\log A + \log B)$ to calculate the value of $A\cdot B$. So, the asymptotics of this algorithm is $\mathcal O(n\log n \cdot \log(n!)) = \mathcal O(n^2\log^2n)$.

  • Unfortunately, since there exist linear-time algorithms for bijections between sets of $n!$ elements and $S_n$, this does not seem better than simply looping through all elements of $S_n$ in terms of complexity. Is there a way to do this faster? – Mel Jun 21 '24 at 11:00
  • But the straightforward algorithm, what enumerates all elements of $S_n$ and then check are each of them in $S_n^m$ has realy vast complexity. If you want to find the $I$-th elemtn from $S_n^m$, you should check at least $I$ permutations, so the asympotics would be at least $\mathcal O(In)$, while my algorithm do not use $I$ in the asymptotics. Or do you mean something other by looping through all elements of $S_n$? – ekravchenko Jun 21 '24 at 14:22
  • No, you are right. Is this just a fundamentally slow problem? Ideally I’d like something linear or linear-logarithmic… – Mel Jun 21 '24 at 21:38
  • If we assume that we can multiply two number in constant time, then we can upgrade this algorithm to complexity $\mathcal O(n\log n)$. But, in fact, we have to multiply sufficently large numbers, so the real comlexity is $\mathcal O(n^2\log^2 n)$. See the update. It seems that only the logarithmic part of the complexity can be improved, because even arithmetic operations with numbers like $|S_n^m|$ takes a lot of time. – ekravchenko Jun 21 '24 at 23:06
  • Can it be accelerated by allowing for an iterator? I.e. is it possible to find the element “after” a given element of $S_n^m$, for some ordering of the elements of $S_n^m$, faster than extracting the element of $S_n^m$ corresponding to $I$ for some bijection? – Mel Jun 22 '24 at 14:02