Sub topic 2
Deterministic finite automata
Prerequisites and scope
This chapter assumes you know what an alphabet, a string, and a language are (the previous chapter). It also assumes basic set notation and the idea of a function from pairs to values. Here you learn the formal definition of a deterministic finite automaton (DFA), how to trace one, how to design one for a given language, and the mistakes that cost marks in GATE.
What a DFA is
A DFA is a machine with a finite number of states that reads a string one symbol at a time. At each symbol it moves to exactly one next state. When the string ends, the machine either accepts or rejects, depending on the state it stopped in.
Formally, a DFA is a 5-tuple (Q, Σ, δ, q0, F):
- Q is a finite set of states.
- Σ is the input alphabet.
- δ: Q × Σ → Q is the transition function. It is total: every state has exactly one move on every symbol.
- q0 ∈ Q is the start state.
- F ⊆ Q is the set of accepting (final) states.
The language of a DFA M, written L(M), is the set of all strings w for which the machine ends in a state of F after reading w.
To talk about whole strings, extend δ to a function δ* on strings:
- δ*(q, ε) = q
- δ(q, wa) = δ(δ(q, w), a)
Then L(M) = { w : δ*(q0, w) ∈ F }.
Two properties follow from this definition:
- Determinism: the run on a given string is unique, so there is never a choice.
- Finite memory: the only thing the machine remembers about the prefix read so far is its current state. Designing a DFA means deciding what information about the prefix is worth remembering.
Tracing a DFA
Take the alphabet {0, 1} and the language of strings with an even number of 1s. Two states are enough: E (even number of 1s so far, start and accepting) and O (odd).
Trace the string 1011:
| Symbol read | State before | State after |
|---|---|---|
| (start) | E | |
| 1 | E | O |
| 0 | O | O |
| 1 | O | E |
| 1 | E | O |
The machine ends in O, which is not accepting, so 1011 (three 1s) is rejected. The empty string leaves the machine in E, so ε is accepted.
Transition tables and the dead state
A DFA is often written as a table, with one row per state and one column per symbol. Mark the start state with an arrow and accepting states with a star.
| State | 0 | 1 |
|---|---|---|
| → *E | E | O |
| O | O | E |
Because δ is total, every cell must be filled. When a language forbids certain strings, you need a dead state (also called a trap state). It is non-accepting and loops to itself on every symbol. Once the machine enters it, the string can no longer be accepted.
Example: strings over {a, b} that start with a. State s is the start. On a go to ok (accepting, loops on both symbols). On b go to dead.
def starts_with_a(w):
# States: "s" (start), "ok" (accepting), "dead" (trap).
delta = {
("s", "a"): "ok", ("s", "b"): "dead",
("ok", "a"): "ok", ("ok", "b"): "ok",
("dead", "a"): "dead", ("dead", "b"): "dead",
}
state = "s"
for ch in w:
state = delta[(state, ch)]
return state == "ok"
print(starts_with_a("abb")) # True
print(starts_with_a("ba")) # False
print(starts_with_a("")) # FalseMany textbook diagrams omit the dead state and its arrows to save space. When a GATE question asks you to count states, check whether the question counts the dead state. The standard formal DFA includes it.
Simulating a DFA in code
Any DFA is a loop over the input with a lookup in δ. A useful general pattern for designing DFAs is to let the state be a summary of the prefix. For divisibility problems, the summary is the remainder.
def divisible_by_3(w):
# DFA states are the remainder (0, 1, 2) of the binary value read so far.
state = 0
for ch in w:
state = (2 * state + int(ch)) % 3
return state == 0
print(divisible_by_3("110")) # 6 -> True
print(divisible_by_3("101")) # 5 -> False
print(divisible_by_3("")) # empty string reads as 0 -> TrueReading bit b after a prefix with value v gives a new value 2v + b. Only v mod 3 matters, so three states suffice. The same idea gives n states for divisibility by n, with transition (r, b) → (2r + b) mod n for binary input.
Designing DFAs: a working method
- Decide what the machine must remember about the prefix. List the possible "situations".
- Make one state per situation. Pick the start state to match the empty prefix.
- Add a transition for every state and symbol. Ask, "If the prefix was in this situation and the next symbol is a, what situation is the new prefix in?"
- Mark as accepting every situation where the prefix itself belongs to the language.
- Check that no cell of the table is empty.
Common design patterns
| Language over {a, b} | What to remember | Minimum states |
|---|---|---|
| Contains the substring `ab` | Not yet seen, last symbol was `a`, already seen | 3 |
| Ends with `ab` | Progress toward `ab` at the end of the prefix | 3 |
| Length is a multiple of n | Length mod n | n |
| Number of `a`s is at least k | Count of `a`s, capped at k | k + 1 |
| Number of `a`s is exactly k | Count of `a`s up to k, plus a dead state | k + 2 |
| Starts with `a` | Start, accepted, dead | 3 |
The "exactly k" row shows why the dead state matters. With counts 0 to k you need k + 1 states, and one more for "more than k".
Product construction for combined conditions
If a language needs two independent conditions at once, run two DFAs in parallel. A state of the combined machine is a pair (p, q). The transition is δ((p, q), a) = (δ1(p, a), δ2(q, a)).
- For intersection (both conditions), accept when both components accept.
- For union (either condition), accept when at least one component accepts.
Example: an even number of as and an even number of bs. Each condition needs 2 states, so the product has 2 × 2 = 4 states. This kind of question appears often with counts such as "number of a's mod 2 and number of b's mod 3", which gives 2 × 3 = 6 states.
Complement
To get the complement of L(M), swap accepting and non-accepting states: F becomes Q \ F. This works only because the DFA is total and deterministic. Every string has exactly one run, so it ends in exactly one state.
Common errors
- Missing transitions. Every state needs exactly one arrow per symbol. A state with two arrows on the same symbol is not a DFA, and a missing arrow means the dead state was left out.
- Forgetting ε. If the start state is accepting, the empty string is in the language. For "number of 1s is even", this is correct. For "ends with ab", it is not.
- Confusing "ends with" and "contains". After seeing
abin "contains", the machine stays accepting forever. In "ends with", it must keep tracking, so an accepting state can go back to a non-accepting one. - Tracking too much. A DFA that remembers the full prefix has unbounded states. If you need unbounded memory, such as matching the number of
as with the number ofbs, no DFA exists. The pumping lemma chapter makes this precise. - Assuming the minimum. A correct DFA is not always the smallest one. Two states are redundant if no suffix distinguishes them. The minimization chapter covers how to merge them.
- Wrong state counts for "at most" and "at least". "At least k
as" needs k + 1 states and no dead state. "At most kas" also needs k + 2 states, because exceeding k leads to a dead state.
Why DFAs matter for the rest of the tutorial
DFAs are the reference model. NFAs and regular expressions turn out to describe exactly the same class of languages, and every conversion in later chapters ends in, or starts from, a DFA. A language is called regular if some DFA accepts it. Because a DFA has finitely many states, it can only count up to a fixed bound, and this limit drives the proofs of non-regularity.
Exercises
Exercise
Design a DFA over {0, 1} that accepts strings whose binary value is divisible by 2 (the empty string counts as 0). How many states does a minimal complete DFA need, and what are the accepting states?
Show answer
Two states are enough. A binary number is even exactly when its last bit is 0, so remember only the last bit. State Z (last symbol was 0, or the string is empty) is the start and accepting state. State N (last symbol was 1) is non-accepting. Reading 0 goes to Z from either state. Reading 1 goes to N from either state. The accepting state set is {Z}.
Exercise
A DFA over {a, b} accepts strings with an even number of as and a number of bs that is a multiple of 3. How many states does the product construction give, and which states are accepting?
Show answer
The a condition needs 2 states (count mod 2) and the b condition needs 3 states (count mod 3). The product has 2 × 3 = 6 states, written as pairs (a mod 2, b mod 3). The start state is (0, 0). Only (0, 0) is accepting, because both conditions must hold. This machine is also minimal, since every pair can be told apart by some suffix.
Exercise
A complete DFA M over {0, 1} has 5 states, 2 of them accepting. How many states does a DFA for the complement of L(M) have, and how many of them are accepting?
Show answer
Swap the accepting and non-accepting sets. The complement DFA has the same 5 states, and 5 − 2 = 3 of them are accepting.