1. Gyanastra
  2. Theory of Computation
  3. DFA Construction
  4. Constructing DFA for Strings of Length at Least 2
Free lesson, no account needed 6 min read 12 practice questions after it
Asked in past papers8 times
  • GATE CSE2023202120132012200920051999
  • UGC NET2013

Theory of Computation · DFA Construction

Constructing DFA for Strings of Length at Least 2

Overview

A DFA is constructed over the alphabet {A,B}\{A, B\} that accepts exactly those strings whose length is at least 2. The construction shows how state meanings are assigned from the smallest strings, and why the final state needs self-loops to keep every longer string accepted.

Key concepts

  • Alphabet: Σ={A,B}\Sigma = \{A, B\}.
  • Language: L={ w∈{A,B}∗∣∣w∣≥2 }L = \{\, w \in \{A, B\}^* \mid |w| \ge 2 \,\} — all strings over {A,B}\{A, B\} of length at least 2.
  • Infinite language: LL is infinite.
    A finite language can always be accepted by a finite automaton; for an infinite language it is not known in advance whether a finite automaton exists, but one is built here — so LL is regular.
  • State meaning:
    • State AA (start state) represents all strings of length 0 (just ε\varepsilon).
    • State BB represents all strings of length 1: reading aa or bb from AA reaches BB.
    • State CC represents all strings of length 2 or more: from AA, reading aa then aa, bb then aa, aa then bb, or bb then bb all give strings of length 2.
  • Final state: CC is the accepting state, because strings of length 2 must be accepted.
  • DFA completeness: a DFA must have a transition for every state on every alphabet symbol. The skeleton with no transitions out of CC is not yet a DFA.
  • Self-loops on CC: once a string of length 2 has been seen (state CC), one more aa or bb makes the length ≥2\ge 2, so it must still be accepted; hence CC loops to itself on both symbols. CC therefore represents all strings of length 2 or more — "2" from the two transitions taken to reach it, "more" from the self-loop.

How it works

  1. Identify the alphabet: Σ={A,B}\Sigma = \{A, B\}.
  2. Identify the language: L={ w∈{A,B}∗∣∣w∣≥2 }L = \{\, w \in \{A, B\}^* \mid |w| \ge 2 \,\}.
  3. Enumerate some strings: length 2 — AA,AB,BA,BBAA, AB, BA, BB; length 3 — AAA,BBBAAA, BBB, and so on.
  4. Note the language is infinite.
  5. Build the skeleton from the smallest strings, then decide the remaining transitions.
  6. Assign meanings to states: AA = length 0, BB = length 1, CC = length 2.
  7. Mark CC as the final (accepting) state.
  8. Check DFA validity: transitions on aa and bb are defined for AA and BB, but none for CC — so it is not yet a DFA.
  9. Complete the transition for CC: from CC on aa go to CC, and from CC on bb go to CC (self-loops).
  10. The final DFA has three states, with CC as the final state.

Formal specification:

  • States Q={A,B,C}Q = \{A, B, C\}
  • Input alphabet Σ={A,B}\Sigma = \{A, B\}
  • Start state =A= A
  • Accepting (final) states F={C}F = \{C\}
  • Transition function δ\delta:
Stateab
A (start)BB
BCC
C (final)CC
Figure. State A has seen 0 symbols, B has seen exactly 1, and C has seen 2 or more. Every symbol moves A to B, B to C, and keeps C in C, so a string is accepted exactly when it reaches C. C (the double circle) is the only final state.

Formulas

  • L={ w∈{A,B}∗∣∣w∣≥2 }L = \{\, w \in \{A, B\}^* \mid |w| \ge 2 \,\} — the language accepted: all strings over {A,B}\{A, B\} whose length ∣w∣|w| is at least 2.

  • δ(A,a)=δ(A,b)=B\delta(A, a) = \delta(A, b) = B — from the start state, one symbol gives a string of length 1.

  • δ(B,a)=δ(B,b)=C\delta(B, a) = \delta(B, b) = C — from length 1, one more symbol gives length 2.

  • δ(C,a)=δ(C,b)=C\delta(C, a) = \delta(C, b) = C — from length 2 or more, one more symbol keeps length ≥2\ge 2.

Worked examples

  • "aa": A→aBA \xrightarrow{a} B. Ends in BB (non-final) → rejected (length 1 < 2). Correct.
  • "bb": A→bBA \xrightarrow{b} B. Ends in BB → rejected. Correct.
  • "aaaa": A→aB→aCA \xrightarrow{a} B \xrightarrow{a} C. Ends in CC (final) → accepted (length 2). Correct.
  • "abab": A→aB→bCA \xrightarrow{a} B \xrightarrow{b} C. Accepted. Correct.
  • "baba": A→bB→aCA \xrightarrow{b} B \xrightarrow{a} C. Accepted. Correct.
  • "bbbb": A→bB→bCA \xrightarrow{b} B \xrightarrow{b} C. Accepted. Correct.
  • "aaaaaa": A→aB→aC→aCA \xrightarrow{a} B \xrightarrow{a} C \xrightarrow{a} C. Ends in CC → accepted (length 3 ≥\ge 2). Correct.
  • "bbbbbb": A→bB→bC→bCA \xrightarrow{b} B \xrightarrow{b} C \xrightarrow{b} C. Accepted. Correct.

Comparison

Language conditionAccepting state(s)Behaviour after length 2
Length exactly 2CC only (no self-loop on CC)Not defined / must go to a dead state (to keep it a DFA)
Length at least 2CCSelf-loop on CC on aa and bb (stays accepting)

Revision summary

  • Σ={A,B}\Sigma = \{A, B\} and L={ w∈{A,B}∗∣∣w∣≥2 }L = \{\, w \in \{A, B\}^* \mid |w| \ge 2 \,\} is infinite, yet regular — a DFA exists for it.
  • State meanings: AA = length 0, BB = length 1, CC = length ≥\ge 2.
  • Start state AA; final state CC; CC has self-loops on both aa and bb.
  • A valid DFA must define a transition for every state on every alphabet symbol — the skeleton without CC's transitions is not a DFA.
  • The self-loop on the final state is what converts "exactly 2" into "at least 2".
  • For "length exactly 2", CC has no self-loop and the missing transitions must go to a dead state to keep it a DFA.

Previous-year exam insights

  • The minimum DFA for 'strings of length at least k' has k+1 states (one state for each length 0..k-1, i.e. k states, plus one accepting sink for length ≥ k), regardless of alphabet size;
    the minimum DFA for 'length exactly k' has k+2 states (lengths 0..k plus a dead state).
    Asked inGATE 1999UGC NET 2013
  • For 'ends with a fixed substring w of length n', the minimal DFA has n+1 states (the n+1 prefixes of w), with the accepting state looping on the last symbol pattern; for 'contains w' the minimal DFA has n+1 states too, with a self-loop on the accepting state.
    Asked inGATE 2021GATE 2009
  • For 'no run of k identical symbols' over a binary alphabet, the minimal DFA must remember the last symbol and its current run length (1..k-1), so it needs 1 (start) + 2(k-1) + 1 (dead) = 2k states (for example k = 2, avoiding 00 and 11, needs 4 states);
    a chain of k+1 states works only for a one-letter alphabet.
    Asked inGATE 2023
  • For 'every window of length 3 has at most two 0s' type constraints, the DFA states must remember the last two symbols read (4 states for the last-two-symbol memory) plus a dead state; strings shorter than the window length are accepted by making the initial states accepting.
    Asked inGATE 2012
  • A DFA is complete only if δ is defined for every state–symbol pair; any missing transition must go to a non-accepting dead (trap) state, and the dead state counts toward the state count.
    Asked inGATE 2012GATE 2021
  • Myhill–Nerode: the number of states of the minimal DFA equals the number of equivalence classes of the relation 'x ≡ y iff for all z, xz ∈ L ⇔ yz ∈ L'; use it to prove minimality or to count states.
    Asked inGATE 1999UGC NET 2013
Pages 1–2 of 2