Lehmer code: Difference between revisions

Content deleted Content added
MalnadachBot (talk | contribs)
m Fixed Lint errors - replaced obsolete center tags. (Task 12)
m clean up, typo(s) fixed: … → ... (7)
Line 6:
The Lehmer code makes use of the fact that there are
:<math>n!=n\times(n-1)\times\cdots\times2\times1</math>
permutations of a sequence of ''n'' numbers. If a permutation ''σ'' is specified by the sequence (''σ''<sub>1</sub>, ..., ''σ''<sub>''n''</sub>) of its images of 1, ..., ''n'', then it is encoded by a sequence of ''n'' numbers, but not all such sequences are valid since every number must be used only once. By contrast the encodings considered here choose the first number from a set of ''n'' values, the next number from a fixed set of {{math|''n'' − 1}} values, and so forth decreasing the number of possibilities until the last number for which only a single fixed value is allowed; ''every'' sequence of numbers chosen from these sets encodes a single permutation. While several [[encoding]]s can be defined, the Lehmer code has several additional useful properties; it is the sequence
:<math>L(\sigma)=(L(\sigma)_1,\ldots,L(\sigma)_n)\quad\text{where}\quad L(\sigma)_i=\#\{ j>i : \sigma_j<\sigma_i \},</math>
in other words the term ''L''(''σ'')<sub>''i''</sub> counts the number of terms in (''σ''<sub>1</sub>, ..., ''σ''<sub>''n''</sub>) to the right of ''σ''<sub>''i''</sub> that are smaller than it, a number between 0 and {{math|''n'' − ''i''}}, allowing for {{math|''n'' + 1 − ''i''}} different values.
 
A pair of indices (''i'',''j'') with {{math|''i'' &lt; ''j''}} and {{math|''σ''<sub>''i''</sub> &gt; ''σ''<sub>''j''</sub>}} is called an '''inversion''' of ''σ'', and ''L''(''σ'')<sub>''i''</sub> counts the number of inversions (''i'',''j'') with ''i'' fixed and varying ''j''. It follows that {{math|''L''(''σ'')<sub>1</sub> + ''L''(''σ'')<sub>2</sub> + … + ''L''(''σ'')<sub>''n''</sub>}} is the total number of inversions of ''σ'', which is also the number of adjacent transpositions that are needed to transform the permutation into the identity permutation. Other properties of the Lehmer code include that the [[lexicographical order]] of the encodings of two permutations is the same as that of their sequences (''σ''<sub>1</sub>, ..., ''σ''<sub>''n''</sub>), that any value 0 in the code represents a right-to-left minimum in the permutation (i.e., a {{math|''σ''<sub>''i''</sub>}} smaller than any {{math|''σ''<sub>''j''</sub>}} to its right), and a value {{math|''n'' − ''i''}}
at position ''i'' similarly signifies a right-to-left maximum, and that the Lehmer code of ''σ'' coincides with the [[factorial number system]] representation of its position in the list of permutations of ''n'' in lexicographical order (numbering the positions starting from 0).
 
Line 19:
The usual way to prove that there are ''n''! different permutations of ''n'' objects is to observe that the first object can be chosen in {{math|''n''}} different ways, the next object in {{math|''n'' − 1}} different ways (because choosing the same number as the first is forbidden), the next in {{math|''n'' − 2}} different ways (because there are now 2 forbidden values), and so forth. Translating this freedom of choice at each step into a number, one obtains an encoding algorithm, one that finds the Lehmer code of a given permutation. One need not suppose the objects permuted to be numbers, but one needs a [[total ordering]] of the set of objects. Since the code numbers are to start from 0, the appropriate number to encode each object ''σ''<sub>''i''</sub> by is the number of objects that were available at that point (so they do not occur before position ''i''), but which are smaller than the object ''σ''<sub>''i''</sub> actually chosen. (Inevitably such objects must appear at some position {{math|''j'' &gt; ''i''}}, and (''i'',''j'') will be an inversion, which shows that this number is indeed ''L''(''σ'')<sub>''i''</sub>.)
 
This number to encode each object can be found by direct counting, in several ways (directly counting inversions, or correcting the total number of objects smaller than a given one, which is its sequence number starting from 0 in the set, by those that are unavailable at its position). Another method which is in-place, but not really more efficient, is to start with the permutation of {0, 1, ... {{math|''n'' − 1}}} obtained by representing each object by its mentioned sequence number, and then for each entry ''x'', in order from left to right, correct the items to its right by subtracting 1 from all entries (still) greater than ''x'' (to reflect the fact that the object corresponding to ''x'' is no longer available). Concretely a Lehmer code for the permutation B,F,A,G,D,E,C of letters, ordered alphabetically, would first give the list of sequence numbers 1,5,0,6,3,4,2, which is successively transformed
:<math> \begin{matrix}
\mathbf1&5&0&6&3&4&2\\
Line 32:
where the final line is the Lehmer code (at each line one subtracts 1 from the larger entries to the right of the boldface element to form the next line).
 
For decoding a Lehmer code into a permutation of a given set, the latter procedure may be reversed: for each entry ''x'', in order from right to left, correct the items to its right by adding 1 to all those (currently) greater than or equal to ''x''; finally interpret the resulting permutation of {0, 1, ... {{math|''n'' − 1}}} as sequence numbers (which amounts to adding 1 to each entry if a permutation of {1, 2, ... ''n''} is sought). Alternatively the entries of the Lehmer code can be processed from left to right, and interpreted as a number determining the next choice of an element as indicated above; this requires maintaining a list of available elements, from which each chosen element is removed. In the example this would mean choosing element 1 from {A,B,C,D,E,F,G} (which is B) then element 4 from {A,C,D,E,F,G} (which is F), then element 0 from {A,C,D,E,G} (giving A) and so on, reconstructing the sequence B,F,A,G,D,E,C.
 
==Applications to combinatorics and probabilities==