Skip to main content
Log in

A loopless algorithm for generating (km)-ary trees in Gray code order

  • Original Paper
  • Published:
Optimization Letters Aims and scope Submit manuscript

Abstract

A family of (km)-ary trees was firstly introduced by Du and Liu when they studied hook length polynomial for plane trees. Recently, Amani and Nowzari-Dalini presented a generation algorithm to produce (km)-ary trees of order n encoding by Z-sequences in reverse lexicographic order. In this paper, we propose a loopless algorithm to generate all such Z-sequences in Gray code order. Hence, the worst-case time complexity of generating one Z-sequence is \({\mathcal {O}}(1)\), and the space requirement of our algorithm is \(2n+{\mathcal {O}}(1)\). Moreover, based on this ordering, we also provide ranking and unranking algorithms. The ranking algorithm can be run in \({\mathcal {O}}(\max \{kmn,n^2\})\) time and \({\mathcal {O}}(kmn)\) space, whereas the unranking algorithm requires \({\mathcal {O}}(kmn^2)\) time and space.

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

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

Similar content being viewed by others

References

  1. Amani, M., Nowzari-Dalini, A.: Efficient generation, ranking, and unranking of \((k, m)\)-ary trees in B-order. Bull. Iranian Math. Soc. 45(4), 1145–1158 (2019)

    Article  MathSciNet  Google Scholar 

  2. Chang, Y.-H., Wu, R.-Y., Chang, R.-S., Chang, J.-M.: Improved algorithms for ranking and unranking \((k,m)\)-ary trees in B-order. J. Combin. Optim. (2020). https://doi.org/10.1007/s10878-019-00469-z

    Article  Google Scholar 

  3. Doković, D.Ž., Kotsireas, I., Recoskie, D., Sawada, J.: Charm bracelets and their application to the construction of periodic Golay pairs. Discrete Appl. Math. 188, 32–40 (2015)

    Article  MathSciNet  Google Scholar 

  4. Dragon, P.B., Hernandez, O.I., Sawada, J., Williams, A., Wong, D.: Constructing de Bruijn sequences with co-lexicographic order: the k-ary Grandmama sequence. Euro. J. Combin. 72, 1–11 (2018)

    Article  MathSciNet  Google Scholar 

  5. Du, R.R.X., Liu, F.: \((k, m)\)-Catalan numbers and hook length polynomials for plane trees. Euro. J. Combin. 28(4), 1312–1321 (2007)

    Article  MathSciNet  Google Scholar 

  6. Ehrlich, G.: Loopless algorithms for generating permutations, combinations, and other combinatorial objects. J. ACM 20(3), 500–513 (1973)

    Article  Google Scholar 

  7. Hartman, P., Sawada, J.: Ranking and unranking fixed-density necklaces and Lyndon words. Theoret. Comput. Sci. 791, 36–47 (2019)

    Article  MathSciNet  Google Scholar 

  8. Johnson, S.M.: Generation of permutations by adjacent transposition. Math. Comput. 17(83), 282–285 (1963)

    Article  MathSciNet  Google Scholar 

  9. Karim, S., Sawada, J., Alamgir, Z., Husnine, S.: Generating bracelets with fixed content. Theoret. Comput. Sci. 475, 103–112 (2013)

    Article  MathSciNet  Google Scholar 

  10. Korsh, J.F., LaFollette, P.: Loopless generation of Gray code for \(k\)-ary trees. Inform. Process. Lett. 70(1), 7–11 (1999)

    Article  MathSciNet  Google Scholar 

  11. Kreher, D.L., Stinson, D.R.: Combinatorial Algorithms: Generation, Enumeration, and Search. CRC Press, Rockville (1999)

    MATH  Google Scholar 

  12. Pai, K.-J., Chang, J.-M., Wu, R.-Y., Chang, S.-C.: Amortized efficiency of generation, ranking and unranking left-child sequences in lexicographic order. Discrete Appl. Math. 268, 223–236 (2019)

    Article  MathSciNet  Google Scholar 

  13. Pallo, J.: Enumerating, ranking and unranking binary trees. Comput. J. 29(2), 171–175 (1986)

    Article  MathSciNet  Google Scholar 

  14. Pallo, J., Racca, R.: A note on generating binary trees in A-order and B-order. Int. J. Comput. Math. 18(1), 27–39 (1985)

    Article  Google Scholar 

  15. van Roelants Baronaigien, D.: A loopless Gray-code algorithm for listing \(k\)-ary trees. J. Algorithms 35(1), 100–107 (2000)

    Article  MathSciNet  Google Scholar 

  16. Savage, C.D.: A survey of combinatorial Gray codes. SIAM Rev. 39(4), 605–629 (1997)

    Article  MathSciNet  Google Scholar 

  17. Sawada, J.: Generating bracelets in constant amortized time. SIAM J. Comput. 31(1), 259–268 (2001)

    Article  MathSciNet  Google Scholar 

  18. Sawada, J.: A fast algorithm to generate necklaces with fixed content. Theoret. Comput. Sci. 301, 477–489 (2003)

    Article  MathSciNet  Google Scholar 

  19. Sawada, J.: A simple Gray code to list all minimal signed binary representations. SIAM J. Discrete Math. 21(1), 16–25 (2007)

    Article  MathSciNet  Google Scholar 

  20. Sawada, J., Williams, A.: A Gray code for fixed-density necklaces and Lyndon words in constant amortized time. Theoret. Comput. Sci. 502, 46–54 (2013)

    Article  MathSciNet  Google Scholar 

  21. Sawada, J., Williams, A., Wong, D.: Necklaces and Lyndon words in colexicographic and binary reflected Gray code order. J. Discrete Algorithms 46–47, 25–35 (2017)

    Article  MathSciNet  Google Scholar 

  22. Stanley, R.P.: Enumerative Combinatorics, vol. 2. Cambridge University Press, Cambridge (1999)

    Book  Google Scholar 

  23. Vajnovszki, V.: On the loopless generation of binary tree sequences. Inform. Process. Lett. 68(3), 113–117 (1998)

    Article  MathSciNet  Google Scholar 

  24. Vajnovszki, V.: Generating a Gray code for P-sequences. J. Math. Model. Algo. 1, 31–41 (2002)

    Article  MathSciNet  Google Scholar 

  25. Williamson, S.G.: Combinatorics for Computer Science. Computer Science Press, Rockville (1985)

    MATH  Google Scholar 

  26. Wu, R.-Y., Chang, J.-M., Chan, H.-C., Pai, K.-J.: A loopless algorithm for generating multiple binary tree sequences simultaneously. Theor. Comput. Sci. 556, 25–33 (2014)

    Article  MathSciNet  Google Scholar 

  27. Wu, R.-Y., Chang, J.-M., Peng, S.-L., Liu, C.-L.: Gray-code ranking and unranking on left-weight sequences of binary trees. IEICE Trans. Fund. E99–A(6), 1067–1074 (2016)

    Article  Google Scholar 

  28. Wu, R.-Y., Chang, J.-M., Wang, Y.-L.: Ranking and unranking of \(t\)-ary trees using RD-sequences. IEICE Trans. Inform. Syst. E94–D(2), 226–232 (2011)

    Article  Google Scholar 

  29. Wu, R.-Y., Hsu, C.-H., Chang, J.-M.: Loopless algorithms for listing Zaks’ sequences in Gray-code order. J. Internet Tech. 15(4), 679–684 (2014)

    Google Scholar 

  30. Xiang, L., Ushijima, K., Tang, C.: Efficient loopless generation of Gray codes for \(k\)-ary trees. Inform. Process. Lett. 76(4–6), 169–174 (2000)

    Article  MathSciNet  Google Scholar 

  31. Xiang, L., Ushijima, K., Tang, C.: On generating \(k\)-ary trees in computer representation. Inform. Process. Lett. 77(5–6), 231–238 (2001)

    Article  MathSciNet  Google Scholar 

  32. Zaks, S.: Lexicographic generation of ordered trees. Theor. Comput. Sci. 10(1), 63–82 (1980)

    Article  MathSciNet  Google Scholar 

Download references

Acknowledgements

The authors express their sincere thanks to the anonymous associate editor and referees for their valuable suggestions that greatly improved the original manuscript.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Jou-Ming Chang.

Additional information

Publisher's Note

Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

This research was partially supported by MOST grants 108-2221-M-262-001 (R.-Y. Wu) and 107-2221-E-141-001-MY3 (J.-M. Chang) from the Ministry of Science and Technology, Taiwan.

Appendices

Appendix A: The pseudo codes of the loopless algorithm

figure a

The algorithm generates the Z-sequences of all (km)-ary trees in Gray code order. The first sequence is generated in \({\mathcal {O}}(n)\) time (see Lines 2–6 in the main program). And, each subsequent one is generated in constant time by calling the procedure NextZ(), where the complexity does not include the output of a sequence.

Appendix B: The pseudo codes of the ranking function

figure b

Appendix C: The pseudo codes of the unranking function

figure c

Rights and permissions

Reprints and permissions

About this article

Check for updates. Verify currency and authenticity via CrossMark

Cite this article

Chang, YH., Wu, RY., Lin, CK. et al. A loopless algorithm for generating (km)-ary trees in Gray code order. Optim Lett 15, 1133–1154 (2021). https://doi.org/10.1007/s11590-020-01613-z

Download citation

  • Received:

  • Accepted:

  • Published:

  • Issue Date:

  • DOI: https://doi.org/10.1007/s11590-020-01613-z

Navigation