Theory of Computation-Lecture 1
Theory of Computation-Lecture 1
Theory of Computation-Lecture 1
Example 1:
• Design a FA with ∑ = {0, 1} accepts those string which starts with 1 and ends with 0.
Solution:
• The FA will have a start state q0 from which only the edge with input 1 will go to the next state.
States/ 0 1
symbols
q0 Ø q1
q1 q2 q1
q2* q2 q1
Transition Diagram
Transition Table
Examples of DFA
Example 2:
• Design a FA with ∑ = {0, 1} accepts the only input 101.
Solution:
States/ 0 1
symbols
q0 Ø q1
q1 q2 Ø
q2 Ø q3
q3* Ø Ø
Examples of DFA
Example 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:
1. Deterministic Finite Automata(DFA)
• In a DFA, for a particular input character, the machine goes to one state
only.
• A transition function is defined on every state for every input symbol.
• Also in DFA null (or ε) move is not allowed, i.e., DFA cannot change state
without any input character.
• For example, below DFA with Σ = {0, 1} accepts all strings ending with 0.
• One important thing to note is, there can be many possible DFAs for a
pattern. A DFA with minimum number of states is generally preferred .
NFA
• For example, below DFA with Σ = {0, 1} accepts all strings ending with
0.
• One important thing to note is, in NFA, if any path for an input
string leads to a final state, then the input string accepted.
• For example, in above NFA, there are multiple paths for input string
“00”. Since, one of the paths leads to a final state, “00” is accepted
by above NFA.
Examples of DFA
Example 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:
Examples of DFA
Example 7:
• Draw a DFA for the language accepting strings with substring ’01’ over input alphabets ∑ = {0, 1}
Examples of DFA
Example 8:
• Draw a DFA for the language accepting strings ending with ‘abb’ over input alphabets ∑ = {a, b}
Examples of DFA
Example 9:
• Draw a DFA for the language accepting strings with substring ‘0011’ over input alphabets ∑ = {0,
1}
Examples of NFA
Example 1:
Design an NFA with ∑ = {0, 1} accepts all string ending with 01.
Solution:
• 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:
Examples of NFA
Example 4:
Design NFA which accepts any binary string that contains 00 or 11 as a substring.
Solution:
• We have show 2 paths for 2 substrings . One for 00 substring and another for 11 substring.
• In above NFA, we have shown upper path for 00 substring and lower path for 11 substring.
Converting NFA to DFA
The following steps are followed to convert a given NFA to a DFA-
Step 1:
• Let Q’ be a new set of states of the DFA. Q’ is null in the starting.
• Let T’ be a new transition table of the DFA.
Step 2:
• Add start state of the NFA to Q’.
• Add transitions of the start state to the transition table T’.
• If start state makes transition to multiple states for some input alphabet, then treat
those multiple states as a single state in the DFA.
• In NFA, if the transition of start state over some input alphabet is null, then perform the
transition of start state over that input alphabet to a dead state in the DFA.
Step 3:
• If any new state is present in the transition table T’,
• Add the new state in Q’.
• Add transitions of that state in the transition table T’.
Step 4:
• Keep repeating Step 3 until no new state is present in the transition table T’.
• Finally, the transition table T’ so obtained is the complete transition table of the
required DFA.
Example:
• Convert the following Non-Deterministic Finite Automata (NFA) to
Deterministic Finite Automata (DFA)
Solution:
State / Alphabet a b
→q0 q0 q0, q1
q1 – *q2
*q2 – –
Step 1:
• Let Q’ be a new set of states of the Deterministic Finite Automata (DFA).
• Let T’ be a new transition table of the DFA.
Step 2:
• Add transitions of start state q0 to Step 4:
the transition table T’. • New state present in state Q’ is {q0,
q1, q2}.
State /
a b • Add transitions for set of states {q0,
Alphabet
q1, q2} to the transition table T’.
→q0 q0 {q0, q1}
State /
Step 3: Alphabet a b
State / a b
Alphabet
→q0 q0 {q0, q1}
Solution:
• For the given transition diagram we will first construct the transition table.
State / Alphabet 0 1
→q0 q0 q1
q1 {q1, q2} q1
State / Alphabet 0 1
Solution:
State / Alphabet a b
→q0 q0 q0, q1
q1 – *q2
*q2 – –
Step 1:
• Let Q’ be a new set of states of the Deterministic Finite Automata (DFA).
• Let T’ be a new transition table of the DFA.
Step 2:
• Add transitions of start state q0 to Step 4:
the transition table T’. • New state present in state Q’ is {q0,
q1, q2}.
State /
a b • Add transitions for set of states {q0,
Alphabet
q1, q2} to the transition table T’.
→q0 q0 {q0, q1}
State /
Step 3: Alphabet a b
State / a b
Alphabet
→q0 q0 {q0, q1}
Solution:
• For the given transition diagram we will first construct the transition table.
State / Alphabet 0 1
→q0 q0 q1
q1 {q1, q2} q1
State / Alphabet 0 1
• Step 1: We will take the ε-closure for the starting state of NFA
as a starting state of DFA.
• Step 2: Find the states for each input symbol that can be
traversed from the present. That means the union of transition
value and their closures for each state of NFA present in the
current state of DFA.
• Step 3: If we found a new state, take it as current state and
repeat step 2.
• Step 4: Repeat Step 2 and Step 3 until there is no new state
present in the transition table of DFA.
• Step 5: Mark the states of DFA as a final state which contains
the final state of NFA.
Example 1:
Convert the given NFA into its equivalent DFA.
Solution:
Let us obtain ε-closure of each state.
• ε-closure {q0} = {q0, q1, q2}
• ε-closure {q1} = {q1}
• ε-closure {q2} = {q2}
• ε-closure {q3} = {q3}
• ε-closure {q4} = {q4}
Now, let ε-closure {q0} = {q0, q1, q2} be state A.
Hence
δ'(A, 0) = ε-closure {δ((q0, q1, q2), 0) }
= ε-closure {δ(q0, 0) ∪ δ(q1, 0) ∪ δ(q2, 0) }
= ε-closure {q3}
= {q3} call it as state B.
δ'(A, 1) = ε-closure {δ((q0, q1, q2), 1) }
= ε-closure {δ((q0, 1) ∪ δ(q1, 1) ∪ δ(q2, 1) }
= ε-closure {q3}
= {q3} = B.
For state C:
δ'(C, 0) = ε-closure {δ(q4, 0) }
= ϕ
δ'(C, 1) = ε-closure {δ(q4, 1) }
= ϕ
• Solution:
• Step 1: In the given DFA, q2 and q4 are the unreachable states so remove them.
• Step 2: Draw the transition table for the rest of the states.
State\ Alphabet 0 1
→q0 q1 q3
q1 q0 q3
*q3 q5 q5
*q5 q5 q5
Step 3: Now divide rows of transition table into two sets as:
1. One set contains those rows, which start from non-final states:
State\Alphabet 0 1
q0 q1 q3
q1 q0 q3
2. Another set contains those rows, which starts from final states.
State\Alphabet 0 1
q3 q5 q5
q5 q5 q5
State\Alphabet 0 1
q3 q3 q3
.
State\Alphabet 0 1
→q0 q1 q3
q1 q0 q3
*q3 q3 q3
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, 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:
Input 1 0 1 1
State q0 q2 q1 q2 q2
Solution:
Transition table for above Mealy
machine is as follows:
Transition Table for Moore machine • Transition diagram for Moore
will be: machine will be:
Conversion from Moore machine to Mealy
Machine
Example 1:
• Convert the following Moore Solution:
machine into its equivalent Mealy • The transition table of given Moore machine
machine.
is as follows:
Q a b Outpu
t(λ)
q0 q0 q1 0
Moore machine
q1 q0 q1 1
Mealy machine
• The equivalent Mealy machine can be • Hence the transition table for
obtained as follows: the Mealy machine can be
drawn 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))
= λ(q0)
= 0
λ' (q1, b) = λ(δ(q1, b)) • The equivalent Mealy machine
= λ(q1) will be,
= 1
Example 2: Solution:
• Convert the given Moore machine • The transition table of given
into its equivalent Mealy machine. Moore machine is as follows:
Q a b Outpu
t(λ)
q0 q1 q0 0
Moore machine
q1 q1 q2 0
q2 q1 q0 1
Mealy machine
The equivalent Mealy machine can • Hence the transition table for the
be obtained as follows: Mealy machine can be drawn as
λ' (q0, a) = λ(δ(q0, a)) follows:
= λ(q1)
= 0
λ' (q0, b) = λ(δ(q0, b))
= λ(q0)
= 0
The λ for state q1 is as follows:
λ' (q1, a) = λ(δ(q1, a))
= λ(q1)
= 0
λ' (q1, b) = λ(δ(q1, b))
= λ(q2)
• The equivalent Mealy machine will
= 1
The λ for state q2 is as follows:
be,
λ' (q2, a) = λ(δ(q2, a))
= λ(q1)
= 0
λ' (q2, b) = λ(δ(q2, b))
= λ(q0)
= 0
Conversion from Mealy machine to Moore
Machine
Example 2:
• Convert the following Mealy machine into equivalent Moore machine.
Solution:
• Transition table for above Mealy machine is as follows:
• For state q1, there is only one incident edge with
output 0. So, we don't need to split this state in • Transition diagram for
Moore machine. Moore machine will be:
• 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).
• 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). Given mealy machine
• For state q4, there is only one incident edge with
output 0. So, we don't need to split this state in
Moore machine.
• Transition table for Moore machine will be:
Solution:
S q0 q0,a q0,e q0, i q0,o q0,u
Transition Diagram
Difference Between Moore and Mealy Machine