Sub topic 13
Decision problems for regular languages
A decision problem asks a yes/no question about a language, and an algorithm must always halt with the right answer. For regular languages, every common question has such an algorithm. This chapter lists the questions, gives the algorithm for each, and shows how to run them in code.
It assumes you know DFAs, NFAs, the subset construction, closure under complement and intersection, and the pumping lemma. The product construction used below comes from the closure-properties chapter.
The standard questions
Let L, L1, and L2 be regular languages given as DFAs, NFAs, or regular expressions. The usual questions are:
- Membership: is the string w in L?
- Emptiness: is L equal to the empty set?
- Finiteness: does L contain finitely many strings?
- Universality: is L equal to Σ*?
- Subset: is L1 contained in L2?
- Equivalence: is L1 equal to L2?
- Disjointness: is the intersection of L1 and L2 empty?
All of these reduce to two basic checks on a DFA. One check is graph reachability, which answers emptiness. The other is cycle detection, which answers finiteness. Closure properties turn the remaining questions into emptiness questions.
Membership
For a DFA, read w one symbol at a time and check whether the final state accepts. This takes O(|w|) steps.
For an NFA, track the set of possible states, as in the earlier NFA simulation. Each step costs at most m² work for m states, so the total is O(|w|·m²). You do not need to build the full DFA, which could be exponentially larger.
For a regular expression, convert it to an ε-NFA with the Thompson construction. The NFA has size linear in the expression, so membership stays polynomial.
Emptiness
L(M) is nonempty exactly when some accepting state is reachable from the start state. Run a breadth-first search over the transition graph and check whether it reaches an accepting state. The cost is O(n·|Σ|) for n states.
An equivalent pumping-style statement: a DFA with n states accepts some string if and only if it accepts one of length less than n. A shortest accepted string never repeats a state, so it cannot be longer than n − 1.
Finiteness
A state is useful if it is reachable from the start and can reach an accepting state. Then:
> L is infinite if and only if the useful states contain a cycle.
A cycle among useful states lets you repeat a loop arbitrarily often and still end in acceptance, which produces infinitely many strings. A cycle that is not useful does not count. The usual example is a self-loop on a dead state. It never leads to acceptance, so it adds no strings.
The pumping-length version, often used in GATE: a DFA with n states has an infinite language if and only if it accepts some string w with n ≤ |w| < 2n.
Equivalence, subset, universality
Build the product DFA of M1 and M2 and mark a pair (p, q) as accepting when exactly one of p and q is accepting. That machine accepts the symmetric difference L1 Δ L2. The two languages are equal if and only if no such pair is reachable.
- Subset: L1 ⊆ L2 if and only if L1 ∩ complement(L2) is empty.
- Universality: L = Σ* if and only if complement(L) is empty. Complementing a DFA requires a complete DFA, so add a dead state first if any transition is missing.
- Disjointness: L1 ∩ L2 = ∅ if and only if the product DFA accepting both has no reachable accepting pair.
There is an alternative test for equivalence. Minimize both DFAs and check whether the results are identical up to renaming of states. The minimal DFA of a regular language is unique, which follows from the Myhill-Nerode theorem.
Working code
The program below decides emptiness, finiteness, and equivalence for DFAs over {a, b}. The equivalence function returns a shortest string on which the two DFAs disagree, or None when they are equivalent.
from collections import deque
ALPHABET = "ab"
def reachable(start, delta):
seen = {start}
queue = deque([start])
while queue:
q = queue.popleft()
for c in ALPHABET:
r = delta[(q, c)]
if r not in seen:
seen.add(r)
queue.append(r)
return seen
def is_empty(dfa):
start, acc, delta = dfa
return not (reachable(start, delta) & acc)
def is_finite(dfa):
start, acc, delta = dfa
reach = reachable(start, delta)
rev = {}
for (q, c), r in delta.items():
rev.setdefault(r, set()).add(q)
co = set(acc)
queue = deque(acc)
while queue:
r = queue.popleft()
for q in rev.get(r, ()):
if q not in co:
co.add(q)
queue.append(q)
useful = reach & co
# Kahn's algorithm: the useful subgraph is acyclic iff every state gets removed.
indeg = {q: 0 for q in useful}
for (q, c), r in delta.items():
if q in useful and r in useful:
indeg[r] += 1
queue = deque(q for q in useful if indeg[q] == 0)
removed = 0
while queue:
q = queue.popleft()
removed += 1
for c in ALPHABET:
r = delta[(q, c)]
if r in useful:
indeg[r] -= 1
if indeg[r] == 0:
queue.append(r)
return removed == len(useful)
def distinguishing_string(d1, d2):
s1, a1, t1 = d1
s2, a2, t2 = d2
start = (s1, s2)
parent = {start: None}
queue = deque([start])
while queue:
p = queue.popleft()
if (p[0] in a1) != (p[1] in a2):
chars = []
while parent[p] is not None:
p, c = parent[p]
chars.append(c)
return "".join(reversed(chars))
for c in ALPHABET:
nxt = (t1[(p[0], c)], t2[(p[1], c)])
if nxt not in parent:
parent[nxt] = (p, c)
queue.append(nxt)
return None
even_a = (0, {0}, {(0, 'a'): 1, (0, 'b'): 0, (1, 'a'): 0, (1, 'b'): 1})
mod4_even = (0, {0, 2}, {**{(i, 'a'): (i + 1) % 4 for i in range(4)},
**{(i, 'b'): i for i in range(4)}})
mod4_zero = (0, {0}, mod4_even[2])
just_a = (0, {1}, {(0, 'a'): 1, (0, 'b'): 2, (1, 'a'): 2, (1, 'b'): 2,
(2, 'a'): 2, (2, 'b'): 2})
print(is_empty(even_a)) # False
print(is_finite(even_a)) # False
print(is_finite(just_a)) # True
print(distinguishing_string(even_a, mod4_even)) # None (equivalent)
print(distinguishing_string(even_a, mod4_zero)) # aaThe 4-state mod4_even counts a's modulo 4 and accepts remainders 0 and 2. It recognizes the same language as the 2-state even_a, and the test confirms this without comparing state counts. The string "aa" separates even_a from mod4_zero: the first DFA accepts it, and the second ends in remainder 2 and rejects.
Cost by input representation
| Problem | Given a DFA | Given an NFA or regular expression | Column | Column |
|---|---|---|---|---|
| Membership | O(\ | w\ | ) | Polynomial (set simulation) |
| Emptiness | Linear, graph search | Polynomial, graph search with no determinization | ||
| Finiteness | Linear, cycle search | Polynomial after removing ε-moves | ||
| Universality | Linear after completing | PSPACE-complete | ||
| Equivalence | O(n1·n2·\ | Σ\ | ) via product | PSPACE-complete |
The last two rows explain why the input form matters. Complementing an NFA needs determinization first, which can cause an exponential blow-up. Universality and equivalence are PSPACE-complete for NFAs and regular expressions, so no polynomial algorithm is expected. For DFAs they are cheap.
Common errors
- Skipping completion. Complement and product tests assume every state has a transition on every symbol. A missing transition silently acts as a dead state, and forgetting to add it gives wrong complements.
- Comparing state counts for equivalence. Two equivalent DFAs can differ in size, as
even_aandmod4_evendo. Compare minimal DFAs, or use the product test. - Testing only the start-to-accept path for finiteness. A cycle on a path to acceptance matters, but a cycle on an unreachable state does not.
- Treating the empty string loosely. A start state that is accepting means ε is in L, so L is nonempty with no search needed.
GATE patterns
Questions in this area usually ask which statement is decidable, which is false, or what bound applies. All the questions in the first section are decidable for regular languages. Questions about undecidability usually involve context-free or recursively enumerable languages, not regular ones. Expect to apply the n ≤ |w| < 2n finiteness bound, the |w| < n emptiness bound, and the rule that equivalence reduces to emptiness of the symmetric difference.
Exercise
A complete DFA M has 5 states, and every accepted string has length at most 6. Could L(M) be infinite? Justify using the finiteness test.
Show answer
No, it must be finite. If L were infinite, M would accept some string of length between 5 and 9 (n ≤ |w| < 2n with n = 5). That string alone is within the allowed bound of 6 only for lengths 5 and 6, so no contradiction arises from one string. The stronger fact is that an infinite language has a reachable cycle that can reach an accepting state, and pumping that cycle gives accepted strings of unbounded length, which contradicts the bound of 6. Hence L is finite.
Exercise
Describe an algorithm that decides whether L1 is a subset of L2 for two DFAs, and name the language whose emptiness it checks.
Show answer
Complete both DFAs, complement M2, and build the product DFA accepting L1 ∩ complement(L2). Run a graph search from the start pair. L1 is a subset of L2 if and only if no accepting pair is reachable, that is, if L1 ∩ complement(L2) is empty.
Exercise
A DFA over {a, b} has a reachable state q that loops to itself on both a and b. No accepting state is reachable from q. Does q alone make the language infinite?
Show answer
No. q is not useful because it cannot reach an accepting state, so its self-loops add no accepted strings. The language is infinite only if a cycle exists among useful states.