Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Module129 KMP Prefix Function

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Module129 KMP Prefix Function

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Knuth-Morris-Pratt Algorithm

Components
• The Prefix
Function Π
• The KMP Matcher
Knuth-Morris-Pratt Algorithm
Compute-Prefix-Function (p)
1 m  length[p] q 1 2 3 4 5 6 7
2 Π[1]  0
p a t a t a c a
3 k0
4 for q  2 to m Π 0
5 do while k > 0 and p[k+1] != p[q]
6 do k  Π[k]
7 If p[k+1] = p[q] Initially:
8 then k  k +1 m = length[p] = 7
9 Π[q]  k Π[1] = 0
10 return Π k=0
Knuth-Morris-Pratt Algorithm
Compute-Prefix-Function (p)
1 m  length[p] q 1 2 3 4 5 6 7
2 Π[1]  0
p a t a t a c a
3 k0
4 for q  2 to m Π 0 0
5 do while k > 0 and p[k+1] != p[q]
6 do k  Π[k]
7 If p[k+1] = p[q] Step 1: q = 2, k=0
8 then k  k +1 p[0+1] ≠ p[q2]
9 Π[q]  k
10 return Π Π[2] = 0
Knuth-Morris-Pratt Algorithm
Compute-Prefix-Function (p)
1 m  length[p] q 1 2 3 4 5 6 7
2 Π[1]  0
p a t a t a c a
3 k0
4 for q  2 to m Π 0 0 1
5 do while k > 0 and p[k+1] != p[q]
6 do k  Π[k]
7 If p[k+1] = p[q] Step 2: q = 3, k = 0,
8 then k  k +1 p[0+1] = p[q3]
9 Π[q]  k K = 0+1 = 1
10 return Π Π[3] = 1
Knuth-Morris-Pratt Algorithm
Compute-Prefix-Function (p)
1 m  length[p] q 1 2 3 4 5 6 7
2 Π[1]  0
p a t a t a c a
3 k0
4 for q  2 to m Π 0 0 1 2
5 do while k > 0 and p[k+1] != p[q]
6 do k  Π[k]
7 If p[k+1] = p[q] Step 3: q = 4, k = 1
8 then k  k +1 p[1+1] = p[q4]
9 Π[q]  k K = 1+1 = 2
10 return Π Π[4] = 2
Knuth-Morris-Pratt Algorithm
Compute-Prefix-Function (p)
1 m  length[p] q 1 2 3 4 5 6 7
2 Π[1]  0
p a t a t a c a
3 k0
4 for q  2 to m Π 0 0 1 2 3
5 do while k > 0 and p[k+1] != p[q]
6 do k  Π[k]
Step 4: q = 5, k =2
7 If p[k+1] = p[q]
8 then k  k +1 p[2+1] = p[q 5]
K = 2+1 = 3
9 Π[q]  k
10 return Π Π[5] = 3
Knuth-Morris-Pratt Algorithm
Compute-Prefix-Function (p)
1 m  length[p] q 1 2 3 4 5 6 7
2 Π[1]  0
p a t a t a c a
3 k0
4 for q  2 to m Π 0 0 1 2 3 1
5 do while k > 0 and p[k+1] != p[q]
6 do k  Π[k]
Step 5: q = 6, k = 3
7 If p[k+1] = p[q]
8 then k  k +1 p[3+1] ≠ p[q 6]
9 Π[q]  k K = Π[3] = 1
10 return Π Π[6] = 1
Knuth-Morris-Pratt Algorithm
Compute-Prefix-Function (p)
1 m  length[p] q 1 2 3 4 5 6 7
2 Π[1]  0
p a t a t a c a
3 k0
4 for q  2 to m Π 0 0 1 2 3 1 0
5 do while k > 0 and p[k+1] != p[q]
6 do k  Π[k]
Step 6: q = 7, k = 1
7 If p[k+1] = p[q]
8 then k  k +1 p[1+1] ≠ p[q 7]
9 Π[q]  k K = Π[1] = 0
10 return Π Π[7] = 0
Knuth-Morris-Pratt Algorithm
Conclusions
• The Prefix
Function Π
• The KMP Matcher

You might also like