DFA Construction for Even # of 0s and Odd # of 1s over
We want a deterministic finite automaton (DFA) over that accepts exactly those strings that contain:
- an even number of 0s, and
- an odd number of 1s.
A standard approach is to build a product-parity automaton that tracks two parities simultaneously: parity of the number of 0s, and parity of the number of 1s.
We represent each state as a pair: where is the parity of the number of 0s read so far, and is the parity of the number of 1s read so far. The DFA starts at because the empty string has 0 zeros (even) and 0 ones (even). It accepts when .
Mermaid view of the core idea:
Key terms:
- DFA
- Alphabet
- Accepting state
- Transition function
Building DFA for Even/Odd Parity (Conceptual)
DFA definition (explicit construction)
We define the DFA as follows.
-
State set:
where means:- = parity of # of 0s so far (e=even, o=odd)
- = parity of # of 1s so far (e=even, o=odd)
-
Alphabet:
-
Start state:
-
Accepting set:
because we need even number of 0s and odd number of 1s. -
Transition function:
Reading a symbol flips the parity of the corresponding count:- On input : toggle parity of # of 0s, leave # of 1s parity unchanged.
- On input : toggle parity of # of 1s, leave # of 0s parity unchanged.
Construct the transition table from the parity rules
- 1Step 1
Create four states q_ee, q_eo, q_oe, q_oo representing (parity of #0s, parity of #1s).
- 2Step 2
On reading 0, swap the first component e<->o while keeping the second component fixed.
- 3Step 3
On reading 1, swap the second component e<->o while keeping the first component fixed.
- 4Step 4
Compute δ(q,s) for each state q and symbol s∈{0,1} using the toggles.
- 5Step 5
Set F={q_eo} because acceptance requires even #0s and odd #1s.
Transition table
Let be the next state.
| Current state | On input 0 | On input 1 |
|---|---|---|
This follows directly from parity toggling:
- flips “#0s parity”
- flips “#1s parity”
Pro Tip
When you see “even/odd number of symbols,” think in terms of parity bits. A DFA for independent parity constraints typically needs states; here , so 4 states.
Common mistake
Do not accept based on just one parity condition. This language requires BOTH: even number of 0s AND odd number of 1s.
Verification: trace accepted vs. rejected strings
We verify using representative strings by counting 0s and 1s and/or simulating the DFA by state transitions.
Strings that should be accepted (even #0s, odd #1s)
We must have:
- #0s = even (0,2,4,...)
- #1s = odd (1,3,5,...)
- Input:
1
- #0s = 0 (even), #1s = 1 (odd) → accept
- DFA trace:
(accepting)
- Input:
001
- #0s = 2 (even), #1s = 1 (odd) → accept
- DFA trace:
(accept)
- Input:
10101
- #0s = 2 (even), #1s = 3 (odd) → accept
- Input:
000111
- #0s = 3 (odd) actually this one would be rejected (included here as a contrast).
So we instead use: Input:00111- #0s = 2 (even), #1s = 3 (odd) → accept
Strings that should be rejected (violates at least one parity condition)
- Input:
0
- #0s = 1 (odd), #1s = 0 (even) → reject
- DFA trace:
(not accepting)
- Input:
11
- #0s = 0 (even), #1s = 2 (even) → reject
- DFA trace:
(back to non-accepting)
- Input:
010
- #0s = 2 (even), #1s = 1 (odd) → actually accept (use as a positive check)
- Therefore for rejection use
0110:0110has #0s=2 (even), #1s=2 (even) → reject
- Input: empty string
ε
- #0s=0 (even), #1s=0 (even) → needs odd #1s, so reject
- DFA trace: start at which is non-accepting.
How the DFA enforces both parity conditions
Initial parities
StartBegin in (0 zeros even, 0 ones even)."
Update #0 parity
Reading 0Toggle the first component (e↔o) to reflect the new count of zeros."
Update #1 parity
Reading 1Toggle the second component (e↔o) to reflect the new count of ones."
Check acceptance
End of inputAccept iff the final state is ."
Quick state interpretation example
Consider input 001:
- Start: .
- Read
0: (odd zeros, even ones). - Read
0: (even zeros, even ones). - Read
1: (even zeros, odd ones) → accept.
This demonstrates the DFA’s correctness mechanism: it never “counts” unboundedly; it only tracks parity.
Additional checks and edge cases
Knowledge Check
Which state(s) should be accepting in the DFA for even number of 0s and odd number of 1s?