Prefix-reversal Gray codes

Elena Konstantinova

Sobolev Institute of Mathematics

joint work with Alexey Medvedev, CEU Budapest

Modern Trends in Algebraic Graph Theory Villanova University, USA, June 2–5, 2014

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 1 / 18 Binary reflected (BRGC)

Gray code [F. Gray, 1953, U.S. Patent 2,632,058] The reflected , also known as Gray code, is a binary numeral system where two successive values differ in only one .

Example n = 2: 00 01 | 11 10 n = 3: 000 001 011 010 | 110 111 101 100

BRGC is related to Hamiltonian cycles of graphs

b b 110 111 10 11 b b

b b 010 011

b b 100 101

b b

00 01 b b 000 001 Elena Konstantinova H2 Prefix-reversal Gray codes H3 Villanova–2014 2 / 18

b b

b b

b b

b b b b

b b

b b

b b Gray codes: generating combinatorial objects

Gray codes Now the term Gray code refers to minimal change order of combinatorial objects.

[D.E. Knuth, The Art of Computer Programming, Vol.4 (2010)] Knuth recently surveyed combinatorial generation: Gray codes are related to efficient for exhaustively generating combinatorial objects. (tuples, , combinations, partitions, trees)

[P. Eades, B. McKay, An of generating subsets of fixed size with a strong minimal change property (1984)] They followed to Gray‘s approach to order the k–combinations of an n element set.

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 3 / 18 Gray codes: generating permutations

[V.L. Kompel’makher, V.A. Liskovets, Successive generation of permutations by means of a transposition basis (1975)] Q: Is it possible to arrange permutations of a given length so that each is obtained from the previous one by a transposition? A: YES

[S. Zaks, A new algorithm for generation of permutations (1984)] In Zaks’ algorithm each successive permutation is generated by reversing a suffix of the preceding permutation.

Start with In = [12 . . . n] and in each step reverse a certain suffix. Let

ζn is the sequence of sizes of these suffixes defined by recursively as follows: ζ2 = 2 n−1 ζn = (ζn−1 n) ζn−1, n > 2, where a sequence is written as a concatenation of its elements.

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 4 / 18 Zaks’ algorithm: examples

If n = 2 then ζ2 = 2 and we have:

[12] [21]

If n = 3 then ζ3 = 23232 and we have: [123] [231] [312] [132] [213] [321]

If n = 4 then ζ4 = 23232423232423232423232 and we have: [1234] [2341] [3412] [4123] [1243] [2314] [3421] [4132] [1342] [2413] [3124] [4231] [1324] [2431] [3142] [4213] [1423] [2134] [3241] [4312] [1432] [2143] [3214] [4321]

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 5 / 18 Greedy Pancake Gray codes: generating permutations

[A. Williams, J. Sawada, Greedy pancake flipping (2013)] Take a stack of pancakes, numbered 1, 2, ..., n by increasing diameter, and repeat the following: Flip the maximum number of topmost pancakes that gives a new stack.

[1234] [4321] [2341] [1432] [3412] [2143] [4123] [3214] [2314] [4132] [3142] [2413] [1423] [3241] [4231] [1324] [3124] [4213] [1243] [3421] [2431] [1342] [4312] [2134]

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 6 / 18 Prefix–reversal Gray codes: generating permutations

Each ’flip’ is formally known as prefix–reversal.

The Pancake graph Pn is the Cayley graph on the symmetric group Symn with generating set {ri ∈ Symn, 1 6 i < n}, where ri is the operation of reversing the order of any substring [1, i], 1 < i 6 n, of a permutation π when multiplied on the right, i.e., [π1 ...πi πi+1 ...πn]ri = [πi ...π1πi+1 ...πn].

n Williams’ prefix–reversal Gray code: (rn rn−1) Flip the maximum number of topmost pancakes that gives a new stack.

3 Zaks’ prefix–reversal Gray code: (r3 r2) Flip the minimum number of topmost pancakes that gives a new stack.

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 7 / 18 Two scenarios of generating permutations: Zaks | Williams

r4 ! ! r r r2 r2 4 ! 3 r3 ! r4 ! ! ! ! ! ! ! ! r r r 3 4 4 r3 r3 r2 r2 r3 r ! ! 2 ! ! ! r ! ! r ! 3 3 r4 r4 r r ! ! ! r ! 2 ! ! 2 r4 4 r4 r4 r r3 ! ! 3 r2 r2

! ! r ! ! r2 2 r4 r4 r4 ! ! ! ! ! ! r r r r 3 r3 r3 2 2 3 ! ! ! ! ! ! r4 r4 r r r r ! ! 2 ! 3 3 ! 2 r3

(a) Zaks’ code in P4 (b) Williams’ code in P4

Resume: both approaches are based on independent cycles in Pn

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 8 / 18 Pancake graph: cycle structure

[A. Kanevsky, . Feng, On the embedding of cycles in pancake graphs (1995)] All cycles of length ℓ, where 6 6 ℓ 6 n! − 2, or ℓ = n!, can embedded in Pn.

[J.J. Sheu, J.J.M. Tan, K.T. Chu, Cycle embedding in pancake interconnection networks (2006)] All cycles of length ℓ, where 6 6 ℓ 6 n!, can embedded in Pn.

Cycles in Pn All cycles of length ℓ, where 6 6 ℓ 6 n!, can be embedded in the Pancake graph Pn, n > 3, but there are no cycles of length 3, 4 or 5.

Proofs are based on the hierarchical structure of Pn.

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 9 / 18 Pancake graphs: hierarchical structure

i i Pn consists of n copies of Pn−1(i) = (V , E ), 1 6 i 6 n, where the vertex set V i is presented by permutations with the fixed last element.

P1 P4

[1] [1234] r4 [4321] r3 r2 r2 r3 P2 [3214] [2341] [2134] [3421] r2 r2 r3 r3 r2 [12] [21] [3124] [2431] [2314] [3241] r3 r2 r2 r3 r4 P3 [1324] [4231] r4 r4 r4 r4 [3142] [2413] [123] r4 r r r r4 4 r3 r2 3 2 r2 r3 [4132] [1423] [321] [213] [1342] [4213] r2 r3 r3 r2 r2 r3 [4312] [1243] [231] [312] [1432] [4123] r2 r2 r3 r2 r3 r3 r4 [132] [3412] [2143]

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 10 / 18 Hamiltonicity due to the hierarchical structure of Pn ⇔ Prefix–reversal Gray codes (PRGC) by Zaks and Williams

Pn−1(n)

π2 n π1 L rn n−1 Pn−1(n − 1) rn π 3 π2n

n−1 Ln−1 1 Ln−1 π4

rn

π rn 5 Pn−1 n−2 (1) Ln−1 rn

Pn−1(n − 2)

Proposition 1. If there is a Gray code in Pn−1 then there is a Gray code in Pn given by the same algorithm.

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 11 / 18 Small independent even cycles and PRGC

Proposition 2. n! The Pancake graph Pn, n > 3, contains the maximal set of ℓ independent ℓ–cycles of the canonical form

k Cℓ = (rk rk−1) ,

where ℓ = 2 k, for any 3 6 k 6 n.

n Williams’ prefix–reversal Gray code: (rn rn−1) This code is based on the maximal set of independent 2n–cycles.

3 Zaks’ prefix–reversal Gray code: (r3 r2) This code is based on the maximal set of independent 6–cycles.

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 12 / 18 Independent cycles in Pn

Theorem 1. n! The Pancake graph Pn, n > 4, contains the maximal set of ℓ independent ℓ–cycles of the canonical form

k Cℓ = (rn rm) , (1)

where ℓ = 2 k, 2 6 m 6 n − 1 and n O(1) if m 6 ⌊ 2 ⌋;  n k =  O(n) if m > ⌊ 2 ⌋ and n ≡ 0 (mod n − m); (2) O(n2) else. 

Corollary The cycles presented in Theorem 1 have no chords.

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 13 / 18 Hamilton cycles based on small independent even cycles

Hamilton cycle ⇒ PRGC

Definition The Hamilton cycle Hn based on independent ℓ–cycles is called a Hamilton cycle in Pn, consisting of paths of lengths l = ℓ − 1 of independent cycles, connected together with external to these cycles edges.

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 14 / 18 Hamilton cycles based on small independent even cycles

Definition ′ The complementary cycle Hn to the Hamilton cycle Hn based on independent cycles is defined on unused edges of Hn and the same external edges.

′ H4 H4

! ! ! !

! ! ! ! ! ! ! !

! ! ! ! ! ! ! !

! ! ! !

! ! ! !

! ! ! ! ! ! ! !

! ! ! ! ! ! ! !

! ! ! !

′ (c) Hamilton cycle H4 in P4 (d) Complement cycle H4 to H4 in P4

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 15 / 18 Hamilton cycles based on small independent even cycles

Theorem 2. There are no other Hamilton cycles in Pn, n > 5, based on independent cycles from Theorem 1 when k = O(1) and k = O(n), except from Zaks and Williams constructions.

Proof is based on examining the complementary cycles’ structures.

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 16 / 18 n! Hamilton cycles based on independent 2 –cycles

Theorem 3. There are no Hamilton cycles in Pn, n > 4, based on independent n! 2 –cycles but there are Hamilton paths based on the following two independent cycles:

1 1 n Cn = ((Cn−1/rn−1)rn) , 2 2 n Cn = ((Cn−1/rn−1)rn) , 1 2 2 2 where C4 = (r3 r2 r4 r2 r3 r4) and C4 = (r2 r3 r4 r3 r2 r4) .

Proof is based on the hierarchical structure of Pn and on the nonexistence 4–cycles in Pn.

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 17 / 18 Thanks for attention!

Elena Konstantinova Prefix-reversal Gray codes Villanova–2014 18 / 18