Algorithms · String Matching with a Finite Automaton and Boyer-Moore
The problem, stated precisely
We are given a text and a pattern , both over a finite alphabet of size . We want every shift with such that
Each such is a valid shift; the others are invalid. Throughout, indices are 1-based (the CLRS convention). A question may label an alignment by its offset or by the 1-based start position , so read the options carefully; when it asks "how many comparisons", only the count matters.
The naive algorithm tries every shift and compares up to characters: worst case, which is when . Everything below is an attempt to beat that.
2.1 The core idea
Scan the text left to right exactly once, and after reading keep only one number: the length of the longest prefix of that is a suffix of what we have read so far. That number is the state. When the state reaches , we have found a match ending at position .
2.2 Definitions
A finite automaton for pattern is a 5-tuple :
| Symbol | Meaning |
|---|---|
| states; state means "the last characters read equal " | |
| start state | |
| only accepting state | |
| the alphabet | |
| transition function |
The transition function is defined by the suffix function:
where is the length of the longest prefix of that is a suffix of the string . Equivalently, for state and character :
Note the subtlety: is not simply . It is only when ; otherwise the automaton must "fall back" to the longest prefix that still matches, which is exactly what computes.
2.3 Preprocessing: building the table in
The straightforward construction (CLRS, procedure COMPUTE-TRANSITION-FUNCTION) fills a table of size :
for q = 0 to m:
for each a in Sigma:
k = min(m, q+1)
while k > 0 and P[1..k] is not a suffix of P[1..q]a:
k = k - 1 # try a shorter prefix
delta[q][a] = k
There are cells, the while loop runs up to times per cell, and each suffix test costs , so this version takes time (the bound CLRS states). The table can be built in by reusing the KMP prefix function (longest proper border of ): for with set ; otherwise set for (and when ). Quote for the optimised construction, for the plain CLRS procedure, and note that the table occupies space.
2.4 Matching:
q = 0
for i = 1 to n:
q = delta[q][T[i]]
if q == m:
print "match ends at i", shift = i - m
Each text character causes exactly one table lookup, so matching is — independent of . Total: .
2.5 Worked example: `ababa`,
Here , so states are . Let us compute every cell by hand. Write .
Fill every cell from the definition (never assume ). The classic trap is : the string read is abb, and no non-empty prefix of ababa is a suffix of abb, so the value is .
Row : string is just or .
- : suffixes
a,ε;a``§M11§✓ → 1. - :
bnot a prefix → 0.
Row (prefix read a):
- → string
aa: suffixesaa,a,ε.aanot a prefix;a``§M15§✓ → 1. - → string
ab:ab``§M17§✓ → 2.
Row (read ab):
- →
aba:aba``§M20§✓ → 3. - →
abb:abb✗,bb✗,b✗,ε✓ → 0.
Row (read aba):
- →
abaa:abaa✗,baa✗,aa✗,a``§M24§✓ → 1. - →
abab:abab``§M26§✓ → 4.
Row (read abab):
- →
ababa``§M29§✓ → 5. - →
ababb:ababb✗,babb✗,abb✗,bb✗,b✗,ε✓ → 0.
Row (read ababa, full match). The automaton must still be able to continue. .
- →
ababaa: check :ababavs suffixbabaa? no. :ababvsabaa? no. :abavsbaa? no. :abvsaa? no. :avsa✓ → 1. - →
ababab: :ababavsbabab? no. :ababvsabab✓ → 4.
Transition table for ababa:
| \ | a | b |
|---|---|---|
| 0 | 1 | 0 |
| 1 | 1 | 2 |
| 2 | 3 | 0 |
| 3 | 1 | 4 |
| 4 | 5 | 0 |
| 5 | 1 | 4 |
Sanity check on the diagonal: for : ✓, ✓, ✓, ✓, ✓. Good.
2.6 Running the automaton on a text
Take aabababaa, . Track state after each character:
| old | new | match? | |||
|---|---|---|---|---|---|
| 1 | a | 0 | 1 | 1 | |
| 2 | a | 1 | 1 | 1 | |
| 3 | b | 1 | 2 | 2 | |
| 4 | a | 2 | 3 | 3 | |
| 5 | b | 3 | 4 | 4 | |
| 6 | a | 4 | 5 | 5 | yes, ends at 6, shift |
| 7 | b | 5 | 4 | 4 | |
| 8 | a | 4 | 5 | 5 | yes, ends at 8, shift |
| 9 | a | 5 | 1 | 1 |
Verify by hand: ababa ✓ (shift 1: the match starts at position 2). ababa ✓ (shift 3). No other occurrence. The automaton found both with 9 lookups, no backtracking.