Advanced Topics · Lesson 9
Factor Avoidance
Avoidance
Fix a nonempty pattern . A word avoids if it has no occurrence of as a factor. Let be the number of length- words over that avoid .
Testing one word is easy — run KMP. Counting all avoiding words is different: we should not list the words individually.
Deterministic finite automata
A DFA is a 5-tuple :
- a finite nonempty set of states ,
- a finite input alphabet ,
- a transition function ,
- a start state ,
- a set of accept (final) states .
A word is accepted if, starting in , the path labeled by its symbols ends in an accept state. A sink state rejects every path that touches it.
The avoidance automaton
To recognize words that avoid , let the states remember how close the word read so far is to ending in an occurrence of . As long as has not occurred, the only relevant information is the longest suffix of the word read that is a prefix of . So the useful states are
with as the start state.
On reading a symbol , append it and find the longest suffix of the result that is a prefix of :
- if reading completes , move to the rejecting sink ;
- otherwise move to the state for that longest proper prefix.
Every non-sink state is accepting, because reaching it means no occurrence of has appeared yet. And this is exactly the information KMP uses: when the longest candidate cannot be extended, the failure function tells us which shorter prefixes remain possible.
Avoiding 010 over 2-letter alphabet {
0, 1
}. The states record the longest suffix of the word read so far that is a prefix of 010.
Start in state 0 (ε) — no letters read yet.
A longer example
The pattern 01001 shows why a mismatch need not send us all the way back to . From the state recording 010, reading 1 gives the word 0101, whose longest suffix that is a prefix of 01001 is 01 — not .
Avoiding 01001 over 2-letter alphabet {
0, 1
}. The states record the longest suffix of the word read so far that is a prefix of 01001.
Start in state 0 (ε) — no letters read yet.
As a check, 010001 follows the states and is accepted, while 01001 reaches on its final letter.
Counting paths instead of listing words
For each accepting state , let count the avoiding words of length whose final state is . Then
and each word has a unique last symbol and a unique path, so
Different letters leading from the same state to the same next state count separately — they produce different words.
For 010, this gives the recurrence
so goes 1, 2, 4, 7, 12, 21, 37, … At length three only 010 itself is excluded; for longer words the state counts retain exactly the suffix information needed.
A recurrence for the total alone
The state recurrences can be combined into a single recurrence involving only . The technique is to push forward through the recurrences and collect coefficients on , then check which combination cancels.
For 010, expanding the sum through the recurrences three times gives
so the totals alone obey the tribonacci recurrence — check: 7 = 4 + 2 + 1, 12 = 7 + 4 + 1, 21 = 12 + 7 + 2, 37 = 21 + 12 + 4.
For the pattern 001 the cancellation leaves a nonzero remainder. Its state recurrences are
Here is invariant (its own recurrence is the difference of the first and third lines above), and , so
so , giving 1, 2, 4, 7, 12, 20, 33, … — each total is the sum of the previous two plus one.
Practice
Trace a transition
In the avoidance DFA for 10, which state does state 0
(recording ε)
reach on reading 1? Use the pattern length for the sink ⊥.
In the avoidance DFA for 0101, which state does state 3
(recording "010")
reach on reading 0? Use the pattern length for the sink ⊥.
Count the avoiding words
How many length-3 words over a 2-letter alphabet avoid 001?
How many length-8 words over a 3-letter alphabet avoid 010100?
Practice this topic →Every quiz from this lesson, at four difficulties.