---
abstract: |
    A Gray code for a combinatorial class is a method for listing the objects in the class so that successive objects differ in some prespecified, small way, typically expressed as a bounded Hamming distance. In a previous work, the authors of the present paper showed, among other things, that the $m$-ary Reflected Gray Code Order yields a Gray code for the set of restricted growth functions. Here we further investigate variations of this order relation, and give the first Gray codes and efficient generating algorithms for bounded restricted growth functions.

    [**Keywords:**]{} [*[Gray code (order), restricted growth function, generating algorithm]{}*]{}
author:
- Ahmad Sabri
- Vincent Vajnovszki
title: 'More restricted growth functions: Gray codes and exhaustive generations'
---

Introduction
============

In [@SV] the authors shown that both the order relation induced by the generalization of the Binary Reflected Gray Code and one of its suffix partitioned version yield Gray codes on some sets of restricted integer sequences, and in particular for restricted growth functions. These results are presented in a general framework, where the restrictions are defined by means of statistics on integer sequences.

In the present paper we investigate two prefix partitioning order relations on the set of [*bounded*]{} restricted growth functions: as in [@SV], the original Reflected Gray Code Order on $m$-ary sequences, and a new order relation which is an appropriate modification of the former one. We show that, according to the parity of the imposed bound, one of these order relations gives a Gray code on the set of bounded restricted growth functions. As a byproduct, we obtain a Gray code for restricted growth functions with a specified odd value for the largest entry; the case of an even value of the largest entry remains an open problem. In the final part we present the corresponding exhaustive generating algorithms. A preliminary version of these results were presented at The Japanese Conference on Combinatorics and its Applications in May 2016 in Kyoto [@SV_Jap].

Notation and definitions
========================

A [*restricted growth function*]{} of length $n$ is an integer sequence ${\boldsymbol{s}}=s_1s_2\ldots s_n$ with $s_1=0$ and $0\leq s_{i+1}\leq \max\{s_j\}_{j=1}^i+1$, for all $i$, $1\leq i\leq n-1$. We denote by $R_n$ the set of length $n$ restricted growth functions, and its cardinality is given by the $n$th Bell number (sequence [A000110]{} in [@sloa]), with the exponential generating function $e^{e^x}-1$. And length $n$ restricted growth functions encode the partitions of an $n$-set.

For an integer $b\geq 1$, let $R_n(b)$ denote the set of [*$b$-bounded*]{} sequences in $R_n$, that is, $$R_n(b)=\{s_1s_2\ldots s_n\in R_n\,:\, \max\{s_i\}_{i=1}^n\leq b\},$$ and $$R_n^{*}(b)=\{s_1s_2\ldots s_n\in R_n\,:\, \max\{s_i\}_{i=1}^n= b\}.$$ See Table \[Tb1\] for an example.

  ---- ----------- -- ----- ----------- --------- ----- ----------------------------------------------------------------------------------------------------- --
    1.  0 0 0 0 0       15.  0 1 0 0 0   [*3*]{}    29.                                      **[0 1 1 1 2]{} & [*1*]{}\                                       
                                                               2. & 0 0 0 0 1 & [*1*]{} & 16. & 0 1 0 0 1 & [*1*]{} & 30. & **[0 1 1 2 2]{} & [*1*]{}\        
                                                            3. & 0 0 0 1 0 & [*2*]{} & 17. & **[0 1 0 0 2]{} & [*1*]{} & 31. & **[0 1 1 2 1]{} & [*1*]{}\     
                                                               4. & 0 0 0 1 1 & [*1*]{} & 18. & 0 1 0 1 0 & [*2*]{} & 32. & **[0 1 1 2 0]{} & [*1*]{}\        
                                                            5. &**[0 0 0 1 2]{} & [*1*]{} & 19. & 0 1 0 1 1 & [*1*]{} & 33. & **[0 1 2 2 0]{} & [*1*]{}\      
                                                            6. & 0 0 1 0 0 & [*3*]{} & 20. & **[0 1 0 1 2]{} & [*1*]{} & 34. & **[0 1 2 2 1]{} & [*1*]{}\     
                                                            7. & 0 0 1 0 1 & [*1*]{} & 21. & **[0 1 0 2 2]{} & [*1*]{} & 35. & **[0 1 2 2 2]{} & [*1*]{}\     
                                                         8. &**[0 0 1 0 2]{} & [*1*]{} & 22. & **[0 1 0 2 1]{} & [*1*]{} & 36. & **[0 1 2 1 2]{} & [*1*]{}\   
                                                            9. & 0 0 1 1 0 & [*2*]{} & 23. & **[0 1 0 2 0]{} & [*1*]{} & 37. & **[0 1 2 1 1]{} & [*1*]{}\     
                                                              10. & 0 0 1 1 1 & [*1*]{} & 24. & 0 1 1 0 0 & [*2*]{} & 38. & **[0 1 2 1 0]{} & [*1*]{}\        
                                                            11. &**[0 0 1 1 2]{} & [*1*]{} & 25. & 0 1 1 0 1 & [*1*]{} & 39. & **[0 1 2 0 2]{} & [*2*]{}\     
                                                         12. &**[0 0 1 2 2]{} & [*1*]{} & 26. & **[0 1 1 0 2]{} & [*1*]{} & 40. & **[0 1 2 0 1]{} & [*1*]{}\  
                                                            13. &**[0 0 1 2 1]{} & [*1*]{} & 27. & 0 1 1 1 0 & [*2*]{} & 41. & **[0 1 2 0 0]{} & [*1*]{}\     
                                                                          14. &**[0 0 1 2 0]{} & [*1*]{} & 28. & 0 1 1 1 1 & [*1*]{} & & &\                   
                                                                                 **************************************************                           
  ---- ----------- -- ----- ----------- --------- ----- ----------------------------------------------------------------------------------------------------- --

  : \[Tb1\] The set $R_5(2)$, and in bold-face the set $R^*_5(2)$. Sequences are listed in $\preccdot$ order (see Definition \[de:coRGCorder\]) and in italic is the Hamming distance between consecutive sequences.

If a list of same length sequences is such that the Hamming distance between successive sequences (that is, the number of positions in which the sequences differ) is bounded from above by a constant, independent on the sequences length, then the list is said to be a [*Gray code*]{}. When we want to explicitly specify this constant, say $d$, then we refer to such a list as a [*$d$-Gray code*]{}; in addition, if the positions where the successive sequences differ are adjacent, then we say that the list is a [*$d$-adjacent Gray code*]{}.

The next two definitions give order relations on the set of $m$-ary integer sequences of length $n$ on which our Gray codes are based.

\[de:RGCorder\] Let $m$ and $n$ be positive integers with $m\geq 2$. The [*Reflected Gray Code Order*]{} $\prec$ on $\{0,1,\ldots,m-1\}^n$ is defined as: ${\boldsymbol{s}}=s_1s_2\ldots s_n$ is less than ${\boldsymbol{t}}=t_1t_2\ldots t_n$, denoted by ${\boldsymbol{s}}\prec{\boldsymbol{t}}$, if

either $\sum_{i=1}^{k-1} s_i$ is even and $s_k<t_k$, or $\sum_{i=1}^{k-1} s_i$ is odd and $s_k>t_k$

for some $k$ with $s_i=t_i$ ($1\leq i\leq k-1$) and $s_k\neq t_k$.

This order relation is the natural extension to $m$-ary sequences of the order induced by the Binary Reflected Gray Code introduced in [@Gray]. See for example [@BBPSV; @SV] where this order relation and its variations are considered in the context of factor avoiding words and of statistic-restricted sequences.

\[de:coRGCorder\] Let $m$ and $n$ be positive integers with $m\geq 2$. The [*co-Reflected Gray Code Order*]{}[^1] $\preccdot$ on $\{0,1,\ldots,m-1\}^n$ is defined as: ${\boldsymbol{s}}=s_1s_2\ldots s_n$ is less than ${\boldsymbol{t}}=t_1t_2\ldots t_n$, denoted by ${\boldsymbol{s}}\preccdot\ {\boldsymbol{t}}$, if

either $U_k$ is even and $s_k<t_k$, or $U_k$ is odd and $s_k>t_k$

for some $k$ with $s_i=t_i$ ($1\leq i\leq k-1$) and $s_k\neq t_k$ where $
U_k= | \{i\in \{1,2,\ldots,k-1\}:s_i\neq 0, s_i \mbox{ is even}\}|$.

See Table \[Tb\_co\] for an example.

For a set $S$ of same length integer sequences the [*$\prec$-first*]{} (resp. [*$\prec$-last*]{}) sequence in $S$ is the first (resp. last) sequence when the set is listed in $\prec$ order; and [*$\preccdot$-first*]{} and [*$\preccdot$-last*]{} are defined in a similar way. And for sequence ${\boldsymbol{u}}$, $ {\boldsymbol{u}}\,|\,S$ denotes the subset of $S$ of sequences having prefix ${\boldsymbol{u}}$.

Both order relations, Reflected and co-Reflected Gray Code Order produce prefix partitioned lists, that is to say, if a set of sequences is listed in one of these order relations, then the sequences having a common prefix are consecutive in the list.

  ---- ------- ----- ------- ----- -------
   1.   0 0 0   10.   1 0 0   19.   2 2 0
   2.   0 0 1   11.   1 0 1   20.   2 2 1
   3.   0 0 2   12.   1 0 2   21.   2 2 2
   4.   0 1 0   13.   1 1 0   22.   2 1 2
   5.   0 1 1   14.   1 1 1   23.   2 1 1
   6.   0 1 2   15.   1 1 2   24.   2 1 0
   7.   0 2 2   16.   1 2 2   25.   2 0 2
   8.   0 2 1   17.   1 2 1   26.   2 0 1
   9.   0 2 0   18.   1 2 0   27.   2 0 0
  ---- ------- ----- ------- ----- -------

  : \[Tb\_co\] The set $\{0,1,2\}^3$ listed in $\preccdot$ order.

The Gray codes
==============

In this section we show that the set $R_n(b)$, with $b$ odd, listed in $\prec$ order is a Gray code. However, $\prec$ does not induce a Gray code when $b$ is even: the Hamming distance between two consecutive sequences can be arbitrary large for large enough $n$. To overcome this, we consider $\preccdot$ order instead of $\prec$ order when $b$ is even, and we show that the obtained list is a Gray code.

In the proof of Theorem \[k\_odd\] below we need the following propositions which give the forms of the last and first sequence in $R_n(b)$ having a certain fixed prefix, when sequences are listed in $\prec$ order.

\[pro:pro\_Rb\_odd1\] Let $b\geq1$ and odd, $k\leq n-2$ and ${\boldsymbol{s}}=s_1\ldots s_{k}$. If ${\boldsymbol{t}}$ is the $\prec$-last sequence in ${\boldsymbol{s}}\,|\,R_n(b)$, then ${\boldsymbol{t}}$ has one of the following forms:

1.  ${\boldsymbol{t}}={\boldsymbol{s}}M0\ldots0$ if $\sum_{i=1}^{k} s_i$ is even and $M$ is odd,

2.  ${\boldsymbol{t}}={\boldsymbol{s}}M(M+1)0\ldots0$ if $\sum_{i=1}^{k} s_i$ is even and $M$ is even,

3.  ${\boldsymbol{t}}={\boldsymbol{s}}0\ldots0$ if $\sum_{i=1}^{k} s_i$ is odd,

where $M=\min\{b,\max\{s_i\}_{i=1}^k+1\}$.

Let ${\boldsymbol{t}}=s_1\dots s_kt_{k+1}\ldots t_n$ be the $\prec$-last sequence in ${\boldsymbol{s}}\,|\,R_n(b)$.

Referring to the definition of $\prec$ order in Definition \[de:RGCorder\], if $\sum_{i=1}^{k} s_i$ is even, then $t_{k+1}=\min\{b,\max\{s_i\}_{i=1}^k+1\}= M$, and based on the parity of $M$, two cases can occur.

-   If $M$ is odd, then we have that $\sum_{i=1}^k s_i+t_{k+1}=\sum_{i=1}^{k} s_i+M$ is odd, thus $t_{k+2}\ldots t_n=0\ldots 0$, and we retrieve the form prescribed by the first point of the proposition.

-   If $M$ is even, then $M\neq b$ and $\sum_{i=1}^k s_i+t_{k+1}=\sum_{i=1}^{k} s_i+M$ is even, thus $t_{k+2}=\max\{s_1,\ldots, s_k,t_{k+1}\}+1=M+1$, which is odd. Next, we have $\sum_{i=1}^k s_i+t_{k+1}+t_{k+2}=\sum_{i=1}^k s_i+2M+1$ is odd, and this implies as above that $t_{k+3}\ldots t_n=0\ldots 0$, and we retrieve the second point of the proposition.

For the case when $\sum_{i=1}^{k} s_i$ is odd, in a similar way we have $t_{k+1}\dots t_n=0\dots 0$.

The next proposition is the ‘first’ counterpart of the previous one. Its proof is similar by exchanging the parity of the summation from ‘odd’ to ‘even’ and vice-versa, and it is left to the reader.

\[pro:pro\_Rb\_odd2\] Let $b\geq1$ and odd, $k\leq n-2$ and ${\boldsymbol{s}}=s_1\ldots s_{k}$. If ${\boldsymbol{t}}$ is the $\prec$-first sequence in ${\boldsymbol{s}}\,|\,R_n(b)$, then ${\boldsymbol{t}}$ has one of the following forms:

1.  ${\boldsymbol{t}}={\boldsymbol{s}}M0\ldots0$ if $\sum_{i=1}^{k} s_i$ is odd and $M$ is odd,

2.  ${\boldsymbol{t}}={\boldsymbol{s}}M(M+1)0\ldots0$ if $\sum_{i=1}^{k} s_i$ is odd and $M$ is even,

3.  ${\boldsymbol{t}}={\boldsymbol{s}}0\ldots0$ if $\sum_{i=1}^{k} s_i$ is even,

where $M=\min\{b,\max\{s_i\}_{i=1}^k+1\}$.

Based on Propositions \[pro:pro\_Rb\_odd1\] and \[pro:pro\_Rb\_odd2\], we have the following theorem.

\[k\_odd\] For any $n,b\geq 1$ and $b$ odd, $R_n(b)$ listed in $\prec$ order is a $3$-adjacent Gray code.

Let ${\boldsymbol{s}}=s_1s_2\dots s_n$ and ${\boldsymbol{t}}=t_1t_2\dots t_n$ be two consecutive sequences in $\prec$ ordered list for the set $R_n(b)$, with ${\boldsymbol{s}}\prec {\boldsymbol{t}}$, and let $k$ be the leftmost position where ${\boldsymbol{s}}$ and ${\boldsymbol{t}}$ differ. If $k\geq n-2$, then obviously ${\boldsymbol{s}}$ and ${\boldsymbol{t}}$ differ in at most three positions, otherwise let ${\boldsymbol{s}}'=s_1\ldots s_k$ and ${\boldsymbol{t}}'=t_1\ldots t_k$. Thus, ${\boldsymbol{s}}$ is the $\prec$-last sequence in ${\boldsymbol{s}}'\,|\,R_n(b)$ and ${\boldsymbol{t}}$ is the $\prec$-first sequence in ${\boldsymbol{t}}'\,|\,R_n(b)$. Combining Propositions \[pro:pro\_Rb\_odd1\] and \[pro:pro\_Rb\_odd2\] we have that, when $k\leq n-3$, $s_{k+3}s_{k+4}\dots s_n=t_{k+3}t_{k+4}\dots t_n=00\dots 0$. And since $s_i=t_i$ for $i=1\ldots,k-1$, the statement holds.

Theorem \[k\] below shows the Graycodeness of $R_n(b)$, $b\geq 1$ and even, listed in $\preccdot$ order, and as for Theorem \[k\_odd\] we need the next two propositions; in its proof we will make use of the Iverson bracket notation: $[P]$ is $1$ if the statement $P$ is true, and 0 otherwise. Thus, for a sequence $s_1s_2\dots s_n$ and a $k\leq n$, $| \{i\in \{1,2,\ldots,k\}:s_i\neq 0 \mbox{ and } s_i \mbox{ is even}\}|=\sum_{i=1}^k[s_i\neq0 {\rm\ and\ } s_i {\rm\ is\ even}]$.

\[pro:pro\_Rb\_even1\] Let $b\geq2$ and even, $k\leq n-2$ and ${\boldsymbol{s}}=s_1s_2\ldots s_k$. If ${\boldsymbol{t}}$ is the $\preccdot$-last sequence in ${\boldsymbol{s}}\,|\,R_n(b)$, then ${\boldsymbol{t}}$ has one of the following forms:

1.  ${\boldsymbol{t}}={\boldsymbol{s}}M0\ldots0$ if $U_{k+1}$ is even and $M$ is even,

2.  ${\boldsymbol{t}}={\boldsymbol{s}}M(M+1)0\ldots0$ if $U_{k+1}$ is even and $M$ is odd,

3.  ${\boldsymbol{t}}={\boldsymbol{s}}0\ldots0$ if $U_{k+1}$ is odd,

where $M=\min\{b,\max\{s_i\}_{i=1}^k+1\}$ and $U_{k+1}=\sum_{i=1}^k[s_i\neq0 {\rm\ and\ } s_i {\rm\ is\ even}]$.

Let ${\boldsymbol{t}}=s_1\dots s_kt_{k+1}\dots t_n$ be the $\preccdot$-last sequence in ${\boldsymbol{s}}\,|\,R_n(b)$.

Referring to the definition of $\preccdot$ order in Definition \[de:coRGCorder\], if $U_{k+1}$ is even, then $t_{k+1}=\min\{b,\max\{s_i\}_{i=1}^k+1\}=M>0$, and based on the parity of $M$, two cases can occur.

-   If $M$ is even, then $U_{k+1}+[t_{k+1}\neq0 {\rm\ and\ } t_{k+1} {\rm\ is\ even}]=U_{k+1}+1$ is odd, thus $t_{k+2}\dots t_n=0\dots 0$, and we retrieve the form prescribed by the first point of the proposition.

-   If $M$ is odd, then $M\neq b$ and $U_{k+1}+[t_{k+1}\neq0 {\rm\ and\ } t_{k+1} {\rm\ is\ even}]=U_{k+1}$ is even, thus $t_{k+2}=\max\{s_1,\dots, s_k,t_{k+1}\}+1=M+1$, which is even. Next, we have $U_{k+1}+[t_{k+1}\neq0 {\rm\ and\ } t_{k+1} {\rm\ is\ even}]+ [t_{k+2}\neq0 {\rm\ and\ } t_{k+2} {\rm\ is\ even}]=U_{k+1}+1$ is odd, and this implies as above that $t_{k+3}\dots t_n=0\dots 0$, and we retrieve the second point of the proposition.

For the case when $U_{k+1}$ is odd, in a similar way we have $t_{k+1}\dots t_n=0\dots 0$.

The next proposition is the ‘first’ counterpart of the previous one.

\[pro:pro\_Rb\_even2\] Let $b\geq2$ and even, $k\leq n-2$ and ${\boldsymbol{s}}=s_1s_2\ldots s_k$. If ${\boldsymbol{t}}$ is the $\preccdot$-first sequence in ${\boldsymbol{s}}\,|\,R_n(b)$, then ${\boldsymbol{t}}$ has one of the following forms:

1.  ${\boldsymbol{t}}={\boldsymbol{s}}M0\ldots0$ if $U_{k+1}$ is odd and $M$ is even,

2.  ${\boldsymbol{t}}={\boldsymbol{s}}M(M+1)0\ldots0$ if $U_{k+1}$ is odd and $M$ is odd,

3.  ${\boldsymbol{t}}={\boldsymbol{s}}0\ldots0$ if $U_{k+1}$ is even,

where $M=\min\{b,\max\{s_i\}_{i=1}^k+1\}$ and $U_{k+1}=\sum_{i=1}^k[s_i\neq0 {\rm\ and\ } s_i {\rm\ is\ even}]$.

Based on Propositions \[pro:pro\_Rb\_even1\] and \[pro:pro\_Rb\_even2\] we have the following theorem, its proof is similar with that of Theorem \[k\_odd\].

\[k\_even\] For any $n\geq 1$, $b\geq 2$ and even, $R_n(b)$ listed in $\preccdot$ order is a $3$-adjacent Gray code.

It is worth to mention that, neither $\prec$ for even $b$, nor $\preccdot$ for odd $b$ yields a Gray code on $R_n(b)$. Considering $b\geq n$ in Theorem \[k\_odd\] and \[k\_even\], the bound $b$ does not actually provide any restriction, and in this case $R_n(b)=R_n$, and we have the following corollary.

For any $n\geq 1$, $R_n$ listed in both $\prec$ and $\preccdot$ order are $3$-adjacent Gray codes.

\[k\] For any $b\geq 1$ and odd, $n>b$, $R_n^*(b)$ listed in $\prec$ order is a $5$-Gray code.

For two integers $a$ and $b$, $0<a\leq b$, we define $\tau_{a,b}$ as the length $b-a$ increasing sequence $(a+1)(a+2) \ldots (b-1)b$, and $\tau_{a,b}$ is vanishingly empty if $a=b$. Imposing to a sequence ${\boldsymbol{s}}$ in $R_n(b)$ to have its largest element equal to $b$ (so, to belong to $R^*_n(b)$) implies that either $b$ occurs in ${\boldsymbol{s}}$ before its last position, or ${\boldsymbol{s}}$ ends with $b$, and in this case the tail of ${\boldsymbol{s}}$ is $\tau_{a,b}$ for an appropriate $a<b$. More precisely, in the latter case, ${\boldsymbol{s}}$ has the form $s_1s_2\ldots s_j\tau_{a,b}$, for some $j$ and $a$, with $a=\max\{s_i\}_{i=1}^j$ and $j=n-(b-a)$.

Now let ${\boldsymbol{s}}=s_1s_2\ldots s_n\prec{\boldsymbol{t}}=t_1t_2\ldots t_n$ be two consecutive sequences in the $\prec$ ordered list for $R^*_n(b)$, and let $k\leq n-3$ be the leftmost position where ${\boldsymbol{s}}$ and ${\boldsymbol{t}}$ differ, thus $s_1s_2\ldots s_{k-1}=t_1t_2\ldots t_{k-1}$. It follows that ${\boldsymbol{s}}$ is the $\prec$-last sequence in $R^*_n(b)$ having the prefix $s_1s_2\ldots s_k$, and using Proposition \[pro:pro\_Rb\_odd1\] and the notations therein, by imposing that $\max\{s_i\}_{i=1}^n$ is equal to $b$, we have:

-   if $\sum_{i=1}^ks_i$ is odd, then ${\boldsymbol{s}}$ has the form $s_1s_2\ldots s_k0\ldots0\,\tau_{a,b}$, where $a=\max\{s_i\}_{i=1}^k$,

-   if $\sum_{i=1}^ks_i$ is even, then ${\boldsymbol{s}}$ has one of the following forms:

    -   $s_1s_2\ldots s_k M0\ldots0\,\tau_{M,b}$, or

    -   $s_1s_2\ldots s_kM(M+1)0\ldots0\,\tau_{M+1,b}$.

When the above $\tau$’s suffixes are empty, we retrieve precisely the three cases in Proposition \[pro:pro\_Rb\_odd1\].

Similarly, ${\boldsymbol{t}}$ is the $\prec$-first sequence in $R^*_n(b)$ having the prefix $t_1t_2\ldots t_{k-1}t_k=s_1s_2\ldots s_{k-1}t_k$. Since by the definition of $\prec$ order we have that $t_k=s_k+1$ or $t_k=s_k-1$, it follows that $\sum_{i=1}^kt_i$ and $\sum_{i=1}^ks_i$ have different parity (that is, $\sum_{i=1}^kt_i$ is odd if and only if $\sum_{i=1}^ks_i$ is even), and by Proposition \[pro:pro\_Rb\_odd2\] and replacing for notational convenience $M$ by $M'$, we have:

-   if $\sum_{i=1}^ks_i$ is odd, then ${\boldsymbol{t}}$ has the form $t_1t_2\ldots t_k0\ldots0\,\tau_{a',b}$, where $a'=\max\{t_i\}_{i=1}^k$,

-   if $\sum_{i=1}^ks_i$ is even, then ${\boldsymbol{t}}$ has one of the following forms:

    -   $t_1t_2\ldots t_k M'0\ldots0\,\tau_{M',b}$, or

    -   $t_1t_2\ldots t_kM'(M'+1)0\ldots0\,\tau_{M'+1,b}$.

With these notations, since $t_k\in \{s_k+1,s_k-1\}$, it follows that

-   if $\sum_{i=1}^ks_i$ is odd, then $a'\in\{a-1,a,a+1\}$, and so the length of $\tau_{a,b}$ and that of $\tau_{a',b}$ differ by at most one; and

-   if $\sum_{i=1}^ks_i$ is even, then $M'\in\{M-1,M,M+1\}$, and the length of the non-zero tail of ${\boldsymbol{s}}$ and that of ${\boldsymbol{t}}$ (defined by means of $\tau$ sequences) differ by at most two.

Finally, the whole sequences ${\boldsymbol{s}}$ and ${\boldsymbol{t}}$ differ in at most five (not necessarily adjacent) positions, and the statement holds.

Generating algorithms
=====================

An exhaustive generating algorithm is one generating all sequences in a combinatorial class, with some predefined properties ([*e.g.*]{}, having the same length). Such an algorithm is said to run in [*constant amortized time*]{} if it generates each object in $O(1)$ time, in amortized sense. In [@Ruskey] the author called such an algorithm [*CAT algorithm*]{} and shows that a recursive generating algorithm satisfying the following properties is precisely a CAT algorithm:

-   Each recursive call either generates an object or produces at least two recursive calls;

-   The amount of computation in each recursive call is proportional to the degree of the call (that is, to the number of subsequent recursive calls produced by current call).

Procedure [Gen1]{} in Fig. \[fig:alg\_Rnb\_odd\] generates all sequences belonging to $R_n(b)$ in Reflected Gray Code Order. Especially when $b$ is odd, the generation induces a 3-adjacent Gray code. The bound $b$ and the generated sequence $s=s_1s_2\ldots s_n$ are global. The $k$ parameter is the position where the value is to be assigned (see line 8 and 13); the $dir$ parameter represents the direction of sequencing for $s_k$, whether it is up (when $dir$ is even, see line 7) or down (when $dir$ is odd, see line 12); and $m$ is such that $m+1$ is the the maximum value that can be assigned to $s_k$, that is, $\min\{b-1,\max\{s_i\}_{i=1}^{k-1}\}$ (see line 5).

The algorithm initially sets $s_1=0$, and the recursive calls are triggered by the initial call [Gen1]{}$(2,0,0)$. For the current position $k$, the algorithm assigns a value to $s_k$ (line 8 or 13) followed by recursive calls in line 10 or 15. This scheme guarantees that each recursive call will produce subsequent recursive calls until $k=n+1$ (line 4), that is, when a sequence of length $n$ is generated and printed out by [Type()]{} procedure. This process eventually generates all sequences in $R_n(b)$. In addition, by construction, algorithm [Gen1]{} satisfies the previous CAT desiderata, and so it is en efficient exhaustive generating algorithm.

+-----------------------------------------------------------------------+
| \                                                                     |
| [01]{} [**procedure**]{} [Gen1]{}($k$, $dir$, $m$: integer)\          |
| [02]{} [**global**]{} $s$, $n$, $b$: integer;\                        |
| [03]{} [**local**]{} $i$, $u$: integer;\                              |
| [04]{} [**if**]{} $k=n+1$ [**then**]{} [Type()]{};\                   |
| [05]{} [**else**]{} **if** $m=b$ [**then**]{} $m:=b-1$;               |
| [**endif**]{}\                                                        |
| [06]{} $dir$ mod $2=0$\                                               |
| [07]{} **for** =$i:=0$ [**to**]{} $m+1$ [**do**]{}\                   |
| [08]{} $s_k:=i$;\                                                     |
| [09]{} $m<s_k$ [**then**]{} $u:=s_k$; [**else**]{} $u:=m$;            |
| [**endif**]{}\                                                        |
| [10]{} ;\                                                             |
| [11]{}\                                                               |
| [12]{} $i:=m+1$ [**downto**]{} $0$ [**do**]{}\                        |
| [13]{} $s_k:=i$;\                                                     |
| [14]{} $m<s_k$ [**then**]{} $u:=s_k$; [**else**]{} $u:=m$;            |
| [**endif**]{}\                                                        |
| [15]{} $(k+1,i+1,u)$;\                                                |
| [16]{}\                                                               |
| [17]{}\                                                               |
| [18]{} [**endif**]{}\                                                 |
| [19]{} [**end procedure.**]{}                                         |
+:=====================================================================:+
+-----------------------------------------------------------------------+

Similarly, the call [Gen2]{}$(2,0,0)$ of the algorithm in Fig. \[fig:alg\_Rnb\_even\] generates sequences in $R_n(b)$ in co-Reflected Gray Code Order, and in particular when $b$ is even, a 3-adjacent Gray code for these sequences. And again it satisfies the CAT desiderata, and so it is en efficient exhaustive generating algorithm.

Finally, algorithm [Gen3]{} in Fig. \[fig:alg\_Pnb\_odd\] generates the set $R^*_n(b)$ in Reflected Gray Code Order and produces a 5-Gray code if $b$ is odd. It mimes algorithm [Gen1]{} and the only differences consist in an additional parameter $a$ and lines 5, 6, 13 and 19, and its main call is [Gen3]{}$(2,0,0,0)$. Parameter $a$ keeps track of the maximum value in the prefix $s_1s_2\dots s_{k-1}$ of the currently generated sequence, and it is updated in lines 13 and 19. Furthermore, when the current position $k$ belongs to a $\tau$-tail (see the proof of Theorem \[k\]), that is, condition $k=n+1+a-b$ in line 5 is satisfied, then the imposed value is written in this position, and similarly for the next two positions. Theorem \[k\] ensures that there are no differences between the current sequence and the previous generated one beyond position $k+2$, and thus a new sequence in $R^*_n(b)$ is generated. And as previously, [Gen3]{} is a CAT generating algorithm.

+-----------------------------------------------------------------------+
| \                                                                     |
| [**procedure**]{} [Gen2]{}($k$, $dir$, $m$: integer)\                 |
| [**global**]{} $s$, $n$, $b$: integer;\                               |
| [**local**]{} $i$, $u$: integer;\                                     |
| [**if**]{} $k=n+1$ [**then**]{} [Type()]{};\                          |
| [**else**]{} **if** $m=b$ [**then**]{} $m:=b-1$; [**endif**]{}\       |
| $dir$ mod 2$=0$\                                                      |
| **for** = $i:=0$ [**to**]{} $m+1$ [**do**]{}\                         |
| $s_k:=i$;\                                                            |
| $m<s_k$ [**then**]{} $u:=s_k$; [**else**]{} $u:=m$; [**endif**]{}\    |
| $s_k=0$ **then** [Gen2]{}$(k+1, 0,u)$;\                               |
| $(k+1,  i+1,u)$;\                                                     |
| \                                                                     |
| \                                                                     |
| $i:=m+1$ [**downto**]{} $0$ [**do**]{}\                               |
| $s_k:=i$;\                                                            |
| $m<s_k$ [**then**]{} $u:=s_k$; [**else**]{} $u:=m$; [**endif**]{}\    |
| $s_k=0$ **then** [Gen2]{}$(k+1,  1,u)$;\                              |
| $(k+1,  i,u)$;\                                                       |
| \                                                                     |
| \                                                                     |
| \                                                                     |
| [**endif**]{}\                                                        |
| [**end procedure.**]{}                                                |
+:=====================================================================:+
+-----------------------------------------------------------------------+

+-----------------------------------------------------------------------+
| \                                                                     |
| [01]{} [**procedure**]{} [Gen3]{}($k$, $dir$, $m$, $a$: integer)\     |
| [02]{} [**global**]{} $s$, $n$, $b$: integer;\                        |
| [03]{} [**local**]{} $i$, $u$, $\ell$: integer;\                      |
| [04]{} [**if**]{} $k=n+1$ [**then**]{} [Type()]{};\                   |
| [05]{} [**else**]{} **if** $k=n+1 + a-b$\                             |
| [06]{} **for** $i:=0$ [**to**]{} $2$ [**do**]{} [**if**]{}            |
| $k+i\leq n$ [**then**]{} $s_{k+i}:=a+1+i$; [**endif**]{}              |
| [**endfor**]{}\                                                       |
| [07]{} ;\                                                             |
| [08]{} **if** $m=b$ [**then**]{} $m:=b-1$; [**endif**]{}\             |
| [09]{} $dir$ mod 2$=0$\                                               |
| [10]{} **for** = $i:=0$ [**to**]{} $m+1$ [**do**]{}\                  |
| [11]{} $s_k:=i$;\                                                     |
| [12]{} $m<s_k$ [**then**]{} $u:=s_k$; [**else**]{} $u:=m$;            |
| [**endif**]{}\                                                        |
| [13]{} $a<s_k$ [**then**]{} $\ell:=s_k$; [**else**]{} $\ell:=a$;      |
| [**endif**]{}\                                                        |
| [14]{} ;\                                                             |
| [15]{}\                                                               |
| [16]{} $i:=m+1$ [**downto**]{} $0$ [**do**]{}\                        |
| [17]{} $s_k:=i$;\                                                     |
| [18]{} $m<s_k$ [**then**]{} $u:=s_k$; [**else**]{} $u:=m$;            |
| [**endif**]{}\                                                        |
| [19]{} $a<s_k$ [**then**]{} $\ell:=s_k$; [**else**]{} $\ell:=a$;      |
| [**endif**]{}\                                                        |
| [20]{} $(k+1,i+1,u,\ell)$;\                                           |
| [21]{}\                                                               |
| [22]{}\                                                               |
| [23]{}\                                                               |
| [24]{} [**endif**]{}\                                                 |
| [25]{} [**end procedure.**]{}                                         |
+:=====================================================================:+
+-----------------------------------------------------------------------+

[**Final remarks.**]{} We suspect that the upper bounds 3 in Theorems \[k\_odd\] and \[k\_even\], and 5 in Theorem \[k\] are not tight, and a natural question arises: are there more restrictive Gray codes for $R_n(b)$ and for $R^*_n(b)$ with $b$ odd? Finally, is there a natural order relation inducing a Gray code on $R^*_n(b)$ when $b$ is even?

A. Bernini, S. Bilotta, R. Pinzani, A. Sabri, V. Vajnovszki, Reflected Gray codes for $q$-ary words avoiding a given factor, Acta Informatica, 52(7), 573-592 (2015).

F. Gray, Pulse code communication, U.S. Patent 2632058 (1953).

F. Ruskey, Combinatorial generation, Book in preparation.

A. Sabri, V. Vajnovszki, Reflected Gray code based orders on some restricted growth sequences, The Computer Journal, 58(5), 1099-1111 (2015).

A. Sabri, V. Vajnovszki, Bounded growth functions: Gray codes and exhaustive generation, The Japanese Conference on Combinatorics and its Applications, May 21-25, 2016, Kyoto, Japan.

N.J.A. Sloane, The On-line Encyclopedia of Integer Sequences, available electronically at [http://oeis.org]{}.

[^1]: In [@SV] a similar terminology is used for a slightly different notion
