| ||||||||||||||||||||||||||||||||||
Two elements in each of the permutations in (2) define 3 positions for the 3rd element 3:
Three elements in each of the permutations in (3) define 4 positions to place the 4th element 4. For example, the first permutation there
In general, there are N ways to extend a permutation
The method just described is simple and appeals directly to the definition of a permutation. No tricks are involved. It has a drawback, though. You must compute a whole pyramid of permutations shorter than the ones you need. Some may not mind doing that. Some may be curious if there are shortcuts. The Johnson-Trotter algorithm (H. F. Trotter[1962], S. M. Johnson[1963]) offers a clever way to directly generate permutations of the required length without computing shorter permutations. If you stare for a while at (*), it may occur to you that N - the length of and the largest number in the permutations - travels rightwards from the leftmost available position to the rightmost. Of course, listing the permutations (*) in the reverse order would reverse the direction of the travel as well. To embody the traveling metaphor into action, the Johnson-Trotter algorithm replaces integer as the basic element of the procedure with a structure that maintains an integer and a direction. Call the structure a directed integer, or just an integer if the context is clear. The direction may take on only one of two values, "left" or "right", and can be implemented as a boolean. In the applet below, an integer k directed leftwards appears as <k; directed rightwards, it appears as k>. It's convenient and certainly appropriate to say that a directed integer is looking left- or rightwards depending on the value of the attached field direction. The algorithm requires one more definition: a directed integer is said to be mobile if it is greater than its immediate neighbor in the direction it is looking at. The Johnson-Trotter algorithm can now be described in five lines:
If you try the algorithm by hand or study the sequence of the permutations produced by the applet, you cannot fail to observe that the largest integer N first travels leftwards, lingers there just for one step that flips its direction and also changes a shorter permutation, so that the integer may now travel rightwards, where again at the end it skips a step that changes its direction and the underlying (N-1)-permutation, and so on. The process continues until a strange permutation
is reached at which point no mobile integers exist. Note that this permutation is obtained from the initial
Thus the algorithm generates a circular sequence of permutations and satisfies a (strong) minimal-change requirement. In that the algorithm is reminiscent of the de Bruijn cycle and the Gray codes. Reference
Permutations
Copyright © 1996-2008 Alexander Bogomolny
|
| |||||||||||||||||||||||||||||||||