Theory of Computation · Moore and Mealy Machines
Formal Definition of Moore and Mealy Machines
Overview
Finite automata with output extend the DFA/NFA model, which only accepts or rejects, by printing a symbol while processing input. The two such machines, the Moore machine and the Mealy machine, share the same 6-tuple structure and differ only in their output function .
Key concepts
- Finite automaton with output: a machine that takes an input and, while processing it, keeps writing an output. Ordinary DFA and NFA have no output — they only accept or reject.
- Moore machine: a finite automaton with output whose output function is , so an output is associated with every state; the output depends only on the state, not on the input.
- Mealy machine: a finite automaton with output whose output function is , so the output is associated with (state, input) pairs.
- Common 6-tuple: represents both machines.
- — finite set of states (e.g. )
- — input alphabet (same meaning as in ordinary finite automata)
- — transition function
- — initial state
- — output alphabet
- — output function
- Output alphabet : the set of symbols the automaton is allowed to print as output.
- Determinism: transitions are determined completely by (state, input), so both the Moore machine and the Mealy machine are deterministic. They should not be called DFAs, but both are deterministic machines.
- The only difference between a Moore machine and a Mealy machine is the function.
How it works
- The machine reads an input string over , starting in the initial state .
- The transition function gives the next state from the current state and the input symbol — exactly the same behaviour as a DFA.
- The output function determines what is printed:
- In a Moore machine, : each state carries a fixed printed symbol, so the output depends only on the state.
- In a Mealy machine, : for a given state and a given input there is an output, so the output depends on the state and the input.
Drawing the diagram…
Formulas
- — common 6-tuple for both Moore and Mealy machines; = finite set of states, = input alphabet, = transition function, = initial state, = output alphabet, = output function.
- — transition function: (current state, input symbol) → next state; same for both machines.
- Moore machine: — output function maps each state to an output symbol.
- Mealy machine: — output function maps (state, input symbol) to an output symbol.
Worked examples
Moore machine with outputs attached to states:
- State → output 1
- State → output 0
Whenever the machine is in it prints 1, and whenever it is in it prints 0; the printed symbol is fixed by the state alone.
Comparison
| Aspect | Moore machine | Mealy machine |
|---|---|---|
| Output function | ||
| Output depends on | Only the current state | Current state and current input |
| Difference between the two machines | — | Only the function differs |
Revision summary
- DFA and NFA have no output; Moore and Mealy machines are finite automata with output.
- Both machines use the same 6-tuple .
- is identical in both and makes both machines deterministic.
- Both are deterministic, but they should not be called DFAs.
- The only difference between Moore and Mealy is .
- Moore: output attached to states (); Mealy: output attached to (state, input) pairs ().
- is the output alphabet — the set of symbols the machine can print.
Previous-year exam insights
- A Moore machine prints its output for the initial state q0 before any input symbol is read, so an input string of length n produces an output string of length n+1; a Mealy machine produces output only on transitions, so length n input gives length n output.
Asked inGATE 2006 - Moore and Mealy machines are equivalent in expressive power: every Moore machine can be converted to an equivalent Mealy machine and vice versa (a Moore output on a state is shifted onto all incoming transitions; a Mealy output on a transition is placed on the destination state, with the initial state's output handled separately).
- In a Moore machine the output is a function of the present state only, so it is stable for the whole duration of the state; in a Mealy machine the output can change within a state as soon as the input changes, so Mealy machines typically need fewer states but can produce glitches.
- The transition function of a Moore/Mealy machine is total (defined for every state-input pair), so the machine never halts or blocks; unlike a DFA there is no set of final/accepting states — the machine's purpose is to produce an output sequence, not to accept.
- For a Moore machine the output sequence for input w = a1a2...an is λ(q0) λ(δ(q0,a1)) λ(δ(δ(q0,a1),a2)) ...;
for a Mealy machine it is λ(q0,a1) λ(q1,a2) ... where qi is the state reached after i symbols. - When drawing state diagrams: Moore machines label the output inside/next to the state circle, Mealy machines label the output on the transition arrow together with the input symbol (input/output).