Prefix-reversal Gray codes - Personal pages of the CEU

Prefix-reversal Gray codes
Alexey Medvedev
Central European University, Budapest, Hungary,
Sobolev Institute of Mathematics, Novosibirsk, Russia
joint work with
Elena Konstantinova, Sobolev Institute of Mathematics
Department of Mathematics and its Applications
CEU, 25.02.2015
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
1 / 31
Binary Reflected Gray code
Hamming cube Hn [F. Gray, (1953), U.S. Patent 2,632,058]
The first Gray code was introduced relative to binary strings
n = 2:
00 01 | 11 10
000 001 011 010 | 110 111 101 100
n = 3:
10
11
010
00
01
000
111
100
101
011
001
H3
H2
Alexey Medvedev (CEU, IM SBRAS)
110
Prefix-reversal Gray codes
1110
CEU–201511112 / 31
Gray codes are useful
The Gray codes are used in many applications in
mathematics;
computer science;
electrical engineering;
data communications;
etc.
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
3 / 31
Example: HDD
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
4 / 31
Example: spinning wheel
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
5 / 31
Example: spinning wheel
1
01
1
1
1
1
1
1
1
1
01
1
0
?10100
1
1
001
.
.
.
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
6 / 31
Example: spinning wheel
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
7 / 31
Example: spinning wheel
001
1
?1
1
1
001
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
8 / 31
Gray codes
Combinatorial Gray codes [J. Joichi et al., (1980)]
A combinatorial Gray code is now referred as a method of generating
combinatorial objects so that successive objects differ in some
pre-specified, usually small, way.
[D.E. Knuth, The Art of Computer Programming, Vol.4 (2010)]
Knuth recently surveyed combinatorial generation:
Gray codes are related to
efficient algorithms for exhaustively generating combinatorial objects.
(tuples, permutations, combinations, partitions, trees)
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
9 / 31
Example: generating permutations
Steinhaus-Johnson-Trotter algorithm, (1964)
List all the n! permutations, such that the successive permutations differ
by transposition of two adjacent elements.
[1234]
[1243]
[1423]
[4123]
[3124]
[3142]
[3412]
[4312]
[2314]
[2341]
[2431]
[4231]
[4132]
[1432]
[1342]
[1324]
[4321]
[3421]
[3241]
[3214]
[4213]
[2413]
[2143]
[2134]
Generating permutations in Sym4
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
10 / 31
Example: generating permutations
Steinhaus-Johnson-Trotter algorithm, (1964)
bc
2341
2431
2314
bc
bc
bc
bc
2143
bc
3214
2134
bc
bc
1234
bc
1324
bc
3241
bc
3124
bc
3142
bc
3421
bc
3412
2413
bc
bc
1243
1423
bc
4123
bc
bc
4213
bc
4231
bc
1342
1432
4132
bc
4123
bc
bc
4321
Figure: Hamilton cycle in Cay(Sym4 , {(1 2), (2 3), (3 4)}
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
11 / 31
Relation between codes and graphs
Define the graph Γ = (V, E), where V – the set of combinatorial objects
and (u, v) ∈ E iff u and v differ in ”pre-specified small way”. Then
the Hamilton path in Γ ∼ Gray code on V ;
the Hamilton cycle in Γ ∼ cyclic Gray code on V .
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
12 / 31
AntiExample: generating permutations
Symmetric group Symn [R. Eggleton, W. Wallis, (1985); D. Rall,
P. Slater, (1987)]
The group of permutations:
Q: Is it possible to list all permutations in a list so that each one differs
from its predecessor in every position?
A: YES!
[1234]
[4123]
[2341]
[3412]
[1324]
[4132]
[3241]
[2413]
[3124]
[4312]
[1243]
[2431]
[3214]
[4321]
[2143]
[1432]
[2314]
[4231]
[3142]
[1423]
[2134]
[4213]
[1342]
[3421]
Generating permutations in Sym4
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
13 / 31
Gray codes: generating permutations
[S. Zaks, (1984)]
Zaks’ algorithm:
each successive permutation is generated by reversing a suffix of the
preceding permutation.
Describe in terms of prefixes:
Start with In = [12 . . . n];
Let ζn be the sequence of sizes of these prefixes defined by recursively
as follows:
ζ2 = 2
ζn = (ζn−1 n)n−1 ζn−1 , n > 2,
where a sequence is written as a concatenation of its elements;
Flip prefixes according to the sequence.
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
14 / 31
Zaks’ algorithm: examples
If n = 2 then ζ2 = 2 and we have:
[12] [21]
If n = 3 then ζ3 = 23232 and we have:
[123] [312] [231]
[213] [132] [321]
If n = 4 then ζ4 = 23232423232423232423232 and we have:
[1234] [4123] [3412] [2341]
[2134] [1423] [4312] [3241]
[3124] [2413] [1342] [4231]
[1324] [4213] [3142] [2431]
[2314] [1243] [4132] [3421]
[3214] [2143] [1432] [4321]
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
15 / 31
Greedy Gray code: generating permutations
[A. Williams, J. Sawada, (2013)]
Describe in terms of prefixes:
Start with In = [12 . . . n];
Take the largest size prefix we can flip not repeating a created
permutation;
Flip this prefix.
Example: for n = 4 then we have
[1234] [4321] [2341] [1432] [3412] [2143] [4123] [3214]
[2314] [4132] [3142] [2413] [1423] [3241] [4231] [1324]
[3124] [4213] [1243] [3421] [2431] [1342] [4312] [2134]
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
16 / 31
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 , 2 6 i 6 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 ].
Cycles in Pn [A. Kanevsky, C. Feng, (1995); J.J. Sheu, J.J.M. Tan,
K.T. Chu, (2006)]
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.
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
17 / 31
Pancake graphs: hierarchical structure
Pn consists of n copies of Pn−1 (i) = (V i , E i ), 1 6 i 6 n, where the vertex
set V i is presented by permutations with the fixed last element.
P1
P4
[1]
P2
[4321]
r3
r2
r2
[3214]
[2341]
[2134]
r2
[12]
r4
[1234]
r3
r2
[21]
[3421]
r3
r2
r3
[3124]
[2431]
[2314]
r2
r3
P3
r4
r2
r3
[1324]
[3142]
r4
[123]
[321]
[213]
r2
r3
r3
r4
r2
[312]
r3
r2
[132]
Alexey Medvedev (CEU, IM SBRAS)
r2
r4
r4
r3
[4132]
[1423]
[1342]
r2
r3
r3
[1432]
r2
r3
[3412]
Prefix-reversal Gray codes
[4213]
r2
[1243]
[4312]
[231]
r3
[4231]
[2413]
r4
r4
[3241]
r2
r4
[4123]
r2
r4
r3
[2143]
CEU–2015
18 / 31
Two scenarios of generating permutations: Zaks | Williams
Both algorithms are based on independent cycles in Pn .
Zaks’ prefix–reversal Gray code:
Williams’ prefix–reversal Gray code:
(r2 r3 )3 – flip the minimum number
(rn rn−1 )n – flip the maximum
of topmost pancakes that gives a
number of topmost pancakes that
new stack.
gives a new stack.
r4
r2
r3
r2
r3
r2
r4
r2
r3
r3
r4
r4
r4
r4
r3
r4
r4
r2
r3
r3
r3
(a) Zaks’ code in P4
r2
r4
r2
r2
r2
Alexey Medvedev (CEU, IM SBRAS)
r4
r4
r2
r3
r3
r4
r3
r4
r2
r4
r2
r3
r3
r3
r2
r3
r4
r3
r4
r2
r2
r3
(b) Williams’ code in P4
Prefix-reversal Gray codes
CEU–2015
19 / 31
Independent cycles in Pn
Theorem 1. (K., M.)
The Pancake graph Pn , n > 4, contains the maximal set of
`–cycles of the canonical form
n!
`
independent
C` = (rn rm )k ,
where ` = 2 k, 2 6 m 6 n − 1 and

 O(1) if m 6 b n2 c;
O(n) if m > b n2 c and n ≡ 0
k=

O(n2 ) else.
(1)
(mod n − m);
(2)
Corollary
The cycles presented in Theorem 1 have no chords.
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
20 / 31
Hamilton cycles based on small independent even cycles
Hamilton cycle or path in Pn ⇒ 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.
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
21 / 31
Hamilton cycles based on small independent even cycles
Definition
The fastening cycle Hn0 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
Alexey Medvedev (CEU, IM SBRAS)
(d) Fastening cycle H40 in P4
Prefix-reversal Gray codes
CEU–2015
22 / 31
Hamilton cycles based on the independent cycles in P4
Theorem
In the Pancake graph P4 there are only four Hamilton cycles based on the
maximal set independent cycles.
Proof. The collection of all possible maximal sets of independent cycles of
the same form in P4 is presented below by the following table:
6–cycles
8–cycles
3
C6 = (r3 r2 )
Alexey Medvedev (CEU, IM SBRAS)
C81
C82
12–cycles
4
= (r4 r2 )
= (r4 r3 )4
Prefix-reversal Gray codes
1
C12
= (r2 r3 r4 r3 r2 r4 )2
2
C12 = (r3 r2 r4 r2 r3 r4 )2
CEU–2015
23 / 31
Hamilton cycles based on the independent cycles in P4
Theorem
In the Pancake graph P4 there are only four Hamilton cycles based on the
maximal set independent cycles.
Proof. All possible cases of Hamilton cycles based on the independent
cycles in P4 are presented in the table below:
H4i
H41
H42
H43
H44
2
4
= ((r2 r3 ) r2 r4 )
= ((r3 r2 )2 r3 r4 )4
= ((r4 r3 )3 r4 r2 )3
= ((r4 r2 )3 r4 r3 )3
Alexey Medvedev (CEU, IM SBRAS)
H4i
H41
H42
H43
H44
= (r4 r3 )4
= (r4 r2 )4
= (r3 r2 )3
= (r2 r3 )3
Prefix-reversal Gray codes
Description
Zaks’ Hamiltonian cycle;
based on independent cycles C6 ;
Williams’ Hamiltonian cycle;
based on independent cycles C8 .
CEU–2015
24 / 31
Hamilton cycles based on the independent cycles in P4
Theorem
In the Pancake graph P4 there are only four Hamilton cycles based on the
maximal set independent cycles.
bc
r3
r2
bc
r2
r4
bc
r2
bc
r4
r3
bc
r4
r3
bc
r2
bc
bc
r2
r4
r3
bc
r2
bc
bc
bc
r2
bc
r4
bc
r2
bc
r4
bc
r2
bc
r4
r3
(e) Hamiltonian cycle (H42 , H42 ) in P4
Alexey Medvedev (CEU, IM SBRAS)
r3
bc
bc
r2
r3
r2
r2
bc
r2
bc
r2
bc
bc
bc
r4
r2
r4
r3
bc
r4
r3
bc
r3
bc
bc
bc
r4
bc
bc
r4
r2
r3
bc
bc
r4
bc
r2
bc
r4
r2
bc
bc
r2
r2
r2
bc
bc
r2
bc
r4
bc
r3
bc
bc
r3
bc
r3
r3
bc
r2
bc
bc
r2
bc
r4
(f) Hamiltonian cycle (H44 , H44 ) in P4
Prefix-reversal Gray codes
CEU–2015
25 / 31
Non-existence of Hamilton cycles
Suppose the fastening cycle Hn0 has form (rm rj )t , where m ∈ {2, . . . , n},
rj ∈ PR\{rm }.
Theorem 2. (K., M.)
The only Hamilton cycles Hn based on independent cycles from Theorem 1
with the fastening cycle Hn0 of form (rm rj )t , where m ∈ {2, . . . , n}, are
Zaks’, Greedy and Hamilton cycle based on (r4 r2 )4 in P4 .
Proof. Hn0 = (rm rj )t ⇒ Hn0 has form from Theorem 1. Thus, the
following inequality should hold
2
n!
Lmax
6 Lmax ,
(3)
where Lmax is the maximal length of cycles from Theorem 1.
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
26 / 31
Non-existence of Hamilton cycles
The length Lmax can be estimated as
Lmax 6 n(n + 2),
and therefore
2n! 6 L2max ,
1
n! 6 n2 (n + 2)2 .
2
The inequality does not hold starting from n = 7. For n from 4 to 6 it is
easy to verify using the exact lengths that inequality holds only for n = 4.
2
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
27 / 31
Non-existence of Hamilton cycles
Suppose the fastening cycle Hn0 has form Hn0 = (rm rξ )t , where by rξ we
mean that every second reversal may be different from previous.
Another way of thinking of it is to treat rξ as a random variable taking
values in P R\{rn , rm } with some distribution.
Theorem 3. (K., M.)
The only Hamilton cycles Hn based on independent cycles from
Theorem 1 with the fastening cycle Hn0 of form (rm rξ )t , where
m 6= {n, n − 2} and rξ ∈ PR\{rn , rm } is Greedy Hamilton cycle in Pn .
Proof is based on structural properties of the graph, hierarchical structure
and length’s argument above.
Remark. Existence in the case m = n − 2 is only unresolved when
` = O(n).
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
28 / 31
Hamilton cycles based on small independent even cycles
Open problem
Suppose the fastening cycle Hn0 has form Hn0 = (rη rξ )t , where
rη ∈ {rn , rm } and rξ ∈ P R\{rn , rm }.
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
29 / 31
PRGC: hierarchical construction
Hierarchical construction
Suppose we know a bunch of Hamilton cycle constructions in graph Pn−1 .
Then the PRGC can be constructed using the fastening 2n–path passing
through all copies of Pn−1 in Pn exactly once.
Example:
Pn−1 (n)
π2
Zaks’ construction:
rn
Pn−1(n − 1)
Lnn−1
π1
rn
π3
π2n
Ln−1
n−1
L1n−1
Hn01 = (rn rn−1 )n
π4
rn
rn
π5
Ln−2
n−1
Pn−1(1)
rn
Pn−1(n − 2)
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
30 / 31
Thank you for your attention!
Alexey Medvedev (CEU, IM SBRAS)
Prefix-reversal Gray codes
CEU–2015
31 / 31