Keywords

1 Introduction

In the framework of combinatorial algorithms, one of the most documented questions consists in the development of methods in order to generate, exactly once, all the objects in some specific class [15]. Many topics are concerned by such a problem: suffice it to mention sequence counting [1], signal encoding [16], and data compression [18].

The so-called binary Gray codes first appeared in [10]: given a binary alphabet A and some positive integer n, they referred to sequences with maximum length of pairwise different n-tuples of characters (that is, words in \(A^n\)), provided that any pair of consecutive items differ by exactly one character. Shortly after, a similar study was drawn in the framework of non-binary alphabets [4, 8]. With regard to other famous combinatorial classes of objects, the term of combinatorial Gray code, for its part, appeared in [11]: actually, the difference between successive items, although being fixed, need not to be small [20]. Generating all permutations of a given n-element set constitutes a noticeable example [7]. Subsets of fixed size are also concerned [6], as well as cross-bifix free words [2], Debruijn sequences [9], set partitions [13], necklaces [19]: the list is far to be exhaustive. Combinatorial Gray sequences are often needed to be cyclic [3], in the sense that the initial term itself can be retrieved as successor of the last one. Such a condition justifies the terminology of Gray cycle [14, Sect. 7.2.1.1].

In view of some formal framework, we notice that each of the sequences we mentioned above involves some word binary relation \(\tau \subseteq A^*\times A^*\), where A stands for a finite alphabet, and \(A^*\) for the free monoid it generates. For its part, the combinatorial class of objects can be modelled by some finite language \(X\subseteq A^*\). Given a sequence of words we denote in square brackets the corresponding indices: this will allow us to make a difference from \(w_i\), the character in position i in a given word w. We define a cyclic Gray sequence over X, with respect to \(\tau \) (for short: \(\tau \)-Gray cycle over X) as every finite sequence of words \(\left( w_{[i]}\right) _{i\in [0,|X|-1]}\) satisfying each of the three following conditions:

  1. (G1)

    For every word \(x\in X\), some \(i\in [0,|X|-1]\) exists such that we have \(x=w_{[i]}\);

  2. (G2)

    For every \(i\in [1,|X|-1] \), we have \(w_{[i]}\in \tau \left( w_{[i-1]}\right) \); in addition, the condition \(w_{[0]}\in \tau \left( w_{[|X|-1]}\right) \) holds;

  3. (G3)

    For every pair \(i,j\in [0,|X|-1]\), \(i\ne j\) implies \(w_{[i]}\ne w_{[j]}\).

With this definition, the set X need not to be uniform that is, in the Gray cycle the terms may have a variable length. For instance, given the alphabet \(A=\{0,1\}\), take for \(\tau \) the word binary relation \(\varLambda _1\) which, with every word w associates all the strings located within a Levenshtein distance of 1 from w (see e.g. [17]); with such a relation, the sequence (0, 00, 01, 11, 10, 1) is a \(\varLambda _1\)-Gray cycle over \(X=A\cup A^2\). Actually, in addition to the topics we mentioned above, two other fields involved by those Gray cycles may be mentioned. Firstly, with regard to graph theory, a \(\tau \)-Gray cycle over X exists if, and only if, there is some Hamiltonian circuit in the graph of the relation \(\tau \) (see e.g. [20]). Secondly, given a binary word relation \(\tau \subseteq A^*\times A^*\), and given \(X\subseteq A^*\), if some \(\tau \)-Gray cycle exists over X, then X is \(\tau \)-closed [17] that is, the inclusion \(\tau (X)\subseteq X\) holds, where \(\tau (X)\) stands for the set of the images of the words in X under the relation \(\tau \). Such closed sets actually constitute a special subfamily in the famous dependence systems [5, 12]. Notice that, given a \(\tau \)-closed set \(X\subseteq A^*\), there do not necessarily exist non-empty \(\tau \)-Gray cycles over X. A typical example is provided by \(\tau \) being \(id_{A^*}\), the identity over \(A^*\), with respect to which every finite set \(X\subseteq A^*\) is closed; however non-empty \(\tau \)-Gray cycle can exist only over singletons.

In the present paper, given a positive integer n, and denoting by \(A^{\le n}\) the set of the words with length not greater than n, we consider the family of all sequences that can be a \(\tau \)-Gray cycle over some subset X of \(A^{\le n}\). This is a natural question to study those sequences of maximum length which, of course, correspond to subsets X of maximum cardinality; such a length, which we denote by \(\lambda _{A,\tau }(n)\), means introducing some complexity measure for the word binary relation \(\tau \). With regard to the preceding examples we have \(\lambda _{\{0,1\},\varLambda _1}(2)=6\); moreover, for every alphabet A and positive integer n, the identity \(\lambda _{A,id_{A^*}}(n)=1\) holds. We focus on the case where \(\tau \) is \(\sigma _k\), the so-called k-character substitution: with every word with length at least k, say w, this relation associates all the words \(w'\), with \(|w'|=|w|\) and such that the character \(w'_i\) differs from \(w_i\) in exactly k values of \(i\in [1,|w|]\). As commented in [12, 17], \(\sigma _k\) has noticeable inference in the famous framework of error detection. On the other hand, by definition, \(w'\in \sigma _k(w)\) implies \(|w'|=|w|\) therefore, if there is some \(\sigma _k\)-Gray cycle over X, then X is a uniform set. From this point of view, the classical Gray codes, which allow to generate all n-tuples over A, correspond to \(\sigma _1\)-Gray cycles over \(A^n\), furthermore we have \(\lambda _{A,\sigma _1}(n)=|A|^n\). In addition, in the case where A is a binary alphabet, it can be easily proved that, for every \(n\ge 3\), we have \(\lambda _{A,\sigma _2}(n)=2^{n-1}\) [14, Exercise 8, p. 77]. However, in the most general case, although an exhaustive description of \(\sigma _k\)-closed variable-length codes has been provided in [17], the question of computing some \(\sigma _k\)-Gray cycle of maximum length has remained open. In our paper we establish the following result:

Theorem. Let A be a finite alphabet, \(k\ge 1\) , and \(n\ge k\) . Then exactly one of the following conditions holds:

  1. (i)

    \(|A|\ge 3\), \(n\ge k\) , and \(\lambda _{A,\sigma _k}(n)=|A|^n\) ;

  2. (ii)

    \(|A|=2\), \(n=k\) , and \(\lambda _{A,\sigma _k}(n)=2\) ;

  3. (iii)

    \(|A|=2\) , \(n\ge k+1\) , k is odd and \(\lambda _{A,\sigma _k}(n)=|A|^n\) ;

  4. (iv)

    \(|A|=2\) , \(n\ge k+1\) , k is even, and \(\lambda _{A,\sigma _k}(n)=|A|^{n-1}\) .

In addition, in each case some \(\sigma _k\) -Gray cycle of maximum length can be explicitly computed.

We now shortly describe the contents of the paper. In Sect. 2, we recall the two famous examples of the binary (resp., |A|-ary) reflected Gray code. By applying some induction based methods, in Sect. 3 and Sect. 4, these sequences allow to compute special families of \(\sigma _k\)-Gray cycles with maximum length. In Sect. 5, in the case where A is a binary alphabet, and k an even positive integer, we also compute a family of Gray cycles with maximum length; in addition some further development is raised.

2 Preliminaries

Several definitions and notation have already been fixed. In the whole paper, A stands for some finite alphabet, with \(|A|\ge 2\). Given a word \(w\in A^*\), we denote by |w| its length; in addition, for every \(a\in A\), we denote by \(|w|_a\) the number of occurrences of the character a in w.

The Reflected Binary Gray Cycle

Let \(A=\{0,1\}\), and \(n\ge 1\). The most famous example of \(\sigma _1\)-Gray cycle over \(A^n\) is certainly the so-called reflected binary Gray code (see e.g. [10] or [14, p. 6]): in the present paper we denote it by \(g^{n,1}\). It can be defined by the recurrent sequence initialized with \(g^{n,1}_{[0]}=0^n\), and satisfying the following property: for every \(i\in [1,|A|^n-1]\), a unique integer \(j\in [1,n]\) exists such that, in both words \(g^{n,1}_{[i]}\) and \(g^{n,1}_{[i-1]}\) the corresponding characters in position j differ; in addition, the position j is chosen to be maximum in such a way that \(g^{n,1}_{[i]}\notin \{g^{n,1}_{[0]},\cdots ,g^{n,1}_{[i-1]}\}\).

Example 1

In what follows we provide a column representation of \(g^{2,1}\) and \(g^{3,1}\):

By construction, for every \(n\ge 1\), each of the following identities holds:

$$\begin{aligned} g^{n,1}_{[0]}=0^n,~~g^{n,1}_{[1]}=0^{n-1}1,~~g^{n,1}_{[2^{n}-2]}=10^{n-2}1,~~g^{n,1}_{[2^{n}-1]}=10^{n-1} \end{aligned}$$
(1)

The |A|-ary Reflected Gray Cycle

The preceding construction can be extended in order to obtain the so-called |A|-ary reflected Gray code [4, 8], a \(\sigma _1\)-Gray cycle over \(A^n\), which we denote by \(h^{n,1}\). Set \(A=\{0, \cdots ,p-1\}\) and denote by \(\theta \) the cyclic permutation \((0,1,\dots p-1)\). The sequence \(h^{n,1}\) is initialized with \(h^{n,1}_{[0]}=0^n\). In addition, for every \(i\in [1,|A|^n-1]\), a unique integer j exists such that c and d, the characters respectively in position j in \(h^{n,1}_{[i-1]}\) and \(h^{n,1}_{[i]}\), satisfy both the following conditions: (i) \(d=\theta (c)\); (ii) j is the greatest integer in [1, n] such that \(h^{n,1}_{[i]}\notin \{h^{n,1}_{[0]},\cdots ,h^{n,1}_{[i-1]}\}\).

Example 2

For \(A=\{0,1,2\}\) the sequence \(h^{3,1}\) is the concatenation in this order of the three following subsequences:

3 The Case Where We Have \(k\ge 1\) and \(|A|\ge 3\)

Let \(n\ge k\ge 1\), \(p\ge 3\), and \(A=\{0,1,\cdots ,p-1\}\). We will indicate the construction of a peculiar \(\sigma _k\)-Gray cycle over \(A^n\), namely \( h^{n,k}\). This will be done by applying some induction over \(k\ge 1\): in view of that we set \(n_0=n-k+1\). The starting point corresponds to \(h^{n_0,1}\), the p-ary reflected Gray code over \(A^{n_0}\) as reminded in Sect. 2. For the induction stage, starting with some \(\sigma _{k-1}\)-Gray cycle over \(A^{n-1}\), namely \( h^{n-1,k-1}\), we compute the sequence \( h^{n,k}\) as indicated in what follows: let \(i\in [0,p^{n}-1]\), and let \(q\in [0,p-1]\), \(r\in [0,p^{n-1}-1]\) be the unique pair of non-negative integers such that \(i=qp^{n-1}+r\). We set:

$$\begin{aligned} h^{n,k}_{[i]}=h^{n,k}_{[qp^{n-1}+r]}= \theta ^{q+r}(0) h^{n-1,k-1}_{[r]} \end{aligned}$$
(2)

As illustrated by Example 3, the resulting sequence \( h^{n,k}\) is actually the concatenation in this order of p subsequences namely \(C_0, \dots , C_{p-1}\), with \(C_q=\left( h^{n,k}_{[qp^{n-1}+r]}\right) _{0\le r\le p^{n-1}-1}\), for each \(q\in [0,p-1]\). Since \(\theta \) is one-to-one, given a pair of different integers \(q,q'\in [0,p-1]\), for every \(r\in [0,p^{n-1}-1]\), in each of the subsequences \(C_q\), \(C_{q'}\), the words \(h^{n,k}_{[qp^{n-1}+r]}\) and \(h^{n,k}_{[q'p^{n-1}+r]}\) only differ in their initial characters, which respectively are \(\theta ^{q+r}(0)\) and \(\theta ^{q'+r}(0)\). In addition, since \(h^{n-1,k-1}\) is a \(\sigma _{k-1}\)-Gray cycle over \(A^{n-1}\), we have \(\left| h^{n,k}\right| =p\left| h^{n-1,k-1}\right| =p^n\).

Example 3

Let \(A=\{0,1,2\}\), \(n=3\), \(k=2\), thus \(p=3\), \(n_0=2\). By starting with the sequence \(h^{n-1,k-1}=h^{2,1}\), \( h^{n,k}\) is the concatenation of \(C_0\), \(C_1\), and \(C_2\) :

Proposition 2

\(h^{n,k}\) is a \(\sigma _k\)-Gray cycle over \(A^n\).

Proof

We argue by induction over \(k\ge 1\). With regard to the base case, as indicated above \(h^{n_0,1}\) is the |A|-ary reflected Gray sequence. In view of the induction stage, we assume that the finite sequence \( h^{n-1,k-1}\) is a \(\sigma _{k-1}\)-Gray cycle over \(A^{n-1}\), for some \(k\ge 2\).

  1. (i)

    We start by proving that \(h^{n,k}\) satisfies Condition (G2). This will be done through the three following steps:

    1. (i.i)

      Firstly, we prove that, for each \(q\in [0,p-1]\), in the subsequence \(C_q\) two consecutive terms are necessarily in correspondence under \(\sigma _k\). Given \(r\in [0, p^{n-1}-1]\), by definition, we have \(\theta ^{r+q}(0)\in \sigma _1\left( \theta ^{r+q-1}(0)\right) \). Since \( h^{n-1,k-1}\) satisfies Condition (G2), we have \( h^{n-1,k-1}_{[r]}\in \sigma _{k-1}\left( h^{n-1,k-1}_{[r-1]}\right) \). We obtain \(\theta ^{r+q}(0) h^{n-1,k-1}_{[r]}\in \sigma _k\left( \theta ^{r+q-1}(0) h^{n-1,k-1}_{[r-1]}\right) \), thus according to (2): \(h^{n,k}_{[qp^{n-1}+r]}\in \sigma _k\left( h^{n,k}_{[qp^{n-1}+r-1]}\right) \).

    2. (i.ii)

      Secondly, we prove that, for each \(q\in [1,p-1]\), the last term of \(C_{q-1}\) and the initial term of \(C_{q}\) are also connected by \(\sigma _k\). Take \(r=0\) in Eq. (2): it follows from \(\theta ^{p^{n-1}}=id_A\) that we have \(h^{n,k}_{[qp^{n-1}]}=\theta ^{q}(0) h^{n-1,k-1}_{[0]}=\theta ^{p^{n-1}+q}(0) h^{n-1,k-1}_{[0]}\). In (2) take \(r=p^{n-1}-1\), moreover substitute \(q-1\in [0,p-2]\) to \(q\in [1,p-1]\): we obtain \(h^{n,k}_{[qp^{n-1}-1]}=\theta ^{q+p^{n-1}-2}(0) h^{n-1,k-1}_{[p^{n-1}-1]}\). It follows from \(p=|A|\ge 3\) that \(\theta (0)\ne \theta ^{-2}(0)\): since \(\theta \) is one-to-one this implies \(\theta ^{q+p^{n-1}}(0)\ne \theta ^{q+p^{n-1}-2}(0)\), thus \(\theta ^{q+p^{n-1}}(0)\in \sigma _1\left( \theta ^{q+p^{n-1}-2}(0)\right) \). By induction we have \( h^{n-1,k-1}_{[0]}=\sigma _{k-1}\left( h^{n-1,k-1}_{[p^{n-1}-1]}\right) \), thus \(h^{n,k}_{[qp^{n-1}]}\in \sigma _k\left( \theta ^{q+p^{n-1}-2}(0) h^{n-1,k-1}_{[qp^{n-1}-1]}\right) \) that is, \(h^{n,k}_{[qp^{n-1}]}\in \sigma _k\left( h^{n,k}_{[qp^{n-1}-1]}\right) \).

    3. (i.iii)

      At last, we prove that the first term of \(C_0\) is an image under \(\sigma _k\) of the last term of \(C_{p-1}\). In Eq. (2), take \(q=0\) and \(r=0\): we obtain \(h^{n,k}_{[0]}=0h^{n-1,k-1}_{[0]}\). Similarly, by setting \(q=p-1\) and \(r=p^{n-1}-1\), we obtain \(h^{n,k}_{[(p-1)p^{n-1}+p^{n-1}-1]}=\theta ^{p^{n-1}+p-2}(0) h^{n-1,k-1}_{[p^{n-1}-1]}\), thus \(h^{n,k}_{[p^n-1]}=\theta ^{-2}(0)h^{n-1,k-1}_{[p^{n-1}-1]}\). Since \( h^{n-1,k-1}\) is a \(\sigma _{k-1}\)-Gray cycle over \(A^{n-1}\), we have \( h^{n-1,k-1}_{[0]}\in \sigma _{k-1}\left( h^{n-1,k-1}_{[p^{n-1}-1]}\right) \). In addition, it follows from \(p\ge 3\), that \(\theta ^{-2}(0)\ne 0\), thus \(0\in \sigma _1\left( \theta ^{-2}(0)\right) \). We obtain \( h^{n,k}_{[0]} \in \sigma _k\left( \theta ^{-2}(0)h^{n-1,k-1}_{[p^{n-1}-1]}\right) \), thus \(h^{n,k}_{[0]}\in \sigma _k\left( h^{n,k}_{[p^{n}-1]}\right) \) that is, the required property.

  2. (ii)

    Now, we prove that, in the sequence \( h^{n,k}\) all terms are pairwise different. Let \(i,i'\in [0,p^n-1]\) such that \(h^{n,k}_{[i]}= h^{n,k}_{[i']}\) and consider the unique 4-tuple of integers \(q,q'\in [0,p-1]\), \(r,r'\in [0,p^{n-1}-1]\) such that \(i=qp^{n-1}+r\) and \(i'=q' p^{n-1}+r'\). According to (2) we have \(\theta ^{q+r}(0)h^{n-1,k-1}_{[r]}=\theta ^{q'+r'}(0)h^{n-1,k-1}_{[r']}\), thus \(\theta ^{q+r}(0)=\theta ^{q'+r'}(0)\in A\) and \(h^{n-1,k-1}_{[r]}=h^{n-1,k-1}_{[r']}\). Since \(h^{n-1,k-1}\) satisfies (G3), the second equation implies \(r=r'\), whence the first one implies \(\theta ^{q}(0)=\theta ^{q'}(0)\), thus \(q=q'\bmod p\). Since we have \(q,q'\in [0,p-1]\) we obtain \(q=q'\), thus \(i=i'\).

  3. (iii)

    Finally, since \( h^{n,k}\) satisfies (G3), we have \(\left| \bigcup _{0\le i\le p^{n}-1}\{h^{n,k}_{[i]}\}\right| =p^{n}\), hence \( h^{n,k}\) satisfies Condition (G1).    \(\square \)

4 The Case Where A Is a Binary Alphabet, With k Odd

Let \(A=\{0,1\}\) and \(n\ge k\). Classically, the cyclic permutation \(\theta \), which was introduced in Sect. 2, can be extended into a one-to-one monoid homomorphism onto \(A^*\): in view of this, we set \(\theta (\varepsilon )=\varepsilon \) and, for any non-empty n-tuple of characters \(a_1,\cdots , a_n\in A\), \(\theta (a_1\cdots a_n)=\theta (a_1)\cdots \theta (a_n)\). Trivially, in the case where we have \(n=k\), if a non-empty \(\sigma _k\)-Gray code exists over \(X\subseteq A^n\), then we have \(X=\{x,\theta (x)\}\), for some \(x\in A^n\). In the sequel of the paper, we assume \(n\ge k+1\). In what follows, we indicate the construction of a peculiar pair of \(\sigma _k\)-Gray cycles over \(A^n\), namely \(\gamma ^{n,k}\) and \(\rho ^{n,k}\). This will be done by induction over \(k'\), the unique non-negative integer such that \(k=2k'+1\). Let \(n_0=n-2k'=n-k+1\).

– For the base case, \(\gamma ^{n_0,1}\) and \(\rho ^{n_0,1}\) are computed by applying some reversal (resp., shift) over the sequence \(g^{n_0,1}\) from Sect. 2:

$$\begin{aligned} \gamma ^{n_0,1}_{[0]}=g^{n_0,1}_{[0]}~~\mathrm{and}~~ \gamma ^{n_0,1}_{[i]}=g^{n_0,1}_{[2^{n_0}-i]}~~(1\le i\le 2^{n_0}-1);\end{aligned}$$
(3)
$$\begin{aligned} \rho ^{n_0,k}_{[0]}=g^{n_0,1}_{[2^{n_0}-1]}~~\mathrm{and}~~ \rho ^{n_0,k}_{[i]}=g^{n_0,1}_{[i-1]}~~(1\le i\le 2^{n_0}-1). \end{aligned}$$
(4)

By construction, \(\gamma ^{n_0,1}\) and \(\rho ^{n_0,1}\) are \(\sigma _1\)-Gray cycles over \(A^{n_0}\). Moreover we have:

$$\begin{aligned} \gamma ^{n_0,1}_{[0]}=g^{n_0,1}_{[0]}=0^{n_0}~~\mathrm{and}~~\rho ^{n_0,1}_{[0]}=g^{n_0,1}_{[2^{n_0}-1]}=10^{n_0-1};\end{aligned}$$
(5)
$$\begin{aligned} \gamma ^{n_0,1}_{[2^{n_0}-1]}=g^{n_0,1}_{[1]}=0^{n_0-1}1~~\mathrm{and}~~\rho ^{n_0,1}_{[2^{n_0}-1]}=g^{n_0,1}_{[2^{n_0}-2]}=10^{n-2}1. \end{aligned}$$
(6)

Example 4

For \(n_0=3\) we obtain the following sequences:

– In view of the induction step, we assume that we have computed the \(\sigma _k\)-Gray cycles \(\gamma ^{n,k}\) and \(\rho ^{n,k}\). Notice that we have \(n+2=n_0+2(k'+1)=n_0+(k+2)-1\): below we explain the construction of the two corresponding \(2^{n+2}\)-term sequences \(\gamma ^{n+2,k+2}\) and \(\rho ^{n+2,k+2}\). Let \(i\in [0,2^{n+2}-1]\), and let \(q\in [0,3]\), \(r\in [0,2^{n}-1]\) be the unique pair of integers such that \(i=q2^n+r\). Since we have \(r\in [0,2^{n}-1]\), taking for q the value \(q=0\) (resp., 1, 2, 3), we state the corresponding equation (7a) (resp., (7b),(7c),(7d)):

$$\begin{aligned} \gamma ^{n+2,k+2}_{[r]}=\theta ^{r}(00)\gamma ^{n,k}_{[r]}; \end{aligned}$$
(7a)
$$\begin{aligned} \gamma ^{n+2,k+2}_{[2^n+r]}=\theta ^{r}(01)\rho ^{n,k}_{[r]}; \end{aligned}$$
(7b)
$$\begin{aligned} \gamma ^{n+2,k+2}_{[2.2^n+r]}=\theta ^{r}(11)\gamma ^{n,k}_{[r]}; \end{aligned}$$
(7c)
$$\begin{aligned} \gamma ^{n+2,k+2}_{[3.2^n+r]}=\theta ^{r}(10)\rho ^{n,k}_{[r]}. \end{aligned}$$
(7d)

Similarly the sequence \(\rho ^{n+2,k+2}\) is computed by substituting, in the preceding equations, the 4-tuple (10, 11, 01, 00) to (00, 01, 11, 10):

$$\begin{aligned} \rho ^{n+2,k+2}_{[r]}=\theta ^{r}(10)\gamma ^{n,k}_{[r]}; \end{aligned}$$
(8a)
$$\begin{aligned} \rho ^{n+2,k+2}_{[2^n+r]}=\theta ^{r}(11)\rho ^{n,k}_{[r]}; \end{aligned}$$
(8b)
$$\begin{aligned} \rho ^{n+2,k+2}_{[2.2^n+r]}=\theta ^{r}(01)\gamma ^{n,k}_{[r]}; \end{aligned}$$
(8c)
$$\begin{aligned} \rho ^{n+2,k+2}_{[3.2^n+r]}=\theta ^{r}(00)\rho ^{n,k}_{[r]}. \end{aligned}$$
(8d)

Example 5

(Example 4 continued) \(\gamma ^{5,3}\) is the concatenation, in this order, of the 4 following subsequences:

Lemma 2

\(\gamma ^{n,k}\) and \(\rho ^{n,k}\) satisfy both the conditions (G1) and (G3).

Proof

We argue by induction over \(k'\ge 0\), with \(k=2k'+1\). the base case corresponds to \(k'=0\) that is, \(k=1\) and \(n=n_0\): as indicated above, \(\gamma ^{n_0,1}\) and \(\rho ^{n_0,1}\) are \(\sigma _1\)-Gray cycles over \(A^{n}\). In view of the induction step we assume that, for some \(k'\ge 0\), both the sequences \(\gamma ^{n,k}\) and \(\rho ^{n,k}\) are \(\sigma _k\)-Gray cycles over \(A^n\).

  1. (i)

    In order to prove that \(\gamma ^{n+2,k+2}\) satisfies Condition (G3), let \(i,i'\in [0,2^{n+2}-1]\) such that \(\gamma ^{n+2,k+2}_{[i]}=\gamma ^{n+2,k+2}_{[i']}\), and \(q,q'\in [0,3]\), \(r,r'\in [0,2^n-1]\) such that \(i=q2^n+r\), \(i'=q'2^n+r'\). According to Eqs. (7a)–(7d), words \(x,x'\in A^2\), \(w,w'\in A^n\) exist such that \(\gamma ^{n+2,k+2}_{[i]}=\theta ^r(x)w\) and \(\gamma ^{n+2,k+2}_{[i']}=\theta ^{r'}(x')w'\) that is, \(\theta ^r(x)=\theta ^{r'}(x')\in A^2\) and \(w=w'\). By the definition of \(\theta \), this implies either \(x,x'\in \{00,11\}\) or \(x,x'\in \{01,10\}\) that is, by construction, either \(q,q'\in \{0,2\}\), \(x,x'\in \{00,11\}\), \(w=\gamma ^{n,k}_{[r]}=\gamma ^{n,k}_{[r']}\), or \(q,q'\in \{1,3\}\), \(x,x'\in \{01,10\}\), \(w=\rho ^{n,k}_{[r]}=\rho ^{n,k}_{[r']}\). Since \(\gamma ^{n,k}\) and \(\rho ^{n,k}\) satisfies (G3), in any case we have \(r=r'\). This implies \(\theta ^r(x)=\theta ^{r}(x')\), thus \(x=x'\). With regard to Eqs. (7a)–(7d), this corresponds to \(q=q'\), thus \(i=q2^n+r=q'2^n+r=i'\), therefore \(\gamma ^{n+2,k+2}\) satisfies Condition (G3).

  2. (ii)

    By substituting (10, 11, 01, 00) to (00, 01, 11, 10), according to (8a)–(8d), similar arguments prove that \(\rho ^{n+2,k+2}_{[i]}=\rho ^{n+2,k+2}_{[i']}\) implies \(i=i'\), thus \(\rho ^{n+2,k+2}\) also satisfies (G3).

  3. (iii)

    Since \(\gamma ^{n+2,k+2}\) satisfies (G3), we have \(\bigcup _{0\le i\le 2^{n+2}-1}\{\gamma ^{n+2,k+2}_i\}=A^{n+2}\), hence it satisfies (G1). Similarly, since \(\rho ^{n+2,k+2}\) satisfies (G3) it satisfies (G1).    \(\square \)

In order to prove that our sequences satisfy (G2), we prove the following property:

Lemma 3

We have \(\gamma ^{n,k}_{[0]}\in \sigma _{k+1}\left( \rho ^{n,k}_{[2^{n}-1]}\right) \) and \(\rho ^{n,k}_{[0]}\in \sigma _{k+1}\left( \gamma ^{n,k}_{[2^{n}-1]}\right) .\)

Proof

We argue by induction over the integer \(k'\ge 0\). The case \(k'=0\) corresponds to \(k=1\) and \(n=n_0\): with such a condition, our property comes from the identities (5) and (6). For the induction step, we assume that, for some \(k'\ge 0\), we have \(\gamma ^{n,k}_{[0]}\in \sigma _{k+1}\left( \rho ^{n,k}_{[2^{n}-1]}\right) \) and \(\rho ^{n,k}_{[0]}\in \sigma _{k+1}\left( \gamma ^{n,k}_{[2^{n}-1]}\right) \).

  1. (i)

    In (7a), by taking \(r=0\) we obtain \(\gamma ^{n+2,k+2}_{[0]}=00\gamma ^{n,k}_{[0]}\), hence by induction: \(\gamma ^{n+2,k+2}_{[0]} \in 00\sigma _{k+1}\left( \rho ^{n,k}_{[2^{n}-1]}\right) \subseteq \sigma _{k+3}\left( 11\rho ^{n,k}_{[2^{n}-1]}\right) \). By setting \(r=2^{n}-1\) in (8d), we obtain \(\rho ^{n+2,k+2}_{[2^{n+2}-1]}=11\rho ^{n,k}_{[2^{n}-1]}\), thus \(\gamma ^{n+2,k+2}_{[0]}\in \sigma _{k+3}\left( \rho ^{n+2,k+2}_{[2^{n+2}-1]}\right) \).

  2. (ii)

    Similarly, by setting \(r=0\) in (8a), and by induction we have: \(\rho ^{n+2,k+2}_{[0]}=10\gamma ^{n,k}_{[0]}\in \sigma _{k+3}\left( 01\rho ^{n,k}_{[2^{n}-1]}\right) \). By taking \(r=2^n-1\) in (7d) we obtain \(\gamma ^{n+2,k+2}_{[2^{n+2}-1]}=01\rho ^{n,k}_{[2^n-1]}\), thus \(\rho ^{n+2,k+2}_{[0]}\in \sigma _{k+3}\left( \gamma ^{n+2,k+2}_{[2^{n+2}-1]} \right) \).

   \(\square \)

Since Eqs. (7a)–(8d) look alike, one may be tempted to compress them thanks to some unique generic formula. Based on our tests, such a formula needs to introduce at least two additionnal technical parameters, which would make their handling tedious. In the proof of the following result, we have opted to report some case-by-case basis argumentation: this has the advantage of making use of arguments which, although being similar, actually are easy to read.

Proposition 3

Both the sequences \(\gamma ^{n,k}\) and \(\rho ^{n,k}\) are \(\sigma _k\)-Gray cycles over \(A^n\).

Proof sketch Once more we argue by induction over \(k'\ge 0\). Since \(\gamma ^{n_0,1}\) and \(\rho ^{n_0,1}\) are \(\sigma _1\)-Gray cycles over \(A^n\), the property holds for \(k'=0\). In view of the induction stage, we assume that, for some \(k'\ge 0\) both the sequences \(\gamma ^{n,k}\) and \(\rho ^{n,k}\) are \(\sigma _k\)-Gray cycles over \(A^n\). According to Lemma 1, it remains to establish that \(\gamma ^{n+2,k+2}\) and \(\rho ^{n+2,k+2}\) satisfy Condition (G2) that is:

$$\begin{aligned} (\forall q\in \{0,1,2,3\})(\forall r\in [1,2^n-1]) ~\gamma ^{n+2,k+2}_{[q2^n+r]}\in \sigma _{k+2}\left( \gamma ^{n+2,k+2}_{[q2^n+r-1]}\right) ; \end{aligned}$$
(9)
$$\begin{aligned} (\forall q\in \{1,2,3\})~\gamma ^{n+2,k+2}_{[q2^{n}]}\in \sigma _{k+2}\left( \gamma ^{n+2,k+2}_{[q2^{n}-1]}\right) ; \end{aligned}$$
(10)
$$\begin{aligned} \gamma ^{n+2,k+2}_{[0]}\in \sigma _{k+2}\left( \gamma ^{n+2,k+2}_{[2^{n+2}-1]}\right) . \end{aligned}$$
(11)
$$\begin{aligned} (\forall q\in \{0,1,2,3\})(\forall r\in [1,2^n-1]) ~\rho ^{n+2,k+2}_{[q2^n+r]}\in \sigma _{k+2}\left( \rho ^{n+2,k+2}_{[q2^n+r-1]}\right) ; \end{aligned}$$
(12)
$$\begin{aligned} (\forall q\in \{1,2,3\})~\rho ^{n+2,k+2}_{[q2^{n}]}\in \sigma _{k+2}\left( \rho ^{n+2,k+2}_{[q2^{n}-1]}\right) ; \end{aligned}$$
(13)
$$\begin{aligned} \rho ^{n+2,k+2}_{[0]}\in \sigma _{k+2}\left( \rho ^{n+2,k+2}_{[2^{n+2}-1]}\right) . \end{aligned}$$
(14)

Condition (9). (i) At first assume \(q=0\). According to (7a), and since \(\gamma ^{n,k}\) satisfies (G2), we have \(\gamma ^{n+2,k+2}_{[r]}= \theta ^{r}(00)\gamma ^{n,k}_{[r]}\in \theta ^{r}(00)\sigma _k\left( \gamma ^{n,k}_{[r-1]}\right) \), thus \(\gamma ^{n+2,k+2}_{[r]}\in \sigma _{k+2}\left( \theta ^{r-1}(00)\gamma ^{n,k}_{[r-1]}\right) \). In (7a), substitute \(r-1\) to r (we have \(0\le r-1\le 2^n-2\)): we obtain \(\gamma ^{n+2,k+2}_{[r-1]}=\theta ^{r-1}(00)\gamma ^{n,k}_{[r-1]}\), thus \(\gamma ^{n+2,k+2}_{[r]}\in \sigma _{k+2}\left( \gamma ^{n+2,k+2}_{[r-1]}\right) \).

(ii) Now assume \(q=1\). According to (7b), and since \(\rho ^{n,k}\) satisfies (G2), we have \(\gamma ^{n+2,k+2}_{[2^n+r]}= \theta ^{r}(01)\rho ^{n,k}_{[r]}\in \sigma _{k+2}\left( \theta ^{r-1}(01)\rho ^{n,k}_{[r-1]}\right) \). In (7b), substitute \(r-1\) to r: we obtain \(\gamma ^{n+2,k+2}_{[2^n+r-1]}= \theta ^{r-1}(01)\rho ^{n,k}_{[r-1]}\), thus \(\gamma ^{n+2,k+2}_{[2^n+r]}\in \sigma _{k+2}\left( \gamma ^{n+2,k+2}_{[2^n+r-1]}\right) \).

(iii) For \(q=2\), the arguments are similar to those applied in (i) by substituting \(\gamma ^{n+2,k+2}_{[2\cdot 2^n+r]}\) to \(\gamma ^{n+2,k+2}_{[r]}\), Eq. (7c) to (7a), and 11 to 00.

(iv) Similarly, for \(q=3\) the proof is obtained by substituting in (ii) \(\gamma ^{n+2,k+2}_{[3\cdot 2^{n}+r]}\) to \(\gamma ^{n+2,k+2}_{[2^n+r]}\), (7d) to (7b), and 10 to 01.

Condition (10). (i) Assume \(q=1\) and take \(r=0\) in (7b). According to Lemma 2, we obtain \(\gamma ^{n+2,k+2}_{[2^n]}=01\rho ^{n,k}_{[0]}\in 01\sigma _{k+1}\left( \gamma ^{n,k}_{[2^n-1]}\right) \subseteq \sigma _{k+2}\left( 11\gamma ^{n,k}_{[2^n-1]}\right) \). Take \(r=2^n-1\) in (7a): we obtain \(\gamma ^{n+2,k+2}_{[2^n-1]}=11\gamma ^{n,k}_{[2^n-1]}\), thus \(\gamma ^{n+2,k+2}_{[2^n]}\in \sigma _{k+2}\left( \gamma ^{n+2,k+2}_{[2^n-1]}\right) \).

(ii) Now, assume \(q=2\), and set \(r=0\) in Eq. (7c). According to Lemma 2 we have \(\gamma ^{n+2,k+2}_{[2\cdot 2^n]}=11\gamma ^{n,k}_{[0]}\in 11\sigma _{k+1}\left( \rho ^{n,k}_{[2^n-1]}\right) \subseteq \sigma _{k+2}\left( 10\rho ^{n,k}_{[2^n-1]}\right) \). By taking \(r=2^n-1\) in (7b), we obtain \(\gamma ^{n+2,k+2}_{[2\cdot 2^n-1)]}=10\rho ^{n,k}_{[2^n-1]}\), thus \(\gamma ^{n+2,k+2}_{[2\cdot 2^n]}\in \sigma _{k+2}\left( \gamma ^{n+2,k+2}_{[2\cdot 2^n-1]}\right) \).

(iii) For \(q=3\), substitute in (i) \(\gamma ^{n+2,k+2}_{[3\cdot 2^n]}\) to \(\gamma ^{n+2,k+2}_{[2^n]}\), Eq. (7d) to Eq. (7b), (7c) to (7a), 10 to 01 and 00 to 11: similar arguments prove that \(\gamma ^{n+2,k+2}_{[3\cdot 2^n]}\in \sigma _{k+2}\left( \gamma ^{n+2,k+2}_{[3\cdot 2^n-1]}\right) \).

Condition (11). Take \(r=0\) in (7a). According to Lemma 2, we have \(\gamma ^{n+2,k+2}_{[0]}=00\gamma ^{n,k}_{[0]}\in 00\sigma _{k+1}\left( \rho ^{n,k}_{[2^n-1]}\right) \subseteq \sigma _{k+2}\left( 01\rho ^{n,k}_{[2^n-1]}\right) \). By taking \(r=2^n-1\) in (7d) we obtain \(\gamma ^{n,k}_{[2^{n+2}-1]}=01\rho ^{n,k}_{[2^n-1]}\), thus \(\gamma ^{n+2,k+2}_{[0]}\in \sigma _{k+2}\left( \gamma ^{n,k}_{[2^{n+2}-1]}\right) \).

According to the structures of Eqs. (8a)–(8d), for proving the conditions (12)–(13), the method consists in substituting the word \(\rho ^{n+2,k+2}_{[r]}\) to \(\gamma ^{n+2,k+2}_{[r]}\), the 4-uple (10, 11, 01, 00) to (00, 01, 11, 10), and Eq. (8a) (resp., (8b), (8c), (8d)) to Eq. (7a) (resp., (7b), (7c), (7d)).    \(\Box \)

5 The Case Where We Have \(|A|=2\) and k Even

Beforehand, we recall some classical algebraic interpretation of the substitution \(\sigma _k\) in the framework of the binary alphabet \(A=\{0,1\}\). Denote by \(\oplus \) the addition in the group \({\mathbb Z}/2{\mathbb Z}\) with identity 0. Given a positive integer n, and \(w,w'\in A^n\), define \(w\oplus w'\) as the unique word of \(A^n\) such that, for each \(i\in [1,n]\): \((w\oplus w')_i=w_i\oplus w'_i\). With this notation the sets \(A^n\) and \(({\mathbb Z}/2{\mathbb Z})^n\) are in one-to-one correspondence. Moreover we have \(w'\in \sigma _k(w)\) if, and only if, some word \(u\in A^n\) exists such that \(|u|_1=k\) and \(w=w' {\oplus } u\), therefore if k is even we have \(|w|_1=|w'|_1\bmod 2\). Consequently, given a \(\sigma _k\)-Gray cycle \(\left( \alpha _{[i]}\right) _{0\le i\le m}\), for each \(i\in [0,m]\) we have \(\left| \alpha _{[i]}\right| _1=\left| \alpha _{[0]}\right| _1\bmod 2\). As a corollary, setting \(\mathrm{Even}_1^n=\{w\in A^*:|w|_1=0 \bmod 2\}\) and \(\mathrm{Odd}_1^n=\{w\in A^*:|w|_1=1\bmod 2\}\):

Lemma 3 With the condition of Sect. 5, given a \(\sigma _k\)-Gray cycle \(\alpha \) over X, either we have \(X\subseteq \mathrm{Even}_1^n\), or we have \(X\subseteq \mathrm{Odd}_1^n\).

Since \(k-1\) is an odd integer, according to Proposition 2, the sequence \(\gamma ^{n-1,k-1}\) is a \(\sigma _{k-1}\)-Gray cycle over \(A^{n-1}\). We set:

$$\begin{aligned} (\forall i\in [0, 2^{n-1}-1])~~\gamma ^{n,k}_{[i]}=\theta ^i(0)\gamma ^{n-1,k-1}_{[i]}~~\mathrm{and} ~~\underline{\gamma }^{n,k}_{[i]}=\theta ^i(1)\gamma ^{n-1,k-1}_{[i]} \end{aligned}$$
(15)

For instance, we have \(\gamma ^{6,4}_{[0]}=0 00 000\), \(\underline{\gamma }^{6,4}_{[0]}=1 00 000\), \(\gamma ^{6,4}_{[1]}=111100\).

Proposition 3 \(\gamma ^{n,k}\) (resp., \(\underline{\gamma }^{n,k}\)) is a \(\sigma _k\)-Gray cycle over \(\mathrm{Even}_1^{n}\) (resp., \(\mathrm{Odd}^{n}_1\)).

Proof

  1. (i)

    According to Eq. (15), since \(\gamma ^{n-1,k-1}\) satisfies (G3), both the sequences \(\gamma ^{n,k}\) and \(\underline{\gamma }^{n,k}\) also satisfy (G3).

  2. (ii)

    By Lemma 3, we have \(\bigcup _{0\le i\le 2^{n}-1}\left\{ \gamma ^{n,k}\right\} \subseteq \mathrm{Even}_1^{n}\) and \(\bigcup _{0\le i\le 2^{n}-1}\left\{ \underline{\gamma }^{n,k}\right\} \subseteq \mathrm{Odd}_1^{n}\). In addition, according to (15), we have \(\left| \gamma ^{n,k}\right| =\left| \underline{\gamma }^{n,k}\right| =\left| \gamma ^{n-1,k-1}\right| =2^{n-1}\). This implies \(\bigcup _{0\le i\le 2^{n}-1}\left\{ \gamma ^{n,k}\right\} = \mathrm{Even}_1^{n}\) and \(\bigcup _{0\le i\le 2^{n}-1}\left\{ \underline{\gamma }^{n,k}\right\} = \mathrm{Odd}_1^{n}\) that is, \(\gamma ^{n,k}\) and \(\underline{\gamma }^{n,k}\) satisfy (G1).

  3. (iii)

    Let \(i\in [1, 2^{n-1}-1]\). Since \(\gamma ^{n-1,k-1}\) satisfies (G2), we have \(\gamma ^{n-1,k-1}_{[i]}\in \sigma _{k-1}\left( \gamma ^{n-1,k-1}_{[i-1]}\right) \). According to (15), the initial characters of \(\gamma ^{n,k}_{[i]}\) and \(\gamma ^{n,k}_{[i-1]}\) (resp., \(\underline{\gamma }^{n,k}_{[i]}\) and \(\underline{\gamma }^{n,k}_{[i-1]}\)) are different, hence we have \(\gamma ^{n,k}_{[i]}\in \sigma _k\left( \gamma ^{n,k}_{[i-1]}\right) \) and \(\underline{\gamma }^{n,k}_{[i]}\in \sigma _k\left( \underline{\gamma }^{n,k}_{[i-1]}\right) \). In addition, once more according to (15) it follows from \(\gamma ^{n-1,k-1}_{[0]}\in \sigma _{k-1}\left( \gamma ^{n-1,k-1}_{[2^{n-1}-1]}\right) \) that \(\gamma ^{n,k}_{[0]}=0\gamma ^{n-1,k-1}_{[0]}\in \sigma _{k}\left( 1\gamma ^{n-1,k-1}_{[2^{n-1}-1]}\right) \subseteq \sigma _k\left( \gamma ^{n,k}_{[2^{n-1}-1]}\right) \), hence \(\gamma ^{n,k}\) satisfies (G2). Similarly, \(\underline{\gamma }^{n-1,k-1}_{[0]}\in \sigma _{k-1}\left( \underline{\gamma }^{n-1,k-1}_{[2^{n-1}-1]}\right) \) implies \(\underline{\gamma }^{n,k}_{[0]}\in \sigma _k\left( \gamma ^{n,k}_{[2^{n-1}-1]}\right) \), hence \(\underline{\gamma }^{n,k}\) satisfies (G2).

   \(\square \)

The following statement provides the description of the complexity \(\lambda _{A,\tau }\):

Theorem 1 Given a finite alphabet A and \(n\ge k\ge 1\), exactly one of the four following properties holds:

  1. (i)

    \(|A|\ge 3\), \(n\ge k\), and \(\lambda _{A,\sigma _k}(n)=|A|^n\);

  2. (ii)

    \(|A|=2\), \(n=k\), and \(\lambda _{A,\sigma _k}(n)=2\);

  3. (iii)

    \(|A|=2\), \(n\ge k+1\), k is odd and \(\lambda _{A,\sigma _k}(n)=2^n\);

  4. (iv)

    \(|A|=2\), \(n\ge k+1\), k is even, and \(\lambda _{A,\sigma _k}(n)=2^{n-1}\).

In addition, in each case some \(\sigma _k\)-Gray cycle of maximum length can be explicitly computed.

Proof

Recall that if some \(\sigma _k\)-Gray cycle exists over \(X\subseteq A^{\le n}\), necessarily X is a uniform set that is, \(X\subseteq A^m\) holds for some \(m\le n\); hence, in any case we have \(\lambda _{A,\sigma _k}(n)\le |A|^n\). According to Proposition 1, if we have \(|A|\ge 3\) and \(n\ge k\), a \(\sigma _k\)-Gray cycle exists over \(A^n\), hence Property (i) holds. Similarly, (iii) comes from Proposition 2. As indicated in the preamble of Sect. 3, Property (ii) trivially holds. Finally, according to Lemma 3, given a binary alphabet A, if k is even we have \(\lambda _{A,\sigma _k}(n)\le 2^{n-1}\), hence (iv) comes from Proposition 3.    \(\square \)

Further Development. Since our Gray cycles were constructed by applying recursive processes, it is legitimate to ask whether some method could exist for computing \(\gamma ^{n,k}_{[i]}\) by directly starting with \(\gamma ^{n,k}_{[i-1]}\), as in the case of the classical reflected Gray cycles. In view of some of our more recent studies, we strongly believe that such algorithms can actually be devised: we hope to develop this point in a further paper.

On the other hand, it could be of interest to study the behaviour of \(\varLambda _{A,\tau }\) in the framework of other word binary relations \(\tau \), even in restraining to special families of sets \(X\subseteq A^*\), such as variable-length codes.