Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KMP failure function calculation

My professor solved the kmp failure function as follows:

index  1 2 3 4 5 6 7 8 9
string a a b a a b a b b
ff     0 1 2 1 2 3 4 5 1

From other texts I checked online, I found out it might be wrong, I went back to confirm from him again and he told me he's absolutely right. Can someone pls explain to me why he thinks it's right or wrong in a simple step by step manner? Thanks

like image 957
Dennis Avatar asked Apr 20 '13 21:04

Dennis


People also ask

How do you calculate failure in KMP?

Failure function This function is based on the fact that: When a mismatch occurs, all the previous characters match correctly; ​this implies that if a prefix of W occurred in this set of matching characters, then that prefix is also a suffix of W .

What is failure function?

Functional failure is the inability of a system to meet a specified performance standard. A complete loss of function is clearly functional failure. However, a functional failure also includes the inability to function at the level of performance that has been specified as satisfactory.

What is prefix function in KMP algorithm?

The Prefix Function (Π): The Prefix Function, Π for a pattern encapsulates knowledge about how the pattern matches against the shift of itself. This information can be used to avoid a useless shift of the pattern 'p. ' In other words, this enables avoiding backtracking of the string 'S. '


2 Answers

As I understand the algorithm, the failure function for your example should be the following:

1 2 3 4 5 6 7 8 9
a a b a a b a b b

0 1 0 1 2 3 4 0 0

f - failure function (by definition, this is the length of the longest prefix of the string which is a suffix also)

Here how I built it step by step:

f(a) = 0 (always = 0 for one letter)

f(aa) = 1 (one letter 'a' is both a prefix and suffix)

f(aab) = 0 (there is no the same suffixes and prefixes: a != b, aa != ab)

f(aaba) = 1 ('a' is the same in the beginning and the end, but if you take 2 letters, they won't be equal: aa != ba)

f(aabaa) = 2 ( you can take 'aa' but no more: aab != baa)

f(aabaab) = 3 ( you can take 'aab')

f(aabaaba) = 4 ( you can take 'aaba')

f(aabaabab) = 0 ( 'a' != 'b', 'aa' != 'ab' and so on, it can't be = 5, so as 'aabaa' != 'aabab')

f(aabaababb) = 0 ( the same situation)

like image 101
user2513978 Avatar answered Oct 31 '22 12:10

user2513978


Since @user1041889 was confused (and got me confused too) I'll lay here the differences between the Z-function and the failure function.

Failure function, π[i]:

Is the mapping of and index to the length of the longest prefix of the string which is also a suffix

But that's arguably Chinese so I'll dumb it down in order to actually understand what I'm saying:

How big is the longest sub-string at the beginning of the string of interest, that is equal to the sub-string ending at index i

Or equivalently:

What is the length of the biggest sub-string ending at index i which matches the start of the string of interest

So in your example:

index  1 2 3 4 5 6 7 8 9
string a a b a a b a b b
ff     0 1 0 1 2 3 4 0 0

We observe that π[6] = 3, so what's the substring that ends at index 6 with length 3? aab!

Interesting how we've seen that before!

Let's check that it is indeed the biggest one: baab != aab. Yup!

Notice how this implies that the failure functions always grows uniformly.

That isn't the case for the Z-algorithm.

[SAVING DRAFT to continue later]

like image 37
DMeneses Avatar answered Oct 31 '22 11:10

DMeneses