Theory of Computation-Lecture 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 78

Theory of Computation

What is Theory of Automata and why we study it


Language, Alphabet, String, Word
Kleene closure
• Given an alphabet ∑, the kleene closure of ∑ is a
language given by
• ∑* = ∑0 U ∑1 U ∑2 …..
• E.g if ∑ ={0,1)
then
∑* ={ε,0,1,00,01,10,11,000,111,101,110,……}
Formal Language VS Informal Language
Strings
• The examples of language are:
1. The language of all strings where the string represents binary
number.
{0,1,00,01,10,11,….}
2. The set of strings of 0’s and 1’s with an equal number of each.
{ε,01,10,0011,1100,0101,1010,1100,…}
3. The set of strings of 0’s and 1’s ending in 11.
{11,011,111,0011,0111,1011,1111,…..}
Finite Automata
Finite Automata
Finite Automata
Finite Automata Types
Deterministic Finite Automata- DFA
DFA
Dead State/ Rejecting State/ Trap State
Examples of DFA

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:

• Hence, NFA would be:


Examples of NFA
Example 2:
Design an NFA with ∑ = {0, 1} in which double '1' is followed by double
'0'.
Solution:

• Now before double 1, there can be any string of 0 and 1. Similarly,


after double 0, there can be any string of 0 and 1.
• Hence the NFA becomes:
Examples of NFA
Example 3:
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:
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

• New state present in state Q’ is {q0, →q0 q0 {q0, q1}


q1}. {q0, q1} q0 {q0, q1, q2}
• Add transitions for set of states {q0, {q0, q1, q2} q0 {q0, q1, q2}
q1} to the transition table T’.
State / Alphabet a b

→q0 q0 {q0, q1}

{q0, q1} q0 {q0, q1, q2}


Step 5: • Now, Deterministic Finite Automata
• Since no new states are left to be (DFA) may be drawn as-
added in the transition table T’, so
we stop.
• States containing q2 as its
component are treated as final
states of the DFA.
• Finally, Transition table for
Deterministic Finite Automata
(DFA) is-

State / a b
Alphabet
→q0 q0 {q0, q1}

{q0, q1} q0 *{q0, q1, q2}

*{q0, q1, q2} q0 *{q0, q1, q2}


NFA to DFA conversion
Example:
Convert the given NFA to DFA.

Solution: 
• For the given transition diagram we will first construct the transition table.

State / Alphabet 0 1

→q0 q0 q1

q1 {q1, q2} q1

*q2 q2 {q1, q2}


• Transition table for DFA is

State / Alphabet 0 1

→[q0] [q0] [q1]

[q1] [q1, q2] [q1]

*[q2] [q2] [q1, q2]

*[q1, q2] [q1, q2] [q1, q2]

• The Transition diagram will be:


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

• New state present in state Q’ is {q0, →q0 q0 {q0, q1}


q1}. {q0, q1} q0 {q0, q1, q2}
• Add transitions for set of states {q0, {q0, q1, q2} q0 {q0, q1, q2}
q1} to the transition table T’.
State / Alphabet a b

→q0 q0 {q0, q1}

{q0, q1} q0 {q0, q1, q2}


Step 5: • Now, Deterministic Finite Automata
• Since no new states are left to be (DFA) may be drawn as-
added in the transition table T’, so
we stop.
• States containing q2 as its
component are treated as final
states of the DFA.
• Finally, Transition table for
Deterministic Finite Automata
(DFA) is-

State / a b
Alphabet
→q0 q0 {q0, q1}

{q0, q1} q0 *{q0, q1, q2}

*{q0, q1, q2} q0 *{q0, q1, q2}


NFA to DFA conversion
Example:
Convert the given NFA to DFA.

Solution: 
• For the given transition diagram we will first construct the transition table.

State / Alphabet 0 1

→q0 q0 q1

q1 {q1, q2} q1

*q2 q2 {q1, q2}


• Transition table for DFA is

State / Alphabet 0 1

→[q0] [q0] [q1]

[q1] [q1, q2] [q1]

*[q2] [q2] [q1, q2]

*[q1, q2] [q1, q2] [q1, q2]

• The Transition diagram will be:


NFA with ε

• Non-deterministic finite automata(NFA) is a finite automata where


for some cases when a specific input is given to the current state,
the machine goes to multiple states or more than 1 states.
• It can contain ε move.
• It can be represented as M = { Q, ∑, δ, q0, F}.
Where
 Q: finite set of states  
 ∑: finite set of the input symbol  
 q0: initial state   
 F: final state  
 δ: Transition function  
• NFA with ∈ move: If any FA contains ε transaction or move, the
finite automata is called NFA with ∈ move.
• ε-closure: ε-closure for a given state A means a set of states which
can be reached from the state A with only ε(null) move including
the state A itself.
Steps for converting NFA with ε to DFA:

• 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 the ε-closure of each state.


• ε-closure(q0) = {q0, q1, q2}  
• ε-closure(q1) = {q1, q2}  
• ε-closure(q2) = {q2}  
Now we will obtain δ' transition. Let ε-closure(q0) = {q0, q1, q2} call it
as state A.

δ'(A, 0) = ε-closure{δ((q0, q1, q2), 0)}


              = ε-closure{δ(q0, 0) ∪ δ(q1, 0) ∪ δ(q2, 0)}
              = ε-closure{q0}
              = {q0, q1, q2}
 
δ'(A, 1) = ε-closure{δ((q0, q1, q2), 1)}
              = ε-closure{δ(q0, 1) ∪ δ(q1, 1) ∪ δ(q2, 1)}
              = ε-closure{q1}
              = {q1, q2}         call it as state B

δ'(A, 2) = ε-closure{δ((q0, q1, q2), 2)}


              = ε-closure{δ(q0, 2) ∪ δ(q1, 2) ∪ δ(q2, 2)}
              = ε-closure{q2}
              = {q2}         call it state C

Thus we have obtained


• δ'(A, 0) = A  
• δ'(A, 1) = B  
• δ'(A, 2) = C  

The partial DFA will be:


Now we will find the transitions on states B and C for each
input.
Hence
δ'(B, 0) = ε-closure{δ((q1, q2), 0)}
              = ε-closure{δ(q1, 0) ∪ δ(q2, 0)}
              = ε-closure{ϕ}
              = ϕ
δ'(B, 1) = ε-closure{δ((q1, q2), 1)}
              = ε-closure{δ(q1, 1) ∪ δ(q2, 1)}
              = ε-closure{q1}
              = {q1, q2}         i.e. state B itself
δ'(B, 2) = ε-closure{δ((q1, q2), 2)}
              = ε-closure{δ(q1, 2) ∪ δ(q2, 2)}
              = ε-closure{q2}
              = {q2}         i.e. state C itself

Thus we have obtained


•δ'(B, 0) = ϕ  
•δ'(B, 1) = B  
•δ'(B, 2) = C  
The partial transition diagram will • δ'(C, 2) = ε-closure{δ(q2, 2)}
be: •               = {q2}

Hence the DFA is:

• Now we will obtain transitions for


C:
• δ'(C, 0) = ε-closure{δ(q2, 0)}
•               = ε-closure{ϕ}
•        =ϕ
• δ'(C, 1) = ε-closure{δ(q2, 1)}
•               = ε-closure{ϕ}
•               = ϕ 
Example:
Convert the NFA with ε 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.

The partial DFA will be


Now,
δ'(B, 0) = ε-closure {δ(q3, 0) }
              = ϕ
δ'(B, 1) = ε-closure {δ(q3, 1) }
              = ε-closure {q4}
              = {q4}           i.e. state C

For state C:
δ'(C, 0) = ε-closure {δ(q4, 0) }  
              = ϕ  
δ'(C, 1) = ε-closure {δ(q4, 1) }  
              = ϕ  

The DFA will be,


Minimization of DFA
• Minimization of DFA means reducing the number of states from given FA. Thus, we
get the FSM(finite state machine) with redundant states after minimizing the FSM.
• We have to follow the various steps to minimize the DFA. These are as follows:
• Step 1: Remove all the states that are unreachable from the initial state via any set of
the transition of DFA.
• Step 2: Draw the transition table for all pair of states.
• Step 3: Now split the transition table into two tables T1 and T2. T1 contains all final
states, and T2 contains non-final states.
• Step 4: Find similar rows from T1 such that:
1. δ (q, a) = p  
2. δ (r, a) = p  
• That means, find the two states which have the same value of a and b and remove
one of them.
• Step 5: Repeat step 3 until we find no similar rows available in the transition table T1.
• Step 6: Repeat step 3 and step 4 for table T2 also.
• Step 7: Now combine the reduced T1 and T2 tables. The combined transition table is
the transition table of minimized DFA.
Example:

• 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

Step 4: Set 1 has no similar rows so set 1 will be the same.


Step 5: In set 2, row 1 and row 2 are similar since q3 and q5 transit to the same
state on 0 and 1. So skip q5 and then replace q5 by q3 in the rest.

State\Alphabet 0 1

q3 q3 q3
.

• Step 6: Now combine set 1 and set 2 as:

State\Alphabet 0 1

→q0 q1 q3

q1 q0 q3

*q3 q3 q3

• Now it is the transition table of minimized DFA.


Moore Machine
• Moore machine is a finite state machine in which the next state is
decided by the current state and current input symbol.
• The output symbol at a given time depends only on the present
state of the machine.
• Moore 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 symbols  
• O: output alphabet  
• δ: transition function where Q × ∑ → Q  
• λ: output function where Q → O 
Example 1: • Transition table for Moore
• The state diagram for Moore Machine is:
Machine is

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:

For instance, take one binary number


1011 then

Input 1 0 1 1

State q0 q2 q1 q2 q2

Thus Moore machine M = (Q, q0, ∑,


Output 0 0 1 0 0 O, δ, λ); where Q = {q0, q1, q2}, ∑ =
{0, 1}, O = {0, 1}. the transition table
shows the δ and λ functions.
Example 3:
• 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.
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:
Design a mealy machine to find 2’s complement of given number
Solution:
• 2’s complement of a given number can be found by changing bits from right
end till the first 1 and then complementing the remaining bits .
• E.g . 2’s complement of binary number 0101101000 is calculated as
0 1 0 1 1 0 1 0 0 0 1 0 1 0 0 1 1 0 0 0
Conversion from Mealy machine to Moore Machine

Example 1: • The state q1 has only one output. The state q2


Convert the following Mealy machine and q3 have both output 0 and 1. So we will
into equivalent Moore machine. 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).
• Transition table for Moore machine will be:

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:

Moore machine after conversion


Mealy machine
Example:
• Design mealy machine that will Transition Table
read the sequences made up of Current Next state output
vowels of English language. It will state
give output in same sequences,
except in cases where ‘i’ follows ‘e’
, it will be changed to u. a e i o u

Solution:
S q0 q0,a q0,e q0, i q0,o q0,u

q1 q0,a q0,e q0,u q0,o q0,u

Transition Diagram
Difference Between Moore and Mealy Machine

Moore Machine – Mealy Machine –


• Output depends only upon present state. • Output depends on present state as well as
• If input changes, output does not change. present input.
• More number of states are required. • If input changes, output also changes.
• Less number of states are required.
• There is less hardware requirement for
circuit implementation. • There is more hardware requirement for
circuit implementation.
• They react faster to inputs.
• They react slower to inputs(One clock cycle
• Synchronous output and state generation. later).
• Output is placed on states. • Asynchronous output generation.
• Easy to design. • Output is placed on transitions.
• It is difficult to design.
Difference between DFA and NFA
• DFA stands for Deterministic • NFA stands for Nondeterministic
Finite Automata. Finite Automata.
• DFA cannot use Empty String • NFA can use Empty String
transition. transition.
• DFA can be understood as • NFA can be understood as multiple
one machine. little machines computing at the
• DFA is more difficult to same time.
construct. • NFA is easier to construct.
• Time needed for executing • Time needed for executing an
an Input string is less. input string is more.
• All DFA are NFA. • Not all NFA are DFA.

You might also like