Sub topic 2
Deterministic Finite Automata
Assume you know basic sets, functions, relations, and strings. A deterministic finite automaton (DFA) is a finite-state machine that reads a string one symbol at a time and decides whether the string belongs to a language.
A DFA has five parts:
- \(Q\): a finite set of states
- \(\Sigma\): an input alphabet
- \(\delta: Q \times \Sigma \to Q\): a transition function
- \(q_0 \in Q\): the start state
- \(F \subseteq Q\): the accepting states
The transition function is deterministic because every pair (state, input symbol) has exactly one next state. A DFA cannot choose between two transitions and cannot stop without processing the complete input.
How a DFA accepts a string
Begin in the start state. For each input symbol, follow the transition labeled with that symbol. After the final symbol, accept if the current state belongs to \(F\).
For a string \(w = a1a2\cdots a_n\), the extended transition function \(\delta^*\) applies the transitions to the whole string:
\[ \delta^*(q, \epsilon)=q \]
\[ \delta^(q, xa)=\delta(\delta^(q,x),a) \]
The language recognized by the DFA is:
\[ L=\{w\in\Sigma^ \mid \delta^(q_0,w)\in F\} \]
Here, \(\Sigma^*\) means all finite strings over the alphabet, including the empty string \(\epsilon\).
Example: binary values divisible by 3
A DFA can remember the remainder modulo 3 of the binary number read so far. If the current remainder is \(r\) and the next bit is \(b\), the new remainder is:
\[ (2r+b)\bmod 3 \]
Use states \(q0,q1,q2\), representing remainders 0, 1, and 2. The start state is \(q0\), and \(q_0\) is accepting.
| Current state | Input 0 | Input 1 |
|---|---|---|
| \(q_0\) | \(q_0\) | \(q_1\) |
| \(q_1\) | \(q_2\) | \(q_0\) |
| \(q_2\) | \(q_1\) | \(q_2\) |
The string 110 represents 6. Its state sequence is \(q0 \to q1 \to q0 \to q0\), so it is accepted.
def divisible_by_3(w):
# The state is the remainder of the binary prefix modulo 3.
state = 0
for ch in w:
state = (2 * state + int(ch)) % 3
return state == 0
print(divisible_by_3("110")) # True: 6
print(divisible_by_3("101")) # False: 5
print(divisible_by_3("")) # True: the empty input represents 0Common errors
- Omitting a transition. A complete DFA needs one transition for every state and every alphabet symbol.
- Marking a state accepting because it was visited. Acceptance depends only on the state after the entire string.
- Forgetting the empty string. It is accepted exactly when the start state is accepting.
- Confusing a state’s meaning with its name. State labels such as
q1have no meaning unless the construction assigns one.
Exercise
Design a DFA over {0, 1} that accepts exactly the strings ending in 1. What should its accepting state represent?
Show answer
Use one state for “the last symbol is not 1 or no symbol has been read” and one accepting state for “the last symbol is 1.” On input 0, move to the first state; on input 1, move to the accepting state.