Moore and Mealy machines to count number of substring βabβ
Prerequisite: Mealy and Moore Machines, Difference between Mealy machine and Moore machine
Problem: Construction of the machines that take set of all string over {a, b} as input and count number of substring βabβ
Assume,
Ξ = {a, b} and Ξ = {0, 1}
where Ξ and Ξ are the input and output alphabet respectively.
Explanation:
The required Moore machine is constructed below.
In the above diagram, the initial state βXβ on getting βbβ as the input it remains in the state of itself and print β0β as the output and on getting βaβ as the input it transits to a state βYβ and prints β0β as the output.
The state βYβ on getting βaβ as the input it remains in the state of itself and prints β0β as the output and on getting βbβ as the input it transmits to the state βZβ and prints β1β as the output. The state βZβ on getting βaβ as the input it transmits to the state βYβ and prints β0β as the output and on getting βbβ as the input it transmits to the state βXβ and prints β0β as the output.
Thus finally above Moore machine can easily count the number of substring βabβ i.e, on getting βabβ as the input it gives β1β as the output thus on counting the outputs β1β, we can easily count substring βabβ.
Conversion of Moore machine to Mealy machine:
Above Moore machine takes set of all string over {a, b} as input and prints β1β as the output for every occurrence of βabβ as substring. Now we need to transform the above transition diagram of Moore machine to equivalent Mealy machine transition diagram.
Steps for the required conversion are given below:
- Step-1: Formation of State Transition Table of the above Moore machine-
In the above transition table, States βXβ, βYβ and βZβ are kept in the first column which on getting βaβ as the input it transits to βYβ, βYβ and βYβ states respectively, kept in the second column and on getting βbβ as the input it transits to βXβ, βZβ and βXβ states respectively, kept in the third column. In the fourth column under Ξ, there are corresponding outputs of the first column states. In the table, An arrow (β) indicates the initial state. - Step-2: Formation of Transition Table for Mealy machine from above Transition Table of Moore machine-
Below transition table is going to be formed with the help of the above table and its entries just by using the corresponding output of the states of the first column and placing them in the second and third column accordingly.
In the above table, the states in the first column like βXβ on getting βaβ as the input it goes to a state βYβ and gives β0β as the output and on getting βbβ as the input it goes to the state βXβ and gives β0β as the output and so on for the remaining states in the first column. In the table, An arrow (β) indicates the initial state. - Step-3: Then finally we can form the state transition diagram of Mealy machine with help of itβs above transition table.
The required diagram is shown below-
Above Mealy machine takes set of all string over {a, b} as input and prints β1β as the output for every occurrence of βabβ as a substring.
Note: While converting from Moore to Mealy machine the number of states remains same for both Moore and Mealy machine but in case of Mealy to Moore conversion it do not give same number of states.