f(k) = MaxOverlap ( "p_{0} p_{1} ... p_{k}" ) where: "p_{0} p_{1} ... p_{k}" = the prefix of length k+1 of pattern P 
Graphically:
Given P = "p_{0} p_{1} ... p_{m1}" Given k = 1, 2, ..., m1 (k = 0 ==> f(0) = 0) 1. Extract the subpattern: "p_{0} p_{1} ... p_{k}" 2. Find the first (= largest) overlap: Try: (p_{0}) p_{1} p_{2} ... p_{k1} p_{0} p_{1} ... p_{k1} p_{k} If (no match) Try: (p_{0}) p_{1} p_{2} ... p_{k1} p_{0} p_{1} ... p_{k1} p_{k} And so on... The first overlap is the longest ! 

In other words: the longest overlapping suffix and prefix in "p_{0} p_{1} ... p_{k1}" has x characters:
f(k1) = x characters <> p_{1} p_{2} p_{3} ... p_{kx2} p_{kx3} p_{kx4} .... p_{k1} ^ ^ ^ ^    equal  v v v v p_{0} p_{1} p_{2} .... p_{x1} p_{x} ... p_{k1} 
$64,000 question:

Yes, because f(k) is computed using a similar prefix as f(k−1):
prefix used to compute f(k1) ++   p_{0} p_{1} p_{2} .... p_{x1} ... p_{k1} p_{k}   ++ prefix used to compute f(k) 
We will next learn how to exploit the similarity to compute f(k)

Proof: by contradiction


Proof:

k = 0123456 Pattern = aaabaaa 
k = 0123456 Pattern = aaabaaa 
(This trick is a bit tricky :))

Worked out further:



public static int[] KMP_failure_function( P ) { int k, i, x, m; int f[] = new int[P.length()]; // f[] stores the function values m = P.length(); f[0] = 0; // f[0] is always 0... for ( k = 1; k < m; k++ ) { // Compute f(k) and store in f[k] i = k1; // Try use f(k1) to compute f(k) x = f[i]; // Character position to match agains P[k] if ( P[k] == P[x] ) // Note: make sure x is valid { f[k] = f[i] + 1; continue; // Compute next f(k) value } else { i = x1; // Try next prefix (and next f(i)) to compute f(k) x = f[i]; // Character position to match agains P[k] } if ( P[k] == P[x] ) // Note: make sure x is valid { f[k] = f[i] + 1; continue; // Compute next f(k) value } else { i = x1; // Try next prefix (and next f(i)) to compute f(k) x = f[i]; // Character position to match agains P[k] } .... (obviously we will make this into a loop !!!) } } 
public static int[] KMP_failure_function(String P) { int k, i, x, m; int f[] = new int[P.length()]; m = P.length(); f[0] = 0; // f(0) is always 0 for ( k = 1; k < m; k++ ) { // Compute f[k] i = k1; // First try to use f(k1) to compute f(k) x = f[i]; while ( P.charAt(x) != P.charAt(k) ) { i = x1; // Try the next candidate f(.) to compute f(k) if ( i < 0 ) // Make sure x is valid break; // STOP the search !!! x = f[i]; } if ( i < 0 ) f[k] = 0; // No overlap at all: max overlap = 0 characters else f[k] = f[i] + 1; // We can compute f(k) using f(i) } return(f); } 
How to run the program:

Example:
>>> java ComputeF P = ababyababa  Prefix = ab  Computing f(1): =================================== Try using: f(0) = 0 ===================================== Matching: i = 1, j = 0 01 ab ab 01 ^  No overlap possible... > f[1] = 0  .......  Prefix = ababyababa  Computing f(9): =================================== Try using: f(8) = 4 ===================================== Matching: i = 9, j = 4 0123456789 ababyababa ababyababa 0123456789 ^  =================================== Try using: f(3) = 2 ===================================== Matching: i = 9, j = 2 0123456789 ababyababa ababyababa 0123456789 ^  Overlap found ... > f[9] = 3 