Abstract
A family of (k, m)-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 (k, m)-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.
Similar content being viewed by others
References
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)
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
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)
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)
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)
Ehrlich, G.: Loopless algorithms for generating permutations, combinations, and other combinatorial objects. J. ACM 20(3), 500–513 (1973)
Hartman, P., Sawada, J.: Ranking and unranking fixed-density necklaces and Lyndon words. Theoret. Comput. Sci. 791, 36–47 (2019)
Johnson, S.M.: Generation of permutations by adjacent transposition. Math. Comput. 17(83), 282–285 (1963)
Karim, S., Sawada, J., Alamgir, Z., Husnine, S.: Generating bracelets with fixed content. Theoret. Comput. Sci. 475, 103–112 (2013)
Korsh, J.F., LaFollette, P.: Loopless generation of Gray code for \(k\)-ary trees. Inform. Process. Lett. 70(1), 7–11 (1999)
Kreher, D.L., Stinson, D.R.: Combinatorial Algorithms: Generation, Enumeration, and Search. CRC Press, Rockville (1999)
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)
Pallo, J.: Enumerating, ranking and unranking binary trees. Comput. J. 29(2), 171–175 (1986)
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)
van Roelants Baronaigien, D.: A loopless Gray-code algorithm for listing \(k\)-ary trees. J. Algorithms 35(1), 100–107 (2000)
Savage, C.D.: A survey of combinatorial Gray codes. SIAM Rev. 39(4), 605–629 (1997)
Sawada, J.: Generating bracelets in constant amortized time. SIAM J. Comput. 31(1), 259–268 (2001)
Sawada, J.: A fast algorithm to generate necklaces with fixed content. Theoret. Comput. Sci. 301, 477–489 (2003)
Sawada, J.: A simple Gray code to list all minimal signed binary representations. SIAM J. Discrete Math. 21(1), 16–25 (2007)
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)
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)
Stanley, R.P.: Enumerative Combinatorics, vol. 2. Cambridge University Press, Cambridge (1999)
Vajnovszki, V.: On the loopless generation of binary tree sequences. Inform. Process. Lett. 68(3), 113–117 (1998)
Vajnovszki, V.: Generating a Gray code for P-sequences. J. Math. Model. Algo. 1, 31–41 (2002)
Williamson, S.G.: Combinatorics for Computer Science. Computer Science Press, Rockville (1985)
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)
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)
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)
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)
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)
Xiang, L., Ushijima, K., Tang, C.: On generating \(k\)-ary trees in computer representation. Inform. Process. Lett. 77(5–6), 231–238 (2001)
Zaks, S.: Lexicographic generation of ordered trees. Theor. Comput. Sci. 10(1), 63–82 (1980)
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
Corresponding author
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
The algorithm generates the Z-sequences of all (k, m)-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
Appendix C: The pseudo codes of the unranking function
Rights and permissions
About this article
Cite this article
Chang, YH., Wu, RY., Lin, CK. et al. A loopless algorithm for generating (k, m)-ary trees in Gray code order. Optim Lett 15, 1133–1154 (2021). https://doi.org/10.1007/s11590-020-01613-z
Received:
Accepted:
Published:
Issue Date:
DOI: https://doi.org/10.1007/s11590-020-01613-z