This paper was converted on www.awesomepapers.org from LaTeX by an anonymous user.
Want to know more? Visit the Converter page.

Inside the Binary Reflected Gray Code:
Flip-Swap Languages in 2-Gray Code Order

Joe Sawada      Aaron Williams      Dennis Wong
Abstract

A flip-swap language is a set 𝐒{\bf S} of binary strings of length nn such that 𝐒{0n}{\bf S}\cup\{0^{n}\} is closed under two operations (when applicable): (1) Flip the leftmost 11; and (2) Swap the leftmost 11 with the bit to its right. Flip-swap languages model many combinatorial objects including necklaces, Lyndon words, prefix normal words, left factors of kk-ary Dyck words, and feasible solutions to 0-11 knapsack problems. We prove that any flip-swap language forms a cyclic 22-Gray code when listed in binary reflected Gray code (BRGC) order. Furthermore, a generic successor rule computes the next string when provided with a membership tester. The rule generates each string in the aforementioned flip-swap languages in O(n)O(n)-amortized per string, except for prefix normal words of length nn which require O(n1.864)O(n^{1.864})-amortized per string. Our work generalizes results on necklaces and Lyndon words by Vajnovski [Inf. Process. Lett. 106(3):96-99, 2008].

1 Introduction

Combinatorial generation studies the efficient generation of each instance of a combinatorial object, such as the n!n! permutations of {1,2,,n}\{1,2,\ldots,n\} or the 1n+1(2nn)\frac{1}{n+1}\binom{2n}{n} binary trees with nn nodes. The research area is fundamental to computer science and it has been covered by textbooks such as Combinatorial Algorithms for Computers and Calculators by Nijenhuis and Wilf [27], Concrete Mathematics: A Foundation for Computer Science by Graham, Knuth, and Patashnik [9], and The Art of Computer Programming, Volume 4A, Combinatorial Algorithms by Knuth [12]. In fact, Knuth’s section on Generating Basic Combinatorial Patterns is over 450 pages. The subject is important to every day programmers, and Arndt’s Matters Computational: Ideas, Algorithms, Source Code is an excellent practical resource [1]. A primary consideration is listing the instances of a combinatorial object so that consecutive instances differ by a specified closeness condition. Lists of this type are called Gray codes. This terminology is due to the eponymous binary reflected Gray code (BRGC) by Frank Gray, which orders the 2n2^{n} binary strings of length nn so that consecutive strings differ in one bit. The BRGC was patented for a pulse code communication system in 1953 [10]. For example, the order for n=4n=4 is

0000,1000,1100,0100,0110,1110,1010,0010,\displaystyle 0000,1000,1100,0100,0110,1110,1010,0010, (1)
0011,1011,1111,0111,0101,1101,1001,0001.\displaystyle 0011,1011,1111,0111,0101,1101,1001,0001.

Variations that reverse the entire order or the individual strings are also commonly used in practice and in the literature. We note that the order in (1) is cyclic because the last and first strings also differ by the closeness condition, and this property holds for all nn.

One challenge facing combinatorial generation is its relative surplus of breadth and lack of depth111This is not to say that combinatorial generation is always easy. For example, the ‘middle levels‘ conjecture was confirmed by Mütze [14] after 30 years and effort by hundreds of researchers.. For example, [1], [12], and [27] have separate subsections for different combinatorial objects, and the majority of the Gray codes are developed from first principles. Thus, it is important to encourage simple frameworks that can be applied to a variety of combinatorial objects. Previous work in this direction includes the following:

  1. 1.

    the ECO framework developed by Bacchelli, Barcucci, Grazzini, and Pergola [2] that generates Gray codes for a variety of combinatorial objects such as Dyck words in constant amortized time per instance;

  2. 2.

    the twisted lexico computation tree by Takaoka [22] that generates Gray codes for multiple combinatorial objects in constant amortized time per instance;

  3. 3.

    loopless algorithms developed by Walsh [25] to generate Gray codes for multiple combinatorial objects, which extend algorithms initially given by Ehrlich in [8];

  4. 4.

    greedy algorithms observed by Williams [28] that provide a uniform understanding for many previous published results;

  5. 5.

    the reflectable language framework by Li and Sawada [13] for generating Gray codes of kk-ary strings, restricted growth strings, and kk-ary trees with nn nodes;

  6. 6.

    the bubble language framework developed by Ruskey, Sawada and Williams [17] that provides algorithms to generate shift Gray codes for fixed-weight necklaces and Lyndon words, kk-ary Dyck words, and representations of interval graphs;

  7. 7.

    the permutation language framework developed by Hartung, Hoang, Mütze and Williams [11] that provides algorithms to generate Gray codes for a variety of combinatorial objects based on encoding them as permutations.

We focus on an approach that is arguably simpler than all of the above: Start with a known Gray code and then filter or induce the list based on a subset of interest. In other words, the subset is listed in the relative order given by a larger Gray code, and the resulting order is a sublist (Gray code) with respect to it. Historically, the first sublist Gray code appears to be the revolving door Gray code for combinations [26]. A combination is a length nn binary string with weight (i.e. number of ones) kk. The Gray code is created by filtering the BRGC, as shown below for n=4n=4 and k=2k=2 (cf. (1))

0000,1000,1100,0100,0110,1110,1010,0010,\displaystyle\cancel{0000},\cancel{1000},1100,\cancel{0100},0110,\cancel{1110},1010,\cancel{0010}, (2)
0011,1011,1111,0111,0101,1101,1001,0001.\displaystyle 0011,\cancel{1011},\cancel{1111},\cancel{0111},0101,\cancel{1101},1001,\cancel{0001}.

This order is a transposition Gray code as consecutive strings differ by transposing two bits222When each string is viewed as the incidence vector of a kk-subset of {1,2,,n}\{1,2,\ldots,n\}, then consecutive kk-subsets change via a “revolving door” (i.e. one value enters and one value exits).. It can be generated directly (i.e. without filtering) by an efficient algorithm [26]. Transposition Gray codes are a special case of 2-Gray codes where consecutive strings differ by flipping (i.e. complementing) at most two bits. Vajnovszki [23] proved that necklaces and Lyndon words form a cyclic 22-Gray code in BRGC order, and efficient algorithms can generate these sublist Gray codes directly [21]. Our goal is to expand upon the known languages that are 2-Gray codes in BRGC order, and which can be efficiently generated. To do this, we introduce a new class of languages.

A flip-swap language (with respect to 1) is a set 𝐒{\bf S} of length nn binary strings such that 𝐒{0n}{\bf S}\cup\{0^{n}\} is closed under two operations (when applicable): (1) Flip the leftmost 11; and (2) Swap the leftmost 11 with the bit to its right. A flip-swap language with respect to 0 is defined similarly. Flip-swap languages encode a wide variety of combinatorial objects.

Theorem 1.1.

The following sets of length nn binary strings are flip-swap languages:

Flip-Swap languages (with respect to 11) i. all strings ii. strings with weight k\leq k iii. strings γ\leq\gamma iv. strings with k\leq k inversions re: 010^{*}1^{*} v. strings with k\leq k transpositions re: 010^{*}1^{*} vi. strings << their reversal vii. strings \leq their reversal (neckties) viii. strings << their complemented reversal ix. strings \leq their complemented reversal x. strings with forbidden 10t10^{t} xi. strings with forbidden prefix 1γ1\gamma xii. 0-prefix normal words xiii. necklaces (smallest rotation) xiv. Lyndon words xv. prenecklaces (smallest rotation) xvi. pseudo-necklaces with respect to 010^{*}1^{*} xvii. left factors of kk-ary Dyck words xviii. feasible solutions to 0-1 knapsack problems Flip-Swap languages (with respect to 0) \triangleright all strings \triangleright strings with weight k\geq k \triangleright strings γ\geq\gamma \triangleright strings with k\leq k inversions re: 101^{*}0^{*} \triangleright strings with k\leq k transpositions re: 101^{*}0^{*} \triangleright strings >> their reversal \triangleright strings \geq their reversal \triangleright strings >> their complemented reversal \triangleright strings \geq their complemented reversal \triangleright strings with forbidden 01t01^{t} \triangleright strings with forbidden prefix 0γ0\gamma \triangleright 11-prefix normal words \triangleright necklaces (largest rotation) \triangleright aperiodic necklaces (largest rotation) \triangleright prenecklaces (largest rotation) \triangleright pseudo-necklaces with respect to 101^{*}0^{*}

Our second result is that every flip-swap language forms a cyclic 22-Gray code when listed in BRGC order. This generalizes the previous sublist BRGC results [21, 23].

Theorem 1.2.

When a flip-swap language 𝐒\mathbf{S} is listed in BRGC order the resulting listing is a 2-Gray code. If 𝐒\mathbf{S} includes 0n0^{n} then the listing is cyclic.

Our third result is a generic successor rule, which efficiently computes the next string in the 22-Gray code of a flip-swap language, so long as a fast membership test is given.

Theorem 1.3.

The languages in Theorem 1.1 can be generated in O(n)O(n)-amortized time per string, with the exception of prefix normal words which require O(n1.864)O(n^{1.864})-time.

In Section 2, we formally define our version of the BRGC. In Section 3, we prove Theorem 1.1, and define the flip-swap partially ordered set. In Section 4, we give our generic successor rule and prove Theorem 1.2. In Section 5, we present a generic generation algorithm that list out each string of a flip-swap language, and we prove Theorem 1.3.

2 The Binary Reflected Gray Code

Let 𝐁(n)\mathbf{B}(n) denote the set of length nn binary strings. Let BRGC(n)BRGC(n) denote the listing of 𝐁(n)\mathbf{B}(n) in BRGC order. Let BRGC¯(n)\overline{BRGC}(n) denote the listing BRGC(n)BRGC(n) in reverse order. Then BRGC(n)BRGC(n) can be defined recursively as follows, where x\mathcal{L}\cdot x denotes the listing \mathcal{L} with the character xx appended to the end of each string:

BRGC(n)={0,1if n=1;BRGC(n1)0,BRGC¯(n1)1if n>1.BRGC(n)=\begin{cases}0,1&\text{if $n=1$};\\ BRGC(n-1)\cdot 0,\ \overline{BRGC}(n-1)\cdot 1&\text{if $n>1$.}\end{cases}

For example, BRGC(2)=00,10,11,01BRGC(2)=00,10,11,01 and BRGC¯(2)=01,11,10,00\overline{BRGC}(2)=01,11,10,00, thus

BRGC(3)=00𝟎,10𝟎,11𝟎,01𝟎,01𝟏,11𝟏,10𝟏,00𝟏BRGC(3)=00{\bf 0},10{\bf 0},11{\bf 0},01{\bf 0},01{\bf 1},11{\bf 1},10{\bf 1},00{\bf 1}.

This definition of BRGC order is the same as the one used by Vajnovzski [23]. When the strings are read from right-to-left, we obtain the classic definition of BRGC order [10]. For flip-swap languages with respect to 0, we interchange the roles of the 0s and 1s; however, for our discussions we will focus on flip-swap languages with respect to 1. Table 1 illustrates BRGC(4)BRGC(4) and six flip-swap languages listed in Theorem 1.1.

n=4n=4  all necklaces 0-PNW 1001\leq 1001 k2k\leq 2 neckties
BRGC i. xiii. xii. iii. ii. vii.
0000
1000
1100
0100
0110
1110
1010
0010
0011
1011
1111
0111
0101
1101
1001
0001
(a) String membership in 6 flip-swap languages.
i. xiii. xii. iii. ii. vii.
 

Refer to caption

 

Refer to caption

 

Refer to caption

 

Refer to caption

 

Refer to caption

 

Refer to caption

(b) Visualizating the 2-Gray codes in (a).
Table 1: Flip-swap languages ordered as sublists of the binary reflected Gray code. Theorem 1.1 covers each language, so the resulting orders are 2-Gray codes.

3 Flip-swap languages

In this section, we formalize some of the non-obvious flip-swap languages stated in Theorem 1.1. Then we prove Theorem 1.1 for a subset of the listed languages including necklaces, prefix normal words, and feasible solutions to the 0-1 knapsack problems. The remainder of the languages are proved in the Appendix.

Consider a binary string α=b1b2bn\alpha=b_{1}b_{2}\cdots b_{n}. The weight of α\alpha is the number of 1s it contains. An inversion in α\alpha with respect to 010^{*}1^{*} is an index pair (i,j)(i,j) such that i<ji<j and bi=1b_{i}=1 and bj=0b_{j}=0. The number of transpositions of α\alpha with respect to another binary string β\beta of length nn is the minimum number of adjacent transpositions required to transform α\alpha to β\beta.

A necklace is the lexicographically smallest (largest) string in an equivalence class under rotation. An aperiodic necklace is a necklace that cannot be written in the form βj\beta^{j} for some j<nj<n. A Lyndon word is an aperiodic necklace when using the lexicographically smallest string as the representative. A prenecklace is a prefix of a necklace. A block with respect to 010^{*}1^{*} is a maximal substring of the form 010^{*}1^{*}. A string α=b1b2bn=BbBb1B1\alpha=b_{1}b_{2}\cdots b_{n}=B_{b}B_{b-1}\cdots B_{1} is a pseudo-necklace with respect to 010^{*}1^{*} if BbBiB_{b}\leq B_{i} for all 1i<b1\leq i<b.

A kk-ary Dyck word is a binary string of length n=tkn=tk with tt copies of 11 and t(k1)t(k-1) copies of 0 such that every prefix has at most k1k-1 copies of 0 for every 11. The set of length nn prefixes of kk-ary Dyck words is called left factors of kk-ary Dyck words.

Let flipα(i)flip_{\alpha}(i) be the string obtained by complementing bib_{i}. Let swapα(i,j)swap_{\alpha}(i,j) be the string obtained by swapping bib_{i} and bjb_{j}. When the context is clear we use flip(i)flip(i) and swap(i,j)swap(i,j) instead of flipα(i)flip_{\alpha}(i) and swapα(i,j)swap_{\alpha}(i,j). Also, let 0(α)\ell_{0}(\alpha) denote the position of the leftmost 0 of α\alpha or n+1n+1 if no such position exists. Similarly, let 1(α)\ell_{1}(\alpha) denote the position of the leftmost 11 of α\alpha or n+1n+1 if no such position exists. We now prove that binary strings, necklaces, prefix normal words, and feasible solutions to the 0-1 knapsack problems are flip-swap languages with respect to 1.

Binary strings: Obviously the set 𝐁(n){\bf B}(n) satisfies the two closure properties of a flip-swap language and thus is a flip-swap language. In fact, the BRGC order induces a cyclic 11-Gray code for 𝐁(n){\bf B}(n) [12, 15].

Necklaces: Let 𝐍(n){\bf N}(n) be the set of necklaces of length nn and α=0j1bj+2bj+3bn\alpha=0^{j}1b_{j+2}b_{j+3}\cdots b_{n} be a necklace in 𝐍(n){\bf N}(n). By the definition of necklace, it is easy to see that flipα(α)=0j+1bj+2bj+3bn𝐍(n)flip_{\alpha}(\ell_{\alpha})=0^{j+1}b_{j+2}b_{j+3}\cdots b_{n}\in{\bf N}(n) and thus 𝐍(n){\bf N}(n) satisfies the flip-first property. For the swap-first operation, observe that if α0n11\alpha\neq 0^{n-1}1 and bj+2=1b_{j+2}=1, then the swap-first operation produces the same necklace. Otherwise if α0n11\alpha\neq 0^{n-1}1 and bj+2=0b_{j+2}=0, then the swap-first operation produces the string 0j+11bj+3bj+4bn0^{j+1}1b_{j+3}b_{j+4}\cdots b_{n} which is clearly a necklace. Thus, the set of necklaces is a flip-swap language.

Prefix normal words: A binary string α\alpha is prefix normal with respect to 0 (also known as 0-prefix normal word) if no substring of α\alpha has more 0s than its prefix of the same length. For example, the string 001010010111011 is a 0-prefix normal word but the string 001010010011011 is not because it has a substring of length 55 with four 0s while the prefix of length 55 has only three 0s.

Observe that the set of 0-prefix normal words of length nn satisfies the two closure properties of a flip-swap language as the flip-first and swap-first operations either increases or maintain the number of 0s in its prefix. Thus, the set of 0-prefix normal words of length nn is a flip-swap language.

Feasible solutions to 0-11 knapsack problems: The input to a 0-11 knapsack problem is a knapsack capacity WW, and a set of nn items each of which has a non-negative weight wi0w_{i}\geq 0 and a value viv_{i}. A subset of items is feasible if the total weight of the items in the subset is less than or equal to the capacity WW. Typically, the goal of the problem is to find a feasible subset with the maximum value, or to decide if a feasible subset exists with value c\geq c.

Given the input to a 0-11 knapsack problem, we reorder the items by non-decreasing weight. That is, wiwi+1w_{i}\geq w_{i+1} for 1in11\leq i\leq n-1. Notice that the incidence vectors of feasible subsets are now a flip-swap language. More specifically, flipping any 11 to 0 causes the subset sum to decrease, and so does swapping any 11 with the bit to its right. Hence, the language satisfies the flip-first and the swap-first closure properties and is a flip-swap language.

3.1 Flip-Swap poset

In this section we introduce a poset whose ideals correspond to a flip-swap language which includes the string 0n0^{n}.

Let α=b1b2bn\alpha=b_{1}b_{2}\cdots b_{n} be a length nn binary string. We define τ(α)\tau(\alpha) as follows:

τ(α)=\displaystyle\tau(\alpha)= α\displaystyle\alpha if α=0n\alpha=0^{n},
τ(α)=\displaystyle\tau(\alpha)= flipα(α)\displaystyle flip_{\alpha}(\ell_{\alpha}) if α0n\alpha\neq 0^{n} and (α=n\ell_{\alpha}=n or bα+1=1b_{\ell_{\alpha}+1}=1)        (flip-first),
τ(α)=\displaystyle\tau(\alpha)= swapα(α,α+1)\displaystyle swap_{\alpha}(\ell_{\alpha},\ell_{\alpha}+1) otherwise (swap-first).

Let τt(α)\tau^{t}(\alpha) denote the string that results from applying the τ\tau operation tt times to α\alpha. We define the binary relation <R<_{R} on 𝐁(n){\bf B}(n) to be the transitive closure of the cover relation τ\tau, that is β<Rα\beta<_{R}\alpha if βα\beta\neq\alpha and β=τt(α)\beta=\tau^{t}(\alpha) for some t>0t>0. It is easy to see that the binary relation <R<_{R} is irreflexive, anti-symmetric and transitive. Thus <R<_{R} is a strict partial order. The relation <R<_{R} on binary strings defines our flip-swap poset.

Definition 1.

The flip-swap poset 𝒫(n)\mathcal{P}(n) is a strict poset with 𝐁(n){\bf B}(n) as the ground set and <R<_{R} as the strict partial order.

Figure 2 shows the Hasse diagram of 𝒫(4)\mathcal{P}(4) with the ideal for binary strings of length 44 that are lexicographically smaller or equal to 10011001 in bold. Observe that 𝒫(n)\mathcal{P}(n) is always a tree with 0n0^{n} as the unique minimum element, and that its ideals are the subtrees that contain this minimum.

Lemma 1.

A set 𝐒{\bf S} over 𝐁(n){\bf B}(n) that includes 0n0^{n} is a flip-swap language if and only if 𝐒{\bf S} is an ideal of 𝒫(n)\mathcal{P}(n).

Proof.

Let 𝐒{\bf S} be a flip-swap language over 𝐁(n){\bf B}(n) and α\alpha be a string in 𝐒{\bf S}. Since 𝐒{\bf S} is a flip-swap language, 𝐒{\bf S} satisfies the flip-first and swap-first properties and thus τ(α)\tau(\alpha) is a string in 𝐒{\bf S}. Therefore every string γ<Rα\gamma<_{R}\alpha is in 𝐒{\bf S} and hence 𝐒{\bf S} is an ideal of 𝒫(n)\mathcal{P}(n). The other direction is similar. ∎

If S is a set of binary strings and γ\gamma is a binary string, then the quotient of S and γ\gamma is 𝐒/γ={α|αγ𝐒}{\bf S}/\gamma=\{\alpha\ |\ \alpha\gamma\in{\bf S}\}.

Refer to caption
(a) The flip-swap poset 𝒫(4)\mathcal{P}(4).
Refer to caption
(b) An ideal of 𝒫(4)\mathcal{P}(4).
Figure 2: Flip-swap languages are the ideals of the flip-swap poset. The ideal in (b) contains the 44-bit binary strings that are 1001\leq 1001 with respect to lexicographic order.
Lemma 2.

If 𝐒1{\bf S}_{1} and 𝐒2{\bf S}_{2} are flip-swap languages and γ\gamma is a binary string, then 𝐒1𝐒2{\bf S}_{1}\cap{\bf S}_{2}, 𝐒1𝐒2{\bf S}_{1}\cup{\bf S}_{2} and 𝐒1/γ{\bf S}_{1}/\gamma are flip-swap languages.

Proof.

Let 𝐒1{\bf S}_{1} and 𝐒2{\bf S}_{2} be two flip-swap languages and let γ\gamma be a binary string. The intersection and union of ideals of any poset are also ideals of that poset, so 𝐒1𝐒2{\bf S}_{1}\cap{\bf S}_{2} and 𝐒1𝐒2{\bf S}_{1}\cup{\bf S}_{2} are flip-swap languages. Now consider α𝐒1/γ\alpha\in{\bf S}_{1}/\gamma.

Suppose α𝐒1/γ\alpha\in{\bf S}_{1}/\gamma for some non-empty γ\gamma where j=|α|j=|\alpha|. This means that αγ𝐒1\alpha\gamma\in{\bf S}_{1}. Consider three cases depending αγ\ell_{\alpha\gamma}. If αγ<j\ell_{\alpha\gamma}<j, then clearly τ(αγ)=τ(α)γ\tau(\alpha\gamma)=\tau(\alpha)\gamma. From Lemma 1, τ(α)γ𝐒1\tau(\alpha)\gamma\in{\bf S}_{1} and thus τ(α)𝐒1/γ\tau(\alpha)\in{\bf S}_{1}/\gamma. If αγ=j\ell_{\alpha\gamma}=j, then α=0j11\alpha=0^{j-1}1 and τ(α)=0j\tau(\alpha)=0^{j}. Since 𝐒1{\bf S}_{1} is a flip-swap language 0jγ𝐒10^{j}\gamma\in{\bf S}_{1}. Again this implies that τ(α)𝐒1/γ\tau(\alpha)\in{\bf S}_{1}/\gamma. If αγ>j\ell_{\alpha\gamma}>j then α=0j\alpha=0^{j} and τ(α)=α\tau(\alpha)=\alpha in this case. For each case we have shown that τ(α)𝐒1/γ\tau(\alpha)\in{\bf S}_{1}/\gamma and thus 𝐒1/γ{\bf S}_{1}/\gamma is a flip-swap language by Lemma 1. ∎

Corollary 1.

Flip-swap languages are closed under union, intersection, and quotient.

Proof.

Let 𝐒A{\bf S}_{A} and 𝐒B{\bf S}_{B} be flip-swap languages and γ\gamma be a binary string. Since 𝐒A{\bf S}_{A} and 𝐒B{\bf S}_{B} can be represented by ideals of the flip-swap poset, possibly excluding 0n0^{n}, by Lemma 2 the sets 𝐒A𝐒B{\bf S}_{A}\cap{\bf S}_{B}, 𝐒A𝐒B{\bf S}_{A}\cup{\bf S}_{B} and 𝐒A/γ{\bf S}_{A}/\gamma are flip-swap languages. ∎

Lemma 3.

If αγ\alpha\gamma is a binary string in a flip-swap language 𝐒{\bf S}, then 0|α|γ𝐒0^{|\alpha|}\gamma\in{\bf S}.

Proof.

This result follows from the flip-first property of flip-swap languages. ∎

4 A generic successor rule for flip-swap languages

Consider any flip-swap language 𝐒{\bf S} that includes the string 0n0^{n}. Let 𝒢𝒞(𝐒)\mathcal{BRGC}({\bf S}) denote the listing of 𝐒\bf S in BRGC order. Given a string α𝐒\alpha\in\mathbf{S}, we define a generic successor rule that computes the string following α\alpha in the cyclic listing 𝒢𝒞(𝐒)\mathcal{BRGC}({\bf S}).

Let α=b1b2bn\alpha=b_{1}b_{2}\cdots b_{n} be a string in 𝐒{\bf S}. Let tαt_{\alpha} be the leftmost position such that flipα(tα)𝐒flip_{\alpha}(t_{\alpha})\in{\bf S} when |𝐒|>1|{\bf S}|>1, such a tαt_{\alpha} exists since 𝐒{\bf S} satisfies the flip-first property and |𝐒|>1|{\bf S}|>1. Recall that α\ell_{\alpha} is defined to be the position of the leftmost 11 of α\alpha (or |α|+1|\alpha|+1 if no such position exists). Notice that tααt_{\alpha}\leq\ell_{\alpha} when |𝐒|>1|{\bf S}|>1 since 𝐒{\bf S} is a flip-swap language.

Necklaces Parity of w(α)w(\alpha) tαt_{\alpha} α\ell_{\alpha} Successor Case
000000 even 66 flip2(5,6)flip2(5,6) (4c)
000011 even 33 flip2(2,3)flip2(2,3) (4c)
011011 even 22 flip(2)flip(2) (4b)
001011 odd 33 flip(4)flip(4) (4e)
001111 even 22 flip2(1,2)flip2(1,2) (4c)
111111 even 11 flip(1)flip(1) (4b)
011111 odd 22 flip(3)flip(3) (4e)
010111 even 33 flip(2)flip(2) (4b)
000111 odd 44 flip(5)flip(5) (4e)
000101 even 22 flip(2)flip(2) (4b)
010101 odd 22 flip2(2,3)flip2(2,3) (4d)
001101 odd 33 flip(4)flip(4) (4e)
001001 even 33 flip(3)flip(3) (4b)
000001 odd flip(6)flip(6) (4a)
Table 2: The necklaces of length 6 induced by successive applications the function ff starting from 000000000000. The sixth column of the table lists out the corresponding rules in ff that apply to each necklace to obtain the next necklace.

Let flip2α(i,j)flip2_{\alpha}(i,j) be the string obtained by complementing both bib_{i} and bjb_{j}. When the context is clear we use flip2(i,j)flip2(i,j) instead of flip2α(i,j)flip2_{\alpha}(i,j). Also, let w(α)w(\alpha) denote the number of 11s of α\alpha. We claim that the following function ff computes the next string in the cyclic ordering 𝒢𝒞(𝐒)\mathcal{BRGC}({\bf S}):

f(α)=\displaystyle f(\alpha)= 0n\displaystyle 0^{n} if α=0n11\alpha=0^{n-1}1; (4a)
f(α)=\displaystyle f(\alpha)= flipα(tα)\displaystyle flip_{\alpha}(t_{\alpha}) if w(α)w(\alpha) is even and (tα=1t_{\alpha}=1 or flip2α(tα1,tα)𝐒flip2_{\alpha}(t_{\alpha}-1,t_{\alpha})\notin{\bf S}); (4b)
f(α)=\displaystyle f(\alpha)= flip2α(tα1,tα)\displaystyle flip2_{\alpha}(t_{\alpha}-1,t_{\alpha}) if w(α)w(\alpha) is even and flip2α(tα1,tα)𝐒flip2_{\alpha}(t_{\alpha}-1,t_{\alpha})\in{\bf S}; (4c)
f(α)=\displaystyle f(\alpha)= flip2α(α,α+1)\displaystyle flip2_{\alpha}(\ell_{\alpha},\ell_{\alpha}+1) if w(α)w(\alpha) is odd and flipα(α+1)𝐒flip_{\alpha}(\ell_{\alpha}+1)\notin{\bf S}; (4d)
f(α)=\displaystyle f(\alpha)= flipα(α+1)\displaystyle flip_{\alpha}(\ell_{\alpha}+1) if w(α)w(\alpha) is odd and flipα(α+1)𝐒flip_{\alpha}(\ell_{\alpha}+1)\in{\bf S}. (4e)

Thus, successive applications of the function ff on a flip-swap language 𝐒{\bf S}, starting with the string 0n0^{n}, list out each string in 𝐒{\bf S} in BRGC order. As an illustration of the function ff, successive applications of this rule for the set of necklaces of length 66 starting with the necklace 000000000000 produce the listing in Table 2.

Theorem 4.1 ().

If 𝐒{\bf S} is a flip-swap language including the string 0n0^{n} and |𝐒|>1|{\bf S}|>1, then f(α)f(\alpha) is the string immediately following the string α\alpha in 𝐒{\bf S} in the cyclic ordering 𝒢𝒞(𝐒)\mathcal{BRGC}({\bf S}).

We will provide a detailed proof of this theorem in the next subsection. Observe that each rule in ff complements at most two bits and thus successive strings in 𝐒{\bf S} differ by at most two bit positions. Observe that when 0n0^{n} is excluded from 𝐒{\bf S}, then 𝒢𝒞(𝐒)\mathcal{BRGC}({\bf S}) is still a 22-Gray code (although not necessarily cyclic). This proves Theorem 1.2.

4.1 Proof of Theorem 4.1

This section proves Theorem 4.1. We begin with a lemma by Vajnovszki [23], and a remark that is due to the fact that 0n110^{n-1}1 is in a flip-swap language 𝐒{\bf S} when |𝐒|>1|{\bf S}|>1.

Lemma 4.

Let α=b1b2bn\alpha=b_{1}b_{2}\cdots b_{n} and β\beta be length nn binary strings such that αβ\alpha\neq\beta. Let rr be the rightmost position in which α\alpha and β\beta differ. Then α\alpha comes before β\beta in BRGC order (denoted by αβ\alpha\prec\beta) if and only if w(brbr+1bn)w(b_{r}b_{r+1}\cdots b_{n}) is even.

Remark 1.

A flip-swap language 𝐒{\bf S} in BRGC order ends with 0n110^{n-1}1 when |𝐒|>1|{\bf S}|>1.

Let succ(𝐒,α)succ({\bf S},\alpha) be the successor of α\alpha in 𝐒{\bf S} in BRGC order (i.e. the string after α\alpha in the cyclic ordering 𝒢𝒞(𝐒)\mathcal{BRGC}({\bf S})). Next we provide two lemmas, and then prove Theorem 4.1.

Lemma 5.

Let 𝐒{\bf S} be a flip-swap language with |𝐒|>1|{\bf S}|>1 and α\alpha be a string in 𝐒{\bf S}. Let tαt_{\alpha} be the leftmost position such that flipα(tα)𝐒flip_{\alpha}(t_{\alpha})\in{\bf S}. If w(α)w(\alpha) is even, then tαt_{\alpha} is the rightmost position in which α\alpha and succ(𝐒,α)succ({\bf S},\alpha) differ.

Proof.

By contradiction. Let α=b1b2bn\alpha=b_{1}b_{2}\cdots b_{n} and β=succ(𝐒,α)\beta=succ({\bf S},\alpha). Let rr be the rightmost position in which α\alpha and β\beta differ with rtαr\neq t_{\alpha}. If tα>rt_{\alpha}>r, then β\beta has the suffix 1br+1br+2bn1b_{r+1}b_{r+2}\cdots b_{n} since br=0b_{r}=0 because r<tααr<t_{\alpha}\leq\ell_{\alpha}. Thus by the flip-first property, 0r11br+1br+2=flipα(r)𝐒0^{r-1}1b_{r+1}b_{r+2}=flip_{\alpha}(r)\in{\bf S} and r<tαr<t_{\alpha}, a contradiction.

Otherwise if tα<rt_{\alpha}<r, then let γ=flipα(tα)\gamma=flip_{\alpha}(t_{\alpha}). Clearly γα\gamma\neq\alpha. Now observe that w(btbt+1bn)w(b_{t}b_{t+1}\cdots b_{n}) is even because tααt_{\alpha}\leq\ell_{\alpha} and w(α)w(\alpha) is even, and thus by Lemma 4, αγ\alpha\prec\gamma. Also, γ\gamma has the suffix brbr+1bnb_{r}b_{r+1}\cdots b_{n} and w(brbr+1bn)w(b_{r}b_{r+1}\cdots b_{n}) is even because αβ\alpha\prec\beta and rr is the rightmost position α\alpha and β\beta differ, and thus also by Lemma 4, γβ\gamma\prec\beta. Thus αγβ\alpha\prec\gamma\prec\beta, a contradiction. Therefore r=tαr=t_{\alpha}. ∎

Lemma 6.

Let 𝐒{\bf S} be a flip-swap language with |𝐒|>1|{\bf S}|>1 and α0n11\alpha\neq 0^{n-1}1 be a string in 𝐒{\bf S}. If w(α)w(\alpha) is odd, then α+1\ell_{\alpha}+1 is the rightmost position in which α\alpha and succ(𝐒,α)succ({\bf S},\alpha) differ.

Proof.

Since α0n11\alpha\neq 0^{n-1}1 and w(α)w(\alpha) is odd, α<n1\ell_{\alpha}<n-1. We now prove the lemma by contradiction. Let α=b1b2bn\alpha=b_{1}b_{2}\cdots b_{n} and β=succ(𝐒,α)\beta=succ({\bf S},\alpha). Let rα+1r\neq\ell_{\alpha}+1 be the rightmost position in which α\alpha and β\beta differ. If r<α+1r<\ell_{\alpha}+1, then w(brbr+1bn)w(b_{r}b_{r+1}\cdots b_{n}) is odd but αβ\alpha\prec\beta, a contradiction by Lemma 4. Otherwise if r>α+1r>\ell_{\alpha}+1, then let γ=flip2α(α,α+1)\gamma=flip2_{\alpha}(\ell_{\alpha},\ell_{\alpha}+1). Clearly γα\gamma\neq\alpha, and by the flip-first and swap-first properties, γ𝐒\gamma\in{\bf S}. Also, observe that w(bα+1bα+2bn)w(b_{\ell_{\alpha}+1}b_{\ell_{\alpha}+2}\cdots b_{n}) is even because w(α)w(\alpha) is odd, and thus by Lemma 4, αγ\alpha\prec\gamma. Further, γ\gamma has the suffix brbr+1bnb_{r}b_{r+1}\cdots b_{n} and w(brbr+1bn)w(b_{r}b_{r+1}\cdots b_{n}) is even because αβ\alpha\prec\beta and rr is the rightmost position α\alpha and β\beta differ, and thus also by Lemma 4, γβ\gamma\prec\beta. Thus αγβ\alpha\prec\gamma\prec\beta, a contradiction. Therefore r=α+1r=\ell_{\alpha}+1. ∎

Proof of Theorem 4.1.

Let α=a1a2an\alpha=a_{1}a_{2}\cdots a_{n} and β=succ(𝐒,α)=b1b2bn\beta=succ({\bf S},\alpha)=b_{1}b_{2}\cdots b_{n}. Let tαt_{\alpha} be the leftmost position such that flipα(tα)𝐒flip_{\alpha}(t_{\alpha})\in{\bf S}. First we consider the case when α=0n11\alpha=0^{n-1}1. Recall that the first string in 𝐁(n){\bf B}(n) in BRGC order is 0n0^{n} [15] and 0n0^{n} is a string in 𝐒{\bf S} by Lemma 3. Also, the last string in 𝐒{\bf S} in BRGC order is 0n110^{n-1}1 by Remark 1 when |𝐒|>1|{\bf S}|>1. Thus the string that appears immediately after α\alpha in the cyclic ordering 𝒢𝒞(𝐒)\mathcal{BRGC}({\bf S}) is f(α)f(\alpha) when α=0n11\alpha=0^{n-1}1. In the remainder of the proof, α0n11\alpha\neq 0^{n-1}1 and we consider the following two cases.

Case 1:

w(α)w(\alpha) is even: If tα=1t_{\alpha}=1, then clearly β=flipα(tα)=f(α)\beta=flip_{\alpha}(t_{\alpha})=f(\alpha). For the remainder of the proof, tα>1t_{\alpha}>1.

Since tααt_{\alpha}\leq\ell_{\alpha}, flip2α(tα1,tα)flip2_{\alpha}(t_{\alpha}-1,t_{\alpha}) has the prefix 0tα210^{t_{\alpha}-2}1. We now consider the following two cases. If flip2α(tα1,tα)𝐒flip2_{\alpha}(t_{\alpha}-1,t_{\alpha})\notin{\bf S}, then flipα(tα)flip_{\alpha}(t_{\alpha}) is the only string in 𝐒{\bf S} that has tαt_{\alpha} as the rightmost position that differ with α\alpha and has the prefix 0t20^{t-2}. Therefore, β=flipα(tα)=f(α)\beta=flip_{\alpha}(t_{\alpha})=f(\alpha). Otherwise, flip2α(tα1,tα)flip2_{\alpha}(t_{\alpha}-1,t_{\alpha}) and flipα(tα)flip_{\alpha}(t_{\alpha}) are the only strings in 𝐒{\bf S} that have tαt_{\alpha} as the rightmost position that differ with α\alpha and have the prefix 0tα20^{t_{\alpha}-2}. By Lemma 4, flip2α(tα1,tα)flipα(tα)flip2_{\alpha}(t_{\alpha}-1,t_{\alpha})\prec flip_{\alpha}(t_{\alpha}) since w(1a¯tαatα+1atα+2an)w(1\overline{a}_{t_{\alpha}}a_{t_{\alpha}+1}a_{t_{\alpha}+2}\cdots a_{n}) is even. Thus, β=flip2α(tα1,tα)=f(α)\beta=flip2_{\alpha}(t_{\alpha}-1,t_{\alpha})=f(\alpha).

Case 2:

w(α)w(\alpha) is odd: By Lemma 6, β\beta has the suffix a¯α+1aα+2aα+3an\overline{a}_{\ell_{\alpha}+1}a_{\ell_{\alpha}+2}a_{\ell_{\alpha}+3}\cdots a_{n}. If flipα(α+1)𝐒flip_{\alpha}(\ell_{\alpha}+1)\notin{\bf S}, then by the flip-first and swap-first properties, flip2α(α,α+1)flip2_{\alpha}(\ell_{\alpha},\ell_{\alpha}+1) is the only string in 𝐒{\bf S} that has α+1\ell_{\alpha}+1 as the rightmost position that differ with β\beta. Thus, β=flip2α(α,α+1)=f(α)\beta=flip2_{\alpha}(\ell_{\alpha},\ell_{\alpha}+1)=f(\alpha). Otherwise by Lemma 4, any string γ𝐒\gamma\in{\bf S} with the suffix a¯α+1aα+2aα+3an\overline{a}_{\ell_{\alpha}+1}a_{\ell_{\alpha}+2}a_{\ell_{\alpha}+3}\cdots a_{n} and γflipα(α+1)\gamma\neq flip_{\alpha}(\ell_{\alpha}+1) has flipα(α+1)γflip_{\alpha}(\ell_{\alpha}+1)\prec\gamma because w(1a¯α+1aα+2aα+3an)w(1\overline{a}_{\ell_{\alpha}+1}a_{\ell_{\alpha}+2}a_{\ell_{\alpha}+3}\cdots a_{n}) is even. Thus, β=flipα(α+1)=f(α)\beta=flip_{\alpha}(\ell_{\alpha}+1)=f(\alpha).

Therefore, the string immediately after α\alpha in the cyclic ordering 𝒢𝒞(𝐒)\mathcal{BRGC}({\bf S}) is f(α)f(\alpha). ∎

5 Generation algorithm for flip-swap languages

In this section we present a generic algorithm to generate 22-Gray codes for flip-swap languages via the function ff.

A naïve approach to implement ff is to find tαt_{\alpha} by test flipping each bit in α\alpha to see if the result is also in the set when w(α)w(\alpha) is even; or test flipping the (α+1\ell_{\alpha}+1)-th bit of α\alpha to see if the result is also in the set when w(α)w(\alpha) is odd. Since tααt_{\alpha}\leq\ell_{\alpha}, we only need to examine the length α1\ell_{\alpha}-1 prefix of α\alpha to find tαt_{\alpha}. Such a test can be done in O(nm)O(nm) time, where O(m)O(m) is the time required to complete the membership test of the set under consideration. Pseudocode of the function ff is given in Algorithm 1.

To list out each string of a flip-swap language 𝐒{\bf S} in BRGC order, we can repeatedly apply the function ff until it reaches the starting string. We also maintain w(α)w(\alpha) and α\ell_{\alpha} which can be easily maintained in O(n)O(n) time for each string generated. We also add a condition to avoid printing the string 0n0^{n} if 0n0^{n} is not a string in 𝐒{\bf S}. Pseudocode for this algorithm, starting with the string 0n0^{n}, is given in Algorithm 2. The algorithm can easily be modified to generate the corresponding counterpart of 𝐒{\bf S} with respect to 0.

A simple analysis shows that the algorithm generates 𝐒{\bf S} in O(nm)O(nm)-time per string. A more thorough analysis improves this to O(n+m)O(n+m)-amortized time per string.

Theorem 5.1.

If 𝐒{\bf S} is a flip-swap language, then the algorithm BRGC produces 𝒢𝒞(S)\mathcal{BRGC}(S) in O(n+m)O(n+m)-amortized time per string, where O(m)O(m) is the time required to complete the membership tester for 𝐒{\bf S}.

Proof.

Let α=a1a2an\alpha=a_{1}a_{2}\cdots a_{n} be a string in 𝐒{\bf S}. Clearly ff can be computed in O(n)O(n) time when w(α)w(\alpha) is odd. Otherwise when w(α)w(\alpha) is even, the while loop in line 5 of Algorithm 1 performs a membership tester on each string β=b1b2bn\beta=b_{1}b_{2}\cdots b_{n} in 𝐒{\bf S} with bαbα+1bn=aαaα+1anb_{\ell_{\alpha}}b_{\ell_{\alpha}+1}\cdots b_{n}=a_{\ell_{\alpha}}a_{\ell_{\alpha}+1}\cdots a_{n} and w(b1b2bα1)=1w(b_{1}b_{2}\cdots b_{\ell_{\alpha}-1})=1. Observe that each of these strings can only be examined by the membership tester once, or otherwise the while loop in line 5 of Algorithm 1 produces the same tαt_{\alpha} which results in a duplicated string, a contradiction. Thus, the total number of membership testers performed by the algorithm is bound by |𝐒||{\bf S}|, and therefore ff runs in O(m)O(m)-amortized time per string. Finally, since the other part of the algorithm runs in O(n)O(n) time per string, the algorithm BRGC runs in O(n+m)O(n+m)-amortized time per string. ∎

The membership tests in this paper can be implemented in O(n)O(n) time and O(n)O(n) space; see [3, 7, 20] for necklaces, Lyndon words, prenecklaces and pseudo-necklaces of length nn. One exception is the test for prefix normal words of length nn, which requires O(n1.864)O(n^{1.864}) time and O(n)O(n) space [5]. Together with the above theorem, this proves Theorem 1.3.

Visit the Combinatorial Object Server [6] for a C implmentation of our algorithms.

Algorithm 1 Pseudocode of the implementation of the function ff.
1:
2:function ff(α\alpha)
3:    if α=0n11\alpha=0^{n-1}1 then  flipα(n)flip_{\alpha}(n)
4:    else if w(α)w(\alpha) is even then
5:        tααt_{\alpha}\leftarrow\ell_{\alpha}
6:        while tα>1t_{\alpha}>1 and flipα(tα1)𝐒flip_{\alpha}(t_{\alpha}-1)\in{\bf S} do  tαtα1t_{\alpha}\leftarrow t_{\alpha}-1         
7:        if tα1t_{\alpha}\neq 1 and flip2α(tα1,tα)𝐒flip2_{\alpha}(t_{\alpha}-1,t_{\alpha})\in{\bf S} then  αflip2α(tα1,tα)\alpha\leftarrow flip2_{\alpha}(t_{\alpha}-1,t_{\alpha})
8:        else αflipα(tα)\alpha\leftarrow flip_{\alpha}(t_{\alpha})         
9:    else
10:        if flipα(α+1)𝐒flip_{\alpha}(\ell_{\alpha}+1)\notin{\bf S} then  αflip2α(α,α+1)\alpha\leftarrow flip2_{\alpha}(\ell_{\alpha},\ell_{\alpha}+1)
11:        else αflipα(α+1)\alpha\leftarrow flip_{\alpha}(\ell_{\alpha}+1)             
Algorithm 2 Algorithm to list out each string of a flip-swap language 𝐒{\bf S} in BRGC order.
1:
2:procedure BRGC
3:    α=b1b2bn0n\alpha=b_{1}b_{2}\cdots b_{n}\leftarrow 0^{n}
4:    do
5:        if α0n\alpha\neq 0^{n} or 0n𝐒0^{n}\in{\bf S} then  Print(α)(\alpha)         
6:        f(α)f(\alpha)
7:        w(α)0w(\alpha)\leftarrow 0
8:        for ii from nn down to 11 do
9:           if bi=1b_{i}=1 then  w(α)w(α)+1w(\alpha)\leftarrow w(\alpha)+1            
10:           if bi=1b_{i}=1 then  αi\ell_{\alpha}\leftarrow i                    
11:    while α0n\alpha\neq 0^{n}

References

  • [1] J. Arndt. Matters Computational: Ideas, Algorithms, Source Code. Springer, 2011.
  • [2] S. Bacchelli, E. Barcucci, E. Grazzini, and E. Pergola. Exhaustive generation of combinatorial objects by ECO. Acta Informatica, 40(8):585–602, 2004.
  • [3] K. S. Booth. Lexicographically least circular substrings. Inf. Process. Lett., 10(4/5):240–242, 1980.
  • [4] B. Bultena and F. Ruskey. An Eades-McKay algorithm for well-formed parenthesis strings. Inf. Process. Lett., 68(5):255–259, 1998.
  • [5] T. M. Chan and M. Lewenstein. Clustered integer 3SUM via additive combinatorics. In Proceedings of the Forty-seventh Annual ACM Symposium on Theory of Computing, STOC 15, pages 31–40, New York, NY, USA, 2015.
  • [6] COS++. The Combinatorial Object Server. http://combos.org/brgc.
  • [7] J. P. Duval. Factorizing words over an ordered alphabet. J. Algorithms, 4(4):363–381, 1983.
  • [8] G. Ehrlich. Loopless algorithms for generating permutations, combinations, and other combinatorial configurations. J. ACM, 20(3):500–513, 1973.
  • [9] R. Graham, D. Knuth, and O. Patashnik. Concrete Mathematics: A Foundation for Computer Science. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA, 1994.
  • [10] F. Gray. Pulse code communication, 1953. US Patent 2,632,058.
  • [11] E. Hartung, H. P. Hoang, T. Mütze, and A. Williams. Combinatorial generation via permutation languages. In Proceedings of the Thirty-First Annual ACM-SIAM Symposium on Discrete Algorithms, SODA ’20, page 1214–1225, USA, 2020.
  • [12] D. Knuth. The Art of Computer Programming. Volume 4, fascicule 2., Generating all tuples and permutations. The Art of Computer Programming. Addison-Wesley, Upper Saddle River (N.J.), 2005. Autre tirage : 2010.
  • [13] Y. Li and J. Sawada. Gray codes for reflectable languages. Inf. Process. Lett., 109(5):296 – 300, 2009.
  • [14] T. Mütze. Proof of the middle levels conjecture. Proceedings of the London Mathematical Society, 112:677––713, 2016.
  • [15] F. Ruskey. Combinatorial Generation. Working version (1j-CSC 425/520) edition, 2003.
  • [16] F. Ruskey and A. Proskurowski. Generating binary trees by transpositions. J. Algorithms, 11(1):68 – 84, 1990.
  • [17] F. Ruskey, J. Sawada, and A. Williams. Binary bubble languages and cool-lex order. J. Comb. Theory Ser. A, 119(1):155 – 169, 2012.
  • [18] F. Ruskey and A. Williams. Generating balanced parentheses and binary trees by prefix shifts. In Proceedings of the Fourteenth Symposium on Computing: The Australasian Theory - Volume 77, CATS ’08, pages 107–115, Darlinghurst, Australia, 2008.
  • [19] C. Savage. A survey of combinatorial Gray codes. SIAM Rev., 39(4):605–629, 1997.
  • [20] J. Sawada and A. Williams. A Gray code for fixed-density necklaces and Lyndon words in constant amortized time. Theor. Comput. Sci., 502:46 – 54, 2013.
  • [21] J. Sawada, A. Williams, and D. Wong. Necklaces and Lyndon words in colexicographic and reflected Gray code order. J. Discrete Algorithms, 46-47:25–35, 2017.
  • [22] T. Takaoka. An O(1)O(1) time algorithm for generating multiset permutations. In Algorithms and Computation, 10th International Symposium, ISAAC ’99, Chennai, India, December 16-18, 1999, Proceedings, volume 1741 of Lecture Notes in Computer Science, pages 237–246. Springer, 1999.
  • [23] V. Vajnovszki. More restrictive Gray codes for necklaces and Lyndon words. Inf. Process. Lett., 106(3):96–99, 2008.
  • [24] V. Vajnovszki and T. Walsh. A loop-free two-close Gray-code algorithm for listing kk-ary Dyck words. J. Discrete Algorithms, 4(4):633–648, 2006.
  • [25] T. Walsh. Generating Gray codes in O(1)O(1) worst-case time per word. In Discrete Mathematics and Theoretical Computer Science, 4th International Conference, DMTCS 2003, Dijon, France, July 7-12, 2003. Proceedings, volume 2731 of LNCS, pages 73–88. Springer, 2003.
  • [26] H. S. Wilf. A unified setting for sequencing, ranking, and selection algorithms for combinatorial objects. Adv. Math., 24:281–291, 1977.
  • [27] H. S. Wilf and A. Nijenhuis. Combinatorial Algorithms: For Computers and Calculators. Academic Press, 2nd edition, 1978.
  • [28] A. Williams. The greedy Gray code algorithm. In 13th International Symposium, WADS 2013, London, ON, Canada, August 12-14, 2013. Proceedings, pages 525–536, 2013.

Appendix: Proofs for flip-swap languages

This section provides the proofs for the remainder of the languages in Theorem 1.1. Unless otherwise stated, the discussion of flip-swap languages are with respect to 1.

Binary strings with weight k\leq k

Recall the weight of a binary string is the number of 1s it contains. Let 𝐒{\bf S} be the set of binary strings of length nn having weight less than or equal to some kk. Observe that 𝐒{\bf S} satisfies the two closure properties of a flip-swap language as the flip-first and swap-first operations either decrease or maintain the weight. Thus, 𝐒{\bf S} is a flip-swap language.

Binary strings γ\leq\gamma

Let 𝐒{\bf S} be the set of binary strings of length nn with each string lexicographically smaller or equal to some string γ\gamma. Observe that 𝐒{\bf S} satisfies the two closure properties of a flip-swap language as the flip-first and swap-first operations either make the resulting string lexicographically smaller or produce the same string. Thus, 𝐒{\bf S} is a flip-swap language.

Binary strings with k\leq k inversions

Recall that an inversion with respect to 010^{*}1^{*} in a binary string α=b1b2bn\alpha=b_{1}b_{2}\cdots b_{n} is any bi=1b_{i}=1 and bj=0b_{j}=0 such that i<ji<j. For example when α=100101\alpha=100101, it has 44 inversions: (b1,b2),(b1,b3),(b1,b5),(b4,b5)(b_{1},b_{2}),(b_{1},b_{3}),(b_{1},b_{5}),(b_{4},b_{5}). Let 𝐒{\bf S} be the set of binary strings of length nn with less than or equal to kk inversions with respect to 010^{*}1^{*}. Observe that 𝐒{\bf S} satisfies the two closure properties of a flip-swap language as the flip-first and swap-first operations either decrease or maintain the number of inversions. Thus, 𝐒{\bf S} is a flip-swap language.

Binary strings with k\leq k transpositions

Recall that the number of transpositions of a binary string α=b1b1bn\alpha=b_{1}b_{1}\cdots b_{n} with respect to 010^{*}1^{*} is the minimum number of swap(i,j)swap(i,j) operations required to change α\alpha into the form 010^{*}1^{*}. For example, the number of transpositions of the string 100101100101 is 11. Let 𝐒{\bf S} be the set of binary strings of length nn with less than or equal to kk transpositions with respect to 010^{*}1^{*}. Observe that 𝐒{\bf S} satisfies the two closure properties of a flip-swap language as the flip-first and swap-first operations either decrease or maintain the number of transpositions. Thus, 𝐒{\bf S} is a flip-swap language.

Binary strings <or<\text{or}\leq their reversal

Let 𝐒{\bf S} be the set of binary strings of length nn with each string lexicographically smaller than their reversal. Observe that 𝐒{\bf S} satisfies the swap-first property as the swap-first operation either produces the same string, or makes the resulting sting lexicographically smaller while its reversal lexicographically larger. Furthermore, 𝐒{0n}{\bf S}\cup\{0^{n}\} satisfies the flip-first property as the flip-first operation complements the most significant bit of α\alpha but the least significant bit of its reversal when w(α)>1w(\alpha)>1; or otherwise produces the string 0n0^{n} when w(α)=1w(\alpha)=1. Thus, 𝐒{\bf S} is a flip-swap language. The proof for the set of binary strings of length nn with each string lexicographically smaller than or equal to their reversal is similar to the proof for 𝐒{\bf S}.

Equivalence class of strings under reversal has also been called neckties [19].

Binary strings <or<\text{or}\leq their complemented reversal

Let 𝐒{\bf S} be the set of binary strings of length nn with each string lexicographically smaller than (or equal to) its complemented reversal. Observe that 𝐒{\bf S} satisfies the flip-first property as the flip-first operation makes the resulting string lexicographically smaller while its complemented reversal lexicographically larger. Furthermore, 𝐒{\bf S} satisfies the swap-first property as the swap-first operation either produces the same string, or complements the most significant bit of α\alpha and also a 11 of its complemented reversal. Thus, the resulting string must also be less than its complemented reversal. Thus, 𝐒{\bf S} is a flip-swap language.

Binary strings with forbidden 10t10^{t}

Let 𝐒{\bf S} be the set of binary strings of length nn without the substring 10t10^{t}. Observe that 𝐒{\bf S} satisfies the two closure properties of a flip-swap language as the flip-first and swap-first operations do not create the substring 10t10^{t}. Thus, 𝐒{\bf S} is a flip-swap language.

Binary strings with forbidden prefix 1γ1\gamma

Let 𝐒{\bf S} be the set of binary strings of length nn without the prefix 1γ1\gamma. Observe that 𝐒{\bf S} satisfies the two closure properties of a flip-swap language as the flip-first and swap-first operations either create a string with the prefix 0 or produce the same string. Thus, 𝐒{\bf S} is a flip-swap language.

Lyndon words

Let 𝐋(n){\bf L}(n) denote the set of Lyndon words of length nn. Since 𝐍(n){\bf N}(n) is a flip-swap language and 𝐋(n){0n}𝐍(n){\bf L}(n)\cup\{0^{n}\}\subseteq{\bf N}(n), it suffices to show that applying the flip-first or the swap-first operation on a Lyndon word either yields an aperiodic string or the string 0n0^{n}.

Clearly 𝐋(n){0n}{\bf L}(n)\cup\{0^{n}\} satisfies the two closure properties of a flip-swap language when α{0n,0n11}\alpha\in\{0^{n},0^{n-1}1\}. Thus in the remaining of the proof, α{0n,0n11}\alpha\notin\{0^{n},0^{n-1}1\}. We first prove by contradiction that 𝐋(n){0n}{\bf L}(n)\cup\{0^{n}\} satisfies the flip-first closure property. Let α=0j1bj+2bj+3bn\alpha=0^{j}1b_{j+2}b_{j+3}\cdots b_{n} be a string in 𝐋(n){0n}{\bf L}(n)\cup\{0^{n}\}. Suppose that 𝐋(n){0n}{\bf L}(n)\cup\{0^{n}\} does not satisfy the flip-first closure property and flipα(α)flip_{\alpha}(\ell_{\alpha}) is periodic. Thus flipα(α)=(0j+1β)tflip_{\alpha}(\ell_{\alpha})=(0^{j+1}\beta)^{t} for some string β\beta and t2t\geq 2. Observe that α=0j1β(0j+1β)t1\alpha=0^{j}1\beta(0^{j+1}\beta)^{t-1} which is clearly not a Lyndon word, a contradiction. Therefore 𝐋(n){0n}{\bf L}(n)\cup\{0^{n}\} satisfies the flip-first closure property.

Then similarly we prove by contradiction that 𝐋(n){0n}{\bf L}(n)\cup\{0^{n}\} satisfies the swap-first property. If bj+2=1b_{j+2}=1, then applying the swap-first operation on α\alpha produces the same Lyndon word. Thus in the remaining of the proof, bj+2=0b_{j+2}=0. Suppose that 𝐋(n){0n}{\bf L}(n)\cup\{0^{n}\} does not satisfy the swap-first closure property such that α𝐋(n){0n}\alpha\in{\bf L}(n)\cup\{0^{n}\} but swapα(α,α+1)swap_{\alpha}(\ell_{\alpha},\ell_{\alpha}+1) is periodic. Thus swapα(α,α+1)=(0j+11β)tswap_{\alpha}(\ell_{\alpha},\ell_{\alpha}+1)=(0^{j+1}1\beta)^{t} for some string β\beta and t2t\geq 2. Thus α\alpha contains the prefix 0j10^{j}1 but also the substring 0j+110^{j+1}1 in its suffix which is clearly not a Lyndon word, a contradiction. Thus, 𝐋(n){\bf L}(n) is a flip-swap language.

In [23], Vajnovszki proved that the BRGC order induces a cyclic 22-Gray code for the set of Lyndon words of length nn.

Prenecklaces

Recall that a string α\alpha is a prenecklace if it is a prefix of some necklace. In Section 3 we prove that applying the flip-first or the swap-first operation on a necklace yields a necklace. Thus by the definition of prenecklace, applying the flip-first or the swap-first operation on a prenecklace also creates a string that is a prefix of a necklace. Thus, the set of prenecklaces of length nn is a flip-swap language.

Pseudo-necklaces

Recall that a block with respect to 010^{*}1^{*} is a maximal substring of the form 010^{*}1^{*}. Each block BiB_{i} with respect to 010^{*}1^{*} can be represented by two integers (si,ti)(s_{i},t_{i}) corresponding to the number of 0s and 11s respectively. For example, the string α=000110100011001\alpha=000110100011001 can be represented by B4B3B2B1=(3,2)(1,1)(3,2)(2,1)B_{4}B_{3}B_{2}B_{1}=(3,2)(1,1)(3,2)(2,1). A block Bi=(si,ti)B_{i}=(s_{i},t_{i}) is said to be lexicographically smaller than a block Bj=(sj,tj)B_{j}=(s_{j},t_{j}) (denoted by Bi<BjB_{i}<B_{j}) if si<sjs_{i}<s_{j} or si=sjs_{i}=s_{j} with ti<tjt_{i}<t_{j}.

A string α=b1b2bn=BbBb1B1\alpha=b_{1}b_{2}\cdots b_{n}=B_{b}B_{b-1}\cdots B_{1} is a pseudo-necklace with respect to 010^{*}1^{*} if BbBiB_{b}\leq B_{i} for all 1i<b1\leq i<b. Observe that the set of pseudo-necklaces of length nn satisfies the two closure properties of a flip-swap language as the flip-first and swap-first operations do not make the first block BbB_{b} lexicographically larger, while the remaining blocks either remain the same or become lexicographically larger. Thus, the set of pseudo-necklaces of length nn is a flip-swap language.

In [21], the authors proved that the BRGC order induces a cyclic 22-Gray code for the set of pseudo-necklaces of length nn.

Left factors of kk-ary Dyck words

Recall that a kk-ary Dyck word is a binary string of length n=tkn=tk with tt copies of 11 and t(k1)t(k-1) copies of 0 such that every prefix has at most k1k-1 copies of 0 for every 11. It is well-known that kk-ary Dyck words are in one-to-one correspondence with kk-ary trees with tt internal nodes. When k=2k=2, Dyck words are counted by the Catalan numbers and are equivalent to balanced parentheses. As an example, 110100110100 is a 22-ary Dyck word and is also a balanced parentheses string while 100110100110 is not a 22-ary Dyck word nor a balanced parentheses because its prefix of length three contains more 0s than 11s. kk-ary Dyck words and balanced parentheses strings are well studied and have lots of applications including trees and stack-sortable permutations [4, 16, 18, 24].

The set of kk-ary Dyck words of length nn is not a flip-swap language with respect to 0 since 110100110100 is a 22-ary Dyck word but 111100111100 is not. The set of length nn prefixes of kk-ary Dyck words is, however, a flip-swap language with respect to 0. This set is also called left factors of kk-ary Dyck words. Let 𝐒{\bf S} be the set of left factors of kk-ary Dyck words. Observe that 𝐒{\bf S} satisfies the two closure properties of a flip-swap language with respect to 0 as the flip-first and swap-first operations do not increase the number 0s in the prefix. Thus, 𝐒{\bf S} is a flip-swap language with respect to 0.