The Steinhaus–Johnson–Trotter algorithm or Johnson - Trotter algorithm, also called plain changes, is an algorithm named after Hugo Steinhaus, Selmer M. Johnson and Hale F. Trotter that generates all of the permutations of
n
This method was known already to 17th-century English change ringers, and Robert Sedgewick calls it "perhaps the most prominent permutation enumeration algorithm". A version of the algorithm can be implemented in such a way that the average time per permutation is constant. As well as being simple and computationally efficient, this algorithm has the advantage that subsequent computations on the generated permutations may be sped up by taking advantage of the similarity between consecutive permutations.
The sequence of permutations generated by the Steinhaus–Johnson–Trotter algorithm has a natural recursive structure, that can be generated by a recursive algorithm. However the actual Steinhaus–Johnson–Trotter algorithm does not use recursion, instead computing the same sequence of permutations by a simple iterative method. A later improvement allows it to run in constant average time per permutation.
The sequence of permutations for a given number
n
n-1
n
(n-1)!
n-1
n
n
n
Thus, from the single permutation on one element,one may place the number 2 in each possible position in descending order to form a list of two permutations on two elements,Then, one may place the number 3 in each of three different positions for these two permutations, in descending order for the first permutation 1 2, and then in ascending order for the permutation 2 1:The same placement pattern, alternating between descending and ascending placements of
n
n
n
n>1
1
2
This sequence may be generated by a recursive algorithm that constructs the sequence of smaller permutations and then performs all possible insertions of the largest number into the recursively-generated sequence.[1] The same ordering of permutations can also be described equivalently as the ordering generated by the following greedy algorithm.Start with the identity permutation
1 2 \ldots n
n=3
1 2 3
3
1 3 2
3
2
1 2 3
3
1
3 1 2
As described by Johnson, the algorithm for generating the next permutation from a given permutation
\pi
i
n
xi
i
\pi
i-1
\pi
yi=xi-1
yi=xi+1
i
yi
\pi
i
xi
yi
i
O(n)
Trotter gives an alternative implementation of an iterative algorithm for the same sequence, in lightly commented ALGOL 60 notation.
Because this method generates permutations that alternate between being even and odd, it may easily be modified to generate only the even permutations or only the odd permutations: to generate the next permutation of the same parity from a given permutation, simply apply the same procedure twice.
A subsequent improvement by Shimon Even provides an improvement to the running time of the algorithm by storing additional information for each element in the permutation: its position, and a direction (positive, negative, or zero) in which it is currently moving (essentially, this is the same information computed using the parity of the permutation in Johnson's version of the algorithm). Initially, the direction of the number 1 is zero, and all other elements have a negative direction:At each step, the algorithm finds the greatest element with a nonzero direction, and swaps it in the indicated direction:If this causes the chosen element to reach the first or last position within the permutation, or if the next element in the same direction is greater than the chosen element, the direction of the chosen element is set to zero:After each step, all elements greater than the chosen element (which previously had direction zero) have their directions set to indicate motion toward the chosen element. That is, positive for all elements between the start of the permutation and the chosen element, and negative for elements toward the end. Thus, in this example, after the number 2 moves, the number 3 becomes marked with a direction again:The remaining two steps of the algorithm for
n=3
This algorithm takes time
O(i)
n-i+1
n
1/n
A more complex loopless version of the same procedure suitable for functional programming allows it to be performed in constant time per permutation in every case; however, the modifications needed to eliminate loops from the procedure make it slower in practice.[2]
The set of all permutations of
n
n!
(1,2,...n)
n
(n-1)
n
A Gray code for numbers in a given radix is a sequence that contains each number up to a given limit exactly once, in such a way that each pair of consecutive numbers differs by one in a single digit. The
n!
n
n
n!
n!-1
ci
i
i
i
(1,2,3,4,...)
(3,1,4,5,2)
c1=0
c2=0
c3=2
c4=1
c5=1
(0,0,2,1,1)
More generally, combinatorial algorithms researchers have defined a Gray code for a set of combinatorial objects to be an ordering for the objects in which each two consecutive objects differ in the minimal possible way. In this generalized sense, the Steinhaus–Johnson–Trotter algorithm generates a Gray code for the permutations themselves.
The method was known for much of its history as a method for change ringing of church bells: it gives a procedure by which a set of bells can be rung through all possible permutations, changing the order of only two bells per change. These so-called "plain changes" or "plain hunt" were known by circa 1621 for four bells, and the general method has been traced to an unpublished 1653 manuscript by Peter Mundy. A 1677 book by Fabian Stedman lists the solutions for up to six bells.[5] More recently, change ringers have abided by a rule that no bell may stay in the same position for three consecutive permutations; this rule is violated by the plain changes, so other strategies that swap multiple bells per change have been devised.[6]
The algorithm is named after Hugo Steinhaus, Selmer M. Johnson and Hale F. Trotter. Johnson and Trotter rediscovered the algorithm independently of each other in the early 1960s.[7] A 1958 book by Steinhaus, translated into English in 1964, describes a related impossible puzzle of generating all permutations by a system of particles, each moving at constant speed along a line and swapping positions when one particle overtakes another. A 1976 paper by Hu and Bien credited Steinhaus with formulating the algorithmic problem of generating all permutations, and by 1989 his book had been (incorrectly) credited as one of the original publications of the algorithm.