Abstract
We show how to speed up two string-matching algorithms: the Boyer-Moore algorithm (BM algorithm), and its version called here the reverse factor algorithm (RF algorithm). The RF algorithm is based on factor graphs for the reverse of the pattern. The main feature of both algorithms is that they scan the text right-to-left from the supposed right position of the pattern. The BM algorithm goes as far as the scanned segment (factor) is a suffix of the pattern. The RF algorithm scans while the segment is a factor of the pattern. Both algorithms make a shift of the pattern, forget the history, and start again. The RF algorithm usually makes bigger shifts than BM, but is quadratic in the worst case. We show that it is enough to remember the last matched segment (represented by two pointers to the text) to speed up the RF algorithm considerably (to make a linear number of inspections of text symbols, with small coefficient), and to speed up the BM algorithm (to make at most 2 ·n comparisons). Only a constant additional memory is needed for the search phase. We give alternative versions of an accelerated RF algorithm: the first one is based on combinatorial properties of primitive words, and the other two use the power of suffix trees extensively. The paper demonstrates the techniques to transform algorithms, and also shows interesting new applications of data structures representing all subwords of the pattern in compact form.
Similar content being viewed by others
References
A. V. Aho, Algorithms for finding patterns in strings, inHandbook of Theoretical Computer Science, vol. A (J. van Leeuwen, ed.), Elsevier, Amsterdam, 1990, pp. 255–300.
A. Apostolico, The myriad virtues of suffix trees, inCombinatorial Algorithms on Words (A. Apostolico and Z. Galil, eds.), NATO Advanced Science Institutes, Series F, vol. 12, Springer-Verlag, Berlin, 1985, pp. 85–96.
A. Apostolico and R. Giancarlo, The Boyer-Moore-Galil string searching strategies revisited,SIAM J. Comput. 15 (1986), 98–105.
R. A. Baeza-Yates and M. Régnier, Average running time of the Boyer-Moore-Horspool algorithm,Theoret. Comput. Sci. 92(1) (1992), 19–31.
L. Banachowski, A. Kreczmar, and W. Rytter,Analysis of Algorithms and Data Structures, Addison-Wesley, Reading, MA, 1991.
A. Blumer, J. Blumer, A. Ehrenfeucht, D. Haussler, M. T. Chen, and J. Seiferas, The smallest automaton recognizing the subwords of a text,Theoret. Comput. Sci. 40 (1985), 31–55.
R. S. Boyer and J. S. Moore, A fast string searching algorithm,Comm. ACM 20 (1977), 762–772.
R. Cole, Tight bounds on the complexity of the Boyer-Moore pattern matching algorithm,Proceedings of the 2nd Annual ACM Symposium on Discrete Algorithms, 1990, pp. 224–233.
M. Crochemore, Transducers and repetitions,Theoret. Comput. Sci. 45 (1986), 63–86.
Z. Galil, On improving the worst case running time of the Boyer-Moore string searching algorithm,Comm. ACM 22 (1979), 505–508.
L. J. Guibas and A. M. Odlyzko, A new proof of the linearity of the Boyer-Moore string searching algorithm,SIAM J. Comput. 9 (1980), 672–682.
R. N. Horspool, Practical fast searching in strings,Software—Practice and Experience,10 (1980), 501–506.
A. Hume and D. M. Sunday, Fast string searching,Software—Practice and Experience 21(11) (1991), 1221–1248.
D. E. Knuth, J. H. Morris Jr and V. R. Pratt, Fast pattern matching in strings,SIAM J. Comput. 6 (1977), 323–350.
T. Lecroq, A variation on Boyer-Moore algorithm,Theoret. Comput. Sci. 92 (1992), 119–144.
W. Rytter, A correct preprocessing algorithm for Boyer-Moore string searching,SIAM J. Comput. 9 (1980), 509–512.
A. C. Yao, The complexity of pattern matching for a random string,SIAM J. Comput. 8 (1979), 368–387.
Author information
Authors and Affiliations
Additional information
Communicated by Alberto Apostolico.
The work by M. Crochemore and T. Lecroq was partially supported by PRC “Mathématiques-Informatique,” M. Crochemore was also partially supported by NATO Grant CRG 900293, and the work by A. Czumaj, L. Gasieniec, S. Jarominek, W. Plandowski, and W. Rytter was supported by KBN of the Polish Ministry of Education.
Rights and permissions
About this article
Cite this article
Crochemore, M., Czumaj, A., Gasieniec, L. et al. Speeding up two string-matching algorithms. Algorithmica 12, 247–267 (1994). https://doi.org/10.1007/BF01185427
Received:
Revised:
Issue Date:
DOI: https://doi.org/10.1007/BF01185427