This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

Ranking and Unranking Restricted Permutations

Peter Kagey
Abstract

We discuss efficient methods for unranking derangements and ménage permutations. That is, we will provide an algorithm to efficiently extract the kk-th earliest such permutation under the lexicographic ordering. We will show that this problem can be reduced to the problem of computing the number of restricted permutations with a given prefix, and then we will use rook theory to solve this counting problem. This has applications to combinatorics, probability, statistics, and modeling.

1 Overview and preliminaries

In 2020, Richard Arratia announced two $100 prizes with a similar flavor, both about unranking problems: given a strict total order on a finite set, when is it possible to directly compute the ii-th element with respect to the total order?

Problem 1 ($100 question).

For n=20n=20 there are

895 014 631 192 902 121>8.91017895\,014\,631\,192\,902\,121>8.9\cdot 10^{17}

elements of the set

{πSnπ(i)i 1in},\left\{\pi\in S_{n}\mid\pi(i)\neq i\ \forall\ 1\leq i\leq n\right\},

which are called derangements.111This function is described by sequence A000166 in the On-Line Encyclopedia of Integer Sequences (OEIS) [4]. Determine the 5×10175\times 10^{17}-th such permutation when listed in lexicographic order.

Answer 1.

The derangement in S20S_{20} with rank 5×10175\times 10^{17} is

12 14 2 9 13 20 6 3 1 17 5 11 19 15 10 18 8 7 4 16.12\ 14\ 2\ 9\ 13\ 20\ 6\ 3\ 1\ 17\ 5\ 11\ 19\ 15\ 10\ 18\ 8\ 7\ 4\ 16.

The algorithm for computing this permutation will be described in Section 4.

Problem 2 ($100 question).

For n=20n=20 there are

312 400 218 671 253 762>3.11017312\,400\,218\,671\,253\,762>3.1\cdot 10^{17}

elements of the set

{πSnπ(i){i1,i}(modn) 1in},\left\{\pi\in S_{n}\mid\pi(i)\not\in\{i-1,i\}\pmod{n}\ \forall\ 1\leq i\leq n\right\},

which are called ménage permutations.222 This function is described by sequence A000179 in the OEIS [4]. Determine the 101710^{17}-th such permutation when listed in lexicographic order.

Answer 2.

The ménage permutation in S20S_{20} with rank 101710^{17} is

7 16 19 12 2 8 15 1 18 14 3 9 20 10 5 17 13 4 11 6.7\ 16\ 19\ 12\ 2\ 8\ 15\ 1\ 18\ 14\ 3\ 9\ 20\ 10\ 5\ 17\ 13\ 4\ 11\ 6.

The algorithm for computing this value will be described in Section 5.

Example 3.

The 4444 derangements in S5S_{5}, ordered lexicographically are.

π1=21453\pi_{1}=21453π2=21534\pi_{2}=21534π3=23154\pi_{3}=23154π4=23451\pi_{4}=23451π5=23514\pi_{5}=23514π6=24153\pi_{6}=24153π7=24513\pi_{7}=24513π8=24531\pi_{8}=24531π9=25134\pi_{9}=25134π10=25413\pi_{10}=25413π11=25431\pi_{11}=25431π12=31254\pi_{12}=31254π13=31452\pi_{13}=31452π14=31524\pi_{14}=31524π15=34152\pi_{15}=34152π16=34251\pi_{16}=34251π17=34512\pi_{17}=34512π18=34521\pi_{18}=34521π19=35124\pi_{19}=35124π20=35214\pi_{20}=35214π21=35412\pi_{21}=35412π22=35421\pi_{22}=35421π23=41253\pi_{23}=41253π24=41523\pi_{24}=41523π25=41532\pi_{25}=41532π26=43152\pi_{26}=43152π27=43251\pi_{27}=43251π28=43512\pi_{28}=43512π29=43521\pi_{29}=43521π30=45123\pi_{30}=45123π31=45132\pi_{31}=45132π32=45213\pi_{32}=45213π33=45231\pi_{33}=45231π34=51234\pi_{34}=51234π35=51423\pi_{35}=51423π36=51432\pi_{36}=51432π37=53124\pi_{37}=53124π38=53214\pi_{38}=53214π39=53412\pi_{39}=53412π40=53421\pi_{40}=53421π41=54123\pi_{41}=54123π42=54132\pi_{42}=54132π43=54213\pi_{43}=54213π44=54231\pi_{44}=54231

In particular, unranking the 1919th derangement in S5S_{5} gives 3521435214; alternatively, ranking the derangement 4325143251 gives 2626.

To be explicit about what we wish to compute efficiently, we define the notion of a unranking.

Definition 4.

Let 𝒞\mathscr{C} be a finite set with a strict total order, and let {ci}i=1|𝒞|\{c_{i}\}_{i=1}^{|\mathscr{C}|} be the unique sequence of elements in 𝒞\mathscr{C} such that ci<ci+1c_{i}<c_{i+1} for all 1i<|𝒞|1\leq i<|\mathscr{C}|. Then a unranking is a map

unrank𝒞:{1,2,,|𝒞|}𝒞\operatorname{unrank}_{\mathscr{C}}\colon\{1,2,\dots,|\mathscr{C}|\}\to\mathscr{C}

that sends icii\mapsto c_{i}.

An efficient algorithm to unrank a collection of objects implies an efficient algorithm for sampling from the collection uniformly at random by sampling the indices uniformly at random. This can be of use in the case of Monte Carlo simulations and other instances where it is useful to be able to sample uniformly from a collection of combinatorial objects.

As the name suggests, every unranking problem comes with a dual problem called the “ranking problem.”

Definition 5.

A ranking of a finite set 𝒞\mathscr{C} with a strict total order is a map

rank𝒞:𝒞{1,2,,|𝒞|}\operatorname{rank}_{\mathscr{C}}\colon\mathscr{C}\to\{1,2,\dots,|\mathscr{C}|\}

that sends ciic_{i}\mapsto i.

The existence of both efficient ranking and unranking maps implies the existence of an efficient encoding for these objects, which may be of interest to computer scientists. The encoding works by ranking an object to get its index, which then can be stored in as a positive integer and unranked on retrieval to recover the original object.

In the remaining sections, we will show how we resolved both of the above problems in order to claim Richard Arratia’s two $100 prizes. In particular, we will construct an algorithm for ranking and unranking both derangements and ménage permutations under the lexicographic ordering. We will show that the existence of an efficient way to count the number of such permutations with a given prefix implies that there is an efficient way to compute the ranking and unranking maps. Then we will develop some ideas from rook theory and apply them to the context of derangements and ménage permutations.

2 Prefix counting and word ranking

In both the case of unranking derangements and menage permutations (and in many other applications) our combinatorial objects are words in lexicographic order, which is a generalization of alphabetical order.

We begin by developing a general theory for unranking collections of words in lexicographic order by counting the number of words with a given prefix.

2.1 Words with a given prefix

We will start by introducing some basic definitions about words and prefixes, and to formalize the notion of lexicographic order.

Definition 6.

A finite word ww over an alphabet 𝒜\mathscr{A} is a finite sequence {wi𝒜}i=1N\{w_{i}\in\mathscr{A}\}_{i=1}^{N}.

The collection of finite words over the alphabet 𝒜\mathscr{A} is denoted by W𝒜\pazocal{W}_{\mathscr{A}}, or just W\pazocal{W} when the alphabet is implicit from context.

Definition 7.

A word w={wi𝒜}i=1Nw=\{w_{i}\in\mathscr{A}\}_{i=1}^{N} is said to begin with a prefix 𝛂\boldsymbol{\alpha} of length 𝐌\boldsymbol{M} if MNM\leq N, α={αi𝒜}i=1M\alpha=\{\alpha_{i}\in\mathscr{A}\}_{i=1}^{M}, and wi=αiw_{i}=\alpha_{i} for all iMi\leq M.

Definition 8.

A word ww is said to be before ww^{\prime} in lexicographic order if either ww is a proper prefix of ww^{\prime}, or if at the first position, ii, where ww and ww^{\prime} differ, wi<wiw_{i}<w^{\prime}_{i}.

With these definitions established, we can turn the problem of unranking words into a problem about counting words with specified prefixes.

Theorem 9.

For k>0k>0, let W\pazocal{W} be a set of nonempty words on the alphabet [n]={1,2,,n}[n]=\{1,2,\dots,n\}, and let 𝒞W\mathscr{C}\subsetneq\pazocal{W} be a finite subset of words on this alphabet, with a total order given by its lexicographic order.

Let #prefix𝒞:W𝒞\operatorname{\#prefix}_{\mathscr{C}}\colon\pazocal{W}\rightarrow\mathscr{C} be the function that counts the number of words in 𝒞\mathscr{C} that begin with a given prefix.

Then the unranking function can be computed recursively by

unrank𝒞(i)=fi𝒞((1),0)\operatorname{unrank}_{\mathscr{C}}(i)=f^{\mathscr{C}}_{i}((1),0) (1)

where

fi𝒞(α,j)=\displaystyle f^{\mathscr{C}}_{i}(\alpha,j)= fi𝒞(α,j+#prefix𝒞(α))\displaystyle f^{\mathscr{C}}_{i}(\alpha^{\prime},j+\operatorname{\#prefix}_{\mathscr{C}}(\alpha)) i>j+#prefix𝒞(α)i>j+\operatorname{\#prefix}_{\mathscr{C}}(\alpha) (2)
fi𝒞(α,j)=\displaystyle f^{\mathscr{C}}_{i}(\alpha,j)= fi𝒞(α′′,j)\displaystyle f^{\mathscr{C}}_{i}(\alpha^{\prime\prime},j) α𝒞\alpha\not\in\mathscr{C} and ij+#prefix𝒞(α)i\leq j+\operatorname{\#prefix}_{\mathscr{C}}(\alpha) (3)
fi𝒞(α,j)=\displaystyle f^{\mathscr{C}}_{i}(\alpha,j)= fi𝒞(α′′,j+1)\displaystyle f^{\mathscr{C}}_{i}(\alpha^{\prime\prime},j+1) α𝒞\alpha\in\mathscr{C}, ij+#prefix𝒞(α)i\leq j+\operatorname{\#prefix}_{\mathscr{C}}(\alpha), and ij+1i\neq j+1 (4)
fi𝒞(α,j)=\displaystyle f^{\mathscr{C}}_{i}(\alpha,j)= α\displaystyle\alpha α𝒞\alpha\in\mathscr{C} and i=j+1i=j+1, (5)

where

α\displaystyle\alpha =(α1,α2,,α),\displaystyle=(\alpha_{1},\alpha_{2},\dots,\alpha_{\ell}),
α\displaystyle\alpha^{\prime} =(α1,α2,,α1,1+α),\displaystyle=(\alpha_{1},\alpha_{2},\dots,\alpha_{\ell-1},1+\alpha_{\ell}),
α′′\displaystyle\alpha^{\prime\prime} =(α1,α2,,α,1),\displaystyle=(\alpha_{1},\alpha_{2},\dots,\alpha_{\ell},1),

and jj denotes the number of words in 𝒞\mathscr{C} that occur strictly before α\alpha.

Proof.

We make three claims that we will prove using induction on the recursive applications of fi𝒞(α,j)f_{i}^{\mathscr{C}}(\alpha,j): (1) that jj is the number of words in 𝒞\mathscr{C} that occur strictly before α\alpha, (2) that αwi\alpha\leq w_{i}, and (3) that the sequence of α\alphas is strictly increasing.

This final claim (the sequence of α\alphas is strictly increasing) follows from the observation that α<α′′<α{\alpha<\alpha^{\prime\prime}<\alpha^{\prime}} in lexicographic order.

Because each iteration increases either jj or \ell (the number of letters in α\alpha) or both, the number of recursive applications of fi𝒞f_{i}^{\mathscr{C}} required to determine wiw_{i} is at most i+maxwW|w|i+\max_{w\in\pazocal W}|w|, which is finite because W\pazocal W only contains of finite words.

The base case is clear: We start with fi𝒞((1),0)f_{i}^{\mathscr{C}}((1),0) because (1)(1) is the lexicographically earliest word, so 0 nonempty words strictly precede it, and (1)wi(1)\leq w_{i}.

We will repeatedly use the observation that if jj words precede α\alpha, then a word has prefix α\alpha if and only if its index is in (j,j+#prefix𝒞(α)](j,j+\operatorname{\#prefix}_{\mathscr{C}}(\alpha)]. Note that this range is empty whenever there are no words prefixed by α\alpha.

Case (2). Because j+#prefix𝒞(α)j+\operatorname{\#prefix}_{\mathscr{C}}(\alpha) is the index of the last word that begins with α\alpha, if i>j+#prefix𝒞(α)i>j+\operatorname{\#prefix}_{\mathscr{C}}(\alpha), then wiw_{i} must begin with a length-\ell prefix that is lexicographically later than α\alpha.

By construction, α\alpha^{\prime} is the lexicographically earliest word of length \ell that comes after α\alpha, therefore αwi\alpha^{\prime}\leq w_{i}. As such, the number of words that strictly precede α\alpha^{\prime} is j+#prefix𝒞(α){j+\operatorname{\#prefix}_{\mathscr{C}}(\alpha)}, which is the sum of the number of words that occur strictly before α\alpha and the number of words that have prefix α\alpha.

Case (3). If α𝒞\alpha\not\in\mathscr{C} and ij+#prefix𝒞(α)i\leq j+\operatorname{\#prefix}_{\mathscr{C}}(\alpha), then α\alpha is a proper prefix of wiw_{i}, and wiw_{i} is of length at least +1\ell+1. By construction, α′′\alpha^{\prime\prime} is the lexicographically earliest word of length +1\ell+1 that has a prefix of α\alpha, so α′′wi\alpha^{\prime\prime}\leq w_{i}, and the number of words in 𝒞\mathscr{C} that precede α′′\alpha^{\prime\prime} is equal to jj, the number of words that precede α\alpha.

Case (4). If α𝒞\alpha\in\mathscr{C}, ij+#prefix𝒞(α)i\leq j+\operatorname{\#prefix}_{\mathscr{C}}(\alpha), and ij+1i\neq j+1 then α\alpha must be the word at index j+1<ij+1<i, because α\alpha itself is the lexicographically earliest word with the prefix α\alpha. Because words cannot appear multiple times in 𝒞\mathscr{C}, wiw_{i} must have α\alpha as a proper prefix. Therefore α′′wi\alpha^{\prime\prime}\leq w_{i} and the number of words that strictly precede α′′\alpha^{\prime\prime} is j+1j+1: the number of words that strictly precede α\alpha plus α\alpha itself.

Case (5). If α𝒞\alpha\in\mathscr{C} and i=j+1i=j+1, then wi=αw_{i}=\alpha because α\alpha itself is the lexicographically earliest word with the prefix α\alpha, so it must occur at index i=j+1i=j+1. ∎

Notice that each recursive call of fi𝒞f_{i}^{\mathscr{C}} increases the sum of the letters of α\alpha. If we suppose that 𝒞\mathscr{C} is a finite set of words on the alphabet [n][n] of length at most kk, then unranking a word from 𝒞(k)\mathscr{C}^{(\leq k)} requires at most nknk recursive applications of fi𝒞(k)f_{i}^{\mathscr{C}^{(\leq k)}}.

Therefore if there exists a polynomial time algorithm for computing #prefix𝒞\operatorname{\#prefix}_{\mathscr{C}}, then there exists an unranking algorithm that is polynomial in the size of the alphabet 𝒜\mathscr{A} and the length of the longest word. In the case of restricted permutations, each of these grow linearly with the number of letters in the ménage permutations.

2.2 Ranking words

Just as we can recursively find a word at a given index, we can also recursively find a index corresponding to a given word.

Theorem 10.

If 𝒞W\mathscr{C}\subsetneq\pazocal{W} is a collection of nonempty words over the alphabet [n][n], then the rank of the word w𝒞w\in\mathscr{C} can be computed as the sum

rank𝒞(w)=i=1|w|(𝟙𝒞(w(i))+=1wi1#prefix𝒞(w(i1)))\operatorname{rank}_{\mathscr{C}}(w)=\sum_{i=1}^{|w|}\left(\mathbbm{1}_{\mathscr{C}}(w_{(i)})+\sum_{\ell=1}^{w_{i}-1}\operatorname{\#prefix}_{\mathscr{C}}(w_{(i-1)}^{\ell})\right)

where

w\displaystyle w =(w1,w2,,w|w|),\displaystyle=(w_{1},w_{2},\dots,w_{|w|}),
w(i)\displaystyle w_{(i)} =(w1,w2,,wi1,wi),\displaystyle=(w_{1},w_{2},\dots,w_{i-1},w_{i}),
w(i1)\displaystyle w_{(i-1)}^{\ell} =(w1,w2,,wi1,),\displaystyle=(w_{1},w_{2},\dots,w_{i-1},\ell),

and 𝟙𝒞:W{0,1}\mathbbm{1}_{\mathscr{C}}\colon\pazocal{W}\to\{0,1\} is the indicator function of 𝒞\mathscr{C}, where 𝟙𝒞(x)=1\mathbbm{1}_{\mathscr{C}}(x)=1 if and only if x𝒞x\in\mathscr{C}.

Proof.

Note that the inner sum

𝟙𝒞(w(i))+=1wi1#prefix𝒞(w(i1))\mathbbm{1}_{\mathscr{C}}(w_{(i)})+\sum_{\ell=1}^{w_{i}-1}\operatorname{\#prefix}_{\mathscr{C}}(w_{(i-1)}^{\ell})

is the number of words in 𝒞\mathscr{C} that are lexicographically later than w(i1)w_{(i-1)} and are less than or equal to w(i)w_{(i)}.

By summing over the number of letters of ww, we count all of the words that are less than or equal to w(|w|)=ww_{(|w|)}=w, which is precisely the rank of ww. ∎

3 Techniques of rook theory

Now that we have shown that we can unrank words whenever we can compute the number of words with a given prefix, we want to develop techniques for this counting problem. In the case of unranking derangements and permutations, it is useful to use ideas from rook theory, which provides a theory for understanding position-restricted permutations. Rook theory was introduced by Kaplansky and Riordan [1] in their 1946 paper The Problem of the Rooks and its Applications. In it, they discuss problems of restricted permutations in the language of rooks placed on a chessboard.

\rook3\rook4\rook8\rook1\rook2\rook7\rook5\rook6
Figure 1: An illustration of the rook placement corresponding to the permutation 34812756S834812756\in S_{8}. A rook is placed in square (i,π(i))(i,\pi(i)) for each ii.

3.1 Definitions in rook theory

We begin by introducing some preliminary ideas from rook theory.

Definition 11.

A board BB is a subset of [n]×[n][n]\times[n] which represents the squares of an n×nn\times n chessboard that rooks are allowed to be placed on. Every board BB has a complementary board Bc=([n]×[n])BB^{c}=([n]\times[n])\setminus B, which consists of all of the squares of BB that a rook cannot be placed on.

To each board, we can associate a generating polynomial that keeps track of the number of ways to place a given number of rooks on the squares of BB in such a way that no two rooks are in the same row or column.

Definition 12.

The rook polynomial associated with a board BB,

pB(x)=r0+r1x+r2x2++rnxn,p_{B}(x)=r_{0}+r_{1}x+r_{2}x^{2}+\dots+r_{n}x^{n}, (6)

is a generating polynomial where rkr_{k} denotes the number of kk-element subsets of BB such that no two elements share an xx-coordinate or a yy-coordinate.

In the context of permutations, we’re typically interested in rnr_{n}, the number of ways to place nn rooks on a restricted n×nn\times n board. However, it turns out that a naive application of the techniques from rook theory does not immediately allow us to count the number of restricted permutations with a given prefix. Computing the number of such permutations is known to be #P-hard for a board with arbitrary restrictions. We can see this by encoding a board BB as a (0,1)(0,1)-matrix and computing the matrix permanent, since there is a bijection between boards and (0,1)(0,1)-matrices. (In fact, Shevelev [6] claims that “the theory of enumerating the permutations with restricted positions stimulated the development of the theory of the permanent.”)

Lemma 13.

Let MB={aij}M_{B}=\{a_{ij}\} be an n×nn\times n matrix where

aij={1(i,j)B0(i,j)B.a_{ij}=\begin{cases}1&(i,j)\in B\\ 0&(i,j)\not\in B\end{cases}. (7)

Then the coefficient of xnx^{n} in pB(x)p_{B}(x) is given by the matrix permanent

perm(MB)=σSni=1naiσ(i).\operatorname{perm}(M_{B})=\sum_{\sigma\in S_{n}}\prod_{i=1}^{n}a_{i\sigma(i)}. (8)

Now is an appropriate time to recall Valiant’s Theorem.

Theorem 14 (Valiant’s Theorem [9]).

The counting problem of computing the permanent of a (0,1)-matrix is #P-complete.

Corollary 15.

Computing the number of rook placements on an arbitrary n×nn\times n board is #P-hard.

Therefore, in order to compute the number of permutations, we must exploit some additional structure of the restrictions.

3.2 Techniques of rook theory

Rook polynomials can be computed recursively. The base case is that for an empty board B=B=\emptyset, the corresponding rook polynomial is p(x)=1p_{\emptyset}(x)=1, because there is one way to place no rooks, and no way to place one or more rooks.

Lemma 16 ([5]).

Given a board BB and a square (x,y)B(x,y)\in B, we can define two resulting boards from including or excluding the given square:

Bi\displaystyle B_{i} ={(x,y)Bxx and yy}\displaystyle=\{(x^{\prime},y^{\prime})\in B\mid x\neq x^{\prime}\text{ and }y\neq y^{\prime}\} (9)
Be\displaystyle B_{e} =B(x,y).\displaystyle=B\setminus{(x,y)}. (10)

Then we can write the rook polynomial for BB in terms of this decomposition.

pB(x)=xpBi(x)+pBe(x).p_{B}(x)=xp_{B_{i}}(x)+p_{B_{e}}(x). (11)

If we want to compute a rook polynomial using this construction, we can end up adding up lots of smaller rook polynomials—a number that is exponential in the size of BB. When the number of squares that are missing from BB is small, it can be easier to compute the rook polynomial of the complementary board, pBcp_{B^{c}}, and use the principle of inclusion/exclusion on its coefficients to determine the rook polynomial for the original board, BB.

In the case of derangements and ménage permutations, this is the strategy we’ll use. We will start by finding the resulting board from a given prefix, find the rook polynomial of the complementary board, and use the principle of inclusion/exclusion to determine the number of ways to place rooks in the resulting board.

4 Unranking derangements

4.1 Overview for unranking derangements

Richard Arratia’s question focused on unranking derangements written as words in lexicographic order. Other authors have looked at unranking derangements based on other total orderings. In particular, Mikawa and Tanaka [3] give an algorithm to rank/unrank derangements with respect to lexicographic ordering in cycle notation.

In this section we will develop an algorithm for ranking and unranking derangements with respect to their lexicographic ordering as words. The technique that we use will broadly be re-used in the next section. It is worthwhile to begin by recalling the definition of a derangement.

Definition 17.

A derangement is a permutation πSn\pi\in S_{n} such that π\pi has no fixed points. That is, the set of derangements on nn letters is

𝒟n={πSnπ(i)ii[n]}.\mathscr{D}_{n}=\{\pi\in S_{n}\mid\pi(i)\neq i\ \forall i\in[n]\}. (12)

4.2 The complementary board

In order to compute the number of derangements with a given prefix, it is useful to look at the board that results after placing \ell rooks according to these positions, as illustrated in Figure 2.

\rook\rook
Figure 2: An example of a prefix α=(6,1)\alpha=(6,1), and the board that results from deleting the first =2\ell=2 rows and columns 66 and 11. The derived complementary board of BB from α\alpha is Bαc={(1,2),(2,3),(3,4),(5,5),,(10,10)}B^{c}_{\alpha}=\{(1,2),(2,3),(3,4),(5,5),\dots,(10,10)\}.
Definition 18.

If BB is an n×nn\times n board, and α=(α1,α2,,α)\alpha=(\alpha_{1},\alpha_{2},\dots,\alpha_{\ell}) is a valid prefix of length \ell, the derived board of BB from α\alpha, denoted BαB_{\alpha}, is constructed by removing rows 1,2,,1,2,\dots,\ell and columns α1,α2,,α\alpha_{1},\alpha_{2},\dots,\alpha_{\ell} from BB, reindexing in such a way that both the row and column indexes are in [n][n-\ell].

The derived complementary board BαcB_{\alpha}^{c} is the complement of BαB_{\alpha} with respect to [n]×[n][n-\ell]\times[n-\ell].

Given a prefix of length \ell, the number of ways of placing nn-\ell rooks on the derived board BαB_{\alpha} is, by construction, equal to the number of words in 𝒞\mathscr{C} with prefix α\alpha

Lemma 19.

Given a valid \ell-letter prefix (α1,α2,,α)(\alpha_{1},\alpha_{2},\dots,\alpha_{\ell}) of a word on nn letters, the number of squares in the derived complementary board is

|Bαc|=n|{+1,+2,,n}{α1,α2,,α}|,|B_{\alpha}^{c}|=n-\ell-|\{\ell+1,\ell+2,\dots,n\}\cap\{\alpha_{1},\alpha_{2},\dots,\alpha_{\ell}\}|, (13)

and no two of these squares are in the same row or column.

Proof.

Notice that the derived complementary board can be constructed in a different order: by first taking the complement, then deleting rows and columns, and finally reindexing the squares. Because the complementary board has no two squares in the same row or column, deleting and reindexing results in a derived complementary board with the same property.

Thus, we only need to classify which squares in the complementary board are deleted to make the derived complementary board. We start by deleting \ell squares corresponding to the deletion of the first \ell rows, namely (1,1),(2,2),,(,)(1,1),(2,2),\dots,(\ell,\ell).

Some of these squares may also be in columns α1,α2,,α\alpha_{1},\alpha_{2},\dots,\alpha_{\ell}, but to avoid double-counting, we only consider those letters that are greater than \ell. These are |{+1,+2,,n}{α1,α2,,α}||\{\ell+1,\ell+2,\dots,n\}\cap\{\alpha_{1},\alpha_{2},\dots,\alpha_{\ell}\}|, as desired. ∎

4.3 Derangements with a given prefix

Now that we have a way of quickly computing |Bαc||B_{\alpha}^{c}|, we can compute the number of ways to place a given number of rooks on the complementary board. We can use this to compute the rook polynomial for the derived complementary board pBαc(x)p_{B_{\alpha}^{c}}(x). We will see later that we can use the coefficients of this polynomial to compute the number of ways of placing nn-\ell rooks on the derived board BαB_{\alpha}.

Lemma 20.

The rook polynomial for the complementary board BαcB_{\alpha}^{c} is

pBαc(x)=j=0|Bαc|(|Bαc|j)xj.p_{B_{\alpha}^{c}}(x)=\sum_{j=0}^{|B_{\alpha}^{c}|}\binom{|B_{\alpha}^{c}|}{j}x^{j}. (14)
Proof.

Recall that no two squares of BαcB^{c}_{\alpha} are in the same row or column. Thus the number of ways to place jj rooks is equivalent to selecting any jj squares from the collection of |Bαc||B^{c}_{\alpha}| squares.

Therefore the coefficient of xjx^{j} in the rook polynomial is (|Bαc|j)\binom{|B_{\alpha}^{c}|}{j}. ∎

Now we introduce a lemma of Stanley [8] to compute the number of ways of placing nn-\ell rooks in the derived board Bα[n]×[n]B_{\alpha}\subseteq[n-\ell]\times[n-\ell].

Lemma 21 ([8]).

Let B[n]×[n]B\subseteq[n]\times[n] be a board with complementary board BcB^{c}, and denote the rook polynomial of BcB^{c} by PBc(x)=k=0nrkcxkP_{B^{c}}(x)=\sum_{k=0}^{n}r^{c}_{k}x^{k}.

Then the number of ways, N0N_{0}, of placing nn nonattacking rooks on BB is given by the principle of inclusion/exclusion

N0=k=0n(1)krkc(nk)!.N_{0}=\sum_{k=0}^{n}(-1)^{k}r^{c}_{k}(n-k)!. (15)

This lemma allows us to compute the number of rook placements on the derived board BαB_{\alpha}, which is the number of derangements in 𝒟\mathscr{D} that begin with the prefix α\alpha.

Corollary 22.

The number of derangements with prefix α=(α1,α2,,α)\alpha=(\alpha_{1},\alpha_{2},\dots,\alpha_{\ell}) is given by

#prefix𝒟(α)=j=0|Bαc|(1)j(|Bαc|j)(nj)!,\operatorname{\#prefix}_{\mathscr{D}}(\alpha)=\sum_{j=0}^{|B_{\alpha}^{c}|}(-1)^{j}\binom{|B_{\alpha}^{c}|}{j}(n-\ell-j)!, (16)

which is A047920(n,|Bαc|)\operatorname{A047920}(n-\ell,|B_{\alpha}^{c}|) in the On-Line Encyclopedia of Integer Sequences [4].

Because we can compute |Bαc||B_{\alpha}^{c}| from α\alpha in linear time (see Lemma 19), if we use a computational model where factorials are given by an oracle and arithmetic can be computed in constant time, then #prefix𝒟\operatorname{\#prefix}_{\mathscr{D}} can be computed in linear time with respect to \ell, the length of the prefix.

Example 23.

For n=12n=12, we wish to count the number of derangements that start with the prefix α=(6,1)\alpha=(6,1), as illustrated in Figure 2. Since the prefix has two letters, =2\ell=2 and n=122=10n-\ell=12-2=10. The number of squares in BαcB_{\alpha}^{c} is

|Bαc|=122|{3,4,,12}{6,1}|1=9.|B_{\alpha}^{c}|=12-2-\underbrace{|\{3,4,\dots,12\}\cap\{6,1\}|}_{1}=9. (17)

Thus there are A047920(10,9)=1 468 457A047920(10,9)=1\,468\,457 derangements in S12S_{12} that start with the prefix α=(6,1)\alpha=(6,1).

Now that we have an efficient algorithm for computing #prefix𝒟:W0\operatorname{\#prefix}_{\mathscr{D}}\colon\pazocal{W}\rightarrow\mathbb{N}_{\geq 0}, we can invoke the recursive formula in Theorem 9 to compute unrank𝒟:0𝒟\operatorname{unrank}_{\mathscr{D}}\colon\mathbb{N}_{\geq 0}\rightarrow\mathscr{D} and unrank derangements. The sequence of recursive steps is illustrated in Table 1.

α\alpha (prefix) #prefix𝒟(α)\operatorname{\#prefix}_{\mathscr{D}}(\alpha) index range |Bαc||B_{\alpha}^{c}| unrank𝒟(1000)\operatorname{unrank}_{\mathscr{D}}(1000)
11 0 (0,0](0,0] - f1000𝒟(1,0)f^{\mathscr{D}}_{1000}(1,0)
22 21192119 (0,2119](0,2119] 66 f1000𝒟(2,0)f^{\mathscr{D}}_{1000}(2,0)
2121 265265 (0,265](0,265] 66 f1000𝒟(21,0)f^{\mathscr{D}}_{1000}(21,0)
2222 0 (265,265](265,265] - f1000𝒟(22,265)f^{\mathscr{D}}_{1000}(22,265)
2323 309309 (265,574](265,574] 55 f1000𝒟(23,265)f^{\mathscr{D}}_{1000}(23,265)
2424 309309 (574,883](574,883] 55 f1000𝒟(24,574)f^{\mathscr{D}}_{1000}(24,574)
2525 309309 (883,1192](883,1192] 55 f1000𝒟(25,883)f^{\mathscr{D}}_{1000}(25,883)
251251 5353 (883,936](883,936] 44 f1000𝒟(251,883)f^{\mathscr{D}}_{1000}(251,883)
253253 0 (936,936](936,936] - f1000𝒟(253,936)f^{\mathscr{D}}_{1000}(253,936)
254254 6464 (936,1000](936,1000] 33 f1000𝒟(254,936)f^{\mathscr{D}}_{1000}(254,936)
25412541 1111 (936,947](936,947] 33 f1000𝒟(2541,936)f^{\mathscr{D}}_{1000}(2541,936)
25432543 1111 (947,958](947,958] 33 f1000𝒟(2543,947)f^{\mathscr{D}}_{1000}(2543,947)
25462546 1414 (958,972](958,972] 22 f1000𝒟(2546,958)f^{\mathscr{D}}_{1000}(2546,958)
25472547 1414 (972,986](972,986] 22 f1000𝒟(2547,972)f^{\mathscr{D}}_{1000}(2547,972)
25482548 1414 (986,1000](986,1000] 22 f1000𝒟(2548,986)f^{\mathscr{D}}_{1000}(2548,986)
2548125481 33 (986,989](986,989] 22 f1000𝒟(25481,986)f^{\mathscr{D}}_{1000}(25481,986)
2548325483 33 (989,992](989,992] 22 f1000𝒟(25483,989)f^{\mathscr{D}}_{1000}(25483,989)
2548625486 44 (992,996](992,996] 11 f1000𝒟(25486,992)f^{\mathscr{D}}_{1000}(25486,992)
2548725487 44 (996,1000](996,1000] 11 f1000𝒟(25487,996)f^{\mathscr{D}}_{1000}(25487,996)
254871254871 22 (996,998](996,998] 0 f1000𝒟(254871,996)f^{\mathscr{D}}_{1000}(254871,996)
254873254873 22 (998,1000](998,1000] 0 f1000𝒟(254873,998)f^{\mathscr{D}}_{1000}(254873,998)
25487312548731 11 (998,999](998,999] 0 f1000𝒟(2548731,998)f^{\mathscr{D}}_{1000}(2548731,998)
25487362548736 11 (999,1000](999,1000] 0 f1000𝒟(2548736,999)f^{\mathscr{D}}_{1000}(2548736,999)
2548736125487361 11 (999,1000](999,1000] 0 f1000𝒟(25487361,999)f^{\mathscr{D}}_{1000}(25487361,999)
Table 1: There are A000166(8)=14833A000166(8)=14833 derangements on 88 letters. The table shows the recursive steps to find that the derangement at index 10001000 is 2548736125487361.

5 Unranking ménage permutations

With the bounding on unranking derangements claimed, Richard proposed a bounty for another family of restricted permutations, namely ménage permutations.

A ménage permutation comes from the problème des ménages, introduced by Édouard Lucas in 1891. There are a few choices of how to define these permutations, but we will use the following definition for simplicity.

Definition 24.

A ménage permutation is a permutation πSn\pi\in S_{n} such that for all i[n]i\in[n], π(i)i\pi(i)\neq i and π(i)+1i(modn)\pi(i)+1\not\equiv i\pmod{n}. The set of ménage permutations of length nn is denoted by Mn\pazocal{M}_{n}.

5.1 Overview for unranking ménage permutations

As in the section about unranking derangements, we will use the fact from Theorem 9 that if we can efficiently count the number of words with a given prefix, then we can efficiently unrank the words.

The technique exploits the following observations: after placing rooks on a board corresponding to our prefix, the remaining board has the property that its complement can be partitioned into sub-boards that do not share rows or columns. These sub-boards have a structure that we can understand, and we can leverage that understanding to compute the rook polynomials of these sub-boards and consequently of the complementary board itself. Once we have computed the rook polynomial of the complementary board, we can again use Lemma 15 to compute the number of full rook placements on the original board. This gives us the number of ménage permutations with a given prefix.

5.2 Disjoint board decomposition

Figure 3 suggestively shows a placement of rooks according to a prefix that results in a board whose complement can be partitioned into sub-boards whose squares don’t share any rows or columns. We will see that this property indeed holds in general, and we can exploit this in order to count the ménage permutations with a given prefix.

\rook\rook\rook\rook
Figure 3: The prefix α=(3,6,1,8)\alpha=(3,6,1,8), the derived board BαB_{\alpha}, and the derived complementary board Bαc=𝒪32𝒪7B_{\alpha}^{c}=\mathscr{O}_{3}\sqcup\mathscr{E}_{2}\sqcup\mathscr{O}_{7}^{\intercal}. There are 80628062 ways of placing eight nonattacking rooks on BαB_{\alpha}.

The property of complements that can be partitioned into sub-boards whose squares don’t share rows or columns is useful because it provides a way of factoring the rook polynomial of the bigger board into the rook polynomials of the sub-boards.

Definition 25.

Two sub-boards BB and BB^{\prime} are called disjoint if no squares of BB are in the same row or column as any square in BB^{\prime}.

Kaplansky gives a way of computing the rook polynomial of a board in terms of its disjoint boards.

Theorem 26 ([1]).

If BB can be partitioned into disjoint boards b1,b2,,bmb_{1},b_{2},\dots,b_{m}, then the rook polynomial of BB is the product of the rook polynomials of each sub-board

pB(x)=i=1mpbi(x).p_{B}(x)=\prod_{i=1}^{m}p_{b_{i}}(x). (18)

We will use this disjoint board decomposition repeatedly, because the boards that result after placing a prefix can be partitioned into disjoint sub-boards whose structure is well understood. Now we will give a name to these blocks, which are illustrated in Figure 4.

\ddots\ddots𝒪2n1\mathscr{O}_{2n-1}
\ddots\ddots𝒪2n1\mathscr{O}_{2n-1}^{\intercal}
\ddots\ddots2n2\mathscr{E}_{2n-2}
\ddots\ddots2n2\mathscr{E}_{2n-2}^{\intercal}
Figure 4: Examples of each of the four staircase-shaped boards. The first two boards are on grids of size n×nn\times n, the third is on a grid of size n×(n1)n\times(n-1) and the fourth is on a grid of size (n1)×n(n-1)\times n.
Definition 27.

A board is called staircase-shaped if it matches one of the following four shapes:

𝒪2n1\displaystyle\mathscr{O}_{2n-1} ={(i,i)i[n]}\displaystyle=\{(i,i)\mid i\in[n]\} {(i,i+1)i[n1]}\displaystyle\cup\ \{(i,i+1)\mid i\in[n-1]\}
𝒪2n1\displaystyle\mathscr{O}_{2n-1}^{\intercal} ={(i,i)i[n]}\displaystyle=\{(i,i)\mid i\in[n]\} {(i+1,i)i[n1]}\displaystyle\cup\ \{(i+1,i)\mid i\in[n-1]\}
2n2\displaystyle\mathscr{E}_{2n-2} ={(i,i)i[n1]}\displaystyle=\{(i,i)\mid i\in[n-1]\}\ {(i+1,i)i[n1]}\displaystyle\cup\ \{(i+1,i)\mid i\in[n-1]\}
2n2\displaystyle\mathscr{E}_{2n-2}^{\intercal} ={(i,i)i[n1]}\displaystyle=\{(i,i)\mid i\in[n-1]\}\ {(i,i+1)i[n1]}.\displaystyle\cup\ \{(i,i+1)\mid i\in[n-1]\}.

The subscripts represent the number of squares, and the names represent their parity.

We now show that our resulting boards can be partitioned into boards of these shapes.

Lemma 28.

For 1\ell\geq 1, and prefix α=(α1,α2,,α)\alpha=(\alpha_{1},\alpha_{2},\dots,\alpha_{\ell}) the derived complementary board BαcB_{\alpha}^{c} can be partitioned into disjoint staircase-shaped boards.

Proof.

The proof proceeds by induction on the length of the prefix.

To establish the base case, consider a prefix of length =1\ell=1. Because of the ménage restriction, α1{2,3,,n1}\alpha_{1}\in\{2,3,\dots,n-1\}, and the derived complementary board B(α1)cB_{(\alpha_{1})}^{c} can be partitioned into two disjoint sub-boards with shapes 𝒪2α13\mathscr{O}_{2\alpha_{1}-3} and 𝒪2n2α11\mathscr{O}^{\intercal}_{2n-2\alpha_{1}-1}. (This is illustrated for the case of n=7n=7 and α1\alpha_{1} in Figure 5.)

The inductive hypothesis is that the derived complementary board for a prefix of length 1{\ell-1} consists of sub-boards with shape 𝒪2m1\mathscr{O}_{2m-1}, 𝒪2m1\mathscr{O}^{\intercal}_{2m-1}, 2m2\mathscr{E}_{2m-2}, or 2m2\mathscr{E}^{\intercal}_{2m-2}. Placing a rook in row \ell can remove a top row or a column or both in a given sub-board. Table 2 below shows the resulting sub-boards after placing a rook in \ell-th row of BB, which may be in the top row, the ii-th column, or both.
 
Rook placement 𝒪2m1\mathscr{O}_{2m-1} 𝒪2m1\mathscr{O}_{2m-1}^{\intercal} Row 11 𝒪2m3\mathscr{O}_{2m-3} 2m2\mathscr{E}_{2m-2}^{\intercal} Column ii 𝒪2i3\mathscr{O}_{2i-3}, 2m2i\mathscr{E}_{2m-2i} 2i2\mathscr{E}_{2i-2}, 𝒪2m2i1\mathscr{O}_{2m-2i-1}^{\intercal} Row 11, column ii 𝒪2i5\mathscr{O}_{2i-5}, 2m2i\mathscr{E}_{2m-2i} 𝒪2i3\mathscr{O}_{2i-3}, 𝒪2m2i1\mathscr{O}_{2m-2i-1}^{\intercal}
 
Rook placement 2m2\mathscr{E}_{2m-2} 2m2\mathscr{E}_{2m-2} Row 11 𝒪2m3\mathscr{O}_{2m-3} 2m4\mathscr{E}_{2m-4}^{\intercal} Column ii 2i2\mathscr{E}_{2i-2}, 2m2i2\mathscr{E}_{2m-2i-2} 𝒪2i3\mathscr{O}_{2i-3}, 𝒪2m2i1\mathscr{O}_{2m-2i-1}^{\intercal} Row 11, column ii 𝒪2i3\mathscr{O}_{2i-3}, 2m2i2\mathscr{E}_{2m-2i-2} 𝒪2i5\mathscr{O}_{2i-5}, 𝒪2m2i1\mathscr{O}_{2m-2i-1}^{\intercal}

Table 2: The results of placing a rook in the first row, ii-th column, or both for all staircase-shaped boards.

Therefore placing any number of rooks in the first \ell rows results in a board whose complementary derived board is composed of disjoint staircase-shaped sub-boards. ∎

\rook
Figure 5: The first chessboard shows a placement of a rook at position 33, the second shows how the derived complementary board can be partitioned into two disjoint boards with 33 and 77 squares respectively.

5.3 Rook polynomials of blocks

Recall that the goal of partitioning BB into disjoint sub-boards b1,b2,,bmb_{1},b_{2},\dots,b_{m} is so that we can factor pB(x)p_{B}(x) in terms of pbi(x)p_{b_{i}}(x). Of course, this is only useful if we can describe pbi(x)p_{b_{i}}(x), which is the goal of this subsection. Conveniently, the rook polynomial of each bib_{i} will turn out to depend only on the number of squares, |bi||b_{i}|, which can be computed recursively because of its staircase shape.

We will begin by defining a family of polynomials that, suggestively, will turn out to be the rook polynomials that we are looking for. (The coefficients of these polynomials are described by OEIS sequence A011973 [4].)

Definition 29.

For j0j\geq 0, the jj-th Fibonacci polynomial Fj(x)F_{j}(x) is defined recursively as:

F0(x)\displaystyle F_{0}(x) =1\displaystyle=1 (19)
F1(x)\displaystyle F_{1}(x) =1+x\displaystyle=1+x (20)
Fn(x)\displaystyle F_{n}(x) =xFn2(x)+Fn1(x).\displaystyle=xF_{n-2}(x)+F_{n-1}(x). (21)

The rook polynomials of the staircase-shaped boards agree with these Fibonacci polynomials.

Lemma 30.

If BB is a staircase-shaped board with kk squares, then BB has rook polynomial pB(x)=Fk(x){p_{B}(x)=F_{k}(x)}, equal to the kk-th Fibonacci polynomial.

Proof.

We will recall the recursive construction of rook polynomials from Lemma 11, and proceed by induction on the number of squares, always choosing to include or exclude the upper-left square.

Since the reflections of board have the same rook polynomial as the unreflected board, without loss of generality, we will compute the rook polynomials for 𝒪2m1\mathscr{O}_{2m-1} and 2m2\mathscr{E}_{2m-2}, respectively.

To establish a base case, consider the rook polynomials when n=1n=1, so the even board has |0|=0|\mathscr{E}_{0}|=0 squares and the odd board has |𝒪1|=1|\mathscr{O}_{1}|=1 square. We can see the corresponding rook polynomials directly. There is 11 way to place 0 rooks on 0\mathscr{E}_{0} and no ways to place more rooks; similarly there is 11 way to place 0 rooks on 𝒪1\mathscr{O}_{1}, 11 way to place 11 rook on 𝒪1\mathscr{O}_{1}, and no way to place more than one rook. Thus

p0(x)\displaystyle p_{\mathscr{E}_{0}}(x) =1\displaystyle=1 =F0(x), and\displaystyle=F_{0}(x),\text{ and} (22)
p𝒪1(x)\displaystyle p_{\mathscr{O}_{1}}(x) =1+x\displaystyle=1+x =F1(x).\displaystyle=F_{1}(x). (23)

With the base case established, our inductive hypothesis is that pB(x)=Fh(x)p_{B}(x)=F_{h}(x) whenever BB is a staircase-shaped board with h<kh<k squares.

Assume that we have kk squares where kk is even, so our board looks like k\mathscr{E}_{k}. We can either place a rook or not in the upper-left square. If we include the square, then (k)ik2(\mathscr{E}_{k})_{i}\cong\mathscr{E}_{k-2}, if we exclude the square, then (k)e𝒪k1(\mathscr{E}_{k})_{e}\cong\mathscr{O}_{k-1}. Thus by Lemma 11, the rook polynomial of k\mathscr{E}_{k} is

pk(x)\displaystyle p_{\mathscr{E}_{k}}(x) =xpk2(x)+p𝒪k1(x)\displaystyle=xp_{\mathscr{E}_{k-2}}(x)+p_{\mathscr{O}_{k-1}}(x) (24)
=xFk2(x)+Fk1(x)\displaystyle=xF_{k-2}(x)+F_{k-1}(x) (25)
=Fk(x).\displaystyle=F_{k}(x). (26)

The case where kk is odd proceeds in almost the same way. Here our board looks like 𝒪k\mathscr{O}_{k}. We can either place a rook or not in the upper-left square. If we include the square, then (𝒪k)i𝒪k2(\mathscr{O}_{k})_{i}\cong\mathscr{O}_{k-2}, if we exclude the square, then (𝒪k)ek1(\mathscr{O}_{k})_{e}\cong\mathscr{E}_{k-1}. Again by Lemma 11, the rook polynomial of 𝒪k\mathscr{O}_{k} is

p𝒪k(x)\displaystyle p_{\mathscr{O}_{k}}(x) =xp𝒪k2(x)+pk1(x)\displaystyle=xp_{\mathscr{O}_{k-2}}(x)+p_{\mathscr{E}_{k-1}}(x) (27)
=xFk2(x)+Fk1(x)\displaystyle=xF_{k-2}(x)+F_{k-1}(x) (28)
=Fk(x).\displaystyle=F_{k}(x). (29)

Therefore, we now have the ingredients to describe the rook polynomial of a derived complementary board.

Corollary 31.

Suppose that BαcB_{\alpha}^{c} can be partitioned into mm disjoint staircase-shaped sub-boards of sizes b1,b2,,bmb_{1},b_{2},\dots,b_{m}. Then the rook polynomial of BαcB_{\alpha}^{c} is

pBαc(x)=i=1mFbi,p_{B_{\alpha}^{c}}(x)=\prod_{i=1}^{m}F_{b_{i}}, (30)

where FjF_{j} is the jj-th Fibonacci polynomial.

Proof.

This follows directly from Theorem 18 together with Lemma 30. ∎

5.4 Sub-boards from prefix

In this part, we discuss how to algorithmically compute the size of the sub-boards of the partition of the derived complementary board BαcB_{\alpha}^{c} for a given prefix α\alpha.

Lemma 32.

Given a nonempty prefix α=(α1,α2,,α)\alpha=(\alpha_{1},\alpha_{2},\dots,\alpha_{\ell}) and iαi\not\in\alpha, the number of squares of BcB^{c} in column ii that do not have a first coordinate in [][\ell] is given by the rule:

ci={0i<1i= or i=n2<i<nc_{i}=\begin{cases}0&i<\ell\\ 1&i=\ell\text{ or }i=n\\ 2&\ell<i<n\end{cases} (31)
Proof.

It is helpful to recall that the complementary board BcB^{c} consists of squares on the diagonal, squares on the subdiagonal, and the square (1,n)(1,n):

Bc={(i,i)i[n]}{(i+1,i)i[n1]}{(1,n)}.B^{c}=\{(i,i)\mid i\in[n]\}\cup\{(i+1,i)\mid i\in[n-1]\}\cup\{(1,n)\}. (32)

Now if i<i<\ell, then (i,i)(i,i) and (i+1,i)(i+1,i) both have a first coordinate less than or equal to \ell.

If i=i=\ell, then (i,i)(i,i) has a first coordinate in [][\ell], but (i+1,i)=(+1,)(i+1,i)=(\ell+1,\ell) does not have its first coordinate in [][\ell].

If i=ni=n, there are two squares of BcB^{c} in column ii: (n,n)(n,n) and (1,n)(1,n). Only (1,n)(1,n) has its first coordinate in [][\ell].

If <i<n\ell<i<n, then neither the square (i,i)(i,i) nor (i+1,i)(i+1,i) has its first coordinate in [][\ell]. ∎

Now we will go through each contiguous section of columns, and count the number of squares in each to build up the size of each of the blocks.

Lemma 33.

Partition [n]α[n]\setminus\alpha into contiguous parts, P\pazocal{P}. Each part PiPP_{i}\in\pazocal{P} of the partition corresponds to a staircase-shaped sub-board of size pPicp\sum_{p\in P_{i}}c_{p}.

Therefore the size of the disjoint sub-boards in the derived complementary board BαcB_{\alpha}^{c} is given by the multiset

Pα={pPicpPiP}.\pazocal{P}_{\alpha}=\left\{\sum_{p\in P_{i}}c_{p}\mid P_{i}\in\pazocal{P}\right\}. (33)
Proof.

Once the first row of a complementary ménage board has been deleted, the resulting board has the property that any two nonadjacent columns do not have any squares in the same row, because column ii has squares in (i,i)(i,i) and (i+1,i)(i+1,i).

Within each contiguous interval between the letters of α\alpha, the columns form a staircase-shaped sub-board because each column with a square in position (i+1,i)(i+1,i) has a square to its right, in position (i+1,i+1)(i+1,i+1) whenever i+1αi+1\not\in\alpha. ∎

Example 34.

As illustrated in Figure 3, if n=12n=12 and α=(3,6,1,8)\alpha=(3,6,1,8), then the contiguous partition of

[12]{3,6,1,8}={2P1,4,5P2,7P3,9,10,11,12P4}[12]\setminus\{3,6,1,8\}=\{\underbrace{2\vphantom{,}}_{P_{1}},\underbrace{4,5}_{P_{2}},\underbrace{7\vphantom{,}}_{P_{3}},\underbrace{9,10,11,12}_{P_{4}}\} (34)

is {P1,P2,P3,P4}\{P_{1},P_{2},P_{3},P_{4}\}. The corresponding staircase-shaped sub-boards have sizes

k1\displaystyle k_{1} =c2\displaystyle=c_{2} =0\displaystyle=0 =0\displaystyle=0
k2\displaystyle k_{2} =c4+c5\displaystyle=c_{4}+c_{5} =1+2\displaystyle=1+2 =3\displaystyle=3
k3\displaystyle k_{3} =c7\displaystyle=c_{7} =2\displaystyle=2 =2\displaystyle=2
k4\displaystyle k_{4} =c9+c10+c11+c12\displaystyle=c_{9}+c_{10}+c_{11}+c_{12} =2+2+2+1\displaystyle=2+2+2+1 =7,\displaystyle=7,

which matches what we observe in the illustration:

Bαc=0𝒪32𝒪7,B_{\alpha}^{c}=\mathscr{E}_{0}\sqcup\mathscr{O}_{3}\sqcup\mathscr{E}_{2}\sqcup\mathscr{O}_{7}^{\intercal}, (35)

5.5 Complementary polynomials

We have now established a method taking a prefix α\alpha and partitioning BαcB_{\alpha}^{c} into disjoint staircase-shaped sub-boards, which allow us to determine the rook polynomial of BαcB_{\alpha}^{c}. Using Lemma 15, this allows us to finally compute the number of ways of placing nn-\ell rooks on BαB_{\alpha}, thus determining the number of derangements that begin with α\alpha.

Theorem 35.

The number of ménage permutations that begin with a valid, nonempty prefix α\alpha is

#prefixM(α)=k=0n(1)krkc(nk)!\operatorname{\#prefix}{M}(\alpha)=\sum_{k=0}^{n}(-1)^{k}r_{k}^{c}(n-k)! (36)

where k=0nrkcxk=pPαFp\sum_{k=0}^{n}r_{k}^{c}x^{k}=\prod_{p\in\pazocal{P}_{\alpha}}F_{p}, FkF_{k} is the kk-th Fibonacci polynomial, and Pα\pazocal{P}_{\alpha} is the multiset corresponding to the size of the staircase-shaped sub-boards in the disjoint partition of BαcB_{\alpha}^{c}.

Proof.

This follows directly from Corollary 31 together with Lemma 33

Now that we have computed the number of ménage permutations, Theorem 9 provides an efficient unranking algorithm for M\pazocal{M}.

We will illustrate this with a specific example computing the number of ménage permutations with a given prefix.

Example 36.

We will continue with the running example illustrated in Figure 3 and expounded on in Example 35.

We’ve already seen that the for n=12n=12, the prefix α=(3,6,1,8)\alpha=(3,6,1,8) partitions the derived complementary board into three nonempty sub-boards:

Bαc=0𝒪32𝒪7.B_{\alpha}^{c}=\mathscr{E}_{0}\sqcup\mathscr{O}_{3}\sqcup\mathscr{E}_{2}\sqcup\mathscr{O}_{7}^{\intercal}. (37)

Lemma 30 tells us that the rook polynomial of BαcB_{\alpha}^{c} is

pBαc(x)\displaystyle p_{B_{\alpha}^{c}}(x) =F3(x)F2(x)F7(x)\displaystyle=F_{3}(x)F_{2}(x)F_{7}(x) (38)
=(1+3x+x2)(1+2x)(1+7x+15x2+10x3+x4)\displaystyle=(1+3x+x^{2})(1+2x)(1+7x+15x^{2}+10x^{3}+x^{4}) (39)
=1+12x+57x2+136x3+170x4+105x5+27x6+2x7\displaystyle=1+12x+57x^{2}+136x^{3}+170x^{4}+105x^{5}+27x^{6}+2x^{7} (40)
=k=07rkcxk.\displaystyle=\sum_{k=0}^{7}r_{k}^{c}x^{k}. (41)

By Lemma 15, the number of ways to place eight rooks on BαB_{\alpha} is is

N0\displaystyle N_{0} =k=07(1)krkc(8k)!\displaystyle=\sum_{k=0}^{7}(-1)^{k}r_{k}^{c}(8-k)! (42)
=1(8!)12(7!)+57(6!)136(5!)+170(4!)105(3!)+27(2!)2(1!)\displaystyle=1(8!)-12(7!)+57(6!)-136(5!)+170(4!)-105(3!)+27(2!)-2(1!) (43)
=8062.\displaystyle=8062. (44)

Therefore there are 80628062 ménage permutations in S12S_{12} that start with the prefix (3,6,1,8)(3,6,1,8).

We can now repeatedly use the above counting technique in conjunction with Theorem 9 to unrank derangements.

Example 37.

There are A000179(8)=4738A000179(8)=4738 ménage permutations on 88 letters. Table 3 shows the steps of the algorithm that determines that the 10001000th ménage permutation in lexicographic order is

w1000=3 5 4 8 2 7 1 6.w_{1000}=3\ 5\ 4\ 8\ 2\ 7\ 1\ 6. (45)
α\alpha #prefix(α)\#\operatorname{prefix}(\alpha) index range block sizes unrankM(i)\operatorname{unrank}{M}(i)
11 0 (0,0](0,0] - f1000M(1,0)f_{1000}{M}(1,0)
22 787787 (0,787](0,787] (1,11)(1,11) f1000M(2,0)f_{1000}{M}(2,0)
33 791791 (787,1578](787,1578] (3,9)(3,9) f1000M(3,787)f_{1000}{M}(3,787)
3131 0 (787,787](787,787] - f1000M(31,787)f_{1000}{M}(31,787)
3232 0 (787,787](787,787] - f1000M(32,787)f_{1000}{M}(32,787)
3333 0 (787,787](787,787] - f1000M(33,787)f_{1000}{M}(33,787)
3434 159159 (787,946](787,946] (1,7)(1,7) f1000M(34,787)f_{1000}{M}(34,787)
3535 166166 (946,1112](946,1112] (1,2,5)(1,2,5) f1000M(35,946)f_{1000}{M}(35,946)
351351 2424 (946,970](946,970] (0,2,5)(0,2,5) f1000M(351,946)f_{1000}{M}(351,946)
\cdots 0 (970,970](970,970] -
354354 3434 (970,1004](970,1004] (0,5)(0,5) f1000M(354,970)f_{1000}{M}(354,970)
35413541 55 (970,975](970,975] (0,5)(0,5) f1000M(3541,970)f_{1000}{M}(3541,970)
35423542 55 (975,980](975,980] (0,5)(0,5) f1000M(3542,975)f_{1000}{M}(3542,975)
\cdots 0 (980,980](980,980] -
35463546 88 (980,988](980,988] (0,3)(0,3) f1000M(3546,980)f_{1000}{M}(3546,980)
35473547 1010 (988,998](988,998] (0,2,1)(0,2,1) f1000M(3547,988)f_{1000}{M}(3547,988)
35483548 66 (998,1004](998,1004] (0,4)(0,4) f1000M(3548,998)f_{1000}{M}(3548,998)
3548135481 11 (998,999](998,999] (0,4)(0,4) f1000M(35481,998)f_{1000}{M}(35481,998)
3548235482 11 (999,1000](999,1000] (0,4)(0,4) f1000M(35482,999)f_{1000}{M}(35482,999)
354821354821 0 (999,999](999,999] (3)(3) f1000M(354821,999)f_{1000}{M}(354821,999)
\cdots 0 (999,999](999,999] -
354827354827 11 (999,1000](999,1000] (0,1)(0,1) f1000M(354827,999)f_{1000}{M}(354827,999)
35482713548271 11 (999,1000](999,1000] (0)(0) f1000M(3548271,999)f_{1000}{M}(3548271,999)
3548271635482716 11 (999,1000](999,1000] ()() f1000M(35482716,999)f_{1000}{M}(35482716,999)
Table 3: The recursive computation of the 1000th ménage permutation.

6 Generalizations and open questions

In this final section we explore several possible future directions for applying these ideas in new contexts. We can potentially apply these unranking techniques to position-restricted permutations, permutations that satisfy certain inequalities with respect to a permutation statistic, or words that avoid or match certain patterns.

6.1 Other restricted permutations

In a 2014 paper about finding linear recurrences for derangements, ménage permutations and other restricted permutations, Doron Zeilberger introduces a more general family of restricted permutations.

Definition 38 ([10]).

Let SS\subset\mathbb{Z} be a finite collection of integers. An SS-avoiding permutation is a permutation πSn\pi\in S_{n} such that

π(i)is0(modn)for alli[n]andsS.\pi(i)-i-s\not\equiv 0\pmod{n}\hskip 7.11317pt\text{for all}\hskip 7.11317pti\in[n]\hskip 7.11317pt\text{and}\hskip 7.11317pts\in S. (46)
Example 39.

In terms of SS-avoiding permutations,

  • ordinary permutations are \emptyset-avoiding permutations,

  • derangements are {0}\{0\}-avoiding permutations, and

  • ménage permutations are {1,0}\{-1,0\}-avoiding permutations.

The results in the previous sections straightforwardly adapt to the cases of unranking {i}\{i\}-avoiding and {i,i+1}\{i,i+1\} avoiding permutations.

Open Question 40.

For arbitrary finite subsets SS\subset\mathbb{Z}, do there exist efficient unranking algorithms on SS-avoiding permutations?

The techniques used to unrank derangements and ménage permutations do not appear to generalize even to superficially similar domains. So, in the spirit of Richard Arratia’s bounties, it is only fair to offer one of my own.

Problem 41 ($100 question).

Do there exist efficient unranking algorithms on {1,1}\{-1,1\}-avoiding permutations?

The main obstruction to using the techniques from Section 5 to resolve this question is that placing a rook and deleting a column does not necessarily cause the left and right sides of that column to be disjoint. As such, unranking {1,1}\{-1,1\}-avoiding permutations appears to require a genuinely novel insight.

6.2 Permutation statistics

Another area for exploration is unranking permutations with a given permutation statistic.

Open Question 42.

Let inv:Sn0\operatorname{inv}\colon S_{n}\rightarrow\mathbb{N}_{\geq 0} be the map that counts inversions of a permutation. Since inv\operatorname{inv} is a Mahonian statistic, the generating function for the number of permutations πSn\pi\in S_{n} such that inv(π)=k\operatorname{inv}(\pi)=k is given by the qq analog of n!n!, n!qn!_{q}.

Does there exist an efficient unranking function on the set

nk={πSn|inv(π)=k},\mathscr{I}_{n}^{k}=\{\pi\in S_{n}|\operatorname{inv}(\pi)=k\}, (47)

and if so, how does one construct it?

We can, of course, substitute inv\operatorname{inv} with any other permutation statistic of interest.

6.3 Pattern avoidance

In the field of combinatorics on words, there exists a notion of patterns and instances of a pattern. At this level of informality, this is probably best illustrated with an example (with undefined words in bold).

Example 43.

The word 11000101101100010110 is an instance of the pattern ABAABA with A=110A=110 and B=0010B=0010. The word 3212321321232123213212 is said to match the pattern CCCC with C=321C=321 because it contains a substring of the form 321321321321.

Open Question 44.

Given a pattern PP, is it possible to unrank words of length nn over an alphabet 𝒜\mathscr{A} that are not instances of the pattern PP? That match the pattern PP? That don’t match the pattern PP?

6.4 Prefixes of Lyndon words

There are other collections of finite words that might be amenable to some of the above techniques. In particular, Kociumaka, Radoszewski, and Rytter [2] give polynomial time algorithms for unranking Lyndon words. We have some conjectures about prefixes of Lyndon words and open questions about other restricted words.

Definition 45.

A Lyndon word is a string over an alphabet of letters that is the unique minimum with respect to all of its rotations.

Example 46.

0010100101 is a Lyndon word because

00101=min{00101,01010,10100,01001,10010}00101=\min\{00101,01010,10100,01001,10010\} (48)

is the unique minimum of all of its rotations.

011011011011 is not a Lyndon word because while

011011=min{011011,110110,101101,011011,110110,101101},011011=\min\{011011,110110,101101,011011,110110,101101\}, (49)

it is not the unique minimum. (That is, rotating it three positions returns it to itself.)

Definition 47 ([7]).

Suppose that {ai}i=1\{a_{i}\}_{i=1}^{\infty} and {bi}i=1\{b_{i}\}_{i=1}^{\infty} are integer sequences related by

1+n=1bnxn=i=11(1xi)ai.1+\sum_{n=1}^{\infty}b_{n}x^{n}=\prod_{i=1}^{\infty}\frac{1}{(1-x^{i})^{a_{i}}}. (50)

Then {bi}i=1\{b_{i}\}_{i=1}^{\infty} is said to be the Euler transform of {ai}i=1\{a_{i}\}_{i=1}^{\infty}, denoted E({ai}i=1)={bi}i=1\pazocal{E}(\{a_{i}\}_{i=1}^{\infty})=\{b_{i}\}_{i=1}^{\infty}.

Definition 48.

Let α={nα}n=1\mathscr{L}_{\alpha}=\{\ell_{n}^{\alpha}\}_{n=1}^{\infty} where nα\ell_{n}^{\alpha} is the number of Lyndon words with prefix α\alpha and length nn over the alphabet {0,1}\{0,1\}.

Conjecture 49.

The Euler transform of the number of Lyndon words with prefix α\alpha and length nn over the alphabet {0,1}\{0,1\}, E(α)={tnα}n=1\pazocal{E}(\mathscr{L_{\alpha}})=\{t_{n}^{\alpha}\}_{n=1}^{\infty}, follows a linear recurrence for all nNαn\geq N_{\alpha}.

This conjecture and the following conjectures are based on the data in Table 4.

α\alpha α\mathscr{L}_{\alpha} and E(α)\pazocal{E}(\mathscr{L}_{\alpha}) Conjectured recurrence
0 1 1 2 3 6 9 18 30 56 99 186 335 an+1=2ana_{n+1}=2a_{n} n2n\geq 2
1 1 2 4 8 16 32 64 128 256 512 1024
0000 0 0 1 2 4 7 14 25 48 88 168 310 an+1=2ana_{n+1}=2a_{n} n4n\geq 4
1 0 0 1 2 4 8 16 32 64 128 256
0101 0 1 1 1 2 2 4 5 8 11 18 25 an+2=an+1+ana_{n+2}=a_{n+1}+a_{n} n1n\geq 1
1 0 1 1 2 3 5 8 13 21 34 55
000000 0 0 0 1 2 4 8 15 30 57 112 214 an+1=2ana_{n+1}=2a_{n} n5n\geq 5
1 0 0 0 1 2 4 8 16 32 64 128
001001 0 0 1 1 2 3 6 10 18 31 56 96 an+3=an+2+an+1+ana_{n+3}=a_{n+2}+a_{n+1}+a_{n} n1n\geq 1
1 0 0 1 1 2 4 7 13 24 44 81
010010 0 0 0 0 1 1 2 3 5 7 12 18 an+2=an+1+ana_{n+2}=a_{n+1}+a_{n} n5n\geq 5
1 0 0 0 0 1 1 2 3 5 8 13
011011 0 0 1 1 1 1 2 2 3 4 6 7 an+3=an+2+ana_{n+3}=a_{n+2}+a_{n} n1n\geq 1
1 0 0 1 1 1 2 3 4 6 9 13
00000000 0 0 0 0 1 2 4 8 16 31 62 121 an+1=2ana_{n+1}=2a_{n} n6n\geq 6
1 0 0 0 0 1 2 4 8 16 32 64
00010001 0 0 0 1 1 2 4 7 14 26 50 93 an+4=an+3+an+2+an+1+ana_{n+4}=a_{n+3}+a_{n+2}+a_{n+1}+a_{n} n1n\geq 1
1 0 0 0 1 1 2 4 8 15 29 56
00100010 0 0 0 0 1 1 3 5 9 16 30 53 an+3=an+2+an+1+ana_{n+3}=a_{n+2}+a_{n+1}+a_{n} n6n\geq 6
1 0 0 0 0 1 1 3 5 9 17 31
00110011 0 0 0 1 1 2 3 5 9 15 26 43 an+4=an+3+an+2+ana_{n+4}=a_{n+3}+a_{n+2}+a_{n} n1n\geq 1
1 0 0 0 1 1 2 3 6 10 18 31
01010101 0 0 0 0 1 1 2 3 5 7 12 18 an+2=an+1+ana_{n+2}=a_{n+1}+a_{n} n5n\geq 5
1 0 0 0 0 1 1 2 3 5 8 13
01100110 0 0 0 0 0 0 1 1 1 2 3 4 an+3=an+2+ana_{n+3}=a_{n+2}+a_{n} n6n\geq 6
1 0 0 0 0 0 0 1 1 1 2 3
01110111 0 0 0 1 1 1 1 1 2 2 3 3 an+4=an+3+ana_{n+4}=a_{n+3}+a_{n} n1n\geq 1
1 0 0 0 1 1 1 1 2 3 4 5
Table 4: A table of conjectures about the number of Lyndon words of length nn and prefix α\alpha. Each row contains a prefix α\alpha, a sequence counting Lyndon words with α\alpha (α\mathscr{L}_{\alpha}), the Euler transformation of that sequence (E(α)\pazocal{E}(\mathscr{L}_{\alpha})), a conjectured recurrence for the Euler-transformed sequence, and the valid range for the conjectured recurrence.

We start with two specific conjectures about two families of prefixes.

Conjecture 50.

For k1k\geq 1, let α\mathscr{L}_{\alpha} be the sequence of the number of Lyndon words of length nn with prefix α=(0,0,,0){\alpha=(0,0,\dots,0)} of length kk over the alphabet {0,1}\{0,1\}. Then the Euler transform E(α)={tnα}n=1\pazocal{E}(\mathscr{L}_{\alpha})=\{t_{n}^{\alpha}\}_{n=1}^{\infty} follows the linear recurrence tn+1α=2tnαt_{n+1}^{\alpha}=2t_{n}^{\alpha} for all nk+2n\geq k+2.

Conjecture 51.

For k2k\geq 2 let α\mathscr{L}_{\alpha} be the sequence of the number of Lyndon words of length nn with prefix α=(1,0,0,,0){\alpha=(1,0,0,\dots,0)} of length kk over the alphabet {0,1}\{0,1\}. Then the Euler transform E(α)={tnα}n=1\pazocal{E}(\mathscr{L}_{\alpha})=\{t_{n}^{\alpha}\}_{n=1}^{\infty} follows the linear recurrence tn+kα=i=0k1tn+iαt_{n+k}^{\alpha}=\sum_{i=0}^{k-1}t_{n+i}^{\alpha} for all n1n\geq 1.

And more broadly, we have a conjecture in the case that the prefix is not the zero sequence.

Conjecture 52.

Let α\mathscr{L}_{\alpha} be the sequence of the number of Lyndon words of length nn with prefix α\alpha over the alphabet {0,1}\{0,1\} such that α\alpha contains at least one 11. Then the Euler transform of the sequence, E(α)={tnα}n=1\pazocal{E}(\mathscr{L_{\alpha}})=\{t_{n}^{\alpha}\}_{n=1}^{\infty}, follows the linear recurrence where all terms have coefficients of 0 or 11.

Open Question 53.

If, as the evidence suggests, E(α)\pazocal{E}(\mathscr{L_{\alpha}}) follows a linear recurrence, what is the length of the recurrence and what are the coefficients of the recurrence as a function of α\alpha?

References

  • [1] Irving Kaplansky and John Riordan “The problem of the rooks and its applications” In Duke Mathematical Journal 13.2 Duke University Press, 1946, pp. 259–268 DOI: 10.1215/S0012-7094-46-01324-5
  • [2] Tomasz Kociumaka, Jakub Radoszewski and Wojciech Rytter “Computing k-th Lyndon Word and Decoding Lexicographically Minimal de Bruijn Sequence” In Combinatorial Pattern Matching Springer International Publishing, 2014, pp. 202–211
  • [3] Kenji Mikawa and Ken Tanaka “Lexicographic ranking and unranking of derangements in cycle notation” In Discret. Appl. Math. 166, 2014, pp. 164–169
  • [4] OEIS Foundation Inc. “The On-Line Encyclopedia of Integer Sequences”, 2021 URL: https://oeis.org/
  • [5] John Riordan “An Introduction to Combinatorial Analysis” USA: Princeton University Press, 1980
  • [6] V.. Shevelev “Some problems of the theory of enumerating the permutations with restricted positions” In Journal of Soviet Mathematics 61.4, 1992, pp. 2272–2317 DOI: 10.1007/BF01104103
  • [7] Neil James Alexander Sloane and Simon Plouffe “Encyclopedia of Integer Sequences” San Diego: Academic Press, 1995
  • [8] Richard P. Stanley “Enumerative Combinatorics: Volume 1” USA: Cambridge University Press, 2011
  • [9] L.G. Valiant “The complexity of computing the permanent” In Theoretical Computer Science 8.2, 1979, pp. 189–201 DOI: https://doi.org/10.1016/0304-3975(79)90044-6
  • [10] Doron Zeilberger “Automatic Enumeration of Generalized Ménage Numbers” In Séminaire Lotharingien de Combinatoire 71.B71a, 2014