PUBLICATIONS DE L’INSTITUT MATHEMATIQUE´ Nouvelle s´erie,tome 78(92) (2005), 87–92 NOTE ON GRAY CODES FOR PERMUTATION LISTS Seymour Lipschutz, Jie Gao, and Dianjun Wang Communicated by Slobodan Simi´c Abstract. Robert Sedgewick [5] lists various Gray codes for the permutations in Sn including the classical algorithm by Johnson and Trotter. Here we give an algorithm which constructs many families of Gray codes for Sn, which closely follows the construction of the Binary Reflexive Gray Code for the n-cube Qn. 1. Introduction There are (n!)! ways to list the n! permutations in Sn. Some such permutation lists have been generated by a computer, and some such permutation lists are Gray codes (where successive permutations differ by a transposition). One such famous Gray code for Sn is by Johnson [4] and Trotter [6]. In fact, each new permutation in the Johnson–Trotter (JT ) list for Sn is obtained from the preceding permutation by an adjacent transposition. Sedgewick [5] gave a survey of various Gray codes for Sn in 1977. Subsequently, Conway, Sloane and Wilks [1] gave a new Gray code CSW for Sn in 1989 while proving the existence of Gray codes for the reflection groups. Recently, Gao and Wang [2] gave simple algorithms, each with an efficient implementation, for two new permutation lists GW1 and GW2 for Sn, where the second such list is a Gray code for Sn. The four lists, JT , CSW , GW1 and GW2, for n = 4, are pictured in Figure 1. This paper gives an algorithm for constructing many families of Gray codes for Sn which uses an idea from the construction of the Binary Reflected Gray Code (BRGC) of the n-cube Qn. Our paper is organized as follows. Section 2 describes the Binary Reflected Gray Code for Qn, and also discusses the generation of the JT list and the CSW list for Sn. Section 3 gives our algorithm which constructs our new Gray code L(n) for Sn, and also constructs many families of Gray codes for Sn. 2000 Mathematics Subject Classification: 94B60, 68R05, 20F65, 05C38. Key words and phrases: Algorithms, Combinatorial problems, Permutations, Gray codes. The third author partially supported by NSFC(60172005). 87 88 LIPSCHUTZ, GAO, AND WANG List JT (4) List CSW (4) List GW1(4) List GW2(4) 1234 [123] 123 4 1243 4321 1234 1423 124 3 4312 1243 4123 142 3 4231 1423 412 3 4132 1324 4132 [132] 421 3 4213 1342 1432 241 3 4123 1432 1342 214 3 1324 3421 4132 123 4 3412 4123 3124 [312] 231 4 2431 2143 3142 1432 3142 3412 234 1 2413 3124 4312 243 1 1423 2134 423 1 4321 [321] 432 1 3241 2314 3421 342 1 3142 2413 3241 324 1 2341 4213 3214 1342 3214 321 4 2143 3412 2314 [231] 312 4 1243 4312 2341 2431 314 2 3214 4321 4231 341 2 3124 4231 431 2 2314 2431 4213 [213] 413 2 1324 3421 2413 143 2 2134 3241 2143 134 2 1234 2341 2134 132 4 Figure 1. JT , CSW , GW1 and GW2 Lists for n = 4 2. Gray Codes and the JT and CSW lists The original idea of a Gray code was to list the codewords (n-bit strings) in the n-cube Qn so that successive codewords differ in only one bit. Thus a Gray code for Qn is a Hamiltonian circuit through the 2n vertices of Qn. [The fact that this can be done for any Qn follows from the construction of the Binary Reflected Gray Code (BRGC) for Qn (see [3]) which we describe below.] The above idea of a Gray code has been generalized as follows. A Gray code for any combinatorial family of objects is a listing of the objects such that only a “small change” takes place from one object to the next in the list. The definition of “small change” depends on the particular family, its context, and its applications. NOTE ON GRAY CODES FOR PERMUTATION LISTS 89 Q3 Q3 reversed 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 Figure 2. Binary Reflected Gray Code for Q4 For example, for a finite group G with a set of generators, a Gray code for G is usually a Hamiltonian circuit in its Cayley graph or, equivalently, a listing of the elements of G so that successive elements differ by the product of a generator. This paper is mainly interested in the following type of Gray code for Sn. [Other types of Gray codes for Sn also exist.] Definition 1. A Gray code for Sn is a list L of the elements in Sn such that successive permutations differ by a transposition. We begin by describing a recursive construction of the Gray code for the n-cube Qn. Specifically, Figure 2 shows how the BRGC for Q4 can be obtained from the BRGC for Q3. That is, first we list the Gray code for Q3 which appears as the upper left 3 £ 8 matrix (where the codewords are the columns). Then next to it we list the Gray code for Q3 but in reverse order. This yields a 3 £ 8 matrix. Finally, we add a fourth row consisting of eight 0’s followed by eight 1’s. This gives the BRGC for Q4. We show that this is in fact a Gray code for Q4. Observe that only one bit changes within the first eight columns since the fourth bit is always 0, and only one bit changes within the last eight columns since the fourth bit is always 1. Furthermore, the two middle columns (and also the first and last columns) are identical except for the added last bits, and so again only one bit changes. The BRGC for Qn is the one obtained recursively in this way beginning with the matrix [0; 1] for Q = Q1. [Note this construction gives a Hamiltonian circuit, not just a Hamiltonian path, for the n-cube Qn.] Discussing the BRGC, Herbert Wilf [7] comments: “This method of copying a list and its reversal seems to be a good thing to think of when trying to construct some new kind of Gray code.” We do this below in our main algorithm. The Johnson–Trotter permutation list is also defined recursively. The list for n = 4 is shown in Figure 1. Note that the list is partitioned into six “blocks”, each with four successive permutations. Each block corresponds and is labelled by a permutation in S3 which is in boldface and is to the right of the block. We note that the boldface labels form the JT list for S3. Now in the first block, the largest item 4 sweeps from right to left, in the second block from left to right, in the third block for right to left, and so on. Also, the relative positions of the remaining items 1, 2, 3 do not change in each block and correspond to the label of the block. Moreover, the recursive changes of the relative positions of 1, 2, 3 occur only from block to block when the largest item 4 is in an end position. Thus the item 4 does not interfere with any transposition involving 90 LIPSCHUTZ, GAO, AND WANG the items 1, 2, 3. Thus the Johnson–Trotter list JT (n) is a Gray code for Sn. [In fact, successive permutations in JT (n) differ by only an adjacent transposition.] The Conway–Sloane–Wilks (CSW ) list is also defined recursively. The second column in Figure 1 indicates how CSW (4) is obtained from CSW (3). First the number n = 4 is appended to each permutation in CSW (3) which appears in boldface in Column 2. Then a sequence of 3! = 6 permutations is inserted between each pair of permutations in CSW (3) which end in the same number, that is, between 123 and 213, between 231 and 321, and between 312 and 132. We refer the reader to their paper for a full description of their algorithm. The last two columns in Figure 1 contain, respectively, the Gao–Wang permu- tation lists GW1 and GW2 for n = 4. Observe that each list is partitioned into four “blocks”, where each block contains 6 successive permutations (which actually correspond to the 6 permutations in S3). We describe the construction of the list GW1(n) here by showing how GW1(4) is obtained from GW1(3). The first block in GW1(4) contains, in boldface, the permutation list for GW1(3) with each such permutation preceded by the number 4. The second block contains the same per- mutation list GW1(3) but now the 4 is in the second position. And so on. The construction of the permutation list GW2 lies beyond the scope of this paper. 3. Generating Algorithm and a New Gray Code L(n) for Sn Here we give an algorithm which is used to generate a new Gray code L(n) for Sn and which can also be used to generate many other families of gray codes for Sn. First, however, we need some new notation and terminology. Let L be a list. We let LR denote the list L written backwards, and we let r ¤ L denote the list consisting of r alternating copies of L and LR; that is: r ¤ L = [L; LR; L; LR; :::; L or LR](r components): Each copy of L or LR will be called a block in r ¤ L.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages6 Page
-
File Size-