THEORY OF COMPUTATION / 1. DFA — DETERMINISTIC FINITE AUTOMATON
DFA — Deterministic Finite Automaton
The simplest model of computation — states, transitions, accept/reject
EXPLANATION
A Deterministic Finite Automaton (DFA) is the simplest model of computation. Despite its simplicity, it's the mathematical foundation of every regex engine in existence.
Formal definition — a DFA is a 5-tuple (Q, Σ, δ, q0, F):
- Q — finite set of states
- Σ (sigma) — input alphabet (set of allowed symbols, e.g. {0,1} or {a-z})
- δ (delta) — transition function: δ(state, symbol) → next_state. DETERMINISTIC: exactly one next state for every (state, symbol) pair
- q0 — start state (q0 ∈ Q)
- F — set of accept states (F ⊆ Q)
How a DFA processes input:
① Start in state q0
② Read input symbols one by one, left to right
③ For each symbol, follow the transition δ(current_state, symbol) → new_state
④ After reading all input: if current state ∈ F → ACCEPT, else → REJECT
The DFA has NO memory beyond its current state. No stack, no tape. Just which state it's in. This is its fundamental limitation — it cannot count.
What DFAs CAN recognize (Regular Languages):
- Strings ending in "ab": build states tracking last two chars seen
- Strings with even number of 0s: two states (even/odd), toggle on each 0
- Valid binary numbers divisible by 3: 3 states (remainder 0, 1, 2)
- Email-like patterns, identifiers, keywords in programming languages
What DFAs CANNOT recognize:
- aⁿbⁿ (n a's followed by n b's) — needs to COUNT, DFA can't
- Palindromes — needs to remember the whole first half
- Balanced parentheses — needs unbounded counting
These require a stack (Pushdown Automaton) or more.
Dead state: if there's no valid transition for a (state, symbol) pair, the DFA goes to a dead/trap state (non-accepting) and stays there. All remaining input is consumed and the string is rejected.
Extended transition function δ*: δ*(q, ε) = q (empty string keeps you in same state). δ*(q, wa) = δ(δ*(q, w), a). This formally defines what happens on a full string.
The Pumping Lemma for Regular Languages: if L is regular, then strings long enough can be "pumped" (repeated middle section) and still be in L. Used to PROVE a language is NOT regular — find a string where pumping breaks membership. This is how we prove aⁿbⁿ is not regular.DIAGRAM
DFA: Strings over {a,b} containing "ab" as substring
States: q0 (start), q1 (seen 'a'), q2 (seen 'ab') [ACCEPT]
a b
→q0 ──────→ q1 ──────→ q2 (accept)
│ │ │
│b │a │ a,b (stay)
↓ ↓ ↓
q0 q1 q2
Transition table:
State │ a │ b
──────┼──────┼──────
→q0 │ q1 │ q0
q1 │ q1 │ q2
*q2 │ q2 │ q2
Trace "aab":
q0 →(a)→ q1 →(a)→ q1 →(b)→ q2 ✓ ACCEPT
Trace "bba":
q0 →(b)→ q0 →(b)→ q0 →(a)→ q1 ✗ REJECT
DFA: Strings with even number of 0s (over {0,1})
→q_even ←──0──→ q_odd
↑ ↑
└──────1─────────┘ (1s don't change parity)
*q_even is accept stateCODE