Running a DFA
Feed a string to a three-state machine and watch it decide, one symbol at a time.
Skip to the animationA deterministic finite automaton is a machine with a fixed, finite number of states that reads a string once, left to right, and — with no choices and no going back — ends in a state that says yes or no.
What makes it deterministic
A DFA is five things: a set of states, an alphabet, a transition function, one start state, and a set of accepting states. The word deterministic is a claim about the transition function alone — from every state, for every symbol, there is exactly one arrow out. Not zero, not two.
That single restriction is what makes a DFA implementable as a while loop over a lookup table. There is never anything to decide, so there is never anything to backtrack over, and the machine reads each character exactly once.
A DFA's memory is its current state and nothing else. With three states it can distinguish exactly three situations — no more, no matter how long the input.
The vocabulary
- Alphabet (Σ)
- The finite set of symbols the machine may read. For everything on this page,
Σ = {a, b}. - String and language
- A string is a finite sequence of symbols from Σ. A language is a set of strings — usually infinite. A machine does not compute a value; it answers one yes/no question about membership in a language.
- State
- The machine's entire memory. Not part of it — all of it. If two different inputs leave the machine in the same state, it can never again tell them apart.
- Transition function (δ)
- Where to go given the current state and the next symbol. For a DFA it is a function: exactly one answer, always. For an NFA it returns a set, possibly empty.
- Accepting state
- Drawn as a double circle. A string is accepted if the machine is in one of these when the input runs out — not if it merely passed through one on the way.
- Trap or dead state
- A non-accepting state with every symbol looping back to itself. Once you fall in you can never get out, so the answer is already no. Diagrams usually omit it and leave the arrow missing instead.
The machine in the animation
The DFA below accepts exactly the strings over {a, b} that end in `ab`. Three states are enough, and each one has a job you can say in a sentence:
| State | Means | On a | On b |
|---|---|---|---|
| q0 | the last symbol was not an a | q1 | q0 |
| q1 | the last symbol was an a | q1 | q2 |
| q2 (accepting) | the last two symbols were ab | q1 | q0 |
Read the table as a claim about suffixes. The machine never stores the string — it stores only the answer to "how much of ab have I just seen?", which has exactly three possible answers. That is why three states suffice for a language with infinitely many strings in it.
- 1Start in q0 with the whole string unread.
- 2Read the next symbol and follow the one arrow that matches it.
- 3Repeat until the input is exhausted.
- 4Accept if and only if the state you stopped in is a double circle.
Why the self-loops matter
Two arrows in this machine point back at the state they left: q0 on b, and q1 on a. Beginners tend to leave these out, and the machine then silently rejects most of its language.
q0 --b--> q0says abat the start gets you no closer toab, but it does not disqualify anything either.q1 --a--> q1saysaais no worse thana— you have still just seen ana, so you are still onebfrom accepting.q2 --a--> q1is the interesting one: having matchedab, reading anotheraloses the match. Acceptance is about where you finish, so passing through q2 earns nothing.
What a DFA cannot do
The classic non-example is aⁿbⁿ — equal numbers of as then bs. No DFA recognises it, and the reason is exactly the finiteness of the state set: to check the count you would need a distinct state for every possible number of as, and there are infinitely many. This is what the pumping lemma formalises.
Advantages and disadvantages
Advantages
- Runs in exactly one pass, O(n) in the length of the input, with constant memory.
- No backtracking and no ambiguity — the implementation is a table lookup.
- Every regular language has a unique smallest DFA, so "is this the best machine?" has a definite answer.
Disadvantages
- Can only recognise regular languages — no counting, no nesting, no matching brackets.
- Often has far more states than the equivalent NFA, and building it by hand is error-prone.
Watch it work
Check yourself
question 1 / 4
One question at a time. Pick an answer to see why it is right or wrong, then move on — there is no score to keep and nothing is saved.