maths.free › Discrete Math & Logic › Finite automata and regular languages
Finite automata and regular languages
Deterministic and nondeterministic automata, regular expressions, the subset construction and the pumping lemma.
An alphabet is a finite set of symbols, a string is a finite sequence of them, and a language is any set of strings. A deterministic finite automaton (DFA) reads a string one symbol at a time. It has a finite set of states, a start state, a set of accepting states, and a transition function saying, for each state and symbol, which state comes next. It accepts a string if it finishes in an accepting state. A two-state DFA that flips state on each 1 and stays put on each 0 accepts exactly the binary strings with an even number of 1s.
A DFA is a finite memory, and counting its accepted strings is a recurrence. Take strings with no two consecutive 1s: states "last symbol was 0 (or nothing yet)" and "last symbol was 1", with a 1 after a 1 leading to a dead state. The numbers of good strings ending in each state obey \( s_n = s_{n-1} + s_{n-2} \), so there are \( F_{n+2} \) good strings of length \( n \), and the example computes \( F_{12} = 144 \) for length 10. In general, if \( T \) is the matrix whose \( (i, j) \) entry counts the symbols leading from state \( i \) to state \( j \), then the entries of \( T^n \) count strings of length \( n \) between states.
A nondeterministic automaton (NFA) may have several possible next states, or none, and accepts if some sequence of choices ends in an accepting state. NFAs are often much smaller to write, but they recognise nothing new: the subset construction builds a DFA whose states are sets of NFA states, at most \( 2^n \) of them for an NFA with \( n \) states. Regular expressions build languages from single symbols by union \( (a \mid b) \), concatenation \( ab \) and repetition \( a^* \), and Kleene's theorem says they describe exactly the languages that finite automata accept: the regular languages.
Not every language is regular. The pumping lemma says that for a regular language \( L \) there is a length \( p \) such that every string in \( L \) of length at least \( p \) can be split as \( xyz \) with \( |xy| \le p \), \( y \) non-empty, and \( xy^iz \in L \) for every \( i \ge 0 \). The reason is pigeonhole: a DFA with \( p \) states reading \( p \) symbols repeats a state, and the loop between the repeats can be run any number of times. The language \( \{0^n1^n : n \ge 0\} \) fails it: take \( 0^p1^p \); since \( |xy| \le p \), the piece \( y \) lies inside the leading 0s, so pumping it changes the number of 0s but not of 1s. The lemma only goes one way: a language that can be pumped is not thereby shown to be regular. A finite memory cannot count without bound.
Picture it: a DFA is a board game with a counter on circles joined by labelled arrows. Each symbol read moves the counter along the arrow with that label, and the string wins if the counter ends on a double circle.
Think it: to design a DFA, decide what the machine must remember about the prefix read so far, and make one state per possibility; if two prefixes can be followed by the same suffixes with the same outcomes, they can share a state. That idea, made precise by the Myhill-Nerode theorem, also gives the minimum number of states.
Worked example · fib(12)
Step by step
- F_0 = 0,; F_1 = 1
Each Fibonacci number is the sum of the two before it.
- F_{2} = F_{1} + F_{0} = 1
- F_{3} = F_{2} + F_{1} = 2
- F_{4} = F_{3} + F_{2} = 3
- F_{5} = F_{4} + F_{3} = 5
- F_{6} = F_{5} + F_{4} = 8
- F_{7} = F_{6} + F_{5} = 13
- F_{8} = F_{7} + F_{6} = 21
- \vdots
- F_{11} = F_{10} + F_{9} = 89
- F_{12} = F_{11} + F_{10} = 144
Reveal the answer
Now you Pick a problem, or type or draw your own. Every step, a picture, the answer hidden until you ask.
Symbols used here
Tap any symbol for the full definition, a picture, and what every letter in it means.
How to: Finite automata and regular languages
- Decide what the machine must remember about the input read so far.
- Make one state per possibility, mark the start state and the accepting states.
- Fill in a transition for every state and every symbol, adding a dead state if needed.
- Test the automaton on short strings that should and should not be accepted.
- To show a language is not regular, choose a string of length at least p and show every allowed split fails to pump.
Questions people ask
Can a DFA recognise balanced brackets?
No. Balanced brackets need an unbounded count of open brackets, and the pumping lemma rules it out just as for 0^n 1^n. A pushdown automaton, which adds a stack, can.
Is every finite language regular?
Yes. A finite language is a union of finitely many strings, and each string is a regular expression on its own.
What makes mathematics "discrete"?
It deals with separate, countable objects (integers, graphs, statements) rather than continuous quantities. No limits, no infinitesimals; instead induction, counting and logic.
How does a proof by induction work?
Show the statement for the first case, then show that whenever it holds for n it holds for n + 1. Like dominoes: the first falls, and each knocks over the next.
What order should I take these lessons in?
Logic and proof first, because every later lesson proves things. Then induction, sets, relations and functions, counting, recurrences, graphs, Boolean algebra and automata, and finally algorithms and complexity, which use nearly everything before them.
Is discrete mathematics the same as the maths of computer science?
Largely. Data structures are graphs and trees, correctness arguments are induction, running times are recurrences and sums, circuits are Boolean algebra, and what a computer can do efficiently is the subject of the last lessons. It is also ordinary mathematics in its own right.
Do I need calculus for this course?
No. School algebra is enough. A little familiarity with limits helps when comparing growth rates, and the lesson on asymptotic analysis explains the one limit fact it uses.
More in Discrete Math & Logic
Truth tablesSums and inductionProof by inductionAlgorithms and growth of functionsPredicate logic and quantifiersDirect proof, contrapositive and contradictionStrong induction and the well-ordering principleRelations: equivalence relations and partial ordersFunctions and cardinalityCounting: rules, choices and bijectionsThe pigeonhole principleSolving linear recurrence relationsGraphs: degrees, paths and connectivityTrees and spanning trees