Discrete Mathematics and Combinatorics

Finite State Machines as Mathematical Objects

See how a few states and a transition rule define a machine, what it can recognize, and a proof of what it can never do.

Key takeaways

  • A deterministic finite automaton consists of states, an input alphabet, a transition function, a start state and a set of accepting states.
  • The strings a finite automaton accepts form a regular language, the same family described by regular expressions.
  • Allowing several possible moves at once adds convenience but no extra recognizing power.
  • A finite machine cannot check that a string has equally many 0s followed by 1s, because that requires unlimited memory.
  • Every regular language has a smallest deterministic machine, unique apart from the names of its states.

A finite state machine is a system that is always in one of a fixed number of states and moves between them according to a rule as it reads input, one symbol at a time. As a mathematical object it is just a few sets and a function. That simplicity lets you prove exactly what such a machine can do and what it never can.

A turnstile is the standard picture. It is either locked or unlocked. A coin unlocks it, and a push while unlocked lets one person through and locks it again. Pushing while locked does nothing. Two states and two inputs describe its entire behavior.

The formal definition

A deterministic finite automaton, or DFA, has five parts:

Part Meaning
Q a finite set of states
Σ a finite alphabet of input symbols
δ a transition function giving the next state from a state and a symbol
q₀ the start state, a member of Q
F the set of accepting states, a subset of Q

To run it on a string, begin in q₀ and apply δ once for each symbol. If you finish in a state belonging to F, the string is accepted. The set of all accepted strings is the language of the machine.

"Deterministic" means δ gives exactly one next state for every state and symbol. There is never a choice and never a gap.

A machine that tests divisibility by 3

Feed a binary number into a machine, most significant digit first. Can it tell whether the number is divisible by 3 without storing the number?

Reading one more bit b turns a value m into 2m + b. So only the remainder on division by 3 matters, and there are just three. Use states 0, 1 and 2, start in 0, accept in 0, and set

δ(r, b) = remainder of (2r + b) divided by 3
State Read 0 Read 1
0 0 1
1 2 0
2 1 2

Run 110, which is six. Start in 0, read 1 and go to 1, read 1 and go to 0, read 0 and stay in 0. Accepted. Run 101, which is five: 0 to 1, to 2, to 2. Rejected.

The machine handles numbers millions of bits long with the same three states. (As defined, it also accepts the empty string, which you can read as the value zero.) The idea rests on remainder arithmetic of the kind used in check digits.

Designing a machine step by step

Suppose you want to accept every string of 0s and 1s that contains 101 somewhere. A reliable method is to let each state record how much of the target you have matched so far.

  1. State s0 means no useful progress.
  2. State s1 means the string so far ends in 1.
  3. State s2 means it ends in 10.
  4. State s3 means 101 has appeared, and it stays that way forever.
State Read 0 Read 1
s0 s0 s1
s1 s2 s1
s2 s0 s3
s3 s3 s3

Start in s0 and accept only in s3. The subtle entries are the failures. In s1, reading another 1 does not lose everything, because the new 1 can itself begin a match. In s2, reading 0 gives a string ending in 100, and no ending of that is a beginning of 101, so you return to s0.

Test the string 1101. The states are s1, s1, s2, s3, so it is accepted. For 1001 they are s1, s2, s0, s1, so it is rejected.

Text search tools use refinements of exactly this construction. Once the table is built, each input character costs one lookup, and the machine never needs to back up in the text.

Choice without extra power

A nondeterministic finite automaton may have several possible next states, or none, for a given symbol. It accepts a string if at least one sequence of choices ends in an accepting state.

For example, "strings whose third symbol from the end is 1" is easy to describe nondeterministically: loop while reading, then guess that this 1 is the special one and count two more symbols. Four states suffice. A deterministic machine must remember the last three symbols, which takes eight.

Yet the two kinds recognize exactly the same languages. The trick, called the subset construction, is to build a deterministic machine whose states are sets of states of the nondeterministic one, tracking every place you could be. With n original states there are at most 2ⁿ sets. The set is finite, so the result is a legitimate DFA, though it can be exponentially larger. Michael Rabin and Dana Scott introduced this line of thought in a 1959 paper.

Regular languages

A regular expression builds string patterns from single symbols using three operations: choice (this or that), sequence (this then that) and repetition (zero or more times). Stephen Kleene showed in the 1950s that the languages these expressions describe are precisely those finite automata accept.

Regular languages are well behaved. If you swap accepting and non-accepting states in a DFA, you get a machine for the complement. Running two machines side by side, with pairs as states, gives machines for the intersection or union. This is the set theory of unions, intersections and complements applied to languages.

Many questions about them can be decided mechanically. Does a machine accept any string at all? Check whether an accepting state can be reached from the start, which is a graph search. Do two machines accept the same language? Build a machine for the strings on which they disagree and check that it is empty.

What a finite machine cannot do

Consider strings of some number of 0s followed by exactly the same number of 1s: 01, 0011, 000111 and so on. No finite automaton accepts precisely these.

Suppose one did, with n states. Feed it the strings of 0, 00, 000, up to n + 1 zeros. There are n + 1 inputs and only n states, so by the pigeonhole principle two different counts, say i and j, leave it in the same state. From then on it cannot tell them apart. If you now append i ones, it must either accept both strings or reject both. But one should be accepted and the other should not. That is a contradiction.

Proof that matching 0s with 1s needs unlimited memory
1 Assume n states Suppose some finite machine does the job. 2 Try n + 1 inputs Read different numbers of 0s. 3 Two must collide Counts i and j end in the same state. 4 Append i ones Both get the same verdict, but only one is correct. Assume n states Suppose some finite machine does the job. 1 Try n + 1 inputs Read different numbers of 0s. 2 Two must collide Counts i and j end in the same state. 3 Append i ones Both get the same verdict, but only one is correct. 4

The related tool taught in many courses, the pumping lemma, packages the same argument: a long enough accepted string visits some state twice, so the loop between the visits can be repeated.

The smallest machine

Call two prefixes equivalent for a language if every possible continuation gives the same verdict for both. John Myhill and Anil Nerode showed that a language is regular exactly when there are finitely many classes of this kind. The number of classes equals the number of states in the smallest DFA, and any two smallest DFAs differ only in the names of states.

For divisibility by 3 the three remainders can be told apart by suitable continuations, so three states are the minimum. There are efficient procedures that shrink any DFA to this form by merging states that no continuation can distinguish.

Counting with the transition graph

Draw states as points and transitions as arrows, and the machine becomes a directed graph. Accepted strings of length n correspond to walks of length n from the start to an accepting state.

So you can count them with matrix powers, as in counting walks with adjacency matrices. For binary strings with no two 1s in a row, a machine with a "just saw 1" state does the checking, and the counts for lengths 1, 2 and 3 are 2, 3 and 5, continuing as Fibonacci numbers.

Variations and where they appear

Machines can produce output. In a Moore machine the output depends on the state, and in a Mealy machine it depends on the state and the current input. Add random transitions and you have a Markov chain with finitely many states.

Finite machines model traffic light controllers, vending machines, network protocols and the first stage of a compiler, which splits source text into words. Give the machine an unlimited tape to write on and you reach Turing machines, which are far more powerful and far harder to analyze.

Common mistakes

  • Leaving a transition out. A DFA needs an arrow for every symbol from every state. If some input should doom the string, send it to a "trap" state that never leaves and does not accept.
  • Forgetting the empty string. Whether the start state is accepting decides whether the empty string belongs to the language.
  • Assuming every infinite language is irregular. The set of strings containing 101 is infinite and regular. What matters is how much must be remembered, not how many strings there are.
  • Reading nondeterminism as randomness. A nondeterministic machine accepts if some sequence of choices works. No probabilities are involved.
  • Trusting intuition about size. Reversing a language keeps it regular, but the smallest deterministic machine for the reverse can be far larger than the original.

Common questions

Is a real computer a finite state machine?

Strictly, a computer with fixed memory has finitely many states. The number is so astronomically large that models with unlimited memory are usually more informative.

Can a finite machine check balanced parentheses?

Not to unlimited depth, for the same reason as the 0s and 1s. With a fixed maximum depth it can, using one state per depth plus a failure state.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic