Generating a Cyclic 2-Gray Code for Lucas Words in Constant Amortized Time
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 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 , 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 space. The algorithm can also be modified to produce the first efficient algorithm for generating -decreasing sequences in constant amortized time per string, also using space. Our work extends a previous result on generating a cyclic 2-Gray code for -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, -decreasing sequence, Gray code, CAT algorithmCopyright and License:
2012 ACM Subject Classification:
Mathematics of computing Combinatorial algorithmsAcknowledgements:
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äkinenSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
A Lucas word of order and length is a binary string of length that is not equal to , avoids consecutive ones, and has neither a prefix nor a suffix such that . In other words, a Lucas word is a binary string of length that is not equal to and avoids consecutive ones when the string is considered cyclically. As an example, the 11 Lucas words of order and length 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 , the initial six terms of the enumeration of Lucas words are 1, 3, 4, 7, 11, 18. The Lucas numbers of order can be defined recursively as follows:
| (1) |
This recursive definition is analogous to the Fibonacci sequence, with the exception of different initial terms. Interestingly, when , 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 , 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 , , and , and the recurrence formula . Similarly when , 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 and . 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 [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 -decreasing words, which are binary strings in which every maximal factor of the form satisfies the condition where or [3, 20]. Another interesting variant is the concept of Fibonacci words, which are binary strings of length that avoid consecutive ones. Interestingly, there exists a bijection between the set of Fibonacci words and the set of -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 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 binary strings of length so that consecutive strings differ in one bit. For example, when the order is
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 , and therefore there is no 1-Gray code for all integers and . However, they were able to find a 2-Gray code for Lucas words for all integers and . 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 leads to the following 2-Gray code for Lucas words of length and :
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 and , 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 -run constrained words and -decreasing sequences. A Fibonacci word of order and length is a binary string of length that avoids consecutive ones. As an example, the 13 Fibonacci words of order and length are
0000, 0001, 0010, 0011, 0100, 0101, 0110, 1000, 1001, 1010, 1011, 1100, 1101.
The seven Fibonacci words of order and length 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 -decreasing sequences, which are binary strings in which every maximal factor of the form satisfies the condition where or [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 . 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 positions apart. For , they provided a Gray code which is precisely the Gray code by Vajnovszki [34]. For , 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) , 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 -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 , 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 -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 . Each block can be represented by two non-negative integers corresponding to the number of 0s and 1s respectively. The length of the block is thus given by . For example, the string is represented by . We first prove the following lemmas.
Lemma 1.
A string is a Fibonacci word of order if and only if and are both Fibonacci words of order .
Proof.
The proofs for both directions are straightforward from the definition.
Lemma 2.
If is a Fibonacci word of order and length , then is a Fibonacci word of order and length .
Proof.
Let . Since is a Fibonacci word, clearly each block in does not contain consecutive 1s. Then we have with , and it is clear that each block in does not contain consecutive 1s and is a Fibonacci word of order and length .
Lemma 3.
The shortest possible length of the first block in a Fibonacci word of length that starts with a 0 is when , and when .
Proof.
If , then the only Fibonacci word is , and thus the shortest possible length . Otherwise if , then for the length to be minimal and less than , 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 . 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 is . Therefore, .
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 for Fibonacci words that start with a 0. If the length of is less than (), we proceed to fill the remaining part of the string with all possible Fibonacci words that start with a 0 and have length . We begin by defining as a listing of Fibonacci words that start with a 0 and have length , each consisting of only a single block. The listing starts with the string , and each subsequent string is obtained by progressively changing the last 0 of the string to a 1 until it reaches the string . For example,
-
: ;
-
: .
We also use the notation and to refer to the listing obtained by reversing a listing , and we use both notations interchangeably. For instance,
-
: ;
-
: .
Similarly, represents the listing obtained by reversing the listing for times. Thus, when is even , and when is odd .
Let denote the listing with the block prepended to the beginning of each string in . Similarly, let denote the listing with the block appended to the end of each string in . Furthermore, we use the notation to represent the listing resulting from concatenating the listings with appearing before , and appearing before , and so on. For example, is the listing formed by concatenating the listings and as follows:
.
We proceed to define as our Gray code listing of Fibonacci words of order and length that start with a 0. Our recursive definition maintains a variable 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 , then . Initially, we set . The listing can be recursively defined as follows, with the base case being the empty list:
| (2) |
As an example, suppose and , then we have
The variable is updated each time after exhausting the generation of , or its reversal. The recursive listing for Fibonacci words of and that start with a 0, that is , is as follows:
Implementation of the recursive algorithm to generate is similar to Algorithm 1 in [37]. A dynamic programming algorithm to generate in constant amortized time per string is provided in Section 4.
Corollary 4.
The listing starts with the string and ends with the string when and .
Proof.
The first string in is the first string in , which is . The last string in is by definition.
Corollary 5.
The listing contains each Fibonacci words that start with a 0 of length and order exactly once when .
Proof.
The proof is by strong induction on . In the base case, when , which clearly contains each Fibonacci word that starts with a 0 and has length one exactly once. Inductively, assuming contains each Fibonacci word that starts with a 0 and has length exactly once for , we consider the case when . By the construction of , the listing exhaustively lists out all possible first block and prepends them to the beginning of each string in . Since each Fibonacci word that starts with a 0 and has length appears exactly once in by the inductive hypothesis, each string in also appears exactly once by Lemma 1.
Lemma 6.
Consecutive strings in differ from each other by at most two bits.
Proof.
The proof is by induction on . If then there is only one string when and clearly it is a 2-Gray code. In the rest of the proof we assume . In the base case when , is the empty list and clearly it is a 2-Gray code. Inductively, assuming consecutive strings in differ by at most two bits for , we consider the case when and two consecutive strings and in , where W.L.O.G. comes before . There are four cases:
-
and have the same first block: and are consecutive strings in for some with their suffixes corresponding to two consecutive strings in . 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 for some with a different first block and by the definition of , their first blocks differ by one bit. Furthermore by the definition of , the strings and share the same suffix and thus and differ by one bit;
-
and have their first blocks of different lengths and : Let and . Thus is a Fibonacci word that starts with a 0 and has length by Lemma 1. Observe that and are consecutive strings that come from different subsequences . By the definition of , the length prefixes of and differ by at most two bits, and . Furthermore, recall that the variable is updated as the complement of the last bit of after generating , and thus by the construction of the strings and share the same length suffix. Therefore, which implies and differ by at most two bits;
Thus, by induction, consecutive strings in differ from each other by at most two bits.
Theorem 7.
The listing lists all Fibonacci words that start with a 0 and have length and order in cyclic 2-Gray code order.
The recursive algorithm to generate 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 -decreasing sequence, which includes prepending the corresponding prefix to each listing of Fibonacci words that start with a 0. We define as our Gray code listing of Fibonacci words of order and length as follows:
| (3) |
Theorem 8.
The listing lists all Fibonacci words of length and order 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 and length can be constructed by taking the union of all strings in the format , where is a Fibonacci word of order that starts with a 0 and has length , and . Removing the prefix and the suffix from each string in the set results in a set that contains all possible Fibonacci words of order that start with a 0 and have length .
The main idea of our algorithm is thus to utilize our recursive algorithm for to first generate a cyclic 2-Gray code for Fibonacci words that starts with a 0, for all possible length . Subsequently, we prepend the prefix and append the suffix to each Fibonacci word that starts with a 0. We first define as a sequence with strings that start with and end with , while in between consists of the strings in , that is:
| (4) |
For the special cases when , our Gray code listings for Lucas words are simply the listing , the listing , and the listing respectively. Otherwise if , our Gray code listing for Lucas words starts with . It then connects to , followed by listings of the form where each time both and are incremented by one until reaches its maximum possible value, . Subsequently, is decremented by one, and the listing connects to the reverse of , that is . This pattern continues with connections to listings of the form and gradually decrementing both and by one together until . At , is incremented by one, and the process of incrementing both and by one continues until reaching the maximum possible value for again. This zigzag pattern proceeds until reaching either the listing or . It then connects to if the current listing is , or to if the current listing is . Next, it connects to , followed by and continues decrementing until reaching .
An example of with and is given as follows:
-
-
: ;
-
: ;
-
: ;
-
: ;
-
: ;
-
: ;
-
: ;
-
: ;
-
: ;
-
: .
As such, the Gray code listing for Lucas words for and , that is , is as follows:
Figure 1 illustrates connections of the listings that create . The algorithm for generating is shown in Algorithm 1.
Lemma 9.
Consecutive strings in the listing differ by at most two bits.
Proof.
By definition, consecutive strings in and differ by at most two bits. The last string in is , and the first string in is if , or otherwise, where in both cases differ with by at most two bits. Therefore, consecutive strings in the listing 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 differ by at most two bits.
Lemma 11.
Consecutive strings in the listing differ by at most two bits.
Lemma 12.
Consecutive strings in the listing differ by at most two bits.
Lemma 13.
Consecutive strings in the listing and the listing differ by at most two bits.
Lemma 14.
Consecutive strings in the listing differ by at most two bits.
Lemma 15.
Consecutive strings in the listing differ by at most two bits.
Theorem 16.
The algorithm Lucas generates a list of all Lucas words of order and length in cyclic 2-Gray code order.
Proof.
Clearly, by the construction of , the sequence contains each string in the format , where is a Fibonacci word of order and length that starts with a 0 and has length , with exactly one. This means that each Lucas word of order and length appears in exactly once. In the remaining proof, we will demonstrate that consecutive strings in differ by at most two bit changes.
Clearly by definition, consecutive strings in and differ by at most two bits. If , then , and it is clearly a cyclic 2-Gray code. If , then . The last string of is , and the first string of is when , or otherwise. Clearly, consecutive strings in also differ by at most two bits. The last string of is , and the first string of is or , which also differ by at most two bits. Therefore, is a cyclic 2-Gray code.
If , then . The last string of is . The first string and the last string of are and , respectively when , or simply otherwise. The first string and the last string of are and , respectively when , or simply otherwise. In addition, the first string of is when , or otherwise, where clearly consecutive strings in also differ by at most two bits. The last string of is , and the first string of is , proving the Gray code is also cyclic.
Otherwise, if , then starts with the subsequence . The last string in is , while the first string in is when , or otherwise, and both strings differ by at most two bits. The sequence then involves the continuous application of the following four steps, and below we show that each of them is a 2-Gray code:
The sequence eventually reaches or , depending on parity. The sequence then connects to if the current subsequence is , or to if the current subsequence is , which in both cases consecutive strings differ by at most two bits (Lemma 12 and Lemma 9). Next, it connects to , followed by , then gradually decrements of from 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 is or , 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 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 using a standard dynamic programming approach, where , following the recursive definition of and stores in a table . The precomputed table stores each block within by a 4-tuple which represents . For example, a precomputed table that stores for and is shown in Table 1. Also note that when , then the variable does not affect the listing as is the empty list and . We thus initialize the variable when . Otherwise when , observe that the variable is updated every time a tuple is stored in the table . We thus complement the variable each time when a tuple is produced when . The algorithm fRun then prints out the Fibonacci words in by referring to the table . To generate Fibonacci words that start with a 0, we make the initial call ConstructfRunTable to construct the table , then we make the call fRun. A complete Python implementation of the algorithm is given in Appendix A.
| 000001 | 00001 | 0001 | 001 | 01 | 0 | ||||||||||||
| 000011 | 00011 | 0011 | 011 | 00 | |||||||||||||
| 000111 | 00111 | 0111 | |||||||||||||||
| 001111 | 01111 | 011 | 000 | ||||||||||||||
| 01111 | 0111 | 001 | |||||||||||||||
| 00111 | 0011 | 01 | |||||||||||||||
| 00011 | 0001 | 0000 | |||||||||||||||
| 00001 | 001 | ||||||||||||||||
| 0001 | 011 | ||||||||||||||||
| 0011 | 01 | ||||||||||||||||
| 0111 | 00000 | ||||||||||||||||
| 011 | |||||||||||||||||
| 001 | |||||||||||||||||
| 01 | |||||||||||||||||
| 000000 | |||||||||||||||||
Lemma 17.
The number of Fibonacci words that start with a 0 of order and length is equal to the number of Fibonacci words of order and length when .
Proof.
Observe that the set of Fibonacci words that start with a 0 of order and length can be obtained by prepending a 0 in front of Fibonacci words of length . Thus, the number of Fibonacci words that start with a 0 of order and length is equal to the number of Fibonacci words of order and length .
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 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 and a pointer that references a table containing Fibonacci words that start with a 0 with length (see Table 1 for an example of ). The operations required to compute a table for for some involves shifting the last 1 of the first block at most times. For each shift, a 1 is added in front of the first 1 for at most times, or a 1 is removed in the first block for at most times. The total time to construct one table is thus , and consequently, the total time to construct tables for precomputation is . The memory needed for each table is when we store each row of the table as a 4-tuple , and since there are at most tables, the total memory required is or since .
The number of Fibonacci words starting with a 0 of order is at least equal to the number of Fibonacci words that start with a 0 of order . By Lemma 17, the number of Fibonacci words that initiate with a 0 with follows the Fibonacci sequence, and hence, the count of such words grows following the golden ratio, that is where [13]. Since the exponential growth rate of the number of Fibonacci words that begin with a 0 grows much faster than the 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 space.
Theorem 19.
The listing that contains all Lucas words of length and order can be generated in constant amortized time per string using space.
Proof.
The algorithm for generating relies on the algorithm for generating , with the additional task of prepending and appending the corresponding prefix and to each Fibonacci words that start with a 0. By Theorem 18, each string in can be generated in constant time per string using space. Moreover, this additional step can be performed in constant time while resulting in the generation of at least one more string in . Hence, the listing that contains all Lucas words of length and order can be produced with constant amortized time per string using space.
Lemma 20.
The listing that contains all Fibonacci words of length and order can be generated in constant amortized time per string using space.
Proof.
A complete proof of the lemma is given in Appendix B.
5 Final remarks
A -decreasing word is a binary string in which every maximal factor of the form satisfies the condition where or , with being a positive real number. As an example, the 21 -decreasing words of length and 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 -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 -decreasing words, and subsequently, -run constrained words.
Theorem 21.
The listing that contains all -decreasing sequences of length and order can be generated in constant amortized time per string using space.
Proof.
The approach is similar to the dynamic programming algorithm to generate . 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 -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 with parts and . Appl. Discret. Math., 3(1):67–84, 2009.
- [5] J.-L. Baril and V. Vajnovszki. Gray codes for order 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 -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 -step and Lucas -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 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 -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 -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
Appendix B Generating a cyclic 2-Gray code for Fibonacci words
The Gray code listing for Fibonacci words is composed of two parts. The first part begins with blocks of with . We then decrement by two until it reaches when is odd or when is even. For the second part of the listing , we follow a similar process but start with . Again, we decrement the value of by two until it reaches when is odd or when is even. Finally, we reverse the entire second part of the listing. The formal definition of is given as follows:
As an example, consider the case where and . Then, we have
-
;
-
-
-
As such, the Gray code listing for Fibonacci words for and , that is , is as follows:
Corollary 22.
The value of which computes the complement of the last bit of the last generated string is equal to .
Proof.
The value is initialized to 0 when , which is equivalent to . Each block of starts with a string that ends with a 1, and ends with a string that ends with a 0. Observe that is decremented by two each time in both the first part and the second part of , and is reversed with the value being complemented each time. Therefore, .
Proof of Theorem 8.
The proof is similar to the proof of Theorem 2 in [37]. Clearly, the listing generates all possible Fibonacci words. We now demonstrate that consecutive strings in differ by at most two bits.
Let and be consecutive strings in and W.L.O.G. we assume . Since consecutive strings in for some differ by at most two bits (Theorem 7), it is evident that consecutive strings in with also differ by at most two bits.
Now if , we consider two cases. Recall that consists of two parts. If , then by the definition of , we must have and and originate from the same part of . Consequently, based on the definition of , the strings and share the same suffix, resulting in a difference of two bits between and . On the other hand, if , then and come from different parts of , implying that and . Furthermore, the suffixes and correspond to either the first or last string in . Thus, we have and by Corollary 4. In either case, and differ by at most two bits.
Finally, the first string of is , and the last string of is , differing by one bit. Hence, constitutes a cyclic 2-Gray code for Fibonacci words.
The listing can then be generated by maintaining the first block and utilizing a generation function for . Implementation of the modification on to generate 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 relies on the algorithm for generating , with the additional task of prepending the corresponding prefix to each listing of Fibonacci words that begin with a 0. By Theorem 18, each string in can be generated in constant time per string using space. Moreover, this additional step can be performed in constant time while resulting in the generation of at least one more string in . Hence, the listing that contains all Fibonacci words of length and order can be produced with constant amortized time per string using space.
Appendix C Proofs of Lemmas 10-15
Proof of Lemma 10.
By definition, consecutive strings in and differ by at most two bits. The last string in is , and the first string in is , where both strings differ from each other by at most two bits. Therefore, consecutive strings in the listing differ by at most two bits.
Proof of Lemma 11.
By definition, consecutive strings in and differ by at most two bits. The last string in is , and the first string in is if or otherwise, where both strings also differ from each other by at most two bits. Therefore, consecutive strings in the listing differ by at most two bits.
Proof of Lemma 12.
By definition, consecutive strings in and differ by at most two bits. The last string in is , and the first string in is if , or otherwise, where in both cases differ with by at most two bits. Therefore, consecutive strings in the listing differ by at most two bits.
Proof of Lemma 13.
The last string in or are or , and the first string in is or , where in both cases differ with by at most two bits. Therefore, consecutive strings in the listing and the listing differ by at most two bits.
Proof of Lemma 14.
The last string in is , and the first string in is , which differ by at most two bits. Therefore, consecutive strings in the listing differ by at most two bits.
Proof of Lemma 15.
The last string in is or , and the first string in is , where in both cases differ by at most two bits. Therefore, consecutive strings in the listing differ by at most two bits.
