Knuth-Morris-Pratt | Boyer-Moore | Rabin-Karp | BITAP | overview

Page 11

occurrences of each symbol into our pattern by assigning M[s][pos] = 0, where s is the symbol and pos is the position in the pattern where s occurs. For the rest of the matrix values, they are assigned to 1. We use a new variable named state of size 31 initialized with ~1 = 111…..10. The process will run by iterating through the characters of the text and performing the two operations below: 1) select the current character of the text and bitwise-OR the corresponding pattern (from the matrix) with the state 2) left-shift the state For example, taking the pattern string “aba”, the matrix will look like: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 | Pattern(a) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 | Pattern(b) The initial state will be: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 The text we want to search in is “baba” Iterating through the first character “b”, we perform Pattern(b) | State, then State << 1, with updating the State after each operation, to result the following new state: 1111111111111111111111111111110 Repeating the process over and over and going through different states, we can check if the pattern was found by testing the lowest m+1th bit of the state against 0. This algorithm has a runtime of O( |mb/w| (n + S)) , for the preprocessing time, where | mb/w | should be considered as the upper bond of mb/w, representing the time to compute a constant number of operations on integers of mb bits using a word of size w. As for the search time, the complexity in the worst and average case is O( |mb/w| n).

VIII. Comparison and contrast; advantages and disadvantages To summarize and quickly compare the approaches offered by the five algorithms discussed, I created the following table:

Complexity Naïve

O((n-m+1)*m)

Additional data structures/ techniques -

Most suitable for

Disadvantages

Applications

Very short Slow and For sized text & inefficient for educational pattern large strings purposes


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.