AQA 7517, section 4.4.2.1
A finite state machine (FSM) is a simple model of computation. It is always in exactly one state out of a finite set of states. When it reads an input, it moves from one state to another, according to fixed rules.
FSMs are used to model things such as traffic lights, vending machines, lift controllers, and to check whether a string of characters follows a pattern.
Parts of an FSM
- A finite set of states.
- An alphabet of possible inputs, such as
{0, 1}. - A start state, which is where it begins.
- Transitions: for each state and input, which state to move to next.
- Often, one or more accepting (final) states, shown with a double circle.
An input string is accepted if the machine ends in an accepting state after reading the whole string. Otherwise it is rejected.
FSMs with no output
State transition diagram
States are drawn as circles, and transitions as arrows labelled with the input that causes them. An arrow with no source marks the start state, and a double circle marks an accepting state.
Example: an FSM that accepts binary strings with an even number of 1s.
S0is the start state, and is also accepting (double circle). It means "an even number of 1s so far".S1means "an odd number of 1s so far".- Reading a
0doesn't change the number of 1s, so the machine stays in the same state. - Reading a
1switches betweenS0andS1.
State transition table
The same machine as a table:
| Current state | Input 0 | Input 1 |
|---|---|---|
| S0 (start, accepting) | S0 | S1 |
| S1 | S1 | S0 |
Tracing an input
Input 1011:
| Read | State after |
|---|---|
| (start) | S0 |
| 1 | S1 |
| 0 | S1 |
| 1 | S0 |
| 1 | S1 |
The machine ends in S1, which isn't accepting, so 1011 is rejected. It contains three 1s, which is odd, so that is correct. The input 1001 ends in S0 (two 1s), so it is accepted.
FSMs with output: Mealy machines
A Mealy machine is an FSM that produces an output as well as changing state. The output depends on the current state and the input, and is produced on the transition.
On a diagram, each transition is labelled input / output, for example 1/0 means "on input 1, output 0".
Example: a machine that outputs 1 if the current input bit is the same as the previous one, and 0 otherwise.
States: S (start, nothing read yet), Z (last bit was 0), O (last bit was 1).
| Current state | Input 0 | Input 1 |
|---|---|---|
| S | Z, output 0 | O, output 0 |
| Z | Z, output 1 | O, output 0 |
| O | Z, output 0 | O, output 1 |
Written as input/output pairs, for example S --0/0--> Z.
Tracing
Input 0 0 1 1 0:
| Input | State before | Output | State after |
|---|---|---|---|
| 0 | S | 0 | Z |
| 0 | Z | 1 | Z |
| 1 | Z | 0 | O |
| 1 | O | 1 | O |
| 0 | O | 0 | Z |
The output is 0 1 0 1 0.
Worked example
Task: design an FSM that accepts strings of a and b that end in b.
States: Q0 (start; the last symbol wasn't b, or nothing has been read) and Q1 (the last symbol was b, accepting).
| Current state | Input a | Input b |
|---|---|---|
| Q0 (start) | Q0 | Q1 |
| Q1 (accepting) | Q0 | Q1 |
Test it on aab: Q0, then a gives Q0, a gives Q0, b gives Q1. It ends in Q1, so it is accepted. Test aba: Q0, a gives Q0, b gives Q1, a gives Q0. It ends in Q0, so it is rejected.
Exam tip: in a diagram question, label every arrow, mark the start state, and mark accepting states with a double circle. Make sure there is a transition from every state for every input. For a Mealy machine, check the format is input/output.
Check yourself
When does an FSM accept an input string?
When it is in an accepting state after reading the whole string.
What is the difference between an FSM with no output and a Mealy machine?
A Mealy machine produces an output on each transition (depending on the current state and the input). An FSM with no output only changes state, and its result is whether the string is accepted.
In the even-number-of-1s machine, is the empty string accepted?
Yes. It stays in the start state S0, which is accepting, and zero is an even number of 1s.