Every time you press Ctrl+F or ask a database to LIKE '%pattern%', somewhere a program is hunting for a short string inside a very long one. The obvious approach — slide the pattern one position at a time and compare — works, but it reads every character of the text. On a 100 MB file that is a lot of reading.
In 1977 Robert S. Boyer and J Strother Moore published an algorithm that does something surprising: it reads the pattern from right to left, and whenever a character mismatches it consults two precomputed tables to jump forward by as many positions as possible — often skipping entire chunks of text without reading them.
The result is a search that can run faster than the length of the text itself — sublinear in the best and average case on natural language. That is not a typo: for long patterns in typical text, Boyer-Moore looks at fewer characters than there are characters in the file.
The algorithm is solved in the sense that its worst-case complexity is proven: comparisons on average (n = text length, m = pattern length), and in the worst case, established by Knuth, Morris and Pratt's complementary analysis and later refined by Cole (1994). No string-search algorithm can asymptotically beat in the worst case on a general alphabet — Boyer-Moore effectively achieves that bound.
Comments
Loading comments...