Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Knuth-Morris-Pratt
Brute Force
S:
W:
Brute Force
S:
W:
Brute Force
S:
W:
Brute Force
S:
W:
Brute Force
S:
W:
Brute Force
S:
W:
Brute Force
S:
W:
W:
W:
Better Algorithms
Backward Algorithm
Raita Algorithm
Colussi Algorithm
Simon Algorithm
Horsepool Algorithm
Smith Algorithm
Uniqueness Algorithm
KMP
Linear Time
Avoids comparisons with elements of S that
have already been involved in a comparison,
i.e. backtracking in S never occurs
Time: O(m+n)
Space: O(m+n)
KMP
Differs from brute force by always keeping
track of the information that it gains from
previous comparisons
A failure function or partial matching table
(T) is computed which tells us how much of
the last comparison can be reused if it fails
T[i]=the longest prefix of W that is also a
proper suffix of W[0..i]
KMP
T shows how much of the beginning of W
matches up to the portion of S immediately
preceding the failed comparison.
.
Resume
comparing here
KMP
T shows how much of the beginning of W
matches up to the portion of S immediately
preceding the failed comparison.
.
Resume
comparing here
KMP
T shows how much of the beginning of W
matches up to the portion of S immediately
preceding the failed comparison.
.
Resume
comparing here
KMP example
m
S
W
i
KMP example
m
S
W
i
KMP example
m
S
W
i
KMP example
m
S
W
i
KMP example
m
S
W
i
KMP example
m
S
W
i
KMP example
m
S
W
i
KMP example
m
S
W
i
KMP
Calculating the longest valid suffix during
runtime will be very inefficient
Pre-processing can eliminate the problem,
as the suffix also exists in W itself
KMP
The algorithm preprocesses the word W to
produce the prefix function, which gives the
number of steps the pattern can skip for every
possible location of a mismatch
Components of KMP
Compute Prefix Function: For a given W,
compute a table T of equal length where T[i]
gives the length of the longest prefix of W
that is also a proper suffix of W[0..i].
KMP Matcher Function: Actual searching.
A
0
Example
S
W
T
Example
S
W
T
Example
S
W
T
Example
S
W
T
Example
S
W
T
Example
S
W
T
Example
S
W
T
Example
S
W
T
Example
S
W
T
Matcher Function
KMP(String S, String W):
set T to prefixFunc(W)
set q to 0
if q is equal to m
print i-m+1
set q to T[q-1]
//Find others
Prefix Function
prefixFunc(List W):
set T[0] to 0
set k to 0
Runtime Analysis
Although the algorithm as implemented here contains a
loop within a loop, it runs in linear time. This is because the
backtracking statement, which essentially shifts the sliding
window to the right, can only execute a maximum of n
times in the entire run of the for loop. The remaining body
of the for loop runs executes exactly n times itself, giving
a runtime of O(n) for the matching function.
Similar reasoning applies to the prefix function.