Sub topic 1
Alphabets, strings, and languages
Prerequisites
This chapter assumes you know set union, intersection, and Cartesian product, what a function is, and basic induction. You should also be comfortable treating a string as a sequence of symbols.
Alphabets, strings, and languages
An alphabet Σ is a finite, nonempty set of symbols. A string is a finite sequence of symbols from Σ. The empty string ε has length zero. The set of all strings over Σ is written Σ*.
A language is any set of strings over Σ. Concatenation joins two strings, and the Kleene star L* is the set of strings formed by concatenating zero or more strings from L.
Deterministic finite automata
A deterministic finite automaton (DFA) has a finite set of states, one start state, a set of accepting states, and a transition function that gives exactly one next state for each state and symbol. A DFA accepts a string if it ends in an accepting state.
This DFA accepts binary strings whose value is divisible by 3. Its three states hold the remainder of the value read so far.
def divisible_by_3(w):
state = 0
for ch in w:
state = (2 * state + int(ch)) % 3
return state == 0
print(divisible_by_3("110")) # True
print(divisible_by_3("101")) # FalseNondeterminism and NFAs
A nondeterministic finite automaton (NFA) may have several next states for one symbol, or none. It accepts a string if at least one path through it ends in an accepting state. Every DFA is also an NFA.
Epsilon transitions and epsilon-closure
An epsilon transition moves between states without reading a symbol. The epsilon-closure of a state set S is every state reachable from S by epsilon moves alone, including S itself.
Converting an NFA to a DFA with subset construction
Subset construction makes each set of NFA states into one DFA state. Start from the epsilon-closure of the NFA start state. For each DFA state S and symbol c, the next DFA state is the epsilon-closure of all states reachable from S on c. A DFA state is accepting if it contains an NFA accepting state. Build only the subsets that are reachable.
Exercise
How many reachable DFA states does subset construction produce for the NFA for (a|b)*abb shown above?
Show answer
Four. The subsets track how much of the suffix abb has been matched so far.