Earlier quoted context omitted.
IMO the Boyer-Moore string searching algorithm is way more elegant. Average case performance of O(n/m)?!? That means that the longer the string you're searching for the faster you can find it! It obviously makes perfect logical sense once you think about it, but when I first heard about the algorithm it seemed magical.
Wikipedia says Boyer Moore is O(n+m) which is the same as this algorithm. https://en.wikipedia.org/wiki/Boyer-Moore_string_search_algo...
Knuth–Morris–Pratt algorithm
41–50 of 50 posts
Re: Knuth–Morris–Pratt algorithm
#42Earlier quoted context omitted.
O(m*n) is not the same as O(n^2).
Yes, but if something is O(m*n) and m <= n (which is the case in string search), then it's also O(n^2). On the other hand, since m can be of the order of, say, n/2, it's not too confusing to say that naive string search is O(n^2), since it's actually Theta(n^2).
I really just meant that "in practice" the growth of the string length (m) varies independently and common cases behave more like O(n) than O(n^2). Also that naive search is probably heavily optimized with SSE assembly instructions and cache tuned. I have implemented several of these algorithms in C and it is difficult to impossible to beat C's strstr consistently with a simple implementation, even on cases that start to make the naive algorithm really inefficient.
You'd most likely need a hybrid approach to make a good general purpose string search function that is effective across many domains.
Re: Knuth–Morris–Pratt algorithm
#43I have implemented KMP in Haskell. This version doesn't use any index! It is built purely functionally by realizing KMP's failure table is just a finite state automaton(well, almost...) However it is much longer than the C++ version... Code: https://github.com/Mgccl/haskell-algorithm/blob/master/KMP.h... Description: http://www.chaoxuprime.com/posts/2014-04-11-the-kmp-algorith... Actually, KMP is a little harder to p…
Re: Knuth–Morris–Pratt algorithm
#44KMP is conceptually very cool, as well as other clever string searching algorithms (BM, RK, AC), though one of the questions for me always was if even its most efficient implementation wouldn't be always slower than executing a brute-force combination of REP CMPSL/CMPSB instructions (x86) for vast majority of searched strings?
If you don't have the entire string in memory then Boyer-Moore is usually fastest because you can avoid doing a lot of I/O (since you skip comparing many subsequences, you don't have to read those subsequences in the first place). This is why grep is very fast given a static string pattern to search for, even on huge files, but grepping for a regex is dog slow.
Re: Knuth–Morris–Pratt algorithm
#45Earlier quoted context omitted.
O(m*n) is not the same as O(n^2).
It's the same when m=n.
Yes there'd be much more different instructions involved but I think KMP would start beating naive pretty quickly in the m=n case.
Re: Knuth–Morris–Pratt algorithm
#46Why is KMP on the front page of HN? Not complaining, just confused. Is this related to some other news?
Re: Knuth–Morris–Pratt algorithm
#47Why is KMP on the front page of HN? Not complaining, just confused. Is this related to some other news?
Re: Knuth–Morris–Pratt algorithm
#48Here is the boost implementation: http://www.boost.org/doc/libs/1_55_0/libs/algorithm/doc/html...
Re: Knuth–Morris–Pratt algorithm
#49Earlier quoted context omitted.
If you don't have the entire string in memory then Boyer-Moore is usually fastest because you can avoid doing a lot of I/O (since you skip comparing many subsequences, you don't have to read those subsequences in the first place). This is why grep is very fast given a static string pattern to search for, even on huge files, but grepping for a regex is dog slow.
Until they implement a JIT and compile the regex.
Re: Knuth–Morris–Pratt algorithm
#50Earlier quoted context omitted.
Until they implement a JIT and compile the regex.
No, you're missing the point -- using a regex does not allow you to read in less than all of the input (in the best case, even). If you grep a 1GB file with a regex you must read 1GB from disk, no exceptions, and that's many orders of magnitude slower than anything on the CPU (or in memory). With Boyer-Moore you can do less than 100% of the file size in I/O, especially if the given pattern is long relative to the ful…
No point was missed.