Sub topic 16
GATE problem patterns and strategies
This chapter assumes you know DFAs, NFAs, subset construction, minimization, regular expressions, closure properties, the pumping lemma and the Myhill-Nerode theorem from earlier chapters. It does not teach them again. It shows how GATE CS questions use them and gives a routine for each recurring question type.
The five recurring question types
Most GATE questions on regular languages fall into one of these:
- Counting states of a minimal DFA, or of an NFA.
- Deciding whether two regular expressions denote the same language.
- Deciding whether a language is regular.
- Applying closure or decision properties to a statement ("which of the following is true?").
- Tracing a machine on a string, including Moore and Mealy outputs.
Identify the type first. Then run the routine for that type.
Pattern 1: counting states
Most counting questions reduce to a short list of results. Memorize the list and the reason behind each entry. A bare number is easy to misapply.
| Language over {a, b} | Minimal DFA states | Reason |
|---|---|---|
| Length divisible by n | n | One state per remainder |
| Ends with a fixed string of length k | k + 1 | Track the longest matched prefix |
| Contains a fixed substring of length k | k + 1 | Same, plus an absorbing accepting state |
| Number of a's divisible by m and number of b's divisible by n | m * n | Product of two counters |
| k-th symbol from the end is a | 2^k | The DFA must remember the last k symbols |
| Binary value divisible by an odd n | n | One state per remainder |
The "k-th symbol from the end" row is the standard example of exponential blow-up. Its NFA has k + 1 states, and the minimal DFA has 2^k.
Two details cause lost marks:
- Most GATE questions count a complete DFA, including the dead (trap) state. Check the wording. For "ends with abb" the count is 4 and no dead state exists, because every state has a move on both symbols. For "starts with abb" the count is 5, and the fifth state is the dead state.
- For an NFA, the number of states is a minimum over all NFAs, and it can be much smaller than the DFA. A DFA with n states becomes an NFA for the reversed language with the same n states, but converting that NFA back to a DFA can need up to 2^n states.
The script below checks the table's claims by building a DFA that remembers the last k symbols. It then minimizes the DFA with partition refinement (Moore's algorithm).
def minimize_count(delta, start, accepting, alphabet):
# Keep only the states reachable from the start state.
seen = {start}
stack = [start]
while stack:
s = stack.pop()
for c in alphabet:
t = delta[(s, c)]
if t not in seen:
seen.add(t)
stack.append(t)
# Refine blocks until the partition stops changing.
block = {s: int(s in accepting) for s in seen}
while True:
sig = {s: (block[s], tuple(block[delta[(s, c)]] for c in alphabet))
for s in seen}
ids = {}
new = {s: ids.setdefault(sig[s], len(ids)) for s in seen}
if len(set(new.values())) == len(set(block.values())):
return len(set(new.values()))
block = new
def kth_from_end_is_a(k):
delta = {}
seen = {""}
stack = [""]
while stack:
s = stack.pop()
for c in "ab":
t = (s + c)[-k:]
delta[(s, c)] = t
if t not in seen:
seen.add(t)
stack.append(t)
accepting = {s for s in seen if len(s) == k and s[0] == "a"}
return delta, accepting
for k in range(1, 5):
delta, accepting = kth_from_end_is_a(k)
print(k, minimize_count(delta, "", accepting, "ab")) # 2, 4, 8, 16
mod3 = {(r, c): (r + 1) % 3 for r in range(3) for c in "ab"}
print(minimize_count(mod3, 0, {0}, "ab")) # 3The unminimized DFA for k = 3 has 15 reachable states (all strings of length 0 to 3). Minimization merges the short ones with their padded forms, so "b" and "bbb" become one state, and the count drops to 8.
Pattern 2: equivalence of regular expressions
Do not try to prove equivalence by trial strings alone. Use this order:
- Test the shortest strings: ε, a, b, ab, ba. One difference settles non-equivalence.
- Reduce both sides with standard identities.
- If the two sides still look different, describe each in words ("all strings over {a, b}", "strings with no two consecutive b's") and compare the descriptions.
Identities that appear again and again:
- (a + b) = (ab) = (a + b) = a(ba)
- (r) = r, and ∅ = ε
- rr = r, and (r + ε) = r*
- (a + b) is not equal to a + b*. The right side lacks "ab".
- r(s + t) = rs + rt, but (rs) is not equal to rs*.
Options that differ only in a trailing ε or in nested stars are usually equal. Options that swap the order of concatenation (ab versus ba) usually are not.
Pattern 3: is the language regular?
The traps in this pattern come from the second question in the flowchart:
- Modular conditions are regular. "Number of a's minus number of b's is divisible by 3" needs only three states. "Number of a's equals number of b's" is not regular, because the difference is unbounded.
- Bounded counting is regular. {a^n b^m : n, m ≤ 100} is finite. {a^n b^m : n + m is even} needs two states.
- Comparison between a prefix and a later part is not regular. Examples are {a^n b^n}, {ww}, {w w^R}, {a^(n^2)}, and {a^p : p is prime}.
- {a^n b^m : n is not equal to m} is not regular. Its complement within ab is {a^n b^n}, and regular languages are closed under complement and under intersection with ab.
- The language of strings with equal numbers of the substrings 01 and 10 is regular. In a binary string, the two counts differ by at most 1, so a few states suffice. Check for this kind of hidden bound before reaching for the pumping lemma.
When the pumping lemma is needed, pick the string s = a^p b^p (or similar) so that the first p symbols lie inside the pumpable region. Then choose i = 0 or i = 2 to break the property. Remember that the lemma can only prove non-regularity. It never proves regularity.
Pattern 4: closure and decision statements
"Which statement is true?" questions test the table below. Learn the exceptions as well as the rules.
| Statement | Verdict |
|---|---|
| Regular languages are closed under union, intersection, complement, concatenation, star, reversal, difference | True |
| Closed under homomorphism and inverse homomorphism | True |
| A subset of a regular language is regular | False. Every language is a subset of the regular language Σ*. |
| A union of infinitely many regular languages is regular | False. {a^n b^n} is a union of the single-string languages {a^n b^n}. |
| Union of a regular and a non-regular language is regular | Can be true or false. Check the specific case. |
| A non-regular language has a regular complement | False. Complement preserves regularity in both directions. |
| Emptiness, finiteness, membership, equivalence, universality | All decidable for regular languages |
The product construction for intersection or union of an m-state and an n-state DFA gives at most m * n states. Complementing a DFA means swapping accepting and non-accepting states, and this works only for a complete DFA. Complementing an NFA by the same swap gives a wrong answer.
Pattern 5: tracing machines
A Moore machine attaches an output to each state, so on an input of length n it produces n + 1 output symbols (the first comes from the start state). A Mealy machine attaches an output to each transition, so it produces n output symbols. Count both carefully, because questions often ask for the length of the output.
For ε-NFAs, compute ε-closures before every symbol step and after it. For subset construction, list only the reachable subsets, starting from the closure of the start state.
A strategy for exam time
- Read the question type before any computation. Counting and equivalence questions often have a one-line answer from the tables above.
- With four options, test the shortest string on which the options differ. This eliminates options faster than constructing the machine.
- For numerical answer questions, state whether the dead state is included, then compute.
- Do not spend time on a full minimization when a Myhill-Nerode argument gives the lower bound. If you can name k strings that are pairwise distinguishable by a suffix, the DFA needs at least k states.
Exercise
What is the number of states in the minimal complete DFA over {a, b} that accepts strings starting with "ab"?
Show answer
4. The DFA needs a start state, a state after "a", an accepting state after "ab" (it stays accepting on any input), and a dead state for strings that begin with "b" or "aa".
Exercise
Is L = {w in {0, 1}* : the number of 0s in w is not equal to the number of 1s in w} regular? Justify it.
Show answer
No. The complement of L is the set of strings with equal numbers of 0s and 1s. Intersecting that complement with the regular language 01 gives {0^n 1^n}, which is not regular by the pumping lemma. If L were regular, the complement and the intersection would be regular, which is a contradiction.
Exercise
Are the regular expressions (a + b)a(a + b) and (a + b)*a equal?
Show answer
No. The first denotes all strings containing at least one a. The second denotes strings ending in a. The string "ab" belongs to the first language and not the second.