Algorithms · Lesson 7

KMP and the Prefix Function

The string matching problem

Given a pattern P and a text T, decide whether P is a factor of T, or find every occurrence.

The naive algorithm tries every starting position and compares P against that factor, taking O(mn) time.

Borders let us skip positions

Suppose we matched the first j−1 characters of P against the text and then hit a mismatch. We already know the text looks like P[1..j−1]. A match can only start where a border of that matched prefix aligns:

Searching for 0011001 in the text. Occurrences found so far: none.

P:
T:

Matched 1 character at text position 1 (matched prefix: 0).

Step 1 / 14

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

fP(j)=the length of the longest border of P[1..j]

with fP(0)=0.

For each prefix, the table gives f(j) — the length of its longest border.

Longest border lengths of each prefix of 0011001
j1234567
f(j)0100123
Position j:

The prefix w[1..1] is unbordered — it has no nonempty border.

The recurrence

Since each position adds O(1) amortized work, the whole table costs O(m) — and the matching pass that uses it is O(n), 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 O(n2) time.

But notice: a word is unbordered exactly when its longest border is empty — that is, exactly when fw(|w|)=0. Since the prefix function is computed in O(n), we can test whether a word is unbordered in linear time.

All borders in linear time

The prefix function also lists every border of w in O(n) time. Start at the longest border and walk the failure chain:

fw(n), fw(fw(n)), fw(fw(fw(n))), …

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 j is the longest border of the j-th prefix of some chain entry. The chain strictly decreases and has at most n entries, so all border lengths are enumerated in linear time.

The same chain counts, for every prefix w[1..j], how many nonempty borders it has: its borders are its longest border together with the borders of that longest border, so

c(j)=1+c(fw(j))if fw(j)>00otherwise

Each value is filled in one pass over the table, so all n counts cost O(n) 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