(Un)ranking Permutation Classes


Abstract

Permutations avoiding a pattern of length three are enumerated by the Catalan numbers. In this work, we present methods for ranking and unranking such permutations in lexicographic or colexicographic order.

1 Introduction↩︎

For an ordered set of combinatorial objects we can ask which position in the ordered list of objects a given object has, or conversely determine the objet in a given position. Formally for a total ordered set \(C\) we have:

Definition 1. The ranking over \(C\) is the function \[\mathrm{rank}_{\,C} : C \rightarrow \{0,1,\dots,|C|-1\}\] that maps \(x\) to the number of elements of \(C\) that are less than \(x\). The unranking is the inverse function \[\mathrm{unrank}_{\,C} : \{0,1,\dots,|C|-1\} \rightarrow C\] that maps \(p\) to the object \(x\) such that \(\mathrm{rank}_{\,C}(x)=p\).

Here, we restrict ourselves to sets \(C\) of restricted permutations of the same length in one-line representation, ordered lexicographically or colexicographically. Whenever the set \(C\) is clear from the context, the subscripts in the functions \(\mathrm{rank}\) and \(\mathrm{unrank}\) will be omitted.

Lehmer [@Lehmer] may have been the first to suggest studying combinatorial objects by imposing an order on them and then developing corresponding ranking and unranking algorithms. Ruskey in his book [@Ruskey] considers such algorithms for unrestricted permutations in both (co)lexicographic order and Steinhaus–Johnson–Trotter Gray code order. Among recent contributions in the field, we can cite [@CurielGenitrini; @GenitriniPepin], and more particularly [@Kagey] for ranking and unranking algorithms for restricted permutations (derangements and ménage permutations).

Here, we are interested in ranking and unranking algorithms for simple yet non-trivial combinatorial classes, namely permutations avoiding a pattern of length three.

Aside from their intrinsic theoretical interest, such methods also have practical value. Indeed, given an unranking method, the ordered list can be computed as \[\mathrm{unrank}(0),\mathrm{unrank}(1),\mathrm{unrank}(2),\dots\] and in the context of random sampling, \(\mathrm{unrank}(p)\) yields a random object as long as \(p\) is a random integer less than the cardinality of the combinatorial class. In addition, having a ranking method, the successor of a given object \(x\) can be computed as \(\mathrm{unrank}(\mathrm{rank}(x)+1)\). Even though these techniques are not efficient in general, they can provide hints for solutions to the underlying problems and enhance understanding of the concerned objects.

A permutation of the set \([n] = \{1, 2, \dots, n\}\) is a bijection from \([n]\) onto itself, and \(\mathfrak{S}_n\) denotes the set of such permutations. We will represent permutations by their one-line notation, that is, the permutation \(\pi \in \mathfrak{S}_n\) by the word \(\pi(1)\pi(2)\cdots \pi(n) \in [n]^n\).

Let \(k\) and \(n\) be integers, \(0<k\leq n\), and \(\sigma=\sigma(1)\sigma(2)\cdots \sigma(k)\in \mathfrak{S}_k\) and \(\pi=\pi(1)\pi(2)\cdots \pi(n)\in \mathfrak{S}_n\) be two permutations. One says that \(\pi\) contains \(\sigma\) if \(\pi\) contains a (not necessarily contiguous) subsequence \(\pi(i_1)\pi(i_2)\cdots\pi(i_k)\), \(i_1 < i_2 <\cdots < i_k\), order isomorphic to \(\sigma\); otherwise one says that \(\pi\) avoids \(\sigma\), or \(\pi\) is \(\sigma\)-avoiding. In this context \(\sigma\) is called a (classical) pattern, and the set of length-\(n\) permutations avoiding the pattern \(\sigma\) is denoted by \(\mathrm{Av}_n(\sigma)\), and \(\mathrm{Av}(\sigma)=\cup_{n\geq 0}\mathrm{Av}_n(\sigma)\).

The lexicographic order on \([n]^n\) induces the lexicographic order on permutations in \(\mathfrak{S}_n\) in their one-line representation, and it generalizes to words over any ordered alphabet. For two same length words \(v=v_1v_2\dots v_n\) and \(w=w_1w_2\dots w_n\) one says that \(v\) is less than \(w\) in colexicographic order if \(v_nv_{n-1}\dots v_1\) is less than \(w_nw_{n-1}\dots w_1\) in lexicographic order.

In the following we need the next easy to check result.

Remark 1. If \(C\) is a set of length-\(n\) words over an ordered alphabet, then the rank of \(w=w_1w_2\dots w_n\in C\), in lexicographic order, is \[\mathrm{rank}(w)= \sum_{i=1}^{n}|E_i|,\] where for each \(i\), \(E_i\) is the set of words \(u_1u_2\dots u_n\in C\) with \(u_j=w_j\) for \(j<i\) and \(u_i<w_i\).

The number of permutations in \(\mathfrak{S}_n\) avoiding a pattern of length three is given by the Catalan number \(c_n=\frac{1}{n+1}{{2n}\choose{n}}\), see [@Knuth], which is sequence A000108 in [@OEIS]. Often it is convenient to represent pattern avoiding permutations \(\pi\in \mathfrak{S}_n\) by an \(n\times n\) array with a dot in each one of the squares \((i,\pi(i))\). See Figures [Fig3perms] (b) and (c) for two examples.

2 Avoiding the \(231\) pattern↩︎

The reduction of a word \(\alpha\) over a finite alphabet \(A \subset \mathbb{N}\) is obtained by replacing each occurrence of the smallest symbol by 1, each occurrence of the second smallest symbol by 2, and so on. The direct sum of two permutations \(\sigma\in \mathfrak{S}_k\) and \(\tau\in \mathfrak{S}_\ell\), denoted by \(\sigma\oplus\tau\), is the permutation \(\pi\in \mathfrak{S}_{k+\ell}\) where \(\pi(1)\pi(2)\cdots \pi(k)=\sigma\) and \(\pi(k+1)\pi(k+2)\cdots \pi(k+\ell)\) is a permutation of \(\{k+1,k+2,\dots, k+\ell\}\) that reduces to \(\tau\). Similarly, the skew sum of \(\sigma\) and \(\tau\), denoted by \(\sigma\ominus\tau\), is the permutation \(\pi\in \mathfrak{S}_{k+\ell}\) where \(\pi(1)\pi(2)\cdots \pi(k)\) is a permutation of \(\{\ell+1,\ell+2,\dots, \ell+k\}\) that reduces to \(\sigma\) and \(\pi(k+1)\pi(k+2)\cdots \pi(k+\ell)=\tau\). With this notation, if \(\pi = 1 \ominus \sigma\), we have \(\pi(1) = k+1\).

The next easy to understand fact is folklore.

Fact 2. A permutation avoids \(231\) if and only if it has the form \((1 \ominus \sigma)\oplus \tau\), where \(\sigma\) and \(\tau\) are (possibly empty) \(231\)-avoiding permutations.

See Figure [Fig3perms] (a) for the general shape of a 231-avoiding permutation, and (b) for the 231-avoiding permutation \(2173546= (1\ominus 1)\oplus (1\ominus (1\oplus ((1\ominus 1)\oplus 1)))\).

Proposition 3. Let \(\pi=\pi(1)\pi(2)\cdots \pi(n)=(1\ominus \sigma)\oplus \tau\) be a length-\(n\) 231-avoiding permutation, with \(\sigma\in \mathrm{Av}_k(231)\) and \(\tau\in \mathrm{Av}_\ell(231)\) (and thus, \(n=k+\ell+1\)). Then the rank of \(\pi\), in lexicographical order, is given recursively by \[\mathrm{rank}_{\mathrm{Av}_n(231)}(\pi)=\sum_{i=1}^{\pi(1)-1} c_i\cdot c_{n-i-1} + \mathrm{rank}_{\mathrm{Av}_k(231)}(\sigma)\cdot c_\ell + \mathrm{rank}_{\mathrm{Av}_\ell(231)}(\tau).\]

Proposition 4. Let \(n,p\) be integers, \(0\leq p<c_n\). The permutation \(\mathrm{unrank}_{\mathrm{Av}_n(231)}(p)\) is obtained recursively as follows. Determine the smallest \(k\) such that \[p< \sum_{i=0}^kc_ic_{n-i-1}\] and denote

  • \(s:=\sum_{i=0}^{k-1} c_ic_{n-i-1}\),

  • \(u:=\lfloor \frac{p-s}{c_{n-k}}\rfloor\), and

  • \(v:=p-s-u\cdot c_{n-k}\).

Then \(\mathrm{unrank}_{\mathrm{Av}_n(231)}(p)=(1\ominus \sigma)\oplus\tau\) where

  • \(\sigma=\mathrm{unrank}_{\mathrm{Av}_{k-1}(231)}(u)\), and

  • \(\tau=\mathrm{unrank}_{\mathrm{Av}_{n-k}(231)}(v)\).

In fact, the value \(k\) defined in the previous proposition is the first entry of \(\mathrm{unrank}_{\mathrm{Av}_n(231)}(p)\).

Figure 1: The arrays representation of: (a) the permutation \pi=(1\ominus \sigma)\oplus \tau; (b) the 231-avoiding permutation 2173546; (c) the 321-avoiding permutation 2153746 corresponding to the Dyck word bbaabbbaabbaaa which codes the over-diagonal path in bold.

3 Avoiding the \(321\) pattern↩︎

A left-to-right maximum position (or lrmp for short) in a permutation \(\pi\) is an index \(i\) such that, for all \(j < i\), we have \(\pi(j) < \pi(i)\). The value \(\pi(i)\) is then called a left-to-right maximum value (or lrmv for short). An excedance is an index \(i\) such that \(\pi(i) > i\). It is known (see, e.g., [@Reifegerste]) that a permutation is \(321\)-avoiding if and only if both the subsequence of its left-to-right maximum values and the subsequence of its remaining elements are increasing. Moreover, such a permutation is uniquely determined by the set \(\{(i,\pi(i))\}\), where \(i\) ranges over its left-to-right maximum positions. Even though we will not use it later on, it is worth mentioning that, for a \(321\)-avoiding permutation, the left-to-right maxima positions coincide with the excedances of the permutation.

Another combinatorial class closely related to length-3 pattern-avoiding permutations is that of Dyck words. Let \(D_n\) denote the set of Dyck words of semilength \(n\), that is, words \(w \in \{a,b\}^{2n}\) containing the same number of occurrences of \(a\) and \(b\), and such that every prefix of \(w\) contains at least as many \(b\)’s as \(a\)’s. (We adopt this unusual notation for convenience; traditionally, the roles of \(a\) and \(b\) are interchanged.) Dyck words in \(D_n\) are counted by the Catalan number \(c_n\).

The bijection \(\psi:\mathrm{Av}_n(321)\to D_n\) appeared originally in [@Knuth] in a slightly different form. Consider the array representation corresponding to \(\pi\in \mathrm{Av}_n(321)\) and the path with east and north steps along the edges of the array that goes from the lower-left corner to the upper-right corner of the array, leaving all the dots to the right and remaining always as close to the main diagonal as possible. Let \(P\) be the resulting path. Then \(\psi(\pi)\) can be obtained from \(P\) just by reading its steps, a north step corresponding to the letter \(b\) and an east step to the letter \(a\). See Figure [Fig3perms] (c) for an example.

The next proposition says that \(\psi\) preserves the lexicographic order.

Proposition 5. If \(\pi,\tau\in \mathrm{Av}_n(321)\) with \(\pi<\tau\) in lexicographic order, then \(\psi(\pi)<\psi(\tau)\) in lexicographic order as well.

Ballot sequences are prefixes of Dyck words. We denote by \(B(i,j)\) the set of ballot sequences with \(i\) occurrences of \(b\) and \(j\) occurrences of \(a\). Similarly, we denote by \(\bar{B}(i,j)\) the set of suffixes of Dyck words.

Clearly, \(B(i,j)\) is empty if \(i<j\), and there is a unique word in \(B(i,0)\), namely \(b^i\). Moreover, if we ask to obtain a ballot sequence \(w\in B(i,j)\) by appending a single letter to a shorter one, then

  • if \(i=j\), this can be obtained in a unique way: \(w=ua\) for some \(u\in B(i,j-1)\), and

  • if \(0<j<i\), this can be obtained in two different ways, namely \(w=ua\), for some \(u\in B(i,j-1)\), and \(w=ub\), for some \(u\in B(i-1,j)\).

The considerations above (see for instance [@Kasa] and the references therein) lead us to the following recursive definition of \(t(i, j)\), the cardinality of \(B(i, j)\): \[t(i,j)=\left\{ \begin{array}{lll} 0 & \mathrm{if} & j>i,\\ 1 & \mathrm{if} & j=0,\\ t(i,j-1) & \mathrm{if} & j=i>0,\\ t(i-1,j)+t(i,j-1) & \mathrm{if} & 0<j<i. \end{array} \right. \label{recurenceT}\tag{1}\]

The sequence \(\bigl(t(i, j)\bigr)_{0\leq j\leq i}\) is the sequence A009766 in [@OEIS], which has the closed form: \[t(i,j)={i+j\choose i}\frac{i-j+1}{i+1},for0\leq j\leq i,\] and its first values are shown in Table [DyckArray].

Proposition 6 ([@Bailey]). For the double indexed sequence \(s(i,j)=\sum_{k=0}^j t(i,k)\) we have

\[s(i,j)=t(i+1,j).\]

c \c & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7
& 1
1 & 1 & 1
2 & 1 & 2 & 2
3 & 1 & 3 & 5 & 5
4 & 1 & 4 & 9 & 14 & 14
5 & 1 & 5 & 14 & 28 & 42 & 42
6 & 1 & 6 & 20 & 48 & 90 & 132 & 132
7 & 1 & 7 & 27 & 75 & 165 & 297 & 429 & 429

Representing a \(321\)-avoiding permutation by both the sequence of its left-to-right maxima and the corresponding Dyck word (see Figure [twoArrays]), we have:

Proposition 7. Let \(\pi\) be a length-\(n\) \(321\)-avoiding permutation, and let \((m_i,\ell_i)_{i=1}^k\) be the sequence where the \(m_i\)’s are the lrmp’s of \(\pi\), and \(\ell_i=\pi(m_i)\) are the lrmv’s. Then,

\[\mathrm{rank}_{\mathrm{Av}_n({321})} (\pi)=\sum_{i=1}^k \left( s(n-m_i+1,n-\max\{\ell_{i-1},m_i\})-s(n-m_i+1,n-\ell_i+1)\right),\] where for convenience \(\ell_0=0\).

Figure 2: The black dots correspond to two consecutive lrmp’s, m_{i-1} and m_i, of a 321-avoiding permutation \pi, together with their lrmv’s \ell_{i-1}=\pi(m_{i-1}) and \ell_i=\pi(m_i). Let \sigma be another 321-avoiding permutation of the same length that is smaller than \pi in lexicographic order and has the same first (i-1) lrmp’s m_1, m_2, \dots, m_{i-1}, and the same first (i-1) lrmv’s, that is\sigma(m_j)=\pi(m_j) for j<i.(a) The gray dots indicate the possible values for the ith lrmv of \sigma if m_i is still an lrmp of \sigma, with \sigma(m_i) < \ell_i.(b) The gray dot indicates the value of \sigma(m_i) if m_i is no longer an lrmp of \sigma (and thus \ell_{i-1} \geq m_i).
Figure 3: Procedure for computing the Dyck word of semilength n and rank p.Here \overline{t}(i,j)=\mathrm{card}(\overline{B}(i,j)), so \overline{t}(i,j)=t(j,i).

Let \(\pi\) be a \(321\)-avoiding permutation of length \(n\), and let \(w = w_1 w_2 \dots w_{2n}\) be its corresponding Dyck word, that is \(w=\Psi(\pi)\in D_n\). Obviously, the number of left-to-right maxima in \(\pi\) is equal to the number of occurrences of the factor \(ba\) (i.e., number of peaks) in \(w\). Moreover, if \(w_{j-1} w_j\) is the \(i\)th occurrence of the factor \(ba\), then the \(i\)th left-to-right maximum \((m_i,\ell_i)\) of \(\pi\) is given by \(m_i = |w_1 w_2 \dots w_j|_a\) and \(\ell_i = |w_1 w_2 \dots w_j|_b.\)

Proposition 8. Let \(n,p\) be integers with \(0 \le p < c_n\). The permutation \(\mathrm{unrank}_{\mathrm{Av}_n(321)}(p)\) is obtained as follows:

  • Run the UnrankDyck procedure in Algorithm [UnrankDyck] to construct the Dyck word \(w\) of semilength \(n\) and rank \(p\).

  • Using the considerations above, construct the set \(\{(m_i,\pi(m_i))\}_{i=1}^k\) of left-to-right maxima (values, positions) of the permutation \(\Psi^{-1}(w)\).

  • Finally, construct the permutation \(\mathrm{unrank}_{\mathrm{Av}_n(321)}(p)\).

4 Trivial symmetries↩︎

Here, based on the previous results and the trivial symmetries between permutation classes, we derive ranking and unranking algorithms for the remaining length-3 avoiding patterns: 123, 132, 213, and 312.

Let \(\mathfrak c\) be the complement operation on \(\mathfrak{S}_n\), that is the involution defined as: \(\sigma=\mathfrak c(\pi)\) if \(\sigma(i)=n+1-\pi(i)\) for \(1\leq i\leq n\). Clearly, the complement operation \(\mathfrak c\) induces a bijection between \(\mathrm{Av}_n(231)\) and \(\mathrm{Av}_n(213)\), and between \(\mathrm{Av}_n(321)\) and \(\mathrm{Av}_n(123)\). Moreover, if \(\pi<\rho\) in lexicographic order, then \(\mathfrak c(\rho)<\mathfrak c(\pi)\) in lexicographic order. With the considerations above we have:

Proposition 9. Let \(\tau\) be one of the length-3 patterns \(213\) or \(123\) (and so, \(\mathfrak c(\tau)\) is \(231\) or \(321\)). Then, for \(n\geq 0\) and

  • for \(\pi\in \mathrm{Av}_n(\tau)\), \(\mathrm{rank}_{\mathrm{Av}_n(\tau)}(\pi) = c_n-\mathrm{rank}_{\mathrm{Av}_n(\mathfrak c(\tau))} (\mathfrak c(\pi))-1\), and

  • for an integer \(p\), \(0\leq p<c_n\), \(\mathrm{unrank}_{\mathrm{Av}_n(\tau)}(p) = \mathrm{unrank}_{\mathrm{Av}_n(\mathfrak c(\tau))}(c_n-p-1)\).

Let now \(\mathfrak r\) be the reverse operation on \(\mathfrak{S}_n\), that is the involution on \(\mathfrak{S}_n\) defined as: \(\sigma=\mathfrak r(\pi)\) if \(\sigma(i)=\pi(n+1-i)\) for \(1\leq i\leq n\). The reverse operation \(\mathfrak r\) induces a bijection between \(\mathrm{Av}_n(231)\) and \(\mathrm{Av}_n(132)\), and between \(\mathrm{Av}_n(213)\) and \(\mathrm{Av}_n(312)\). Moreover, if \(\pi<\rho\) in lexicographic order, then \(\mathfrak r(\pi)<\mathfrak r(\rho)\) in colexicographic order. Denoting \(\mathrm{rank}^*\) (resp. \(\mathrm{unrank}^*\)) the ranking (resp. unranking) function in colexicographic order we have:

Proposition 10. Let \(\tau\) be one of the length-3 patterns \(132\) or \(312\) (and so, \(\mathfrak r(\tau)\) is \(231\) or \(213\)). Then, for \(n\geq 0\) and

  • for \(\pi\in \mathrm{Av}_n(\tau)\), \(\mathrm{rank}^*_{\,\mathrm{Av}(\tau)}(\pi)=\mathrm{rank}_{\,\mathrm{Av}(\mathfrak r(\tau))}(\mathfrak r(\pi))\),

  • for an integer \(p\), \(0\leq p<c_n\), \(\mathrm{unrank}^*_{\,\mathrm{Av}(\tau)}(p)=\mathfrak r(\mathrm{unrank}_{\,\mathrm{Av}(\mathfrak r(\tau))}(p))\).

Open problems and future directions:

Even when the counting sequences for permutations avoiding multiple patterns of length three are trivial (e.g., \(2^{n-1}\), Fibonacci numbers), ranking and unranking methods in lexicographic order still seem less straightforward. It would also be interesting to consider the avoidance of patterns of size greater than three. Finally, the efficient implementation of these methods remains an open problem.

[@*]