Abstract 1 Introduction 2 Generating a cyclic 2-Gray code for Fibonacci words 3 Generating a cyclic 2-Gray code for Lucas words 4 A dynamic programming approach to generate Lucas words 5 Final remarks References Appendix A Python code to generate Fibonacci words that start with a 0, Fibonacci words and Lucas words in cyclic 2-Gray code order Appendix B Generating a cyclic 2-Gray code for Fibonacci words Appendix C Proofs of Lemmas 10-15

Generating a Cyclic 2-Gray Code for Lucas Words in Constant Amortized Time

Bowie Liu ORCID Faculty of Applied Sciences, Macao Polytechnic University, Macao Dennis Wong 111Corresponding author ORCID Faculty of Applied Sciences, Macao Polytechnic University, Macao Chan-Tong Lam ORCID Faculty of Applied Sciences, Macao Polytechnic University, Macao Sio-Kei Im ORCID Macao Polytechnic University, Macao
Abstract

We present a two-stage algorithm to generate a cyclic 2-Gray code for Lucas words. The first step involves a simple recursive algorithm that generates a cyclic 2-Gray code for Fibonacci words starting with a 0, which are strings that avoid p consecutive ones starting with a 0. Then, by considering the first and last blocks of 1s and concatenating lists of Fibonacci words starting with a 0 of different length n, we construct the first cyclic 2-Gray code for Lucas words. By using a dynamic programming approach, our algorithm generates each Lucas word and Fibonacci word in constant amortized time per string, using O⁢(n3) space. The algorithm can also be modified to produce the first efficient algorithm for generating q-decreasing sequences in constant amortized time per string, also using O⁢(n3) space. Our work extends a previous result on generating a cyclic 2-Gray code for q-decreasing sequences [Conference proceeding: International Conference and Workshop on Algorithms and Computation (WALCOM), LNCS 14549:91-102, 2024].

Keywords and phrases:
Lucas word, Fibonacci word, Fibonacci sequence, q-decreasing sequence, Gray code, CAT algorithm
Copyright and License:
[Uncaptioned image] © Bowie Liu, Dennis Wong, Chan-Tong Lam, and Sio-Kei Im; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Mathematics of computing → Combinatorial algorithms
Acknowledgements:
The second author would also like to thank his parents for their invaluable assistance in helping him to move house while he was busy drafting this paper.
Funding:
This research is supported by the Macao Science and Technology Development Fund FDCT/0033/2023/RIA1.
Editors:
Paola Bonizzoni and Veli Mäkinen

1 Introduction

A Lucas word of order p and length n is a binary string of length n that is not equal to 1n, avoids p consecutive ones, and has neither a 1ℓ prefix nor a 1m suffix such that ℓ+m≥p. In other words, a Lucas word is a binary string of length n that is not equal to 1n and avoids p consecutive ones when the string is considered cyclically. As an example, the 11 Lucas words of order p=3 and length n=4 are

0000, 0001, 0010, 0011, 0100, 0101, 0110, 1000, 1001, 1010, 1100.

The number of Lucas words, also known as Lucas numbers, is an interesting topic in combinatorics due to its close relationship with the famous Fibonacci sequence. The sequence was first discovered by Edouard Lucas, who found such a sequence when investigating Fibonacci number patterns. For example, when p=2, the initial six terms of the enumeration of Lucas words are 1, 3, 4, 7, 11, 18. The Lucas numbers Ln,p of order p=2 can be defined recursively as follows:

Ln,2={1if n=1;3if n=2;Ln,2=Ln−1,2+Ln−2,2for n>2. (1)

This recursive definition is analogous to the Fibonacci sequence, with the exception of different initial terms. Interestingly, when p>2, the number of Lucas words corresponds to multi-step Lucas numbers, which are similar to multi-step Fibonacci numbers but with different initial terms. For example, when p=3, the first six terms of the enumeration of Lucas words are 1, 3, 7, 11, 21, 39, and 71. These terms can be computed similarly to the famous tribonacci numbers with the initial terms L1,3=1, L2,3=3, and L3,3=7, and the recurrence formula Ln,3=Ln−1,3+Ln−2,3+Ln−3,3. Similarly when p=4, the number of Lucas words can be computed recursively in the same manner as the tetranacci number but with different initial terms, and this property holds for all integers n and p. For more information about multi-step Lucas numbers and multi-step Fibonacci sequences, see [12, 13, 24, 27]. The enumeration sequences for Lucas words can be accessed on the Online Encyclopedia of Integer Sequences for various values of p [28].

The study of Lucas words and their variations has attracted significant attention from mathematicians [2, 9, 10, 19]. One intriguing variant that has attracted considerable interest is q-decreasing words, which are binary strings in which every maximal factor of the form 0a⁢1b satisfies the condition where a=0 or q⁢a>b [3, 20]. Another interesting variant is the concept of Fibonacci words, which are binary strings of length n that avoid p consecutive ones. Interestingly, there exists a bijection between the set of Fibonacci words and the set of q-decreasing sequences [3]. Moreover, our algorithm for generating Lucas words relies on our algorithm for generating Fibonacci words. In a more general context, Lucas words can be considered as a special case of binary strings of length n that avoid a specific factor, which finds numerous applications in cryptography and coding theory. For more applications of Lucas words and other relatives, see [11, 14, 17, 22, 26, 29, 30, 33].

One of the most important aspects of combinatorial generation is to list the instances of a combinatorial object so that consecutive instances differ by a specified closeness condition involving a constant amount of change. 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 2n binary strings of length n so that consecutive strings differ in one bit. For example, when n=5 the order is

00000,00001,00011,00010,00110,00111,00101,00100,

01100,01101,01111,01110,01010,01011,01001,01000,

11000,11001,11011,11010,11110,11111,11101,11100,

10100,10101,10111,10110,10010,10011,10001,10000.

The BRGC listing is a 1-Gray code in which consecutive strings differ by one bit change. We note that the order is also cyclic because the last and first strings also differ by the closeness condition.‘ For a survey on Gray codes, see [26, 30]. In this paper, we are focusing on 2-Gray code, where consecutive strings differ by at most two bit changes.

An interesting problem related to Lucas words is thus to discover a Gray code for such strings. The problem was first studied under the name Lucas cube, where a Hamiltonian path in the Lucas cube corresponds to a 1-Gray code for Lucas words. Several results related to Lucas cubes have been published [1, 8, 18, 21, 25, 27, 36]. The problem of finding a Gray code for Lucas words was solved by Baril and Vajnovzski [5, 6]. They proved that a 1-Gray code exists if and only if (p+1)∤n, and therefore there is no 1-Gray code for all integers n and p. However, they were able to find a 2-Gray code for Lucas words for all integers n and p. Baril and Vajnovszki proved that the dual reflected order induces a 2-Gray code for Lucas words. The dual reflected order can be obtained by complementing, reversing, and then listing the strings in BRGC order in reverse. For example, filtering the dual reflected Gray code produced by complementing and reversing the BRGC listing for n=5 leads to the following 2-Gray code for Lucas words of length n=5 and p=2:

11110,01110,00110,10110,10010,00010,01010,11010,

11000,01000,00000,10000,10100,00100,01100,11100,

11101,01101,00101,10101,10001,00001,01001,11001,

11011,01011,00011,10011,10111,00111,01111,11111.

Baril and Vajnovszki also developed an efficient algorithm to generate Lucas words in dual reflected order in constant amortized time per string. However, their Gray code is not cyclic. This paper thus provides the first cyclic 2-Gray code for Lucas words that can be constructed efficiently in constant amortized time per string. Since there is no 1-Gray code for Lucas words for all integers n and p, our cyclic 2-Gray code is optimal. For more information about Gray codes induced by the binary reflected Gray code or the dual reflected Gray code, see [31, 32, 35].

The rest of the paper is outlined as follows. In Section 2, we describe a simple recursive algorithm for generating a cyclic 2-Gray code for Fibonacci words that start with a 0, along with additional operations to generate a cyclic 2-Gray code for Fibonacci words. Then in Section 3, we present an algorithm that utilizes our approach for generating Fibonacci words that start with a 0 to construct a cyclic 2-Gray code for Lucas words. Lastly in Section 4, we introduce a dynamic programming approach to generate Fibonacci words that start with a 0, Fibonacci words and Lucas words, and demonstrate that the strings can be generated in constant amortized time per string.

2 Generating a cyclic 2-Gray code for Fibonacci words

In this section, we first describe a simple recursive algorithm for generating a cyclic 2-Gray code for Fibonacci words that start with a 0. We then discuss simple modifications to the algorithm to generate a cyclic 2-Gray code for Fibonacci words. The approach is similar to the algorithms described in [37] for constructing cyclic 2-Gray codes for q-run constrained words and q-decreasing sequences. A Fibonacci word of order p and length n is a binary string of length n that avoids p consecutive ones. As an example, the 13 Fibonacci words of order p=3 and length n=4 are

0000, 0001, 0010, 0011, 0100, 0101, 0110, 1000, 1001, 1010, 1011, 1100, 1101.

The seven Fibonacci words of order p=3 and length n=4 that start with a 0 are highlighted in bold and in blue color. The set of Fibonacci words is in bijection with the set of q-decreasing sequences, which are binary strings in which every maximal factor of the form 0a⁢1b satisfies the condition where a=0 or q⁢a>b [3].

There are several Gray codes for Fibonacci words. Liu, Hsu, and Chung [23] first studied this problem under the name Fibonacci cube, showing that the Fibonacci cube is Hamiltonian, meaning there exists a 1-Gray code for Fibonacci words for any integer order p. Later, Vajnovszki [34] provided an efficient algorithm that constructs a 1-Gray code for Fibonacci words in constant amortized time per string. Chinburg, Savage, and Wilf [7] considered a more restricted Gray code for Fibonacci words, where any two 1s are at least d≤2 positions apart. For d=2, they provided a Gray code which is precisely the Gray code by Vajnovszki [34]. For d≥3, they showed that a 1-Gray code does not exist, but they constructed a 2-Gray code that only uses single bit-flips or transpositions of a 0 and 1; see also [4]. The Gray codes provided by Vajnovszki and Chinburg et. al., however, are not cyclic. More recently, Hassler, Vajnovszki, and Wong [15, 16] proposed a greedy algorithm to generate a homogeneous 2-Gray code for Fibonacci words with a fixed weight (number of 1s) k, where the bits between the swapped 0 and 1 are all 0s. A 2-Gray code for Fibonacci words with no weight restriction can thus be constructed by interleaving listings of fixed-weight Fibonacci words with odd weights and reversals of the listings of fixed-weight Fibonacci words with even weights, in a manner similar to the approach described in [37] for the q-decreasing sequence. Hassler, Vajnovszki, and Wong also provided an efficient construction for generating the listing in constant amortized time per string. Interestingly, the number of Fibonacci words also follows the Fibonacci numbers and multi-step Fibonacci numbers [12, 24]. For example, when p=2, the initial six terms of the enumeration of Fibonacci words are 2, 3, 5, 8, 13, 21.

Below we provide a new cyclic 2-Gray code for Fibonacci words. Our construction is similar to the one we provided in [37] for generating Gray codes for q-decreasing sequences. All strings considered in this paper are binary. Our algorithms use a run-length representation for binary strings using a series of blocks which are maximal substrings of the form 0∗⁢1∗. Each block Bi can be represented by two non-negative integers (si,ti) corresponding to the number of 0s and 1s respectively. The length of the block Bi is thus given by |Bi|=si+ti. For example, the string α=000110100011001 is represented by B1⁢B2⁢B3⁢B4=(3,2)⁢(1,1)⁢(3,2)⁢(2,1). We first prove the following lemmas.

Lemma 1.

A string B1⁢B2⁢⋯⁢Bk is a Fibonacci word of order p if and only if B1 and B2⁢B3⁢⋯⁢Bk are both Fibonacci words of order p.

Proof.

The proofs for both directions are straightforward from the definition. ◀

Lemma 2.

If α=b1⁢b2⁢⋯⁢bn=B1⁢B2⁢⋯⁢Bk is a Fibonacci word of order p and length n, then β=0⁢b1⁢b2⁢⋯⁢bn is a Fibonacci word of order p and length n+1.

Proof.

Let B1=(s1,t1). Since α is a Fibonacci word, clearly each block Bi in α does not contain p consecutive 1s. Then we have β=B1′⁢B2⁢⋯⁢Bk with B1′=(s1+1,t1), and it is clear that each block in β does not contain p consecutive 1s and is a Fibonacci word of order p and length n+1. ◀

Lemma 3.

The shortest possible length ℓ of the first block B1=(s1,t1) in a Fibonacci word of length n≥2 that starts with a 0 is ℓ=2 when p>1, and ℓ=n when p=1.

Proof.

If p=1, then the only Fibonacci word is 0n, and thus the shortest possible length ℓ=n. Otherwise if p>1, then for the length ℓ to be minimal and less than n, the block must end with a 1. Moreover, the number of 1s of the shortest first block should be minimized to achieve the shortest possible first block, and thus t1=1. Since the string is a Fibonacci word that starts with a 0, each block must start with a 0 and thus the smallest possible value of s1 is s1=1. Therefore, ℓ=s1+t1=2. ◀

We first describe a simple recursive construction to generate a cyclic 2-Gray code for Fibonacci words that start with a 0. The start and end strings of our Gray code for Fibonacci words are essential for our later algorithm to generate our Gray code for Lucas words. The main idea of our recursive algorithm is to leverage Lemma 1 by generating a list of all possible first blocks B1 for Fibonacci words that start with a 0. If the length of B1 is less than n (|B1|<n), we proceed to fill the remaining part of the string with all possible Fibonacci words that start with a 0 and have length n−|B1|. We begin by defining 𝒢pℓ as a listing of Fibonacci words that start with a 0 and have length ℓ, each consisting of only a single block. The listing 𝒢pℓ starts with the string 0ℓ−1⁢1, and each subsequent string is obtained by progressively changing the last 0 of the string to a 1 until it reaches the string 0ℓ−p+1⁢1p−1. For example,

  • ■

    𝒢59: 000000001,000000011,000000111,000001111;

  • ■

    𝒢47: 0000001,0000011,0000111.

We also use the notation 𝒮−1 and 𝒮¯ to refer to the listing obtained by reversing a listing 𝒮, and we use both notations interchangeably. For instance,

  • ■

    𝒢¯59=(𝒢59)−1: 000001111,000000111,000000011,000000001;

  • ■

    𝒢¯47=(𝒢47)−1: 0000111,0000011,0000001.

Similarly, 𝒮−k represents the listing obtained by reversing the listing 𝒮 for k times. Thus, when k is even 𝒮−k=𝒮, and when k is odd 𝒮−k=𝒮−1.

Let B1⋅𝒮 denote the listing 𝒮 with the block B1 prepended to the beginning of each string in 𝒮. Similarly, let 𝒮⋅Bk denote the listing 𝒮 with the block Bk appended to the end of each string in 𝒮. Furthermore, we use the notation ∏i∈{1,2,…,k}𝒮i to represent the listing resulting from concatenating the listings 𝒮1,𝒮2,…,𝒮k with 𝒮1 appearing before 𝒮2, and 𝒮2 appearing before 𝒮3, and so on. For example, ∏𝒮∈{𝒢59,𝒢47}𝒮 is the listing formed by concatenating the listings 𝒢59 and 𝒢47 as follows:

000000001,000000011,000000111,000001111,0000001,0000011,0000111.

We proceed to define 𝒵pn as our Gray code listing of Fibonacci words of order p and length n that start with a 0. Our recursive definition maintains a variable j which stores the complement of the last bit of the string just generated by our algorithm. For instance, if the string just generated by our algorithm is 0000111, then j=0. Initially, we set j=0. The listing 𝒵pn can be recursively defined as follows, with the base case 𝒵p0 being the empty list:

𝒵pn=∏i∈{0,1,…,n−2}(∏B∈(𝒢pn−i)−iB⋅(𝒵pi)−j),0n. (2)

As an example, suppose p=4 and n=6, then we have

  • ■

    ∏B∈𝒢46B⋅(𝒵40)−j=(𝒢46)−0=000001,000011,000111;

  • ■

    ∏B∈𝒢¯45B⋅(𝒵41)−j=00111⋅(𝒵41)−0,00011⋅(𝒵41)−1,00001⋅(𝒵41)−1=001110,000110,000010;

  • ■

    ∏B∈𝒢44B⋅(𝒵42)−j=0001⋅(𝒵42)−1,0011⋅(𝒵42)−0,0111⋅(𝒵42)−1=000100,000101,001101,001100,011100,011101;

  • ■

    ∏B∈𝒢¯43B⋅(𝒵43)−j=011⋅(𝒵43)−0,001⋅(𝒵43)−1=011001,011011,011010,011000,001000,001010,001011,001001;

  • ■

    ∏B∈𝒢42B⋅(𝒵44)−j=01⋅(𝒵44)−0=010001,010011,010111,010110,010010,010100,010101,010000.

The variable j is updated each time after exhausting the generation of 𝒵pi, or its reversal. The recursive listing for Fibonacci words of p=4 and n=6 that start with a 0, that is 𝒵46, is as follows:

000001,000011,000111,001110,000110,000010,000100,000101,

001101,001100,011100,011101,011001,011011,011010,011000,

001000,001010,001011,001001,010001,010011,010111,010110,

010010,010100,010101,010000,000000.

Implementation of the recursive algorithm to generate 𝒵pn is similar to Algorithm 1 in [37]. A dynamic programming algorithm to generate 𝒵pn in constant amortized time per string is provided in Section 4.

Corollary 4.

The listing 𝒵pn starts with the string 0n−1⁢1 and ends with the string 0n when p>1 and n>1.

Proof.

The first string in 𝒵pn is the first string in ∏B∈𝒢pnB⋅(𝒵p0)−j=(𝒢pn)−0, which is 0n−1⁢1. The last string in 𝒵pn is 0n by definition. ◀

Corollary 5.

The listing 𝒵pn contains each Fibonacci words that start with a 0 of length n and order p exactly once when n≥1.

Proof.

The proof is by strong induction on n. In the base case, when n=1, 𝒵pn=0 which clearly contains each Fibonacci word that starts with a 0 and has length one exactly once. Inductively, assuming 𝒵pt contains each Fibonacci word that starts with a 0 and has length t exactly once for 1<t<k, we consider the case when n=k. By the construction of 𝒵pk, the listing 𝒵pk exhaustively lists out all possible first block B1 and prepends them to the beginning of each string in 𝒵pk−|B1|. Since each Fibonacci word that starts with a 0 and has length k−|B1| appears exactly once in 𝒵pk−|B1| by the inductive hypothesis, each string in 𝒵pk also appears exactly once by Lemma 1. ◀

Lemma 6.

Consecutive strings in 𝒵pn differ from each other by at most two bits.

Proof.

The proof is by induction on n. If p=1 then there is only one string 0n when n≥1 and clearly it is a 2-Gray code. In the rest of the proof we assume p>1. In the base case when n=0, 𝒵p0 is the empty list and clearly it is a 2-Gray code. Inductively, assuming consecutive strings in 𝒵pn differ by at most two bits for n∈{1,2,…,k−1}, we consider the case when n=k and two consecutive strings α and β in 𝒵pk, where W.L.O.G. α comes before β. There are four cases:

  • ■

    α and β have the same first block: α and β are consecutive strings in B⋅(𝒵pt)−j for some 1≤t<k with their k−|B| suffixes corresponding to two consecutive strings in 𝒵pt. Thus, α and β differ by at most two bits by induction;

  • ■

    α and β have a different first block but their first blocks have the same length: α and β are consecutive strings in ∏B∈(𝒢pk−t)−tB⋅(𝒵pt)−j for some 1≤t<k with a different first block B and by the definition of 𝒢pk−t, their first blocks differ by one bit. Furthermore by the definition of j, the strings α and β share the same k−|B| suffix and thus α and β differ by one bit;

  • ■

    α and β have their first blocks of different lengths and β≠0k: Let α=a1⁢a2⁢⋯⁢ak=B1⁢B2⁢⋯⁢Bh and β=b1⁢b2⁢⋯⁢bk. Thus a|B1|+1⁢a|B1|+2⁢⋯⁢ak is a Fibonacci word that starts with a 0 and has length k−|B1| by Lemma 1. Observe that α and β are consecutive strings that come from different subsequences ∏B∈(𝒢pk−t)−tB⋅(𝒵pt)−j. By the definition of 𝒢pk−t, the length |B1| prefixes of α and β differ by at most two bits, and b|B1|=0. Furthermore, recall that the variable j is updated as the complement of the last bit of α after generating α, and thus by the construction of 𝒵pn the strings α and β share the same length k−|B1| suffix. Therefore, β=b1⁢b2⁢⋯⁢b|B1|−1⁢0⁢a|B1|+1⁢a|B1|+2⁢⋯⁢ak which implies α and β differ by at most two bits;

  • ■

    β=0k: The first block B1 of α is 01 by Lemma 3 and by the construction of 𝒵pn. The length k−2 suffix of α is either 0k−2 or 0k−3⁢1 by Corollary 4. In either case α differs with β=0k by at most two bits.

Thus, by induction, consecutive strings in 𝒵pn differ from each other by at most two bits. ◀

Together, Corollary 4, Corollary 5 and Lemma 6 prove the following theorem.

Theorem 7.

The listing 𝒵pn lists all Fibonacci words that start with a 0 and have length n and order p in cyclic 2-Gray code order.

The recursive algorithm to generate 𝒵pn can be easily modified to generate a cyclic 2-Gray code for Fibonacci words. The algorithm is similar to the one mentioned in [37] for q-decreasing sequence, which includes prepending the corresponding prefix 1r to each listing of Fibonacci words that start with a 0. We define ℱpn as our Gray code listing of Fibonacci words of order p and length n as follows:

ℱpn={∏r∈{p−1,p−3,…,0}1r⋅(𝒵pn−r)−j,∏r∈{p−2,p−4,…,1}1r⋅(𝒵pn−r)−j¯if p is odd;∏r∈{p−1,p−3,…,1}1r⋅(𝒵pn−r)−j,∏r∈{p−2,p−4,…,0}1r⋅(𝒵pn−r)−j¯if p is even. (3)
Theorem 8.

The listing ℱpn lists all Fibonacci words of length n and order p in cyclic 2-Gray code order.

Proof.

A complete proof of the theorem is given in Appendix B. ◀

3 Generating a cyclic 2-Gray code for Lucas words

In this section, we leverage our results on Fibonacci words to generate a cyclic 2-Gray code for Lucas words. Observe that the set of Lucas words of order p and length n can be constructed by taking the union of all strings in the format 1ℓ⁢γ⁢01m, where γ is a Fibonacci word of order p that starts with a 0 and has length n−ℓ−m−1, and ℓ+m<p. Removing the prefix 1ℓ and the suffix 01m from each string in the set results in a set that contains all possible Fibonacci words of order p that start with a 0 and have length n−ℓ−m−1.

The main idea of our algorithm is thus to utilize our recursive algorithm for 𝒵pn to first generate a cyclic 2-Gray code for Fibonacci words that starts with a 0, for all possible length n−ℓ−m−1. Subsequently, we prepend the prefix 1ℓ and append the suffix 01m to each Fibonacci word that starts with a 0. We first define ℳk,s,rn as a sequence with strings that start with 1s−r and end with 01r, while in between consists of the strings in 𝒵pn−s−1, that is:

ℳp,s,rn=1s−r⋅𝒵pn−s−1⋅01r. (4)

For the special cases when p={1,2,3}, our Gray code listings for Lucas words ℒpn are simply the listing 0n, the listing 𝒵2n,ℳ2,1,0n, and the listing 𝒵¯3n,ℳ3,2,1n,ℳ¯3,2,0n,ℳ3,1,0n respectively. Otherwise if p>3, our Gray code listing ℒpn for Lucas words starts with 𝒵¯pn. It then connects to ℳp,2,1n, followed by listings of the form ℳp,s,rn where each time both s and r are incremented by one until s reaches its maximum possible value, s=p−1. Subsequently, r is decremented by one, and the listing connects to the reverse of ℳp,p−1,p−3n, that is ℳ¯p,p−1,p−3n. This pattern continues with connections to listings of the form ℳ¯p,s,rn and gradually decrementing both s and r by one together until r=1. At r=1, s is incremented by one, and the process of incrementing both s and r by one continues until reaching the maximum possible value for s again. This zigzag pattern proceeds until reaching either the listing ℳp,p−1,1n or ℳ¯p,p−1,1n. It then connects to ℳp,p−2,0n if the current listing is ℳp,p−1,1n, or to ℳ¯p,p−2,0n if the current listing is ℳ¯p,p−1,1n. Next, it connects to ℳp,p−1,0n, followed by ℳ¯p,p−3,0n and continues decrementing s until reaching ℳ¯p,1,0n.

An example of ℒpn with n=6 and p=5 is given as follows:

  • ■

    𝒵¯56: 000000,010000,010101,010100,010010,010110,010111,010011,010001, 001001,001011,001010,001000,011000,011010,011011,011001,011101, 011100,001100,001101,000101,000100,000010,000110,001110,011110, 001111,000111,000011,000001;

  • ■

    ℳ5,2,16: 100101,101101,101001,100001;

  • ■

    ℳ5,3,26: 101011,100011;

  • ■

    ℳ5,4,36: 100111;

  • ■

    ℳ¯5,4,26: 110011;

  • ■

    ℳ¯5,3,16: 110001,110101;

  • ■

    ℳ5,4,16: 111001;

  • ■

    ℳ5,3,06: 111010,111000;

  • ■

    ℳ5,4,06: 111100;

  • ■

    ℳ¯5,2,06: 110000,110100,110110,110010;

  • ■

    ℳ¯5,1,06: 100000,101010,101000,100100,101100,101110,100110,100010.

As such, the Gray code listing for Lucas words for p=5 and n=6, that is ℒ56, is as follows:

000000,010000,010101,010100,010010,010110,010111,010011,010001,001001,

001011,001010,001000,011000,011010,011011,011001,011101,011100,001100,

001101,000101,000100,000010,000110,001110,011110,001111,000111,000011,

000001,100101,101101,101001,100001,101011,100011,100111,110011,110001,

110101,111001,111100,111010,111000,110010,110110,110100,110000,100010,

100110,101110,101100,100100,101000,101010,100000.

Figure 1 illustrates connections of the listings that create ℒ56. The algorithm for generating ℒkn is shown in Algorithm 1.

Figure 1: Connections of subsequences that create ℒ56.
Algorithm 1 An algorithm to generate ℒpn for Lucas words.
Lemma 9.

Consecutive strings in the listing ℳp,s,rn,ℳp,s+1,r+1n differ by at most two bits.

Proof.

By definition, consecutive strings in ℳp,s,rn and ℳp,s+1,r+1n differ by at most two bits. The last string in ℳp,s,rn=1s−r⋅𝒵pn−s−1⋅01r is 1s−r⁢0n−s⁢1r, and the first string in ℳp,s+1,r+1n=1s−r⋅𝒵pn−s−2⋅01r+1 is 1s−r⁢0n−s−3⁢101r+1 if n−s−3≥1, or 1s−r⁢0n−s−1⁢1r+1 otherwise, where in both cases differ with 1s−r⁢0n−s⁢1r by at most two bits. Therefore, consecutive strings in the listing ℳk,s,rn,ℳk,s+1,r+1n differ by at most two bits. ◀ The proofs of the following lemmas are similar to the proof of Lemma 9. Detailed proofs can be found in Appendix C.

Lemma 10.

Consecutive strings in the listing ℳp,s,rn,ℳ¯p,s,r−1n differ by at most two bits.

Lemma 11.

Consecutive strings in the listing ℳ¯p,s,rn,ℳp,s+1,rn differ by at most two bits.

Lemma 12.

Consecutive strings in the listing ℳp,s,rn,ℳp,s−1,r−1n differ by at most two bits.

Lemma 13.

Consecutive strings in the listing ℳp,s,rn,ℳp,s+1,rn and the listing ℳ¯p,s,rn,ℳp,s+1,rn differ by at most two bits.

Lemma 14.

Consecutive strings in the listing ℳp,s,rn,ℳ¯p,s−2,rn differ by at most two bits.

Lemma 15.

Consecutive strings in the listing ℳ¯p,s,rn,ℳ¯p,s−1,rn differ by at most two bits.

Theorem 16.

The algorithm Lucas generates a list of all Lucas words of order p and length n in cyclic 2-Gray code order.

Proof.

Clearly, by the construction of ℒpn, the sequence contains each string in the format 1ℓ⁢γ⁢01m, where γ is a Fibonacci word of order p and length n that starts with a 0 and has length n−ℓ−m−1, with ℓ+m<p exactly one. This means that each Lucas word of order p and length n appears in ℒpn exactly once. In the remaining proof, we will demonstrate that consecutive strings in ℒpn differ by at most two bit changes.

Clearly by definition, consecutive strings in 𝒵pn and ℳp,s,rn differ by at most two bits. If p=1, then ℒ1n=0n, and it is clearly a cyclic 2-Gray code. If p=2, then ℒ2n=𝒵2n,ℳ2,1,0n. The last string of 𝒵2n is 0n, and the first string of ℳ2,1,0n is 10n−3⁢10 when n>3, or 10n−1 otherwise. Clearly, consecutive strings in ℒ2n also differ by at most two bits. The last string of ℳ2,1,0n is 10n−1, and the first string of 𝒵2n is 0n−1⁢1 or 0n, which also differ by at most two bits. Therefore, ℒ2n is a cyclic 2-Gray code.

If p=3, then ℒ3n=𝒵¯3n,ℳ3,2,1n,ℳ¯3,2,0n,ℳ3,1,0n. The last string of 𝒵¯3n is 0n−1⁢1. The first string and the last string of ℳ3,2,1n are 10n−4⁢101 and 10n−2⁢1, respectively when n>4, or simply ℳ3,2,1n=10n−2⁢1 otherwise. The first string and the last string of ℳ¯3,2,0n are 12⁢0n−2 and 12⁢0n−4⁢10, respectively when n>4, or simply ℳ¯3,2,0n=12⁢0n−2 otherwise. In addition, the first string of ℳ3,1,0n is 10n−3⁢10 when n>3, or 10n−1 otherwise, where clearly consecutive strings in ℒ3n also differ by at most two bits. The last string of ℳ3,1,0n is 10n−1, and the first string of 𝒵¯3n is 0n, proving the Gray code is also cyclic.

Otherwise, if p>3, then ℒpn starts with the subsequence 𝒵¯pn,ℳp,2,1n. The last string in 𝒵¯pn is 0n−1⁢1, while the first string in ℳp,2,1n=1⋅𝒵pn−3⋅01 is 10n−4⁢101 when n>4, or 10n−2⁢1 otherwise, and both strings differ by at most two bits. The sequence ℒpn then involves the continuous application of the following four steps, and below we show that each of them is a 2-Gray code:

  1. 1.

    ℳp,s,s−tn,ℳp,s+1,s−t+1n: By Lemma 9;

  2. 2.

    ℳp,p−1,p−t−1n,ℳ¯p,p−1,p−t−2n: By Lemma 10;

  3. 3.

    ℳ¯p,s,s−tn,ℳ¯p,s−1,s−t−1n: By Lemma 9;

  4. 4.

    ℳ¯p,t+1,1n,ℳp,t+2,1n: By Lemma 11.

The sequence eventually reaches ℳp,p−1,1n or ℳ¯p,p−1,1n, depending on parity. The sequence then connects to ℳp,p−2,0n if the current subsequence is ℳp,p−1,1n, or to ℳ¯p,p−2,0n if the current subsequence is ℳ¯p,p−1,1n, which in both cases consecutive strings differ by at most two bits (Lemma 12 and Lemma 9). Next, it connects to ℳp,p−1,0n, followed by ℳ¯p,p−3,0n, then gradually decrements s of ℳ¯p,s,0n from p−3 to 1. By Lemma 13, Lemma 14, and Lemma 15, consecutive strings in the concatenation of these subsequences differ by at most two bits. Lastly, since the last string in ℳ¯p,1,0n is 10n−1 or 10n−3⁢10, and thus the first string and the last string also differ by two bits, and the Gray code is cyclic. ◀

4 A dynamic programming approach to generate Lucas words

Our algorithm to generate Fibonacci words and Lucas words relies on our algorithm to generate Fibonacci words that start with a 0. We thus first discuss an efficient algorithm to generate our listing 𝒵pn for Fibonacci words that start with a 0.

Applying the formulae presented in the previous subsection, the pseudocodes in Algorithm 2 and Algorithm 3 generate Fibonacci words that start with a 0 in cyclic 2-Gray code order. The algorithm ConstructfRunTable precompute 𝒵pt using a standard dynamic programming approach, where p≤t<n, following the recursive definition of 𝒵pn and stores 𝒵pn in a table R. The precomputed table R stores each block B⋅(𝒵pi)−j within 𝒵pt by a 4-tuple (r1,r2,r3,r4) which represents 0r1⁢1r2⋅(𝒵pr4)−((j+r3)mod2). For example, a precomputed table that stores 𝒵pn for p=5 and 1≤n≤6 is shown in Table 1. Also note that when t<2, then the variable j does not affect the listing as 𝒵p0 is the empty list and 𝒵p1=0. We thus initialize the variable j=1 when t≤2. Otherwise when t≥2, observe that the variable j is updated every time a tuple (r1,r2,r3,r4) is stored in the table R. We thus complement the variable j each time when a tuple is produced when n>2. The algorithm fRun then prints out the Fibonacci words in 𝒵pt by referring to the table R. To generate Fibonacci words that start with a 0, we make the initial call ConstructfRunTable(n,p) to construct the table R, then we make the call fRun(n,0). A complete Python implementation of the algorithm is given in Appendix A.

Algorithm 2 A function that constructs dynamic programming tables for generating 𝒵pn.
Algorithm 3 A dynamic programming approach to generate 𝒵pn for Fibonacci words that start with a 0.
Table 1: Dynamic programming tables constructed by Algorithm 2 for 𝒵51 to 𝒵56. Each row in a table is stored by a 4-tuple (r1,r2,r3,r4) which represents 0r1⁢1r2⁢(𝒵pr4)−((j+r3)mod2).
𝒵56 𝒵55 𝒵54 𝒵53 𝒵52 𝒵51
000001 00001 0001 001 01 0
000011 00011 0011 011 00
000111 00111 0111 01⋅ 𝒵¯51
001111 01111 011⋅ 𝒵51 000
01111⋅ 𝒵¯51 0111⋅ 𝒵¯51 001⋅ 𝒵¯51
00111⋅ 𝒵51 0011⋅ 𝒵51 01⋅ 𝒵¯52
00011⋅ 𝒵¯51 0001⋅ 𝒵¯51 0000
00001⋅ 𝒵51 001⋅ 𝒵¯52
0001⋅ 𝒵¯52 011⋅ 𝒵52
0011⋅ 𝒵52 01⋅ 𝒵¯53
0111⋅ 𝒵¯52 00000
011⋅ 𝒵53
001⋅ 𝒵¯53
01⋅ 𝒵54
000000
Lemma 17.

The number of Fibonacci words that start with a 0 of order p and length n is equal to the number of Fibonacci words of order p and length n−1 when n>1.

Proof.

Observe that the set of Fibonacci words that start with a 0 of order p and length n can be obtained by prepending a 0 in front of Fibonacci words of length n−1. Thus, the number of Fibonacci words that start with a 0 of order p and length n is equal to the number of Fibonacci words of order p and length n−1. ◀

We now prove that our dynamic programming algorithm generates each Fibonacci words that start with a 0 in constant amortized time per string. As standard for generation algorithms, the time required to output a string is not part of the analysis.

Theorem 18.

The algorithm fRun generates a cyclic 2-Gray code for Fibonacci words that start with a 0 in constant amortized time per string using O⁢(n3) space.

Proof.

By Theorem 7, the algorithm fRun clearly generates a 2-Gray code for Fibonacci words that start with a 0. The precomputation process of the dynamic programming algorithm involves constructing tables that memorize the first block B1 and a pointer that references a table containing Fibonacci words that start with a 0 with length n−|B1| (see Table 1 for an example of 𝒵56). The operations required to compute a table for 𝒵pt for some t<n involves shifting the last 1 of the first block B1 at most n times. For each shift, a 1 is added in front of the first 1 for at most p times, or a 1 is removed in the first block B1 for at most p times. The total time to construct one table is thus O⁢(p⁢n), and consequently, the total time to construct n−1 tables for precomputation is O⁢(p⁢n2). The memory needed for each table is O⁢(p⁢n) when we store each row of the table as a 4-tuple (r1,r2,r3,r4), and since there are at most n tables, the total memory required is O⁢(p⁢n2) or O⁢(n3) since p≤n.

The number of Fibonacci words starting with a 0 of order p≥2 is at least equal to the number of Fibonacci words that start with a 0 of order p=2. By Lemma 17, the number of Fibonacci words that initiate with a 0 with p=2 follows the Fibonacci sequence, and hence, the count of such words grows following the golden ratio, that is Θ⁢(ϕn) where ϕ=1+52 [13]. Since the exponential growth rate of the number of Fibonacci words that begin with a 0 grows much faster than the O⁢(p⁢n2) total time required to construct the tables, each Fibonacci word that starts with a 0 can be generated in constant amortized time per string using O⁢(n3) space. ◀

Theorem 19.

The listing ℒpn that contains all Lucas words of length n and order p can be generated in constant amortized time per string using O⁢(n3) space.

Proof.

The algorithm for generating ℒpn relies on the algorithm for generating 𝒵pn, with the additional task of prepending and appending the corresponding prefix 1ℓ and 01m to each Fibonacci words that start with a 0. By Theorem 18, each string in 𝒵pn can be generated in constant time per string using O⁢(n3) space. Moreover, this additional step can be performed in constant time while resulting in the generation of at least one more string in ℒpn. Hence, the listing ℒpn that contains all Lucas words of length n and order p can be produced with constant amortized time per string using O⁢(n3) space. ◀

Lemma 20.

The listing ℱpn that contains all Fibonacci words of length n and order p can be generated in constant amortized time per string using O⁢(n3) space.

Proof.

A complete proof of the lemma is given in Appendix B. ◀

5 Final remarks

A q-decreasing word is a binary string in which every maximal factor of the form 0a⁢1b satisfies the condition where a=0 or q⁢a>b, with q being a positive real number. As an example, the 21 q-decreasing words of length n=6 and q=1 are

000000, 000001, 000010, 000011, 000100, 000110, 001000,

001001, 100000, 100011, 100010, 100100, 100001, 110000,

110001, 110010, 111000, 111001, 111100, 111110, 111111.

In [37], we provided a recursive algorithm to generate cyclic 2-Gray codes for q-decreasing words, in a similar manner as the recursive algorithm discussed in this paper. The dynamic programming approach discussed in this paper for generating Fibonacci words that start with a 0 can thus be efficiently applied to generate 2-Gray codes for q-decreasing words, and subsequently, q-run constrained words.

Theorem 21.

The listing 𝒬pn that contains all q-decreasing sequences of length n and order p can be generated in constant amortized time per string using O⁢(n3) space.

Proof.

The approach is similar to the dynamic programming algorithm to generate 𝒵pn. The details of the algorithm and the proof are left to the full version of the paper. ◀

References

  • [1] J. Azarija, S. Klavz̆ar, J. Lee, and Y. Rho. Connectivity of Fibonacci cubes, Lucas cubes and generalized cubes. Discret. Math. Theor. Comput. Sci. , Vol. 17 no. 1, 2015. doi:10.46298/DMTCS.2115.
  • [2] J.-L. Baril, S. Kirgizov, and V. Vajnovszki. Asymptotic bit frequency in Fibonacci words. Pure Math. Appl. , 30(1):23–30, 2022.
  • [3] J.-L. Baril, S. Kirgizov, and V. Vajnovszki. Gray codes for Fibonacci q-decreasing words. Theor. Comput. Sci., 927:120–132, 2022. doi:10.1016/J.TCS.2022.06.003.
  • [4] J.-L. Baril and C. Moreira dos Santos. Gray code for compositions of n with parts 1 and p. Appl. Discret. Math., 3(1):67–84, 2009.
  • [5] J.-L. Baril and V. Vajnovszki. Gray codes for order p Lucas strings. In International Conference on Combinatorics on Words, Turku, Finland, 2003.
  • [6] J.-L. Baril and V. Vajnovszki. Minimal change list for Lucas strings and some graph theoretic consequences. Theor. Comput. Sci., 346(2):189–199, 2005. doi:10.1016/J.TCS.2005.08.020.
  • [7] T. Chinburg, C. Savage, and H. Wilf. Combinatorial families that are exponentially far from being listable in Gray code sequence. Trans. Am. Math. Soc., 351:379–402, 1999.
  • [8] P. Codara and O. M. D’Antona. Generalized Fibonacci and Lucas cubes arising from powers of paths and cycles. Discrete Math., 339(1):270–282, 2016. doi:10.1016/J.DISC.2015.08.012.
  • [9] O. Eǧecioǧlu and V. Iršič. Fibonacci-run graphs I: Basic properties. Discret. Appl. Math., 295:70–84, 2021. doi:10.1016/J.DAM.2021.02.025.
  • [10] O. Eǧecioǧlu and V. Iršič. Fibonacci-run graphs II: Degree sequences. Discret. Appl. Math., 300:56–71, 2021. doi:10.1016/J.DAM.2021.05.018.
  • [11] O. Eǧecioǧlu, S. Klavžar, and M. Mollard. Fibonacci Cubes With Applications And Variations. World Scientific Publishing Company, 2023.
  • [12] M. Feinberg. Fibonacci–Tribonacci. Fibonacci Q., 1(3):71–74, 1963.
  • [13] I. Flores. Direct calculation of k-generalized Fibonacci numbers. Fibonacci Q., 5(3):259–266, 1967.
  • [14] I. Goulden and D. Jackson. Combinatorial Enumeration. A Wiley-Interscience Publication. John Wiley & Sons Inc., New York, 1983.
  • [15] N. Hassler, V. Vajnovszki, and D. Wong. Efficient generation of some greedy Gray codes. Korean Mathematical Society Annual Meeting, 2024.
  • [16] N. Hassler, V. Vajnovszki, and D. Wong. Greedy Gray codes for some restricted classes of binary words. Electronic Proceedings in Theoretical Computer Science, EPTCS, 403:108–112, June 2024. Publisher Copyright: © N. Hassler, V. Vajnovszki, D. Wong; 13th Conference on Random Generation of Combinatorial Structures. Polyominoes and Tilings, GASCom 2024; Conference date: 24-06-2024 Through 28-06-2024. doi:10.4204/EPTCS.403.23.
  • [17] W.-J. Hsu. Fibonacci cubes - a new interconnection topology. IEEE Trans. Parallel Distrib. Syst., 4(1):3–12, 1993.
  • [18] A. Ilić, S. Klavz̆ar, and Y. Rho. Generalized Lucas cubes. Appl. Anal. Discrete Math., 6(1):82–94, 2012.
  • [19] S. Kirgizov. Q-bonacci words and numbers. Fibonacci Q., 60(5):187–195, 2022.
  • [20] S. Kirgizov and J. Ramírez. Polyominoes and graphs built from Fibonacci words. Fibonacci Q., 60(5):196–211, 2022.
  • [21] S. Klavžar and M. Mollard. Asymptotic properties of Fibonacci cubes and Lucas cubes. Ann. Comb., 18(3):447–457, 2014.
  • [22] D. Knuth. The Art of Computer Programming, Volume 4A, Combinatorial Algorithms. Addison-Wesley Professional, 2011.
  • [23] J. Liu, W.-J. Hsu, and M. J. Chung. Generalized Fibonacci cubes are mostly Hamiltonian. J. Graph Theory, 18(8):817–829, 1994. doi:10.1002/JGT.3190180806.
  • [24] E. Miles. Generalized Fibonacci numbers and associated matrices. Amer. Math. Monthly, 67(8):745–752, 1960.
  • [25] E. Munarini, C. P. Cippo, and N. Z. Salvi. On the Lucas cubes. Fibonacci Q., 39(1):12–21, 2001.
  • [26] T. Mütze. Combinatorial Gray codes — an updated survey. Electron. J. Comb., 30:3, 2023.
  • [27] T. Noe and J. Post. Primes in Fibonacci n-step and Lucas n-step sequences. J. of Integer Sequences, 8:Article 05.4.4, 2005.
  • [28] OEIS Foundation Inc. The on-line encyclopedia of integer sequences, published electronically at http://oeis.org. Sequences A000032, A001644, A073817, A074048, A074584, A074584, A105754, and A105755, 2023.
  • [29] F. Ruskey. Combinatorial Generation. Book under preparation, 2003.
  • [30] C. Savage. A survey of combinatorial Gray codes. SIAM Review, (4):605–629, 1997. doi:10.1137/S0036144595295272.
  • [31] J. Sawada, A. Williams, and D. Wong. Inside the binary reflected Gray code: Flip-swap languages in 2-Gray code order. In Thierry Lecroq and Svetlana Puzynina, editors, Combinatorics on Words, pages 172–184, Cham, 2021. doi:10.1007/978-3-030-85088-3_15.
  • [32] J. Sawada, A. Williams, and D. Wong. Flip-swap languages in binary reflected Gray code order. Theor. Comput. Sci., 933:138–148, 2022. doi:10.1016/J.TCS.2022.08.024.
  • [33] D. Stanton and D. White. Constructive Combinatorics. Springer Science & Business Media, 2012.
  • [34] V. Vajnovszki. A loopless generation of bitstrings without p consecutive ones. In C. S. Calude, M. J. Dinneen, and S. Sburlan, editors, Combinatorics, Computability and Logic, pages 227–240, London, 2001. Springer London. doi:10.1007/978-1-4471-0717-0_19.
  • [35] V. Vajnovszki. Gray code order for Lyndon words. Discret. Math. Theor. Comput. Sci., 9(2), 2007. doi:10.46298/DMTCS.393.
  • [36] J. Wei and Y. Yang. Fibonacci and Lucas p-cubes. Discret. Appl. Math., 322:365–383, 2022. doi:10.1016/J.DAM.2022.09.004.
  • [37] D. Wong, B. Liu, C.-T. Lam, and M. Im. Generating cyclic 2-Gray codes for Fibonacci q-decreasing words. In Ryuhei Uehara, Katsuhisa Yamanaka, and Hsu-Chun Yen, editors, WALCOM: Algorithms and Computation, pages 91–102, Singapore, 2024. Springer Nature Singapore. doi:10.1007/978-981-97-0566-5_8.

Appendix A Python code to generate Fibonacci words that start with a 0, Fibonacci words and Lucas words in cyclic 2-Gray code order

1fib_run_dp_table = {}
2
3def construct_fib_run_table(n, p):
4
5 for m in range(1, n+1):
6 R = []
7 j = 1
8
9 for i in range(0, m-1):
10 j = 1 if i == 2 else j
11
12 for k in range(*(1, min(p-1, m-i-1)+1, 1) if (i+1)%2 else (min(p-1, m-i-1), 0, -1)):
13 R += [(m-i-k, k, j%2, i)]
14 j = 1 - j
15
16 R += [(m, 0, 0, 0)]
17 fib_run_dp_table[m] = R
18
19
20def fib_run_dp(n, j=0, prev=None, tail=None):
21
22 if n == 0:
23 yield prev if not tail else prev + tail
24 return
25
26 prev = [] if prev is None else prev
27
28 for (r1, r2, r3, r4) in reversed(fib_run_dp_table[n]) if j else fib_run_dp_table[n]:
29 yield from fib_run_dp(r4, (j+r3)%2, prev + [0]*r1 + [1]*r2, tail)
30
31
32def fib_run(n, p):
33 construct_fib_run_table(n, p)
34 yield from fib_run_dp(n)
35
36
37def fib_string(n, p):
38 construct_fib_run_table(n, p)
39
40 j = 0
41 for r in range(p-1, -1, -2):
42 yield from fib_run_dp(n-r, j, [1]*r)
43 j = 1 - j
44
45 # calculate the direction of the last Z
46 j = ((p + 1) // 2) % 2
47 for r in reversed(range(p-2, -1, -2)):
48 yield from fib_run_dp(n-r, j, [1]*r)
49 j = 1 - j
50
51
52def lucas_string(n, p):
53
54 if p > n: p = n
55 construct_fib_run_table(n, p)
56
57 yield from fib_run_dp(n, 1) if p > 2 else fib_run_dp(n, 0)
58
59 # left side
60 j = 0
61 for t in range(1, p-1): # t = s - r: len of 1s in the front
62 for s in reversed(range(t+1, p)) if j else range(t+1, p):
63 yield from fib_run_dp(n-s-1, j, [1]*t, [0]+[1]*(s-t))
64 j = 1 - j
65
66 # right side
67 if p == 2:
68 yield from fib_run_dp(n-2, 0, [1], [0])
69 elif p == 3:
70 yield from fib_run_dp(n-3, 1, [1]*2, [0])
71 yield from fib_run_dp(n-2, 0, [1], [0])
72 elif p > 3:
73 # M^n_{p, s, r} -> fRun(n-s-1, j, 1^s-r, 01^r)
74 yield from fib_run_dp(n-p+1, 1-j, [1]*(p-2), [0])
75 yield from fib_run_dp(n-p, 0, [1]*(p-1), [0])
76 for s in range(p-3, 0, -1):
77 yield from fib_run_dp(n-s-1, 1, [1]*s, [0])
78
79
80if __name__ == ’__main__’:
81 print("================================================")
82 print("1. Fibonacci words that start with a 0")
83 print("2. Fibonacci words")
84 print("3. Lucas words")
85 print("================================================")
86 print("Enter selection #: ")
87
88 this_type = [fib_run, fib_string, lucas_string][int(input())-1]
89
90 print("Enter n: ")
91 this_n = int(input())
92 print("Enter p: ")
93 this_p = int(input())
94
95 cnt = 0
96 for perm in this_type(this_n, this_p):
97 cnt += 1
98 print(*perm)
99
100 print("Total: " + str(cnt))

Appendix B Generating a cyclic 2-Gray code for Fibonacci words

The Gray code listing ℱpn for Fibonacci words is composed of two parts. The first part begins with blocks of 1r⋅(𝒵pn−r)−j with r=p−1. We then decrement r by two until it reaches r=0 when p is odd or r=1 when p is even. For the second part of the listing ℱpn, we follow a similar process but start with r=p−2. Again, we decrement the value of r by two until it reaches r=1 when p is odd or r=0 when p is even. Finally, we reverse the entire second part of the listing. The formal definition of ℱpn is given as follows:

ℱpn={∏r∈{p−1,p−3,…,0}1r⋅(𝒵pn−r)−j,∏r∈{p−2,p−4,…,1}1r⋅(𝒵pn−r)−j¯if p is odd;∏r∈{p−1,p−3,…,1}1r⋅(𝒵pn−r)−j,∏r∈{p−2,p−4,…,0}1r⋅(𝒵pn−r)−j¯if p is even.

As an example, consider the case where n=6 and p=4. Then, we have

  • ■

    13⋅(𝒵43)−j=13⋅(𝒵43)−0=111001,111011,111010,111000;

  • ■

    11⋅(𝒵45)−j=11⋅(𝒵45)−1=100000,101001,101011,101010,101000,101100,   101101,100101,100100,100010,100110,101110,   100111,100011,100001;

  • ■

    12⋅(𝒵44)−j=12⋅(𝒵44)−0=110001,110011,110111,110110,110010,110100,   110101,110000;

  • ■

    10⋅(𝒵46)−j=(𝒵46)−1=000000,010000,010101,010100,010010,010110,010111,010011,   010001,001001,001011,001010,001000,011000,011010,011011,   011001,011101,011100,001100,001101,000101,000100,000010,   000110,001110,000111,000011,000001;

As such, the Gray code listing for Fibonacci words for p=4 and n=6, that is ℱ46, is as follows:

111001,111011,111010,111000,100000,101001,101011,101010,

101000,101100,101101,100101,100100,100010,100110,101110,

100111,100011,100001,000001,000011,000111,001110,000110,

000010,000100,000101,001101,001100,011100,011101,011001,

011011,011010,011000,001000,001010,001011,001001,010001,

010011,010111,010110,010010,010100,010101,010000,000000,

110000,110101,110100,110010,110110,110111,110011,110001.

Corollary 22.

The value j of ℱpn which computes the complement of the last bit of the last generated string is equal to ⌊p−r2⌋mod2.

Proof.

The value j is initialized to 0 when r={p−1,p−2}, which is equivalent to ⌊p−r2⌋mod2=0. Each block of 𝒵pn−r starts with a string that ends with a 1, and ends with a string that ends with a 0. Observe that r is decremented by two each time in both the first part and the second part of ℱpn, and 𝒵pn−r is reversed with the value j being complemented each time. Therefore, j=⌊p−r2⌋mod2=0. ◀

Proof of Theorem 8.

The proof is similar to the proof of Theorem 2 in [37]. Clearly, the listing ℱpn generates all possible Fibonacci words. We now demonstrate that consecutive strings in ℱpn differ by at most two bits.

Let α=1x⁢γ and β=1y⁢ρ be consecutive strings in ℱpn and W.L.O.G. we assume x≥y. Since consecutive strings in 𝒵pt for some t<n differ by at most two bits (Theorem 7), it is evident that consecutive strings in ℱpn with x=y also differ by at most two bits.

Now if x≠y, we consider two cases. Recall that ℱpn consists of two parts. If x−y≥2, then by the definition of ℱpn, we must have x−y=2 and α and β originate from the same part of ℱpn. Consequently, based on the definition of j, the strings α and β share the same n−x suffix, resulting in a difference of two bits between α and β. On the other hand, if x−y<2, then α and β come from different parts of ℱpn, implying that x=1 and y=0. Furthermore, the suffixes γ and ρ correspond to either the first or last string in 𝒵pn. Thus, we have γ∈{0n−x,0n−x−1⁢1} and ρ∈{0n−y,0n−y−1⁢1} by Corollary 4. In either case, α and β differ by at most two bits.

Finally, the first string of ℱpn is 1p−1⁢0n−p⁢1, and the last string of ℱpn is 1p−2⁢0n−p+1⁢1, differing by one bit. Hence, ℱpn constitutes a cyclic 2-Gray code for Fibonacci words. ◀

The listing ℱpn can then be generated by maintaining the first block 1r and utilizing a generation function for 𝒵pn. Implementation of the modification on 𝒵pn to generate ℱpn is similar to Algorithm 2 in [37].

Proof of Lemma 20.

The proof is similar to the proof of Lemma 19. The algorithm for generating ℱpn relies on the algorithm for generating 𝒵pn, with the additional task of prepending the corresponding prefix 1r to each listing of Fibonacci words that begin with a 0. By Theorem 18, each string in 𝒵pn can be generated in constant time per string using O⁢(n3) space. Moreover, this additional step can be performed in constant time while resulting in the generation of at least one more string in ℱpn. Hence, the listing ℱpn that contains all Fibonacci words of length n and order p can be produced with constant amortized time per string using O⁢(n3) space. ◀

Appendix C Proofs of Lemmas 10-15

Proof of Lemma 10.

By definition, consecutive strings in ℳp,s,rn and ℳ¯p,s,r−1n differ by at most two bits. The last string in ℳp,s,rn=1s−r⋅𝒵pn−s−1⋅01r is 1s−r⁢0n−s⁢1r, and the first string in ℳ¯k,s,r−1n=1s−r+1⋅𝒵pn−s−1⋅01r−1 is 1s−r+1⁢0n−s⁢1r−1, where both strings differ from each other by at most two bits. Therefore, consecutive strings in the listing ℳp,s,rn,ℳ¯p,s,r−1n differ by at most two bits. ◀

Proof of Lemma 11.

By definition, consecutive strings in ℳ¯p,s,rn and ℳp,s+1,rn differ by at most two bits. The last string in ℳ¯p,s,rn=1s−r⋅𝒵¯pn−s−1⋅01r is 1s−r⁢0n−s−2⁢101r, and the first string in ℳp,s+1,rn=1s−r+1⋅𝒵pn−s−2⋅01r is 1s−r+1⁢0n−s−3⁢101r if n−s−3≥1 or 1s−r+1⁢0n−s−1⁢1r otherwise, where both strings also differ from each other by at most two bits. Therefore, consecutive strings in the listing ℳ¯p,s,rn,ℳp,s+1,rn differ by at most two bits. ◀

Proof of Lemma 12.

By definition, consecutive strings in ℳp,s,rn and ℳp,s−1,r−1n differ by at most two bits. The last string in ℳp,s,rn=1s−r⋅𝒵pn−s−1⋅01r is 1s−r⁢0n−s⁢1r, and the first string in ℳp,s−1,r−1n=1s−r⋅𝒵pn−s⋅01r−1 is 1s−r⁢0n−s−1⁢101r−1 if n−s−1≥1, or 1s−r⁢0n−s+1⁢1r−1 otherwise, where in both cases differ with 1s−r⁢0n−s⁢1r by at most two bits. Therefore, consecutive strings in the listing ℳp,s,rn,ℳp,s−1,r−1n differ by at most two bits. ◀

Proof of Lemma 13.

The last string in ℳp,s,rn or ℳ¯p,s,rn are 1s−r⁢0n−s−2⁢101r or 1s−r⁢0n−s⁢1r, and the first string in ℳp,s+1,rn=1s−r+1⋅𝒵pn−s−2⋅01r is 1s−r+1⁢0n−s−3⁢101r or 1s−r+1⁢0n−s−1⁢1r, where in both cases differ with 1s−r⁢0n−s⁢1r by at most two bits. Therefore, consecutive strings in the listing ℳp,s,rn,ℳ¯p,s+1,rn and the listing ℳ¯p,s,rn,ℳ¯p,s+1,rn differ by at most two bits. ◀

Proof of Lemma 14.

The last string in ℳp,s,rn=1s−r⋅𝒵pn−s−1⁢01r is 1s−r⁢0n−s⁢1r, and the first string in ℳ¯p,s−2,rn=1s−r−2⋅𝒵¯pn−s+1⋅01r is 1s−r−2⁢0n−s⁢1r, which differ by at most two bits. Therefore, consecutive strings in the listing ℳp,s,rn,ℳ¯p,s−2,rn differ by at most two bits. ◀

Proof of Lemma 15.

The last string in ℳ¯p,s,rn=1s−r⋅𝒵¯pn−s−1⁢01r is 1s−r⁢0n−s−2⁢101r or 1s−r⁢0n−s⁢1r, and the first string in ℳ¯p,s−1,rn=1s−r−1⋅𝒵¯pn−s⋅01r is 1s−r−1⁢0n−s+1⁢1r, where in both cases differ by at most two bits. Therefore, consecutive strings in the listing ℳ¯p,s,rn,ℳ¯p,s−1,rn differ by at most two bits. ◀