Sub topic 15
Moore and Mealy machines
This chapter assumes you can read a DFA transition table and trace a string through one. A DFA only answers accept or reject. Moore and Mealy machines extend the DFA so that it produces an output string. They are finite-state transducers, and GATE asks about them regularly.
From recognizers to transducers
A DFA has a set of accepting states. A transducer has none. Instead it has an output alphabet Δ and an output rule, and it writes a symbol to the output as it reads input.
Both machines are 6-tuples (Q, Σ, Δ, δ, λ, q₀):
- Q is the finite set of states, and q₀ is the start state.
- Σ is the input alphabet and Δ is the output alphabet.
- δ: Q × Σ → Q is the transition function. It is deterministic and total, as in a DFA.
- λ is the output function. Its domain is what distinguishes the two models.
| Feature | Moore machine | Mealy machine |
|---|---|---|
| Output function | λ: Q → Δ | λ: Q × Σ → Δ |
| Output depends on | Current state only | Current state and current input symbol |
| Output written | On entering a state | On taking a transition |
| Output length for input of length n | n + 1 | n |
| Output on the empty string | λ(q₀), one symbol | Empty string |
The length difference follows from the definitions. A Moore machine emits a symbol for the start state before it reads anything, so it produces n + 1 symbols. A Mealy machine emits one symbol per transition, so it produces n.
Worked example: detecting the pattern 101
The task is to output 1 each time the input so far ends in 101, and 0 otherwise. Overlapping occurrences count, so 10101 contains two matches.
The Mealy version
The Mealy machine needs three states that record how much of 101 has been matched:
s0: no useful suffix.s1: the input so far ends in1.s2: the input so far ends in10.
Each transition is written input/output:
s0: on 0 go tos0with output 0. On 1 go tos1with output 0.s1: on 0 go tos2with output 0. On 1 stay ins1with output 0.s2: on 0 go tos0with output 0. On 1 go tos1with output 1.
The last transition outputs 1 because the machine has just completed 101. It moves to s1, not s0, because the final 1 can start another match.
The Moore version
A Moore machine must put the output on the state, so the match needs its own state. It uses four states:
q0: no progress, output 0.q1: suffix1, output 0.q2: suffix10, output 0.q3: suffix101, output 1.
The transitions are:
q0: 0 →q0, 1 →q1.q1: 0 →q2, 1 →q1.q2: 0 →q0, 1 →q3.q3: 0 →q2, 1 →q1.
From q3, reading 0 gives the suffix 10, so the machine goes to q2. Reading 1 gives the suffix 1, so it goes to q1.
For the input 10101 the Mealy machine prints 00101. The Moore machine prints 000101, which is the same string with one extra leading symbol.
Simulating both machines
The code below runs both machines and checks that they agree. It also builds the Mealy machine directly from the Moore one, using the conversion rule described in the next section.
def run_mealy(delta, lam, start, w):
state, out = start, []
for ch in w:
out.append(lam[(state, ch)]) # output depends on state AND input
state = delta[(state, ch)]
return "".join(out)
def run_moore(delta, out, start, w):
state = start
result = [out[state]] # output of the start state comes first
for ch in w:
state = delta[(state, ch)]
result.append(out[state]) # output depends on the new state only
return "".join(result)
# Mealy machine for "101" (3 states)
m_delta = {("s0", "0"): "s0", ("s0", "1"): "s1",
("s1", "0"): "s2", ("s1", "1"): "s1",
("s2", "0"): "s0", ("s2", "1"): "s1"}
m_lam = {("s0", "0"): "0", ("s0", "1"): "0",
("s1", "0"): "0", ("s1", "1"): "0",
("s2", "0"): "0", ("s2", "1"): "1"}
# Moore machine for "101" (4 states)
o_delta = {("q0", "0"): "q0", ("q0", "1"): "q1",
("q1", "0"): "q2", ("q1", "1"): "q1",
("q2", "0"): "q0", ("q2", "1"): "q3",
("q3", "0"): "q2", ("q3", "1"): "q1"}
o_out = {"q0": "0", "q1": "0", "q2": "0", "q3": "1"}
# Moore -> Mealy: same states and transitions, lam'(q, a) = out[delta(q, a)]
conv_lam = {(q, a): o_out[p] for (q, a), p in o_delta.items()}
w = "10101"
print(run_mealy(m_delta, m_lam, "s0", w)) # 00101
print(run_moore(o_delta, o_out, "q0", w)) # 000101
print(run_mealy(o_delta, conv_lam, "q0", w)) # 00101The Moore output is one symbol longer, and dropping its first symbol gives the Mealy output.
Converting between the models
Two machines count as equivalent when, for every input w, the Mealy output equals the Moore output with its first symbol removed. The first symbol is λ(q₀) and carries no information about the input.
Moore to Mealy
This conversion is easy and adds no states. Keep Q, δ and q₀. Define the new output on a transition as the output of the state it enters:
λ′(q, a) = λ(δ(q, a))
The conv_lam line in the code applies this rule.
Mealy to Moore
This direction may need more states, because a Moore state carries exactly one output. A single Mealy state can be entered by transitions with different outputs.
- Create a state (p, o) for each Mealy state p and output symbol o that some transition into p produces.
- For a Mealy transition p –a/o→ r, the new transition goes from (p, o′) to (r, o), for every o′. Moore output of (r, o) is o.
- Choose a start state (q₀, b), where b is any fixed output symbol. This b becomes the ignored first output.
The result has at most |Q| · |Δ| states. Our s1 has only output 0 on its incoming transitions. State s2 also has only 0. State s0 also has only 0. State s1 however is entered with output 0 from s0, s1 and s2... except s2 –1/1→ s1, which enters s1 with output 1. So s1 splits into (s1,0) and (s1,1), which gives four states. This matches the hand-built Moore machine.
Diagram: a Mealy machine for two's complement
A classic machine reads a binary number least significant bit first and outputs its two's complement. It copies bits up to and including the first 1, then flips every later bit.
For the input 0110 (read as 0, 1, 1, 0) the machine outputs 0, 1, 0, 1. Written most significant bit first, 6 becomes 1010, which is 10, the two's complement of 6 in four bits. This machine needs only two states because a Mealy machine can output a different symbol on each transition.
What these machines can and cannot do
- No acceptance. Moore and Mealy machines have no final states. Questions about them ask about output strings, not membership.
- Finite memory. The machine cannot count without bound. A transducer that outputs the sum of two arbitrary binary numbers is possible, because a serial adder needs only a carry bit (2 states). A transducer that multiplies two arbitrary numbers is impossible.
- Output alphabet. Δ can differ from Σ. A counter modulo 3 that prints the remainder uses Δ = {0, 1, 2}.
- Relation to DFAs. A DFA is a Moore machine whose outputs are 1 for accepting states and 0 for the rest. The first output (the empty string case) tells you whether ε is accepted.
Common GATE patterns
- Output length. For an input of length n, a Moore machine gives n + 1 symbols and a Mealy machine gives n. Questions often ask this directly.
- Tracing. Run the string and write the output. For Moore, do not forget the start-state output.
- State counting. Mealy to Moore may increase the state count, but Moore to Mealy never does. The upper bound is |Q| · |Δ|.
- Design. Pick a pattern detector, a bit flipper, a remainder counter or a serial adder. Decide what each state must remember, then add outputs.
- Equivalence. Compare outputs after dropping the Moore machine's first symbol.
Exercises
Exercise
A Moore machine has 5 states and is run on an input string of length 7. How many output symbols does it produce? What if the machine is a Mealy machine?
Show answer
The Moore machine produces 7 + 1 = 8 symbols, because it outputs for the start state before reading anything. The Mealy machine produces 7 symbols, one per transition. The number of states does not affect the output length.
Exercise
A Mealy machine has 3 states and the output alphabet {0, 1}. Every state is entered by transitions that produce both 0 and 1. What is the maximum number of states in an equivalent Moore machine, and how many are needed here?
Show answer
The upper bound is |Q| · |Δ| = 3 · 2 = 6 states. Since each state is entered with both outputs, each splits into two copies, so this machine needs the full 6 states.
Exercise
Design a Mealy machine over {0, 1} that outputs 1 on a transition exactly when the current input bit equals the previous one, and 0 otherwise. The first bit has no previous bit and outputs 0. How many states does it need?
Show answer
Three states: start (nothing read), last0 (previous bit was 0) and last1 (previous bit was 1). From start, both inputs output 0 and go to last0 or last1. From last0, input 0 outputs 1 and stays, and input 1 outputs 0 and goes to last1. From last1, input 1 outputs 1 and stays, and input 0 outputs 0 and goes to last0.