G27
Self-checkedTier 2 14 min readtoc.finite-state-transducers

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 (Q,Σ,Δ,δ,λ,q0)(Q, \Sigma, \Delta, \delta, \lambda, q_0) with a finite set of states QQ, an input alphabet Σ\Sigma, an output alphabet Δ\Delta, a transition function δ:Q×Σ→Q\delta : Q \times \Sigma \to Q, an output function λ:Q×Σ→Δ\lambda : Q \times \Sigma \to \Delta and a start state q0q_0. On the input a1a2…ana_1 a_2 \dots a_n it goes through states q0,q1,…,qnq_0, q_1, \dots, q_n with qi=δ(qi−1,ai)q_i = \delta(q_{i-1}, a_i) and writes λ(qi−1,ai)\lambda(q_{i-1}, a_i) at step ii. The output has exactly nn symbols.

A Moore machine has the same parts except λ:Q→Δ\lambda : Q \to \Delta: the output depends only on the state. The machine writes λ(q0)\lambda(q_0) before reading anything and then λ(qi)\lambda(q_i) after step ii. The output has n+1n + 1 symbols. (Some books drop the output of the start state so that both models give nn 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 typed
0/01/10/1,1/0copyflip

Double circle = accepting state. Arrow from nowhere = start state.

input0110100
state{copy}

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.

0 / 8
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

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 01010110101011, which is 106=128−22106 = 128 - 22, the two's complement in 7 bits of 01101000110100 read as 2222.

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 inputs

Mealy against Moore

The same task, a detector that outputs 1 exactly when the last three symbols read are 101101 (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 typed
0/01/01/00/00/01/1s0s1s2

Double circle = accepting state. Arrow from nowhere = start state.

input1011010
state{s0}

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.

0 / 8
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

Moore machine for the same task (output inside the state): start

Simulated, not typed
01100110m0/0m1/0m2/0m3/1

Double circle = accepting state. Arrow from nowhere = start state.

input1011010
state{m0/0}

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.

0 / 8
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

The Mealy machine has 3 states and the Moore machine 4: the Moore machine needs a state that means "just completed 101101" 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
timeinput symbolMealy stateMealy outputMoore stateMoore 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.

0 / 8
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py
MealyMoore
output belongs totransition (state and input)state
output length for input length nnnnn+1n + 1
reacts to an inputin the same stepone step later (it must enter the state first)
number of states for the same taskusually fewerusually more
in hardwareoutput can change when the input changesoutput changes only at the clock edge, no glitches from the input

Conversions

Moore to Mealy. Keep the states. Put on the edge (q,a)(q, a) the output of the state δ(q,a)\delta(q, a) 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 (q,o)(q, o) with q∈Qq \in Q and oo an output symbol that some edge into qq writes (plus the start state with an arbitrary first output). At most ∣Q∣⋅∣Δ∣|Q| \cdot |\Delta| states are needed, and sometimes that many are.

Converting between the two models: start

Computed by a script, not typed
machinestates beforestates after the conversionstates 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.

0 / 4
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

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
roundclassesnumber 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.

0 / 3
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

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 88 states shrink to 44. Two states of a machine with kk states are equivalent exactly when they write the same output on every input of length less than kk, 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
taskstates as designedstates 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).

0 / 6
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

For a sequence detector of a pattern of length nn the minimal Mealy machine has nn states (state kk means: the longest suffix read that is a prefix of the pattern has length kk), and the minimal Moore machine has n+1n + 1. The values for the patterns 101101 and 10111011 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 ss with output oo?" 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
levelnew statesoutputs writteninputs 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.

0 / 4
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

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 r↦2r+b(mod3)r \mapsto 2r + b \pmod 3. 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 typed
011001r0/0r1/1r2/2

Double circle = accepting state. Arrow from nowhere = start state.

input1101
state{r0/0}

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.

0 / 5
computed by script: machines are simulated and tables constructed by scripts/fst_lib.py and scripts/toc_t2.py, one step at a time. python3 scripts/gen-toc6-traces.py

Worked examples

EX 1Output of a Mealy machine

Easy

Run the two's complement machine on 01101000110100: 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 01010110101011, seven symbols for seven input symbols. A Mealy machine always writes as many symbols as it reads.

EX 2Length of the output

Easy

A Moore machine reads a string of length nn. Its output has n+1n + 1 symbols, because the start state writes before anything is read. A Mealy machine on the same string writes nn. A question that says "the output is of length nn" for a Moore machine is using the other convention: read the question's definition.

EX 3Converting a Mealy machine to a Moore machine

Medium

The Mealy detector for 101101 has 3 states. States of the Moore machine are pairs (state, output of the entering edge). Only (s0,0)(s_0, 0), (s1,0)(s_1, 0), (s2,0)(s_2, 0) and (s1,1)(s_1, 1) can be reached, since the output 1 occurs only on the edge from s2s_2 on input 1, which enters s1s_1. 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

Medium

To 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 kk 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?" nn for Mealy, n+1n + 1 for Moore.

Cheat box

Cheat box

  • Mealy: output on the edge, nn symbols for nn inputs. Moore: output on the state, n+1n + 1 symbols (start state writes first).
  • Moore to Mealy: same states. Mealy to Moore: states become (state, output), up to ∣Q∣⋅∣Δ∣|Q| \cdot |\Delta|.
  • 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 nn: nn states (Mealy), n+1n + 1 (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

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 nn, 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

GATE 2025 · Set 1 · NAT · 2 marksQ8 · page 198 of the book
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)
GATE 2009 · MCQQ60 · page 206 of the book
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
year not read yetQ74 · page 210 of the book
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'
GATE 2005 · MCQ · 2 marksQ77 · page 211 of the book
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 answer

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 answer
  1. 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 answer

3×2=63 \times 2 = 6 (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 10001000 read from the least significant bit?

Show answer

Read the string left to right as the bits from the least significant end, so the number is 11. The first symbol is the lowest 1 and is copied; every later symbol is inverted. Output 11111111, which is the number 15=16−115 = 16 - 1, the two's complement of 11 in 4 bits.

Question 5

How many states has the smallest Mealy machine that outputs 1 whenever the last two symbols were 1111 (overlapping)?

Show answer

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 (Q,Σ,Δ,δ,λ:Q×Σ→Δ,q0)(Q, \Sigma, \Delta, \delta, \lambda: Q \times \Sigma \to \Delta, q_0), nn output symbols. Moore λ:Q→Δ\lambda: Q \to \Delta, n+1n + 1.
  • Moore to Mealy keeps the states; Mealy to Moore needs up to ∣Q∣⋅∣Δ∣|Q| \cdot |\Delta|.
  • 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 nn: nn states Mealy, n+1n + 1 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