A regular expression describes a language. A finite automaton recognises it: it is a tiny machine with a fixed, finite memory (its states) that reads a string from left to right and finally says “accept” or “reject”. Lexical analysers in compilers, vending machines, traffic lights and protocol handlers are all finite automata.
Intuition
A DFA is like a board game with a single counter. The squares are the states; one square is marked START and some are marked WIN. You read the input letters one at a time; each letter tells you exactly which square to move to next. When the letters run out, you win (accept) if your counter is on a WIN square.
Definition 2.16 · Deterministic finite automaton (DFA)
A DFA is a 5-tuple M = ( Q , Σ , δ , q 0 , F ) M = (Q, \Sigma, \delta, q_0, F) M = ( Q , Σ , δ , q 0 , F ) where Q Q Q is a finite set of states , Σ \Sigma Σ is a finite input alphabet, δ : Q × Σ → Q \delta: Q \times \Sigma \to Q δ : Q × Σ → Q is the transition function (for each state and each input symbol, exactly one next state), q 0 ∈ Q q_0 \in Q q 0 ∈ Q is the start state , and F ⊆ Q F \subseteq Q F ⊆ Q is the set of final (accepting) states.
A DFA is drawn as a transition diagram : circles for states, an arrow labelled “start” into q 0 q_0 q 0 , double circles for final states, and an arrow labelled a a a from p p p to q q q when δ ( p , a ) = q \delta(p, a) = q δ ( p , a ) = q . Every state has exactly one outgoing arrow for each symbol. It can also be given as a transition table .
Running a DFA on a string
The extended transition function δ ∗ \delta^* δ ∗ handles whole strings: δ ∗ ( q , λ ) = q \delta^*(q, \lambda) = q δ ∗ ( q , λ ) = q and δ ∗ ( q , w a ) = δ ( δ ∗ ( q , w ) , a ) \delta^*(q, wa) = \delta(\delta^*(q, w), a) δ ∗ ( q , w a ) = δ ( δ ∗ ( q , w ) , a ) . The DFA accepts w w w if δ ∗ ( q 0 , w ) ∈ F \delta^*(q_0, w) \in F δ ∗ ( q 0 , w ) ∈ F , and the language of M M M is L ( M ) = { w ∈ Σ ∗ ∣ δ ∗ ( q 0 , w ) ∈ F } L(M) = \{w \in \Sigma^* \mid \delta^*(q_0, w) \in F\} L ( M ) = { w ∈ Σ ∗ ∣ δ ∗ ( q 0 , w ) ∈ F } .
Example 2.17 A DFA for strings that contain b
Goal. States x x x (start), y y y , z z z (final); δ ( x , a ) = y \delta(x, a) = y δ ( x , a ) = y , δ ( x , b ) = z \delta(x, b) = z δ ( x , b ) = z , δ ( y , a ) = x \delta(y, a) = x δ ( y , a ) = x , δ ( y , b ) = z \delta(y, b) = z δ ( y , b ) = z , δ ( z , a ) = δ ( z , b ) = z \delta(z, a) = \delta(z, b) = z δ ( z , a ) = δ ( z , b ) = z . Trace a a a aaa aaa and a b b a abba abba , and find L ( M ) L(M) L ( M ) .
Step. Transition table:
State a a a b b b → x x x (start) y y y z z z y y y x x x z z z z z z (final)z z z z z z
Step. a a a aaa aaa : x → a y → a x → a y x \xrightarrow{a} y \xrightarrow{a} x \xrightarrow{a} y x a y a x a y . y ∉ F y \notin F y ∈ / F : rejected.
Step. a b b a abba abba : x → a y → b z → b z → a z x \xrightarrow{a} y \xrightarrow{b} z \xrightarrow{b} z \xrightarrow{a} z x a y b z b z a z . z ∈ F z \in F z ∈ F : accepted.
Step. With only a a a ‘s the machine moves between x x x and y y y ; the first b b b sends it to z z z , which it never leaves.
Result. L ( M ) L(M) L ( M ) = strings over { a , b } \{a, b\} { a , b } containing at least one b b b = L ( ( a + b ) ∗ b ( a + b ) ∗ ) L((a + b)^*b(a + b)^*) L (( a + b ) ∗ b ( a + b ) ∗ ) .
Correction · Transition rule 3 of the first FA example
Study material says: In §2.4.1 the list of rules contains “3. from state y and input b go to state x” and “4. from state y and input b go to state z”.
Correct version: Rule 3 is “from state y y y and input a a a go to state x x x ”, as shown in Table 1 and used in the trace of a a a aaa aaa (“the next input is also an a, and this tells us (by rule 3) to go back to state x”).
Why: In a DFA each (state, symbol) pair has exactly one next state; two rules for (y y y , b b b ) would make the machine non-deterministic and leave (y y y , a a a ) undefined.
Example 2.18 Tracing a DFA with a program
Goal. States 1 (start), 2 (final), 3 over { 0 , 1 } \{0, 1\} { 0 , 1 } with δ ( 1 , 0 ) = 1 \delta(1,0) = 1 δ ( 1 , 0 ) = 1 , δ ( 1 , 1 ) = 2 \delta(1,1) = 2 δ ( 1 , 1 ) = 2 , δ ( 2 , 0 ) = 2 \delta(2,0) = 2 δ ( 2 , 0 ) = 2 , δ ( 2 , 1 ) = 3 \delta(2,1) = 3 δ ( 2 , 1 ) = 3 , δ ( 3 , 0 ) = δ ( 3 , 1 ) = 3 \delta(3,0) = \delta(3,1) = 3 δ ( 3 , 0 ) = δ ( 3 , 1 ) = 3 . Trace 0001001 0001001 0001001 and 0100 0100 0100 and describe L ( M ) L(M) L ( M ) .
Step. A dictionary stores δ \delta δ ; the loop applies it to each symbol:
Python · dfa_trace.py Copy delta = {( 1 , "0" ): 1 , ( 1 , "1" ): 2 ,
( 2 , "0" ): 2 , ( 2 , "1" ): 3 ,
( 3 , "0" ): 3 , ( 3 , "1" ): 3 }
start, final = 1 , { 2 }
def run(word):
"""Return the list of states visited and whether word is accepted."""
state, path = start, [start]
for symbol in word:
state = delta[(state, symbol)]
path.append(state)
return path, state in final
for w in [ "0001001" , "0100" ]:
path, ok = run(w)
print (w, "->" , " " .join( map ( str , path)), "accepted" if ok else "rejected" )
Output Copy 0001001 -> 1 1 1 1 2 2 2 3 rejected
0100 -> 1 1 2 2 2 accepted
Step. The machine stays in 1 until the first 1, stays in 2 until a second 1, then is trapped in 3 (a dead state ).
Result. L ( M ) L(M) L ( M ) = strings over { 0 , 1 } \{0, 1\} { 0 , 1 } with exactly one 1 = L ( 0 ∗ 10 ∗ ) = L(0^*10^*) = L ( 0 ∗ 1 0 ∗ ) .
Common mistake
Leaving out transitions. In a DFA every state needs exactly one arrow for each symbol. Add a non-final dead state that loops on every symbol to catch “impossible” inputs.
Common mistake
Deciding acceptance too early. A DFA accepts only if it is in a final state after the whole string has been read. Passing through a final state in the middle does not matter.
Key idea
A DFA ( Q , Σ , δ , q 0 , F ) (Q, \Sigma, \delta, q_0, F) ( Q , Σ , δ , q 0 , F ) reads a string symbol by symbol, moving to exactly one next state each time, and accepts if it ends in a final state; the set of accepted strings is its language.
TEE Exam note
Expect: “Define a DFA formally”, “Draw the transition diagram / table”, “Trace the DFA on … and say whether it is accepted”, “What language does this DFA accept?”. For a trace, write the sequence of states with arrows labelled by the input symbols. For a language, describe it in words and with a regular expression, and test it on a few strings.
Key takeaways
DFA = ( Q , Σ , δ , q 0 , F ) (Q, \Sigma, \delta, q_0, F) ( Q , Σ , δ , q 0 , F ) with δ : Q × Σ → Q \delta: Q \times \Sigma \to Q δ : Q × Σ → Q (exactly one next state).
Diagrams: start arrow, double circles for final states, one arrow per symbol from each state.
w w w is accepted iff δ ∗ ( q 0 , w ) ∈ F \delta^*(q_0, w) \in F δ ∗ ( q 0 , w ) ∈ F .
A dead state collects all strings that can never be accepted.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice In a DFA ( Q , Σ , δ , q 0 , F ) (Q, \Sigma, \delta, q_0, F) ( Q , Σ , δ , q 0 , F ) with ∣ Q ∣ = 4 |Q| = 4 ∣ Q ∣ = 4 and ∣ Σ ∣ = 2 |\Sigma| = 2 ∣Σ∣ = 2 , how many transitions must be defined?
A 4 B 6 C 8 D 16
Check answer
Show answer Answer: C. 8
One transition for each (state, symbol) pair: 4 × 2 = 8 4 \times 2 = 8 4 × 2 = 8 . Option 4 would be 4 2 4^2 4 2 , which has no meaning here.
Question 2 · multiple choice For the DFA that accepts strings containing b b b (states x , y , z x, y, z x , y , z ), which string is rejected?
Check answer
Show answer Answer: D. a a a a aaaa aaaa
Without a b b b the machine never reaches z z z . All other strings contain a b b b .
Question 3 · multiple choice A DFA passes through a final state while reading w w w but ends in a non-final state. Then:
Check answer
Show answer Answer: A. w w w is rejected
Only the state after the last symbol counts. Option 2 is the common mistake of accepting too early.
Question 4 · multiple choice For the “exactly one 1” DFA (states 1, 2, 3), what is δ ∗ ( 1 , 0110 ) \delta^*(1, 0110) δ ∗ ( 1 , 0110 ) ?
A 1 B 2 C 3 D undefined
Check answer
Show answer Answer: C. 3
1 → 0 1 → 1 2 → 1 3 → 0 3 1 \xrightarrow{0} 1 \xrightarrow{1} 2 \xrightarrow{1} 3 \xrightarrow{0} 3 1 0 1 1 2 1 3 0 3 . Option 2 is where the machine is after the first 1.
Question 5 · multiple choice The DFA with q 0 → b q 1 → b q 2 q_0 \xrightarrow{b} q_1 \xrightarrow{b} q_2 q 0 b q 1 b q 2 , q 2 q_2 q 2 looping on a , b a, b a , b , and all other moves to a dead state, accepts:
Check answer
Show answer Answer: D. strings beginning with b b bb bb
The first two letters must both be b b b ; after that anything is allowed. Option 1 is the study material’s wording, but b b b bbb bbb is accepted too.
Question 6 · short answer
Give the formal 5-tuple and transition table of a DFA over { a , b } \{a, b\} { a , b } that accepts strings with an even number of a a a ‘s.
Show worked answer M = ( { q e , q o } , { a , b } , δ , q e , { q e } ) M = (\{q_e, q_o\}, \{a, b\}, \delta, q_e, \{q_e\}) M = ({ q e , q o } , { a , b } , δ , q e , { q e }) , where q e q_e q e means “even number of a a a ‘s so far” and q o q_o q o “odd”. Table: δ ( q e , a ) = q o \delta(q_e, a) = q_o δ ( q e , a ) = q o , δ ( q e , b ) = q e \delta(q_e, b) = q_e δ ( q e , b ) = q e , δ ( q o , a ) = q e \delta(q_o, a) = q_e δ ( q o , a ) = q e , δ ( q o , b ) = q o \delta(q_o, b) = q_o δ ( q o , b ) = q o . Start in q e q_e q e (zero a a a ‘s is even); accept in q e q_e q e . Check: a b a b abab abab has two a a a ‘s: q e → q o → q o → q e → q e q_e \to q_o \to q_o \to q_e \to q_e q e → q o → q o → q e → q e , accepted.