A levels Computer Science · Theory of computation
Finite state machines
1What is a finite state machine?
A finite state machine (FSM) is a model of computation with a fixed number of states. It reads an input one symbol at a time and moves between states by following transitions.
An FSM has:
- a finite set of states, one of which is the start state;
- an input alphabet, the symbols it can read (for example 0 and 1);
- a transition function that gives the next state for each state and input;
- for a recogniser, a set of accepting (final) states.
An FSM has no memory apart from the state it is in. That is what makes it simple, and also what limits it.
Check yourself
What is the only memory a finite state machine has?
Show the answer
The state it is currently in
Nothing else.
2State transition diagrams
In a state transition diagram, each state is a circle, each transition is an arrow labelled with the input that causes it, the start state has an incoming arrow from nowhere, and accepting states have a double circle.
An FSM that accepts binary strings with an even number of 1s. A 1 switches state; a 0 loops back to the same state.
This FSM accepts binary strings with an even number of 1s. Reading a 1 switches state; reading a 0 stays put.
Input 1101
- Start in Even. Read 1 → Odd.
- Read 1 → Even.
- Read 0 → Even.
- Read 1 → Odd. Odd is not accepting, so 1101 is rejected (it has three 1s).
Check yourself
Is the string 1001 accepted by the even-parity FSM?
Show the answer
Yes, it has two 1s, so it ends in Even
Two switches.
3State transition tables
A state transition table holds the same information as the diagram:
| Current state | Input | Next state |
|---|---|---|
| Even | 0 | Even |
| Even | 1 | Odd |
| Odd | 0 | Odd |
| Odd | 1 | Even |
A table is easier for a program to use: the FSM can be coded as a dictionary lookup, next = table[(state, symbol)].
table = {("Even","0"):"Even", ("Even","1"):"Odd",
("Odd","0"):"Odd", ("Odd","1"):"Even"}
state = "Even"
for ch in "1101":
state = table[(state, ch)]
print(state == "Even") # False: rejected
Check yourself
How many rows does a complete transition table need for 3 states and an alphabet of 2 symbols?
Show the answer
6
Every state with every symbol.
4FSMs with outputs
A Mealy machine produces an output on each transition. Transitions are labelled input / output.
A rising-edge detector
- It outputs 1 when the input changes from 0 to 1, and 0 otherwise.
- States: Low and High, starting in Low.
- Low, input 0 → Low / 0. Low, input 1 → High / 1.
- High, input 1 → High / 0. High, input 0 → Low / 0.
- Input 0110 11 gives output 0100 10 (spaces added to read it): a 1 each time the signal rises.
A Mealy machine with output can also translate one string into another, which is why FSMs are used in simple encoders and decoders.
Check yourself
What labels the transitions in a Mealy machine?
Show the answer
An input and the output it produces
input / output.
5Deterministic and non-deterministic
- In a deterministic FSM (DFA), each state has exactly one transition for each input symbol, so the path is always certain.
- In a non-deterministic FSM (NFA), a state may have several transitions for the same input, or none. It accepts if any path leads to an accepting state.
Every NFA can be converted to an equivalent DFA, though the DFA may need many more states. So both kinds recognise exactly the same languages: the regular languages.
Check yourself
What is true of a deterministic FSM?
Show the answer
Each state has exactly one transition for each input
Path is certain.
6Uses and limits of FSMs
Uses:
- traffic lights and vending machines, which move through fixed states;
- lexical analysis in compilers, which splits source code into tokens;
- checking input formats, such as whether a string is a valid number;
- simple game AI (for example a guard that switches between patrol, chase and return).
Limit: an FSM cannot count without limit. For example, no FSM can check that a string has the same number of 0s as 1s for every length, because it would need infinitely many states to remember the difference. More powerful models (with a stack, or a Turing machine's tape) are needed.
Check yourself
Why can no FSM recognise every string with equal numbers of 0s and 1s?
Show the answer
It would need unlimited memory to keep count, but it has a finite number of states
Finite states, unlimited count.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera