A Dynamic Programming Approach for Generating N-ary Reflected Gray Code List Mehmet Kurt 1, Can Atilgan 2, Murat Ersen Berberler 3 1 Izmir University, Department of Mathematics and Computer Science, Izmir 2, 3 Dokuz Eylul University, Department of Computer Science, Izmir Abstract In this work, some dynamic programming approaches are proposed to generate binary gray codes, which have various fields of application, as well as n-ary gray codes and anti-gray codes. Algorithms are described and programmed to carry out computational tests. Keywords : Binary gray code, N-ary gray code, anti-gray code, dynamic programming 1. Introduction The gray code is named after Frank Gray, who patented the use of them in shaft encoders in 1953 [1]. The gray code is a code with the property that there is only one bitchange between any 2 consecutive numbers. This unique property allows gray codes to be used for linear and rotary shaft position encoding, labeling the axes of Karnaugh maps, solving puzzle problems such as Tower of Hanoi, processing of mutations of genetic algorithms efficiently, detecting and correcting errors in digital data transmission [2]. Generating gray codes is a well-studied task and various solution approaches such as, recursive, straightforward, dynamic programming approaches are brought [3-8]. In some works, a generalization of the gray code is also studied. The generalized gray code is known as n-ary gray code, in which numbers are represented in any base n [9, 10]. The rest of the paper is organized as follows. A dynamic programming approach for generating binary reflected gray codes is given in section 2. The approach is then expanded to produce n-ary reflected gray codes in section 3, and producing anti-gray codes (i.e. total number of bit-changes is maximum) using previously generated binary gray codes is discussed in section 4. Finally, section 5 concludes the paper. 2. Binary Reflected Gray Code Algorithm Consider a set A that has n elements. There are 2 n subsets of set A and they can be mapped using integer indices, where 0 is the empty set and 2 n -1 is the entire set A. Binary representations of these indices can be consecutively generated by toggling the rightmost bit at every step, second-rightmost bit at every second step and likewise the leftmost bit, i.e. nth bit at (2 n-1 )th step. By the nature of dynamic programming, a matrix consisting of n2 n elements is generated. The first row of the matrix is a zero vector. For each index value, corresponding row in the matrix is incrementally updated using the loop below. Note that each row will hold the binary representation of its index.
for i 1 to 2 n do for j 1 to n do if (i mod 2 j = 0) b[j] (b[j] XOR 1); Same approach is used to generate binary reflected gray code. As shown in Fig.1., jth column of gray code binary string of length n is actually stored in the vertical block starting with 2 j+1 th index of (j+1)th column of (n+1)-length ordinary binary code. Exploiting this pattern, the following loop is used to generate gray code with a dynamic programming sense. for i 1 to 2 n do for j 1 to n do if (i mod 2 j+1 = 2 j ) g[j] (g[j] XOR 1); Fig. 1.
Algorithm is programmed in C language and computational tests are done for some n values. Results are seen in Table 1. Column Full involves CPU times for the tests where the entire set with 2 n indices is generated straightforward, and column Half involves the CPU times for the tests where the subset with the first 2 n-1 indices (that is, the half of the set) is generated and the rest is completed by taking advantage of symmetry in reflected gray code. Table 1. CPU Time (sec) n Full Half 15 0,000 0,000 16 0,000 0,000 17 0,031 0,025 18 0,046 0,031 19 0,109 0,046 20 0,223 0,109 21 0,436 0,218 22 0,993 0,468 23 2,001 1,007 24 4,077 2,100 25 8,421 4,245 26 17,343 8,759 27 35,990 18,081 28 74,403 37,206 29 163,608 77,157 3. N-ary Reflected Gray Code Algorithm In this chapter, symmetry property is exploited for proposing a new approach. This generalized approach will allow to generate reflected gray codes in any radix and length. The rightmost bit is the most significant but in a symmetric gray code, since the entire structure can be constructed starting from this bit. The rightmost bit repeats symmetric sequences such as (0, 1, 1, 0) in binary, (0, 1, 2, 2, 1, 0) in ternary, (0, 1, 2, 3, 3, 2, 1, 0) in quaternary, etc. Using this observation, an array with 2m elements, where m is the number base, is defined and filled in with symmetric sequences of the rightmost bit. In order to use at the later steps of the algorithm, an array representing the indices is created and filled with zeros. In the following, the loop for filling the arrays is given. for j 0 to m-1 do k[j] 0; g[j] j; g[2*m-1-j] j; Given a radix and a length, notated with m and n respectively, the loop below will generate the desired n-ary reflected gray code.
for i 0 to m n do for j 0 to n-1 do if ((i>0) AND (i mod m j = 0)) k[j] (k[j]+1) mod (2*m); print(g[k[j]]); Results of the computational tests for 3-ary and 4-ary reflected gray codes are shown in Table 2. Table 2. CPU Time (sec) n 3-ary 4-ary 10 0,000 0,093 11 0,015 0,453 12 0,062 1,953 13 0,203 8,500 14 0,656 36,421 15 2,093 158,265 16 6,703 679,859 4. Anti-Gray Code Another interesting task is generating a binary list whose consecutive lines have the maximum number of bit-changes. Such a list is called an anti-gray code. Binary gray code can easily be used to generate anti-gray code. Evidently, the maximum number of bit-changes between 2 consecutive lines is the given length n. An obvious example is a line and it s complement. For an n-length anti-gray code, there can be 2 n /2 = 2 n-1 pairs of lines, which are complements of each other. It is illustrated below, in Fig. 2. Fig. 2. A previously generated binary gray code list can be used to sort these 2 n-1 pairs and obtain an anti-gray code. Since only 1 bit is toggled between 2 consecutive lines of a gray code, there are n-1 different bits between the complement of the first line and the second line itself. Hence, there can be 2 n-1-1 consecutive lines yielding (n-1) bit-changes. In total, there
will be n2 n-1 + (n - 1) (2 n-1-1) bit-changes, which is the maximum possible number of changes (See Table 3). Table 3. 5. Conclusion The dynamic programming approaches in this paper are easy to implement. If the entire gray code list is not obligatory to be present at a time, a small memory space to hold merely two lines will be sufficient. Contrarily, if memory is not an issue, the gray code list can be swiftly generated without the overhead. The algorithms presented in the paper are programmed and uploaded to http://kisi.deu.edu.tr/murat.berberler/gray/. References [1] F. Gray, Pulse code communication, U.S. Patent 2 632 058, Mar. 17, 1953. [2] A. Ahmad, F. Bait-Shiginah, A Nonconventional Approach To Generating Efficient Binary Gray Code Sequences, IEEE Potentials, vol. 31, issue 3, pp. 16-19, 2012. [3] J. Boothroyd, Algorithm 246, Gray code, Comm. ACM 7, 12, pp. 701, 1964. [4] J. Misra, Remark on Algorithm 246, ACM Trans. Math. Software 1, 3, pp. 285, 1975. [5] E.N. Gilbert, Gray codes and paths on the n-cube. Bell Syst. Tech. J. 37, 9, pp. 815-826, 1958. [6] G. Ehrlich, Algorithm 466, Four combinatorial algorithms, Comm. ACM16, 1, pp. 690-691, 1973. [7] J.W. Lenstra, A. H. G. Rinnooy Kan, A recursive approach to the generation of combinatorial configurations, Rep. BW 50/75, Mathematisch Centrum, Amsterdam, 1975. [8] A. T. Phillips, M. R. Wick, A Dynamic Programming Approach to Generating a Binary Reflected Gray Code Sequence, Proc. 38th Annual Midwest Instruction and Computing Symposium, Eau Claire, WI, 2005. [9] M.C. Er, On generating the N-ary reflected gray codes, IEEE Transactions on Computers, vol. 33, 8, pp. 739-741, 1984. [10] I. Stojmenovic, Generating n-ary reflected gray codes on a linear array of processors, Parallel Processing Letters, vol. 6, 1, pp. 27-34, 1996.