Questions tagged [knuth-morris-pratt]

The Knuth–Morris–Pratt algorithm is an efficient string matching algorithm.

The Knuth–Morris–Pratt algorithm is an efficient string matching algorithm.

110 questions
40
votes
1 answer

When to use Rabin-Karp or KMP algorithms?

I have generated an string using the following alphabet. {A,C,G,T}. And my string contains more than 10000 characters. I'm searching the following patterns in it. ATGGA TGGAC CCGT I have asked to use a string matching algorithm which has O(m+n)…
Sukeshini
  • 1,241
  • 2
  • 23
  • 48
34
votes
2 answers

Why does String.indexOf() not use KMP?

I read the source code of java.lang.String and I was surprised to find that String.indexof() does not use the Knuth–Morris–Pratt algorithm? As we know, KMP is more effective. So why isn't it used in String.indexOf()? Someone around me told me that…
D0n9X1n
  • 2,114
  • 1
  • 14
  • 17
27
votes
2 answers

When would you use KMP over BOYER-MOORE

I am currently learning about pattern matching algorithms and have come across these two algorithms. I have the following general ideas: KMP Compares text left-to-right Uses a failure array to shift intelligently takes O(m), where m is the length…
Eric
  • 1,356
  • 2
  • 14
  • 24
19
votes
2 answers

What is the theory behind KMP pattern matching algorithm?

What is the theoretical basis of KMP pattern-matching algorithm? I understand the algorithm itself, but, don't understand how did Knuth, Morris and Pratt invent this algorithm. Was there any mathematical proof? Can you give a link please?
user366312
  • 16,949
  • 65
  • 235
  • 452
14
votes
3 answers

When is Rabin Karp more effective than KMP or Boyer-Moore?

I'm learning about string searching algorithms and understand how they work but haven't found a good enough answer about in which cases Rabin-Karp algorithm would be more effective than KMP or Boyer-Moore. I see that it is easier to implement and…
E.K
  • 315
  • 1
  • 3
  • 6
13
votes
4 answers

What's the worst case complexity for KMP when the goal is to find all occurrences of a certain string?

I would also like to know which algorithm has the worst case complexity of all for finding all occurrences of a string in another. Seems like Boyer–Moore's algorithm has a linear time complexity.
Ouais Alsharif
  • 317
  • 1
  • 4
  • 16
13
votes
1 answer

DFA construction in Knuth-Morris-Pratt algorithm

I am referring to the outline of the Knuth-Morris-Pratt (KMP) algorithm for substring search in Sedgewick's book "Algorithms" (4th ed.). The KMP algorithm uses a backup in substring search based on a deterministic finite automaton (DFA). I…
madison54
  • 743
  • 2
  • 8
  • 19
11
votes
1 answer

Knuth-Morris-Pratt algorithm in Haskell

I have a trouble with understanding this implementation of the Knuth-Morris-Pratt algorithm in Haskell. http://twanvl.nl/blog/haskell/Knuth-Morris-Pratt-in-Haskell In particular I don't understand the construction of the automaton. I know that it…
pizet
  • 419
  • 1
  • 4
  • 12
10
votes
3 answers

Why can the KMP failure function be computed in O(n) time?

Wikipedia claims that the failure function table can be computed in O(n) time. Let's look at its `canonical' implementation (in C++): vector prefix_function (string s) { int n = (int) s.length(); vector pi (n); for (int i=1;…
vortexxx192
  • 929
  • 1
  • 9
  • 24
8
votes
4 answers

String pattern matching with one or zero mismatch

Given a string and a pattern to be matched, how efficiently can the matches be found having zero or one mismatch. e.g) S = abbbaaabbbabab P = abab Matches are abbb(index 0),aaab(index 4),abbb(index 6),abab(index 10) I tried to modify KMP…
6
votes
2 answers

Why/how does the Longest Proper Prefix/Suffix algorithm work?

The LPS (Longest Proper Prefix which is also a Suffix) algorithm goes as follows: public static int[] constructLPSArray(String s) { int n = s.length(); int[] arr = new int[n]; int j = 0; for (int i = 1; i < n; ) { …
User_Targaryen
  • 4,125
  • 4
  • 30
  • 51
6
votes
5 answers

Find the smallest period of input string in O(n)?

Given the following problem : Definition : Let S be a string over alphabet Σ .S' is the smallest period of S if S' is the smallest string such that : S = (S')^k (S'') , where S'' is a prefix of S. If no such S' exists , then S is not…
JAN
  • 21,236
  • 66
  • 181
  • 318
5
votes
1 answer

is there any paper or an explanation on how to implement a two dimensional KMP?

I tried to solve the problem of two dimensional search using a combination of Aho-Corasick and a single dimensional KMP, however, I still need something faster. To elaborate, I have a matrix A of characters of size n1*n2 and I wish to find all…
5
votes
4 answers

Implementing Knuth-Morris-Pratt (KMP) algorithm for string matching with Python

I am following Cormen Leiserson Rivest Stein (clrs) book and came across "kmp algorithm" for string matching. I implemented it using Python (as-is). However, it doesn't seem to work for some reason. where is my fault? The code is given below: def…
serious_luffy
  • 419
  • 1
  • 6
  • 17
5
votes
2 answers

KMP failure function calculation

My professor solved the kmp failure function as follows: index 1 2 3 4 5 6 7 8 9 string a a b a a b a b b ff 0 1 2 1 2 3 4 5 1 From other texts I checked online, I found out it might be wrong, I went back to confirm from him again and he told…
Dennis
  • 51
  • 1
  • 1
  • 4
1
2 3 4 5 6 7 8