Formalization Blueprint for “An Introduction to Algebraic Combinatorics” by Darij Grinberg

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

Definition 3.35
#

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)\).

Lemma 3.36

The identity permutation \(\operatorname {id} \in S_n\) has no inversions: \(\operatorname {inv}(\operatorname {id}) = \emptyset \) and \(\ell (\operatorname {id}) = 0\).

Proof

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)\).

Proposition 3.37

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

\[ \left(\text{\# of }\sigma \in S_{n}\text{ with }\ell \left(\sigma \right) =0\right) = 1. \]

Indeed, the only permutation \(\sigma \in S_{n}\) with \(\ell \left( \sigma \right)=0\) is the identity map \(\operatorname {id}\).

(c) We have

\[ \left(\text{\# of }\sigma \in S_{n}\text{ with }\ell \left(\sigma \right) =\binom {n}{2}\right) = 1. \]

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

\[ \left(\text{\# of }\sigma \in S_{n}\text{ with }\ell \left(\sigma \right) =1\right) = n-1. \]

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

\[ \left(\text{\# of }\sigma \in S_{n}\text{ with }\ell \left(\sigma \right) =2\right) = \frac{\left(n-2\right)\left(n+1\right)}{2}. \]

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

\[ \left(\text{\# of }\sigma \in S_{n}\text{ with }\ell \left(\sigma \right) =k\right) = \left(\text{\# of }\sigma \in S_{n}\text{ with }\ell \left( \sigma \right)=\binom {n}{2}-k\right). \]
theorem AlgebraicCombinatorics.Perm.card_invCount_symm {n : } (k : ) :
{σ : Equiv.Perm (Fin n) | (invCount σ) = k}.card = {σ : Equiv.Perm (Fin n) | (invCount σ) = (n.choose 2) - k}.card
Proof

(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

Lemma 3.38

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\).

Proof

Direct computation.

Lemma 3.39

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]\).

Proof

Both send \(i\) to \(n - 1 - i\); the equality follows by extensionality.

Lemma 3.40

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\).

Proof

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)\).

Definition 3.41
#

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.

Lemma 3.42

For any \(\sigma \in S_n\), we have \(\ell (\sigma ^{-1}) = \ell (\sigma )\).

Proof

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 \).

Lemma 3.43

For any \(\sigma \in S_n\), the inversions of \(w_0 \sigma \) are exactly the non-inversions of \(\sigma \), and

\[ \ell (w_0 \sigma ) = \binom {n}{2} - \ell (\sigma ). \]
Proof

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.

Lemma 3.44

For any \(\sigma , \tau \in S_n\),

\[ \ell (\sigma \tau ) \leq \ell (\sigma ) + \ell (\tau ). \]

This is the triangle inequality for the inversion count.

Proof

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

Definition 3.45
#

Let \(n\in \mathbb {N}\).

(a) For each \(\sigma \in S_{n}\) and \(i\in \left[n\right]\), we set

\begin{align*} \ell _{i}\left(\sigma \right) := & \left(\text{\# of all }j\in \left[n\right] \text{ satisfying }i{\lt}j\text{ and }\sigma \left(i\right) {\gt}\sigma \left(j\right)\right) \\ = & \left(\text{\# of all }j\in \left\{ i+1,i+2,\ldots ,n\right\} \text{ such that }\sigma \left(i\right){\gt}\sigma \left(j\right)\right). \end{align*}

(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

\begin{align*} & \left[n-1\right]_{0}\times \left[n-2\right]_{0}\times \cdots \times \left[n-n\right]_{0}\\ & = \left\{ \left(j_{1},j_{2},\ldots ,j_{n}\right)\in \mathbb {N}^{n} \ \mid \ j_{i}\leq n-i\text{ for each }i\in \left[n\right]\right\} . \end{align*}

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

\begin{align*} L:S_{n} & \rightarrow H_{n},\\ \sigma & \mapsto \left(\ell _{1}\left(\sigma \right),\ell _{2}\left( \sigma \right),\ldots ,\ell _{n}\left(\sigma \right)\right). \end{align*}

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 \).

Proposition 3.46

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)\).

Proof

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 )\).

Theorem 3.47

Let \(n\in \mathbb {N}\). Then, the map \(L:S_{n}\rightarrow H_{n}\) is a bijection.

First proof of Theorem 3.47 (sketched).

Let \(\sigma \in S_{n}\), \(i\in [n]\). We can rewrite

\begin{align} \ell _{i}\left(\sigma \right) & = \left(\text{\# of elements of } \left[n\right]\setminus \left\{ \sigma \left(1\right),\ldots ,\sigma \left(i-1\right)\right\} \text{ that are smaller than }\sigma \left(i\right)\right). \label{eq.thm.perm.lehmer.bij.1st.1} \end{align}

Therefore,

\begin{align} \sigma \left(i\right) & = \left(\text{the }\left(\ell _{i}\left(\sigma \right)+1\right)\text{-st smallest element of } \left[n\right]\setminus \left\{ \sigma \left(1\right),\ldots ,\sigma \left(i-1\right)\right\} \right). \label{eq.thm.perm.lehmer.bij.1st.2} \end{align}

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

\[ \sigma (i) = (\text{the }(j_i+1)\text{-st smallest element of } [n]\setminus \{ \sigma (1),\ldots ,\sigma (i-1)\} ). \]

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.

Lemma 3.48

For \(\sigma \in S_n\) and \(i \in [n]\),

\[ \ell _i(\sigma ) = \left|\left\{ v {\lt} \sigma (i) : v \notin \sigma (\{ 1,\ldots ,i-1\} ) \right\} \right|. \]
theorem AlgebraicCombinatorics.Perm.lehmerEntry_eq_card_filter {n : } (σ : Equiv.Perm (Fin n)) (i : Fin n) :
lehmerEntry σ i = {v : Fin n | v < σ i vFinset.image σ {x : Fin n | x < i}}.card
Proof

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)\} \).

Lemma 3.49

For \(\sigma \in S_n\) and \(i \in [n]\),

\[ \ell _i(\sigma ) {\lt} n - i. \]

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.

theorem AlgebraicCombinatorics.Perm.lehmerEntry_lt {n : } (σ : Equiv.Perm (Fin n)) (i : Fin n) :
lehmerEntry σ i < n - i
Proof

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

Definition 3.50
#

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

\[ \left(a_{1},a_{2},\ldots ,a_{n}\right) {\lt}_{\operatorname {lex}}\left( b_{1},b_{2},\ldots ,b_{n}\right) \]

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).

theorem AlgebraicCombinatorics.lexLt_trans {n : } {a b c : Fin n} (hab : lexLt a b) (hbc : lexLt b c) :
lexLt a c
theorem AlgebraicCombinatorics.lexLt_asymm {n : } {a b : Fin n} (hab : lexLt a b) :
theorem AlgebraicCombinatorics.lexLt_strictTotalOrder {n : } (a b : Fin n) :
a = b ¬lexLt a b ¬lexLt b a a b lexLt a b ¬lexLt b a a b ¬lexLt a b lexLt b a
Proposition 3.51

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}\).

theorem AlgebraicCombinatorics.lexLt_trichotomous {n : } (a b : Fin n) (hab : a b) :
lexLt a b lexLt b a
Proof

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}\).

Proposition 3.52

Let \(\sigma \in S_{n}\) and \(\tau \in S_{n}\) be such that

\[ \left(\sigma \left(1\right),\sigma \left(2\right),\ldots ,\sigma \left( n\right)\right) {\lt}_{\operatorname {lex}}\left(\tau \left(1\right) ,\tau \left(2\right),\ldots ,\tau \left(n\right)\right). \]

Then,

\[ \left(\ell _{1}\left(\sigma \right),\ell _{2}\left(\sigma \right) ,\ldots ,\ell _{n}\left(\sigma \right)\right) {\lt}_{\operatorname {lex}}\left( \ell _{1}\left(\tau \right),\ell _{2}\left(\tau \right),\ldots ,\ell _{n}\left(\tau \right)\right). \]

(In other words, \(L\left(\sigma \right) {\lt}_{\operatorname {lex}} L\left( \tau \right)\).)

theorem AlgebraicCombinatorics.Perm.lehmerCode_preserves_lexLt {n : } (σ τ : Equiv.Perm (Fin n)) (h : lexLt (fun (i : Fin n) => (σ i)) fun (i : Fin n) => (τ i)) :
lexLt (fun (i : Fin n) => (lehmerEntry σ i)) fun (i : Fin n) => (lehmerEntry τ i)
Proof of Proposition 3.52 (sketched).

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 )\).

Second proof of Theorem 3.47 (sketched).

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

Proposition 3.53

Let \(n\in \mathbb {N}\). Then,

\begin{align*} \sum _{\sigma \in S_{n}} x^{\ell \left(\sigma \right)} & = \prod _{i=1}^{n-1}\left(1+x+x^{2}+\cdots +x^{i}\right) \\ & = \left(1+x\right)\left(1+x+x^{2}\right)\left(1+x+x^{2}+x^{3}\right) \cdots \left(1+x+x^{2}+\cdots +x^{n-1}\right) \\ & = \left[n\right]_{x}!. \end{align*}
theorem AlgebraicCombinatorics.length_generating_function (n x : ) :
σ : Equiv.Perm (Fin n), x ^ Perm.invCount σ = iFinset.range n, jFinset.range (i + 1), x ^ j
Proof of Proposition 3.53.

Each \(\sigma \in S_{n}\) satisfies

\[ \ell \left(\sigma \right) = \ell _{1}\left(\sigma \right)+\ell _{2}\left( \sigma \right)+\cdots +\ell _{n}\left(\sigma \right) \]

by Proposition 3.46, whence \(x^{\ell (\sigma )} = \prod _{i=1}^{n} x^{\ell _i(\sigma )}\). Therefore

\begin{align*} \sum _{\sigma \in S_{n}} x^{\ell \left(\sigma \right)} & = \sum _{\sigma \in S_{n}} \prod _{i=1}^{n} x^{\ell _i(\sigma )} = \sum _{\left(j_{1},\ldots ,j_{n}\right)\in H_{n}} x^{j_{1}} x^{j_{2}} \cdots x^{j_{n}} \end{align*}

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

\begin{align*} \sum _{(j_1,\ldots ,j_n) \in H_n} x^{j_1}\cdots x^{j_n} & = \left(\sum _{j_1=0}^{n-1} x^{j_1}\right) \left(\sum _{j_2=0}^{n-2} x^{j_2}\right) \cdots \left(\sum _{j_n=0}^{0} x^{j_n}\right) \\ & = \prod _{i=1}^{n-1} \left(1+x+x^2+\cdots +x^i\right) = [n]_x!. \end{align*}