I am reading about the KMP substring search algorithm and the examples I find online use an one-dimensional table to build the prefix information table.
I also read the Sedgewick explanation and he used a 2-D array to build the table and explicitly states that the space complexity of KMP is O(RM)
where R
is the alphabet size and M
the pattern size while everywhere else it is stated that the space complexity is just O(M + N)
i.e. the text to process and the pattern size itself.
So I am confused on the difference. Are there multiple KMP algorithmic approaches? And do they have different scope? Or what am I missing?
Why is the 2D needed if 1D can solve the substring problem too?
Asked
Active
Viewed 241 times
2

Jim
- 3,845
- 3
- 22
- 47
1 Answers
1
I guess Sedgewick wanted to demonstrate a variant of KMP that constructs a deterministic finite automaton in the standard sense of that term. It's a weird choice that (as you observe) bloats the running time, but maybe there was a compelling pedagogical reason that I don't appreciate (then again my PhD was on algorithms, so...). I'd find another description that follows the original more closely.

David Eisenstat
- 64,237
- 7
- 60
- 120
-
Yes he does use the simulation of a DFA and the description is quite complicated. Some times I lose his point and wasn't sure if this inclusion of all the characters in the alphabet were some kind of general solution to the problem (because he actually says that there is a more space efficient version of KMP using NFA!). So in non-academic setting the "simple" approach of an 1-D array just on the pattern works fine right? – Jim Aug 12 '20 at 20:16
-
The NFA approach he mentions is not the 1-D I am seeing in other examples right? – Jim Aug 12 '20 at 20:31
-
@Jim The canonical version as I remember it is you build a 1D table by searching for the pattern in the pattern, so that given any prefix of the pattern, you can find the maximal suffix that is a prefix of the pattern. Then at each step of the search, you either successfully extend the match or slide the pattern over. I'm sure you can squint and view it as an NFA but I just don't agree that that's a productive view of it except to observe how KMP were able to derive their algorithm from existing results in the pattern matching literature. – David Eisenstat Aug 12 '20 at 20:57
-
Would you be kind enough if that would be possible to have a look here too? https://stackoverflow.com/questions/63448730/random-primes-and-rabin-karp-substring-search – Jim Aug 17 '20 at 13:09