Aoa Chapter 6
Aoa Chapter 6
NAIVE-STRING-MATCHER(T,P)
1. n = T:length a c a a b c
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m] a a b
4. print “Pattern occurs with shift” s
NAIVE-STRING-MATCHER(T,P)
1. n = T:length a c a a b c
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m] a a b
4. print “Pattern occurs with shift” s
NAIVE-STRING-MATCHER(T,P)
1. n = T:length a c a a b c
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m] a a b
4. print “Pattern occurs with shift” s
1 2 3 4 5 6
a c a a b c T
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
1 2 3 4 5 6
a c a a b c T
a a b P
1 2 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
1 2 3 4 5 6
a c a a b c T
a a b P
1 2 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
1 2 3 4 5 6
a c a a b c T n=6
a a b P
1 2 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
1 2 3 4 5 6
a c a a b c T n=6
a a b P
1 2 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=0 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=0 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=0 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=0 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=0 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=0 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=0 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=0 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=0 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=0 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=0 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1..3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1+1..1+3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[2 .. 4]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[2 .. 4]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[2 .. 4]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[2 .. 4]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[2 .. 4]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[2 .. 4]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[2 .. 4]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[2 .. 4]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=1 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[2 .. 4]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=2 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[1+2 .. 2+3]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=2 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[3 .. 5]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=2 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[3 .. 5]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=2 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[3 .. 5]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=2 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[3 .. 5]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=2 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[3 .. 5]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=2 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[3 .. 5]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=2 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
P[1..3] = T[3 .. 5]
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=2 n-m = 3
The Naive string-matching algorithm
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
Pattern Occurs with shift 2
1 2 3 4 5 6
a c a a b c T n=6
a a b P m=3
1 2 3
S=2 n-m = 3
The Naive string-matching algorithm:
Time Complexity
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m (n – m) + 1
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
The Naive string-matching algorithm:
Time Complexity
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m (n – m) + 1
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s
m
The Naive string-matching algorithm:
Time Complexity
NAIVE-STRING-MATCHER(T,P)
1. n = T:length
2. m = P:length
3. for s = 0 to n - m (n – m) + 1
1. if P[1..m] == T[s + 1.. s + m]
4. print “Pattern occurs with shift” s O((n – m) + 1)m)
m
Topics to be covered
➢ The Naive string-matching algorithm
➢ The Rabin Karp algorithm
➢ The Knuth-Morris-Pratt algorithm
The Rabin Karp algorithm
➢ Rabin-Karp string searching algorithm calculates a numerical (hash) value
for the pattern p, and for each m-character substring of text t.
➢ Then it compares the numerical values instead of comparing the actual
symbols.
➢ If any match is found, it compares the pattern with the substring by naive
approach.
➢ Otherwise it shifts to next substring of t to compare with p.
The Rabin Karp algorithm
➢ Let us assume that ∑ = {0, 1, 2, … ,9}, so that each character is a a 0
decimal digit. (Radix = | ∑ | )
b 1
➢ Given a pattern P[1..m]
➢ let p denote its corresponding decimal value c 2
➢ In a similar manner, given a text T[1.. n] d 3
➢ Let ts denote the decimal value of the length-m substring e 4
f 5
g 6
h 7
i 8
j 9
The Rabin Karp algorithm
➢ Let us assume that ∑ = {0, 1, 2, … ,9}, so that each character is a a 0
decimal digit. (Radix = | ∑ | )
b 1
➢ Given a pattern P[1..m]
➢ let p denote its corresponding decimal value c 2
➢ In a similar manner, given a text T[1.. n] d 3
➢ Let ts denote the decimal value of the length-m substring e 4
p is computed by using Horner’s rule f 5
p = P[m] + 10(p[m-1] + 10(p[m-2] + …. + 10(p[1])….) g 6
h 7
i 8
j 9
The Rabin Karp algorithm
➢ Let us assume that ∑ = {0, 1, 2, … ,9}, so that each character is a a 0
decimal digit. (Radix = | ∑ | )
b 1
➢ Given a pattern P[1..m]
➢ let p denote its corresponding decimal value c 2
➢ In a similar manner, given a text T[1.. n] d 3
➢ Let ts denote the decimal value of the length-m substring e 4
p is computed by using Horner’s rule f 5
p = P[m] + 10(p[m-1] + 10(p[m-2] + …. + 10(p[1])….) g 6
h 7
31415 = 5 + 10(1 + 10(4 + 10(1 + 10(3))))
i 8
j 9
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
P
1 2 3 4 5
3 1 4 1 5 Find P mod 13
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
P
1 2 3 4 5
3 1 4 1 5
7
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
P
1 2 3 4 5
3 1 4 1 5 Length of P is 5
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
P
1 2 3 4 5
3 1 4 1 5
7
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
8 9
P
1 2 3 4 5
3 1 4 1 5
7
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
8 9 3
P
1 2 3 4 5
3 1 4 1 5
7
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
8 9 3 11
P
1 2 3 4 5
3 1 4 1 5
7
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
8 9 3 11 0
P
1 2 3 4 5
3 1 4 1 5
7
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
8 9 3 11 0 1
P
1 2 3 4 5
3 1 4 1 5
7
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
8 9 3 11 0 1 7
P
1 2 3 4 5
3 1 4 1 5
7
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
8 9 3 11 0 1 7
P
1 2 3 4 5
3 1 4 1 5
7
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
8 9 3 11 0 1 7 8 4 3 5 10 11 7
P
1 2 3 4 5
3 1 4 1 5
7
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
8 9 3 11 0 1 7 8 4 3 5 10 11 7
P
1 2 3 4 5
3 1 4 1 5
7
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
P
1 2 3 4 5
3 1 4 1 5
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
7 ?
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
7 ?
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
7 ?
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
7 ?
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
7 ?
shift
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
7 ?
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
7 ?
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
7 ?
14152 =8
The Rabin Karp algorithm
T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
7 8
14152 =8
The Rabin Karp algorithm
RABIN-KARP-MATCHER(T, P, d, q)
n = T . length
m = P . length
h = dm - 1 mod q
p=0
t0 = 0
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
The Rabin Karp algorithm
RABIN-KARP-MATCHER(T, P, d, q)
n = T . length
m = P . length
h = dm - 1 mod q text T
p=0
t0 = 0
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
The Rabin Karp algorithm
RABIN-KARP-MATCHER(T, P, d, q)
n = T . length
m = P . length
h = dm - 1 mod q the pattern P
p=0
t0 = 0
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
The Rabin Karp algorithm
RABIN-KARP-MATCHER(T, P, d, q)
n = T . length
m = P . length
h = dm - 1 mod q the radix d to use
p=0 = │∑│ = 10 a 0
t0 = 0
for i = 1 to m // preprocessing b 1
RABIN-KARP-MATCHER(T, P, d, q)
n = T . length
m = P . length
h = dm - 1 mod q the prime q to use
p=0
t0 = 0
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
The Rabin Karp algorithm
RABIN-KARP-MATCHER(T, P, d, q) T
n = T . length
c d f j a c d b e b f c g h d j j c b
m = P . length
h = dm - 1 mod q
p=0
t0 = 0
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
The Rabin Karp algorithm
RABIN-KARP-MATCHER(T, P, d, q) T
n = T . length
c d f j a c d b e b f c g h d j j c b
m = P . length
h = dm - 1 mod q P
p=0
t0 = 0 d b e b f
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
c d f j a c d b e b f c g h d j j c b
m = P . length
h = dm - 1 mod q
P
p=0
t0 = 0 1 2 3 4 5 a 0
for i = 1 to m // preprocessing d b e b f b 1
p = (dp + P[i]) mod q c 2
t0 = (dt0 + T[i]) mod q
d 3
for s = 0 to n - m // matching
if p == ts e 4
if P[1 .. m] == T[s + 1 .. s + m] f 5
print “Pattern occurs with shift” s
g 6
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q h 7
i 8
j 9
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 d b e b f a 0
for i = 1 to m // preprocessing b 1
p = (dp + P[i]) mod q c 2
t0 = (dt0 + T[i]) mod q
d 3
for s = 0 to n - m // matching
if p == ts e 4
if P[1 .. m] == T[s + 1 .. s + m] f 5
print “Pattern occurs with shift” s
g 6
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q h 7
i 8
j 9
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5 a 0
for i = 1 to m // preprocessing b 1
d b e b f
p = (dp + P[i]) mod q c 2
t0 = (dt0 + T[i]) mod q
d 3
for s = 0 to n - m // matching
if p == ts e 4
if P[1 .. m] == T[s + 1 .. s + m] f 5
print “Pattern occurs with shift” s
g 6
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q h 7
i 8
j 9
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5 a 0
for i = 1 to m // preprocessing b 1
p = (dp + P[i]) mod q c 2
t0 = (dt0 + T[i]) mod q
d 3
for s = 0 to n - m // matching
if p == ts e 4
if P[1 .. m] == T[s + 1 .. s + m] f 5
print “Pattern occurs with shift” s
g 6
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q h 7
i 8
j 9
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=0
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=0
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=0 t0 = 0
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*0 + 3) mod 13 = 3
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*0 + 3) mod 13 = 3
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*0 + 3) mod 13 = 3
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*0 + 3) mod 13 = 3
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3 t0 = (10*0 + 2) mod 13 = 2
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*0 + 3) mod 13 = 3
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3 t0 = (10*0 + 2) mod 13 = 2
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*3 + 1) mod 13 = 5
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*3 + 1) mod 13 = 5
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*3 + 1) mod 13 = 5
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*3 + 1) mod 13 = 5
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3 t0 = (10*2 + 3) mod 13= 10
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*3 + 1) mod 13 = 5
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3 t0 = (10*2 + 3) mod 13= 10
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*5 + 4) mod 13 = 2
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*5 + 4) mod 13 = 2
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*5 + 4) mod 13 = 2
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*5 + 4) mod 13 = 2
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3 t0 =(10*10 +5) mod 13=1
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*5 + 4) mod 13 = 2
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3 t0 =(10*10 +5) mod 13=1
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*2 + 1) mod 13 = 8
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*2 + 1) mod 13 = 8
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*2 + 1) mod 13 = 8
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*2 + 1) mod 13 = 8
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3 t0 =(10*1 +9) mod 13=6
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*2 + 1) mod 13 = 8
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3 t0 =(10*1 +9) mod 13=6
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*8 + 5) mod 13 = 7
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*8 + 5) mod 13 = 7
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*8 + 5) mod 13 = 7
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*8 + 5) mod 13 = 7
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3 t0 =(10*6 +0) mod 13=8
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching h = 105-1 mod 13 p = (10*8 + 5) mod 13 = 7
if p == ts = 104 mod 13
if P[1 .. m] == T[s + 1 .. s + m] =3 t0 =(10*6 +0) mod 13=8
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t0 = 8 h =3
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t0 = 8 h =3 s = 0 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t0 = 8 h =3 s = 0 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t0 = 8 h =3 s = 0 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t0 = 8 h =3 s = 0 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t0 = 8 h =3 s = 0 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T1 = (10(8 – T[1]* 3) + T[6]) mod 13
for s = 0 to n - m // matching
if p == ts T1 = (10(8 – 2 *3) + 2) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T1 = (10(8 – 6) + 2) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T1 = (22) mod 13 = 9
n = 19 m=5 d = 10 q = 13 p=7 t0 = 8 h =3 s = 0 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T1 = (10(8 – T[1]* 3) + T[6]) mod 13
for s = 0 to n - m // matching
if p == ts T1 = (10(8 – 2 *3) + 2) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T1 = (10(8 – 6) + 2) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T1 = (22) mod 13 = 9
n = 19 m=5 d = 10 q = 13 p=7 t1 = 9 h =3 s = 0 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t1 = 9 h =3 s = 0 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t1 = 9 h =3 s = 1 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t1 = 9 h =3 s = 1 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t1 = 9 h =3 s = 1 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t1 = 9 h =3 s = 1 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t1 = 9 h =3 s = 1 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T2 = (10(9 – T[2]* 3) + T[7]) mod 13
for s = 0 to n - m // matching
if p == ts T2 = (10(9 – 3 *3) + 3) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T2 = (10(9 – 9) + 3) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T2 = (3) mod 13 = 2
n = 19 m=5 d = 10 q = 13 p=7 t1 = 9 h =3 s = 1 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T2 = (10(9 – T[2]* 3) + T[7]) mod 13
for s = 0 to n - m // matching
if p == ts T2 = (10(9 – 3 *3) + 3) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T2 = (10(9 – 9) + 3) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T2 = (3) mod 13 = 3
n = 19 m=5 d = 10 q = 13 p=7 t2 = 3 h =3 s = 1 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t2 = 3 h =3 s = 1 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t2 = 3 h =3 s = 2 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t2 = 3 h =3 s = 2 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t2 = 3 h =3 s = 2 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t2 = 3 h =3 s = 2 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t2 = 3 h =3 s = 2 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T3 = (10(3 – T[3]* 3) + T[8]) mod 13
for s = 0 to n - m // matching
if p == ts T3 = (10(3 – 5 *3) + 1) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T3 = (10(3 – 15) + 1) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T3 = (-119) mod 13 = 11
n = 19 m=5 d = 10 q = 13 p=7 t2 = 3 h =3 s = 2 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T3 = (10(3 – T[3]* 3) + T[8]) mod 13
for s = 0 to n - m // matching
if p == ts T3 = (10(3 – 5 *3) + 1) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T3 = (10(3 – 15) + 1) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T3 = (-119) mod 13 = 11
n = 19 m=5 d = 10 q = 13 p=7 t3 = 11 h =3 s = 2 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t3 = 11 h =3 s = 2 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t3 = 11 h =3 s = 3 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t3 = 11 h =3 s = 3 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t3 = 11 h =3 s = 3 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t3 = 11 h =3 s = 3 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t3 = 11 h =3 s = 3 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T4 = (10(11 – T[4]* 3) + T[9]) mod 13
for s = 0 to n - m // matching
if p == ts T4 = (10(11 – 9 *3) + 4) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T4 = (10(11 – 27) + 4) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T4 = (-156) mod 13 = 0
n = 19 m=5 d = 10 q = 13 p=7 t3 = 11 h =3 s = 3 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T4 = (10(11 – T[4]* 3) + T[9]) mod 13
for s = 0 to n - m // matching
if p == ts T4 = (10(11 – 9 *3) + 4) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T4 = (10(11 – 27) + 4) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T4 = (-156) mod 13 = 0
n = 19 m=5 d = 10 q = 13 p=7 t4 = 0 h =3 s = 3 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t4 = 0 h =3 s = 3 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t4 = 0 h =3 s = 4 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t4 = 0 h =3 s = 4 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t4 = 0 h =3 s = 4 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t4 = 0 h =3 s = 4 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t4 = 0 h =3 s = 4 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T5 = (10(0 – T[5]* 3) + T[10]) mod 13
for s = 0 to n - m // matching
if p == ts T5 = (10(0 – 0 *3) + 1) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T5 = (10(0 – 0) + 1) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T5 = (1) mod 13 = 1
n = 19 m=5 d = 10 q = 13 p=7 t4 = 0 h =3 s = 4 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T5 = (10(0 – T[5]* 3) + T[10]) mod 13
for s = 0 to n - m // matching
if p == ts T5 = (10(0 – 0 *3) + 1) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T5 = (10(0 – 0) + 1) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T5 = (1) mod 13 = 1
n = 19 m=5 d = 10 q = 13 p=7 t5 = 1 h =3 s = 4 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t5 = 1 h =3 s = 4 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t5 = 1 h =3 s = 5 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t5 = 1 h =3 s = 5 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t5 = 1 h =3 s = 5 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t5 = 1 h =3 s = 5 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t5 = 1 h =3 s = 5 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T6 = (10(1 – T[6]* 3) + T[11]) mod 13
for s = 0 to n - m // matching
if p == ts T6 = (10(1 – 2 *3) + 5) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T6 = (10(1 – 6) + 5) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T6 = (-45) mod 13 = 7
n = 19 m=5 d = 10 q = 13 p=7 t5 = 1 h =3 s = 5 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q T6 = (10(1 – T[6]* 3) + T[11]) mod 13
for s = 0 to n - m // matching
if p == ts T6 = (10(1 – 2 *3) + 5) mod 13
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s T6 = (10(1 – 6) + 5) mod 13
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q T6 = (-45) mod 13 = 7
n = 19 m=5 d = 10 q = 13 p=7 t6 = 7 h =3 s = 5 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t6 = 7 h =3 s = 5 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t6 = 7 h =3 s = 6 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t6 = 7 h =3 s = 6 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t6 = 7 h =3 s = 6 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t6 = 7 h =3 s = 6 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t6 = 7 h =3 s = 6 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m] Pattern occurs with shift s = 6
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t6 = 7 h =3 s = 6 to 14
The Rabin Karp algorithm T
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
RABIN-KARP-MATCHER(T, P, d, q) 2 3 5 9 0 2 3 1 4 1 5 2 6 7 3 9 9 2 1
n = T . length
m = P . length
P
h = dm - 1 mod q
1 2 3 4 5
p=0
t0 = 0 3 1 4 1 5
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m] Pattern occurs at position 7
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
n = 19 m=5 d = 10 q = 13 p=7 t6 = 7 h =3 s = 6 to 14
The Rabin Karp algorithm: Time Complexity
RABIN-KARP-MATCHER(T, P, d, q)
n = T . length
m = P . length
h = dm - 1 mod q
p=0
t0 = 0
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q O(m)
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
The Rabin Karp algorithm: Time Complexity
RABIN-KARP-MATCHER(T, P, d, q)
n = T . length
m = P . length
h = dm - 1 mod q
p=0
t0 = 0
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q O(m)
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s O(n-m)
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
The Rabin Karp algorithm: Time Complexity
RABIN-KARP-MATCHER(T, P, d, q)
n = T . length
m = P . length
h = dm - 1 mod q
p=0
t0 = 0
for i = 1 to m // preprocessing
p = (dp + P[i]) mod q O(m)
t0 = (dt0 + T[i]) mod q
for s = 0 to n - m // matching
O((n-m)m)
if p == ts
if P[1 .. m] == T[s + 1 .. s + m]
print “Pattern occurs with shift” s O(n-m)
if s < n - m
Ts+1 = (d(ts – T[s + 1]h ) + T[s + m + 1] )mod q
Topics to be covered
➢ The Naive string-matching algorithm
➢ The Rabin Karp algorithm
➢ The Knuth-Morris-Pratt algorithm
The Knuth-Morris-Pratt algorithm
➢ The Naive string matching algorithm doesn’t work well in cases where we see many matching
characters followed by a mismatching character. Following are some examples.
Text= "AAAAAAAAAAAAAAAAAB"
pattern = "AAAAB"
➢ The KMP matching algorithm uses degenerating property (pattern having same sub-patterns
appearing more than once in the pattern) of the pattern
➢ The basic idea behind KMP’s algorithm is: whenever we detect a mismatch (after some matches),
we already know some of the characters in the text of the next window.
➢ We take advantage of this information to avoid matching the characters that we know will anyway
match.
The Knuth-Morris-Pratt algorithm
The prefix-function π:
➢ π[i] is the largest integer smaller than i such that P1 . . . Pπ[i] is a suffix of P1 . . . Pi
Mismatch at T11!
KMP-MATCHER(T, P)
COMPUTE-PREFIX-FUNCTION(P)
n = T . length
m = P . length
m = P . length
Let π[1 .. m] be a new array
π = COMPUTE-PREFIX-FUNCTION(P)
π[1] = 0
q = 0 // number of characters matched
k=0
for i = 1 to n // scan the text from left to right
for q = 2 to m
while q > 0 and P[q + 1] ≠ T[i]
while k > 0 and P[k + 1] ≠ P[q]
q = π[q] // next character does not match
k = π[k]
if P[q + 1] == T[i]
if P[k + 1] == P[q]
q = q + 1 // next character matches
k=k+1
if q == m // is all of P matched?
π[q] = k
print “Pattern occurs with shift” i - m
return π
q = π[q] // look for the next match
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m
q = π[q] // look for the next match
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m
q = π[q] // look for the next match
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m
q = π[q] // look for the next match
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m
q = π[q] // look for the next match
n = 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m
q = π[q] // look for the next match
n = 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m
q = π[q] // look for the next match
n = 15 m=7
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m
q = π[q] // look for the next match
n = 15 m=7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
k=k+1
π[q] = k
return π
n = 15 m=7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
k=k+1
π[q] = k
return π
n = 15 m=7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
k=k+1
π[q] = k
return π
n = 15 m=7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k
π
return π
n = 15 m=7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k
π
return π
n = 15 m=7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 π
return π
n = 15 m=7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 π
return π
n = 15 m=7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 π
return π
n = 15 m=7 k=0
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 π
return π
n = 15 m=7 k=0
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 π
return π
n = 15 m=7 k=0 q = 2 to m
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 π
return π
n = 15 m=7 k=0 q = 2 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 π
return π
n = 15 m=7 k=0 q = 2 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 π
return π
n = 15 m=7 k=0 q = 2 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 π
return π
n = 15 m=7 k=0 q = 2 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 π
return π
n = 15 m=7 k=0 q = 2 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 π
return π
n = 15 m=7 k=0 q = 2 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 π
return π
n = 15 m=7 k=0 q = 2 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 π
return π
n = 15 m=7 k=0 q = 2 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 π
return π
n = 15 m=7 k=0 q = 3 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 π
return π
n = 15 m=7 k=0 q = 3 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 π
return π
n = 15 m=7 k=0 q = 3 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 π
return π
n = 15 m=7 k=0 q = 3 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 π
return π
n = 15 m=7 k=0 q = 3 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 π
return π
n = 15 m=7 k=0 q = 3 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 π
return π
n = 15 m=7 k=1 q = 3 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 π
return π
n = 15 m=7 k=1 q = 3 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 π
return π
n = 15 m=7 k=1 q = 3 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 π
return π
n = 15 m=7 k=1 q = 3 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 π
return π
n = 15 m=7 k=1 q = 4 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 π
return π
n = 15 m=7 k=1 q = 4 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 π
return π
n = 15 m=7 k=1 q = 4 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 π
return π
n = 15 m=7 k=1 q = 4 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 π
return π
n = 15 m=7 k=1 q = 4 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 π
return π
n = 15 m=7 k=1 q = 4 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 π
return π
n = 15 m=7 k=2 q = 4 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 π
return π
n = 15 m=7 k=2 q = 4 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 π
return π
n = 15 m=7 k=2 q = 4 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 π
return π
n = 15 m=7 k=2 q = 4 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 π
return π
n = 15 m=7 k=2 q = 5 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 π
return π
n = 15 m=7 k=2 q = 5 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 π
return π
n = 15 m=7 k=2 q = 5 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 π
return π
n = 15 m=7 k=2 q = 5 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 π
return π
n = 15 m=7 k=2 q = 5 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 π
return π
n = 15 m=7 k=2 q = 5 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 π
return π
n = 15 m=7 k=3 q = 5 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 π
return π
n = 15 m=7 k=3 q = 5 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=3 q = 5 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=3 q = 5 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=3 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=3 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=3 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=3 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=3 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=1 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=1 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=1 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=1 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=1 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=0 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=0 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=0 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=0 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=0 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 π
return π
n = 15 m=7 k=0 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 π
return π
n = 15 m=7 k=0 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 π
return π
n = 15 m=7 k=0 q = 6 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 π
return π
n = 15 m=7 k=0 q = 7 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 π
return π
n = 15 m=7 k=0 q = 7 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 π
return π
n = 15 m=7 k=0 q = 7 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 π
return π
n = 15 m=7 k=0 q = 7 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 π
return π
n = 15 m=7 k=0 q = 7 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 π
return π
n = 15 m=7 k=0 q = 7 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 π
return π
n = 15 m=7 k=1 q = 7 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 π
return π
n = 15 m=7 k=1 q = 7 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 1 π
return π
n = 15 m=7 k=1 q = 7 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 1 π
return π
n = 15 m=7 k=1 q = 7 to 7
The Knuth-Morris-Pratt algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
COMPUTE-PREFIX-FUNCTION(P)
m = P . length b a c b a b a b a a b c b a b T
Let π[1 .. m] be a new array
π[1] = 0
k=0 1 2 3 4 5 6 7
for q = 2 to m a b a b a c a P
while k > 0 and P[k + 1] ≠ P[q]
k = π[k]
if P[k + 1] == P[q]
1 2 3 4 5 6 7
k=k+1
π[q] = k 0 0 1 2 3 0 1 π
return π
n = 15 m=7 k=1 q = 7 to 7
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 1 to n
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 1 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 1 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 1 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 1 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 1 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 1 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 1 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 1 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
a b a b a c a P
while q > 0 and P[q + 1] ≠ T[i]
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 2 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
a b a b a c a P
while q > 0 and P[q + 1] ≠ T[i]
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 2 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
a b a b a c a P
while q > 0 and P[q + 1] ≠ T[i]
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 2 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
a b a b a c a P
while q > 0 and P[q + 1] ≠ T[i]
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 2 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
a b a b a c a P
while q > 0 and P[q + 1] ≠ T[i]
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 2 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
a b a b a c a P
while q > 0 and P[q + 1] ≠ T[i]
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 2 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched 1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
a b a b a c a P
while q > 0 and P[q + 1] ≠ T[i]
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 2 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 2 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 2 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 2 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 2 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 3 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 3 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 3 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 3 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 3 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 3 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 3 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 3 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 3 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 3 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 3 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 4 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 4 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
for i = 1 to n // scan the text from left to right 1 2 3 4 5 6 7
n = 15 m=7 q=0 i = 4 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 4 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 4 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 4 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 4 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 4 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 4 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 4 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 4 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 5 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 5 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 5 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 5 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 5 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 5 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 5 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 5 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 5 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 5 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 6 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 6 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 6 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 6 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 6 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 6 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 6 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 6 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 6 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 6 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 7 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 7 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 7 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 7 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 7 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 7 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 7 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 7 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 7 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 7 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 8 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 8 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 8 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 8 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 8 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 8 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 8 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 8 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 8 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 8 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 9 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 9 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 9 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 9 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 9 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 9 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=5 i = 9 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=5 i = 9 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=5 i = 9 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=5 i = 9 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=5 i = 10 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=5 i = 10 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=5 i = 10 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=5 i = 10 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=5 i = 10 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 10 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 10 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 10 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 10 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 10 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 10 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 11 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 11 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 11 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 11 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 11 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=3 i = 11 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 11 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 11 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 11 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 11 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=4 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 12 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 13 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 13 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 13 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 13 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 13 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 13 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 13 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 13 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 13 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 13 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 13 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 14 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 14 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 14 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 14 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 14 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=0 i = 14 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 14 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 14 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 14 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 14 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 14 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=1 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = T . length
b a c b a b a b a a b c b a b T
m = P . length
π = COMPUTE-PREFIX-FUNCTION(P)
q = 0 // number of characters matched
1 2 3 4 5 6 7
for i = 1 to n // scan the text from left to right
while q > 0 and P[q + 1] ≠ T[i] a b a b a c a P
q = π[q] // next character does not match
if P[q + 1] == T[i]
q = q + 1 // next character matches 1 2 3 4 5 6 7
if q == m // is all of P matched?
print “Pattern occurs with shift” i - m 0 0 1 2 3 0 1 π
q = π[q] // look for the next match
n = 15 m=7 q=2 i = 15 to 15
The Knuth-Morris-Pratt algorithm
KMP-MATCHER(T, P)
COMPUTE-PREFIX-FUNCTION(P)
n = T . length
m = P . length
m = P . length
Let π[1 .. m] be a new array
π = COMPUTE-PREFIX-FUNCTION(P)
π[1] = 0
q = 0 // number of characters matched
k=0
for i = 1 to n // scan the text from left to right
for q = 2 to m
while q > 0 and P[q + 1] ≠ T[i]
while k > 0 and P[k + 1] ≠ P[q]
q = π[q] // next character does not match
k = π[k]
if P[q + 1] == T[i]
if P[k + 1] == P[q]
q = q + 1 // next character matches
k=k+1
if q == m // is all of P matched?
π[q] = k
print “Pattern occurs with shift” i - m
return π
q = π[q] // look for the next match