Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

String matching algorithm, Rabin Karp Algorithm, KMP Algorithm

Typology: Lecture notes

2022/2023

Uploaded on 05/05/2023

aishvarya-srivastava
aishvarya-srivastava 🇮🇳

3 documents

1 / 112

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
String Matching
Finding all occurrences of a pattern in
the text.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

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

nm

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 ( tshT [ 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 ( tshT [ 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 ( tshT [ 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 ( tshT [ 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 ( tshT [ 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 ( tshT [ 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.