本书暂未推出简体中文版。现有版本:English。
University Computer Science — Year 2 · 本科二年级
21Finite Automata
Is a typed string a decimal number, such as -12.5e3? A program can decide with one table and one loop:
// dfa_demo.rs: is the string a decimal number such as -12.5e3? One table and one loop.
const START: usize = 0;
const ACCEPT: [bool; 8] = [false, false, true, false, true, false, false, true];
// columns: digit, sign, point, e or E; rows: the 8 states; NO: no transition
const NO: usize = usize::MAX;
const TABLE: [[usize; 4]; 8] = [
[2, 1, NO, NO], // 0 start
[2, NO, NO, NO], // 1 after the sign
[2, NO, 3, 5], // 2 in the integer part (accepting)
[4, NO, NO, NO], // 3 after the point
[4, NO, NO, 5], // 4 in the fraction (accepting)
[7, 6, NO, NO], // 5 after the e
[7, NO, NO, NO], // 6 after the exponent's sign
[7, NO, NO, NO], // 7 in the exponent (accepting)
];
pub fn class(c: char) -> Option<usize> {
match c {
'0'..='9' => Some(0),
'+' | '-' => Some(1),
'.' => Some(2),
'e' | 'E' => Some(3),
_ => None,
}
}
pub fn is_number(s: &str) -> (bool, Vec<usize>) {
let (mut q, mut path) = (START, vec![START]);
for c in s.chars() {
match class(c).map(|k| TABLE[q][k]) {
Some(next) if next != NO => q = next,
_ => return (false, path), // no transition: rejected at once
}
path.push(q);
}
(ACCEPT[q], path)
}
$ rustc --edition 2021 -O -o dfa_demo dfa_demo.rs && ./dfa_demo
"-12.5e3" number states 0 1 2 2 3 4 5 7
"+0.25E-7" number states 0 1 2 3 4 4 5 6 7
"42" number states 0 2 2
"3." no states 0 2 3
".5" no states 0
"1e" no states 0 2 5
"12a" no states 0 2 2
"--1" no states 0 1
"" no states 0
The program remembers nothing about the string but one number from 0 to 7, its state, and it reads each character once. Such a device is a finite automaton. This chapter defines automata exactly, gives them a power they seem to lack, guessing, shows that guessing adds nothing but costs states, sometimes exponentially many, and builds automata for combinations of conditions. The next chapter connects them with the patterns of regular expressions, and finds what no finite automaton can do.
You already know
The Year 1 volume: a finite-state machine has a finite set of states, a start state, and a transition function from a state and an input to the next state; it may also produce outputs. Proofs by induction on the length of a word, or by structural induction. Sets, maps, and the set of subsets of a set, which has elements when the set has .
21.1 Words and languages
Definition 21.1 (Alphabet, word)
An alphabet is a finite nonempty set , whose elements are letters. A word over is a finite sequence of letters ; its length is . The empty word has length 0. The set of all words is , and there are words of length .
The concatenation of and is the word , followed by : it is associative, is neutral, and . In the hook, is the set of characters, and the program reads "-12.5e3" as a word of length 7.
Definition 21.2 (Language)
A language over is a set of words, finite or infinite. For languages and , the concatenation is ; the powers are and ; and the star of is , the words made by concatenating any number of words of , none included.
For example, is every word over , , and , written , is the words made of one and then any number of ’s.
Definition 21.3 (Prefix, suffix, factor, subword)
Let with words. Then is a prefix of , a suffix, and a factor: a block of consecutive letters. A subword of is a word obtained by deleting letters of , not necessarily consecutive.
The word has the prefixes ; is a factor; is a subword but not a factor. A word of length has prefixes and suffixes.
21.2 Deterministic automata
Definition 21.4 (Deterministic finite automaton)
A deterministic finite automaton (DFA) is : a finite set of states , an alphabet , a transition function , a partial map from to , a start state , and a set of accepting states. The extended transition function on words is defined by and , undefined as soon as one step is. accepts when is defined and lies in , and the language recognised by is , the set of words it accepts.
Proposition 21.5 (Reading in two parts)
For every state and words : , both sides being defined or undefined together.
Proof. By induction on . For both sides are . If , then , by the definition, the hypothesis for , and the definition again; each equality holds also when one side is undefined. ∎
This is what makes an automaton a streaming program: after reading , the state is all that is needed to continue, whatever was. The run_from method of automaton.rs, the library of this chapter’s listings, is written as a fold over the letters.
Definition 21.6 (Complete automaton)
A DFA is complete if is defined for every state and letter . Any DFA is made complete, with the same language, by adding a non-accepting sink state and sending every missing transition, and every letter from , to .
Definition 21.7 (Accessible, co-accessible, trim)
A state is accessible if for some word , and co-accessible if for some word . An automaton is trim if all its states are both.
Proposition 21.8 (Trimming)
Removing from a DFA every state that is not both accessible and co-accessible, with the transitions that touch it, leaves the recognised language unchanged. Both sets are computed by a graph search, in time linear in the number of transitions.
Proof. If is accepted, every state on its run is accessible (by a prefix of ) and co-accessible (by the rest of ), so the run survives the removal. Conversely, removing states and transitions can only remove runs, so no new word is accepted. Accessible states are those reachable from in the graph of the transitions; co-accessible states are those from which is reachable, that is, those reachable from in the reversed graph. ∎
Definition 21.9 (Recognisable language)
A language is recognisable if some finite automaton recognises it.
21.3 Nondeterministic automata
Consider the words over whose third letter from the end is . Reading from the left, a program cannot know which letter will be third from the end. It would be easy if it could guess: on each , guess “this is the one”, then check that exactly two letters follow.
Definition 21.10 (Nondeterministic finite automaton)
A nondeterministic finite automaton (NFA) is where is a set of transitions, so that a state may have several transitions, or none, on the same letter, and is a set of start states. A run on is a sequence with and ; accepts if some run on ends in .
A nondeterministic automaton is not a program that guesses well; it is a definition by existence of a run. It is also convenient to allow moves that read nothing.
Definition 21.11 (Spontaneous transition, closure)
An NFA with spontaneous transitions also has a set of pairs , written , which a run may take without reading a letter. The -closure of a set of states is the set of states reachable from by spontaneous transitions alone, included: the states reachable from in the graph of those transitions.
/// The states reachable from the set by spontaneous transitions: a graph search.
pub fn closure(&self, set: &Subset) -> Subset {
let mut out = set.clone();
let mut todo: Vec<usize> = set.iter().copied().collect();
while let Some(q) = todo.pop() {
for &r in &self.eps[q] {
if out.insert(r) {
todo.push(r); // new: its own spontaneous moves follow
}
}
}
out
}
/// One letter from a closed set of states, then the closure.
pub fn step(&self, set: &Subset, a: usize) -> Subset {
let next = set.iter().flat_map(|&q| self.delta[q][a].iter().copied()).collect();
self.closure(&next)
}
Proposition 21.12 (Removing spontaneous transitions)
Let be the closure of . The NFA with the same states and start states, the transitions for every in the closure of , no spontaneous transition, and the accepting states , recognises the same language.
Proof. A run of the original automaton on is a sequence of spontaneous moves, a letter, spontaneous moves, and so on. Cut it just before each letter: from state at the cut, it moves spontaneously within , reads , then moves spontaneously to the next cut , which is exactly a new transition ; after the last letter it moves spontaneously to , which makes accepting in the new automaton. Conversely, each new transition and each new accepting state unfolds into such moves of the original one. ∎
$ rustc --edition 2021 -O -o epsilon epsilon.rs && ./epsilon
closure of 0: {0, 1, 2}
closure of 1: {1, 2}
closure of 2: {2}
state 0: a -> [0, 1, 2], b -> [1, 2], c -> [2] accepting
state 1: a -> [], b -> [1, 2], c -> [2] accepting
state 2: a -> [], b -> [], c -> [2] accepting
1093 of 1093 words up to length 6 judged alike; 84 accepted
21.4 The subset construction
An NFA can be run by a program that keeps the set of states it might be in. That set takes finitely many values, which makes it the state of a DFA.
Method 21.13 (The subset construction)
Given an NFA , build a DFA whose states are sets of NFA states. The start state is . From a set and a letter , the transition goes to . A set is accepting if it contains a state of . Build only the sets reachable from the start, with a queue.
/// The subset construction, from the start set, keeping accessible subsets only.
pub fn determinise(&self) -> (Dfa, Vec<Subset>) {
let first = self.closure(&self.start.iter().copied().collect());
let mut index = HashMap::from([(first.clone(), 0)]);
let (mut subsets, mut delta) = (vec![first], Vec::new());
let mut todo = VecDeque::from([0]);
while let Some(i) = todo.pop_front() {
let mut row = Vec::new();
for a in 0..self.alphabet.len() {
let target = self.step(&subsets[i], a);
let j = *index.entry(target.clone()).or_insert_with(|| {
subsets.push(target);
todo.push_back(subsets.len() - 1);
subsets.len() - 1
});
row.push(Some(j));
}
delta.push(row);
}
let accept = subsets.iter().map(|s| s.iter().any(|&q| self.accept[q])).collect();
(Dfa { alphabet: self.alphabet.clone(), delta, start: 0, accept }, subsets)
}
Theorem 21.14 (The subset construction is correct)
For every word , the state of the DFA after reading is the set of NFA states at which some run on can end. Hence the DFA recognises the language of the NFA, and a language is recognisable by an NFA if and only if it is recognisable by a DFA.
Proof. By induction on . For , the runs on are the spontaneous moves from , which end exactly in , the start state. Suppose the claim holds for , with set . A run on is a run on , ending in some , then a transition , then spontaneous moves: it ends exactly in the closure of the ’s, which is the transition of the DFA from on . So the DFA’s state after contains a state of if and only if some run on ends in . Conversely a DFA is an NFA with one start state and at most one transition per state and letter. ∎
$ rustc --edition 2021 -O -o subset subset.rs && ./subset
DFA state subset of NFA states on a on b
0 {0} {0,1} {0}
1 {0,1} {0,1,2} {0,2}
2 {0,1,2} {0,1,2,3} {0,2,3}
3 {0,2} {0,1,3} {0,3}
4 {0,1,2,3} {0,1,2,3} {0,2,3} accepting
5 {0,2,3} {0,1,3} {0,3} accepting
6 {0,1,3} {0,1,2} {0,2} accepting
7 {0,3} {0,1} {0} accepting
abb NFA true DFA true
bab NFA false DFA false
baab NFA true DFA true
abba NFA false DFA false
The DFA remembers which of the last three letters were : the set means the last letter was and the two before it were . That needs states, and no DFA can do with fewer.
Theorem 21.15 (The blow-up is unavoidable)
Let be the words over whose -th letter from the end is . An NFA with states recognises , and every DFA that recognises has at least states; the subset construction produces exactly .
Proof. Let be two words of length , and the first position, counting from 0, where they differ; say has there and has . Append : in and , that position is now -th from the end, so and . In a DFA recognising , if then by Proposition 21.5 , and the two words would be both accepted or both rejected. So the words of length lead to different states. The NFA of the figure has states; the reachable sets of the subset construction are for the sets of positions among the last letters that hold an , all reached. ∎
$ rustc --edition 2021 -O -o blowup blowup.rs && ./blowup
k NFA states DFA states 2^k reached by the 2^k words pairs told apart
1 2 2 2 2 1
2 3 4 4 4 6
3 4 8 8 8 28
4 5 16 16 16 120
5 6 32 32 32 496
6 7 64 64 64 2016
7 8 128 128 128 8128
8 9 256 256 256 32640
9 10 512 512 512 130816
10 11 1024 1024 1024 523776
11 12 2048 2048 2048 2096128
12 13 4096 4096 4096 8386560
A set of words that must lead to pairwise different states, as here, is a fooling set; it gives a lower bound on the size of every DFA for the language (Exercise 21.10).
History
Finite automata come from the 1950s. Stephen Kleene’s paper of 1956, in the collection Automata Studies, started from models of nerve nets and characterised the “events” a finite automaton can recognise; that result is the next chapter’s main theorem. In 1959 Michael Rabin and Dana Scott defined nondeterministic automata and proved that the subset construction removes the nondeterminism, the theorem of this section.
21.5 Combining automata
Definition 21.16 (Product automaton)
The product of two complete DFAs and has the states , the start and the transitions : it runs both automata at once.
Proposition 21.17 (Closure under the Boolean operations)
If and are recognisable, so are , and .
Proof. Take complete DFAs for and . Swapping accepting and other states of the first gives the complement: the run on every word is defined and ends in exactly one state, accepting in one automaton if and only if not in the other. By induction on , the product’s state after is the pair of the two states after ; so with accepting states it recognises , and with it recognises . ∎
Completeness matters for the complement: an incomplete automaton rejects some words by having no run at all, and swapping its accepting states leaves them rejected (Exercise 21.2). The product of “even length” and “starts with ” has six states, and not all of them are useful:
$ rustc --edition 2021 -O -o product product.rs && ./product
pair on a on b accepting accessible co-accessible
(0,0) (1,1) (1,2) false true true
(0,1) (1,1) (1,1) true true true
(0,2) (1,2) (1,2) false true false
(1,0) (0,1) (0,2) false false true
(1,1) (0,1) (0,1) false true true
(1,2) (0,2) (0,2) false true false
trim automaton: 3 of 6 states
accepted words of length 0..8: [0, 0, 2, 0, 8, 0, 32, 0, 128]
The counts, words of every even length , are computed from the automaton by dynamic programming: the number of words leading to each state, letter after letter (Exercise 21.4).
21.6 Automata as programs
A DFA runs in constant time per letter: one table lookup, as in the hook. An NFA can be run either by building the DFA first, or by simulating it on the fly, keeping the set of possible states, which costs up to the number of NFA states per letter but builds nothing. With at most 64 states, the set fits in the bits of one machine word, and a step is a few shifts and ORs (Exercise 21.11). On , over two million letters:
$ rustc --edition 2021 -O -o simulate_vs_determinise simulate_vs_determinise.rs && ./simulate_vs_determinise
k NFA, sets NFA, bits DFA states build (ms) DFA
ns/letter ns/letter ns/letter
4 100 5.3 16 0.0 3.9
10 137 7.6 1024 0.7 7.8
16 207 8.9 65536 79.8 82.5
The DFA wins while it is small. At it has 65 536 states, takes 80 ms to build, and runs slower than the simulation by bits, since its table no longer fits in the processor’s caches and each letter costs a memory access (Chapter 12). Programs that search text with patterns face the same choice, and many build the DFA lazily, one state the first time it is needed.
21.7 Exercises
Exercise 21.1 ★
Give the run of the automaton of Listing 21.1 on 0.0, -.5, 1E+10, 7e3.5 and +, and say which are accepted.
Solution
Solution of Exercise 21.1.
$ rustc --edition 2021 -O -o exo_runs exo_runs.rs && ./exo_runs
"0.0" number states [0, 2, 3, 4]
"-.5" no states [0, 1]
"1E+10" number states [0, 2, 5, 6, 7, 7]
"7e3.5" no states [0, 2, 5, 7]
"+" no states [0, 1]
0.0 and 1E+10 are accepted. -.5 stops in state 1: after a sign, only a digit may come. 7e3.5 stops in state 7: the exponent has no point. + ends in state 1, which is not accepting.
Exercise 21.2 ★
The DFA with states 0 and 1, start 0, accepting 1, and the transitions and recognises . Swap its accepting and other states: which language do you get? Which words are missing from the complement, and how is it repaired?
Solution
Solution of Exercise 21.2.
$ rustc --edition 2021 -O -o exo_complement exo_complement.rs && ./exo_complement
word a b* swapped complement
"" false true true
"a" true false false
"b" false false true
"ab" true false false
"ba" false false true
"abb" true false false
"aab" false false true
The swapped automaton accepts only : every other word either ends in state 1, now rejecting, or has no run. The words without a run in ’s automaton, such as , or , are rejected by both, though the complement must accept them. Completing first with a sink state, then swapping, gives the complement (Proposition 21.17).
Exercise 21.3 ★
Build a DFA with three states that recognises the binary writings of the multiples of 3 (the empty word standing for 0), and prove it correct.
Solution
Solution of Exercise 21.3.
$ rustc --edition 2021 -O -o exo_mult3 exo_mult3.rs && ./exo_mult3
state 0: 0 -> 0, 1 -> 1 (start, accepting)
state 1: 0 -> 2, 1 -> 0
state 2: 0 -> 1, 1 -> 2
agrees with x % 3 == 0 for x = 0 .. 999: true
The state is the value read so far, modulo 3: reading a bit after a prefix of value gives the value , so state goes to . By induction on the length, : true for , of value 0, and kept by each step since . So is accepted, ending in state 0, exactly when its value is a multiple of 3.
Exercise 21.4 ★
Build a complete DFA for the words over that do not contain the factor , and find a recurrence for the number of such words of length .
Solution
Solution of Exercise 21.4.
$ rustc --edition 2021 -O -o exo_count exo_count.rs && ./exo_count
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]
States: 0, the last letter is not (or no letter yet); 1, the last letter is ; 2, has been seen, a sink. States 0 and 1 accept. Let and count the words of length ending in states 0 and 1. Then (an from either) and (a from state 0 only), so satisfies , with and : the Fibonacci numbers.
Exercise 21.5 ★★
Apply the subset construction to the NFA with states 0, 1, 2, start 0, accepting 2, and the transitions , and . Which language is it?
Solution
Solution of Exercise 21.5.
$ rustc --edition 2021 -O -o exo_subset exo_subset.rs && ./exo_subset
0: {0} a -> 1, b -> 0
1: {0, 1} a -> 1, b -> 2
2: {0, 2} a -> 1, b -> 0, accepting
From , leads to and back to ; from , stays and leads to , accepting; from , leads to and to . Only 3 of the 8 subsets are reachable. The language is the words that end with .
Exercise 21.6 ★★
A program computes the closure of a set by adding, once, the targets of the spontaneous transitions of its states. On the automaton of , which words does it get wrong, and why?
Solution
Solution of Exercise 21.6.
$ rustc --edition 2021 -O -o exo_closure_bug exo_closure_bug.rs && ./exo_closure_bug
"" one step: false closure: true
"a" one step: false closure: true
"ab" one step: true closure: true
"abc" one step: true closure: true
"c" one step: false closure: true
From , one step adds 1 but not 2, which is two spontaneous steps away. So , and are wrongly rejected: each needs a run that passes from 0 to 2 without reading anything. Words that read a pass through state 1, from which one step reaches 2, and come out right. The closure is reachability in a graph, and must loop until no new state appears, as Listing 21.2 does.
Exercise 21.7 ★★
Build a DFA with four states for the words over that contain the factor , and explain what each state remembers.
Solution
Solution of Exercise 21.7.
// factor_aba.rs: a DFA for the words over {a, b} that contain the factor aba (exercise 7).
// State i: the longest suffix of what was read that is a prefix of aba has length i;
// state 3: aba has been seen.
pub const DELTA: [[usize; 2]; 4] = [
[1, 0], // "": a starts the pattern, b does not
[1, 2], // "a": a again keeps "a", b makes "ab"
[3, 0], // "ab": a completes aba, b leaves nothing
[3, 3], // found: stay
];
pub fn contains_aba(word: &str) -> bool {
let mut q = 0;
for c in word.chars() {
q = DELTA[q][if c == 'a' { 0 } else { 1 }];
}
q == 3
}
State means that the longest suffix of what has been read that is a prefix of has length ; state 3 means has appeared. From state 2 (), a gives , whose suffixes , , are not prefixes of except : back to 0. From state 1 (), an gives , whose longest such suffix is : state 1. The test compares it with contains("aba") on every word up to length 10. The same idea, for any pattern, is the automaton of the text-search algorithms of Chapter 25.
Exercise 21.8 ★★
The reverse of is . Show that if is recognisable, the set of the reverses of its words is recognisable.
Solution
Solution of Exercise 21.8.
Take a DFA for and reverse every transition: becomes . Make the accepting states the start states, and the old start state the only accepting state. This is an NFA; a run of it on from some to is exactly a run of the DFA on read backwards. So the NFA recognises the reverses of the words of , and by Theorem 21.14 so does a DFA.
Exercise 21.9 ★★
For integers and , build a DFA with states that recognises the writings in base of the multiples of , and test it.
Solution
Solution of Exercise 21.9.
// multiples.rs: the multiples of n written in base b, as a DFA on the digits (exercise 9).
// The state is the value read so far, modulo n: reading digit d sends r to (b r + d) mod n.
pub fn delta(n: u32, b: u32) -> Vec<Vec<u32>> {
(0..n).map(|r| (0..b).map(|d| (b * r + d) % n).collect()).collect()
}
/// digits: most significant first; the empty word stands for 0.
pub fn is_multiple(n: u32, b: u32, digits: &[u32]) -> bool {
let table = delta(n, b);
digits.iter().fold(0, |r, &d| table[r as usize][d as usize]) == 0
}
As in Exercise 21.3: reading a digit after a value gives , so the state goes to ; the start and the only accepting state are 0. The test checks five pairs , included, on every number below 2000.
Exercise 21.10 ★★★
A fooling set for is a set of words such that for any two in there is a word with exactly one of and in . Show that every complete DFA recognising (one whose transition function is defined everywhere) has at least accessible states, and a partial one at least . Find a fooling set of size 3 for the language of Exercise 21.3.
Solution
Solution of Exercise 21.10.
Let in , and suppose a DFA for has , both defined. For the word that tells them apart, Proposition 21.5 gives , so and are both accepted or both rejected: a contradiction. If one of the two runs is undefined, or has no extension in , and at most one word of can be like that (two such words could not be told apart). So the words of lead to different states, or states and one undefined run; the completed automaton has at least states, and the bound holds for every complete DFA. For the multiples of 3, , of values 0, 1 and 2: appending separates from the others, and appending separates (value 3, a multiple) from (value 5).
Exercise 21.11 ★★★
Write an NFA simulation that keeps the set of states as the bits of a 64-bit word, and compare its speed with the determinised automaton for .
Solution
Solution of Exercise 21.11.
/// Simulation with the set of states as the bits of one machine word.
fn simulate_bits(n: &Nfa, word: &[usize]) -> bool {
let mut masks = vec![[0u64; 2]; n.delta.len()]; // successors of q on a and on b
for (q, row) in n.delta.iter().enumerate() {
for a in 0..2 {
masks[q][a] = row[a].iter().fold(0, |m, &r| m | 1 << r);
}
}
let mut set = 1u64 << n.start[0];
for &a in word {
let mut next = 0;
let mut rest = set;
while rest != 0 {
let q = rest.trailing_zeros() as usize; // one state of the set
next |= masks[q][a];
rest &= rest - 1;
}
set = next;
}
(0..n.delta.len()).any(|q| set >> q & 1 == 1 && n.accept[q])
}
For each state and letter, the set of successors is precomputed as a mask. A step ORs the masks of the states in the current set, found by trailing_zeros and cleared one by one. Output 21.6 gives about 5 to 9 ns per letter, against 100 to 210 with ordered sets, and against a DFA that wins for small but whose 65 536 states at make each lookup a cache miss.
Exercise 21.12 ★★★
Show that if is recognisable, the set of the prefixes of its words is recognisable.
Solution
Solution of Exercise 21.12.
Take a trim DFA for (Proposition 21.8), and make every state accepting. If is a prefix of a word , the run on exists, so the run on exists and ends in a state of the trim automaton: accepted. Conversely, if the run on ends in a state , then is co-accessible, so some leads from to an accepting state, and . If is empty, the trim automaton has no state, and the language of prefixes is empty too.
21.8 Problem: Decoding the lighthouse’s light signals
Problem 21.1
Weekend problem — Decoding the lighthouse’s light signals
The lighthouse sends messages as flashes, short () or long (). A message is a word over ; the harbour reads it as it comes.
Part I — Signals as words.
- How many messages of length 10 are there, and how many of length at most 10?
- List the prefixes, suffixes and factors of length 2 of .
- Is a subword of ? A factor?
- Describe and in words.
- Show that for every language .
- Is ? Give a word that tells.
Part II — Valid messages.
A message is valid if it has an even number of long flashes and never three short flashes in a row.
- Build a complete DFA with two states for “an even number of ”.
- Build a complete DFA with four states for “no factor ”.
- How many states has their product, and which are accepting?
- Which states of the product are not co-accessible? How many states does the trim automaton keep?
- Run it on and .
- Write the recurrence that counts valid messages, state by state, and compute the counts for lengths 0 to 6.
Part III — A pattern near the end.
The harbour must react when the fourth flash from the end is long.
- Draw an NFA with 5 states for it.
- What does its subset construction remember after each flash?
- How many states does the DFA have, and what is its start state?
- Give two messages of length 4 that lead to different DFA states, and a suffix that tells them apart.
- Is the NFA’s on-the-fly simulation cheaper than the DFA here? Use Output 21.6.
- Why can the harbour not use an automaton that reads the message backwards?
Part IV — The blow-up.
- For “the -th flash from the end is long”, how many states do the NFA and the DFA need?
- Which words form a fooling set, and why does that prove the bound?
- Could a different NFA with fewer than states recognise the language?
- To check also that the message is valid, the harbour runs the product of the DFA of question 19 with the trim automaton of question 10. At most how many states does it have for ?
- How does Output 21.4 check the proof for ?
- State the result: the law, exactly states, none removable.
Solution
Solution of Problem 21.1.
1. ; of length at most 10.
2. Prefix , suffix ; factors of length 2: , , .
3. is a subword (delete both ’s) but not a factor.
4. The messages that contain at least one long flash; the messages in which short flashes come in pairs (every maximal block of has even length).
5. since ; and a concatenation of words of followed by words of is a concatenation of of them.
6. No: is in but not in .
7. States (even, start, accepting) and ; swaps them, keeps them.
8. States 0, 1, 2 (the number of trailing , all accepting) and a sink 3: moves to (2 to 3), moves 0, 1, 2 to 0.
9. 8 states; accepting: , , .
10. and , the sink pairs, cannot reach acceptance; all states are accessible (from the start, and reach the two sink pairs). The trim automaton keeps 6 states.
11. : , accepted. : after the run is in , rejected.
12. Writing and for the words of length ending in and : and (an switches the parity), and , for . The valid messages of lengths 0 to 6 are 1, 1, 2, 3, 7, 12, 22.
13. States 0 to 4: 0 loops on and , , then up to 4, accepting.
14. Which of the last four flashes were long: the set , where holds the positions, among the last four, of the long ones.
15. states; the start state is .
16. and : the suffix tells them apart, since the first has a long flash fourth from the end.
17. For , the DFA has 16 states and its table fits in the caches: in Output 21.6 it runs at about 4 ns a flash, a little faster than the simulation by bits. The simulation wins only for large .
18. The flashes arrive one at a time and the harbour must react as they come; a backward automaton would need the whole message first. (Read backwards, the condition concerns the fourth flash from the start, which a 5-state DFA checks.)
19. for the NFA, for the DFA (Theorem 21.15).
20. The messages of length : any two differ first at some position , and the suffix puts that flash -th from the end, so one message becomes accepted and the other not; by Exercise 21.10, every DFA has at least states.
21. No. Take the pairs , for : is accepted, so an accepting run passes, after , through some state . If with , the run on followed by the rest of the run on accepts , which has fewer than letters and is not in the language. So the states are distinct.
22. At most pairs, of which only the accessible ones need to be built.
23. It counts the states reached by the words of length , and checks, for each of the pairs, that the suffix of the proof makes exactly one of the two words accepted: 8 386 560 pairs for .
24. The law: for “the -th letter from the end”, an NFA has states, and every DFA has at least , which the subset construction reaches exactly; none of its states can be removed or merged, since the words of length lead to pairwise distinguishable states.