Sub topic 15
Moore and Mealy Machines
Assume you know DFAs, NFAs, transition functions, and finite alphabets. This chapter adds output to a finite automaton. The machine reads an input string and produces an output string or output sequence.
Finite-state transducers
A finite-state transducer is a finite-state machine that produces outputs while it reads input. A DFA only answers whether the complete input is accepted. A transducer can report information during the computation.
The two standard models are:
- A Moore machine assigns an output to each state.
- A Mealy machine assigns an output to each transition.
For a Moore machine, the output depends on the current state. For a Mealy machine, the output depends on the current state and the input symbol being read.
| Model | Output location | Output timing |
|---|---|---|
| Moore | States | On entering or observing a state |
| Mealy | Transitions | While consuming an input symbol |
Moore machines
A Moore machine can be represented as (Q, Σ, Γ, δ, λ, q₀):
Qis the finite set of states.Σis the input alphabet.Γis the output alphabet.δis the transition function.λ: Q → Γgives each state an output.q₀is the initial state.
Because the initial state has an output, a Moore machine usually produces one more output symbol than the number of input symbols. Some textbooks omit the initial output when comparing machines, so always check the convention.
Mealy machines
A Mealy machine uses an output function
λ: Q × Σ → Γ.
Each transition is labeled with both an input and an output, such as 1/0. The first part is consumed input, and the second part is produced output.
A Mealy machine produces one output for each consumed input symbol. It therefore reacts immediately to the current input. This often requires fewer states than an equivalent Moore machine.
def mealy_parity(bits):
"""Output the parity before reading each bit, then update it."""
parity = 0
output = []
for bit in bits:
output.append(str(parity))
parity ^= int(bit)
return "".join(output)
def moore_parity(bits):
"""Output the current parity after each bit."""
parity = 0
output = [str(parity)] # output of the initial state
for bit in bits:
parity ^= int(bit)
output.append(str(parity))
return "".join(output)
print(mealy_parity("1011")) # 0010
print(moore_parity("1011")) # 01100The two functions describe the same evolving parity state, but their output timing differs. The Mealy version reports the old parity before each input update. The Moore version reports the initial parity and then the new parity after every update.
Converting between the models
To convert a Mealy machine to a Moore machine, split states when necessary. If transitions entering the same state produce different outputs, create separate Moore states that remember both the destination state and the transition output. This can increase the state count.
To convert a Moore machine to a Mealy machine, label each transition with the output of its destination state. The initial Moore output has no corresponding input transition, so it may need to be handled separately.
Common errors
- Treating a Mealy transition label as only an input label.
- Forgetting the Moore machine's initial-state output.
- Assuming equivalent machines must have the same number of states.
- Comparing outputs without specifying whether they occur before or after a transition.
Exercise
A Moore machine has output 0 in its initial state and changes its output after every input symbol. How many output symbols does it produce for an input of length n?
Show answer
It produces n + 1 symbols when the initial-state output is included.