Finite automata: DFA and NFA
A finite automaton reads input one symbol at a time and moves between states, accepting or rejecting. NFAs can always be turned into DFAs.
The idea
A turnstile remembers one fact: locked or open. A coin opens it and a push locks it again.
That is a finite automaton: a fixed set of states and a rule for each input. It needs no memory beyond the state it is in.
Intuition
To check that a binary string ends with 01, remember only your progress. Three states are enough.
q0 means nothing useful yet, q1 means the last symbol was 0, and q2 means the last two were 01. The double circle marks q2 as the accept state.
The input sits on a tape. The machine reads one symbol per step, left to right, and never goes back.
After the last symbol, the answer is the current state. An accept state means yes, any other state means no.
Complexity
A DFA does one lookup per symbol, so it runs in O(n) time with O(1) memory. An NFA tracked as a set costs O(n·k).
The DFA stores only the current state, whatever the input length. The NFA keeps a set of up to k states and follows every arrow from each one.
Pitfalls
Here q2 loops on every symbol, so the machine accepts 0110. It checks 'contains 01', not 'ends with 01'.
An accept state must leave again when more input breaks the property. In the right DFA, q2 goes back to q1 on 0 and to q0 on 1.
A DFA needs one arrow per symbol from every state. A dead state catches inputs that can never be accepted.
Pictures often leave out the dead state. A missing arrow then means 'reject', but code that looks up the arrow must handle the gap.
Variations
Regular expressions describe the same languages. The regex (0|1)*01 matches exactly what this DFA accepts.
Code
Pseudocode
state ← start
for each symbol c of the input
if δ(state, c) is missing: return reject
state ← δ(state, c)
if state is an accept state: return accept
return reject
Python
def run_dfa(delta, start, accept, text):
state = start
for c in text:
if (state, c) not in delta:
return False
state = delta[(state, c)]
if state in accept:
return True
return False
ENDS_01 = {
("q0", "0"): "q1", ("q0", "1"): "q0",
("q1", "0"): "q1", ("q1", "1"): "q2",
("q2", "0"): "q1", ("q2", "1"): "q0",
}
print(run_dfa(ENDS_01, "q0", {"q2"}, "1101"))
JavaScript
function runDfa(delta, start, accept, text) {
let state = start;
for (const c of text) {
const next = delta[state]?.[c];
if (next === undefined) return false;
state = next;
}
if (accept.has(state)) return true;
return false;
}
const ENDS_01 = {
q0: { 0: "q1", 1: "q0" },
q1: { 0: "q1", 1: "q2" },
q2: { 0: "q1", 1: "q0" },
};
console.log(runDfa(ENDS_01, "q0", new Set(["q2"]), "1101"));
Practice problems (5)
Accept or reject Warm-up
This DFA reads binary strings. E is the start state and the only accept state. Run it on the tape 10110.
Does it accept or reject?
Which language is this? Warm-up
The DFA below reads strings of a and b. S is the start state and F is the only accept state. Which set of strings does it accept?
Build a DFA for "ends with 01" Standard
Build a DFA over {0, 1} that accepts exactly the strings ending in 01. Use three states, each remembering how much of 01 the input ends with:
- q0: the input does not end with 0 or 01 (the start state)
- q1: the input ends with 0
- q2: the input ends with 01 (accept)
Match every state and symbol to the state the DFA moves to.
NFA to DFA Standard
The NFA below accepts strings that end with 01. State a loops on 0 and 1, a also goes to b on 0, and b goes to c on 1.
In the subset construction, the DFA state {a, b} reads 1. Which set of NFA states does it go to? Write it like {x, y}.
Run the DFA on 10010 Standard
Run the 'ends with 01' DFA on 10010. Before each marked step, click the state the machine moves to. At the end, say whether it accepts.