Inversion (discrete mathematics) explained
In computer science and discrete mathematics, an inversion in a sequence is a pair of elements that are out of their natural order.
Definitions
Inversion
Let
be a
permutation. There is an
inversion of
between
and
if
and
. The inversion is indicated by an ordered pair containing either the places
or the elements
.
The inversion set is the set of all inversions. A permutation's inversion set using place-based notation is the same as the inverse permutation's inversion set using element-based notation with the two components of each ordered pair exchanged. Likewise, a permutation's inversion set using element-based notation is the same as the inverse permutation's inversion set using place-based notation with the two components of each ordered pair exchanged.
Inversions are usually defined for permutations, but may also be defined for sequences:
Let
be a
sequence (or
multiset permutation). If
and
, either the pair of places
or the pair of elements
is called an inversion of
.
For sequences, inversions according to the element-based definition are not unique, because different pairs of places may have the same pair of values.
Inversion number
The inversion number
of a sequence
X=\langlex1,...,xn\rangle
, is the
cardinality of the inversion set. It is a common measure of sortedness (sometimes called presortedness) of a permutation or sequence. The inversion number is between 0 and
inclusive. A permutation and its inverse have the same inversion number.
For example
inv(\langle1,2,...,n\rangle)=0
since the sequence is ordered. Also, when
is even,
inv(\langlem+1,m+2,...,2m,1,2,...,m\rangle)=m2
(because each pair
is an inversion). This last example shows that a set that is intuitively "nearly sorted" can still have a quadratic number of inversions.
The inversion number is the number of crossings in the arrow diagram of the permutation, the permutation's Kendall tau distance from the identity permutation, and the sum of each of the inversion related vectors defined below.
Other measures of 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. Standard comparison sorting algorithms can be adapted to compute the inversion number in time .
Inversion related vectors
Three similar vectors are in use that condense the inversions of a permutation into a vector that uniquely determines it. They are often called inversion vector or Lehmer code. (A list of sources is found here.)
This article uses the term inversion vector (
) like
Wolfram.
[1] The remaining two vectors are sometimes called
left and
right inversion vector, but to avoid confusion with the inversion vector this article calls them
left inversion count (
) and
right inversion count (
). Interpreted as a
factorial number the left inversion count gives the permutations reverse colexicographic,
[2] and the right inversion count gives the lexicographic index.
Inversion vector
:
With the element-based definition
is the number of inversions whose smaller (right) component is
.
is the number of elements in
greater than
before
.
v(i)~~=~~\#\{k\midk>i~\land~\pi-1(k)<\pi-1(i)\}
Left inversion count
:
With the place-based definition
is the number of inversions whose bigger (right) component is
.
is the number of elements in
greater than
before
.
l(i)~~=~~\#\left\{k\midk<i~\land~\pi(k)>\pi(i)\right\}
Right inversion count
, often called
Lehmer code:
With the place-based definition
is the number of inversions whose smaller (left) component is
.
is the number of elements in
smaller than
after
.
r(i)~~=~~\#\{k\midk>i~\land~\pi(k)<\pi(i)\}
Both
and
can be found with the help of a Rothe diagram, which is a
permutation matrix with the 1s represented by dots, and an inversion (often represented by a cross) in every position that has a dot to the right and below it.
is the sum of inversions in row
of the Rothe diagram, while
is the sum of inversions in column
. The permutation matrix of the inverse is the transpose, therefore
of a permutation is
of its inverse, and vice versa.
Example: All permutations of four elements
The following sortable table shows the 24 permutations of four elements (in the
column) with their place-based inversion sets (in the p-b column), inversion related vectors (in the
,
, and
columns), and inversion numbers (in the # column). (The columns with smaller print and no heading are reflections of the columns next to them, and can be used to sort them in colexicographic order.)
It can be seen that
and
always have the same digits, and that
and
are both related to the place-based inversion set. The nontrivial elements of
are the sums of the descending diagonals of the shown triangle, and those of
are the sums of the ascending diagonals. (Pairs in descending diagonals have the right components 2, 3, 4 in common, while pairs in ascending diagonals have the left components 1, 2, 3 in common.)
The default order of the table is reverse colex order by
, which is the same as colex order by
. Lex order by
is the same as lex order by
.
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.
The Hasse diagram of the inversion sets ordered by the subset relation forms the skeleton of a permutohedron.
If a permutation is assigned to each inversion set using the place-based definition, the resulting order of permutations is that of the permutohedron, where an edge corresponds to the swapping of two elements with consecutive values. This is the weak order of permutations. The identity is its minimum, and the permutation formed by reversing the identity is its maximum.
If a permutation were assigned to each inversion set using the element-based definition, the resulting order of permutations would be that of a Cayley graph, where an edge corresponds to the swapping of two elements on consecutive places. This Cayley graph of the symmetric group is similar to its permutohedron, but with each permutation replaced by its inverse.
See also
Sequences in the OEIS:
- Sequences related to factorial base representation
- Factorial numbers: and
- Inversion numbers:
- Inversion sets of finite permutations interpreted as binary numbers: (related permutation:)
- Finite permutations that have only 0s and 1s in their inversion vectors: (their inversion sets:)
- Number of permutations of n elements with k inversions; Mahonian numbers: (their row maxima; Kendall-Mann numbers:)
- Number of connected labeled graphs with n edges and n nodes:
References
Source bibliography
- Book: Aigner, Martin
. A course in enumeration . Word Representation . Springer . Berlin, New York . 2007 . 978-3642072536.
- Wilhelm . Barth . Petra . Mutzel . Petra Mutzel . Simple and Efficient Bilayer Cross Counting . . 8 . 2 . 179 - 194 . 2004 . 10.7155/jgaa.00088. free .
- Book: Bóna, Miklós . Miklós Bóna . Combinatorics of permutations . 2.2 Inversions in Permutations of Multisets . CRC Press . Boca Raton, FL . 2012 . 978-1439850510 .
- Book: Comtet, Louis
. Advanced combinatorics; the art of finite and infinite expansions . 6.4 Inversions of a permutation of [n] . D. Reidel Pub. Co . Dordrecht, Boston . 1974 . 9027704414 .
- Book: Thomas H. . Cormen . Thomas H. Cormen . Leiserson . Charles E. . Charles E. Leiserson . Rivest . Ronald L. . Ron Rivest . Stein . Clifford . Clifford Stein . . MIT Press and McGraw-Hill . 2001 . 0-262-53196-8 . 2nd .
- Book: Gratzer, George . George Grätzer
. George Grätzer . Lattice theory. special topics and applications . 7-2 Basic objects . Birkhäuser . Cham, Switzerland . 2016 . 978-3319442358 .
- Book: Kleinberg. Jon . Tardos. Éva . Algorithm Design . 2005 . 0-321-29535-8 .
- Book: Knuth . Donald . . 5.1.1 Inversions . Addison-Wesley Pub. Co . 1973 . 0201896850.
- Book: Mahmoud
, Hosam Mahmoud . Sorting: a distribution theory . Sorting Nonrandom Data . 54 . Wiley-Interscience series in discrete mathematics and optimization . Wiley-IEEE . 2000 . 978-0-471-32710-3.
- Book: Computational discrete mathematics: combinatorics and graph theory with Mathematica . Permutations and combinations . Sriram V. . Pemmaraju . Steven S.. Skiena . Cambridge University Press . 2003 . 978-0-521-80686-2.
- Book: Algorithms and Complexity . 1 . J.S. . Vitter . Ph. . Flajolet . Jan . van Leeuwen. Jan van Leeuwen . 2nd . Elsevier . 1990 . 978-0-444-88071-0 . Average-Case Analysis of Algorithms and Data Structures.
Further reading
- Journal of Integer Sequences. 4. 2001. Permutations with Inversions. Barbara H.. Margolius.
Presortedness measures
- Mannila . Heikki . Heikki Mannila . April 1985 . 10.1109/tc.1985.5009382 . 4 . IEEE Transactions on Computers . 318–325 . Measures of presortedness and optimal sorting algorithms . C-34.
- Vladimir. Estivill-Castro. Derick. Wood. Derick Wood. A new measure of presortedness. Information and Computation. 83. 1. 111 - 119. 1989. 10.1016/0890-5401(89)90050-3. free.
- Steven S.. Skiena. 1988. Encroaching lists as a measure of presortedness. BIT. 28. 4. 755 - 784. 10.1007/bf01954897. 33967672.
Notes and References
- Weisstein, Eric W. "Inversion Vector" From MathWorld--A Wolfram Web Resource
- Reverse colex order of finitary permutations