Steinhaus–Johnson–Trotter algorithm

From HandWiki
Revision as of 23:24, 6 February 2024 by NBrushPhys (talk | contribs) (correction)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
The Hamiltonian cycle in the Cayley graph of the symmetric group generated by the Steinhaus–Johnson–Trotter algorithm
Wheel diagram of all permutations of length [math]\displaystyle{ n=4 }[/math] generated by the Steinhaus-Johnson-Trotter algorithm, where each permutation is color-coded (1=blue, 2=green, 3=yellow, 4=red).

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 [math]\displaystyle{ n }[/math] elements. Each two adjacent permutations in the resulting sequence differ by swapping two adjacent permuted elements. Equivalently, this algorithm finds a Hamiltonian cycle in the permutohedron, a polytope whose vertices represent permutations and whose edges represent swaps.

This method was known already to 17th-century English change ringers, and Robert Sedgewick calls it "perhaps the most prominent permutation enumeration algorithm".[1] 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.[1]

Algorithm

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.

Recursive structure

The sequence of permutations for a given number [math]\displaystyle{ n }[/math] can be formed from the sequence of permutations for [math]\displaystyle{ n-1 }[/math] by placing the number [math]\displaystyle{ n }[/math] into each possible position in each of the shorter permutations. The Steinhaus–Johnson–Trotter algorithm follows this structure: the sequence of permutations it generates consists of [math]\displaystyle{ (n-1)! }[/math] blocks of permutations, so that within each block the permutations agree on the ordering of the numbers from 1 to [math]\displaystyle{ n-1 }[/math] and differ only in the position of [math]\displaystyle{ n }[/math]. The blocks themselves are ordered recursively, according to the Steinhaus–Johnson–Trotter algorithm for one less element. Within each block, the positions in which [math]\displaystyle{ n }[/math] is placed occur either in descending or ascending order, and the blocks alternate between these two orders: the placements of [math]\displaystyle{ n }[/math] in the first block are in descending order, in the second block they are in ascending order, in the third block they are in descending order, and so on.[2]

Thus, from the single permutation on one element,

1

one may place the number 2 in each possible position in descending order to form a list of two permutations on two elements,

1 2
2 1

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:

1 2 3
1 3 2
3 1 2
3 2 1
2 3 1
2 1 3

The same placement pattern, alternating between descending and ascending placements of [math]\displaystyle{ n }[/math], applies for any larger value of [math]\displaystyle{ n }[/math].[2] In sequences of permutations with this recursive structure, each permutation differs from the previous one either by the single-position-at-a-time motion of [math]\displaystyle{ n }[/math], or by a change of two smaller numbers inherited from the previous sequence of shorter permutations. In either case this difference is just the transposition of two adjacent elements. When [math]\displaystyle{ n \gt 1 }[/math] the first and final elements of the sequence, also, differ in only two adjacent elements (the positions of the numbers [math]\displaystyle{ 1 }[/math] and [math]\displaystyle{ 2 }[/math]), as may be proven by induction.

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.[2] The same ordering of permutations can also be described equivalently as the ordering generated by the following greedy algorithm.[3] Start with the identity permutation [math]\displaystyle{ 1\;2\;\ldots\;n }[/math]. Now repeatedly transpose the largest possible entry with the entry to its left or right, such that in each step, a new permutation is created that has not been encountered in the list of permutations before. For example, in the case [math]\displaystyle{ n=3 }[/math] the sequence starts with [math]\displaystyle{ 1\;2\;3 }[/math], then flips [math]\displaystyle{ 3 }[/math] with its left neighbor to get [math]\displaystyle{ 1\;3\;2 }[/math]. From this point, flipping [math]\displaystyle{ 3 }[/math] with its right neighbor [math]\displaystyle{ 2 }[/math] would yield the initial permutation [math]\displaystyle{ 1\;2\;3 }[/math], so the sequence instead flips [math]\displaystyle{ 3 }[/math] with its left neighbor [math]\displaystyle{ 1 }[/math] and arrives at [math]\displaystyle{ 3\;1\;2 }[/math], etc. The direction of the transposition (left or right) is always uniquely determined in this algorithm. However, the actual Steinhaus–Johnson–Trotter algorithm does not use recursion, and does not need to keep track of the permutations that it has already encountered. Instead, it computes the same sequence of permutations by a simple iterative method.

Original version

As described by Johnson, the algorithm for generating the next permutation from a given permutation [math]\displaystyle{ \pi }[/math] performs the following steps.

  • For each [math]\displaystyle{ i }[/math] from 1 to [math]\displaystyle{ n }[/math], let [math]\displaystyle{ x_i }[/math] be the position where the value [math]\displaystyle{ i }[/math] is placed in permutation [math]\displaystyle{ \pi }[/math]. If the order of the numbers from 1 to [math]\displaystyle{ i-1 }[/math] in permutation [math]\displaystyle{ \pi }[/math] defines an even permutation, let [math]\displaystyle{ y_i=x_i-1 }[/math] otherwise, let [math]\displaystyle{ y_i=x_i+1 }[/math].
  • Find the largest number [math]\displaystyle{ i }[/math] for which [math]\displaystyle{ y_i }[/math] defines a valid position in permutation [math]\displaystyle{ \pi }[/math] that contains a number smaller than [math]\displaystyle{ i }[/math]. Swap the values in positions [math]\displaystyle{ x_i }[/math] and [math]\displaystyle{ y_i }[/math].

When no number [math]\displaystyle{ i }[/math] can be found meeting the conditions of the second step of the algorithm, the algorithm has reached the final permutation of the sequence and terminates. This procedure may be implemented in [math]\displaystyle{ O(n) }[/math] time per permutation.[4]

Trotter gives an alternative implementation of an iterative algorithm for the same sequence, in lightly commented ALGOL 60 notation.[5]

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.[6]

Even's speedup

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:

1 −2 −3

At each step, the algorithm finds the greatest element with a nonzero direction, and swaps it in the indicated direction:

1 −3 −2

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:

3 1 −2

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:

+3 2 1

The remaining two steps of the algorithm for [math]\displaystyle{ n=3 }[/math] are:

2 +3 1
2 1 3

When all numbers become unmarked, the algorithm terminates.[7]

This algorithm takes time [math]\displaystyle{ O(i) }[/math] for every step in which the greatest number to move is [math]\displaystyle{ n-i+1 }[/math]. Thus, the swaps involving the number [math]\displaystyle{ n }[/math] take only constant time; since these swaps account for all but a [math]\displaystyle{ 1/n }[/math] fraction of all of the swaps performed by the algorithm, the average time per permutation generated is also constant, even though a small number of permutations will take a larger amount of time.[1]

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.[8]

Related structures

Permutohedron

The set of all permutations of [math]\displaystyle{ n }[/math] items may be represented geometrically by a permutohedron, the polytope formed from the convex hull of [math]\displaystyle{ n! }[/math] vectors, the permutations of the vector [math]\displaystyle{ (1,2,\dots n) }[/math]. Although defined in this way in [math]\displaystyle{ n }[/math]-dimensional space, it is actually an [math]\displaystyle{ (n-1) }[/math]-dimensional polytope; for example, the permutohedron on four items is a three-dimensional polyhedron, the truncated octahedron. If each vertex of the permutohedron is labeled by the inverse permutation to the permutation defined by its vertex coordinates, the resulting labeling describes a Cayley graph of the symmetric group of permutations on [math]\displaystyle{ n }[/math] items, as generated by the permutations that swap adjacent pairs of items. Thus, each two consecutive permutations in the sequence generated by the Steinhaus–Johnson–Trotter algorithm correspond in this way to two vertices that form the endpoints of an edge in the permutohedron, and the whole sequence of permutations describes a Hamiltonian path in the permutohedron, a path that passes through each vertex exactly once. If the sequence of permutations is completed by adding one more edge from the last permutation to the first one in the sequence, the result is instead a Hamiltonian cycle.[9]

Gray codes

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 [math]\displaystyle{ n! }[/math] permutations of the [math]\displaystyle{ n }[/math] numbers from 1 to [math]\displaystyle{ n }[/math] may be placed in one-to-one correspondence with the [math]\displaystyle{ n! }[/math] numbers from 0 to [math]\displaystyle{ n!-1 }[/math] by pairing each permutation with the sequence of numbers [math]\displaystyle{ c_i }[/math] that count the number of positions in the permutation that are to the right of value [math]\displaystyle{ i }[/math] and that contain a value less than [math]\displaystyle{ i }[/math] (that is, the number of inversions for which [math]\displaystyle{ i }[/math] is the larger of the two inverted values), and then interpreting these sequences as numbers in the factorial number system, that is, the mixed radix system with radix sequence [math]\displaystyle{ (1,2,3,4,\dots) }[/math] For instance, the permutation [math]\displaystyle{ (3,1,4,5,2) }[/math] would give the values [math]\displaystyle{ c_1=0 }[/math], [math]\displaystyle{ c_2=0 }[/math], [math]\displaystyle{ c_3=2 }[/math], [math]\displaystyle{ c_4=1 }[/math], and [math]\displaystyle{ c_5=1 }[/math]. The sequence of these values, [math]\displaystyle{ (0,0,2,1,1) }[/math], gives the number [math]\displaystyle{ 0\times 0!+0\times 1!+2\times 2!+1\times 3!+1\times 4!=34. }[/math] Consecutive permutations in the sequence generated by the Steinhaus–Johnson–Trotter algorithm have numbers of inversions that differ by one, forming a Gray code for the factorial number system.[10]

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.[11]

History

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,[12] and the general method has been traced to an unpublished 1653 manuscript by Peter Mundy.[6] A 1677 book by Fabian Stedman lists the solutions for up to six bells.[13] 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.[14]

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.[15] 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.[16] A 1976 paper by Hu and Bien credited Steinhaus with formulating the algorithmic problem of generating all permutations,[17] and by 1989 his book had been (incorrectly) credited as one of the original publications of the algorithm.[18]

See also

Notes

  1. 1.0 1.1 1.2 Sedgewick (1977).
  2. 2.0 2.1 2.2 (Lenstra Rinnooy Kan); (Savage 1997), section 3; (Bird 2010).
  3. Williams (2013).
  4. Johnson (1963).
  5. Trotter (1962).
  6. 6.0 6.1 Knuth (2011).
  7. Even (1973).
  8. (Ehrlich 1973); (Dershowitz 1975); (Sedgewick 1977); (Bird 2010).
  9. See, e.g., section 11 of (Savage 1997).
  10. (Dijkstra 1976); (Knuth 2011).
  11. Savage (1997).
  12. White (1996).
  13. (Stedman 1677); for lists of plain changes on up to six bells, see pp. 48–80.
  14. (McGuire 2003); (Knuth 2011).
  15. (Johnson 1963); (Trotter 1962).
  16. Steinhaus (1964).
  17. Hu & Tien (1976).
  18. Ruskey (1989).

References