Formal Language and Automata Theory: UNIT-1
Formal Language and Automata Theory: UNIT-1
Computational Problem:
Computation:
“Computation” can be defined as finding a solution to a problem from the given inputs by
means of an algorithm.
Automata theory:
It deals with the formal definitions and properties of mathematical models of computation.
These models play a role in several applied areas of computer science. One model, called the
finite automaton, is used in text processing, compilers, and hardware design. Another model,
called the context-free grammar, is used in programming languages and artificial intelligence.
Proper prefix: prefix of a string other than itself is called proper prefix.
Example: w=sri, |w|=3 proper prefix= sr, s, ɛ.
suffix of a string: suffix of a string is formed by removing zero or more leading symbols of
the string. For a string of length n, number of suffixes= n+1.
Example: w=sri, suffix=sri,ri,i,ɛ.
Proper suffix: suffix of a string other than itself is called proper suffix.
Example: w=sri, proper suffix= ri,i,ɛ.
substring of a string: substring of a string is formed by removing either prefix or suffix or
both from a string excluding ɛ. For a string of length n, number of substrings= n*(n+1)/2.
Example: w=sri, substring= sri, sr,s,ri,i,r
Language: language is a collection of strings formed from a particular alphabet.
Example: if ∑= {0, 1}, then the language of all strings consisting of n 0' s followed by n l' s,
for some n≥0 is L= {ɛ, 01,0011,000111,. .}.
Operations on strings:
1. Concatenation of a string
2. Kleene closure of a string
3. Positive closure of a string
4. Reverse of a string
Concatenation of a string: concatenation of a string is obtained by appending second string
at the end of first string. If w1 and w2 are strings, then w1.w2 or w1w2 denotes the
concatenation of w1 and w2.
Operations on languages:
1. Union of a language
2. Intersection of a language
3. Difference of a language
4. Complement of a language
5. Concatenation of a language
6. Kleene closure of a language
7. Positive closure of a language
Union of a language: Union of two languages is defined as collection of strings from L1 as
well as from L2. It is denoted as L1∪L2.
L1∪L2={w| w in L1 or w in L2}
LC =∑* −L
Example: if L= {set of strings that starts with 010} over ∑= {0, 1}
then LC =∑* −L ={set of all strings} −{set of strings that starts with 010}
= { set of strings that does not starts with 010}
Concatenation of a language: Concatenation of a two languages is obtained by appending
every string in second language at the end of every string in first language. If L1 and L2 are
two languages, then L1.L2 or L1L2 denotes the concatenation of L1 and L2.
Finite State Machine/ Finite Automata: “Finite State Machine” is a model of computation
which is used to simulate the behavior of a machine or a system.
Input tape:
It is used to store the input string that is to be processed by Finite State Machine. The input is
finite and taken from a set of input alphabets ∑.
Read head:
It is used to read one symbol at a time from input tape and moves from left to right.
Initially it points to the leftmost symbol on the tape and it is controlled by “Finite control”.
Finite Control:
Finite control contains a set of states which are connected by transitions. In finite control, it is
decided that “machine is in this state and it is getting this input, so it will go to this state”.
2. Elements of Finite State Machine: The main elements of Finite State Machine are
1. State
2. Rules
3. State Transition
4. Input Events
1) State :-
This element defines the behavior of the system and generate action to be performed. There
are different types of states. They are a. Start state
b. Next state
c. Accepting state
d. Dead state
e. Inaccessible state
f. Universal State
g. Existential State
Types of states:
a. Start state: State which is used to start processing of input string in finite state
machine is called as “start state” or “initial state”.
b. Next state: State which is defined by the transition function of finite state machine for
current state qi and input symbol x is called as “next state”.
𝛿(𝑞𝑖 , 𝑥) → 𝑞
c. Accepting state: Once entire string is processed, state which leads to acceptance of
string is called as “Accepting state” or “final state”.
d. Dead state: A non final state of finite state machine whose transitions on every input
symbol terminates on itself is called as “dead state”.
𝑞 ∉ 𝐹 𝑎𝑛𝑑 𝛿(𝑞𝑖 , Σ) → qi
e. Inaccessible state: State which can never be reached from initial state is called
“inaccessible state” or “useless state”.
f. Equivalent state: Two states are “equivalent” or “indistinguishable”, if both
produces final states or if both produces non final states on applying same input
symbol.
𝛿(𝑞𝑖 , 𝑥) ∈ 𝐹 → 𝛿(𝑞𝑗 , 𝑥) ∈ 𝐹
𝛿(𝑞𝑖 , 𝑥) ∈ 𝑁𝐹 → 𝛿(𝑞𝑗 , 𝑥) ∈ 𝑁F
g. Distinguishable state: Two states are “not equivalent” or “distinguishable”, if both
produces final states or if both produces non final states on applying same input
symbol.
𝛿(𝑞𝑖 , 𝑥) ∈ 𝐹 → 𝛿(𝑞𝑗 , 𝑥) ∈ 𝑁𝐹
𝛿(𝑞𝑖 , 𝑥) ∈ 𝑁𝐹 → 𝛿(𝑞𝑗 , 𝑥) ∈ F
2. Rules:
This element defines the conditions that are to be satisfied for enabling state transition.
3. State transition:
This element defines the change in state. i,e., moving from one state to another state is known
as transition. Transitions are represented in 3 ways.
They are 1. Transition diagram
2. Transition table
3. Transition function
Types of transition:
I. Transition diagram/ Transition graph/ Transition system:
A diagrammatical representation of states and transitions is called as transition
diagram. In transition diagram, circles are used to represent states and directed arrows
are used to represent transitions between states.
Representations:
Start state
Final state
Other states
Computation begins in start state with input string in input tape. The read head scans the
input from input tape and sends it to finite control.
Based on current state and input symbol of finite state machine, next state will be determined.
After processing the entire string, if the finite control enters into one of the final states, FSM
declares that string is accepted and recognizes that string belongs to the given language.,
otherwise it declares that string doesn’t belongs to the given language.
Transition System:
A transition system is formally defined as a directed labeled graph where each vertex is a
state, and directed edge is a transition between two states and edges are labeled with
input/output.
A transition system is a 5-tuple (𝑸, 𝚺, 𝜹, 𝒒𝟎,𝑭) where
𝑄 − 𝑓𝑖𝑛𝑖𝑡𝑒 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑠𝑡𝑎𝑡𝑒𝑠
Σ − 𝑖𝑛𝑝𝑢𝑡 𝑎𝑙𝑝ℎ𝑎𝑏𝑒𝑡 𝑜𝑟 𝑠𝑒𝑡 𝑜𝑓 𝑖𝑛𝑝𝑢𝑡 𝑠𝑦𝑚𝑏𝑜𝑙𝑠
𝛿 − 𝑡𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑑𝑒𝑓𝑖𝑛𝑒𝑑 𝑏𝑦 𝛿:𝑄𝑋Σ∗→𝑄
𝑞0 − 𝑠𝑡𝑎𝑟𝑡 𝑠𝑡𝑎𝑡𝑒 𝑞0 ∈ 𝑄
𝐹 − 𝑠𝑒𝑡 𝑜𝑓 𝑓𝑖𝑛𝑎𝑙 𝑠𝑡𝑎𝑡𝑒𝑠 𝐹 ⊆ 𝑄 i.e if (𝑞0, w. 𝑞2) is in 𝜹.
it means that the graph starts at the vertex 𝑞0, goes along a set of edges, and reaches the vertex
𝑞2 . The concatenation of the label of all the edges thus encountered is w.
i.e., A string w is accepted by finite automata M = (𝑸, 𝚺, 𝜹, 𝒒𝟎,𝑭) if 𝛿(𝑞0, 𝑤) = 𝑞f for some
𝑞𝑓 𝜖 F. This is basically acceptability of a string by the final state.
Types of Finite Automata: Finite Automata can be classified into 2 types.
They are 1. Finite Automata without output (recognizers)
2. Finite Automata with output(tranducers)
1. Finite Automata without output are classified into 3 types. They are
Deterministic Finite Automata (DFA)
Non Deterministic Finite Automata (NFA)
Non Deterministic Finite Automata with Epsilon transitions (NFA-ℇ)
Minimized DFA
2. Finite Automata with output are classified into 2 types. They are
Moore Machine
Mealy Machine
Working of DFA:
• Initially DFA is assumed to be in its start state q0 with its read head on leftmost
symbol of input string in input tape.
• The read head scans the input from input tape and sends it to finite control.
• Based on current state and input symbol of finite state machine, next state will be
determined by finite control.
• During each move, read head moves one position to the right.
• After processing the entire string, if the DFA enters into one of the final states, the
string is accepted, otherwise the string is rejected.
Design of DFAs:
1. Write the strings in the given language using the given input alphabet ∑.
2. Design DFA for the minimum string in the language.
3. At each state, for the input symbol which has no transition, put a loop over that
particular state with that input symbol.
4. If the language is satisfied for that loop on that state, then check for next state.
Otherwise, put the transition for the previous state with that input symbol.
5. If then also, it is not satisfied, then put the transition for the previous state with that
input symbol.
6. If it is a start state, include a new state called as dead state.
7. Repeat the steps 2-5 until each and every state in DFA is having a transition with each
and every input symbol.
• Initially NFA is assumed to be in its start state q0 with its read head on leftmost
symbol of input string in input tape.
• The read head scans the input from input tape and sends it to finite control.
• Based on current state and input symbol of finite state machine, several choices may
exist for the next state. Then the machine chooses one of them and continues.
• If the next input symbol matches, it continues. Otherwise, it chooses another way.
• During each move, read head moves one position to the right.
• After processing the entire string, if the NFA enters into one of the final states, the
string is accepted, otherwise the string is rejected.
Design of NFAs:
1. Write the strings in the given language using the given input alphabet ∑.
2. Design NFA for the minimum string in the language.
3. Put the transition with the input symbol wherever necessary, such that all strings of
given language are accepted by NFA.
Equivalence of DFA and DFA: Two DFAs M1 and M2 are said to be equivalent, if the
language accepted by 2 DFAs is same irrespective of number of states. i.e., if L(M1)=L(M2),
then M1 and M2 are equivalent.
Procedure:
1. Draw transition table format with input symbols and pair of states. Each pair of states
contains state from M1 and state from M2.
2. Initially take start states of M1 and M2 as a pair and generate new pair of states with
the input symbols of DFA.
3. During the process of generating new pair of states, if a pair of type (F,NF) or (NF,F)
is generated, then immediately stop the process and conclude that given DFAs are not
equivalent.
4. During the process of generating pairs of states, if a pair of type (F,F) or (NF,NF) is
generated, then repeat the process until no new pair of states are found and conclude
that given DFAs are equivalent.
ɛ - transitions: ɛ - transitions are the transitions by using which NFA can change its state
without reading any input symbol.
ɛ - closure: ɛ - closure of a state is set of all the states reachable from that state by using only ɛ -
transitions including itself.
ɛ-closure(q0)={q0,q1,q2} for
Mathematical representation of NFA-ɛ:
• Initially NFA-ɛ is assumed to be in its start state q0 with its read head on leftmost
symbol of input string in input tape.
• The read head scans the input from input tape and sends it to finite control.
• Based on current state and input symbol of finite state machine, several choices may
exist for the next state. Then the machine chooses one of them and continues.
• If there is no input symbol, then using epsilon transition, it moves to next state and
continues. If the next input symbol matches, it continues. Otherwise, it chooses
another way.
• During each move, read head moves one position to the right.
• After processing the entire string, if the NFA-ɛ enters into one of the final states, the
string is accepted, otherwise the string is rejected.
Design of NFA-ɛ:
1. Write the strings in the given language using the given input alphabet ∑.
2. Design NFA- ɛ for the minimum string in the language.
3. Put the transition with the ɛ symbol wherever necessary, such that all strings of given
language are accepted by NFA-ɛ.
Procedure to minimize DFA: There are 2 ways to minimize DFA. They are
1. Equivalence method
2. Myhill-Nerode theorem
1. Equivalence method:
Def. of k-equivalent: Two states are said to be “k-equivalent” (k≥0) , if both produces final
states or if both produces non final states on applying same input of length k or less.
Def. of K=0, 0-equivalent: The only string with length “0” is ɛ. If ɛ is applied on any state, it
cannot reach a final state, but if ɛ is applied on any state, it reaches a final state, since the ɛ- closure
of any state includes the state itself.
Therefore at level “0”, behavior of final states differ from non final states. so, partition the states
into 2 groups, final and non final. It is denoted by 𝜋0.
Def. of K=1, 1-equivalent: It is denoted by 𝜋1. Equivalence at level 1 can exist if and only if there
is equivalence at level “0”. Therefore here equivalence is to be checked among the states that were
equivalent in level 0 (subsets of 𝜋0). States having the same behavior will be grouped.
Procedure:
MOORE MACHINE:
Definition: It was proposed by Edward F. Moore. Moore machine is a finite automata with output
where output depends on present state only. In this machine, states are labeled with state name and
output.
Mathematical representation of Moore machine:
Moore machine is formally defined as 6-tuple 𝑴 = (𝑸, 𝚺, 𝚫, 𝜹, 𝝀, 𝒒𝟎) where
𝑀 = 𝑀𝑜𝑜𝑟𝑒 𝑀𝑎𝑐ℎ𝑖𝑛𝑒
𝑄 − 𝑓𝑖𝑛𝑖𝑡𝑒 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑠𝑡𝑎𝑡𝑒𝑠
Σ − 𝑖𝑛𝑝𝑢𝑡 𝑎𝑙𝑝ℎ𝑎𝑏𝑒𝑡 𝑜𝑟 𝑠𝑒𝑡 𝑜𝑓 𝑖𝑛𝑝𝑢𝑡 𝑠𝑦𝑚𝑏𝑜𝑙𝑠
Δ − 𝑜𝑢𝑡𝑝𝑢𝑡 𝑎𝑙𝑝ℎ𝑎𝑏𝑒𝑡 𝑜𝑟 𝑠𝑒𝑡 𝑜𝑓 𝑜𝑢𝑡𝑝𝑢𝑡 𝑠𝑦𝑚𝑏𝑜𝑙𝑠
𝛿 − 𝑖𝑛𝑝𝑢𝑡 𝑡𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑑𝑒𝑓𝑖𝑛𝑒𝑑 𝑏𝑦 𝛿: 𝑄𝑋Σ → Q
𝜆 − 𝑜𝑢𝑡𝑝𝑢𝑡 𝑡𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑑𝑒𝑓𝑖𝑛𝑒𝑑 𝑏𝑦 𝛿:𝑄 → Δ
𝑞0 − 𝑠𝑡𝑎𝑟𝑡 𝑠𝑡𝑎𝑡𝑒 , 𝑞0 ∈ 𝑄
MEALY MACHINE:
Definition: It was proposed by George H. Mealy. Mealy machine is a finite automata with output
where output depends on present state and present input. In this machine, transition is labeled with
input and output.
Mathematical representation of Mealy machine:
Mealy machine is formally defined as 6-tuple 𝑴 = (𝑸, 𝚺, 𝚫, 𝜹, 𝝀, 𝒒𝟎) Where
𝑀 -- 𝑀EALY 𝑀𝑎𝑐ℎ𝑖𝑛𝑒
𝑄 − 𝑓𝑖𝑛𝑖𝑡𝑒 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑠𝑡𝑎𝑡𝑒𝑠
Σ − 𝑖𝑛𝑝𝑢𝑡 𝑎𝑙𝑝ℎ𝑎𝑏𝑒𝑡 𝑜𝑟 𝑠𝑒𝑡 𝑜𝑓 𝑖𝑛𝑝𝑢𝑡 𝑠𝑦𝑚𝑏𝑜𝑙𝑠
Δ − 𝑜𝑢𝑡𝑝𝑢𝑡 𝑎𝑙𝑝ℎ𝑎𝑏𝑒𝑡 𝑜𝑟 𝑠𝑒𝑡 𝑜𝑓 𝑜𝑢𝑡𝑝𝑢𝑡 𝑠𝑦𝑚𝑏𝑜𝑙𝑠
𝛿 − 𝑖𝑛𝑝𝑢𝑡 𝑡𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑑𝑒𝑓𝑖𝑛𝑒𝑑 𝑏𝑦 𝛿: 𝑄𝑋Σ → Q
𝜆 − 𝑜𝑢𝑡𝑝𝑢𝑡 𝑡𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑑𝑒𝑓𝑖𝑛𝑒𝑑 𝑏𝑦 𝛿:𝑄𝑋Σ → Δ
𝑞0 − 𝑠𝑡𝑎𝑟𝑡 𝑠𝑡𝑎𝑡𝑒 𝑞0 ∈ 𝑄
Conversion of Moore Machine to Mealy machine:
1. Draw transition table of moore machine.
2. Draw transition table format for mealy machine.
3. Put present states of moore machine in present state of mealy machine.
4. Put next states for corresponding present states and input of moore machine in next states
for those present states and input of mealy machine.
5. For output, consider present state and output of moore machine.
6. Output for next state at input in mealy machine will be Output for that state as present
state in moore machine.
In state q1, if we read 1, we will be in state q1, but if we read 0 at state q1, we will reach to state q2
which is the final state. In state q2, if we read either 0 or 1, we will go to q2 state or q1 state
respectively. Note that if the input ends with 0, it will be in the final state.
In the given solution, we can see that only input 101 will be accepted. Hence, for input 101, there is
no other path shown for other input.
Ex 3: Design FA with ∑ = {0, 1} accepts even number of 0's and even number of 1's.
Solution: This FA will consider four different stages for input 0 and input 1. The stages could be:
Here q0 is a start state and the final state also. Note carefully that a symmetry of 0's and 1's is
maintained. We can associate meanings to each state as:
q0: state of even number of 0's and even number of 1's.
q1: state of odd number of 0's and even number of 1's.
q2: state of odd number of 0's and odd number of 1's.
q3: state of even number of 0's and odd number of 1's.
Ex 4: Design FA with ∑ = {0, 1} accepts the set of all strings with three consecutive 0's.
Solution: The strings that will be generated for this particular languages are 000, 0001, 1000, 10001,
.... in which 0 always appears in a clump of 3. The transition graph is as follows:
Note that the sequence of triple zeros is maintained to reach the final state.
Ex 5: Design a DFA L(M) = {w | w ε {0, 1}*} and W is a string that does not contain
consecutive 1's.
Solution: When three consecutive 1's occur the DFA will be:
The stages q0, q1, q2 are the final states. The DFA will generate the strings that do not contain
consecutive 1's like 10, 110, 101,..... etc.
Ex 6: Design a FA with ∑ = {0, 1} accepts the strings with an even number of 0's followed by
single 1.
Solution: The DFA can be shown by a transition diagram as:
Example Problems of NDFA
1. NFA stands for non-deterministic finite automata. It is easy to construct an NFA than DFA
for a given regular language.
2. The finite automata are called NFA when there exist many paths for specific input from the
current state to the next state.
3. Every NFA is not DFA, but each NFA can be translated into DFA.
4. NFA is defined in the same way as DFA but with the following two exceptions, it contains
multiple next states, and it contains ε transition.
In the following image, we can see that from state q0 for input a, there are two next states q1 and q2,
similarly, from q0 for input b, the next states are q0 and q1. Thus it is not fixed or determined that
with a particular input where to go next. Hence this FA is called non-deterministic finite automata.
Formal definition of NFA: NFA also has five states same as DFA, but with different transition function, as
shown follows:
δ: Q x ∑ →2Q
where, Q: finite set of states
∑: finite set of the input symbol
q0: initial state
F: final state
δ: Transition function
Graphical Representation of an NFA :-An NFA can be represented by digraphs called state
diagram. In which:
1. The state is represented by vertices.
2. The arc labeled with an input character show the transitions.
3. The initial state is marked with an arrow.
4. The final state is denoted by the double circle.
→q0 q0, q1 q1
q1 q2 q0
*q2 q2 q1, q2
In the above diagram, we can see that when the current state is q0, on input 0, the next state will be
q0 or q1, and on 1 input the next state will be q1. When the current state is q1, on input 0 the next
state will be q2 and on 1 input, the next state will be q0. When the current state is q2, on 0 input the
next state is q2, and on 1 input the next state will be q1 or q2.
Solution:
Transition Table:
Present State Next state for Input 0 Next State of Input 1
→q0 q1 ε
q1 ε q2
*q2 q2 q2
Example 3: NFA with ∑ = {0, 1} and accept all string of length at least 2.
Solution:
Transition Table:
Present State Next state for Input 0 Next State of Input 1
→q0 q1 q1
q1 q2 q2
*q2 ε ε
Example 4: Design a NFA for the transition table as given below:
Present State 0 1
Here,
δ(q0, 0) = {q0, q1}
δ(q0, 1) = {q0, q2}
Then, δ(q1, 0) = {q3}
Then, δ(q2, 0) = {q2, q3}
δ(q2, 1) = {q3}
Then, δ(q3, 0) = {q3}
δ(q3, 1) = {q3}
Example 5: Design an NFA with ∑ = {0, 1} accepts all string ending with 01.
Solution:
Example 6:Design an NFA with ∑ = {0, 1} in which double '1' is followed by double '0'.
Solution: The FA with double 1 is as follows:
Example 7: Design an NFA in which all the string contain a substring 1110.
Solution: The language consists of all the string containing substring 1010. The partial transition
diagram can be:
Now as 1010 could be the substring. Hence we will add the inputs 0's and 1's so that the substring
1010 of the language can be maintained. Hence the NFA becomes:
Transition table for the above transition diagram can be given below:
Present State 0 1
→q1 q1 q1, q2
q2 q3
q3 q4
q4 q5
*q5 q5 q5
Consider a string 111010,
δ(q1, 111010) = δ(q1, 1100)
= δ(q1, 100)
= δ(q2, 00)
Got stuck! As there is no path from q2 for input symbol 0. We can process string 111010 in another
way.
δ(q1, 111010) = δ(q2, 1100)
= δ(q3, 100)
= δ(q4, 00)
= δ(q5, 0)
= δ(q5, ε)
As state q5 is the accept state. We get the complete scanned, and we reached to the final state.
Example 8: Design an NFA with ∑ = {0, 1} accepts all string in which the third symbol from the
right end is always 0.
Solution:
Thus we get the third symbol from the right end as '0' always. The NFA can be:
The above image is an NFA because in state q0 with input 0, we can either go to state q0 or q1.
Eliminating ε Transitions
NFA with ε can be converted to NFA without ε, and this NFA without ε can be converted to DFA.
To do this, we will use a method, which can remove all the ε transition from given NFA. The
method will be:
1. Find out all the ε transitions from each state from Q. That will be called as ε-closure{q1}
where qi ∈ Q.
2. Then δ' transitions can be obtained. The δ' transitions mean a ε-closure on δ moves.
3. Repeat Step-2 for each input symbol and each state of given NFA.
4. Using the resultant states, the transition table for equivalent NFA without ε can be built.
Let, M = (Q, ∑, δ, q0, F) is an NFA which accepts the language L(M). There should be equivalent
DFA denoted by M' = (Q', ∑', q0', δ', F') such that L(M) = L(M').
Solution: For the given transition diagram we will first construct the transition table.
State 0 1
→q0 q0 q1
q1 {q1, q2} q1
Solution: For the given transition diagram we will first construct the transition table.
State 0 1
Similarly,
δ'([q0, q1], 1) = δ(q0, 1) ∪ δ(q1, 1)
= {q1} ∪ {q0, q1}
= {q0, q1}
= [q0, q1]
As in the given NFA, q1 is a final state, then in DFA wherever, q1 exists that state becomes a final
state. Hence in the DFA, final states are [q1] and [q0, q1]. Therefore set of final states
F = {[q1], [q0, q1]}. The transition table for the constructed DFA will be:
State 0 1
In the above Moore machine, the output is represented with each input state separated by /.
The output length for a Moore machine is greater than input by 1.
Input: 010
Transition: δ (q0,0) => δ(q1,1) => δ(q1,0) => q2
Output: 1110(1 for q0, 1 for q1, again 1 for q1, 0 for q2)
Example 2: Design a Moore machine to generate 1's complement of a given binary number.
Solution: To generate 1's complement of a given binary number the simple logic is that if the input is
0 then the output will be 1 and if the input is 1 then the output will be 0. That means there are three
states. One state is start state. The second state is for taking 0's as input and produces output as 1. The
third state is for taking 1's as input and producing output as 0.
Hence the Moore machine will be,
For instance, take one binary number 1011 then
Input 1 0 1 1
State q0 q2 q1 q2 q2
Output 0 0 1 0 0
Thus we get 00100 as 1's complement of 1011, we can neglect the initial 0 and the output which we
get is 0100 which is 1's complement of 1011.
The transaction table is as follows:
Thus Moore machine M = (Q, q0, ∑, O, δ, λ); where Q = {q0, q1, q2}, ∑ = {0, 1}, O = {0, 1}. the
transition table shows the δ and λ functions.
Example 3: Design a Moore machine for a binary input sequence such that if it has a substring
101, the machine output A, if the input has substring 110, it outputs B otherwise it outputs C.
Solution: For designing such a machine, we will check two conditions, and those are 101 and 110. If
we get 101, the output will be A, and if we recognize 110, the output will be B. For other strings, the
output will be C.
Now we will insert the possibilities of 0's and 1's for each state. Thus the Moore machine becomes:
Example 4: Construct a Moore machine that determines whether an input string contains an
even or odd number of 1's. The machine should give 1 as output if an even number of 1's are in
the string and 0 otherwise.
Solution: The Moore machine will be:
This is the required Moore machine. In this machine, state q1 accepts an odd number of 1's and state
q0 accepts even number of 1's. There is no restriction on a number of zeros. Hence for 0 input, self-
loop can be applied on both the states.
Example 5: Design a Moore machine with the input alphabet {0, 1} and output alphabet {Y,
N} which produces Y as output if input sequence contains 1010 as a substring otherwise, it
produces N as output.
Solution: The Moore machine will be:
Mealy Machine
A Mealy machine is a machine in which output symbol depends upon the present input symbol and
present state of the machine. In the Mealy machine, the output is represented with each input symbol
for each state separated by /. The Mealy machine can be described by 6 tuples (Q, q0, ∑, O, δ, λ')
where
Q: finite set of states
q0: initial state of machine
∑: finite set of input alphabet
O: output alphabet
δ: transition function where Q × ∑ → Q
λ': output function where Q × ∑ →O
Example 1: Design a Mealy machine for a binary input sequence such that if it has a substring
101, the machine output A, if the input has substring 110, it outputs B otherwise it outputs C.
Solution: For designing such a machine, we will check two conditions, and those are 101 and 110. If
we get 101, the output will be A. If we recognize 110, the output will be B. For other strings the
output will be C.
Example 2: Design a mealy machine that scans sequence of input of 0 and 1 and generates
output 'A' if the input string terminates in 00, output 'B' if the string terminates in 11, and
output 'C' otherwise.
The following steps are used for converting Mealy machine to the Moore machine:
Step 1: For each state(Qi), calculate the number of different outputs that are available in the
transition table of the Mealy machine.
Step 2: Copy state Qi, if all the outputs of Qi are the same. Break qi into n states as Qin, if it has n
distinct outputs where n = 0, 1, 2....
Step 3: If the output of initial state is 0, insert a new initial state at the starting which gives 1 output.
Example 1: Convert the following Mealy machine into equivalent Moore machine.
1. For state q1, there is only one incident edge with output 0. So, we don't need to split this state
in Moore machine.
2. For state q2, there is 2 incident edge with output 0 and 1. So, we will split this state into two
states q20( state with output 0) and q21(with output 1).
3. For state q3, there is 2 incident edge with output 0 and 1. So, we will split this state into two
states q30( state with output 0) and q31( state with output 1).
4. For state q4, there is only one incident edge with output 0. So, we don't need to split this state
in Moore machine.
5. Transition table for Moore machine will be:
The state q1 has only one output. The state q2 and q3 have both output 0 and 1. So we will create two
states for these states. For q2, two states will be q20(with output 0) and q21(with output 1).
Similarly, for q3 two states will be q30(with output 0) and q31(with output 1).
We cannot directly convert Moore machine to its equivalent Mealy machine because the length of
the Moore machine is one longer than the Mealy machine for the given input. To convert Moore
machine to Mealy machine, state output symbols are distributed into input symbol paths. We are
going to use the following method to convert the Moore machine to Mealy machine.
Example 1: Convert the following Moore machine into its equivalent Mealy machine.
Q a b Output(λ)
q0 q0 q1 0
q1 q0 q1 1
Note: The length of output sequence is 'n+1' in Moore machine and is 'n' in the Mealy
machine.
Example 2:Convert the given Moore machine into its equivalent Mealy machine.
Q a b Output(λ)
q0 q1 q0 0
q1 q1 q2 0
q2 q1 q0 1
Example 3:Convert the given Moore machine into its equivalent Mealy machine.
Q a b Output(λ)
q0 q0 q1 0
q1 q2 q0 1
q2 q1 q2 2
Solution: The transaction diagram for the given problem can be drawn as:
The equivalent Mealy machine can be obtained as follows:
λ' (q0, a) = λ(δ(q0, a))
= λ(q0)
=0
λ' (q0, b) = λ(δ(q0, b))
= λ(q1)
=1
The λ for state q1 is as follows:
λ' (q1, a) = λ(δ(q1, a))
= λ(q2)
=2
λ' (q1, b) = λ(δ(q1, b))
= λ(q0)
=0
The λ for state q2 is as follows:
λ' (q2, a) = λ(δ(q2, a))
= λ(q1)
=1
λ' (q2, b) = λ(δ(q2, b))
= λ(q2)
=2
Hence the transition table for the Mealy machine can be drawn as follows: