3.2 Inversions, length and Lehmer codes
Throughout this section, let \(n \in \mathbb {N}\). We write \(S_n\) for the symmetric group on \([n] = \{ 1,2,\ldots ,n\} \).
3.2.1 Inversions and lengths
Let \(n\in \mathbb {N}\) and \(\sigma \in S_{n}\).
(a) An inversion of \(\sigma \) means a pair \(\left(i,j\right)\) of elements of \(\left[n\right]\) such that \(i{\lt}j\) and \(\sigma \left(i\right) {\gt}\sigma \left(j\right)\).
(b) The length (also known as the Coxeter length) of \(\sigma \) is the # of inversions of \(\sigma \). It is called \(\ell \left( \sigma \right)\).
The identity permutation \(\operatorname {id} \in S_n\) has no inversions: \(\operatorname {inv}(\operatorname {id}) = \emptyset \) and \(\ell (\operatorname {id}) = 0\).
The identity preserves the natural order: if \(i {\lt} j\) then \(\operatorname {id}(i) = i {\lt} j = \operatorname {id}(j)\), so no pair \((i,j)\) with \(i {\lt} j\) satisfies \(\operatorname {id}(i) {\gt} \operatorname {id}(j)\).
Let \(n\in \mathbb {N}\).
(a) For any \(\sigma \in S_{n}\), we have \(\ell \left(\sigma \right) \in \left\{ 0,1,\ldots ,\binom {n}{2}\right\} \).
(b) We have
Indeed, the only permutation \(\sigma \in S_{n}\) with \(\ell \left( \sigma \right)=0\) is the identity map \(\operatorname {id}\).
(c) We have
Indeed, the only permutation \(\sigma \in S_{n}\) with \(\ell \left( \sigma \right)=\binom {n}{2}\) is the permutation with OLN \(n\left( n-1\right)\left(n-2\right)\cdots 21\), often called \(w_{0}\).
(d) If \(n\geq 1\), then
Indeed, the only permutations \(\sigma \in S_{n}\) with \(\ell \left( \sigma \right)=1\) are the simple transpositions \(s_{i}\) with \(i\in \left[ n-1\right]\).
(e) If \(n\geq 2\), then
Indeed, the only permutations \(\sigma \in S_{n}\) with \(\ell \left( \sigma \right)=2\) are the products \(s_{i}s_{j}\) with \(1\leq i{\lt}j{\lt}n\) as well as the products \(s_{i}s_{i-1}\) with \(i\in \left\{ 2,3,\ldots ,n-1\right\} \). If \(n\geq 2\), then there are \(\frac{\left(n-2\right)\left(n+1\right)}{2}\) such products (and they are all distinct).
(f) For any \(k\in \mathbb {Z}\), we have
(a) The set of inversions of \(\sigma \) is a subset of the set of all pairs \((i,j)\) with \(i {\lt} j\), and the latter has cardinality \(\binom {n}{2}\).
(b) If \(\sigma \) has no inversions, then \(\sigma \) is strictly monotone on \([n]\); a strictly monotone permutation on a finite set must be the identity (since if any \(\sigma (i) {\gt} i\), the sum \(\sum _j \sigma (j)\) would strictly exceed \(\sum _j j\), contradicting bijectivity). Conversely, \(\operatorname {id}\) clearly has no inversions.
(c) The longest element \(w_0\) reverses the ordering, so \(w_0(i) = n+1-i\), making every pair \((i,j)\) with \(i{\lt}j\) an inversion. Thus \(\ell (w_0) = \binom {n}{2}\). Conversely, if \(\ell (\sigma ) = \binom {n}{2}\) then \(\operatorname {inv}(\sigma )\) equals the set of all ordered pairs, so \(\sigma \) is strictly antitone. A strictly antitone permutation on \([n]\) must send \(i\) to \(n+1-i\), i.e., \(\sigma = w_0\).
(d) Each simple transposition \(s_i = \operatorname {swap}(i, i+1)\) has exactly one inversion. Conversely, if \(\sigma \) has exactly one inversion \(\{ (a,b)\} \), then \(b = a + 1\) (otherwise an element between \(a\) and \(b\) would create an additional inversion), and \(\sigma = \operatorname {swap}(a, b) = s_a\).
(e) The permutations with exactly 2 inversions are exactly: products \(s_i s_j\) with \(1 \le i {\lt} j {\lt} n\) (non-adjacent transpositions, contributing \(\binom {n-2}{2}\) permutations) and products \(s_i s_{i-1}\) with \(i \in \{ 2, \ldots , n-1\} \) (adjacent 3-cycles, contributing \(n-2\) permutations). These give \(\binom {n-2}{2} + (n-2) = \frac{(n-2)(n+1)}{2}\) in total.
(f) The bijection \(\sigma \mapsto w_0 \sigma \) satisfies \(\ell (w_0\sigma ) = \binom {n}{2} - \ell (\sigma )\), since \(w_0\) swaps inversions and non-inversions.
3.2.2 Helpers for Proposition 3.37
The longest element \(w_0 \in S_n\) is the reversal permutation \(w_0(i) = n+1-i\). It is an involution: \(w_0^2 = \operatorname {id}\) and \(w_0^{-1} = w_0\).
Direct computation.
The longest element \(w_0 \in S_n\) equals the standard reversal permutation from Mathlib. That is, \(w_0(i) = n - 1 - i\) for all \(i \in [n]\).
Both send \(i\) to \(n - 1 - i\); the equality follows by extensionality.
The set of inversions of the longest element \(w_0\) is the set of all pairs \((i,j)\) with \(i {\lt} j\). That is, every ordered pair is an inversion of \(w_0\).
Since \(w_0(i) = n+1-i\), if \(i {\lt} j\) then \(w_0(i) = n+1-i {\gt} n+1-j = w_0(j)\).
The non-inversions of a permutation \(\sigma \in S_n\) are the pairs \((i,j)\) with \(i {\lt} j\) and \(\sigma (i) {\lt} \sigma (j)\). These are exactly the ordered pairs that are not inversions. The inversions and non-inversions partition the set of all \(\binom {n}{2}\) ordered pairs.
For any \(\sigma \in S_n\), we have \(\ell (\sigma ^{-1}) = \ell (\sigma )\).
The map \((a,b) \mapsto (\sigma ^{-1}(b), \sigma ^{-1}(a))\) is a bijection between the inversions of \(\sigma ^{-1}\) and the inversions of \(\sigma \).
For any \(\sigma \in S_n\), the inversions of \(w_0 \sigma \) are exactly the non-inversions of \(\sigma \), and
A pair \((i,j)\) with \(i {\lt} j\) is an inversion of \(w_0 \sigma \) iff \((w_0 \sigma )(i) {\gt} (w_0 \sigma )(j)\) iff \(\sigma (i) {\lt} \sigma (j)\) (since \(w_0\) reverses the order), which is the condition for \((i,j)\) to be a non-inversion of \(\sigma \). The cardinality identity follows since inversions and non-inversions partition the \(\binom {n}{2}\) ordered pairs.
For any \(\sigma , \tau \in S_n\),
This is the triangle inequality for the inversion count.
An inversion \((i,j)\) of \(\sigma \tau \) (i.e., \(i {\lt} j\) and \((\sigma \tau )(i) {\gt} (\sigma \tau )(j)\)) is either an inversion of \(\tau \) (if \(\tau (i) {\gt} \tau (j)\)) or corresponds to an inversion of \(\sigma \) at \((\tau (i), \tau (j))\) (if \(\tau (i) {\lt} \tau (j)\)). Thus \(\operatorname {inv}(\sigma \tau ) \subseteq \operatorname {inv}(\tau ) \cup \tau ^{-1}(\operatorname {inv}(\sigma ))\), and the result follows by cardinality.
3.2.3 Lehmer codes
Let \(n\in \mathbb {N}\).
(a) For each \(\sigma \in S_{n}\) and \(i\in \left[n\right]\), we set
(b) For each \(m\in \mathbb {Z}\), we let \(\left[m\right]_{0}\) denote the set \(\left\{ 0,1,\ldots ,m\right\} \). (This is an empty set when \(m{\lt}0\).)
(c) We let \(H_{n}\) denote the set
This set \(H_{n}\) has size \(n!\).
(d) Each \(\sigma \in S_n\) and each \(i\in [n]\) satisfy \(\ell _i(\sigma ) \in \{ 0,1,\ldots ,n-i\} = [n-i]_0\).
(e) We define the map
If \(\sigma \in S_{n}\) is a permutation, then the \(n\)-tuple \(L\left(\sigma \right)\) is called the Lehmer code (or just the code) of \(\sigma \).
Let \(n\in \mathbb {N}\) and \(\sigma \in S_{n}\). Then, \(\ell \left(\sigma \right)=\ell _{1}\left(\sigma \right)+\ell _{2}\left( \sigma \right)+\cdots +\ell _{n}\left(\sigma \right)\).
The inversions of \(\sigma \) are pairs \((i,j)\) with \(i{\lt}j\) and \(\sigma (i){\gt}\sigma (j)\). The Lehmer entry \(\ell _i(\sigma )\) counts the \(j{\gt}i\) with \(\sigma (i){\gt}\sigma (j)\). Hence the sum \(\sum _i \ell _i(\sigma )\) partitions the set of inversions by first coordinate, so equals \(\ell (\sigma )\).
Let \(n\in \mathbb {N}\). Then, the map \(L:S_{n}\rightarrow H_{n}\) is a bijection.
Let \(\sigma \in S_{n}\), \(i\in [n]\). We can rewrite
Therefore,
This allows recovering \(\sigma (i)\) from \(\ell _i(\sigma )\) and \(\sigma (1),\ldots ,\sigma (i-1)\) by induction on \(i\), proving that \(L\) is injective.
Conversely, given any \(\mathbf{j} = (j_1,\ldots ,j_n) \in H_n\), define \(M(\mathbf{j})\) to be the map \(\sigma :[n]\to [n]\) whose values are determined by
This is well-defined (since \(j_i \le n - i\)) and gives a permutation (since \(\sigma (i) \notin \{ \sigma (1),\ldots ,\sigma (i-1)\} \)). The maps \(L\) and \(M\) are mutually inverse, so \(L\) is bijective.
For \(\sigma \in S_n\) and \(i \in [n]\),
The elements \(j {\gt} i\) with \(\sigma (j) {\lt} \sigma (i)\) are in bijection (via \(j \mapsto \sigma (j)\)) with elements \(v {\lt} \sigma (i)\) not in the image \(\sigma (\{ 1,\ldots ,i-1\} )\), since these are precisely \([n] \setminus \{ \sigma (1),\ldots ,\sigma (i)\} \) intersected with \(\{ v : v {\lt} \sigma (i)\} \).
For \(\sigma \in S_n\) and \(i \in [n]\),
This strict bound (compared to the weak bound \(\ell _i(\sigma ) \leq n - i\) from Definition 3.45 part (d)) holds because \(\ell _i(\sigma )\) counts elements among \(\{ i+1, \ldots , n-1\} \) where \(\sigma (j) {\lt} \sigma (i)\), and there are only \(n - 1 - i\) such elements.
The set \(\{ j : j {\gt} i \text{ and } \sigma (j) {\lt} \sigma (i)\} \) is a subset of \(\{ j : j {\gt} i\} \), which has cardinality \(n - 1 - i {\lt} n - i\).
3.2.4 Lexicographic order
Let \(\left(a_{1},a_{2},\ldots ,a_{n}\right)\) and \(\left(b_{1},b_{2},\ldots ,b_{n}\right)\) be two \(n\)-tuples of integers. We say that
if and only if
there exists some \(k\in \left[n\right]\) such that \(a_{k}\neq b_{k}\), and
the smallest such \(k\) satisfies \(a_{k}{\lt}b_{k}\).
The relation \({\lt}_{\operatorname {lex}}\) is a strict total order on \(\mathbb {Z}^n\) (the lexicographic order).
If \(\mathbf{a}\) and \(\mathbf{b}\) are two distinct \(n\)-tuples of integers, then we have either \(\mathbf{a} {\lt}_{\operatorname {lex}}\mathbf{b}\) or \(\mathbf{b}{\lt}_{\operatorname {lex}} \mathbf{a}\).
Since \(\mathbf{a} \neq \mathbf{b}\), there exists some index where they differ. By well-founded induction on \(\operatorname {Fin} n\), let \(k\) be the minimal such index. Then \(a_i = b_i\) for all \(i {\lt} k\), and \(a_k \neq b_k\). If \(a_k {\lt} b_k\) then \(\mathbf{a} {\lt}_{\operatorname {lex}} \mathbf{b}\); otherwise \(b_k {\lt} a_k\) and \(\mathbf{b} {\lt}_{\operatorname {lex}} \mathbf{a}\).
Let \(\sigma \in S_{n}\) and \(\tau \in S_{n}\) be such that
Then,
(In other words, \(L\left(\sigma \right) {\lt}_{\operatorname {lex}} L\left( \tau \right)\).)
The assumption says there exists a smallest \(k \in [n]\) with \(\sigma (k) \neq \tau (k)\) and \(\sigma (k) {\lt} \tau (k)\). For \(i {\lt} k\): \(\sigma (i) = \tau (i)\), so the images \(\sigma (\{ 1,\ldots ,i-1\} )\) and \(\tau (\{ 1,\ldots ,i-1\} )\) agree. Since \(\sigma (i) = \tau (i)\) as well, we get \(\ell _i(\sigma ) = \ell _i(\tau )\).
Let \(Z = [n] \setminus \{ \sigma (1),\ldots ,\sigma (k-1)\} = [n] \setminus \{ \tau (1),\ldots ,\tau (k-1)\} \). Then \(\ell _k(\sigma )\) counts elements of \(Z\) smaller than \(\sigma (k)\), and \(\ell _k(\tau )\) counts elements of \(Z\) smaller than \(\tau (k)\). Since \(\sigma (k) {\lt} \tau (k)\) and \(\sigma (k) \in Z\), every element of \(Z\) smaller than \(\sigma (k)\) is also smaller than \(\tau (k)\), and \(\sigma (k)\) itself is smaller than \(\tau (k)\) but not smaller than \(\sigma (k)\). Thus \(\ell _k(\sigma ) {\lt} \ell _k(\tau )\).
Combining, we get \(L(\sigma ) {\lt}_{\operatorname {lex}} L(\tau )\).
We first show that \(L\) is injective. Let \(\sigma \) and \(\tau \) be two distinct permutations in \(S_n\). Their OLNs are distinct, so by Proposition 3.51 we have either OLN\((\sigma ) {\lt}_{\operatorname {lex}}\) OLN\((\tau )\) or vice versa. In either case, Proposition 3.52 gives \(L(\sigma ) {\lt}_{\operatorname {lex}} L(\tau )\) or \(L(\tau ) {\lt}_{\operatorname {lex}} L(\sigma )\). In particular \(L(\sigma ) \neq L(\tau )\), so \(L\) is injective. Since \(|S_n| = n! = |H_n|\) and \(L\) is injective between finite sets of equal size, the Pigeonhole Principle shows \(L\) is bijective.
3.2.5 Generating function for lengths
Let \(n\in \mathbb {N}\). Then,
Each \(\sigma \in S_{n}\) satisfies
by Proposition 3.46, whence \(x^{\ell (\sigma )} = \prod _{i=1}^{n} x^{\ell _i(\sigma )}\). Therefore
where we substituted \((j_1,\ldots ,j_n)\) for \(L(\sigma )\), which is valid since \(L: S_n \to H_n\) is a bijection (Theorem 3.47). Since \(H_{n} = [n-1]_0 \times [n-2]_0 \times \cdots \times [0]_0\), the product rule gives