Skip to main content

The Coolest Way to Generate Binary Strings

Abstract

Pick a binary string of length n and remove its first bit b. Now insert b after the first remaining 10, or insert \(\overline{b}\) at the end if there is no remaining 10. Do it again. And again. Keep going! Eventually, you will cycle through all 2n of the binary strings of length n. For example, are the binary strings of length n=4, where and . And if you only want strings with weight (number of 1s) between and u? Just insert b instead of \(\overline{b}\) when the result would have too many 1s or too few 1s. For example, are the strings with n=4, =0 and u=2. This generalizes ‘cool-lex’ order by Ruskey and Williams (The coolest way to generate combinations, Discrete Mathematics) and we present two applications of our ‘cooler’ order. First, we give a loopless algorithm for generating binary strings with any weight range in which successive strings have Levenshtein distance two. Second, we construct de Bruijn sequences for (i) =0 and any u (maximum specified weight), (ii) any and u=n (minimum specified weight), and (iii) odd u (even size weight range). For example, all binary strings with n=6, =1, and u=4 appear once (cyclically) in . We also investigate the recursive structure of our order and show that it shares certain sublist properties with lexicographic order.

This is a preview of subscription content, access via your institution.

Fig. 1
Fig. 2
Fig. 3
Fig. 4
Algorithm 1
Algorithm 2
Algorithm 3
Algorithm 4
Fig. 5
Fig. 6

Notes

  1. 1.

    When consulting these various applications, it should be noted that they may use different modifications of cool-lex order including reflecting the order or strings, reversing the bits in each string, or complementing the bits in each string.

References

  1. 1.

    Berstel, J., Perrin, D.: The origins of combinatorics on words. Eur. J. Comb. 28, 996–1022 (2007)

    Article  MATH  MathSciNet  Google Scholar 

  2. 2.

    Chase, P.J.: Combination generation and graylex ordering. Congr. Numer. 69(19), 215–242 (1989)

    MathSciNet  Google Scholar 

  3. 3.

    de Bruijn, N.G.: A combinatorial problem. Proc. K. Ned. Acad. Wet., Ser. A, Math. Sci. 49, 758–764 (1946)

    MATH  Google Scholar 

  4. 4.

    Durocher, S., Li, P.C., Mondal, D., Ruskey, F., Williams, A.: Cool-lex order and k-ary Catalan structures. J. Discrete Algorithms 16, 287–307 (2012)

    Article  MATH  MathSciNet  Google Scholar 

  5. 5.

    Duval, J.P.: Génération d’une section de classes de conjugasion et arbre des mots de Lyndon de lonueur bornée. Theor. Comput. Sci. 60, 255–283 (1988)

    Article  MATH  MathSciNet  Google Scholar 

  6. 6.

    Eades, P., McKay, B.: An algorithm for generating subsets of fixed size with a strong minimal change property. Inf. Process. Lett. 19, 131–133 (1984)

    Article  MATH  MathSciNet  Google Scholar 

  7. 7.

    Fredericksen, H., Kessler, I.J.: An algorithm for generating necklaces of beads in two colors. Discrete Math. 61, 181–188 (1986)

    Article  MathSciNet  Google Scholar 

  8. 8.

    Fredericksen, H., Maiorana, J.: Necklaces of beads in k colors and kary de Bruijn sequences. Discrete Math. 23(3), 207–210 (1978)

    Article  MathSciNet  Google Scholar 

  9. 9.

    Gray, F.: Pulse code communication. US Patent 2,632,058, 1947

  10. 10.

    Hurlbert, G., Isaak, G.: On the de Bruijn torus problem. J. Comb. Theory 61(1), 50–62 (1995)

    Google Scholar 

  11. 11.

    Johnson, R.J.: Long cycles in the middle two layers of the discrete cube. J. Comb. Theory, Ser. A 105(2), 255–271 (2004)

    Article  MATH  Google Scholar 

  12. 12.

    Knuth, D.E.: The Art of Computer Programming, Generating All Tuples and Permutations, vol. 4 fascicle 2. Addison-Wesley, Reding (2005). errata (updated 10/02/2008) edition. ISBN 0-201-85393-0

    Google Scholar 

  13. 13.

    Knuth, D.E.: The Art of Computer Programming, Generating All Combinations and Partitions, vol. 4 fascicle 3. Addison-Wesley, Reading (2005). errata (updated 10/02/2008) edition. ISBN 0-201-85394-9

    Google Scholar 

  14. 14.

    Knuth, D.E.: The Art of Computer Programming. Combinatorial Algorithms, vol. 4, Part 1. Addison-Wesley, Reading (2010)

    Google Scholar 

  15. 15.

    Martin, M.H.: A problem in arrangements. Bull. Am. Math. Soc. 40, 859–864 (1934)

    Article  Google Scholar 

  16. 16.

    Ruskey, F.: Adjacent interchange generation of combinations. J. Algorithms 9(2), 162–180 (1988)

    Article  MATH  MathSciNet  Google Scholar 

  17. 17.

    Ruskey, F., Savage, C., Wang, T.: Generating necklaces. J. Algorithms 13, 414–430 (1992)

    Article  MATH  MathSciNet  Google Scholar 

  18. 18.

    Ruskey, F., Sawada, J., Williams, A.: Binary bubble languages and cool-lex Gray codes. J. Comb. Theory, Ser. A 119(1), 155–169 (2012)

    Article  MATH  MathSciNet  Google Scholar 

  19. 19.

    Ruskey, F., Sawada, J., Williams, A.: De Bruijn sequences for fixed-weight binary strings. SIAM J. Discrete Math. 26(2), 605–617 (2012)

    Article  MATH  MathSciNet  Google Scholar 

  20. 20.

    Ruskey, F., Williams, A.: The coolest way to generate combinations. Discrete Math. 309(17), 5305–5320 (2009)

    Article  MATH  MathSciNet  Google Scholar 

  21. 21.

    Flye Sainte-Marie, C.: Solution to question nr. 48. L’Intermédiaire Math. 1, 107–110 (1894)

    Google Scholar 

  22. 22.

    Savage, C., Winkler, P.: Monotone Gray codes and the middle levels problem. J. Comb. Theory, Ser. A 70(2), 230–248 (1995)

    Article  MATH  MathSciNet  Google Scholar 

  23. 23.

    Sawada, J., Stevens, B., Williams, A.: De Bruijn sequences for the binary strings with a maximum density. In: WALCOM 2011: The 5th International Workshop on Algorithms and Computation. Lecture Notes in Computer Science, New Delhi, India, vol. 6552, pp. 182–190. Springer, Berlin (2011)

    Google Scholar 

  24. 24.

    Sawada, J., Williams, A.: Efficient oracles for generating binary bubble languages. Electron. J. Comb. 19, Paper 42 (2012)

    MathSciNet  Google Scholar 

  25. 25.

    Sawada, J., Williams, A.: A Gray code for fixed-density necklaces and Lyndon words in constant amortized time. Theor. Comput. Sci. (2012, in press). doi:10.1016/j.tcs.2012.01.013

  26. 26.

    Stevens, B., Williams, A.: The coolest order of binary strings. In: Kranakis, E., Krizanc, D., Luccio, F. (eds.) FUN ’12: Proceedings of the Sixth International Conference on Fun with Algorithms. Lecture Notes in Computer Science, San Servolo Island, Venice, Italy, vol. 7288, pp. 322–333. Springer, Berlin (2012)

    Google Scholar 

  27. 27.

    Williams, A.: Loopless generation of multiset permutations using a constant number of variables by prefix shifts. In: SODA ’09: The Twentieth Annual ACM-SIAM Symposium on Discrete Algorithms, New York, USA (2009)

    Google Scholar 

  28. 28.

    Williams, A.: Shift Gray codes. PhD thesis, in Computer Science, University of Victoria (2009)

Download references

Author information

Affiliations

Authors

Corresponding author

Correspondence to Aaron Williams.

Additional information

Research supported in part by the NSERC Accelerator and Discovery Programmes, and a basic research grant from ONR.

Rights and permissions

Reprints and Permissions

About this article

Cite this article

Stevens, B., Williams, A. The Coolest Way to Generate Binary Strings. Theory Comput Syst 54, 551–577 (2014). https://doi.org/10.1007/s00224-013-9486-8

Download citation

Keywords

  • Cool-lex order
  • Gray code
  • Binary strings
  • Combinatorics on words
  • Necklace prefix algorithm
  • FKM algorithm
  • De Bruijn sequence
  • Universal cycle
  • Hamming distance
  • Levenshtein distance