0% found this document useful (0 votes)
77 views26 pages

Unit 1-1

unit 1

Uploaded by

ashokkumar.aa361
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views26 pages

Unit 1-1

unit 1

Uploaded by

ashokkumar.aa361
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Unit 1

Automata theory (also known as Theory Of Computation) is a theoretical branch of Computer Science
and Mathematics, which mainly deals with the logic of computation with respect to simple machines,
referred to as automata.
Automata enables scientists to understand how machines compute the functions and solve problems. The
main motivation behind developing Automata Theory was to develop methods to describe and analyze the
dynamic behavior of discrete systems.

Basic Terminologies of Theory of Computation:

Symbol:

A symbol (often also called a character) is the smallest building block, which can be any alphabet, letter,
or picture.

Alphabets (Σ):

Alphabets are a set of symbols, which are always finite.

String:

A string is a finite sequence of symbols from some alphabet. A string is generally denoted as w and the
length of a string is denoted as |w|.

Note: Empty string is the string with zero occurrence of symbols, represented as ε.
Number of Strings (of length 2)
that can be generated over the alphabet {a, b}:
- -
a a
a b
b a
b b
Length of String |w| = 2, Number of Strings = 4
Conclusion:
For alphabet {a, b} with length n, number of strings can be generated = 2n.

Note: If the number of symbols in the alphabet Σ is represented by |Σ|, then a number of
strings of length n, possible over Σ is |Σ| n .

Closure Representation in TOC:

L+ : It is a Positive Closure that represents a set of all strings except Null or ε-strings.

L*: It is “Kleene Closure“, that represents the occurrence of certain alphabets for given language
alphabets from zero to the infinite number of times. In which ε-string is also included.
From the above two statements, it can be concluded that:
L* = εL+

Example:
(a) Regular expression for language accepting all combination of g's over Σ={g}:
R = g*
R={ε,g,gg,ggg,gggg,ggggg,...}

(b) Regular Expression for language accepting all combination of g's over Σ={g} :
R = g+
R={g,gg,ggg,gggg,ggggg,gggggg,...}

Note: Σ* is a set of all possible strings(often power set(need not be unique here or we can say multiset) of
string) So this implies that language is a subset of Σ*.This is also called a “Kleene Star”.

Kleene Star is also called a “Kleene Operator” or “Kleene Closure”. Engineers and IT professionals
make use of Kleene Star to achieve all set of strings which is to be included from a given set of characters
or symbols. It is one kind of Unary operator. In Kleene Star methodology all individual elements of a given
string must be present but additional elements or combinations of these alphabets can be included to any
extent.

Example:
Input String: "GFG".
Σ* = { ε,"GFG","GGFG","GGFG","GFGGGGGGGG","GGGGGGGGFFFFFFFFFGGGGGGGG",...}
(Kleene Star is an infinite set but if we provide any grammar rules then it can
work as a finite set.
Please note that we can include ε string also in given Kleene star
representation.)
Language:

A language is a set of strings, chosen from some Σ* or we can say- ‘A language is a subset of
Σ* ‘. A language that can be formed over ‘ Σ ‘ can be Finite or Infinite.

Example of Finite Language:


L1 = { set of string of 2 } L1 = { xy, yx, xx, yy }

Example of Infinite Language:


L1 = { set of all strings starts with 'b' }
L1 = { babb, baa, ba, bbb, baab, ....... }

DFA (Deterministic finite automata)


o DFA refers to deterministic finite automata. Deterministic refers to the uniqueness of the computation. The
finite automata are called deterministic finite automata if the machine is read an input string one symbol
at a time.
o In DFA, there is only one path for specific input from the current state to the next state.
o DFA does not accept the null move, i.e., the DFA cannot change state without any input character.
o DFA can contain multiple final states. It is used in Lexical Analysis in Compiler.

In the following diagram, we can see that from state q0 for input a, there is only one path which is going to q1.
Similarly, from q0, there is only one path for input b going to q2.

Formal Definition of DFA


A DFA is a collection of 5-tuples same as we described in the definition of FA.

1. Q: finite set of states


2. ∑: finite set of the input symbol
3. q0: initial state
4. F: final state
5. δ: Transition function: It can be defined as: δ: Q x ∑→Q
Graphical Representation of DFA
A DFA 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 a double circle.

Example 1:

1. Q = {q0, q1, q2}


2. ∑ = {0, 1}
3. q0 = {q0}
4. F = {q2}

Solution:

Transition Diagram:

Transition Table:

Present State Next state for Input 0 Next State of Input 1

→q0 q0 q1

q1 q2 q1

*q2 q2 q2
Example 2:

DFA with ∑ = {0, 1} accepts all starting with 0.

Solution:

Explanation:

o In the above diagram, we can see that on given 0 as input to DFA in state q0 the DFA changes state to q1
and always go to final state q1 on starting input 0. It can accept 00, 01, 000, 001....etc. It can't accept any
string which starts with 1, because it will never go to final state on a string starting with 1.

Example 3:

DFA with ∑ = {0, 1} accepts all ending with 0.

Solution:

Explanation:

In the above diagram, we can see that on given 0 as input to DFA in state q0, the DFA changes state to q1. It can
accept any string which ends with 0 like 00, 10, 110, 100....etc. It can't accept any string which ends with 1, because
it will never go to the final state q1 on 1 input, so the string ending with 1, will not be accepted or will be rejected.

Example 4:

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.
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.

Example 5:

Design a FA with ∑ = {0, 1} accepts the only input 101.

Solution:

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.

Example 6:
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.

Example 7:
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.

Example 8:

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:

Here two consecutive 1's or single 1 is acceptable, hence

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.
Example 9:

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:

NFA (Non-Deterministic finite automata)


o NFA stands for non-deterministic finite automata. It is easy to construct an NFA than DFA for a
given regular language.
o The finite automata are called NFA when there exist many paths for specific input from the
current state to the next state.
o Every NFA is not DFA, but each NFA can be translated into DFA.
o 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,
1. Q: finite set of states
2. ∑: finite set of the input symbol
3. q0: initial state
4. F: final state
5. δ: 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.

Example 1:

1. Q = {q0, q1, q2}


2. ∑ = {0, 1}
3. q0 = {q0}
4. F = {q2}

Solution:

Transition diagram:

Transition Table:

Present State Next state for Input 0 Next State of Input 1

→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.

Example 2:

NFA with ∑ = {0, 1} accepts all strings with 01.

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 atleast 2.

Solution:

Transition Table:

Present State Next state for Input 0 Next State of Input 1

→q0 q1 q1

q1 q2 q2

*q2 ε ε
Examples of NFA
Example 1:

Design a NFA for the transition table as given below:

Present State 0 1

→q0 q0, q1 q0, q2

q1 q3 ε

q2 q2, q3 q3

→q3 q3 q3

Solution:

The transition diagram can be drawn by using the mapping function as given in the table.

Here,

1. δ(q0, 0) = {q0, q1}


2. δ(q0, 1) = {q0, q2}
3. Then, δ(q1, 0) = {q3}
4. Then, δ(q2, 0) = {q2, q3}
5. δ(q2, 1) = {q3}
6. Then, δ(q3, 0) = {q3}
7. δ(q3, 1) = {q3}

Example 2:
Design an NFA with ∑ = {0, 1} accepts all string ending with 01.
Solution:

Hence, NFA would be:

Example 3:

Design an NFA with ∑ = {0, 1} in which double '1' is followed by double '0'.

Solution:

The FA with double 1 is as follows:

It should be immediately followed by double 0.

Then,

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:


Now considering the string 01100011

q0 → q1 → q2 → q3 → q4 → q4 → q4 → q4

Example 4:

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
Example 5:

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
To remove the epsilon move/Null move from epsilon-NFA and to convert it into NFA, we follow the steps mentioned below.

Step-1: Consider the two vertexes having the epsilon move. Here in Fig.1 we have vertex v1 and vertex v2 having
epsilon move from v1 to v2.
Step-2: Now find all the moves to any other vertex that start from vertex v2 (other than the epsilon move that is
considering). After finding the moves, duplicate all the moves that start from vertex v2, with the same input to
start from vertex v1 and remove the epsilon move from vertex v1 to vertex v2.
Step-3: See that if the vertex v1 is a start state or not. If vertex v1 is a start state, then we will also make vertex v2
as a start state. If vertex v1 is not a start state, then there will not be any change.
Step-4: See that if the vertex v2 is a final state or not. If vertex v2 is a final state, then we will also make vertex v1
as a final state. If vertex v2 is not a final state, then there will not be any change. Repeat the steps(from step 1 to
step 4) until all the epsilon moves are removed from the NFA.
Step-5: Remove all unreachable states/Dead states from any state(i.e state that is unreachable from any state).
Example: Convert epsilon-NFA to NFA. Consider the example having states q0, q1, q2, q3, and q4.

In the above example, we have 5 states named as q0, q1, q2, q3 and q4. Initially, we have q0 as start state and q2 as final
state. We have q1, q3 and q4 as intermediate states.
Transition table for the above NFA is:

States/Input Input 0 Input 1 Input epsilon

q0 – q1 q2

q1 – q0 –

q2 q3 q4 –

q3 q2 – –

q4 q2 – –
According to the transition table above,
 state q0 on getting input 1 goes to state q1.
 State q0 on getting input as a null move (i.e. an epsilon move) goes to state q2.
 State q1 on getting input 1 goes to state q0.
 Similarly, state q2 on getting input 0 goes to state q3, state q2 on getting input 1 goes to state q4.
 Similarly, state q3 on getting input 0 goes to state q2.
 Similarly, state q4 on getting input 0 goes to state q2.
We can see that we have an epsilon move from state q0 to state q2, which is to be removed. To remove epsilon move from
state q0 to state q1, we will follow the steps mentioned below.
Step-1: Considering the epsilon move from state q0 to state q2. Consider the state q0 as vertex v1 and state q2 as vertex v2.
Step-2: Now find all the moves that starts from vertex v2 (i.e. state q2). After finding the moves, duplicate all the moves that
start from vertex v2 (i.e state q2) with the same input to start from vertex v1 (i.e. state q0) and remove the epsilon move from
vertex v1 (i.e. state q0) to vertex v2 (i.e. state q2). Since state q2 on getting input 0 goes to state q3. Hence on duplicating the
move, we will have state q0 on getting input 0 also to go to state q3.
Similarly state q2 on getting input 1 goes to state q4. Hence on duplicating the move, we will have state q0 on getting input
1 also to go to state q4.
So, NFA after duplicating the moves is:

Step-3: Since vertex v1 (i.e. state q0) is a start state. Hence we will also make vertex v2 (i.e. state q2) as a start state. Note that
state q2 will also remain as a final state as we had initially. NFA after making state q2 also as a start state is:

Step-4: Since vertex v2 (i.e. state q2) is a final state. Hence we will also make vertex v1 (i.e. state q0) as a final state. Note that
state q0 will also remain as a start state as we had initially. After making state q0 also as a final state, the resulting NFA is:

Figure – Resulting NFA (state q0 as a final state)The transition table for the above resulting NFA is:
States/Input Input 0 Input 1

q0 q3 q1,q4

q1 – q0,q2

q2 q3 q4

q3 q2 –

q4 q2 –

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 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 0 1

q0 q1 q3

q1 q0 q3

2. Another set contains those rows, which starts from final states.

State 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 0 1

q3 q3 q3

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

State 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, ∑, Oor, δ, λ) 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:
The state diagram for Moore Machine is

Transition table for Moore 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,

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.

The partial diagram will be:

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.

The partial diagram will be:

Now we will insert the possibilities of 0's and 1's for each state. Thus the Mealy machine becomes:
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.

Solution: The mealy machine will be:

Moore Machines vs Mealy Machines


Aspect Moore Machines Mealy Machines
Outputs depend only on the Outputs depend on the current
Output
current state. state and input.

Tends to require more states due Might require fewer states as


Number of States
to separate output behavior. outputs are tied to transitions.

Slower response to input changes


Faster response to input changes
Response Time as outputs update on state
due to immediate output updates.
changes.
Can be simpler due to separation Can be more complex due to
Complexity
of output behavior. combined state-input cases.

You might also like