Docsity

String Algorithms, Lecture notes of Algorithms and Programming

Partial preview of the text

Download String Algorithms and more Lecture notes Algorithms and Programming in PDF only on Docsity!

String Algorithms

Jaehyun Park Stanford UniversityCS 97SI

June 30, 2015

Outline

String Matching Problem Hash Table Knuth-Morris-Pratt (KMP) Algorithm Suffix Trie Suffix Array

String Matching Problem 2

Example

◮ T = AGCATGCTGCAGTCATGCTTAGGCTA

◮ P = GCT

◮ (^) P appears three times in T ◮ (^) A naive method takes O ( mn ) time

  • – Initiate string comparison at every starting pointEach comparison takes O ( m ) time

◮ (^) We can do much better!

String Matching Problem 4

Outline

String Matching Problem Hash Table Knuth-Morris-Pratt (KMP) Algorithm Suffix Trie Suffix Array

Hash Table 5

Hash Table

◮ (^) Main idea: preprocess T to speedup queries

  • – Hash every substring of length k is a small constant k

◮ (^) For each query P , hash the first k letters of P to retrieve all the occurrences of it within T ◮ (^) Don’t forget to check collisions!

Hash Table 7

Hash Table

◮ (^) Pros:

  • – Easy to implementSignificant speedup in practice

◮ (^) Cons: – Doesn’t help the asymptotic efficiency ◮ (^) Can still takedifficult Θ( nm ) time if hashing is terrible or data is

  • A lot of memory consumption

Hash Table 8

Knuth-Morris-Pratt (KMP) Matcher

◮ (^) A linear time (!) algorithm that solves the string matchingproblem by preprocessing P in Θ( m ) time

  • Main idea is to skip some comparisons by using the previouscomparison result ◮ (^) Uses an auxiliary array π that is defined as the following:
  • π a suffix of[ i ] is the largest integer smaller than P 1_... Pi i_ such that P 1_... Pπ_ [ i ] is ◮ (^) ... It’s better to see an example than the definition

Knuth-Morris-Pratt (KMP) Algorithm 10

π Table Example (from CLRS)

◮ (^) π is a suffix of[ i ] is the largest integer smaller than P i such that P 1_... Pπ_ [ i ]

  • e.g. , π [6] = 4^1_... P_ since i abab is a suffix of ababab
  • e.g. , π [9] = 0 since no prefix of length ≤ 8 ends with c ◮ (^) Let’s see why this is useful

Knuth-Morris-Pratt (KMP) Algorithm 11

Using the π Table

◮ (^) We matched k = 3 letters so far, and π [ k ] = 0

  • Thus, there is no point in starting the comparison at (crucial observation) T 2 , T 3 ◮ (^) Shift P by k − π [ k ] = 3 letters

◮ (^) Mismatch at T 4 again!

Knuth-Morris-Pratt (KMP) Algorithm 13

Using the π Table

◮ (^) We matched k = 0 letters so far ◮ (^) Shift P by k − π [ k ] = 1 letter (we define π [0] = − 1 )

◮ (^) Mismatch at T 11!

Knuth-Morris-Pratt (KMP) Algorithm 14

Using the π Table

◮ (^) Mismatch at T 11 again!

◮ (^) Currently 2 letters are matched ◮ (^) Shift P by 2 − π [2] = 2 letters

Knuth-Morris-Pratt (KMP) Algorithm 16

Using the π Table

◮ (^) Mismatch at T 11 yet again!

◮ (^) Currently no letters are matched ◮ (^) Shift P by 0 − π [0] = 1 letter

Knuth-Morris-Pratt (KMP) Algorithm 17

Using the π Table

◮ (^) Finally, there it is!

◮ (^) Currently all 7 letters are matched ◮ (^) After recording this match (atorder to find other matches T 16_... T_ 22 , we shift P again in

  • Shift by 7 − π [7] = 7 letters

Knuth-Morris-Pratt (KMP) Algorithm 19

Computing π

◮ (^) Observation 1: if P P 1_... Pπ_ [ i ] is a suffix of P 1_... Pi_ , then (^1) –... P Well, obviously... π [ i ]− 1 is a suffix of^ P 1_... Pi_ − 1 ◮ (^) Observation 2: all the prefixes of P P that are a suffix of (^1) –... P e.g.i , (^) can be obtained by recursively applying P 1_... Pπ_ [ i ], P 1_... , Pπ_ [ π [ i ]], P 1_... , Pπ_ [ π [ π [ i ]]]^ π are all^ to^ i suffixes of P 1_... Pi_

Knuth-Morris-Pratt (KMP) Algorithm 20