Efficient Counting of Square Substrings in a Tree

  • Tomasz Kociumaka
  • Jakub Pachocki
  • Jakub Radoszewski
  • Wojciech Rytter
  • Tomasz Waleń
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 7676)


We give an algorithm which in O(nlog2 n) time counts all distinct squares in labeled trees. There are two main obstacles to overcome. Crochemore et al. showed in 2012 that the number of such squares is bounded by Θ(n 4/3). This is substantialy different from the case of classical strings, which admit only a linear number of distinct squares. We deal with this difficulty by introducing a compact representation of all squares (based on maximal cyclic shifts) that requires only O(n logn) space. The second obstacle is lack of adequate algorithmic tools for labeled trees. Consequently we develop several novel techniques, which form the most complex part of the paper. In particular we extend Imre Simon’s implementation of the failure function in pattern matching machines.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Bender, M.A., Farach-Colton, M.: The level ancestor problem simplified. Theor. Comput. Sci. 321(1), 5–12 (2004)MathSciNetzbMATHCrossRefGoogle Scholar
  2. 2.
    Bresar, B., Grytczuk, J., Klavzar, S., Niwczyk, S., Peterin, I.: Nonrepetitive colorings of trees. Discrete Mathematics 307(2), 163–172 (2007)MathSciNetzbMATHCrossRefGoogle Scholar
  3. 3.
    Crochemore, M., Ilie, L., Rytter, W.: Repetitions in strings: Algorithms and combinatorics. Theor. Comput. Sci. 410(50), 5227–5235 (2009)MathSciNetzbMATHCrossRefGoogle Scholar
  4. 4.
    Crochemore, M., Iliopoulos, C.S., Kociumaka, T., Kubica, M., Radoszewski, J., Rytter, W., Tyczyński, W., Waleń, T.: The Maximum Number of Squares in a Tree. In: Kärkkäinen, J., Stoye, J. (eds.) CPM 2012. LNCS, vol. 7354, pp. 27–40. Springer, Heidelberg (2012)CrossRefGoogle Scholar
  5. 5.
    Crochemore, M., Iliopoulos, C.S., Kubica, M., Radoszewski, J., Rytter, W., Waleń, T.: Extracting Powers and Periods in a String from Its Runs Structure. In: Chavez, E., Lonardi, S. (eds.) SPIRE 2010. LNCS, vol. 6393, pp. 258–269. Springer, Heidelberg (2010)CrossRefGoogle Scholar
  6. 6.
    Crochemore, M., Rytter, W.: Jewels of Stringology. World Scientific (2003)Google Scholar
  7. 7.
    Fraenkel, A.S., Simpson, J.: How many squares can a string contain? J. of Combinatorial Theory Series A 82, 112–120 (1998)MathSciNetzbMATHCrossRefGoogle Scholar
  8. 8.
    Gusfield, D., Stoye, J.: Linear time algorithms for finding and representing all the tandem repeats in a string. J. Comput. Syst. Sci. 69(4), 525–546 (2004)MathSciNetzbMATHCrossRefGoogle Scholar
  9. 9.
    Harel, D., Tarjan, R.E.: Fast algorithms for finding nearest common ancestors. SIAM J. Comput. 13(2), 338–355 (1984)MathSciNetzbMATHCrossRefGoogle Scholar
  10. 10.
    Kosaraju, S.R.: Efficient tree pattern matching (preliminary version). In: FOCS, pp. 178–183. IEEE Computer Society (1989)Google Scholar
  11. 11.
    Main, M.G., Lorentz, R.J.: An O(n log n) algorithm for finding all repetitions in a string. J. Algorithms 5(3), 422–432 (1984)MathSciNetzbMATHCrossRefGoogle Scholar
  12. 12.
    Shibuya, T.: Constructing the Suffix Tree of a Tree with a Large Alphabet. In: Aggarwal, A., Pandu Rangan, C. (eds.) ISAAC 1999. LNCS, vol. 1741, pp. 225–236. Springer, Heidelberg (1999)CrossRefGoogle Scholar
  13. 13.
    Simon, I.: String Matching Algorithms and Automata. In: Karhumäki, J., Rozenberg, G., Maurer, H.A. (eds.) Results and Trends in Theoretical Computer Science. LNCS, vol. 812, pp. 386–395. Springer, Heidelberg (1994)CrossRefGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2012

Authors and Affiliations

  • Tomasz Kociumaka
    • 1
  • Jakub Pachocki
    • 1
  • Jakub Radoszewski
    • 1
  • Wojciech Rytter
    • 1
    • 2
  • Tomasz Waleń
    • 3
    • 1
  1. 1.Faculty of Mathematics, Informatics and MechanicsUniversity of WarsawWarsawPoland
  2. 2.Faculty of Mathematics and Computer ScienceNicolaus Copernicus UniversityToruńPoland
  3. 3.Laboratory of Bioinformatics and Protein EngineeringInternational Institute of Molecular and Cell Biology in WarsawPoland

Personalised recommendations