skip to main content
article
Free Access

A fast string searching algorithm

Published:01 October 1977Publication History
Skip Abstract Section

Abstract

An algorithm is presented that searches for the location, “il” of the first occurrence of a character string, “pat,” in another string, “string.” During the search operation, the characters of pat are matched starting with the last character of pat. The information gained by starting the match at the end of the pattern often allows the algorithm to proceed in large jumps through the text being searched. Thus the algorithm has the unusual property that, in most cases, not all of the first i characters of string are inspected. The number of characters actually inspected (on the average) decreases as a function of the length of pat. For a random English pattern of length 5, the algorithm will typically inspect i/4 characters of string before finding a match at i. Furthermore, the algorithm has been implemented so that (on the average) fewer than i + patlen machine instructions are executed. These conclusions are supported with empirical evidence and a theoretical analysis of the average behavior of the algorithm. The worst case behavior of the algorithm is linear in i + patlen, assuming the availability of array space for tables linear in patlen plus the size of the alphabet.

References

  1. 1 Aho, A.V., and Corasick, M.J. Fast pattern matching: An aid to bibliographic search. Comm. ACM 18, 6 (June, 1975), 333-340. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. 2 Beeler, M., Gosper, R.W., and Schroeppel, R. Hakmem. Memo No. 239, M.I.T. Artificial Intelligence Lab., M.I.T., Cambridge, Mass., Feb. 29, 1972. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. 3 Dewey, G. Relativ Frequency o f English Speech Sounds. Harvard U. Press, Cambridge, Mass., 1923, p. 185.Google ScholarGoogle Scholar
  4. 4 Knuth, D.E., Morris, J.H., and Pratt, V.R. Fast pattern matching in strings. TR CS-74-440, Stanford U., Stanford, Calif., 1974.Google ScholarGoogle Scholar
  5. 5 Knuth, D.E., Morris, J.H., and Pratt, V.R. Fast pattern matching in strings. (to appear in SIAM J. Comput.).Google ScholarGoogle Scholar

Recommendations

Comments

Login options

Check if you have access through your login credentials or your institution to get full access on this article.

Sign in

Full Access

  • Published in

    cover image Communications of the ACM
    Communications of the ACM  Volume 20, Issue 10
    Oct. 1977
    93 pages
    ISSN:0001-0782
    EISSN:1557-7317
    DOI:10.1145/359842
    Issue’s Table of Contents

    Copyright © 1977 ACM

    Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

    Publisher

    Association for Computing Machinery

    New York, NY, United States

    Publication History

    • Published: 1 October 1977

    Permissions

    Request permissions about this article.

    Request Permissions

    Check for updates

    Qualifiers

    • article

PDF Format

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader