You are on page 1of 51

SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

LIST OF PRACTICALS

S.NO EXPERIMENT SIGNATURE

1. To Study the theory of Automata

2. To Study FINITE Automata

3. To study the Conversion from NFA to DFA

4. To Study Mealy & Moore Machine.

To Study the Conversion of DFA to Regular


5. Expression.

6. To Study Minimization of DFA

7. To Study Pushdown Automata(PDA)

8. To Study Turing Machine

9. To Study P & NP Problems


SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Practical 1.

To Study the theory of Automata


Theory of automata is a theoretical branch of computer science and mathematical. It is the study
of abstract machines and the computation problems that can be solved using these machines. The
abstract machine is called the automata. The main motivation behind developing the automata
theory was to develop methods to describe and analyse the dynamic behaviour of discrete
systems.

This automaton consists of states and transitions. The State is represented by circles, and
the Transitions is represented by arrows.

Automata is the kind of machine which takes some string as input and this input goes through a
finite number of states and may enter in the final state.

There are the basic terminologies that are important and frequently used in automata:

Symbols:

Symbols are an entity or individual objects, which can be any letter, alphabet or any picture.

Example:
1, a, b, #

Alphabets:

Alphabets are a finite set of symbols. It is denoted by ∑.

Examples:

∑ = {a, b}
∑ = {A, B, C, D}
∑ = {0, 1, 2}
∑ = {0, 1, ....., 5]
∑ = {#, β, Δ}
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

String:

It is a finite collection of symbols from the alphabet. The string is denoted by w.

Example 1:
If ∑ = {a, b}, various string that can be generated from ∑ are {ab, aa, aaa, bb, bbb, ba, aba.....}.

o A string with zero occurrences of symbols is known as an empty string. It is represented


by ε.
o The number of symbols in a string w is called the length of a string. It is denoted by |w|.

Example 2:

w = 010
Number of Sting |w| = 3

Language:

A language is a collection of appropriate string. A language which is formed over Σ can


be Finite or Infinite.

Example: 1
L1 = {Set of string of length 2}

= {aa, bb, ba, bb} Finite Language

Example: 2
L2 = {Set of all strings starts with 'a'}

= {a, aa, aaa, abb, abbb, ababb} Infinite Language


SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Transition Diagram
A transition diagram or state transition diagram is a directed graph which can be constructed as
follows:

o There is a node for each state in Q, which is represented by the circle.


o There is a directed edge from node q to node p labeled a if δ(q, a) = p.
o In the start state, there is an arrow with no source.
o Accepting states or final states are indicating by a double circle.

Some Notations that are used in the transition diagram:


SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Transition Table
The transition table is basically a tabular representation of the transition function. It takes two
arguments (a state and a symbol) and returns a state (the "next state").

A transition table is represented by the following things:

o Columns correspond to input symbols.


o Rows correspond to states.
o Entries correspond to the next state.
o The start state is denoted by an arrow with no source.
o The accept state is denoted by a star.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Practical 2.

To Study FINITE Automata

o Finite automata are used to recognize patterns.


o It takes the string of symbol as input and changes its state accordingly. When the desired symbol
is found, then the transition occurs.
o At the time of transition, the automata can either move to the next state or stay in the same state.
o Finite automata have two states, Accept state or Reject state. When the input string is processed
successfully, and the automata reached its final state, then it will accept.

Formal Definition of FA
A finite automaton is a collection of 5-tuple (Q, ∑, δ, q0, F), where:

Q: finite set of states


∑: finite set of the input symbol
q0: initial state
F: final state
δ: Transition function

Finite Automata Model


Finite automata can be represented by input tape and finite control.

Input tape: It is a linear tape having some number of cells. Each input symbol is placed in each
cell.

Finite control: The finite control decides the next state on receiving particular input from input
tape. The tape reader reads the cells one by one from left to right, and at a time only one input
symbol is read.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Types of Automata:
There are two types of finite automata:

1. DFA(deterministic finite automata)


2. NFA(non-deterministic finite automata)

1. DFA

DFA refers to deterministic finite automata. Deterministic refers to the uniqueness of the
computation. In the DFA, the machine goes to one state only for a particular input character.
DFA does not accept the null move.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

2. NFA

NFA stands for non-deterministic finite automata. It is used to transmit any number of states for
a particular input. It can accept the null move.

Some important points about DFA and NFA:

1. Every DFA is NFA, but NFA is not DFA.


2. There can be multiple final states in both NFA and DFA.
3. DFA is used in Lexical Analysis in Compiler.
4. NFA is more of a theoretical concept.

There is a description of how a DFA operates:

1. In DFA, the input to the automata can be any string. Now, put a pointer to the start state q and
read the input string w from left to right and move the pointer according to the transition
function, δ. We can read one symbol at a time. If the next symbol of string w is a and the pointer
is on state p, move the pointer to δ(p, a). When the end of the input string w is encountered, then
the pointer is on some state F.

2. The string w is said to be accepted by the DFA if r ∈ F that means the input string w is
processed successfully and the automata reached its final state. The string is said to be rejected
by DFA if r ∉ F.

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

Solution:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

The finite automata can be represented using a transition graph. In the above diagram, the
machine initially is in start state q0 then on receiving input 1 the machine changes its state to q1.
From q0 on receiving 0, the machine changes its state to q2, which is the dead state. From q1 on
receiving input 0, 1 the machine changes its state to q1, which is the final state. The possible
input strings that can be generated are 10, 11, 110, 101, 111......., that means all string starts with
1.

Example 2:
NFA with ∑ = {0, 1} accepts all strings starting with 1.

Solution:

The NFA can be represented using a transition graph. In the above diagram, the machine initially
is in start state q0 then on receiving input 1 the machine changes its state to q1. From q1 on
receiving input 0, 1 the machine changes its state to q1. The possible input string that can be
generated is 10, 11, 110, 101, 111......, that means all string starts with 1.

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.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Formal Definition of DFA


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

Q: finite set of states


∑: finite set of the input symbol
q0: initial state
F: final state
δ: Transition function

Transition function can be defined as:

δ: Q x ∑→Q

Graphical Representation of DFA


A DFA can be represented by digraphs called state diagram. In which:

Skip Ad

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.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Example 1:

Q = {q0, q1, q2}


∑ = {0, 1}
q0 = {q0}
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:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

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.

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.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

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.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Example 1:

Q = {q0, q1, q2}


∑ = {0, 1}
q0 = {q0}
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.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

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:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Transition Table:

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

→q0 q1 q1

q1 q2 q2

*q2 ε ε
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Practical 3.

To study the Conversion from NFA to DFA


In this section, we will discuss the method of converting NFA to its equivalent DFA. In NFA,
when a specific input is given to the current state, the machine goes to multiple states. It can have
zero, one or more than one move on a given input symbol. On the other hand, in DFA, when a
specific input is given to the current state, the machine goes to only one state. DFA has only one
move on a given input symbol.

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

Steps for converting NFA to DFA:


Step 1: Initially Q' = ϕ

Step 2: Add q0 of NFA to Q'. Then find the transitions from this start state.HTML Tutorial

Step 3: In Q', find the possible set of states for each input symbol. If this set of states is not in Q',
then add it to Q'.

Step 4: In DFA, the final state will be all the states which contain F(final states of NFA)

Example 1:
Convert the given NFA to DFA.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

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

State 0 1

→q0 q0 q1

q1 {q1, q2} q1

*q2 q2 {q1, q2}

Now we will obtain δ' transition for state q0.

δ'([q0], 0) = [q0]
δ'([q0], 1) = [q1]

The δ' transition for state q1 is obtained as:

δ'([q1], 0) = [q1, q2] (new state generated)


δ'([q1], 1) = [q1]

The δ' transition for state q2 is obtained as:

δ'([q2], 0) = [q2]
δ'([q2], 1) = [q1, q2]

Now we will obtain δ' transition on [q1, q2].

δ'([q1, q2], 0) = δ(q1, 0) ∪ δ(q2, 0)


= {q1, q2} ∪ {q2}
= [q1, q2]
δ'([q1, q2], 1) = δ(q1, 1) ∪ δ(q2, 1)
= {q1} ∪ {q1, q2}
= {q1, q2}
= [q1, q2]

The state [q1, q2] is the final state as well because it contains a final state q2. The transition table
for the constructed DFA will be:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

State 0 1

→[q0] [q0] [q1]

[q1] [q1, q2] [q1]

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

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

The Transition diagram will be:

The state q2 can be eliminated because q2 is an unreachable state.

Example 2:
Convert the given NFA to DFA.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

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

State 0 1

→q0 {q0, q1} {q1}

*q1 ϕ {q0, q1}

Now we will obtain δ' transition for state q0.


δ'([q0], 0) = {q0, q1}
= [q0, q1] (new state generated)
δ'([q0], 1) = {q1} = [q1]

The δ' transition for state q1 is obtained as:

δ'([q1], 0) = ϕ
δ'([q1], 1) = [q0, q1]

Now we will obtain δ' transition on [q0, q1].

δ'([q0, q1], 0) = δ(q0, 0) ∪ δ(q1, 0)


= {q0, q1} ∪ ϕ
= {q0, q1}
= [q0, q1]

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]}.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

The transition table for the constructed DFA will be:

State 0 1

→[q0] [q0, q1] [q1]

*[q1] ϕ [q0, q1]

*[q0, q1] [q0, q1] [q0, q1]

The Transition diagram will be:

Even we can change the name of the states of DFA.

Suppose

A = [q0]
B = [q1]
C = [q0, q1]
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

With these new names the DFA will be as follows:


SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Practical 4.
To Study Mealy & Moore Machine.
Finite automata may have outputs corresponding to each transition. There are two types of finite
state machines that generate output −

 Mealy Machine
 Moore machine

Mealy Machine
A Mealy Machine is an FSM whose output depends on the present state as well as the present
input.
It can be described by a 6 tuple (Q, ∑, O, δ, X, q0) where −
 Q is a finite set of states.
 ∑ is a finite set of symbols called the input alphabet.
 O is a finite set of symbols called the output alphabet.
 δ is the input transition function where δ: Q × ∑ → Q
 X is the output transition function where X: Q × ∑ → O
 q0 is the initial state from where any input is processed (q0 ∈ Q).

The state table of a Mealy Machine is shown below −

Next state
Present state
input = 0 input = 1

State Output State Output

→a b x1 c x1

b b x2 d x3

c d x3 c x1

d d x3 d x2
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

The state diagram of the above Mealy Machine is −

Moore Machine
Moore machine is an FSM whose outputs depend on only the present state.
A Moore machine can be described by a 6 tuple (Q, ∑, O, δ, X, q0) where −
 Q is a finite set of states.
 ∑ is a finite set of symbols called the input alphabet.
 O is a finite set of symbols called the output alphabet.
 δ is the input transition function where δ: Q × ∑ → Q
 X is the output transition function where X: Q → O
 q0 is the initial state from where any input is processed (q0 ∈ Q).
The state table of a Moore Machine is shown below −

Next State
Present state
Output
Input = 0 Input = 1

→a b c x2

b b d x1

c c d x2

d d d x3
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

The state diagram of the above Moore Machine is −

Mealy Machine vs. Moore Machine


The following table highlights the points that differentiate a Mealy Machine from a Moore
Machine.

Mealy Machine Moore Machine

Output depends both upon the present state and the Output depends only upon the present state.
present input

Generally, it has fewer states than Moore Machine. Generally, it has more states than Mealy Machine.

The value of the output function is a function of the The value of the output function is a function of the
transitions and the changes, when the input logic current state and the changes at the clock edges,
on the present state is done. whenever state changes occur.

Mealy machines react faster to inputs. They In Moore machines, more logic is required to decode
generally react in the same clock cycle. the outputs resulting in more circuit delays. They
generally react one clock cycle later.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Moore Machine to Mealy Machine


Input − Moore Machine
Output − Mealy Machine
Step 1 − Take a blank Mealy Machine transition table format.
Step 2 − Copy all the Moore Machine transition states into this table format.
Step 3 − Check the present states and their corresponding outputs in the Moore Machine state
table; if for a state Qi output is m, copy it into the output columns of the Mealy Machine state
table wherever Qi appears in the next state.

Example
Let us consider the following Moore machine –

Next State
Present State Output
a=0 a=1

→a d b 1

b a d 0

c c c 0

d b a 1

Now we apply Algorithm to convert it to Mealy Machine.


SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Step 1 & 2 −

Next State

Present State a=0 a=1

State Output State Output

→a d b

b a d

c c c

d b a

Step 3 −

Next State

Present State a=0 a=1

State Output State Output

=> a d 1 b 0

b a 1 d 1

c c 0 c 0

d b 0 a 1
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Mealy Machine to Moore Machine


Input − Mealy Machine
Output − Moore Machine
Step 1 − Calculate the number of different outputs for each state (Qi) that are available in the
state table of the Mealy machine.
Step 2 − If all the outputs of Qi are same, copy state Q i. If it has n distinct outputs, break Qi into
n states as Qin where n = 0, 1, 2.......
Step 3 − If the output of the initial state is 1, insert a new initial state at the beginning which
gives 0 output.

Example
Let us consider the following Mealy Machine –

Next State

Present State a=0 a=1

Next State Output Next State Output

→a d 0 b 1

b a 1 d 0

c c 1 c 0

d b 0 a 1

Here, states ‘a’ and ‘d’ give only 1 and 0 outputs respectively, so we retain states ‘a’ and ‘d’.
But states ‘b’ and ‘c’ produce different outputs (1 and 0). So, we divide b into b0,
b1 and c into c0, c1.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Next State
Present State Output
a=0 a=1

→a d b1 1

b0 a d 0

b1 a d 1

c0 c1 C0 0

c1 c1 C0 1

d b0 a 0
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Practical 5.
To Study the Conversion of DFA to Regular Expression.
In order to find out a regular expression of a Finite Automaton, we use Arden’s Theorem along
with the properties of regular expressions.
Statement −
Let P and Q be two regular expressions.
If P does not contain null string, then R = Q + RP has a unique solution that is R = QP*
Proof −
R = Q + (Q + RP)P [After putting the value R = Q + RP]
= Q + QP + RPP
When we put the value of R recursively again and again, we get the following equation −
R = Q + QP + QP2 + QP3…..
R = Q (ε + P + P2 + P3 + …. )
R = QP* [As P* represents (ε + P + P2 + P3 + ….) ]
Hence, proved.

Assumptions for Applying Arden’s Theorem


 The transition diagram must not have NULL transitions
 It must have only one initial state

Method
Step 1 − Create equations as the following form for all the states of the DFA having n states
with initial state q1.
q1 = q1R11 + q2R21 + … + qnRn1 + ε
q2 = q1R12 + q2R22 + … + qnRn2
…………………………
…………………………
…………………………
…………………………
qn = q1R1n + q2R2n + … + qnRnn
Rij represents the set of labels of edges from qi to qj, if no such edge exists, then Rij = ∅
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Step 2 − Solve these equations to get the equation for the final state in terms of Rij
Problem
Construct a regular expression corresponding to the automata given below −

Solution −
Here the initial state and final state is q1.
The equations for the three states q1, q2, and q3 are as follows −
q1 = q1a + q3a + ε (ε move is because q1 is the initial state0
q2 = q1b + q2b + q3b
q3 = q2a
Now, we will solve these three equations −
q2 = q1b + q2b + q3b
= q1b + q2b + (q2a)b (Substituting value of q3)
= q1b + q2(b + ab)
= q1b (b + ab)* (Applying Arden’s Theorem)
q1 = q1a + q3a + ε
= q1a + q2aa + ε (Substituting value of q3)
= q1a + q1b(b + ab*)aa + ε (Substituting value of q2)
= q1(a + b(b + ab)*aa) + ε
= ε (a+ b(b + ab)*aa)*
= (a + b(b + ab)*aa)*
Hence, the regular expression is (a + b(b + ab)*aa)*.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Problem
Construct a regular expression corresponding to the automata given below −

Solution −
Here the initial state is q1 and the final state is q2
Now we write down the equations −
q1 = q10 + ε
q2 = q11 + q20
q3 = q21 + q30 + q31
Now, we will solve these three equations −
q1 = ε0* [As, εR = R]
So, q1 = 0*
q2 = 0*1 + q20
So, q2 = 0*1(0)* [By Arden’s theorem]
Hence, the regular expression is 0*10*.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Practical 6.

To Study 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:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

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
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

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.


SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Practical 7.

To Study Pushdown Automata(PDA)

o Pushdown automata is a way to implement a CFG in the same way we design DFA for a regular
grammar. A DFA can remember a finite amount of information, but a PDA can remember an
infinite amount of information.
o Pushdown automata is simply an NFA augmented with an "external stack memory". The addition
of stack is used to provide a last-in-first-out memory management capability to Pushdown
automata. Pushdown automata can store an unbounded amount of information on the stack. It can
access a limited amount of information on the stack. A PDA can push an element onto the top of
the stack and pop off an element from the top of the stack. To read an element into the stack, the
top elements must be popped off and are lost.
o A PDA is more powerful than FA. Any language which can be acceptable by FA can also be
acceptable by PDA. PDA also accepts a class of language which even cannot be accepted by FA.
Thus PDA is much more superior to FA.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

PDA Components:
Input tape: The input tape is divided in many cells or symbols. The input head is read-only and
may only move from left to right, one symbol at a time.

Finite control: The finite control has some pointer which points the current symbol which is to
be read.

Stack: The stack is a structure in which we can push and remove the items from one end only. It
has an infinite size. In PDA, the stack is used to store the items temporarily.

Formal definition of PDA:


The PDA can be defined as a collection of 7 components:

Q: the finite set of states

∑: the input set

Γ: a stack symbol which can be pushed and popped from the stack

q0: the initial state

Z: a start symbol which is in Γ.

F: a set of final states

δ: mapping function which is used for moving from current state to next state.

Instantaneous Description (ID)


ID is an informal notation of how a PDA computes an input string and make a decision that
string is accepted or rejected.

An instantaneous description is a triple (q, w, α) where:

q describes the current state.

w describes the remaining input.

α describes the stack contents, top at the left.


SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Turnstile Notation:
⊢ sign describes the turnstile notation and represents one move.

⊢* sign describes a sequence of moves.

For example,

(p, b, T) ⊢ (q, w, α)

In the above example, while taking a transition from state p to q, the input symbol 'b' is
consumed, and the top of the stack 'T' is represented by a new string α.

Example 1:
Design a PDA for accepting a language {anb2n | n>=1}.

Solution: In this language, n number of a's should be followed by 2n number of b's. Hence, we
will apply a very simple logic, and that is if we read single 'a', we will push two a's onto the
stack. As soon as we read 'b' then for every single 'b' only one 'a' should get popped from the
stack.

The ID can be constructed as follows:

δ(q0, a, Z) = (q0, aaZ)


δ(q0, a, a) = (q0, aaa)

Now when we read b, we will change the state from q0 to q1 and start popping corresponding 'a'.
Hence,

δ(q0, b, a) = (q1, ε)

Thus this process of popping 'b' will be repeated unless all the symbols are read. Note that
popping action occurs in state q1 only.

δ(q1, b, a) = (q1, ε)

After reading all b's, all the corresponding a's should get popped. Hence when we read ε as input
symbol then there should be nothing in the stack. Hence the move will be:

δ(q1, ε, Z) = (q2, ε)
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Where

PDA = ({q0, q1, q2}, {a, b}, {a, Z}, δ, q0, Z, {q2})

We can summarize the ID as:

δ(q0, a, Z) = (q0, aaZ)


δ(q0, a, a) = (q0, aaa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z) = (q2, ε)

Now we will simulate this PDA for the input string "aaabbbbbb".

δ(q0, aaabbbbbb, Z) ⊢ δ(q0, aabbbbbb, aaZ)


⊢ δ(q0, abbbbbb, aaaaZ)
⊢ δ(q0, bbbbbb, aaaaaaZ)
⊢ δ(q1, bbbbb, aaaaaZ)
⊢ δ(q1, bbbb, aaaaZ)
⊢ δ(q1, bbb, aaaZ)
⊢ δ(q1, bb, aaZ)
⊢ δ(q1, b, aZ)
⊢ δ(q1, ε, Z)
⊢ δ(q2, ε)
ACCEPT
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Example 2:
Design a PDA for accepting a language {0n1m0n | m, n>=1}.

Solution: In this PDA, n number of 0's are followed by any number of 1's followed n number of
0's. Hence the logic for design of such PDA will be as follows:

Push all 0's onto the stack on encountering first 0's. Then if we read 1, just do nothing. Then read
0, and on each read of 0, pop one 0 from the stack.

For instance:

This scenario can be written in the ID form as:

δ(q0, 0, Z) = δ(q0, 0Z)


δ(q0, 0, 0) = δ(q0, 00)
δ(q0, 1, 0) = δ(q1, 0)
δ(q0, 1, 0) = δ(q1, 0)
δ(q1, 0, 0) = δ(q1, ε)
δ(q0, ε, Z) = δ(q2, Z) (ACCEPT state)

Now we will simulate this PDA for the input string "0011100".
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

δ(q0, 0011100, Z) ⊢ δ(q0, 011100, 0Z)


⊢ δ(q0, 11100, 00Z)
⊢ δ(q0, 1100, 00Z)
⊢ δ(q1, 100, 00Z)
⊢ δ(q1, 00, 00Z)
⊢ δ(q1, 0, 0Z)
⊢ δ(q1, ε, Z)
⊢ δ(q2, Z)
ACCEPT
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Practical 8.

To Study Turing Machine


Turing machine was invented in 1936 by Alan Turing. It is an accepting device which accepts
Recursive Enumerable Language generated by type 0 grammar.

There are various features of the Turing machine:

1. It has an external memory which remembers arbitrary long sequence of input.


2. It has unlimited memory capability.
3. The model has a facility by which the input at left or right on the tape can be read easily.
4. The machine can produce a certain output based on its input. Sometimes it may be required that
the same input has to be used to generate the output. So in this machine, the distinction between
input and output has been removed. Thus a common set of alphabets can be used for the Turing
machine.

Formal definition of Turing machine


A Turing machine can be defined as a collection of 7 components:

Q: the finite set of states


∑: the finite set of input symbols
T: the tape symbol
q0: the initial state
F: a set of final states
B: a blank symbol used as a end marker for input
δ: a transition or mapping function.

The mapping function shows the mapping from states of finite automata and input symbol on the
tape to the next states, external symbols and the direction for moving the tape head. This is
known as a triple or a program for turing machine.

(q0, a) → (q1, A, R)

That means in q0 state, if we read symbol 'a' then it will go to state q1, replaced a by X and move
ahead right(R stands for right).
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Example:
Construct TM for the language L ={0n1n} where n>=1.

Solution:

We have already solved this problem by PDA. In PDA, we have a stack to remember the
previous symbol. The main advantage of the Turing machine is we have a tape head which can
be moved forward or backward, and the input tape can be scanned.

The simple logic which we will apply is read out each '0' mark it by A and then move ahead
along with the input tape and find out 1 convert it to B. Now, repeat this process for all a's and
b's.

Now we will see how this turing machine work for 0011.

The simulation for 0011 can be shown as below:

Now, we will see how this turing machine will works for 0011. Initially, state is q0 and head
points to 0 as:

The move will be δ(q0, 0) = δ(q1, A, R) which means it will go to state q1, replaced 0 by A and
head will move to the right as:

The move will be δ(q1, 0) = δ(q1, 0, R) which means it will not change any symbol, remain in
the same state and move to the right as:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

The move will be δ(q1, 1) = δ(q2, B, L) which means it will go to state q2, replaced 1 by B and
head will move to left as:

Now move will be δ(q2, 0) = δ(q2, 0, L) which means it will not change any symbol, remain in
the same state and move to left as:

The move will be δ(q2, A) = δ(q0, A, R), it means will go to state q0, replaced A by A and head
will move to the right as:

The move will be δ(q0, 0) = δ(q1, A, R) which means it will go to state q1, replaced 0 by A, and
head will move to right as:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

The move will be δ(q1, B) = δ(q1, B, R) which means it will not change any symbol, remain in
the same state and move to right as:

The move will be δ(q1, 1) = δ(q2, B, L) which means it will go to state q2, replaced 1 by B and
head will move to left as:

The move δ(q2, B) = (q2, B, L) which means it will not change any symbol, remain in the same
state and move to left as:

Now immediately before B is A that means all the 0?s are market by A. So we will move right to
ensure that no 1 is present. The move will be δ(q2, A) = (q0, A, R) which means it will go to
state q0, will not change any symbol, and move to right as:

The move δ(q0, B) = (q3, B, R) which means it will go to state q3, will not change any symbol,
and move to right as:
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

The move δ(q3, B) = (q3, B, R) which means it will not change any symbol, remain in the same
state and move to right as:

The move δ(q3, Δ) = (q4, Δ, R) which means it will go to state q4 which is the HALT state and
HALT state is always an accept state for any TM.

The same TM can be represented by Transition Diagram:

Basic Model of Turing machine


The turning machine can be modelled with the help of the following representation.

1. The input tape is having an infinite number of cells, each cell containing one input symbol and
thus the input string can be placed on tape. The empty tape is filled by blank characters.

2. The finite control and the tape head which is responsible for reading the current input symbol.
The tape head can move to left to right.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

3. A finite set of states through which machine has to undergo.

4. Finite set of symbols called external symbols which are used in building the logic of turing
machine.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

Practical 9.
To Study P & NP Problems
In Computer Science, many problems are solved where the objective is to maximize or
minimize some values, whereas in other problems we try to find whether there is a solution or
not. Hence, the problems can be categorized as follows −

Optimization Problem
Optimization problems are those for which the objective is to maximize or minimize some
values. For example,
 Finding the minimum number of colors needed to color a given graph.
 Finding the shortest path between two vertices in a graph.

Decision Problem
There are many problems for which the answer is a Yes or a No. These types of problems are
known as decision problems. For example,
 Whether a given graph can be colored by only 4-colors.
 Finding Hamiltonian cycle in a graph is not a decision problem, whereas checking a
graph is Hamiltonian or not is a decision problem.

What is Language?
Every decision problem can have only two answers, yes or no. Hence, a decision problem may
belong to a language if it provides an answer ‘yes’ for a specific input. A language is the totality
of inputs for which the answer is Yes. Most of the algorithms discussed in the previous chapters
are polynomial time algorithms.
For input size n, if worst-case time complexity of an algorithm is O(nk), where k is a constant,
the algorithm is a polynomial time algorithm.
Algorithms such as Matrix Chain Multiplication, Single Source Shortest Path, All Pair Shortest
Path, Minimum Spanning Tree, etc. run in polynomial time. However there are many problems,
such as traveling salesperson, optimal graph coloring, Hamiltonian cycles, finding the longest
path in a graph, and satisfying a Boolean formula, for which no polynomial time algorithms is
known. These problems belong to an interesting class of problems, called the NP-
Complete problems, whose status is unknown.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

In this context, we can categorize the problems as follows −

P-Class
The class P consists of those problems that are solvable in polynomial time, i.e. these problems
can be solved in time O(nk) in worst-case, where k is constant.
These problems are called tractable, while others are called intractable or super polynomial.
Formally, an algorithm is polynomial time algorithm, if there exists a polynomial p(n) such that
the algorithm can solve any instance of size n in a time O(p(n)).
Problem requiring Ω(n50) time to solve are essentially intractable for large n. Most known
polynomial time algorithm run in time O(nk) for fairly low value of k.
The advantages in considering the class of polynomial-time algorithms is that all
reasonable deterministic single processor model of computation can be simulated on each
other with at most a polynomial slow-d

NP-Class
The class NP consists of those problems that are verifiable in polynomial time. NP is the class
of decision problems for which it is easy to check the correctness of a claimed answer, with the
aid of a little extra information. Hence, we aren’t asking for a way to find a solution, but only to
verify that an alleged solution really is correct.
Every problem in this class can be solved in exponential time using exhaustive search.

P versus NP
Every decision problem that is solvable by a deterministic polynomial time algorithm is also
solvable by a polynomial time non-deterministic algorithm.
All problems in P can be solved with polynomial time algorithms, whereas all problems in NP -
P are intractable.
It is not known whether P = NP. However, many problems are known in NP with the property
that if they belong to P, then it can be proved that P = NP.
If P ≠ NP, there are problems in NP that are neither in P nor in NP-Complete.
The problem belongs to class P if it’s easy to find a solution for the problem. The problem
belongs to NP, if it’s easy to check a solution that may have been very tedious to find.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

A problem is in the class NPC if it is in NP and is as hard as any problem in NP. A problem
is NP-hard if all problems in NP are polynomial time reducible to it, even though it may not be
in NP itself.

If a polynomial time algorithm exists for any of these problems, all problems in NP would be
polynomial time solvable. These problems are called NP-complete. The phenomenon of NP-
completeness is important for both theoretical and practical reasons.

Definition of NP-Completeness
A language B is NP-complete if it satisfies two conditions
 B is in NP
 Every A in NP is polynomial time reducible to B.
If a language satisfies the second property, but not necessarily the first one, the language B is
known as NP-Hard. Informally, a search problem B is NP-Hard if there exists some NP-
Complete problem A that Turing reduces to B.
The problem in NP-Hard cannot be solved in polynomial time, until P = NP. If a problem is
proved to be NPC, there is no need to waste time on trying to find an efficient algorithm for it.
Instead, we can focus on design approximation algorithm.

NP-Complete Problems
Following are some NP-Complete problems, for which no polynomial time algorithm is known.

 Determining whether a graph has a Hamiltonian cycle


 Determining whether a Boolean formula is satisfiable, etc.
SHRI YOGINDRA SAGAR INSTITUTE OF TECHNOLOGY & SCIENCE

NP-Hard Problems
The following problems are NP-Hard

 The circuit-satisfiability problem


 Set Cover
 Vertex Cover
 Travelling Salesman Problem
In this context, now we will discuss TSP is NP-Complete

TSP is NP-Complete
The traveling salesman problem consists of a salesman and a set of cities. The salesman has to
visit each one of the cities starting from a certain one and returning to the same city. The
challenge of the problem is that the traveling salesman wants to minimize the total length of the
trip

Proof
To prove TSP is NP-Complete, first we have to prove that TSP belongs to NP. In TSP, we find
a tour and check that the tour contains each vertex once. Then the total cost of the edges of the
tour is calculated. Finally, we check if the cost is minimum. This can be completed in
polynomial time. Thus TSP belongs to NP.
Secondly, we have to prove that TSP is NP-hard. To prove this, one way is to show
that Hamiltonian cycle ≤p TSP (as we know that the Hamiltonian cycle problem is
NPcomplete).
Assume G = (V, E) to be an instance of Hamiltonian cycle.
Hence, an instance of TSP is constructed. We create the complete graph G' = (V, E'), where
E’={(i,j):i,j∈V andi≠j
Thus, the cost function is defined as follows −
t(i,j)= 0 if(i,j)∈E
1 otherwise
Now, suppose that a Hamiltonian cycle h exists in G. It is clear that the cost of each edge
in h is 0 in G' as each edge belongs to E. Therefore, h has a cost of 0 in G'. Thus, if graph G has
a Hamiltonian cycle, then graph G' has a tour of 0 cost.
Conversely, we assume that G' has a tour h' of cost at most 0. The cost of edges
in E' are 0 and 1 by definition. Hence, each edge must have a cost of 0 as the cost of h' is 0. We
therefore conclude that h' contains only edges in E.
We have thus proven that G has a Hamiltonian cycle, if and only if G' has a tour of cost at
most 0. TSP is NP-complete.

You might also like