Mealy and Moore machines
Finite automata that produce output: Mealy machines (output on the transitions) and Moore machines (output on the states), what an output string looks like (length n for Mealy, n + 1 for Moore), conversion in both directions and what it does to the number of states, minimisation by partition refinement, shortest input that reaches a given state with a given output, and the standard designs: two's complement, adding one, the serial adder, remainder machines and overlapping sequence detectors.
Why it exists
A DFA answers yes or no. Many machines must compute: add two numbers one bit at a time, find the two's complement of a binary number, flag every occurrence of a pattern in a stream, output the remainder of the number read so far. A finite-state transducer is a finite automaton that writes an output symbol as it reads. Two models are used: in a Mealy machine the output belongs to the transition, in a Moore machine it belongs to the state. They are the models behind the synchronous circuits of digital logic (state machines). GATE asks: what does this machine output on this input, what does it compute, how many states are needed, convert one model to the other, the shortest input that reaches a state with a given output.
Prerequisites
You need DFAs (DFA): states, transitions on every symbol, a run on a string. The minimisation technique is the one of DFA minimisation.
Precise definitions
A Mealy machine is a tuple with a finite set of states , an input alphabet , an output alphabet , a transition function , an output function and a start state . On the input it goes through states with and writes at step . The output has exactly symbols.
A Moore machine has the same parts except : the output depends only on the state. The machine writes before reading anything and then after step . The output has symbols. (Some books drop the output of the start state so that both models give symbols; then a Moore machine is a Mealy machine whose output is the same on all edges entering a state. State the convention in an answer.)
There are no accepting states: a transducer computes a function from input strings to output strings, not a language. A DFA is the special case of a Moore machine with outputs accept and reject.
Equivalence. Two machines are equivalent if they compute the same function. A Moore machine and a Mealy machine are compared after dropping the first symbol of the Moore output.
How it works
Reading a Mealy diagram
Edges are labelled input/output. The machine below reads a binary number from the least significant bit and outputs the two's complement: it copies bits up to and including the first 1, then inverts every bit after that.
Mealy machine for the two's complement (numbers read from the least significant bit): start
Simulated, not typedDouble circle = accepting state. Arrow from nowhere = start state.
Step 0
Start in copy. A Mealy machine writes one output symbol on every transition, so the output will have exactly as many symbols as the input. There are no accepting states: the result of a run is the output string.
Why it works: the two's complement is the one's complement plus 1. Inverting every bit turns the trailing 0s into 1s and the lowest 1 into 0; adding 1 then turns those trailing 1s back into 0s and the 0 into 1. The net effect: the bits up to and including the lowest 1 are unchanged, every bit above it is inverted. Reading from the right, the machine needs only one bit of memory: "have I seen a 1 yet?". That is why 2 states suffice. The same input gives output , which is , the two's complement in 7 bits of read as .
Other Mealy machines of the same kind, all with 2 states: add one (state "carry pending", it turns trailing 1s into 0s and the first 0 into 1, then copies) and the serial adder (the input symbol is a pair of bits, the state is the carry). The carry flip-flop of a serial adder in hardware is exactly this second state.
A Mealy machine in C
A Mealy machine is two tables indexed by state and input symbol: the next state and the output symbol.
#include <stdio.h>
#include <string.h>
struct mealy {
int next[3][2];
char out[3][2];
};
static void run(const struct mealy *m, const char *w, char *out) {
int q = 0;
size_t n = strlen(w);
for (size_t i = 0; i < n; i++) {
int a = w[i] - '0';
out[i] = m->out[q][a];
q = m->next[q][a];
}
out[n] = '\0';
}
int main(void) {
/* two's complement, least significant bit first: state 0 = copy, state 1 = invert */
struct mealy twos = { {{0, 1}, {1, 1}, {0, 0}}, {{'0', '1'}, {'1', '0'}, {'0', '0'}} };
/* detector of 101: state k = length of the longest suffix read that is a prefix of 101 */
struct mealy det = { {{0, 1}, {2, 1}, {0, 1}}, {{'0', '0'}, {'0', '0'}, {'0', '1'}} };
char out[32];
run(&twos, "0110100", out);
printf("two's complement of 0110100: %s\n", out);
run(&det, "1011010", out);
printf("detector of 101 on 1011010: %s\n", out);
int good = 0;
for (int v = 0; v < 256; v++) {
char in[9];
for (int i = 0; i < 8; i++) in[i] = ((v >> i) & 1) ? '1' : '0';
in[8] = '\0';
run(&twos, in, out);
int r = 0;
for (int i = 0; i < 8; i++) r |= (out[i] == '1') << i;
if (r == ((256 - v) & 255)) good++;
}
printf("correct two's complement for %d of 256 eight-bit inputs\n", good);
return 0;
}two's complement of 0110100: 0101011
detector of 101 on 1011010: 0010010
correct two's complement for 256 of 256 eight-bit inputsMealy against Moore
The same task, a detector that outputs 1 exactly when the last three symbols read are (overlapping occurrences count), as a Mealy and as a Moore machine:
Mealy machine that outputs 1 whenever the last three symbols read are 101: start
Simulated, not typedDouble circle = accepting state. Arrow from nowhere = start state.
Step 0
Start in s0. A Mealy machine writes one output symbol on every transition, so the output will have exactly as many symbols as the input. There are no accepting states: the result of a run is the output string.
Moore machine for the same task (output inside the state): start
Simulated, not typedDouble circle = accepting state. Arrow from nowhere = start state.
Step 0
Start in m0. A Moore machine writes the output of the state it is in, also before reading anything: the first output symbol is 0. So an input of length n gives an output of length n + 1.
The Mealy machine has 3 states and the Moore machine 4: the Moore machine needs a state that means "just completed " so that the output 1 can be attached to a state, while the Mealy machine puts the 1 on the transition. Running both on the same input:
The same task as a Mealy and as a Moore machine: start
Computed by a script, not typed| time | input symbol | Mealy state | Mealy output | Moore state | Moore output |
|---|---|---|---|---|---|
| start | |||||
| after symbol 1 | |||||
| after symbol 2 | |||||
| after symbol 3 | |||||
| after symbol 4 | |||||
| after symbol 5 | |||||
| after symbol 6 | |||||
| after symbol 7 |
Step 0
Input 1011010. Mealy output: 0010010. Moore output: 00010010. Dropping the first symbol of the Moore output gives 0010010 = 0010010: the two machines compute the same function once the extra initial output is ignored.
| Mealy | Moore | |
|---|---|---|
| output belongs to | transition (state and input) | state |
| output length for input length | ||
| reacts to an input | in the same step | one step later (it must enter the state first) |
| number of states for the same task | usually fewer | usually more |
| in hardware | output can change when the input changes | output changes only at the clock edge, no glitches from the input |
Conversions
Moore to Mealy. Keep the states. Put on the edge the output of the state it enters. The Mealy output is the Moore output without its first symbol. The number of states does not change.
Mealy to Moore. A Moore state must remember the output of the edge that entered it, so the new states are pairs with and an output symbol that some edge into writes (plus the start state with an arbitrary first output). At most states are needed, and sometimes that many are.
Converting between the two models: start
Computed by a script, not typed| machine | states before | states after the conversion | states after minimising |
|---|---|---|---|
| Moore detector to Mealy | |||
| Mealy detector to Moore | |||
| remainder mod 3: Moore to Mealy | |||
| two's complement: Mealy to Moore |
Step 0
Both models compute the same functions (apart from the extra first output of a Moore machine). The conversion Mealy to Moore can multiply the number of states; Moore to Mealy never increases it.
Minimisation
Two states of a Mealy machine are equivalent if from either of them every input string produces the same output. The partition-refinement algorithm of DFAs works: start with the partition that puts two states together when they write the same output on every input symbol, then repeatedly split a class when two of its states send some input symbol to different classes; stop when nothing changes, and merge each class into one state. For a Moore machine, start from the partition by the state output instead. Unreachable states are deleted first.
Minimising a Mealy machine by partition refinement: start
Computed by a script, not typed| round | classes | number of classes |
|---|---|---|
| round 0 (by output row) | ||
| round 1 (by successor classes) | ||
| round 2 (by successor classes) |
Step 0
The eight-state machine (output = the input two steps ago, but the designer remembered three inputs) ends with 4 classes, so 4 states are enough. Merge each class into one state: the third remembered input never matters.
The table counts the classes of a machine whose designer remembered the last three inputs to output the input of two steps ago: only the last two matter, so states shrink to . Two states of a machine with states are equivalent exactly when they write the same output on every input of length less than , which is why short inputs are enough to compare the states of a small machine.
Minimum numbers of states of some Mealy machines: start
Computed by a script, not typed| task | states as designed | states after minimising |
|---|---|---|
| two's complement (LSB first) | ||
| add one (LSB first) | ||
| serial adder (pairs of bits) | ||
| detect 101 (overlapping) | ||
| detect 1011 (overlapping) | ||
| delay by two, remembering three inputs |
Step 0
The detector for a pattern of length n needs n states as a Mealy machine (state k = length of the longest suffix read that is a prefix of the pattern, for k from 0 to n - 1).
For a sequence detector of a pattern of length the minimal Mealy machine has states (state means: the longest suffix read that is a prefix of the pattern has length ), and the minimal Moore machine has . The values for the patterns and above were confirmed by trying every machine with fewer states.
Shortest input to a state with a given output
"What is the shortest input that takes the machine to state with output ?" is a breadth first search: explore the pairs (state, last output) in order of the length of the input that reaches them, remembering the input.
Shortest input that drives a Mealy machine to a given state with a given output: start
Computed by a script, not typed| level | new states | outputs written | inputs that reach them |
|---|---|---|---|
| length 0 | |||
| length 1 | |||
| length 2 | |||
| length 3 |
Step 0
The shortest input is 101 (3 symbols). Breadth first search finds a shortest one because it explores the inputs in order of length and never revisits a (state, output) pair.
A Moore machine that computes
Reading a binary number from the most significant bit, the remainder modulo 3 of the number read so far is updated by . Three states suffice, and the output of a state is its remainder:
Moore machine that writes the remainder modulo 3 of the binary number read so far (most significant bit first): start
Simulated, not typedDouble circle = accepting state. Arrow from nowhere = start state.
Step 0
Start in r0. A Moore machine writes the output of the state it is in, also before reading anything: the first output symbol is 0. So an input of length n gives an output of length n + 1.
Worked examples
EX 1Output of a Mealy machine
EasyRun the two's complement machine on : state copy, symbol 0 writes 0 (copy); symbol 1 writes 1 and moves to invert; then 1, 0, 1, 0, 0 are written as 0, 1, 0, 1, 1. Output , seven symbols for seven input symbols. A Mealy machine always writes as many symbols as it reads.
EX 2Length of the output
EasyA Moore machine reads a string of length . Its output has symbols, because the start state writes before anything is read. A Mealy machine on the same string writes . A question that says "the output is of length " for a Moore machine is using the other convention: read the question's definition.
EX 3Converting a Mealy machine to a Moore machine
MediumThe Mealy detector for has 3 states. States of the Moore machine are pairs (state, output of the entering edge). Only , , and can be reached, since the output 1 occurs only on the edge from on input 1, which enters . So the conversion gives 4 states, and the refinement of the Moore machine finds no states to merge, so 4 states are needed for a Moore detector.
EX 4Number of states of an output machine
MediumTo find the minimum number of states of a given machine: delete the unreachable states, group by output behaviour, refine until stable, count the classes. For the eight-state machine in the minimisation table the classes after round 0 are 2, after round 1 are 4 and round 2 changes nothing: 4 states. A shortcut for a machine built from "the last inputs" is to ask how many of those inputs the output ever depends on.
EX 5Typical questions
GATE level(a) "A machine has the state table ... what is the minimum number of states required to realise it?" Delete unreachable states and merge equivalent ones as in the table. (b) "What does this machine compute (increment, decrement, one's complement, two's complement)?" Run it on a few short inputs, least significant bit first: copy-then-invert is the two's complement, invert-everything is the one's complement, flip trailing 1s and then copy is add one. (c) "What is the shortest input that takes the machine to state 01 with output 1?" Breadth first search as in the table: the answer is the length of the shortest path, not of the first path you try. (d) "Mealy or Moore: which needs more states for a given task?" In general Moore. (e) "How many symbols does the output have?" for Mealy, for Moore.
Cheat box
Cheat box
- Mealy: output on the edge, symbols for inputs. Moore: output on the state, symbols (start state writes first).
- Moore to Mealy: same states. Mealy to Moore: states become (state, output), up to .
- No accepting states: the answer is the output string.
- Minimise: delete unreachable states, partition by outputs, refine by successor classes, merge.
- Sequence detector of a pattern of length : states (Mealy), (Moore); overlapping occurrences return to the longest border.
- Two's complement (LSB first): copy through the first 1, invert the rest, 2 states. Serial adder: 2 states (the carry). Remainder modulo 3 (MSB first): 3 states.
Traps and GATE patterns
Connections
Builds on
Unlocks
Nothing built depends on this yet.
A transducer is a DFA with output, and its minimisation is the algorithm of DFA minimisation with a different initial partition. In digital logic the same machines appear as clocked sequential circuits (state machines): the flip-flops hold the state and combinational logic computes the next state and the output; the carry flip-flop of a serial adder is the second state of the Mealy machine here (adders). Standard designs of DFA design patterns (counting modulo , remembering the last symbols) carry over to output machines.
PYQs
4 previous year questions from your book. I re-solved each one myself (4 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs
Consider a finite state machine (FSM) with one input X and one output f, represented by the given state transition table. The minimum number of states required to realize this FSM is ________. (Answer in integer)
Given the following state table of an FSM with two states A and B ,one input and one output. If the initial state is A=0,B=0.what is the minimum length of an input string which will take the machine to the state A=0,B=1 with output = 1.
- (a)3
- (b)4
- (c)5
- (d)6
For a state machine with the following state diagram the expression for the next state S+ in terms of the current state and the input variables x and y is
- (a)S+=S'.y'+S.x
- (b)S+=S.x.y'+S'.y.x'
- (c)S+=x.y'
- (d)S+=S'.y+S.x'
The following diagram represents a finite state machine which takes as input a binary number from the least significant bit. Which of the following is TRUE?
- (a)It computes 1's complement of the input number
- (b)It computes 2's complement of the input number
- (c)It increments the input number
- (d)It decrements the input number
Self-check
Question 1
How many symbols does a Moore machine write on an input of length 5? A Mealy machine?
Show answerHide
Six (the start state writes first); five.
Question 2
Convert a Moore machine with 4 states and 2 output symbols to a Mealy machine. How many states?
Show answerHide
- The states are kept; each edge gets the output of the state it enters.
Question 3
A Mealy machine with 3 states and 2 output symbols is converted to a Moore machine. At most how many states?
Show answerHide
(a state for each pair of state and output of an entering edge), plus possibly a special start state if the start state needs its own first output.
Question 4
What does the two's complement machine output on read from the least significant bit?
Show answerHide
Read the string left to right as the bits from the least significant end, so the number is . The first symbol is the lowest 1 and is copied; every later symbol is inverted. Output , which is the number , the two's complement of in 4 bits.
Question 5
How many states has the smallest Mealy machine that outputs 1 whenever the last two symbols were (overlapping)?
Show answerHide
Two: state 0 (last symbol was not 1) and state 1 (last symbol was 1); on input 1 from state 1 write 1.
Revision card
One-screen revision
- Mealy , output symbols. Moore , .
- Moore to Mealy keeps the states; Mealy to Moore needs up to .
- Minimise by partition refinement (start by outputs); shortest input by breadth first search.
- Two's complement (LSB first): copy to the first 1, invert the rest. Add one: carry state. Serial adder: carry state.
- Detector of a pattern of length : states Mealy, Moore.
References
- ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 2.7.1 (Moore and Mealy machines, in the exercises and the discussion of automata with output)
- ·Mano, Digital Design (5th edition), Chapter 5 (synchronous sequential logic: Mealy and Moore state diagrams)
- ·Kohavi, Jha, Switching and Finite Automata Theory (3rd edition), Chapters 10 and 11 (finite-state machines; minimisation of completely specified machines)
Verification
scripts/fst_lib.py, scripts/gen-toc6-traces.py and scripts/enum-checks/toc_t2_check.py: the two's complement, add-one and serial-adder machines equal integer arithmetic on all strings up to length 10 (6 pairs of bits for the adder); the overlapping detectors for 101 and 1011 equal the direct definition up to length 12; the remainder machine equals the remainder of the prefix modulo 3 up to length 12; Moore to Mealy and Mealy to Moore conversions preserve the function (the Moore output without its first symbol is the Mealy output) on named machines and 150 random Mealy and 150 random Moore machines; the minimal machine is equivalent to the original and its number of states equals the number of different behaviours counted by brute force on the same machines; minimality of the numbers 2, 3, 4, 4 (two's complement, detector 101, detector 1011, the delay machine) is shown by exhaustive search over ALL machines with fewer states (all 46656 machines with 3 states for 1011 and for the delay machine); the shortest-input search equals brute force on the example and on 300 random machines. The C program on this page is compiled and run by npm run verify:c. Textbooks differ on the convention for the output of a Moore machine before the first input (this page: one output symbol per state visited, including the start state, so n + 1 symbols) and on whether the final answer for a Mealy machine counts the start state; always state the convention you use. The chapter references are from memory (needs verification). The independent second pass has not been run, so the page stays draft. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)