Classifying Rotationally-Closed Languages Having Greedy Universal Cycles

Let $\textbf{T}(n,k)$ be the set of strings of length $n$ over the alphabet $\Sigma=\{1,2,\ldots,k\}$. A universal cycle for $\textbf{T}(n,k)$ can be constructed using a greedy algorithm: start with the string $k^n$, and continually append the least symbol possible without repeating a substring of length $n$. This construction also creates universal cycles for some subsets $\textbf{S}\subseteq\textbf{T}(n,k)$; we will classify all such subsets that are closed under rotations.

Let T(n, k) be the set of strings of length n over the alphabet Σ = {1, 2, . . . , k}. Given a subset S ⊆ T(n, k), we will be interested in finding a "universal cycle" for S: that is, a string of length |S| where each element of S occurs exactly once in that string when it is viewed as a cycle.
For example, we could consider the subset S 1 ⊆ T(3, 5) consisting of ascending strings and their rotations. That is, Below is a universal cycle for S 1 . To make it easier to locate certain strings in the cycle, the string 534 is repeated at both the start and the end of the cycle. (The 534 is put in parentheses at the start as a reminder that the 534 is repeated.) (534)123124134234512513514523524534 This universal cycle was constructed using a greedy algorithm: after choosing 534 as the starting string, each subsequent digit was chosen by looking for the smallest digit that could be chosen without duplicating any length-3 substrings (except for 534 itself, to conclude the cycle).
As another example, consider the subset S 2 ⊆ T (2,9) consisting of all two-digit strings where both the string and its reverse yield composite numbers in base 10. For example, 43 ∈ S 2 because 43 is prime; 34 ∈ S 2 because 34 is the reverse of a prime number. If we try to construct a universal cycle for S 2 using a greedy algorithm starting from 99, here's what we get: Unfortunately, this cycle isn't quite universal: 77, 78, 87, and 88 are missing. This raises the question: can we find necessary and sufficient conditions on S ⊆ T(n, k) so that a universal cycle for S can be generated from a greedy algorithm? In this paper, we will find such conditions, under the assumption that S is closed under rotations.

Results
Some notational conventions: when we are working with a particular set T(n, k), we will use α and β (possibly with subscripts) to represent strings in T(n, k). Other Greek letters (like γ) will represent strings in T(m, k) for some m ≤ n. (This includes the possibility of γ being an empty string.) Latin letters (a, b, etc.) will represent individual elements of {1, . . . , k}. We will sometimes use exponential notation to write strings with repetitions in a shorter form. A couple of examples: 2 5 3 represents the string 222223, and 4(21) 3 represents the string 4212121.
Given a set S ⊆ T(n, k) (where S = ∅), let α ∈ S. Define the string Greedy α (S) as follows: let β 0 = α, and having defined β 0 through β j = aγ, let β j+1 = γa j+1 , where a j+1 is the least element of {1, . . . , k} such that γa j+1 ∈ S and γa j+1 = β i for all 1 ≤ i ≤ j. The process halts when we reach an β m where either β m = α, or it is impossible to define β m+1 . (The latter occurs when, given β m = aγ, we have γb ∈ {β i } m i=1 for all b ∈ {1, . . . , k} such that γb ∈ S.) Let m be the largest positive integer for which β m exists. We then define Greedy α (S) to be: In the case where the process ends because β m = α, the string Greedy α (S) may be viewed as a cycle; each of the strings from β 1 to β m = α appears exactly once in that cycle.
Let G(n, k) be the collection of subsets S ⊆ T(n, k) such that, for some α ∈ S, Greedy α (S) is a universal cycle for S. That is, the length-n suffix of Greedy α (S) is α, and for all β ∈ S, β is a substring of Greedy α (S) (treated as a cycle). The goal is to find a characterization of the sets in G(n, k) that are closed under rotations.
Given S ∈ T(n, k), and given α, β ∈ S, we will say that β is "increasable in S to α" if we can transform β into α by continually increasing individual symbols, and if the resulting string after each such increase is in S. (By convention, we will say that α ∈ S is increasable in S to α.) For example, for our "no primes" set S 2 ⊆ T(2, 9), 57 is increasable in S 2 to 99: Given this definition, our ultimate result is the following: Theorem 1. Let S ⊆ T(n, k) be closed under rotations, and let α, β ∈ S. Then β is a substring of Greedy α (S) (treated as a cycle) if and only if β is increasable in S to a rotation of α.
Thus, if S ⊆ T(n, k) is closed under rotations, then S ∈ G(n, k) if and only if there exists an α ∈ S such that every β ∈ S is increasable in S to α.
This theorem explains the absence of 77, 78, 87, and 88 in the string Greedy 99 (S 2 ): none of those four strings are increasable in S 2 to 99, since none of 79, 89, 97, or 98 is in S. Note that this theorem is a generalization of Theorem 3, from [2].
The proof of this result will rely on an analysis of a combinatorial game, which we'll call the Warden's Game. The rules for this game will be given in Section 2, and the proof of Theorem 1 will follow in Section 3. In Section 4, we will look at several interesting families of sets S ⊆ T(n, k) where a universal cycle can be generated with the greedy algorithm. Lastly, a possible avenue for future work will be detailed in Section 5.

The Warden's Game
Consider the following fanciful scenario: there's a certain prison warden who loves playing games. He sometimes makes an offer to let his prisoners out of prison, if they can beat him at a particular game. The game works as follows: The warden shows the prisoner a row of n k-sided dice on a table. On each die, the faces are numbered from 1 to k. (It's possible to have k = 2 here; the "dice" would then be coins with a 1 on one side and a 2 on the other.) A certain string α ∈ T(n, k) is chosen: the prisoner will earn his freedom if, after any move, the dice on the table are showing the string α.
(If the dice are showing α at the start of the game, the prisoner does not immediately win; the prisoner only wins when the dice show α after a move.) This game will be played at a rate of one move per day. So, the prisoner wants to reach α as quickly as possible; the warden wants to delay this as long as possible (indefinitely, if he can).
Each day, the rightmost die in the row will be moved to the far left, and possibly rotated to show a different number. The warden always has priority; he may transfer the rightmost die to the far left, and lower the number on that die. If he doesn't want to do that (or he can't, because he can't lower the number any further), then the warden passes; then the prisoner must transfer the rightmost die to the far left, and optionally increase the number on that die.
As an example: let n = 3 and k = 6, so that the game is being played with three 6-sided dice. Let's say the current position is 513; the leftmost die shows 5, the middle die shows 1, and the rightmost die shows 3. The warden may transfer the rightmost die to the far left, lowering its value to 1 or 2 (thus producing the position 151 or 251). Or the warden may pass, in which case the prisoner must transfer the rightmost die and optionally increase its value (producing one of the positions 351, 451, 551, or 651). Let's say the warden chooses to move to the position 251. Then on the next move, the warden can't lower the value showing on the rightmost die. So the warden must pass, and the prisoner can move to 125, 225, 325, 425, 525, or 625. And so on.
Note: in the case where k = 2, the rules can be stated even more simply. If the rightmost coin is showing a 2, the warden transfers that coin, and optionally flips it to 1. If the rightmost coin is showing a 1, the prisoner transfers that coin, and optionally flips it to 2.
We can generalize this game still further, by limiting the legal positions in the game. We can choose any subset S ⊆ T(n, k), closed under rotations, to be the set of legal positions. (We'll assume that the goal state α is in S.) Then each move of the game, whether made by the prisoner or the warden, must be to a position in S. We require that S be closed under rotations so that there is a legal move from every legal position; if the warden ever passes, the prisoner always has the option to transfer the rightmost die without changing its value.
In [3], Weiss analyzed the Warden's Game (though not under that name) in the case where k = 2, S = T(n, 2), and α is the string 2 n . Weiss proved that the game tree for the game is summarized by the lexicographically minimal de Bruijn sequence for T(n, 2); if both players play optimally, the game will proceed backwards through the de Bruijn sequence, one move at a time. For example, if n = 4, the lexicographically minimal de Bruijn sequence is the following: For this game, consider the position 2212. If we move one step backwards in the de Bruijn sequence from 2212, we get 1221; thus, the optimal move from 2212 must be for the warden to flip the rightmost coin before moving it, producing the position 1221. Similarly, the next optimal move is for the prisoner to move from 1221 to 1122, and so on, until the goal position 2222 is finally reached.
As we will prove in Section 3, the same holds true for any values of n and k, any subset S ⊆ T(n, k) of legal positions (closed under rotations), and any goal state α ∈ S. The greedy algorithm always generates the full game tree for the Warden's Game. As an example, let's once again consider the subset S 2 ⊆ T(2, 9) consisting of those strings where both the string itself and its reverse are 2-digit composite numbers. Here, once again, is the (not quite universal) cycle generated by the greedy algorithm, starting from α = 99. (99)33621224251526394454648182728496556685758699 For example, consider the position 82. The preceding substring of length 2 is 18; thus, the optimal move must be for the warden to move the rightmost die and reduce its value from 2 to 1. The next optimal move must be to 81; both the prisoner and the warden refuse to change the value on the rightmost die. The next optimal move is to 48, which means the warden passes, and the prisoner increases the value on the rightmost die from 1 to 4. And so on.
Remember, four positions from S 2 do not appear in this cycle: 77, 78, 87, and 88. Why don't they appear? Because they are losing positions for the prisoner! From any of those positions, the warden has a simple way to keep the game going indefinitely: he refuses to ever decrease the value on a die, and passes every time. The prisoner will never be able to increase a die to a 9, because 79, 89, 97, and 98 are all illegal positions. So the prisoner will never be able to reach the goal state, 99.
Our goal for the next section is to prove that this sort of thing happens regardless of the choices of S and α. We will show that the prisoner can win from a given position β ∈ S if and only if he can win from β with the warden always passing: this happens when β is increasable in S to a rotation of α. We will also show that the greedy algorithm generates the game tree for this game; thus, the greedy algorithm generates a universal cycle if and only if every β ∈ S is increasable in S to a rotation of α.

Proof of Theorem 1
Assume we are given values of n and k, a set S ⊆ T(n, k) of legal positions (closed under rotations), and a goal state α ∈ S. Define the "remoteness function" r on S as follows: given β ∈ S, the remoteness of β, r(β), is the number of moves the game will last starting from β if both players play optimally. If the warden can keep the game going forever, then r(β) = ∞. This definition of remoteness is similar to the concept of remoteness used in [1].
Note: we can consider α to either be an end position (of remoteness 0) or a start position (of nonzero remoteness). We will always use the notation r(α) for the number of moves the game will last starting from α; thus, r(α) > 0. Lemma 1. Let β 1 , β 2 ∈ S be such that β 1 = γa and β 2 = a ′ γ for a ′ ≥ a. (Thus, if the current position is β 1 and the warden passes, then the prisoner may move to β 2 .) Then, starting from β 1 , the prisoner has a strategy which can force the position to eventually reach β 2 .
Proof. This can be proven by induction on the sum of the symbols in β 1 . Starting from β 1 = b 1 b 2 · · · b n−1 a, if the warden passes, then the prisoner may move immediately to β 2 . Otherwise, the warden must move to β 3 = a ′′ b 1 b 2 · · · b n−1 for some a ′′ < a. But the sum of the symbols of β 3 is less than the sum of the symbols of β 1 . So by the inductive hypothesis, the prisoner has a strategy to eventually force the position to b n−1 a ′′ b 1 · · · b n−2 , then to b n−2 b n−1 a ′′ b 1 · · · b n−3 , and so on to b 1 b 2 · · · b n−1 a ′′ . We still have a smaller sum than the sum of the symbols in β 1 , so the prisoner can eventually force the position to Lemma 2. Given β ∈ S, the prisoner can win from β if and only if β is increasable in S to a rotation of α.
Proof. If β is not increasable in S to a rotation of α, then the warden can keep the game going indefinitely, simply by passing on every turn. Since the prisoner can only increase values, if the warden always passes, the prisoner will only be able to reach positions γ where β is increasable in S to a rotation of γ. Since α is not such a position, the prisoner can never win. Now assume that β is increasable in S to a rotation of α. Then, if the warden chooses to pass on every move, then there is a sequence of moves β = β 0 , β 1 , β 2 , . . . , β m = α that the prisoner may make to win. By Lemma 1, if the game starts from β = β 0 , then prisoner can eventually force the position to be β 1 , then β 2 , and so on until finally reaching α and winning.
Note: while this shows that the prisoner can win eventually from any position β that is increasable in S to α, the recursive strategy described above will probably not be the prisoner's optimal strategy.  The point here is that, the greater the rightmost symbol in the string, the better off the warden is. From γa 1 , the warden may move to any aγ ∈ S such that a < a 1 , or the warden may give the prisoner the choice to move to any aγ ∈ S where a ≥ a 1 . From γa 2 , the warden still may move to any aγ ∈ S such that a < a 1 , or the warden can ensure that the next move is to aγ ∈ S for some a ≥ a 1 ... but in the latter case, the warden may choose a specific aγ ∈ S such that a 1 ≤ a < a 2 , if he so desires. This extra option can only help the warden, never hurt him. So we must have r(γa 1 ) ≤ r(γa 2 ).
Lemma 5. For any nonnegative integer m, there is at most one position of remoteness m.
This is a significant result; combined with Lemma 3, the conclusion is that the "game tree" is really a chain, not a tree. There is one position of remoteness 0 (namely, α), one position of remoteness 1, one position of remoteness 2, and so on until all the winning positions for the prisoner have been exhausted. And given any position β that is winning for the prisoner, if a game starting from β is played optimally, the game will pass through all positions of remoteness less than r(β) until finally reaching α.
Proof. We will prove this by contradiction. Let m be the smallest integer where there are multiple positions of remoteness m. There is only one position of remoteness 0 (namely, α), so m ≥ 1. Let aγ be the one position of remoteness m − 1; this position must be reachable in one move from all positions of remoteness m, so all such positions must have the form γb.
For each i ≤ j, let a i γ be the next position reached from γb i if both sides play optimally (the a i 's for 1 ≤ i ≤ j are all distinct). We can now show that it is impossible to have r(γb j ) = r(γb j+1 ) = m: Consider the two sets {a i } j i=1 and {b i } j i=1 . If these two sets are identical, then consider what happens if the warden passes from the position γb j+1 .
The prisoner is then forced to move to aγ for some a ≥ b j+1 . This a will not be an element of {a i } j i=1 , and hence aγ will not have remoteness at most m − 1. So by passing, the warden can force the next move to be to a position of remoteness at least m; γb j+1 can't have remoteness m.
On the other hand, assume If the warden moves from γb j+1 to b i γ, then the warden has not moved to a position of remoteness at most m − 1. So again, the warden was able to force the next move to be to a position of remoteness at least m; γb j+1 can't have remoteness m.
This completes the contradiction; it is impossible to have two positions of the same finite remoteness.
Lemma 6. If α ∈ S is the goal state, then the position in S of highest finite remoteness is α.
The reason: since α is (trivially) increasable in S to a rotation of α, r(α) is finite. Say r(α) = m > 0 (we are treating α as a start position, not an end position). If there were any position β such that r(β) = m + 1, then with optimal play, the next move from β would be to a position of remoteness m: namely, α. But that means, with optimal play, β is just one move from the goal state; so r(β) = 1, not m+1. Thus, α has the maximal finite remoteness of any string in S.
This also means that the positions in S that are winning for the prisoner form a cycle. The only question remaining is why this is the same cycle that we would get from the greedy algorithm.
Theorem 2. The greedy algorithm generates the game "tree" for the warden's game.
Proof. Let {β m } ⊆ S be the sequence of strings generated by the greedy algorithm, starting from α. We have β 0 = α, and for each m ≥ 0, if β m = aγ, then β m+1 = γb, where b is the least element of {1, · · · , k} such that γb ∈ S and γb does not appear in the set , · · · , k}, then there is no β m+1 ; β m is the last string in the sequence.) Obviously, β 0 = α is the one position of remoteness 0. We must show that r(β m ) = m for all m > 0; we will prove this by induction.
Assume we have r(β i ) = i whenever 0 < i ≤ m. Let β m = aγ. Assume there is a position of remoteness m + 1; it must be of the form γb ∈ S (so that there is a move available to aγ), where γb is not in {β i } m i=1 (since all strings in that set have remoteness m or less). Let b 1 < b 2 < · · · < b j be the elements of {1, · · · k} such that γb i ∈ S, but γb i is not in {β i } m i=1 . (Thus, β m+1 = γb 1 .) By Lemma 4, r(γb 1 ) ≤ r(γb 2 ) ≤ · · · ≤ r(γb j ); by Lemma 5, all of those inequalities are strict except for where we have multiple positions of infinite remoteness. We have r(γb 1 ) > m, so the only i where we can have r(γb i ) = m + 1 is i = 1. Thus, we must have r(β m+1 ) = r(γb 1 ) = m + 1. Now assume that m is the largest finite remoteness of any position in S; that is, r(α) = m. The above inductive argument shows that β m = α. And the way we defined Greedy α (S), the process halts if we ever have β m = α. So we do have r(β m ) = m for all m > 0; the length-n substrings of Greedy α (S) are exactly the winning positions for the prisoner, in order of remoteness.
We thus have proven Theorem 1; the length-n substrings of Greedy α (S) are exactly the winning positions for the prisoner, which are exactly the strings in S which are increasable in S to a rotation of α.
As a final note for this section, here's a comment on the optimal strategy for the warden: Corollary 1. Given any position γb ∈ S, if a is the greatest number less than b such that aγ ∈ S, then the optimal move for the warden from γb is either to move to aγ, or to pass. (So, when the warden does decrease a number, he should always do so by the smallest amount possible.) Proof. Assume not. Assume there is a position γb ∈ S, where a is the greatest number less than b where aγ ∈ S, but the warden's optimal move is to cγ, where c < a. Let r(γb) = m; then the remoteness of cγ is m − 1 (where, if cγ = α, we are treating α as an end position).
Since S is closed under rotations and aγ ∈ S, we have γa ∈ S. From Lemmas 4 and 5, since a < b, we have r(γa) < r(γb). So r(γa) < m. But from the position γa, the warden can move to cγ, a position of remoteness m − 1. So r(γa) ≥ m, contradiction.
There seems to be no similar statements we can make about the optimal strategy for the prisoner; depending on the situation, the prisoner may want to increase the value on a die by the least amount possible, the greatest amount possible, or some amount in between. For example, all such possiblities occur in our "no primes" example, S 2 ⊆ T (2,9). There seems to be no way for the prisoner to determine anything about his optimal next move from β, other than to generate the entire string Greedy α (S) until β is reached.

Interesting examples
In [2], there are a number of examples of interesting sets S ⊆ T(n, k) where the greedy algorithm produces a universal cycle for S. Here are some new such sets derived from Theorem 1.

Strings increasable to a rotation of α
Choose any α ∈ T(n, k), and let S ⊆ T(n, k) be the strings that are increasable in T(n, k) to a rotation of α. Obviously, S is then closed under rotations.
Given any β ∈ S, β is increasable in T(n, k) to a rotation of α. So there are strings β 0 , β 1 , . . . , β m ∈ T(n, k) such that β 0 = β, β m is a rotation of α, and each β i can be changed to β i+1 by increasing one symbol. Then each β i is increasable in T(n, k) to a rotation of α, so each β i ∈ S. But that means β is actually increasable in S to a rotation of α. Since this is true for all β ∈ S, the greedy algorithm starting from α generates a universal cycle of S.
For example, let n = 3, k = 4, and α = 143. Here's a universal cycle for the strings in T(3, 4) increasable to a rotation of 143:

Unions
Let S 1 , S 2 ∈ G(n, k), where S 1 and S 2 are both closed under rotations. Assume all strings in S 1 and S 2 are increasable (in their respective sets) to rotations of a single string α. Then all strings in S 1 ∪ S 2 are increasable in S 1 ∪ S 2 to a rotation of α, so the greedy algorithm starting from α generates a universal cycle of S 1 ∪ S 2 .
This raises the question of whether the same can be said of intersections. However, this turns out to be false: even if the greedy algorithm generates universal cycles for S 1 and S 2 , the same may not be true of S 1 ∩ S 2 . One simple example will demonstrate why. Let S 1 , S 2 ⊆ T(2, 3) be as follows: {11, 12, 21, 23, 32, 33} The greedy algorithm (starting from 33) generates universal cycles for both S 1 and S 2 , but does not do so for S 1 ∩ S 2 = {11, 33}. The problem is that there may be an element β ∈ S 1 ∩ S 2 which is increasable to a rotation of α in both S 1 and in S 2 , but the paths from β to α may be different in each set. (Here, β = 11; we have 11 → 13 → 33 in S 1 , and 11 → 12 → 32 → 33 in S 2 .)

Rotations of increasing strings
Assume that n ≤ k. Let S be the set containing all strictly increasing strings in T(n, k) and their rotations. For example, in T(3, 5), S contains the following strings and their rotations: By definition, S is closed under rotations. Let α be the lexicographically maximal, strictly increasing string in T(n, k): α = (k − n + 1)(k − n + 2) · · · (k − 1)k.
Any string β ∈ S is increasable in S to a rotation of α; the greatest symbol in β can be increased to k, then the next-greatest symbol can be increased to k − 1, and so on. So a universal cycle for S can be generated with the greedy algorithm.

Maximum cyclic increment or cyclic decrement
Choose integers I > 0 and D > 0. Let S ⊆ T(n, k) be the set of strings with no cyclic increment of size greater than I and no cyclic decrement of size greater than D. For example, if we take T(3, 4), I = 2, and D = 1, then S consists of the following strings and their rotations: 112, 122, 132, 222, 223, 233, 243, 333, 334, 344, 444} By definition, S is closed under rotations. Let α = k n ; α contains no cyclic increments or decrements, so α ∈ S.
To show that the greedy algorithm works here: choose any β ∈ S such that β = k n . Assume β = γ 1 aγ 2 , where a is the least symbol in β. Let β ′ = γ 1 (a + 1)γ 2 . This change from β to β ′ will either decrease the size of cyclic increments/decrements, or will produce a new cyclic increment or decrement of size 1 (which is legal). So β ′ ∈ S. Thus, for any β ∈ S, it's possible to increase a symbol of β by 1 to produce another string in S. This process can be continued until α is reached. So any β ∈ S is increasable in S to α, and the greedy algorithm (starting from α) produces a universal cycle.

Minimum span, maximum span
Choose integers m and M such that 0 ≤ m < M < k. Let S ⊆ T(n, k) be the set of strings β whose span is at least m and at most M. (The "span" of a string β is the difference between the least and greatest symbols in β.) For example, if we take T(3, 4), m = 1, and M = 2, then S consists of the following strings and their permutations: {112, 113, 122, 123, 133, 223, 224, 233, 234, 244, 334, 344} Clearly, S is closed under rotations. Any β ∈ S can be increased in S to a rotation of α = (k − m)k n−1 , as follows: if the span of β is greater than m, increase the least symbol of β by 1. If the span of β equals m, increase the greatest symbol of β by 1, unless the greatest symbol is k. Repeat this process until a string β containing the symbol k is reached. At that point, the least symbol in β will be k −m; leave that one symbol alone, and increase all the other symbols of β to k.
Thus, the greedy algorithm generates a universal cycle for S.

Avoiding a substring
Choose a string γ ∈ T(m, k) for some m ≥ 1, and let S ⊆ T(n, k) be the set of strings that do not contain γ as a cyclic substring. It was proven in [2] that if γ does not contain k, then S can be generated by the greedy algorithm. If γ does contain k, then we can still make a weaker statement: let i, j ∈ {1, . . . , k} be two symbols such that i < j. If γ contains i but not j, then S can be generated by the greedy algorithm.
As an example, let S ⊆ T(3, 3) be the set of strings not containing 13 as a cyclic substring. (In this case, i = 1 and j = 2.) Then S consists of the rotations of the following strings: The reason why the greedy algorithm works: k n ∈ S, since the forbidden substring γ includes a symbol a < k. Given any β ∈ S, we can increase β in S to k n , as follows: replace any occurrences of a in β with b, then increase all symbols in β to k. So all strings in S are increasable in S to k n .
It would seem to be a difficult question to completely categorize the forbidden substrings γ for which S can be generated by the greedy algorithm. I have not found any examples of a forbidden substring γ containing a symbol a ≤ k − 2 where the greedy algorithm fails. And I would conjecture that there are none: Conjecture 1. Let γ ∈ T(m, k) be a string containing a symbol a ≤ k − 2. Let S ⊆ T(n, k) (for some n ≥ m) be the set of strings not containing γ as a cyclic substring. Then the greedy algorithm starting from k n generates a universal cycle for S. Now, if all symbols in γ are either k or k − 1, then the greedy algorithm may fail. Let S ⊆ T(n, 9) (for some n ≥ 4) be the set of strings not containing a particular γ as a cyclic substring. I leave it as an exercise to the interested reader to show that the greedy algorithm (starting from α = 9 n ) succeeds if γ is in the following set...

{8899, 8989}
... but the greedy algorithm fails if γ is in the following set.
{89, 889, 899, 8889, 8999} Note: for some strings γ, the outcome depends on the value of n. One example is γ = 8998; the greedy algorithm will fail if and only if n is a multiple of 3. (All strings in S will be increasable in S to 9 n , except for β = (889) n/3 and its rotations.)

Conclusion and future work: necklaces
At this point, we have classified the subsets S ⊆ T(n, k), closed under rotations, where the greedy algorithm produces a universal cycle for S. But there's one major problem with generating universal cycles with the greedy algorithm: we must store the entire cycle (which could be exponential in length) in order to generate the cycle. Fortunately, when S = T(n, k), there is a faster method: Given α ∈ T(n, k), α is a "necklace" if, out of all rotations of α, α itself is the lexicographically earliest such rotation. If we take all such necklaces in lexicographic order, and append their aperiodic prefixes, we obtain a de Bruijn cycle for T(n, k) (the same cycle produced by the greedy algorithm). For example, here's the resulting universal cycle for T(3, 3) (with spaces added between the prefixes): ( 333)1 112 113 122 123 132 133 2 223 233 3 In [2], this is called the FKM algorithm. It is proved in [2] that this algorithm produces a universal cycle for S ⊆ T(n, k) (the same universal cycle produced by the greedy algorithm) if 1. S is closed under rotations, and 2. every necklace in S remains a necklace in S if the suffix of length i (whenever 1 ≤ i ≤ n) is replaced with k i . (When this second condition holds true, S is referred to as a "k-suffix language".) However, this is not an "if and only if" situation. The following example of a set S ⊆ T(4, 3) is given in [2]: So it is natural to ask whether there is a necessary and sufficient condition on S ⊆ T(n, k) so that the FKM algorithm generates a universal cycle for S. I have a possible candidate for just such a condition: Let's generalize the concept of a k-suffix language as follows. Given a string α ∈ T(n, k), call a set S ⊆ T(n, k) an "α-suffix language" if, for any β ∈ S, each symbol in β is at most the corresponding symbol in α, and we obtain another element of S if we replace any suffix of β with an equal-length suffix of α. That is, if β = b 1 · · · b n and α = a 1 · · · a n , then for all m such that 1 ≤ m ≤ n, we have b m ≤ a m and b 1 · · · b m−1 a m · · · a n ∈ S. With this definition in place, I would conjecture the following: Conjecture 2. Let S ⊆ T(n, k) be a set that is closed under rotations. Then the FKM algorithm generates a universal cycle for S if and only if the set of necklaces in S is an α-suffix language, where α is the lexicographically maximal necklace in S.
The reason for this conjecture: assume that S ⊆ T(n, k) is closed under rotations and is an α-suffix language, where α is the lexicographically maximal necklace in S. Under these circumstances, it appears that the prisoner has a strategy such that, if the current position is a necklace β ∈ S, the prisoner can ensure that the next necklace position reached is lexicographically earlier than β. Thus, in the universal cycle generated by the greedy algorithm, the necklaces appear in lexicographic order. Perhaps then, the FKM algorithm generates the same cycle as the greedy algorithm.