Generating Gray Codes in O(1) Worst-Case Time per Word

  • Timothy Walsh
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 2731)


We give a definition of Gray code that, unlike the standard “minimal change” definition, is satisfied by the word-lists in the literature called “Gray codes” and we give several examples to illustrate the various concepts of minimality. We show that a non-recursive generation algorithm can be obtained for a word-list such that all the words with the same prefix (or, equivalently, suffix) are consecutive and that the Bitner-Ehrlich-Reingold method of generating each word in a time bounded by a constant works under the additional condition that in the interval of words with the same prefix or suffix the next letter assumes at least two values. Finally we generalize this method so that it works under a weaker condition satisfied by almost all the Gray codes in the literature: if the next letter assumes only one value, then the interval contains only one word.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Gray, F.: Pulse Code Communication. U.S. Patent 2 632 058 (March 17, 1953)Google Scholar
  2. 2.
    Liu, C.N., Tang, D.T.: Algorithm 452, Enumerating M out of N objects. Comm. ACM 16 (1973) 485CrossRefGoogle Scholar
  3. 3.
    Eades, P., McKay, B.: An Algorithm for Generating Subsets to Fixed Size with a Strong Minimal Interchange Property. Information Processing Letters 19 (1984) 131–133MATHCrossRefMathSciNetGoogle Scholar
  4. 4.
    Johnson, S.M.: Generation of Permutations by Adjacent Transpositions. Mathematics of Computation 17 (1963) 282–285MATHCrossRefMathSciNetGoogle Scholar
  5. 5.
    Trotter, H.F.: Algorithm 115: Perm. Comm. ACM 5 (1962) 434–435CrossRefGoogle Scholar
  6. 6.
    Nijenhuis, A., Wilf, H.S.: Combinatorial Algorithms for Computers and Calculators, second edition. Academic Press, N.Y. (1978)MATHGoogle Scholar
  7. 7.
    Chase, P.J.: Combination Generation and Graylex Ordering. Proceedings of the 18th Manitoba Conference on Numerical Mathematics and Computing, Winnipeg, 1988. Congressus Numerantium 69 (1989) 215–242MathSciNetGoogle Scholar
  8. 8.
    Ruskey, F.: Simple Combinatorial Gray Codes Constructed by Reversing Sublists. L.N.C.S. 762 (1993) 201–208MathSciNetGoogle Scholar
  9. 9.
    Vajnovszki, V., Walsh, T.R.: A loopless two-close Gray code algorithm for listing k-ary Dyck words. Submitted for publicationGoogle Scholar
  10. 10.
    Ruskey, F., Proskurowski, A.: Generating Binary Trees by Transpositions. J. Algorithms 11 (1990) 68–84MATHCrossRefMathSciNetGoogle Scholar
  11. 11.
    Bultena, B., Ruskey, F.: An Eades-McKay Algorithm for Well-Formed Parentheses Strings. Inform. Process. Lett. 68 (1998), no. 5, 255–259CrossRefMathSciNetGoogle Scholar
  12. 12.
    Savage, C.: Gray Code Sequences of Partitions. Journal of Algorithms 10 (1989) 577–595MATHCrossRefMathSciNetGoogle Scholar
  13. 13.
    Walsh, T.R.: A Simple Sequencingan d Ranking Method that Works on Almost All Gray Codes. Research Report No. 243, Department of Mathematics and Computer Science, Université du Québec à Montréal (April 1995)Google Scholar
  14. 14.
    Ehrlich, G.: Loopless Algorithms for Generating Permutations, Combinations, and Other Combinatorial Configurations: J. ACM 20 (1973) 500–513MATHCrossRefMathSciNetGoogle Scholar
  15. 15.
    Bitner, J.R., Ehrlich, G., Reingold, E.M.: Efficient Generation of the Binary Reflected Gray Code and its Applications. Comm. ACM 19 (1976) 517–521MATHCrossRefMathSciNetGoogle Scholar
  16. 16.
    Joichi, J.T., White, D.E., Williamson, S.G.: Combinatorial Gray Codes. SIAM J. Computing 9 (1980) 130–141MATHCrossRefMathSciNetGoogle Scholar
  17. 17.
    Walsh, T.R.: Gray Codes for Involutions. JCMCC 36 (2001) 95–118MATHGoogle Scholar
  18. 18.
    Walsh, T.R.: Generation of Well-Formed Parenthesis Strings in Constant Worst-Case Time. J. Algorithms 29 (1998) 165–173MATHCrossRefMathSciNetGoogle Scholar
  19. 19.
    Roelants van Baronaigien, D.: A Loopless Gray-Code Algorithm for Listing k-ary Trees. J. Algorithms 35 (2000), no. 1, 100–107MATHCrossRefMathSciNetGoogle Scholar
  20. 20.
    Williamson, S.G.: Combinatorics for Computer Science. Computer Science Press, Rockville (1985)Google Scholar
  21. 21.
    Roelants van Baronaigien, D.: Constant Time Generation of Involutions. Congressus Numerantium 90 (1992) 87–96MathSciNetGoogle Scholar
  22. 22.
    Vajnovszki, V.: Generating Multiset Permutations. Accepted for publication in Theoretical Computer ScienceGoogle Scholar
  23. 23.
    Klingsberg, P.: A Gray Code for Compositions. Journal of Algorithms 3 (1982) 41–44MATHCrossRefMathSciNetGoogle Scholar
  24. 24.
    Walsh, T.R.: Loop-free sequencing of bounded integer compositions, JCMCC 33 (2000) 323–345MATHGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2003

Authors and Affiliations

  • Timothy Walsh
    • 1
  1. 1.Department of Computer ScienceUQAMMontrealCanada

Personalised recommendations