# Inversion (discrete mathematics)

The permutation (4,1,5,2,6,3) has the inversion vector (0,1,0,2,0,3) and the inversion set {(1,2),(1,4),(3,4),(1,6),(3,6),(5,6)}. The inversion vector converted to decimal is 373.
Inversion set of the permutation
(0,15, 14,1, 13,2, 3,12,
11,4, 5,10, 6,9, 8,7)
showing the pattern of the
Thue–Morse sequence

In computer science and discrete mathematics, an inversion is a pair of places of a sequence where the elements on these places are out of their natural order.

## Definitions

Formally, let ${\displaystyle (A(1),\ldots ,A(n))}$ be a sequence of n distinct numbers. If ${\displaystyle i and ${\displaystyle A(i)>A(j)}$, then the pair ${\displaystyle (i,j)}$ is called an inversion of ${\displaystyle A}$.[1][2]

The inversion number of a sequence is one common measure of its sortedness.[3][2] Formally, the inversion number is defined to be the number of inversions, that is,

${\displaystyle {\text{inv}}(A)=|\{(A(i),A(j))\mid iA(j)\}|}$.[3]

Equivalently, it is the Kendall tau distance from the identity permutation. Other measures of (pre-)sortedness include the minimum number of elements that can be deleted from the sequence to yield a fully sorted sequence, the number and lengths of sorted "runs" within the sequence, the Spearman footrule (sum of distances of each element from its sorted position), and the smallest number of exchanges needed to sort the sequence.[4] Standard comparison sorting algorithms can be adapted to compute the inversion number in time O(n log n).

The inversion vector V(i) of the sequence is defined for i = 2, ..., n as ${\displaystyle V[i]=\left\vert \{k\mid kA(i)\}\right\vert }$. In other words, each element is the number of elements preceding the element in the original sequence that are greater than it. Note that the inversion vector of a sequence has one less element than the sequence, because of course the number of preceding elements that are greater than the first is always zero. Each permutation of a sequence has a unique inversion vector and it is possible to construct any given permutation of a (fully sorted) sequence from that sequence and the permutation's inversion vector.[5]

## Weak order of permutations

The set of permutations on n items can be given the structure of a partial order, called the weak order of permutations, which forms a lattice.

To define this order, consider the items being permuted to be the integers from 1 to n, and let Inv(u) denote the set of inversions of a permutation u for the natural ordering on these items. That is, Inv(u) is the set of ordered pairs (i, j) such that 1 ≤ i < jn and u(i) > u(j). Then, in the weak order, we define uv whenever Inv(u) ⊆ Inv(v).

The edges of the Hasse diagram of the weak order are given by permutations u and v such that u < v and such that v is obtained from u by interchanging two consecutive values of u. These edges form a Cayley graph for the group of permutations that is isomorphic to the skeleton of a permutohedron.

The identity permutation is the minimum element of the weak order, and the permutation formed by reversing the identity is the maximum element.

Sequences in the OEIS:

## References

1. ^ Cormen et al. 2001, pp. 39.
2. ^ a b Vitter & Flajolet 1990, pp. 459.
3. ^ a b Barth & Mutzel 2004, pp. 183.
4. ^ Mahmoud 2000, pp. 284.
5. ^ Pemmaraju & Skiena 2003, pp. 69.