In mathematics, a permutation of a set is, loosely speaking, an arrangement of its members into a sequence or linear order, or if the set is already ordered, a rearrangement of its elements. The word "permutation" also refers to the act or process of changing the linear order of an ordered set.
Permutations differ from combinations, which are selections of some members of a set regardless of order. For example, written as tuples, there are six permutations of the set {1, 2, 3}, namely (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), and (3, 2, 1). These are all the possible orderings of this three-element set. Anagrams of words whose letters are different are also permutations: the letters are already ordered in the original word, and the anagram is a reordering of the letters. The study of permutations of finite sets is an important topic in the fields of combinatorics and group theory.
Permutations are used in almost every branch of mathematics and in many other fields of science. In computer science, they are used for analyzing sorting algorithms; in quantum physics, for describing states of particles; and in biology, for describing RNA sequences.
The number of permutations of n distinct objects is n factorial, usually written as n!, which means the product of all positive integers less than or equal to n.
Technically, a permutation of a set S is defined as a bijection from S to itself. That is, it is a function from S to S for which every element occurs exactly once as an image value. This is related to the rearrangement of the elements of S in which each element s is replaced by the corresponding f(s). For example, the permutation (3, 1, 2) mentioned above is described by the function defined as
- .
The collection of all permutations of a set form a group called the symmetric group of the set. The group operation is the composition (performing two given rearrangements in succession), which results in another rearrangement. As properties of permutations do not depend on the nature of the set elements, it is often the permutations of the set that are considered for studying permutations.
In elementary combinatorics, the k-permutations, or partial permutations, are the ordered arrangements of k distinct elements selected from a set. When k is equal to the size of the set, these are the permutations of the set.
History
Permutations called hexagrams were used in China in the I Ching (Pinyin: Yi Jing) as early as 1000 BC.
In Greece, Plutarch wrote that Xenocrates of Chalcedon (396–314 BC) discovered the number of different syllables possible in the Greek language. This would have been the first attempt on record to solve a difficult problem in permutations and combinations.
Al-Khalil (717–786), an Arab mathematician and cryptographer, wrote the Book of Cryptographic Messages. It contains the first use of permutations and combinations, to list all possible Arabic words with and without vowels.
The rule to determine the number of permutations of n objects was known in Indian culture around 1150 AD. The Lilavati by the Indian mathematician Bhaskara II contains a passage that translates as follows:
The product of multiplication of the arithmetical series beginning and increasing by unity and continued to the number of places, will be the variations of number with specific figures.
In 1677, Fabian Stedman described factorials when explaining the number of permutations of bells in change ringing. Starting from two bells: "first, two must be admitted to be varied in two ways", which he illustrates by showing 1 2 and 2 1. He then explains that with three bells there are "three times two figures to be produced out of three" which again is illustrated. His explanation involves "cast away 3, and 1.2 will remain; cast away 2, and 1.3 will remain; cast away 1, and 2.3 will remain". He then moves on to four bells and repeats the casting away argument showing that there will be four different sets of three. Effectively, this is a recursive process. He continues with five bells using the "casting away" method and tabulates the resulting 120 combinations. At this point he gives up and remarks:
Now the nature of these methods is such, that the changes on one number comprehends the changes on all lesser numbers, ... insomuch that a compleat Peal of changes on one number seemeth to be formed by uniting of the compleat Peals on all lesser numbers into one entire body;
Stedman widens the consideration of permutations; he goes on to consider the number of permutations of the letters of the alphabet and of horses from a stable of 20.
A first case in which seemingly unrelated mathematical questions were studied with the help of permutations occurred around 1770, when Joseph Louis Lagrange, in the study of polynomial equations, observed that properties of the permutations of the roots of an equation are related to the possibilities to solve it. This line of work ultimately resulted, through the work of Évariste Galois, in Galois theory, which gives a complete description of what is possible and impossible with respect to solving polynomial equations (in one unknown) by radicals. In modern mathematics, there are many similar situations in which understanding a problem requires studying certain permutations related to it.
Permutations played an important role in the cryptanalysis of the Enigma machine, a cipher device used by Nazi Germany during World War II. In particular, one important property of permutations, namely, that two permutations are conjugate exactly when they have the same cycle type, was used by cryptologist Marian Rejewski to break the German Enigma cipher in turn of years 1932-1933.
Permutations without repetitions
The simplest example of permutations is permutations without repetitions where we consider the number of possible ways of arranging n items into n places. The factorial has special application in defining the number of permutations in a set which does not include repetitions. The number n!, read "n factorial", is precisely the number of ways we can rearrange n things into a new order. For example, if we have three fruits: an orange, apple and pear, we can eat them in the order mentioned, or we can change them (for example, an apple, a pear then an orange). The exact number of permutations is then . The number gets extremely large as the number of items (n) goes up.
In a similar manner, the number of arrangements of k items from n objects is sometimes called a partial permutation or a k-permutation. It can be written as (which reads "n permute k"), and is equal to the number (also written as ).
Definition
In mathematics texts it is customary to denote permutations using lowercase Greek letters. Commonly, either and or and are used.
Permutations can be defined as bijections from a set S onto itself. All permutations of a set with n elements form a symmetric group, denoted , where the group operation is function composition. Thus for two permutations, and in the group , the four group axioms hold:
- Closure: If and are in then so is
- Associativity: For any three permutations ,
- Identity: There is an identity permutation, denoted and defined by for all . For any ,
- Invertibility: For every permutation , there exists an inverse permutation , so that
In general, composition of two permutations is not commutative, that is,
As a bijection from a set to itself, a permutation is a function that performs a rearrangement of a set, and is not an arrangement itself. An older and more elementary viewpoint is that permutations are the arrangements themselves. To distinguish between these two, the identifiers active and passive are sometimes prefixed to the term permutation, whereas in older terminology substitutions and permutations are used.
A permutation can be decomposed into one or more disjoint cycles, that is, the orbits, which are found by repeatedly tracing the application of the permutation on some elements. For example, the permutation defined by has a 1-cycle, while the permutation defined by and has a 2-cycle (for details on the syntax, see § Cycle notation below). In general, a cycle of length k, that is, consisting of k elements, is called a k-cycle.
An element in a 1-cycle is called a fixed point of the permutation. A permutation with no fixed points is called a derangement. 2-cycles are called transpositions; such permutations merely exchange two elements, leaving the others fixed.
Notations
Since writing permutations elementwise, that is, as piecewise functions, is cumbersome, several notations have been invented to represent them more compactly. Cycle notation is a popular choice for many mathematicians due to its compactness and the fact that it makes a permutation's structure transparent. It is the notation used in this article unless otherwise specified, but other notations are still widely used, especially in application areas.
Two-line notation
In Cauchy's two-line notation, one lists the elements of S in the first row, and for each one its image below it in the second row. For instance, a particular permutation of the set S = {1, 2, 3, 4, 5} can be written as
this means that σ satisfies σ(1) = 2, σ(2) = 5, σ(3) = 4, σ(4) = 3, and σ(5) = 1. The elements of S may appear in any order in the first row. This permutation could also be written as:
or
One-line notation
If there is a "natural" order for the elements of S, say , then one uses this for the first row of the two-line notation:
Under this assumption, one may omit the first row and write the permutation in one-line notation as
- ,
that is, as an ordered arrangement of the elements of S. Care must be taken to distinguish one-line notation from the cycle notation described below. In mathematics literature, a common usage is to omit parentheses for one-line notation, while using them for cycle notation. The one-line notation is also called the word representation of a permutation. The example above would then be 2 5 4 3 1 since the natural order 1 2 3 4 5 would be assumed for the first row. (It is typical to use commas to separate these entries only if some have two or more digits.) This form is more compact, and is common in elementary combinatorics and computer science. It is especially useful in applications where the elements of S or the permutations are to be compared as larger or smaller.
Cycle notation
Cycle notation describes the effect of repeatedly applying the permutation on the elements of the set. It expresses the permutation as a product of cycles; since distinct cycles are disjoint, this is referred to as "decomposition into disjoint cycles".
To write down the permutation in cycle notation, one proceeds as follows:
- Write an opening bracket then select an arbitrary element x of and write it down:
- Then trace the orbit of x; that is, write down its values under successive applications of :
- Repeat until the value returns to x and write down a closing parenthesis rather than x:
- Now continue with an element y of S, not yet written down, and proceed in the same way:
- Repeat until all elements of S are written in cycles.
So the permutation 2 5 4 3 1 (in one-line notation) could be written as (125)(34) in cycle notation.
While permutations in general do not commute, disjoint cycles do; for example,
1-cycles are often omitted from the cycle notation, provided that the context is clear; for any element x in S not appearing in any cycle, one implicitly assumes . The identity permutation, which consists only of 1-cycles, can be denoted by a single 1-cycle (x), by the number 1, or by id.
A convenient feature of cycle notation is that cycle notation of the inverse permutation is given by reversing the order of the elements in the permutation's cycles. For example,
Canonical cycle notation
In some combinatorial contexts it is useful to fix a certain order for the elements in the cycles and of the (disjoint) cycles themselves. Miklós Bóna calls the following ordering choices the canonical cycle notation:
- in each cycle the largest element is listed first
- the cycles are sorted in increasing order of their first element
For example, (312)(54)(8)(976) is a permutation in canonical cycle notation. The canonical cycle notation does not omit one-cycles.
Richard P. Stanley calls the same choice of representation the "standard representation" of a permutation, and Martin Aigner uses the term "standard form" for the same notion. Sergey Kitaev also uses the "standard form" terminology, but reverses both choices; that is, each cycle lists its least element first and the cycles are sorted in decreasing order of their least, that is, first elements.
Composition of permutations
There are two ways to denote the composition of two permutations. is the function that maps any element x of the set to . The rightmost permutation is applied to the argument first, because of the way the function application is written.
Since function composition is associative, so is the composition operation on permutations: . Therefore, products of more than two permutations are usually written without adding parentheses to express grouping; they are also usually written without a dot or other sign to indicate composition.
Some authors prefer the leftmost factor acting first, but to that end permutations must be written to the right of their argument, often as an exponent, where σ acting on x is written xσ; then the product is defined by xσ·π = (xσ)π. However this gives a different rule for multiplying permutations; this article uses the definition where the rightmost permutation is applied first.
Other uses of the term permutation
The concept of a permutation as an ordered arrangement admits several generalizations that are not permutations, but have been called permutations in the literature.
k-permutations of n
A weaker meaning of the term permutation, sometimes used in elementary combinatorics texts, designates those ordered arrangements in which no element occurs more than once, but without the requirement of using all the elements from a given set. These are not permutations except in special cases, but are natural generalizations of the ordered arrangement concept. Indeed, this use often involves considering arrangements of a fixed length k of elements taken from a given set of size n, in other words, these k-permutations of n are the different ordered arrangements of a k-element subset of an n-set (sometimes called variations or arrangements in older literature). These objects are also known as partial permutations or as sequences without repetition, terms that avoid confusion with the other, more common, meaning of "permutation". The number of such -permutations of is denoted variously by such symbols as , , , , or (the stands for "arrangements"), and its value is given by the product
- ,
which is 0 when k > n, and otherwise is equal to
The product is well defined without the assumption that is a non-negative integer, and is of importance outside combinatorics as well; it is known as the Pochhammer symbol or as the -th falling factorial power of .
This usage of the term permutation is closely related to the term combination. A k-element combination of an n-set S is a k element subset of S, the elements of which are not ordered. By taking all the k element subsets of S and ordering each of them in all possible ways, we obtain all the k-permutations of S. The number of k-combinations of an n-set, C(n,k), is therefore related to the number of k-permutations of n by:
These numbers are also known as binomial coefficients and are denoted by .
Permutations with repetition
Ordered arrangements of k elements of a set S, where repetition is allowed, are called k-tuples. They have sometimes been referred to as permutations with repetition, although they are not permutations in general. They are also called words over the alphabet S in some contexts. If the set S has n elements, the number of k-tuples over S is There is no restriction on how often an element can appear in an k-tuple, but if restrictions are placed on how often an element can appear, this formula is no longer valid.
Permutations of multisets
If M is a finite multiset, then a multiset permutation is an ordered arrangement of elements of M in which each element appears a number of times equal exactly to its multiplicity in M. An anagram of a word having some repeated letters is an example of a multiset permutation. If the multiplicities of the elements of M (taken in some order) are , , ..., and their sum (that is, the size of M) is n, then the number of multiset permutations of M is given by the multinomial coefficient,
For example, the number of distinct anagrams of the word MISSISSIPPI is:
- .
A k-permutation of a multiset M is a sequence of length k of elements of M in which each element appears a number of times less than or equal to its multiplicity in M (an element's repetition number).
Circular permutations
Permutations, when considered as arrangements, are sometimes referred to as linearly ordered arrangements. In these arrangements there is a first element, a second element, and so on. If, however, the objects are arranged in a circular manner this distinguished ordering no longer exists, that is, there is no "first element" in the arrangement, any element can be considered as the start of the arrangement. The arrangements of objects in a circular manner are called circular permutations. These can be formally defined as equivalence classes of ordinary permutations of the objects, for the equivalence relation generated by moving the final element of the linear arrangement to its front.
Two circular permutations are equivalent if one can be rotated into the other (that is, cycled without changing the relative positions of the elements). The following four circular permutations on four letters are considered to be the same.
1 4 2 3 4 3 2 1 3 4 1 2 2 3 1 4
The circular arrangements are to be read counter-clockwise, so the following two are not equivalent since no rotation can bring one to the other.
1 1 4 3 3 4 2 2
The number of circular permutations of a set S with n elements is (n – 1)!.
Properties
The number of permutations of n distinct objects is n!.
The number of n-permutations with k disjoint cycles is the signless Stirling number of the first kind, denoted by c(n, k).
Cycle type
The cycles (including the fixed points) of a permutation of a set with n elements partition that set; so the lengths of these cycles form an integer partition of n, which is called the cycle type (or sometimes cycle structure or cycle shape) of . There is a "1" in the cycle type for every fixed point of , a "2" for every transposition, and so on. The cycle type of is
This may also be written in a more compact form as [112231]. More precisely, the general form is , where are the numbers of cycles of respective length. The number of permutations of a given cycle type is
- .
The number of cycle types of a set with n elements equals the value of the partition function .
Conjugating permutations
In general, composing permutations written in cycle notation follows no easily described pattern – the cycles of the composition can be different from those being composed. However the cycle type is preserved in the special case of conjugating a permutation by another permutation , which means forming the product . Here, is the conjugate of by and its cycle notation can be obtained by taking the cycle notation for and applying to all the entries in it. It follows that two permutations are conjugate exactly when they have the same cycle type.
Permutation order
The order of a permutation is the smallest positive integer m so that . It is the least common multiple of its cycles lengths. For example, the order of is .
Parity of a permutation
Every permutation of a finite set can be expressed as the product of transpositions. Although many such expressions for a given permutation may exist, either they all contain an even number of transpositions or they all contain an odd number of transpositions. Thus all permutations can be classified as even or odd depending on this number.
This result can be extended so as to assign a sign, written , to each permutation. if is even and if is odd. Then for two permutations and
It follows that
Matrix representation
A permutation matrix is an n × n matrix that has exactly one entry 1 in each column and in each row, and all other entries are 0. There are several different conventions that one can use to assign a permutation matrix to a permutation of {1, 2, ..., n}. One natural approach is to associate to the permutation σ the matrix whose (i, j) entry is 1 if i = σ(j) and is 0 otherwise. This convention has two attractive properties: first, the product of matrices and of permutations is in the same order, that is, for all permutations σ and π. Second, if represents the standard column vector (the vector with ith entry equal to 1 and all other entries equal to 0), then .
For example, with this convention, the matrix associated to the permutation is and the matrix associated to the permutation is . Then the composition of permutations is , and the corresponding matrix product is
It is also common in the literature to find the inverse convention, where a permutation σ is associated to the matrix whose (i, j) entry is 1 if j = σ(i) and is 0 otherwise. In this convention, permutation matrices multiply in the opposite order from permutations, that is, for all permutations σ and π. In this correspondence, permutation matrices act by permuting indices of standard row vectors : one has .
The Cayley table on the right shows these matrices for permutations of 3 elements.
Permutations of totally ordered sets
In some applications, the elements of the set being permuted will be compared with each other. This requires that the set S has a total order so that any two elements can be compared. The set {1, 2, ..., n} is totally ordered by the usual "≤" relation and so it is the most frequently used set in these applications, but in general, any totally ordered set will do. In these applications, the ordered arrangement view of a permutation is needed to talk about the positions in a permutation.
There are a number of properties that are directly related to the total ordering of S.
Ascents, descents, runs and exceedances
An ascent of a permutation σ of n is any position i < n where the following value is bigger than the current one. That is, if σ = σ1σ2...σn, then i is an ascent if σi < σi+1.
For example, the permutation 3452167 has ascents (at positions) 1, 2, 5, and 6.
Similarly, a descent is a position i < n with σi > σi+1, so every i with either is an ascent or is a descent of σ.
An ascending run of a permutation is a nonempty increasing contiguous subsequence of the permutation that cannot be extended at either end; it corresponds to a maximal sequence of successive ascents (the latter may be empty: between two successive descents there is still an ascending run of length 1). By contrast an increasing subsequence of a permutation is not necessarily contiguous: it is an increasing sequence of elements obtained from the permutation by omitting the values at some positions. For example, the permutation 2453167 has the ascending runs 245, 3, and 167, while it has an increasing subsequence 2367.
If a permutation has k − 1 descents, then it must be the union of k ascending runs.
The number of permutations of n with k ascents is (by definition) the Eulerian number ; this is also the number of permutations of n with k descents. Some authors however define the Eulerian number as the number of permutations with k ascending runs, which corresponds to k − 1 descents.
An exceedance of a permutation σ1σ2...σn is an index j such that σj > j. If the inequality is not strict (that is, σj ≥ j), then j is called a weak exceedance. The number of n-permutations with k exceedances coincides with the number of n-permutations with k descents.
Foata's transition lemma
There is a relationship between the one-line notation and the canonical cycle notation. Consider the permutation in canonical cycle notation; if we simply remove the parentheses, we obtain the permutation in one-line notation. Foata's transition lemma establishes the nature of this correspondence as a bijection on the set of n-permutations (to itself). Richard P. Stanley calls this correspondence the fundamental bijection.
Let be the parentheses-erasing transformation which returns in one-line notation when given in canonical cycle notation. As stated, operates by simply removing all parentheses. The operation of the inverse transformation, , which returns in canonical cycle notation when given in one-line notation, is a bit less intuitive. Given the one-line notation , the first cycle of in canonical cycle notation must start with . As long as the subsequent elements are smaller than , we are in the same cycle of . The second cycle of starts at the smallest index such that . In other words, is larger than everything else to its left, so it is called a left-to-right maximum. Every cycle in the canonical cycle notation starts with a left-to-right maximum.
For example, in the permutation , 5 is the first element larger than the starting element 3, so the first cycle of must be . Then 8 is the next element larger than 5, so the second cycle is . Since 9 is larger than 8, is a cycle by itself. Finally, 9 is larger than all the remaining elements to its right, so the last cycle is . Concatenating these 4 cycles gives in canonical cycle notation.
The following table shows both and for the six permutations of . The bold side of each equality shows the permutation using its designated notation (one-line notation for and canonical cycle notation for ) while the non-bold side shows the same permutation in the other notation. Comparing the bold side of each column of the table shows the parenthesis removing/restoring operation of Foata's bijection, while comparing the same side of each column (for example, the LHS) shows which permutations are mapped to themselves by the bijection (first 3 rows) and which are not (last 3 rows).
As a first corollary, the number of n-permutations with exactly k left-to-right maxima is also equal to the signless Stirling number of the first kind, . Furthermore, Foata's mapping takes an n-permutation with k-weak exceedances to an n-permutations with k − 1 ascents. For example, (2)(31) = 321 has two weak exceedances (at index 1 and 2), whereas f(321) = 231 has one ascent (at index 1; that is, from 2 to 3).
Inversions
An inversion of a permutation σ is a pair (i, j) of positions where the entries of a permutation are in the opposite order: and . So a descent is just an inversion at two adjacent positions. For example, the permutation σ = 23154 has three inversions: (1, 3), (2, 3), and (4, 5), for the pairs of entries (2, 1), (3, 1), and (5, 4).
Sometimes an inversion is defined as the pair of values (σi,σj) whose order is reversed; this makes no difference for the number of inversions, and this pair (reversed) is also an inversion in the above sense for the inverse permutation σ−1. The number of inversions is an important measure for the degree to which the entries of a permutation are out of order; it is the same for σ and for σ−1. To bring a permutation with k inversions into order (that is, transform it into the identity permutation), by successively applying (right-multiplication by) adjacent transpositions, is always possible and requires a sequence of k such operations. Moreover, any reasonable choice for the adjacent transpositions will work: it suffices to choose at each step a transposition of i and i + 1 where i is a descent of the permutation as modified so far (so that the transposition will remove this particular descent, although it might create other descents). This is so because applying such a transposition reduces the number of inversions by 1; as long as this number is not zero, the permutation is not the identity, so it has at least one descent. Bubble sort and insertion sort can be interpreted as particular instances of this procedure to put a sequence into order. Incidentally this procedure proves that any permutation σ can be written as a product of adjacent transpositions; for this one may simply reverse any sequence of such transpositions that transforms σ into the identity. In fact, by enumerating all sequences of adjacent transpositions that would transform σ into the identity, one obtains (after reversal) a complete list of all expressions of minimal length writing σ as a product of adjacent transpositions.
The number of permutations of n with k inversions is expressed by a Mahonian number, it is the coefficient of Xk in the expansion of the product
Let such that and . In this case, say the weight of the inversion is . Kobayashi (2011) proved the enumeration formula
where denotes Bruhat order in the symmetric groups. This graded partial order often appears in the context of Coxeter groups.
Permutations in computing
Numbering permutations
One way to represent permutations of n things is by an integer N with 0 ≤ N < n!, provided convenient methods are given to convert between the number and the representation of a permutation as an ordered arrangement (sequence). This gives the most compact representation of arbitrary permutations, and in computing is particularly attractive when n is small enough that N can be held in a machine word; for 32-bit words this means n ≤ 12, and for 64-bit words this means n ≤ 20. The conversion can be done via the intermediate form of a sequence of numbers dn, dn−1, ..., d2, d1, where di is a non-negative integer less than i (one may omit d1, as it is always 0, but its presence makes the subsequent conversion to a permutation easier to describe). The first step then is to simply express N in the factorial number system, which is just a particular mixed radix representation, where, for numbers less than n!, the bases (place values or multiplication factors) for successive digits are (n − 1)!, (n − 2)!, ..., 2!, 1!. The second step interprets this sequence as a Lehmer code or (almost equivalently) as an inversion table.
σi i
|
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Lehmer code |
---|---|---|---|---|---|---|---|---|---|---|
1 | × | × | × | × | × | • | d9 = 5 | |||
2 | × | × | • | d8 = 2 | ||||||
3 | × | × | × | × | × | • | d7 = 5 | |||
4 | • | d6 = 0 | ||||||||
5 | × | • | d5 = 1 | |||||||
6 | × | × | × | • | d4 = 3 | |||||
7 | × | × | • | d3 = 2 | ||||||
8 | • | d2 = 0 | ||||||||
9 | • | d1 = 0 | ||||||||
Inversion table | 3 | 6 | 1 | 2 | 4 | 0 | 2 | 0 | 0 |
|
In the Lehmer code for a permutation σ, the number dn represents the choice made for the first term σ1, the number dn−1 represents the choice made for the second term σ2 among the remaining n − 1 elements of the set, and so forth. More precisely, each dn+1−i gives the number of remaining elements strictly less than the term σi. Since those remaining elements are bound to turn up as some later term σj, the digit dn+1−i counts the inversions (i,j) involving i as smaller index (the number of values j for which i < j and σi > σj). The inversion table for σ is quite similar, but here dn+1−k counts the number of inversions (i,j) where k = σj occurs as the smaller of the two values appearing in inverted order. Both encodings can be visualized by an n by n Rothe diagram (named after Heinrich August Rothe) in which dots at (i,σi) mark the entries of the permutation, and a cross at (i,σj) marks the inversion (i,j); by the definition of inversions a cross appears in any square that comes both before the dot (j,σj) in its column, and before the dot (i,σi) in its row. The Lehmer code lists the numbers of crosses in successive rows, while the inversion table lists the numbers of crosses in successive columns; it is just the Lehmer code for the inverse permutation, and vice versa.
To effectively convert a Lehmer code dn, dn−1, ..., d2, d1 into a permutation of an ordered set S, one can start with a list of the elements of S in increasing order, and for i increasing from 1 to n set σi to the element in the list that is preceded by dn+1−i other ones, and remove that element from the list. To convert an inversion table dn, dn−1, ..., d2, d1 into the corresponding permutation, one can traverse the numbers from d1 to dn while inserting the elements of S from largest to smallest into an initially empty sequence; at the step using the number d from the inversion table, the element from S inserted into the sequence at the point where it is preceded by d elements already present. Alternatively one could process the numbers from the inversion table and the elements of S both in the opposite order, starting with a row of n empty slots, and at each step place the element from S into the empty slot that is preceded by d other empty slots.
Converting successive natural numbers to the factorial number system produces those sequences in lexicographic order (as is the case with any mixed radix number system), and further converting them to permutations preserves the lexicographic ordering, provided the Lehmer code interpretation is used (using inversion tables, one gets a different ordering, where one starts by comparing permutations by the place of their entries 1 rather than by the value of their first entries). The sum of the numbers in the factorial number system representation gives the number of inversions of the permutation, and the parity of that sum gives the signature of the permutation. Moreover, the positions of the zeroes in the inversion table give the values of left-to-right maxima of the permutation (in the example 6, 8, 9) while the positions of the zeroes in the Lehmer code are the positions of the right-to-left minima (in the example positions the 4, 8, 9 of the values 1, 2, 5); this allows computing the distribution of such extrema among all permutations. A permutation with Lehmer code dn, dn−1, ..., d2, d1 has an ascent n − i if and only if di ≥ di+1.
Algorithms to generate permutations
In computing it may be required to generate permutations of a given sequence of values. The methods best adapted to do this depend on whether one wants some randomly chosen permutations, or all permutations, and in the latter case if a specific ordering is required. Another question is whether possible equality among entries in the given sequence is to be taken into account; if so, one should only generate distinct multiset permutations of the sequence.
An obvious way to generate permutations of n is to generate values for the Lehmer code (possibly using the factorial number system representation of integers up to n!), and convert those into the corresponding permutations. However, the latter step, while straightforward, is hard to implement efficiently, because it requires n operations each of selection from a sequence and deletion from it, at an arbitrary position; of the obvious representations of the sequence as an array or a linked list, both require (for different reasons) about n2/4 operations to perform the conversion. With n likely to be rather small (especially if generation of all permutations is needed) that is not too much of a problem, but it turns out that both for random and for systematic generation there are simple alternatives that do considerably better. For this reason it does not seem useful, although certainly possible, to employ a special data structure that would allow performing the conversion from Lehmer code to permutation in O(n log n) time.
Random generation of permutations
For generating random permutations of a given sequence of n values, it makes no difference whether one applies a randomly selected permutation of n to the sequence, or chooses a random element from the set of distinct (multiset) permutations of the sequence. This is because, even though in case of repeated values there can be many distinct permutations of n that result in the same permuted sequence, the number of such permutations is the same for each possible result. Unlike for systematic generation, which becomes unfeasible for large n due to the growth of the number n!, there is no reason to assume that n will be small for random generation.
The basic idea to generate a random permutation is to generate at random one of the n! sequences of integers d1,d2,...,dn satisfying 0 ≤ di < i (since d1 is always zero it may be omitted) and to convert it to a permutation through a bijective correspondence. For the latter correspondence one could interpret the (reverse) sequence as a Lehmer code, and this gives a generation method first published in 1938 by Ronald Fisher and Frank Yates. While at the time computer implementation was not an issue, this method suffers from the difficulty sketched above to convert from Lehmer code to permutation efficiently. This can be remedied by using a different bijective correspondence: after using di to select an element among i remaining elements of the sequence (for decreasing values of i), rather than removing the element and compacting the sequence by shifting down further elements one place, one swaps the element with the final remaining element. Thus the elements remaining for selection form a consecutive range at each point in time, even though they may not occur in the same order as they did in the original sequence. The mapping from sequence of integers to permutations is somewhat complicated, but it can be seen to produce each permutation in exactly one way, by an immediate induction. When the selected element happens to be the final remaining element, the swap operation can be omitted. This does not occur sufficiently often to warrant testing for the condition, but the final element must be included among the candidates of the selection, to guarantee that all permutations can be generated.
The resulting algorithm for generating a random permutation of a[0], a[1], ..., a[n − 1]
can be described as follows in pseudocode:
for i from n downto 2 do di ← random element of { 0, ..., i − 1 } swap a[di] and a[i − 1]
This can be combined with the initialization of the array a[i] = i
as follows
for i from 0 to n−1 do di+1 ← random element of { 0, ..., i } a[i] ← a[di+1] a[di+1] ← i
If di+1 = i, the first assignment will copy an uninitialized value, but the second will overwrite it with the correct value i.
However, Fisher-Yates is not the fastest algorithm for generating a permutation, because Fisher-Yates is essentially a sequential algorithm and "divide and conquer" procedures can achieve the same result in parallel.
Generation in lexicographic order
There are many ways to systematically generate all permutations of a given sequence. One classic, simple, and flexible algorithm is based upon finding the next permutation in lexicographic ordering, if it exists. It can handle repeated values, for which case it generates each distinct multiset permutation once. Even for ordinary permutations it is significantly more efficient than generating values for the Lehmer code in lexicographic order (possibly using the factorial number system) and converting those to permutations. It begins by sorting the sequence in (weakly) increasing order (which gives its lexicographically minimal permutation), and then repeats advancing to the next permutation as long as one is found. The method goes back to Narayana Pandita in 14th century India, and has been rediscovered frequently.
The following algorithm generates the next permutation lexicographically after a given permutation. It changes the given permutation in-place.
- Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation.
- Find the largest index l greater than k such that a[k] < a[l].
- Swap the value of a[k] with that of a[l].
- Reverse the sequence from a[k + 1] up to and including the final element a[n].
For example, given the sequence [1, 2, 3, 4] (which is in increasing order), and given that the index is zero-based, the steps are as follows:
- Index k = 2, because 3 is placed at an index that satisfies condition of being the largest index that is still less than a[k + 1] which is 4.
- Index l = 3, because 4 is the only value in the sequence that is greater than 3 in order to satisfy the condition a[k] < a[l].
- The values of a[2] and a[3] are swapped to form the new sequence [1, 2, 4, 3].
- The sequence after k-index a[2] to the final element is reversed. Because only one value lies after this index (the 3), the sequence remains unchanged in this instance. Thus the lexicographic successor of the initial state is permuted: [1, 2, 4, 3].
Following this algorithm, the next lexicographic permutation will be [1, 3, 2, 4], and the 24th permutation will be [4, 3, 2, 1] at which point a[k] < a[k + 1] does not exist, indicating that this is the last permutation.
This method uses about 3 comparisons and 1.5 swaps per permutation, amortized over the whole sequence, not counting the initial sort.
Generation with minimal changes
An alternative to the above algorithm, the Steinhaus–Johnson–Trotter algorithm, generates an ordering on all the permutations of a given sequence with the property that any two consecutive permutations in its output differ by swapping two adjacent values. This ordering on the permutations was known to 17th-century English bell ringers, among whom it was known as "plain changes". One advantage of this method is that the small amount of change from one permutation to the next allows the method to be implemented in constant time per permutation. The same can also easily generate the subset of even permutations, again in constant time per permutation, by skipping every other output permutation.
An alternative to Steinhaus–Johnson–Trotter is Heap's algorithm, said by Robert Sedgewick in 1977 to be the fastest algorithm of generating permutations in applications.
The following figure shows the output of all three aforementioned algorithms for generating all permutations of length , and of six additional algorithms described in the literature.
- Lexicographic ordering;
- Steinhaus–Johnson–Trotter algorithm;
- Heap's algorithm;
- Ehrlich's star-transposition algorithm: in each step, the first entry of the permutation is exchanged with a later entry;
- Zaks' prefix reversal algorithm: in each step, a prefix of the current permutation is reversed to obtain the next permutation;
- Sawada-Williams' algorithm: each permutation differs from the previous one either by a cyclic left-shift by one position, or an exchange of the first two entries;
- Corbett's algorithm: each permutation differs from the previous one by a cyclic left-shift of some prefix by one position;
- Single-track ordering: each column is a cyclic shift of the other columns;
- Single-track Gray code: each column is a cyclic shift of the other columns, plus any two consecutive permutations differ only in one or two transpositions.
Meandric permutations
Meandric systems give rise to meandric permutations, a special subset of alternate permutations. An alternate permutation of the set {1, 2, ..., 2n} is a cyclic permutation (with no fixed points) such that the digits in the cyclic notation form alternate between odd and even integers. Meandric permutations are useful in the analysis of RNA secondary structure. Not all alternate permutations are meandric. A modification of Heap's algorithm has been used to generate all alternate permutations of order n (that is, of length 2n) without generating all (2n)! permutations. Generation of these alternate permutations is needed before they are analyzed to determine if they are meandric or not.
The algorithm is recursive. The following table exhibits a step in the procedure. In the previous step, all alternate permutations of length 5 have been generated. Three copies of each of these have a "6" added to the right end, and then a different transposition involving this last entry and a previous entry in an even position is applied (including the identity; that is, no transposition).
Previous sets | Transposition of digits | Alternate permutations |
---|---|---|
1-2-3-4-5-6 | 1-2-3-4-5-6 | |
4, 6 | 1-2-3-6-5-4 | |
2, 6 | 1-6-3-4-5-2 | |
1-2-5-4-3-6 | 1-2-5-4-3-6 | |
4, 6 | 1-2-5-6-3-4 | |
2, 6 | 1-6-5-4-3-2 | |
1-4-3-2-5-6 | 1-4-3-2-5-6 | |
2, 6 | 1-4-3-6-5-2 | |
4, 6 | 1-6-3-2-5-4 | |
1-4-5-2-3-6 | 1-4-5-2-3-6 | |
2, 6 | 1-4-5-6-3-2 | |
4, 6 | 1-6-5-2-3-4 |