Skip to content

Inversions ​

The number of inversions of a permutation pp: pairs i<ji < j with pi>pjp_i > p_j (its Kendall-tau distance from the identity).

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaInversion (discrete mathematics)MathWorldPermutationInversionWikidataQ1368009FindStatSt000293via BinaryWord✓St000766via IntegerComposition✓St000018via Permutation✓
Inversions(p)the inversion count of a one-line permutation pp
CombinatoricaInversions[p]SageMathenumeratio_stat("Permutation", p, "Inversions")
Typed signatures
TypePackageApplies
(binary_word | composition | list | permutation) -> integer | numbercombinatoricsalways

Domain: Permutation statistics

Details
  • Ranges from 00 (the identity) to (n2)\binom{n}{2} (the reversal n,n−1,…,1n, n-1, \dots, 1)
  • Generating function over SnS_n is the q-factorial [n]q!=∏i=1n1−qi1−q[n]_q! = \prod_{i=1}^{n} \frac{1 - q^i}{1 - q}
  • Equidistributed with MajorIndex on SnS_n (MacMahon), so both are Mahonian statistics
  • Aggregates over a whole collection compose from built-ins: Sum(Map(Inversions,SymmetricGroup(n)))\mathrm{Sum}(\mathrm{Map}(\mathrm{Inversions}, \mathrm{SymmetricGroup}(n))) folds the stat over the lazy family
  • Defined over Permutation as an expression in _x, evaluated by compute-engine — the definition IS the implementation.
  • Takes a Permutation, and also a bare list of integers: this reading compares entries with each other rather than with their positions, so it stands on any sequence.

Definition

Epsil, as the implementation runs it.

on Permutation, over _x

0 if Length(_x) < 2 else Sum(Map(
                                  Function(
                                            Count(Filter(
                                                          Range(
                                                                 i + 1,
                                                                 Length(_x),
                                                               ),
                                                          Function(
                                                                    _x[i] >
                                                                    _x[j],
                                                                    j,
                                                                  ),
                                                        )),
                                            i,
                                          ),
                                  Range(1, Length(_x)),
                                ))

on IntegerComposition, over _x

0 if Length(_x) < 2 else Sum(Map(Function(
                                           Count(Filter(Range(
                                                               i + 1,
                                                               Length(_x),
                                                               1,
                                                             ), Function(_x[i] >
                                                                        _x[j], j))),
                                           i,
                                         ), Range(1, Length(_x) - 1)))

on BinaryWord, over _x

0 if Length(_x) < 2 else Sum(Map(Function(
                                           Count(Filter(Range(
                                                               i + 1,
                                                               Length(_x),
                                                               1,
                                                             ), Function(_x[i] >
                                                                        _x[j], j))),
                                           i,
                                         ), Range(1, Length(_x) - 1)))

Examples

Basic #

(3,1)(3,1) and (3,2)(3,2)

live ↗
InInversions([3,1,2])\mathrm{Inversions}(\bigl\lbrack3, 1, 2\bigr\rbrack)Out22

the identity has none

live ↗
InInversions([1,2,3])\mathrm{Inversions}(\bigl\lbrack1, 2, 3\bigr\rbrack)Out00

the one-line word 312312

live ↗
InInversions(Permutation⁡([3,1,2]))\mathrm{Inversions}({\operatorname{Permutation}}([3, 1, 2]))Out22

the one-line word 312312, as a plain list

live ↗
InInversions([3,1,2])\mathrm{Inversions}(\bigl\lbrack3, 1, 2\bigr\rbrack)Out22

the one-line word 24132413

live ↗
InInversions(Permutation⁡([2,4,1,3]))\mathrm{Inversions}({\operatorname{Permutation}}([2, 4, 1, 3]))Out33

the arrangement 314314

live ↗
InCombinatorialStat(Arrangement⁡([3,1,4]),Inversions)\mathrm{CombinatorialStat}({\operatorname{Arrangement}}([3, 1, 4]), \text{Inversions})Out11
Applications #

the inversion distribution over S3S_3, mapped across the lazy SymmetricGroup

live ↗
InListFrom(Map(p↦Inversions(p),SymmetricGroup(3)))\mathrm{ListFrom}(\mathrm{Map}(p↦\mathrm{Inversions}(p), \mathrm{SymmetricGroup}(3)))Out[0,1,1,2,2,3]\bigl\lbrack0, 1, 1, 2, 2, 3\bigr\rbrack

total inversions over all of S4S_4, folded with Sum\mathrm{Sum}: n!2(n2)=72\frac{n!}{2}\binom{n}{2} = 72

live ↗
In∑Map(p↦Inversions(p),SymmetricGroup(4))\sum\mathrm{Map}(p↦\mathrm{Inversions}(p), \mathrm{SymmetricGroup}(4))Out7272
Properties #

the reversal attains the maximum (42)\binom{4}{2}

live ↗
InInversions([4,3,2,1])\mathrm{Inversions}(\bigl\lbrack4, 3, 2, 1\bigr\rbrack)Out66

FindStat's value for [1]

live ↗
InCombinatorialStat(Permutation⁡([1]),Inversions)\mathrm{CombinatorialStat}({\operatorname{Permutation}}([1]), \text{Inversions})Out00

Known value · FindStat St000018

FindStat's value for [1,2]

live ↗
InCombinatorialStat(Permutation⁡([1,2]),Inversions)\mathrm{CombinatorialStat}({\operatorname{Permutation}}([1, 2]), \text{Inversions})Out00

Known value · FindStat St000018

FindStat's value for [2,1]

live ↗
InCombinatorialStat(Permutation⁡([2,1]),Inversions)\mathrm{CombinatorialStat}({\operatorname{Permutation}}([2, 1]), \text{Inversions})Out11

Known value · FindStat St000018

About
Examples12
Checked againstCombinatorica 7 agreeSageMath 9 agreemathlib4 6 agree
CrosswalkWikipediaMathWorldWikidataFindStat ×3CombinatoricaSageMath

Implementation

mappedcombinatoricapackages/oracle/src/mappings.ts
mappedsagepackages/oracle/src/mappings.ts
mappedmathlib4packages/oracle/src/mappings.ts

Pairs i < j with l[i] > l[j], counted over Fin pairs of the list; a permutation word and a plain list agree, a composition (which has its own inversions) does not

See also: MajorIndex, Descents, SymmetricGroup