Theory of Computation · DFA Construction
Constructing DFA for Strings of Length at Least 2
Overview
A DFA is constructed over the alphabet 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: .
- Language: — all strings over of length at least 2.
- Infinite language: 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 is regular. - State meaning:
- State (start state) represents all strings of length 0 (just ).
- State represents all strings of length 1: reading or from reaches .
- State represents all strings of length 2 or more: from , reading then , then , then , or then all give strings of length 2.
- Final state: 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 is not yet a DFA.
- Self-loops on : once a string of length 2 has been seen (state ), one more or makes the length , so it must still be accepted; hence loops to itself on both symbols. 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
- Identify the alphabet: .
- Identify the language: .
- Enumerate some strings: length 2 — ; length 3 — , and so on.
- Note the language is infinite.
- Build the skeleton from the smallest strings, then decide the remaining transitions.
- Assign meanings to states: = length 0, = length 1, = length 2.
- Mark as the final (accepting) state.
- Check DFA validity: transitions on and are defined for and , but none for — so it is not yet a DFA.
- Complete the transition for : from on go to , and from on go to (self-loops).
- The final DFA has three states, with as the final state.
Formal specification:
- States
- Input alphabet
- Start state
- Accepting (final) states
- Transition function :
| State | a | b |
|---|---|---|
| A (start) | B | B |
| B | C | C |
| C (final) | C | C |
Drawing the diagram…
Formulas
-
— the language accepted: all strings over whose length is at least 2.
-
— from the start state, one symbol gives a string of length 1.
-
— from length 1, one more symbol gives length 2.
-
— from length 2 or more, one more symbol keeps length .
Worked examples
- "": . Ends in (non-final) → rejected (length 1 < 2). Correct.
- "": . Ends in → rejected. Correct.
- "": . Ends in (final) → accepted (length 2). Correct.
- "": . Accepted. Correct.
- "": . Accepted. Correct.
- "": . Accepted. Correct.
- "": . Ends in → accepted (length 3 2). Correct.
- "": . Accepted. Correct.
Comparison
| Language condition | Accepting state(s) | Behaviour after length 2 |
|---|---|---|
| Length exactly 2 | only (no self-loop on ) | Not defined / must go to a dead state (to keep it a DFA) |
| Length at least 2 | Self-loop on on and (stays accepting) |
Revision summary
- and is infinite, yet regular — a DFA exists for it.
- State meanings: = length 0, = length 1, = length 2.
- Start state ; final state ; has self-loops on both and .
- A valid DFA must define a transition for every state on every alphabet symbol — the skeleton without '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", 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