Constructing an NFA for “0+ then odd number of 1s then 2*” over Σ = {0,1,2}
We want an NFA over that accepts exactly the strings of the form Concretely: the string must start with one or more s, then have an odd number of s, and may end with any number of s (including none).
A helpful regex-like characterization is: We’ll encode the “odd number of 1s” requirement by tracking parity using NFA states. This is a standard regular-language pattern, implemented either via DFA parity states or via NFA nondeterminism; here we’ll explicitly build the NFA.
Key ideas:
- NFA: transitions can be nondeterministic.
- Accepting state.
- Start state.
- Epsilon transition.
Language decomposition
Let’s view an accepted string as:
- Prefix: (at least one )
- Middle: odd number of s (i.e., )
- Suffix: (any number of s)
We will ensure the NFA does not allow:
- ending the string before the odd-number-of-s portion is completed,
- reading any symbol outside the required structure (e.g., no after the middle begins).
Step-by-step NFA construction
- 1Step 1
Use states for (i) being in the required 0+ prefix, (ii) tracking parity of the number of 1s read so far, and (iii) allowing only 2s after the odd-parity condition is satisfied.
- 2Step 2
From the start, require at least one 0: after reading the first 0 you can remain in the 0-region on further 0s. On symbol 1, you may enter the 1-region only after at least one 0 was seen. Disallow 2 in this prefix.
- 3Step 3
Track parity: after reading an even number of 1s you are not allowed to accept; after reading an odd number you are allowed to accept (or continue with 2s). Reading a 1 toggles parity (even <-> odd).
- 4Step 4
Once you are in the 'odd number of 1s' state, allow reading any number of 2s while staying in an accepting configuration. Disallow any 0 after the middle begins.
- 5Step 5
Accept exactly when the NFA can end in the 'odd parity' state after consuming the entire input (2* may have been read).
- 6Step 6
Verify three cases: (i) strings with fewer than 1 zero are rejected, (ii) strings with even number of 1s are rejected, (iii) strings that have extra symbols in wrong positions are rejected.
Proposed NFA (explicit construction)
Let the NFA be where:
- Start state:
- Accepting states:
Interpretation of states:
- : before reading the required first (so empty or leading nonzero is impossible to accept)
- : have read at least one , still in the prefix
- : have read an even number of s (and already transitioned out of the 0-prefix)
- : have read an odd number of s; from here you may read and acceptance depends on ending in
Now define as a set of transitions (for each state and symbol):
Prefix transitions (enforcing )
- From :
- From :
- (keep consuming leading zeros)
- (on first , parity becomes even? careful: we start counting 1s here)
- (no 2 allowed before 1s)
We must be consistent with parity:
- If we enter the “1-region” on reading the first , then the number of s becomes which is odd, not even. So we should set:
Parity transitions among s
- From :
- (even -> odd after reading another 1)
- From :
- (odd -> even)
- (2* allowed after odd number of 1s)
Allow 2* suffix
- Already captured by
- Note: has no outgoing transition on , so if you’ve read an even number of s, you cannot “skip” to trailing s.
Putting it all together, the transition function is:
| State | on 0 | on 1 | on 2 |
|---|---|---|---|
This is actually a DFA (no nondeterministic branching needed), but it is a valid NFA since DFA is a special case.
Why it matches the specification
- Begins with one or more 0s: The only way to leave is by reading a , and acceptance is impossible unless you reach later.
- Odd number of 1s: Parity is tracked between and ; each 1 toggles parity. is accepting.
- Ends with any number of 2s: From , you can loop on staying in , so trailing s don’t change acceptance.
- No extra 0s after entering the 1/2 phase: and have no transitions on .
type="tip" title="Pro Tip: parity states are the cleanest way to enforce “odd number of 1s”" content="Model the middle as a 2-state parity machine: even () vs odd (). Then restrict symbol classes per phase (no 0 after starting ones; 2 only allowed after odd)."
type="warning" title="Common pitfall: allowing 2* too early breaks the “odd number of 1s” constraint" content="If you add transitions on 2 from the even-parity state (), then inputs with even # of 1s followed by 2s would incorrectly be accepted. Our construction prevents this by having ."
Construction roadmap
Enforce 0+
Phase 1Only leave the start on symbol 0; loop on 0 to guarantee at least one 0 exists."
Enter 1-region and track parity
Phase 2The first 1 moves into odd-parity state; each subsequent 1 toggles parity."
Attach 2* after odd 1s
Phase 3Allow 2-loop only from the odd-parity accepting state; disallow 2 from even-parity state."
Verify rejects
Phase 4Test: missing zeros, even # of ones, and misplaced symbols (like 0 after ones)."
Quick checks and edge cases
Acceptance conditions by final state (conceptual)
If the input ends in q_O after consuming all symbols, the string is accepted; q_E or q_0 are rejecting.
Knowledge Check
In the construction, which state(s) should be accepting to enforce an odd number of 1s?