views:

431

answers:

6

OK, so I don't sound like an idiot I'm going to state the problem/requirements more explicitly:

  • Needle (pattern) and haystack (text to search) are both C-style null-terminated strings. No length information is provided; if needed, it must be computed.
  • Function should return a pointer to the first match, or NULL if no match is found.
  • Failure cases are not allowed. This means any algorithm with non-constant (or large constant) storage requirements will need to have a fallback case for allocation failure (and performance in the fallback care thereby contributes to worst-case performance).
  • Implementation is to be in C, although a good description of the algorithm (or link to such) without code is fine too.

...as well as what I mean by "fastest":

  • Deterministic O(n) where n = haystack length. (But it may be possible to use ideas from algorithms which are normally O(nm) (for example rolling hash) if they're combined with a more robust algorithm to give deterministic O(n) results).
  • Never performs (measurably; a couple clocks for if (!needle[1]) etc. are okay) worse than the naive brute force algorithm, especially on very short needles which are likely the most common case. (Unconditional heavy preprocessing overhead is bad, as is trying to improve the linear coefficient for pathological needles at the expense of likely needles.)
  • Given an arbitrary needle and haystack, comparable or better performance (no worse than 50% longer search time) versus any other widely-implemented algorithm.
  • Aside from these conditions, I'm leaving the definition of "fastest" open-ended. A good answer should explain why you consider the approach you're suggesting "fastest".

My current implementation runs in roughly between 10% slower and 8 times faster (depending on the input) than glibc's implementation of Two-Way.

Update: My current optimal algorithm is as follows:

  • For needles of length 1, use strchr.
  • For needles of length 2-4, use machine words to compare 2-4 bytes at once as follows: Preload needle in a 16- or 32-bit integer with bitshifts and cycle old byte out/new bytes in from the haystack at each iteration. Every byte of the haystack is read exactly once and incurs a check against 0 (end of string) and one 16- or 32-bit comparison.
  • For needles of length >4, use Two-Way algorithm with a bad shift table (like Boyer-Moore) which is applied only to the last byte of the window. To avoid the overhead of initializing a 1kb table, which would be a net loss for many moderate-length needles, I keep a bit array (32 bytes) marking which entries in the shift table are initialized. Bits that are unset correspond to byte values which never appear in the needle, for which a full-needle-length shift is possible.

The big questions left in my mind are:

  • Is there a way to make better use of the bad shift table? Boyer-Moore makes best use of it by scanning backwards (right-to-left) but Two-Way requires a left-to-right scan.
  • The only two viable candidate algorithms I've found for the general case (no out-of-memory or quadratic performance conditions) are Two-Way and String Matching on Ordered Alphabets. But are there easily-detectable cases where different algorithms would be optimal? Certainly many of the O(m) (where m is needle length) in space algorithms could be used for m<100 or so. It would also be possible to use algorithms which are worst-case quadratic if there's an easy test for needles which provably require only linear time.

Bonus points for:

  • Can you improve performance by assuming the needle and haystack are both well-formed UTF-8? (With characters of varying byte lengths, well-formed-ness imposes some string alignment requirements between the needle and haystack and allows automatic 2-4 byte shifts when a mismatching head byte is encountered. But do these constraints buy you much/anything beyond what maximal suffix computations, good suffix shifts, etc. already give you with various algorithms?)

Note: I'm well aware of most of the algorithms out there, just not how well they perform in practice. Here's a good reference so people don't keep giving me references on algorithms as comments/answers: http://www-igm.univ-mlv.fr/~lecroq/string/index.html

A: 

KMP may solve your problem.

taskinoor
KMP can't account for giant inputs - it needs `m*sizeof(size_t)` bytes of working space where `m` is the length of the needle. If it were good enough, KMP could be a special-case for moderate length needles, but I don't think it's that fast anyway.. Please correct me if I'm mistaken on that.
R..
A: 

I don't know if it's the absolute best, but I've had good experience with Boyer-Moore.

R Samuel Klatchko
Do you know a way to combine Boyer-Moore's bad shift table with Two-Way? Glibc does a variant of this for long needles (>32 byte) but only checks the last byte. The problem is that Two-Way needs to search the right portion of the needle left-to-right, whereas Boyer-Moore's bad shift is most efficient when searching from right-to-left. I tried using it with left-to-right in Two-Way (advance by shift table or normal Two-Way right half mismatch, whichever is longer) but I got a 5-10% slowdown versus normal Two-Way in most cases and couldn't find any cases where it improved performance.
R..
+5  A: 

Build up a test library of likely needles and haystacks. Profile the tests on several search algorithms, including brute force. Pick the one that performs best with your data.

Boyer-Moore uses a bad character table with a good suffix table.

Boyer-Moore-Horspool uses a bad character table.

Knuth-Morris-Pratt uses a partial match table.

Rabin-Karp uses running hashes.

They all trade overhead for reduced comparisons to a different degree, so the real world performance will depend on the average lengths of both the needle and haystack. The more initial overhead, the better with longer inputs. With very short needles, brute force may win.

Edit:

A different algorithm might be best for finding base pairs, english phrases, or single words. If there were one best algorithm for all inputs, it would have been publicized.

Think about the following little table. Each question mark might have a different best search algorithm.

                 short needle     long needle
short haystack         ?               ?
long haystack          ?               ?

This should really be a graph, with a range of shorter to longer inputs on each axis. If you plotted each algorithm on such a graph, each would have a different signature. Some algorithms suffer with a lot of repetition in the pattern, which might affect uses like searching for genes. Some other factors that affect overall performance are searching for the same pattern more than once and searching for different patterns at the same time.

If I needed a sample set, I think I would scrape a site like google or wikipedia, then strip the html from all the result pages. For a search site, type in a word then use one of the suggested search phrases. Choose a few different languages, if applicable. Using web pages, all the texts would be short to medium, so merge enough pages to get longer texts. You can also find public domain books, legal records, and other large bodies of text. Or just generate random content by picking words from a dictionary. But the point of profiling is to test against the type of content you will be searching, so use real world samples if possible.

I left short and long vague. For the needle, I think of short as under 8 characters, medium as under 64 characters, and long as under 1k. For the haystack, I think of short as under 2^10, medium as under a 2^20, and long as up to a 2^30 characters.

drawnonward
Do you have good suggestions for a test library? The previous question I asked on SO was related to that and I never got any real answers. (except my own...) It should be extensive. Even if my idea of an application for strstr is searching English text, somebody else's might be searching for genes in base pair sequences...
R..
It's a bit more complicated than short/long. For the needle, the big questions relevant to the performance of most algorithms are: Length? Is there any periodicity? Does the needle contain all unique characters (no repeats)? Or all the same character? Are there a large number of characters in the haystack that never appear in the needle? Is there a chance of having to deal with needles provided by an attacker who wants to exploit worst-case performance to cripple your system? Etc..
R..
+3  A: 

The http://www-igm.univ-mlv.fr/~lecroq/string/index.html link you point to is an excellent source and summary of some of the best known and researched string matching algorithms.

Solutions to most search problems involve trade offs with respect to pre-processing overhead, time and space requirements. No single algorithm will be optimal or practical in all cases.

If you objective is to design a specific algorithm for string searching, then ignore the rest of what I have to say, If you want to develop a generalized string searching service routine then try the following:

Spend some time reviewing the specific strengths and weaknesses of the algorithms you have already referenced. Conduct the review with the objective of finding a set of algorithms that cover the range and scope of string searches you are interested in. Then, build a front end search selector based on a classifier function to target the best algorithm for the given inputs. This way you may employ the most efficient algorithm to do the job. This is particularly effective when an algorithm is very good for certain searches but degrades poorly. For example, brute force is probably the best for needles of length 1 but quickly degrades as needle length increases, whereupon the sustik-moore algoritim may become more efficient (over small alphabets), then for longer needles and larger alphabets, the KMP or Boyer-Moore algorithms may be better. These are just examples to illustrate a possible strategy.

The multiple algorithm approach not a new idea. I believe it has been employed by a few commercial Sort/Search packages (e.g. SYNCSORT commonly used on mainframes implements several sort algorithms and uses heuristics to choose the "best" one for the given inputs)

Each search algorithm comes in several variations that can make significant differences to its performance, as, for example, this paper illustrates.

Benchmark your service to categorize the areas where additional search strategies are needed or to more effectively tune your selector function. This approach is not quick or easy but if done well can produce very good results.

NealB
Thanks for the response, especially the link to Sustik-Moore which I hadn't seen before. The multiple algorithms approach is surely in widespread use. Glibc basically does strchr, Two-Way without bad character shift table, or Two-Way with bad character shift table, depending on whether needle_len is 1, <32, or >32. My current approach is the same except that I always use the shift table; I replaced the 1kb memset necessary to do so with a 32 byte memset on a bitset used to mark which elements of the table have been initialized, and I get the benefit (but not overhead) even for tiny needles.
R..
After thinking about it, I'm really curious what the intended application for Sustik-Moore is. With small alphabets, you'll never get to make any significant shifts (all characters of the alphabet almost surely appear near the end of the needle) and finite automata approaches are very efficient (small state transition table). So I can't envision any scenario where Sustik-Moore could be optimal...
R..
great response -- if I could star this particular answer I would.
Jason S
@R.. The theory behind the sustik-moore algorithm is that it should give you largeraverage shift amounts when the needle is relatively large and the alphabet isrelatively small (eg. searching for DNA sequences). Larger in this case just means larger than the basic Boyer-Moore algorithm would produce given the same inputs. How much more efficient this is relative to a finite automata approach or to some other Boyer-Moore variation (of which there are many) is hard to say. That is why I emphasised spending some time to research the specific strengths/weaknesses of your candidate algorithms.
NealB
Hm, I guess I was stuck thinking of shifts just in the sense of bad character shifts from Boyer-Moore. With an improvement on BM good suffix shifts though, Sustik-Moore could possibly outperform DFA approaches to DNA searching. Neat stuff.
R..
A: 

You might also want to have diverse benchmarks with several types of strings, as this may have a great impact on performance. The algos will perform differenlty based on searching natural language (and even here there still might be fine grained distinctions because of the different morphologoies), DNA strings or random strings etc.

Alphabet size will play a role in many algos, as will needle size. For instance Horspool does good on English text but bad on DNA because of the different alphabet size, making life hard for the bad-character rule. Introducing the good-suffix allieviates this greatly.

johanbev
+1  A: 

A really good question. Just add some tiny bits...

  1. Someone were talking about DNA sequence matching. But for DNA sequence, what we usually do is to build a data structure (e.g. suffix array, suffix tree or FM-index) for the haystack and match many needles against it. This is a different question.

  2. It would be really great if someone would like to benchmark various algorithms. There are very good benchmarks on compression and the construction of suffix arrays, but I have not seen a benchmark on string matching. Potential haystack candidates could be from the SACA benchmark.

  3. A few days ago I was testing the Boyer-Moore implementation from the page you recommended (EDIT: I need a function call like memmem(), but it is not a standard function, so I decided to implement it). My benchmarking program uses random haystack. It seems that the Boyer-Moore implementation in that page is times faster than glibc's memmem() and Mac's strnstr(). In case you are interested, the implementation is here and the benchmarking code is here. This is definitely not a realistic benchmark, but it is a start.

If you have some good needles to test along with the haystack candidates from the SACA benchmark, post them as an answer to my [other question](http://stackoverflow.com/questions/3134602/what-are-good-test-cases-for-benchmarking-stress-testing-substring-search-algor) and, short of getting a better answer, I'll mark it accepted.
R..
About your memmem and Boyer-Moore, it's very likely that Boyer-Moore (or rather one of the enhancements to Boyer-Moore) will perform best on random data. Random data has an extremely low probability of periodicity and long partial matches which lead to quadratic worst-case. I'm looking for a way to combine Boyer-Moore and Two-Way or to efficiently detect when Boyer-Moore is "safe to use" but so far I haven't had any success. BTW I wouldn't use glibc's memmem as a comparison. My implementation of what's basically the same algorithm as glibc's is several times faster.
R..
As I said, it is not my implementation. Credit to Christian Charras and Thierry Lecroq. I can imagine why random input is bad for benchmarking and I am sure glibc chooses algorithms for reasons. I also guess memmem() is not implemented efficiently. I will try. Thanks.