Download presentation
Presentation is loading. Please wait.
1
KMP-Prefix Table
2
Algorithm: To compute Prefix Table
Algorithm prefix_table(p) { m = length(p) p[1] = 0, k=0 for q = 2 to m do while((k>0) and (p[k+1) != p[q])) { k = p[k] } //only statement if(p[k+1] = p[q]) k=k+1 //only statement p[q] = k } Length between 1 to m
3
Algorithm: To compute Prefix Table
For q = 2 to m do //q=2 and k=0 [m=8] Whie((k>0) and p[k+1]!=p[q]) False P[k+1]=p[1]=a and p[q]=e k = p[k] Not executed If (p[k+1]=p[q]) K=k+1 p[q] = k P[q]=0 P[2]=0 q=3, k=0 q=4, k=1 While Not Executed While [k=p[1]=0] Executed IF (p[1]=p[3]) True1 If(p[2]=p[4]) [char “e” and “b”) [False] K=0+1 =1 K=1 P[4] = k P[4]=0 P[3]=1 q=5, k=0 q=6, k=1 Executed //K=0 If IF P[5]=1 P[6]=1 Sample example:1 t 1 2 3 4 5 6 7 8 a e b P m = Length[pattern] = 8 [1-8] P[1] = 0 // initial condition K = 0 //pointer to traverse the pattern q=7, k=1 While Executed //K=0 If Executed K=1 P[7]=1 q=8, k=1 While Executed//K=0 If Not executed K=0 P[8]=0
4
Algorithm: To compute Prefix Table
For q = 2 to m do //q=2 and k=0 [m=8] Whie((k>0) and p[k+1]!=p[q]) False P[k+1]=p[1]=a and p[q]=e k = p[k] Not executed If (p[k+1]=p[q]) K=k+1 p[q] = k P[q]=0 P[2]=0 q=3, k=0 q=4, k=1 While Not Executed IF (p[1]=p[3]) True1 If(p[2]=p[4]) [char “b” and “b”) [True] K=k+1 = 2 K=0+1 =1 K=1 P[4] = k P[4]=2 P[3]=1 q=5, k=2 q=6, k=3 Executed Goback step P[4]!=p[6] If Executed p[2]!=p[6] //k=0 K=3 P[5]=3 Not executed //p[6]=0 Sample example:2 t 1 2 3 4 5 6 7 8 a b c P m = Length[pattern] = 8 [1-8] P[1] = 0 // initial condition K = 0 //pointer to traverse the pattern q=7, k=0 While Not executed If Executed K=1 P[7]=1 q=8, k=1 While Not executed If Executed K=2 P[8]=2
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.