Algorithms · Lesson 7
KMP and the Prefix Function
The string matching problem
Given a pattern and a text , decide whether is a factor of , or find every occurrence.
The naive algorithm tries every starting position and compares against that factor, taking time.
Borders let us skip positions
Suppose we matched the first characters of against the text and then hit a mismatch. We already know the text looks like . A match can only start where a border of that matched prefix aligns:
Searching for 0011001 in the text. Occurrences found so far:
none.
In the lecture example, after matching 001100 and seeing a mismatch, the border 00 of 001100 tells us the next useful text position: shift past 01 and 11 at the start, resuming with the prefix 00 already matched.
The prefix function
The shift is always determined by the longest border of the already-matched prefix. Define
with .
For each prefix, the table gives f(j) — the length of its longest border.
| j | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| f(j) | 0 | 1 | 0 | 0 | 1 | 2 | 3 |
The prefix w[1..1] is unbordered — it has no nonempty border.
The recurrence
Since each position adds amortized work, the whole table costs — and the matching pass that uses it is , so KMP runs in linear time.
Detecting unbordered words in linear time
The naive unbordered test compares every nonempty proper prefix against the same-length suffix, taking time.
But notice: a word is unbordered exactly when its longest border is empty — that is, exactly when . Since the prefix function is computed in , we can test whether a word is unbordered in linear time.
All borders in linear time
The prefix function also lists every border of in time. Start at the longest border and walk the failure chain:
Every entry is a border length, because the longest border of a border is again a border of the original word; and every border appears on this chain, since a border of length is the longest border of the -th prefix of some chain entry. The chain strictly decreases and has at most entries, so all border lengths are enumerated in linear time.
The same chain counts, for every prefix , how many nonempty borders it has: its borders are its longest border together with the borders of that longest border, so
Each value is filled in one pass over the table, so all counts cost time in total.
Practice
Prefix function values
What is the longest border length f(j) of the prefix 01100[1..5] of 01100?
Count the borders of a prefix
How many nonempty borders does the prefix 110[1..3] of 110 have?
How many nonempty borders does the prefix 0110101010[1..1] of 0110101010 have?
Find the occurrences
Find all starting positions of 00 in 10000.
Find all starting positions of 1100 in 11010000000011.
Practice this topic →Every quiz from this lesson, at four difficulties.
References
- Knuth–Morris–Pratt algorithm — Wikipedia
- Substring — Wikipedia
- Prefix function (Knuth–Morris–Pratt) — Algorithms for Competitive Programming
- Gusfield, Algorithms on Strings, Trees and Sequences, Cambridge University Press, 1997