Docsity

String Search Algorithms: Brute Force, Karp-Rabin, Knuth-Morris-Pratt, Boyer-Moore, Slides of Data Representation and Algorithm Design

Toggle sidebar
bg1
String Searching
2
String Search
String search. Given a pattern string, find first match in text.
Model. Can't afford to preprocess the text.
Parameters. N = length of text, M = length of pattern.
inahay
Text
stackaneedleina
needle
Pattern
M = 6, N = 21
typically N >> M
3
Applications
Applications.
!Parsers.
!Lexis/Nexis.
!Spam filters.
!Virus scanning.
!Digital libraries.
!Screen scrapers.
!Word processors.
!Web search engines.
!Natural language processing.
!Carnivore surveillance system.
!Computational molecular biology.
!Feature detection in digitized images.
4
Brute Force: Typical Case
hayneedsanneedlex
needle
needle
needle
needle
needle
needle
needle
needle
needle
needle
needle
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download String Search Algorithms: Brute Force, Karp-Rabin, Knuth-Morris-Pratt, Boyer-Moore and more Slides Data Representation and Algorithm Design in PDF only on Docsity!

String Searching

2

String Search

String search. Given a pattern string, find first match in text.

Model. Can't afford to preprocess the text.

Parameters. N = length of text, M = length of pattern.

i n a h a y Text s t a c k a n e e d l e i n a n e e d l e Pattern M = 6 , N = 21 typically N >> M 3

Applications

Applications.

! Parsers.

! Lexis/Nexis.

! Spam filters.

! Virus scanning.

! Digital libraries.

! Screen scrapers.

! Word processors.

! Web search engines.

! Natural language processing.

! Carnivore surveillance system.

! Computational molecular biology.

! Feature detection in digitized images.

4

Brute Force: Typical Case

h a y n e e d s a n n e e d l e x n e e d l e n e e d l e n e e d l e n e e d l e n e e d l e n e e d l e n e e d l e n e e d l e n e e d l e n e e d l e n e e d l e

5

Brute Force

Brute force. Check for pattern starting at every text position.

public static int search(String pattern, String text) { int M = pattern.length(); int N = text.length(); for (int i = 0 ; i < N - M; i++) { int j; for (j = 0 ; j < M; j++) { if (text.charAt(i+j) != pattern.charAt(j)) break; } if (j == M) return i; // return offset i of match } return - 1 ; // not found } 6

Brute Force: Worst Case

a a a a a a a a a a a a a a a a b a a a a a b a a a a a b a a a a a b a a a a a b a a a a a b a a a a a b a a a a a b a a a a a b a a a a a b a a a a a b a a a a a b a a a a a b 7

Analysis of Brute Force

Analysis of brute force.

! Running time depends on pattern and text.

! Slow if M and N are large, and have lots of repetition.

Implementation Typical Worst Brute 1. 1 N †^ M N † assumes appropriate model character comparisons Search for M-character pattern in N-character text 8

Screen Scraping

Goal. Find current stock price of Google.

http://finance.yahoo.com/q?s=goog NYSE symbol

13

Computing the Hash Function

Brute force. O(M) arithmetic ops per hash.

Faster method to compute hash of adjacent substrings.

! Use previous hash to compute next hash.

! O(1) time per hash.

Ex.

! Pre-computed: 10000 % 97 = 9

! Previous hash: 41592 % 97 = 76

! Next hash: 15926 % 97 = ??

Observation.

key property of mod: can mod out any time except first one 14 public static int search(String p, String t) { int M = p.length(), N = t.length(); int q = 8355967 ; // table size int d = 256 ; // radix int dM = 1 ; // precompute d^(M- 1 ) % q for (int j = 1 ; j < M; j++) dM = (d * dM) % q; int h 1 = 0 , h 2 = 0 ; for (int i = 0 ; i < M; j++) { h 1 = (h 1 d + p.charAt(i)) % q; // hash of pattern h 2 = (h 2 d + t.charAt(i)) % q; // hash of text } if (h 1 == h 2 ) return 0 ; // match found for (int i = M; i < N; i++) { h 2 = (h 2 + dq - dMt.charAt(i-M)) % q; // remove leftmost digit h 2 = (h 2 *d + t.charAt(i)) % q;M)) % q; // insert rightmost digit if (h 1 == h 2 ) return i - M + 1 ; // match found } return - 1 ; // not found }

Java Implementation

15

Karp-Rabin: False Matches

False match. Hash of pattern collides with another substring.

How to choose modulus p.

! p too small $ many false matches.

! p too large $ too much arithmetic.

! Ex: p = 8355967 $ avoid 32-bit integer overflow.

! Ex: p = 35888607147294757 $ avoid 64-bit integer overflow.

16

Karp-Rabin: Randomized Algorithms

Theorem. If MN " 29 and p is a random prime between 1 and MN^2 , then

Pr[false match] # 2.53/N.

Randomized algorithm. Choose table size p at random to be huge prime.

Monte Carlo. Don't bother checking for false matches.

! Guaranteed to be fast: O(M + N).

! Expected to be correct (but false match possible).

Las Vegas. Upon hash match, do full compare; if false match, try again

with new random prime.

! Guaranteed to be correct.

! Expected to be fast: O(M + N).

Q. Would either version of Rabin-Karp make a good library function?

relies on prime number theorem

17

String Search Implementation Cost Summary

Karp-Rabin summary.

! Create fingerprint of each substring and compare fingerprints.

! Expected running time is linear.

! Idea generalizes, e.g., to 2D patterns.

Karp-Rabin Implementation %(N) Typical %(N) ‡ Worst Brute 1. 1 N †^ M N † assumes appropriate model ‡ randomized character comparisons Search for M-character pattern in N-character text 18

Knuth-Morris-Pratt

Don Knuth 1974 Turing award Jim Morris Vaughan Pratt 19

Knuth-Morris-Pratt: DFA Simulation

KMP algorithm. [over binary alphabet]

! Build DFA from pattern.

! Run DFA on text.

3 4 a a 5 a 0 1 a a 2 b b b b b b a a a a b a a Text b a a a b accept state a a b a a a a a b a a a a a b a a a 20

Knuth-Morris-Pratt: DFA Simulation

Interpretation of state i. Length of longest prefix of search pattern

that is a suffix of input string.

Ex. End in state 4 iff text ends in aaba.

Ex. End in state 2 iff text ends in aa (but not aabaa or aabaaa).

3 4 a a 5 a 0 1 a a 2 b b b b b b a a a b a a a Pattern accept state

25

Knuth-Morris-Pratt: DFA Construction

DFA construction for KMP. DFA builds itself!

State 6. Given DFA for aabaaa and state X of simulating aabaaa,

compute DFA for aabaaab and state X of simulating aabaaab.

! next[ 6 ] = X & a = 2.

! Update X = X & b = 3.

3 4 a a 5 6 a 0 1 a a 2 b b b b b b a b 7 a X = 2 26

DFA Construction for KMP

DFA construction for KMP. DFA builds itself!

State 7. Given DFA for aabaaab and state X of simulating aabaaab,

compute DFA for aabaaabb and state X of simulating aabaaabb.

! next[ 7 ] = X & a = 4.

! Update X = X & b = 0.

3 4 a a 5 6 a 0 1 a a 2 b b 7 b b 8 b a b b (^) a b a X = 3 27

DFA Construction for KMP: Java Implementation

Build DFA for KMP.

! Takes O(M) time.

! Requires O(M) extra space to store next[] table.

int X = 0 ; int[] next = new int[M]; for (int j = 1 ; j < M; j++) { if (p.charAt(X) == p.charAt(j)) { // char match next[j] = next[X]; X = X + 1 ; } else { // char mismatch next[j] = X + 1 ; X = next[X]; } } DFA Construction for KMP (assumes binary alphabet) 28

Optimized KMP Implementation

Ultimate search program for aabaaabb pattern.

! Specialized C program.

! Machine language version of C program.

int kmpearch(char t[]) { int i = 0 ; s 0 : if (t[i++] != 'a') goto s0; s 1 : if (t[i++] != 'a') goto s0; s 2 : if (t[i++] != 'b') goto s2; s 3 : if (t[i++] != 'a') goto s0; s 4 : if (t[i++] != 'a') goto s0; s 5 : if (t[i++] != 'a') goto s3; s 6 : if (t[i++] != 'b') goto s2; s 7 : if (t[i++] != 'b') goto s4; return i - 8 ; } (^) next[] assumes pattern is in text (o/w use sentinel) pattern[]

29

KMP Over Arbitrary Alphabet

DFA for patterns over arbitrary alphabet '.

! For each character in alphabet, determine next state.

! Lookup table requires O(M |'|) space.

Ex. DFA for pattern ababcb.

0 a^1 b^2 a^3 b^4 c^5 b b b, c c a a c b, c a a can be expensive if ' = Unicode alphabet c 30

KMP Over Arbitrary Alphabet

NFA for patterns over arbitrary alphabet '.

! Read new character only upon success (or failure at beginning).

! Reuse current character upon failure and follow back.

Ex. NFA for pattern ababcb.

0 1 2 3 4 5 mismatch^ mismatch a (^) b a b c b mismatch mismatch

a b a b a a mismatch b a text = ababaaba 31

String Search Implementation Cost Summary

KMP analysis.

! NFA simulation requires at most 2N comparisons.

  • advances # N
  • retreats # advances

! NFA construction takes %(M) time and space.

Karp-Rabin Implementation KMP %(N) Typical

  1. 1 N † %(N) ‡ Worst 2 N Brute 1. 1 N †^ M N character comparisons † assumes appropriate model ‡ randomized Search for M-character pattern in N-character text 32

History of KMP

History of KMP.

! Inspired by esoteric theorem of Cook that says linear time

algorithm should be possible for 2-way pushdown automata.

! Discovered in 1976 independently by two theoreticians and a hacker.

  • Knuth: discovered linear time algorithm
  • Pratt: made running time independent of alphabet
  • Morris: trying to build a text editor.

Resolved theoretical and practical problems.

! Surprise when it was discovered.

! In hindsight, seems like right algorithm.

37

Bad Character Rule: Java Implementation

public static int search(String pattern, String text) { int M = pattern.length(), N = text.length(); int[] right = new int[ 256 ]; for (int c = 0 ; c < 256 ; c++) right[c] = - 1 ; for (int j = 0 ; j < M; j++) right[pattern.charAt(j)] = j; int i = 0 ; // offset while (i < N - M) { int skip = 0 ; for (int j = M- 1 ; j >= 0 ; j--) { if (pattern.charAt(j) != text.charAt(i + j)) { skip = Math.max( 1 , j - right[text.charAt(i + j)]); break; } } if (skip == 0 ) return i; // found i = i + skip; } return - 1 ; } rightmost occurrence of c in pattern bad character rule 38

Bad Character Rule: Analysis

Bad character rule analysis.

! Highly effective in practice, particularly for English text: O(N / M).

! Takes ((MN) time in worst case.

b a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a b a a a a a a b a a a a a a b a a a a a a b a a a a a a b a a a a a a b a a a a a a b a a a a a a 39

Strong Good Suffix Rule

Strong good suffix rule. [a KMP-like suffix rule]

! Right-to-left scanning.

! Suppose text matches suffix t of pattern but mismatches in

previous character c.

! Find rightmost copy of t in pattern whose preceding letter is not c,

and shift; if no such copy, shift M positions.

x c a b d a b d a b string good suffix rule: can skip over this since we already know dab doesn't match bad character rule: skip only 1 position x c a b d a b d a b x x x x x x x b a b?????? x x x x x x x x t = "ab" c = 'b' 40

Boyer-Moore

Boyer-Moore.

! Right-to-left scanning.

! Bad character rule.

! Strong good suffix rule.

Boyer-Moore analysis.

! O(N / M) average case if given letter usually doesn't occur in string.

  • time decreases as pattern length increases
  • sublinear in input size!

! At most 3N comparisons to find a match.

Boyer-Moore in the wild. Unix grep, emacs.

always take best of two shifts

41

String Search Implementation Cost Summary

Karp-Rabin Implementation KMP %(N) Typical

  1. 1 N † %(N) ‡ Worst 2 N Boyer-Moore N / M †^3 N Brute 1. 1 N †^ M N † assumes appropriate model ‡ randomized Search for M-character pattern in N-character text 42

Boyer-Moore and Alphabet Size

Boyer-Moore space requirement. %(M + |'|)

Big alphabets.

! Direct implementation may be impractical, e.g., Unicode.

! Fix: search one byte at a time.

Small alphabets.

! Loses effectiveness when ' is too small, e.g., DNA.

! Fix: group characters together, e.g., aaaa, aaac, ….

43

Finding All Matches

Karp-Rabin. Can find all matches in O(M + N) expected time using

Muthukrishnan variant.

Knuth-Morris-Pratt. Can find all matches in O(M + N) time via simple

modification.

Boyer-Moore. Can find all matches in O(M + N) time using Galil variant.

search pattern: aabaaa 0 a^1 a^2 b 3 a^4 a^5 a 6 b b b b b a accept state b a 44

Multiple String Search

Multiple string search. Search for any of k different patterns.

! Naïve KMP: O(kN + M 1 + … + Mk).

! Aho-Corasick: O(N + M 1 + … + Mk).

! Ex: screen out dirty words from a text stream.

0 b^1 a^2 b 3 a 4 b 5 a 6 a 7 a b (^8) b 9 b a (^) a DFA for ( aaa or abb or baba )