Docsity

String matching algorithm, Lecture notes of Design and Analysis of Algorithms

Partial preview of the text

Download String matching algorithm and more Lecture notes Design and Analysis of Algorithms in PDF only on Docsity!

String Matching

Finding all occurrences of a pattern in

the text.

Applications

• Spell Checkers.

• Search Engines.

• Spam Filters.

• Intrusion Detection System.

• Plagiarism Detection.

• Bioinformatics – DNA Sequencing.

• Digital Forensics.

• Information Retrieval, etc.

Contd…

shift = 0

shift = 1

shift = 2

shift = 3

shift = 4

shift = 5

shift = 6

shift = 7

shift = 8

shift = 9

T a b c a b a a b c a b a c

P a b a a

P a b a a

P a b a a

P a b a a

P a b a a

P a b a a

P a b a a

P a b a a

P a b a a

P a b a a

1 2 3 4 5 6 7 8 9 10 11 12 13

Contd…

• Shift s a valid shift, if P occurs with shift s in T.

– 0 ≤ s ≤ n – m and T [ s + 1.. s + m ] = P [1.. m ].

• Otherwise, shift s is an invalid shift.

• String-matching problem means finding all valid

shifts with which a given pattern P occurs in a given

text T.

Example

• Text T = acaabc , and pattern P = aab.

Rabin-Karp Algorithm

• Uses elementary number-theoretic notions.

– Equivalence of two numbers modulo a third

number.

• Let, Σ = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }.

• String of k consecutive characters represents a

length- k decimal number.

– Thus, character string 31415 corresponds to the

decimal number 31,415.

• Note:

– In the general case, each character is a digit in

radix- d notation, where d = |Σ|.)

A few calculations…

• For a pattern P [1.. m ], let p denote its corresponding

value in radix- d notation.

• Using Horner’s rule, p can be computed in time ϴ ( m ).

p = P [ m ] + d ( P [ m – 1] + d ( P [ m – 2] + … + d ( P [2] + dP [1]) …)).

• Similarly, for a text T [1.. n ], let t

s

denotes the radix- d

notation value of the length- m substring T [ s + 1.. s +

m ], for s = 0, 1, …, n – m.

• Again, t

0

can be computed from T [1.. m ] in ϴ ( m ).

Contd…

• Each of the remaining values t

1

, t

2

, …, t

n – m

can be

computed in constant time.

– Subtracting d

m – 1

T [ s + 1] removes the high-order digit

from t

s

, multiplying the result by d shifts the number left

by one digit position, and adding T [ s + m + 1] brings in

the appropriate low-order digit.

t

s +

= d ( t

s

– d

m – 1

T [ s + 1]) + T [ s + m + 1].

• Let h = d

m – 1

, then

t

s +

= d ( t

s

– hT [ s + 1]) + T [ s + m + 1].

• Modulus:

– p modulo q takes ϴ ( m ) time.

– For all t

s

, t

s

modulo q takes ϴ ( n – m + 1) time.

Example – 1

Index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Text (T) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2

Pattern: 3 1 4 1 5

Σ = {0, 1, …, 9} d = |Σ| = 10 q = 13

  • n = 19, m = 5, n – m = 14.
  • h = 10 5- mod 13 = 10 4 mod 13 = 3.
  • p = (3 × 10 4 + 1 × 10 3 + 4 × 10 2 + 1 × 10 1 + 5 × 10 0 ) mod 13 = 7.
  • t 0 = (2 × 10 4 + 3 × 10 3 + 5 × 10 2 + 9 × 10 1 + 0 × 10 0 ) mod 13 = 8. Step 1: s = 0, t 0 = 8, p = 7. p == t 0 → No. s < 14 → Yes. ts +1 = d ( ts – hT [ s + 1]) + T [ s + m + 1] (mod 13) t 1 = 10 (8 – 3 (2)) + 2 (mod 13) t 1 = 10 (2) + 2 (mod 13) = 22 (mod 13) = 9.

Contd…

Index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Text (T) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2

Pattern: 3 1 4 1 5

Σ = {0, 1, …, 9} d = |Σ| = 10 q = 13

  • n = 19, m = 5, n – m = 14.
  • h = 10 5- mod 13 = 10 4 mod 13 = 3.
  • p = (3 × 10 4 + 1 × 10 3 + 4 × 10 2 + 1 × 10 1 + 5 × 10 0 ) mod 13 = 7.
  • t 0 = (2 × 10 4 + 3 × 10 3 + 5 × 10 2 + 9 × 10 1 + 0 × 10 0 ) mod 13 = 8. Step 2: s = 1, t 1 = 9, p = 7. p == t 1 → No. s < 14 → Yes. ts +1 = d ( ts – hT [ s + 1]) + T [ s + m + 1] (mod 13) t 2 = 10 (9 – 3 (3)) + 3 (mod 13) t 2 = 10 (0) + 3 (mod 13) = 3 (mod 13) = 3.

Contd…

Index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Text (T) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2

Pattern: 3 1 4 1 5

Σ = {0, 1, …, 9} d = |Σ| = 10 q = 13

  • n = 19, m = 5, n – m = 14.
  • h = 10 5- mod 13 = 10 4 mod 13 = 3.
  • p = (3 × 10 4 + 1 × 10 3 + 4 × 10 2 + 1 × 10 1 + 5 × 10 0 ) mod 13 = 7.
  • t 0 = (2 × 10 4 + 3 × 10 3 + 5 × 10 2 + 9 × 10 1 + 0 × 10 0 ) mod 13 = 8. Step 4: s = 3, t 3 = 11, p = 7. p == t 3 → No. s < 14 → Yes. ts + 1 = d ( ts – hT [ s + 1 ]) + T [ s + m + 1 ] (mod 13 ) t 4 = 10 (11 – 3 (9)) + 4 (mod 13) = 10 (-16) + 4 (mod 13) Because -16 mod 13 = 10^ t 4 = 10 (10) + 4 (mod 13)^ = 104 (mod 13) = 0.

Contd…

Index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Text (T) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2

Pattern: 3 1 4 1 5

Σ = {0, 1, …, 9} d = |Σ| = 10 q = 13

  • n = 19, m = 5, n – m = 14.
  • h = 10 5- mod 13 = 10 4 mod 13 = 3.
  • p = (3 × 10 4 + 1 × 10 3 + 4 × 10 2 + 1 × 10 1 + 5 × 10 0 ) mod 13 = 7.
  • t 0 = (2 × 10 4 + 3 × 10 3 + 5 × 10 2 + 9 × 10 1 + 0 × 10 0 ) mod 13 = 8. Step 5: s = 4, t 4 = 0, p = 7. p == t 4 → No. s < 14 → Yes. ts + 1 = d ( ts – hT [ s + 1 ]) + T [ s + m + 1 ] (mod 13 ) t 5 = 10 (0 – 3 (0)) + 1 (mod 13) t 5 = 1 (mod 13) = 1.

Contd…

Index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Text (T) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2

Pattern: 3 1 4 1 5

Σ = {0, 1, …, 9} d = |Σ| = 10 q = 13

  • n = 19, m = 5, n – m = 14.
  • h = 10 5- mod 13 = 10 4 mod 13 = 3.
  • p = (3 × 10 4 + 1 × 10 3 + 4 × 10 2 + 1 × 10 1 + 5 × 10 0 ) mod 13 = 7.
  • t 0 = (2 × 10 4 + 3 × 10 3 + 5 × 10 2 + 9 × 10 1 + 0 × 10 0 ) mod 13 = 8. Step 7: s = 6, t 6 = 7, p = 7. p == t 6 → Yes. Character by character matching p[1..5] == T[7..11]. {3 1 4 1 5} == {3 1 4 1 5}. Match, hence s = 6 is a valid shift. s < 14 → Yes. ts +1 = d ( ts – hT [ s + 1]) + T [ s + m + 1] (mod 13) t 7 = 10 (7 – 3 (3)) + 2 (mod 13) = 10 (-2) + 2 (mod 13) t 7 = 10 (11) + 2 (mod 13) = 112 (mod 13) = 8. Because -2 mod 13 = 11

Contd…

Index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Text (T) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2

Pattern: 3 1 4 1 5

Σ = {0, 1, …, 9} d = |Σ| = 10 q = 13

  • n = 19, m = 5, n – m = 14.
  • h = 10 5- mod 13 = 10 4 mod 13 = 3.
  • p = (3 × 10 4 + 1 × 10 3 + 4 × 10 2 + 1 × 10 1 + 5 × 10 0 ) mod 13 = 7.
  • t 0 = (2 × 10 4 + 3 × 10 3 + 5 × 10 2 + 9 × 10 1 + 0 × 10 0 ) mod 13 = 8. Step 8: s = 7, t 7 = 8, p = 7. p == t 7 → No. s < 14 → Yes. ts +1 = d ( ts – hT [ s + 1]) + T [ s + m + 1] (mod 13) t 8 = 10 (8 – 3 (1)) + 6 (mod 13) t 8 = 10 (5) + 6 (mod 13) = 56 (mod 13) = 4.