Gray Codes for Refletable Languages Yue Li Joe Sawada Marh 8, 2008 Abstrat We lassify a type of language alled a refletable language. We then develop a generi algorithm that an be used to list all strings of length n for any refletable language in Gray ode order. The algorithm generalizes Gray ode algorithms developed independently for k-ary strings, restrited growth strings, and k-ary trees, as eah of these objets an be represented by a refletable language. Finally, we apply the algorithm to open meanderi systems whih an also be represented by a refletable language. Introdution The term Gray ode originally referred to a speifi ordering of length n binary strings, patented by Frank Gray [4], where onseutive bitstrings differ by a single bit. The following is an example of this listing for n = 3 where the bit that differs with the previous element is underlined: 000, 00, 0, 00, 0,, 0, 00. Today, the term Gray ode refers more generally to an exhaustive listing of any ombinatorial objet where eah suessive objet differs by some onstant amount. In 997, Savage [9] surveyed many of these Gray ode algorithms and sine then many more have been developed for various ombinatorial objets. For a signifiant number of these algorithms, a ommon strategy of refleting subtrees is applied - a tehnique that is similar in spirit to the original binary refleted Gray ode [4]. To illustrate this notion of refleting subtrees we onsider the set S 3 of all length 3 strings over the alphabet {a, b, } with no bb substring. A straight forward reursive algorithm an be used to generate suh strings in lexiographi order by following a omputation tree like the one in Figure. In the omputation tree, eah leaf represents a unique string that is Computing and Information Siene, University of Guelph, Canada. email: yli04@uoguelph.a Computing and Information Siene, University of Guelph, Canada. Researh supported by NSERC. email: jsawada@uoguelph.a
a b a b a a b a b a a b a b a b a b a a b Figure : Computation tree to generate S 3 in lexiographi order. a b a b a a b a b a a b b a a b b a a b a Figure 2: Computation tree to generate S 3 in Gray ode order. obtained by traing the path from the root to the leaf. Observe that the resulting listing is not a Gray ode sine in some ases suessive strings may differ in all positions. However, by refleting (reversing) the order of the hildren at partiular nodes, as illustrated in Figure 2, we an obtain an ordering of S 3 that is a Gray ode. Some objets for whih this strategy has been applied to inlude: binary [4] and k-ary strings [5, 3], restrited growth funtions [3, 7] and tails [8], and binary [] and k-ary trees [2, 4]. In this paper we generalize what various representations for these objets have in ommon by introduing the notion of refletable languages. We then provide a generi algorithm that an generate all length n words for any refletable language in Gray ode order. As a new appliation, we apply the algorithm to open meandri systems. 2
2 Refletable Languages Definition 2.. A language L over the alphabet Σ is said to be refletable if for every i > there exists two haraters x i and y i in Σ suh that if w w 2 w i is a prefix of a word in L then both w w 2 w i x i and w w i y i are also prefixes of words in L. Reall the language S 3 defined in the previous setion as the set of all length 3 strings over {a, b, } with no bb substring. It is refletable by onsidering x i = a and y i =. As another example, onsider the language L = {a, aa, a, aaa, aab, aa, aa, ab, ad} over Σ = {a, b,, d}. Observe that this language is also refletable by onsidering x 2 = a, y 2 = and x 3 = a, y 3 = b. On the other hand, the language L = {a, aa, a, aaa, aab, aa, ab, aa, ab} is not refletable sine ab L, but no matter what we use for x 3 and y 3 the strings abx 3 and aby 3 an not both be in L. It turns out that many ommon ombinatorial objets an be represented by refletable languages. In the following subsetions we give examples of suh refletable languages by demonstrating their x i and y i values. 2. Binary strings, k-ary strings, and variants Letting Σ = {0,, 2,..., k }, the set of all k-ary strings for k 2 trivially form a refletable language by onsidering x i = 0 and y i =. A generalization of k-ary strings is to onsider elements of the produt spae S = S S 2 S n where eah S i = {0,,..., r i } for i =, 2,..., n. If eah r i 2, then suh a spae will again orrespond to a refletable language by onsidering x i = 0 and y i =. Strings with a forbidden substring α are a variation on k-ary strings that have been studied in [0]. If the forbidden substring α is omposed from a subset of k 2 haraters in the alphabet, then the language of strings with forbidden substring α is refletable. This follows from the definition by assigning any two haraters from the alphabet that do not appear in α to x i and y i respetively. As an example, reall that S 3 orresponds to a refletable language. 2.2 Restrited growth strings Restrited growth strings are strings of non-negative integers w w n satisfying w = 0 and w i + max{w, w 2,..., w i }. There is a well known bijetion between restrited growth strings and set partitions [9]. By letting x i = 0 and y i = for eah i, observe that restrited growth strings of length n are refletable. A slight generalization of restrited growth strings are restrited growth tails [8], whih are strings of non-negative integers w w n satisfying w k and w i + max{w, w 2,..., w i, k }. Observe that this generalization also orresponds to a refletable language by letting x i = 0 and y i =. 3
0 4 2 0 2 4 2 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 8 2 6 0 2 4 2 0 2 4 (a) (b) () Figure 3: (a) Zaks representation n = 6, k = 3. (b) Right distane sequene n = 6, k = 3. () Weight sequene for a binary tree with n = 6. 2.3 Binary and k-ary trees A k-ary tree is a tree where eah internal node has k ordered subtrees. A ommon bitsequene representation for k-ary trees is obtained by visiting a tree in pre-order where a is assigned to eah internal node and a 0 is assigned to eah leaf. This representation is often attributed to Zaks [5]. Using this pre-order traversal, a unique bit-sequene of length kn+ is obtained for eah k-ary tree with n internal nodes. Sine there are so many zeros in the bit sequene representation, it is often useful to use an alternate representation where only the positions of the ones are reorded. For example, a tree and its bit sequene representation is given in Figure 3(a). Using this representation a sequene a a 2 a n will orrespond to a k-ary tree if a = and for eah i > 0 we have a i < a i k(i ) + [2]. Observe that suh a set suh strings orresponds to a refletable language where x i = k(i ) and y i = k(i ) +. Another representation is presented in [4] where the nodes in a k-ary tree are defined reursively as follows: the root node is assigned 0, then for eah hild i =,..., k from left to right we assign the value k i plus the value of its parent. A pre-order traversal yields what is alled the right-distane sequene for a k-ary tree. An example is illustrated in Figure 3(b). Using this representation, any sequene orresponding to a tree with n nodes an be extended to a tree with n + nodes by appending any value between 0 and k [4]. Thus, the set of all right distane sequenes is a refletable language where x i = 0 and y i =. In the speial ase of binary trees (k = 2), we an assign a weight to eah vertex orresponding to the number of leaves in its left subtree. The sequene that results by reording these weights via an in-order traversal is the weight sequene introdued by Pallo [6]. An example is illustrated in Figure 3(). Pallo also shows a sequene w w 2... w n will orrespond uniquely to the weight sequene for a binary tree with n nodes if for eah i we have w i i, and for eah i w i + j i we have i w i j w j. Notie that this set of weight sequenes orresponds to a refletable language where x i = and y i = i. 4
proedure GrayCode (t) if (t > n) then Proess(w) else r := w t //w t will be either x t or y t GrayCode(t+) for eah z Σ {x t, y t } suh that w w t z is a prefix of some word in L n w t := z GrayCode(t+) end if (r = x t ) then w t := y t else w t := x t GrayCode(t+) Figure 4: Algorithm, GrayCode(t), to list all words of length n from a refletable language L in Gray ode order. 2.4 Open meandri systems We demonstrate that open meandri systems an be represented by a refletable language in Setion 4. 3 A Simple Gray Code Algorithm In this setion, we present a simple reursive algorithm to list L n (words of length n for some refletable language L) in Gray ode order. This algorithm generalizes independently developed algorithms for: binary strings [4], k-ary strings and ross produts [5, 3], restrited growth strings [3, 7], restrited growth tails [8], and binary [] and k-ary trees [2, 4]. These individual algorithms are still of interest, however, sine they often inlude extra effiieny onsiderations that are speifi to eah objet. For instane, adding data strutures to make the algorithm run in onstant amortized time or demonstrating a loop-free implementation. The basi idea behind the generi reursive algorithm is to apply the simple idea of refleting partiular subtrees that was disussed earlier. To do this, we use the speial haraters x i and y i as the first and last hildren of eah node at level i. The order of the other haraters (hildren) does not matter. This way, at the start of eah reursive all we an be sure that that the previous word generated had either the harater x i or y i at position i. Pseudoode is shown in Figure 4. The word being generated is stored in w = w w 2 w n. The urrent position is given by the parameter t and the variables x i and y i are speifi to the refletable language under onsideration as desribed in Definition 2.. To run the algorithm w 2 w n is initialized to x 2 x 3 x n, then for eah z Σ that starts a word in L n we assign w := z and all GrayCode(2). 5
To illustrate the algorithm, again reall the language S 3 whih onsists of all length 3 strings over the alphabet {a, b, } with no bb substring. By applying x i = a and y i =, the omputation tree that results from applying algorithm GrayCode(t) is given in Figure 2. Theorem 3.. For any refletable language L and given integer n, the algorithm GrayCode(t) will produe a list of all words L n in Gray ode order. Proof: First, it should be lear that every word in L n is generated exatly one by the algorithm. Thus, we need only show that suessive words in the resulting listing differ in exatly one position. Consider any two suh suessive words α = a a n and β = b b n. Suppose that their first ommon anestor in the omputation tree is at level i. Then a a i = b b i and a i+ b i+. Now sine the first reursive all in GrayCode(t) does not hange the value for urrent position t and sine the last reursive all never leads to a dead end, we must have a i+2 a n = b i+2 b n. Thus, every pair of suessive words generated will differ in exatly one position, i.e., the listing is a Gray ode. 3. Analyzing the generi Gray ode algorithm To analyze the generi Gray ode algorithm GrayCode(t), we perform an amortized analysis omparing the running time to the number of objets generated. For this analysis, the best we an hope for is an algorithm that runs in onstant amortized time. One assumption made in this analysis is that the time taken by the funtion Proess(w) is onstant, sine for many appliations this may be the ase. Now, fousing on the algorithm itself, observe that every non-leaf node in the omputation tree has at least two hildren sine reursive alls must be made for both x t and y t. Also, observe from the prefix test in the for loop that there will be no dead-ends, whih means that every leaf will be at level n of the omputation tree and will orrespond to a word in L n. Thus, if the time taken at eah internal node of omputation is proportional to the number of hildren (reursive alls made), then the overall running time will be proportional to the number of nodes in the omputation tree. Sine the branhing fator of eah internal node is at least 2, the number of leaves (words generated) will be greater than the number of internal nodes in whih ase the algorithm will run in onstant amortized time. Theorem 3.2. The algorithm GrayCode(t) runs in onstant amortized time if the following two onditions hold:. heking whether or not w w t z is a prefix of some word in L n takes O() time and 2. eah internal node in the omputation tree has Ω( Σ ) hildren. In several of the examples of refletable languages seen so for, the alphabet symbols that are possible at ertain positions in eah word may vary. Thus, in an analysis it may be more appropriate to onsider an alphabet Σ i for eah level i n of the omputation tree. The size of these alphabets ould then be applied to Theorem 3.2 rather than the more general alphabet Σ. 6
o d u d u o d u u (a) (b) Figure 5: (a) An open meander of order 6. (b) An open meandri system of order 6 with 3 urves. 4 A New Appliation: Open Meandri Systems An open meander an be thought of as an infinite meandering river whih passes beneath a series of bridges of an infinite straight road going from west to east. For example, Figure 5(a) illustrates an open meander with 6 bridges. Using a urve to represent the river and a line to represent the road, we an generalize the notion of an open meander by allowing multiple non-interseting urves to ross the line. Suh a onfiguration is alled an open meandri system (OMS). The order of an OMS is defined to be the number of times the urves ross the line. An example of an open meandri system of order 6 with 3 urves is shown in Figure 5(b). Enumeration sequenes for open meandri systems were studied by Baher [] and a fast algorithm for generating open meandri systems appears in [2]. The latter paper uses the alphabet Σ = {u,d,o,} to represent the four different types of rossings: u=up, d=down, o=open, =lose. Using this representation, eah OMS of order n an be represented uniquely by a word of length n over Σ as illustrated in Figure 5. Observe that any OMS of order n an always be extended into an OMS of order n + by appending either an o, d, or u. Only when adding a to an existing OMS is it possible that that a previously open urve an beome losed [2]. Thus, the language of all OMSs is refletable by setting x i =d and y i =u. The algorithm in [2] to generate all OMSs runs in onstant amortized time due to the introdution of data strutures that an test when it is possible to append a to an existing OMS in onstant time. Therefore, it is possible to diretly apply our generi Gray ode algorithm to onvert their lexiographi algorithm into a Gray ode algorithm. By applying the same data strutures the resulting Gray ode algorithm will ahieve the same asymptoti running time as the original algorithm. The result also follows from Theorem 3.2. Corollary 4.. A Gray ode for open meandri systems of order n an be generated in onstant amortized time. As an illustration of the resulting Gray ode algorithm, we show a partial omputation tree for n = 4 in Figure 6. The dead ends are shown by the dotted edges. For example, the words o, uo and oud are all invalid OMSs beause they inlude a losed urve. 7
d o u d o u u o d d o u u o d do uuoddo u uod do u uod do u uoddo uuod do uuoddo uuod do u do u uod do u uod do u Figure 6: The partial tree for OMS of order 4 that results from applying the generi Gray ode algorithm with x i =d and y i =u. Referenes [] R. Baher. Meander algebras. Prepubliation de l Institut Fourier, 478, 999. [2] B. Bobier and J. Sawada. A fast algorithm to generate open meandri systems. Manusript https://www.is.uoguelph.a/pubs/meander.pdf, 2007. [3] G. Ehrlih. Loopless algoirthms for generating permutations, ombinations, and other ombinatorial onfigurations. Journal of the ACM, 20:500 53, 973. [4] F. Gray. Pulse ode ommuniation. U.S. Patent, 2632058, 953. [5] G. Manku and J. Sawada. A loopless Gray ode for minimal signed-binary representations. In Pro. 3th Annual European Symposium on Algorithms (ESA 2005) LNCS, pages 438 447, Ot 2005. [6] J. M. Pallo. Enumerating, ranking and unranking binary trees. The Computer Journal, 29(2):7 75, 986. [7] F. Ruskey. Combinatorial Generation. Manusript, 200. [8] F. Ruskey and C. Savage. Gray odes for set partitions and restrited growth tails. Australasian Journal of Combinatoris, 0:85 96, 994. [9] C. Savage. A survey of ombinatorial Gray odes. SIAM Review, 39(4):605 629, 997. [0] M. Squire. Gray odes for a-free strings. Eletroni Journal of Combinatoris, 3(), 996. [] V. Vajnovszki. On the loopless generation of binary tree sequenes. Information Proessing Letters, 68:3 7, 998. 8
[2] D. Roelants van Baronaigien. A loopless Gray-ode algorithm for listing k-ary trees. Journal of Algorithms, 35:00 07, 2000. [3] S. Williamson. Combinatorial for Computer Siene. Computer Siene Press, 985. [4] R. Wu, J. Chang, and Y. Wang. Ranking, unranking and loopless Gray-odes generation of t-ary trees. Manusript, 2007. [5] S. Zaks. Generation and ranking or k-ary trees. Information Proessing Letters, 4():44 48, 982. 9