A deterministic finite automaton (DFA) is a 5-tuple (Q, Σ, δ, q0, F
) where:
Q
is a finite set of statesΣ
is a finite alphabetδ: Q × Σ → Q
is the transition functionq0 ∈ Q
is the start stateF ⊆ Q
is the set of accept states
A framework for understanding
A Deterministic Finite Automaton (DFA) is the simplest type of automaton, capable of recognizing regular languages. DFAs can be visualized as state diagrams and are used in various applications like lexical analysis and pattern matching.
A deterministic finite automaton (DFA) is a 5-tuple (Q, Σ, δ, q0, F
) where:
Q
is a finite set of statesΣ
is a finite alphabetδ: Q × Σ → Q
is the transition functionq0 ∈ Q
is the start stateF ⊆ Q
is the set of accept statesA DFA accepts a string w
if, starting from the start state q0
, the DFA ends in an accept state after processing all symbols in w
. Formally, a string w = w1w2...wn
is accepted if there exists a sequence of statesr0,r1,...,rn
such that:
r0 = q0
(start at the initial state)ri = δ(ri-1, wi)
for i = 1,2,...,n
(follow transitions)rn ∈ F
(end in an accept state)The set of all strings accepted by a DFA M
is called the language of M
, denoted L(M)
.
To formally define string acceptance, we need to extend the transition function δ
to handle strings rather than just individual symbols. The extended transition function, denoted δ̂: Q × Σ* → Q
, is defined recursively:
δ̂(q, ε) = q
(on empty string, stay in the same state)δ̂(q, wa) = δ(δ̂(q, w), a)
(process the string w
first, then the symbol a
)With this definition, we can formally state that a DFA M
accepts a string w
if and only if δ̂(q0, w) ∈ F
.
DFAs can be represented in several ways:
Q, Σ, δ, q0, F
) described above.Consider the language L = {w | w ends with the substring ab}
over the alphabet Σ = {a, b}
.
Q = {q₀, q₁, q₂}
Σ = {a
, b
}
q0
is the start stateF = {q₂}
(accept state)δ
:δ(q0, a
) = q1
(saw a
, might be the start of ab
)δ(q0, b
) = q0
(still waiting for an a
)δ(q1, a
) = q1
(saw another a
, restart the pattern)δ(q1, b
) = q2
(saw b
after a
, completed ab
)δ(q2, a
) = q1
(saw a
, might be the start of a new ab
)δ(q2, b
) = q0
(saw b
, not part of ab
pattern)This DFA will accept strings like ab
, aab
, bab
, ababab
, but reject strings like a
, b
, ba
, abb
.
For certain regular languages, we can establish precise lower bounds on the number of states required in any DFA that recognizes them:
L = {w | w ends with ab}
requires at least 3 statesL = {w | w contains the substring a}
n requires at least n+1 statesL = {w | |w| mod n = 0}
requires at least n statesL = {w | w has a 1 in the nth position from the end}
requires at least 2n statesWe prove that any DFA recognizing L = {w | w ends with ab}
requires at least 3 states.
Suppose, for contradiction, that there exists a DFA M
with fewer than 3 states that recognizes L
. Since M
has at most 2 states, by the pigeonhole principle, when processing a string of length ≥ 2, some state must be visited more than once.
Consider the strings ε
, a
, and ab
.
Let q0
be the start state. Let q1 = δ̂(q0, a)
be the state after reading a
. Since M
has at most 2 states, either q1 = q0
or M
has exactly 2 states.
Case 1: If q1 = q0
, then δ̂(q0, a) = δ̂(q0, aa) = δ̂(q0, aaa) = ...
, meaning that the DFA cannot distinguish between strings ending in a
and strings ending in ab
.
Case 2: If M
has exactly 2 states, q0
and q1
, then consider q2 = δ̂(q1, b)
. By our assumption, q2
must be either q0
or q1
.
If q2 = q0
, then ab
and ε
end in the same state, which contradicts the language definition since ab ∈ L
but ε ∉ L
.
If q2 = q1
, then a
and ab
end in the same state, which contradicts the language definition since ab ∈ L
but a ∉ L
.
Therefore, a DFA for L
requires at least 3 states.
Consider these two languages:
L1 = {w | |w| ≡ 0 (mod 3)}
– length divisible by 3 (needs 3 states)L2 = {w | w contains a
}
– contains the letter a
(needs 2 states)Their intersection L1 ∩ L2
= length divisible by 3 AND contains a
a
seena
seena
seena
seen ✓a
seena
seenAll 6 states are reachable and distinguishable, achieving the 3×2 bound!
Let L1 =
a*
(2 states) and L2 = {w | 3rd symbol from end is
(8 states).b
}
For L1L2
, we need to track:
L₁
and L₂
partsL₂
partL₂
states, leading to exponential sizeThe table-filling algorithm for DFA minimization is correct: It produces a minimal DFA equivalent to the input DFA.
Formally, given a DFA M = (Q, Σ, δ, q0, F)
, the algorithm produces a DFA M' = (Q', Σ, δ', q0', F')
such that:
L(M) = L(M')
(language equivalence)M''
with L(M) = L(M'')
, |Q'| ≤ |Q''|
(minimality)O(|Q|²|Σ|)
timeWe prove that the table-filling algorithm correctly identifies all and only the pairs of distinguishable states.
Two states p, q ∈ Q
are distinguishable if there exists a string w ∈ Σ*
such that either δ̂(p, w) ∈ F
and δ̂(q, w) ∉ F
, or δ̂(p, w) ∉ F
and δ̂(q, w) ∈ F
.
Claim 1: If states p, q
are distinguishable, the algorithm will mark them.
Proof by induction on the length of the distinguishing string:
Base case: If p, q
are distinguished by ε
, then one is accepting and the other is not. The algorithm marks these pairs in the initialization step.
Inductive step: Assume all pairs distinguishable by strings of length ≤ k are marked. Consider states p, q
distinguishable by a string w = ax
of length k+1, where a ∈ Σ
and |x| = k
.
Let p' = δ(p, a)
and q' = δ(q, a)
. Since w = ax
distinguishes p
and q
, the string x
must distinguish p'
and q'
. By the induction hypothesis, (p', q')
is marked. Therefore, in the iterative step, when considering (p, q)
and symbol a
, the algorithm will mark (p, q)
because (δ(p, a), δ(q, a))
is marked.
Claim 2: If the algorithm marks a pair of states, they are distinguishable.
Proof by induction on the iteration number:
Base case: In the initialization step, we mark pairs where one state is accepting and the other is not. These are distinguished by the empty string ε
.
Inductive step: Assume all pairs marked in iterations ≤ k are distinguishable. Consider a pair (p, q)
marked in iteration k+1. This occurs because there exists a symbol a ∈ Σ
such that (δ(p, a), δ(q, a))
was marked in a previous iteration. By the induction hypothesis, δ(p, a)
and δ(q, a)
are distinguishable by some string x
. Therefore, p
and q
are distinguishable by the string ax
.
Thus, the algorithm correctly identifies all distinguishable state pairs, and the resulting DFA after merging indistinguishable states is minimal and language-equivalent to the original.
For every integer n ≥ 1
, there exists a regular language Ln
such that:
Ln
has exactly n
statesn
states can recognize Ln
Moreover, the number of distinct n-state minimal DFAs (up to isomorphism) over an alphabet of size k
is at least 2kn-n
.
For every n ≥ 1
, consider the language Ln = {w ∈ {0,1}* | |w| mod n = 0}
of all strings whose length is a multiple of n
.
To recognize Ln
, a DFA needs to count modulo n
. We can construct an n
-state DFA Mn = (Q, Σ, δ, q0, F)
where:
Q = {0, 1, 2, ..., n-1}
Σ = {0, 1}
q0 = 0
F = {0}
δ(i, a) = (i + 1) mod n
for all i ∈ Q, a ∈ Σ
The state i
represents the fact that the length of the string read so far is congruent to i
modulo n
. Thus, Mn
accepts exactly the strings whose length is a multiple of n
.
Now we prove that n
states are necessary. Consider the strings ε, 0, 00, ..., 0n-1
. These strings have lengths 0, 1, 2, ..., n-1
. For any two distinct strings 0i
and 0j
with 0 ≤ i < j < n
, the string 0n-j+i
distinguishes them because 0i · 0n-j+i = 0n+i-j
has length n+i-j
, which is congruent to i-j mod n
and is not a multiple of n
(since 0 < j-i < n
), while 0j · 0n-j+i = 0n+i
has length n+i
, which is congruent to i mod n
.
Since there are n
distinguishable states in any DFA for Ln
, at least n
states are necessary.
L1 = Σ*
(accepts everything)L2 = {w | |w| is even}
L3 = {w | |w| ≡ 0 (mod 3)}
L4 = {w | w ends with ab
or has even length}
For L3 = {w | |w| ≡ 0 (mod 3)}
, why exactly 3 states?
ε
, a
, aa
are all distinguishable:ε
+ "" → accept (length 0 ≡ 0 mod 3)a
+ aa
→ accept (length 3 ≡ 0 mod 3)aa
+ a
→ accept (length 3 ≡ 0 mod 3)a
+ "" → reject, aa
+ "" → reject, ε
+ a
→ rejectSince we have 3 distinguishable strings, we need at least 3 states. Our construction uses exactly 3, so it's minimal.
L2n = {w | w has
a
in nth position from end}
a
in 2nd-to-last position" → needs 4 statesa
in 3rd-to-last position" → needs 8 statesa
in 4th-to-last position" → needs 16 statesEach language requires exactly 2ⁿ states because the DFA must remember the last n symbols to determine if the nth-to-last was a
.
Let M1
and M2
be minimal DFAs with n
and m
states, respectively. The following bounds on state complexity apply:
sc(L(M1) ∪ L(M2)) ≤ nm
, and this bound is tightsc(L(M1) ∩ L(M2)) ≤ nm
, and this bound is tightsc(Σ* - L(M1)) = n
sc(L(M1)·L(M2)) ≤ n·2m - 2m-1
for n > 1, m > 1
sc(L(M1)*) ≤ 2n - 1
for n > 1
sc(L(M1)R) ≤ 2n
, and this bound is tightHere, sc(L)
denotes the state complexity of L
, i.e., the number of states in the minimal DFA recognizing L
.
For a DFA M = (Q, Σ, δ, q0, F)
with |Q| = n
and |Σ| = k
:
w ∈ L(M)
has time complexity O(|w|)
and space complexity O(log n)
O(n²k)
and space complexity O(n²)
L(M1) ∪ L(M2)
or L(M1) ∩ L(M2)
has time complexity O(n·m·k)
where |Q1| = n
and |Q2| = m
Σ* - L(M)
has time complexity O(1)
L(M1) = L(M2)
has time complexity O((n+m)²·k)
The following decision problems for DFAs are decidable:
M
, is L(M) = ∅
?M
, is L(M) = Σ*
?M1
and M2
, is L(M1) = L(M2)
?M1
and M2
, is L(M1) ⊆ L(M2)
?M
, is L(M)
finite?All these problems can be decided in polynomial time.
We prove that the emptiness problem for DFAs is decidable in linear time.
Given a DFA M = (Q, Σ, δ, q0, F)
, L(M) = ∅
if and only if no accepting state is reachable from the start state.
Algorithm:
q0
.Correctness: The search explores all states reachable from q0
. If an accepting state is reachable, there exists a string that leads from q0
to that accepting state, which means L(M)
is not empty. Conversely, if no accepting state is reachable, then no string can lead from q0
to an accepting state, so L(M)
is empty.
Time Complexity: BFS or DFS has time complexity O(|Q| + |δ|) = O(|Q| + |Q|·|Σ|) = O(|Q|·|Σ|)
, which is linear in the size of the DFA representation.
Consider DFA M
with states {q₀, q₁, q₂}
, start state q₀
, accept state {q₂}
:
δ(q₀,a) = q₁
, δ(q₁,b) = q₂
, δ(q₂,a) = q₀
, δ(q₂,b) = q₀
δ(q₀,b)
and δ(q₁,a)
are undefined (go to reject state)Reachability Analysis: q₀ → q₁
(on a
) → q₂
(on b
) ✓
Since q₂
is reachable from q₀
, L(M) ≠ ∅
. In fact, L(M)
contains ab
.
Test if L = {w | w contains
is universal over alphabet a
}{a,b}
:
a
a
a
? Yes! (e.g., b
)b
)Are these languages equivalent?
L1 = {w | w starts with a
}
L2 = {w | w starts with a
and |w| ≥ 1}
ε
(empty string)a
, ab
, aa
, ... ✓ but not ε
or b
(L₁ - L₂) ∪ (L₂ - L₁) = ∅
Is L = {aⁿbⁿ | n ≥ 0}
finite? Wait... this isn't regular!
Better example: Is L = {w | |w| ≤ 3}
finite?
{a,b}
The class of languages recognized by DFAs is closed under the following operations:
Given DFAs M1 = (Q1, Σ, δ1, q1, F1)
and M2 = (Q2, Σ, δ2, q2, F2)
, we construct a DFA M = (Q, Σ, δ, q0, F)
for L(M1) ∪ L(M2)
:
Q = Q1 × Q2
(Cartesian product)q0 = (q1, q2)
F = {(r₁, r₂) | r₁ ∈ F₁ or r₂ ∈ F₂}
δ((r1, r2), a) = (δ1(r1, a), δ2(r2, a))
This construction simulates both machines in parallel, accepting if either would accept.
For intersection, we use the same construction as union but with a different acceptance condition:
F = {(r₁, r₂) | r₁ ∈ F₁ and r₂ ∈ F₂}
The rest of the construction (Q, q0, δ
) remains identical to the union case.
Given a DFA M = (Q, Σ, δ, q0, F)
, we construct a DFA M' = (Q, Σ, δ, q0, Q-F)
that recognizes Σ* - L(M)
:
F' = Q - F
(accept states become non-accept and vice versa)This machine accepts precisely the strings that M
rejects, and rejects the strings that M
accepts.
Given DFAs M1 = (Q1, Σ, δ1, q1, F1)
and M2 = (Q2, Σ, δ2, q2, F2)
, we construct a DFA M = (Q, Σ, δ, q0, F)
for L(M1)·L(M2)
:
Q = Q1 × 2Q2
(Cartesian product of Q1 and power set of Q2)q0 = (q1, S0)
where S0 = {q₂}
if q1 ∈ F1
, otherwise S0 = ∅
F = {(r, S) | S ∩ F₂ ≠ ∅}
(accept if any state in S is accepting in M2)δ
, for each (r, S) ∈ Q
and a ∈ Σ
:r' = δ1(r, a)
(next state in M1)S' = {δ₂(s, a) | s ∈ S}
(next states in M2 from all current states)r' ∈ F1
, add q2
to S'
(start M2 if M1 accepts)δ((r, S), a) = (r', S')
This construction keeps track of: 1. The current state in M12. All possible states in M2 that might be active 3. It adds the start state of M2 whenever M1 reaches an accepting state
Given a DFA M = (Q, Σ, δ, q0, F)
, we construct a DFA M' = (Q', Σ, δ', q0', F')
for L(M)*
:
Q' = 2Q
(the power set of Q)q0' = E({q₀})
where E(S) is the ε-closure of S (states reachable from S by ε-transitions in the NFA construction)F' = {S ⊆ Q | S ∩ F ≠ ∅ or S = E({q₀})}
(accept if any state in S is accepting or if S is the initial state set)δ'
, for each S ⊆ Q
and a ∈ Σ
:δ'(S, a) = E({δ(q, a) | q ∈ S})
E(T) = T ∪ {q₀ | T ∩ F ≠ ∅}
(add start state if any state in T is accepting)This construction simulates the standard NFA construction for Kleene star (which adds an ε-transition from accept states back to the start state) directly as a DFA using the subset construction.
For a language L ⊆ Σ*
, define the Myhill-Nerode relation ≡L
by:x ≡L y
if and only if for all z ∈ Σ*
,xz ∈ L ⟺ yz ∈ L
.
Theorem: The following are equivalent:
L
is regular (recognizable by some DFA)≡L
has finitely many equivalence classes≡L
equals the number of states in the minimal DFA for L
(1 ⇒ 2): Suppose L
is recognized by a DFA M = (Q, Σ, δ, q0, F)
with n
states.
Define f: Σ* → Q
by f(x) = δ̂(q0, x)
. If f(x) = f(y)
, then for any z ∈ Σ*
:
δ̂(q0, xz) = δ̂(f(x), z) = δ̂(f(y), z) = δ̂(q0, yz)
Therefore xz ∈ L ⟺ yz ∈ L
, so x ≡L y
. Since |Q| = n
, there are at most n
equivalence classes.
(2 ⇒ 1): Suppose ≡L
has k
equivalence classesC1, C2, ..., Ck
. Construct DFA M = (Q, Σ, δ, q0, F)
where:
Q = {C₁, C₂, ..., Cₖ}
q0 = Ci
where ε ∈ Ci
F = {Cᵢ : Cᵢ ∩ L ≠ ∅}
δ(Ci, a) = Cj
where for any x ∈ Ci
, xa ∈ Cj
This construction is well-defined because if x, y ∈ Ci
then x ≡L y
, which implies xa ≡L ya
for any a
.
(3): The minimal DFA has exactly one state per equivalence class by construction, and no further reduction is possible since distinct equivalence classes are distinguishable.
L = {w ∈
{a,b}*
| w ends with ab
}
Two strings x
and y
are equivalent if xz ∈ L ⟺ yz ∈ L
for all z
.
ε
, b
, bb
, ba
, aba
, ... – strings not ending in a
a
, aa
, baa
, abaa
, ... – strings ending in a
but not ab
ab
, aab
, bab
, abab
, ... – strings ending in ab
ab
to get into L
b
to get into L
L
, stay in L
with any extension ending ab
Since there are exactly 3 equivalence classes, the minimal DFA has exactly 3 states. This matches our earlier construction!
For minimal DFAs M1
and M2
withm
and n
states respectively, the following bounds are tight:
sc(L1 ∪ L2) = mn
in the worst casesc(L1 ∩ L2) = mn
in the worst casesc(L1L2) = m·2n - 2n-1
in the worst casesc(L1*) = 2m-1 + 1
in the worst casesc(L1R) = 2m
in the worst caseThese bounds are achieved by specific witness languages over binary alphabets.
We construct languages L1
and L2
whose union requires exactly mn
states.
Let L1 = {w ∈ {a,b,c}* | |w|a ≡ 0 (mod m)}
(number of a
is divisible by m
).
Let L2 = {w ∈ {a,b,c}* | |w|b ≡ 0 (mod n)}
(number of b
is divisible by n
).
The minimal DFA for L1
has m
states (counting a
mod m
). The minimal DFA for L2
has n
states (counting b
mod n
).
For L1 ∪ L2
, consider strings ai bj
for0 ≤ i < m, 0 ≤ j < n
. Each such string reaches a different state in the product construction, and all mn
states are reachable and distinguishable.
To see they are distinguishable: ai bj
and ai' bj'
with (i,j) ≠ (i',j')
can be distinguished by appendingam-i
or bn-j
appropriately.
Consider:
L1 = {w | |w| is even}
– requires 2 statesL2 = {w | w contains a
}
– requires 2 statesTheir union L1 ∪ L2
= even length OR contains a
requires 4 states:
a
seena
seena
seena
seenConsider L = {w | w has
:a
in 3rd position from end}
L*
requires exponentially more statesA two-way deterministic finite automaton (2DFA) is a 6-tuple(Q, Σ, δ, q0, F, ⊢, ⊣)
where:
Q, Σ, q0, F
are defined as in a standard DFA⊢, ⊣
are special left and right endmarker symbols not in Σ
δ: Q × (Σ ∪ {⊢, ⊣}) → Q × {-1, 0, +1}
is the transition functionIf δ(q, a) = (q', d)
, the automaton moves to state q'
and moves the input head in direction d
(-1 for left, 0 for no movement, +1 for right).
For any 2DFA, there exists an equivalent DFA that recognizes the same language. Moreover:
cn
states for some constant c
A k-track DFA reads input on k parallel tracks simultaneously. Formally, it is a DFA over the alphabet Σk
where each input symbol is a k-tuple (a1, a2, ..., ak)
with ai ∈ Σ
.
Multi-track DFAs are useful for modeling computations that need to track multiple aspects of the input simultaneously, such as:
Design a 2DFA to check if a string is a palindrome. While palindromes aren't regular, we can show how a 2DFA would approach this problem before proving it's impossible.
This 2DFA approach fails because:
A 2-track DFA can easily check if two strings are identical:
a b a b
a b a c
The computational complexity of fundamental DFA decision problems:
M
and string w
, is w ∈ L(M)
? - O(|w|)
time, O(log |Q|)
spaceM
, is L(M) = ∅
? - O(|Q| + |δ|)
timeM
, is L(M) = Σ*
? - O(|Q| + |δ|)
timeM1, M2
, is L(M1) = L(M2)
? - O((|Q1| + |Q2|)²)
timeM1, M2
, is L(M1) ⊆ L(M2)
? - O(|Q1| × |Q2|)
timeM
, compute minimal equivalent DFA - O(|Q|² × |Σ|)
timeTo test if L(M) = Σ*
for DFA M = (Q, Σ, δ, q0, F)
:
Algorithm: L(M) = Σ*
if and only if L(M̄) = ∅
, where M̄
is the complement DFA.
M̄ = (Q, Σ, δ, q0, Q - F)
(flip accept states)L(M̄)
is empty using reachability from q0
L(M̄) = ∅
, otherwise "not universal"Correctness: L(M) = Σ*
⟺ Σ* - L(M) = ∅
⟺ L(M̄) = ∅
Time Complexity: O(|Q| + |δ|)
for the reachability analysis.
To test if two DFAs M1
and M2
are equivalent:
(L1 - L2) ∪ (L2 - L1)
L1 = L2
These polynomial bounds make DFA operations practical even for large automata.
For any regular language, there exists a unique minimal DFA (with the fewest possible states) that recognizes it. This minimal DFA can be constructed by merging equivalent states in a larger DFA. Two states are equivalent if they behave identically for all possible input strings - either both lead to acceptance or both lead to rejection.
A standard method for DFA minimization is the table-filling algorithm, which marks distinguishable pairs of states. Initially, final vs non-final state pairs are marked. Then, iteratively mark any pair (p,q)
where δ(p,a)
and δ(q,a)
lead to already-distinguished pairs. Finally, merge the remaining unmarked pairs.
Consider a 5-state DFA with states {q₀, q₁, q₂, q₃, q₄}
where q0
is the start state and {q₂, q₄}
are accepting states:
State | δ(q, a) | δ(q, b) |
---|---|---|
→ q0 | q1 | q3 |
q1 | q2 | q4 |
q2 (accept) | q2 | q4 |
q3 | q4 | q3 |
q4 (accept) | q2 | q4 |
Minimization Steps:
(q0,q2), (q0,q4), (q1,q2), (q1,q4), (q3,q2), (q3,q4)
(q2,q4)
, check if δ(q2,a)
and δ(q4,a)
lead to a marked pair. They both lead to q2
, so this isn't marked. Check δ(q2,b)
and δ(q4,b)
: both lead to q4
, so this isn't marked either. Leave (q2,q4)
unmarked.(q0,q1)
, check if δ(q0,a)=q1
and δ(q1,a)=q2
lead to a marked pair. Yes, (q1,q2)
is marked, so mark (q0,q1)
.(q0,q3)
, check if δ(q0,a)=q1
and δ(q3,a)=q4
lead to a marked pair. Yes, (q1,q4)
is marked, so mark (q0,q3)
.(q1,q3)
, check if δ(q1,a)=q2
and δ(q3,a)=q4
lead to a marked pair. Since (q2,q4)
is unmarked, we don't mark (q1,q3)
yet. But δ(q1,b)=q4
and δ(q3,b)=q3
lead to a marked pair (q3,q4)
, so mark (q1,q3)
.(q2,q4)
. We merge these states to get a minimized 4-state DFA.The Myhill-Nerode theorem provides a theoretical foundation for DFA minimization. It establishes that states can be merged if they are indistinguishable by any future input. You can explore this concept interactively in the Regular Language Properties section, where we provide a Myhill-Nerode visualizer.
Q, Σ, δ, q0, F
) where each state has exactly one transition for each symbolδ
): Maps each state-symbol pair to a next stateδ̂
): Extends δ
to handle strings instead of just symbolsL(M) = {w ∈ Σ* | δ̂(q₀, w) ∈ F}