24 COMP 6651 – B. Jaumard – Fall 2013 The failure function F (3/3) Input: String P(pattern) with m characters Output: The failure function Ffor P, which maps jto the length of the longest prefix of P that is a suffix of P[1..j] F ←0 ; i ←1 ; j ←0 While i < mdo If P[i] = P[j] then <* we have matched j+1characters *> F[i] ←j+1 ; i ←i+1 ; j ←j+1 Else Ifj > 0 then <* jindexes just after a prefix of Pthat must match *> j ←F[j-1] Else <* no match *>F[i] ←0 ; i ←i+1 Endif Endif Endwhile
25 COMP 6651 – B. Jaumard – Fall 2013 Example T:P:a a b c a b a a c a c b a a c b a b a a b b a a a a b c a b 1 3 2 4 5 6 7 a a b c a b a a b c a b a a b c a b i 0 1 2 3 4 5 F 0 0 1 0 1 2 14 16 15 17 18 19 13 8 10 9 11 12 a a b c a b
26 COMP 6651 – B. Jaumard – Fall 2013
27 COMP 6651 – B. Jaumard – Fall 2013 Complexity •A character of Tmay be compared against many characters of P. •If there is a mismatch, then the same character of Tis compared against the character ofPpointed by the next table. •If there is another mismatch, then we continue comparing against the same character ofTuntil there is either a match or we reach the beginning ofP. •How many times can we backtrack for one character of T? •Let us assume that the first mismatch involved pk. •Since each backtrack leads us to a smaller index in P, we can backtrack onlyk times. •However, to reachpkwe must have gone forwardktimes without any backtracking! •If we assign the costs of backtracking to the forward moves, then we at most double the cost of the forward moves. •But there are exactlynforward moves, so the number of comparisons is O(n).