Theory of Computation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 166
At a glance
Powered by AI
The document discusses the theory of computation and covers topics like finite automata, grammars, pushdown automata, Turing machines, and unsolvable problems.

The document appears to be course material for a theory of computation class. It covers various computing models and discusses decidability and complexity of problems.

The document discusses five units - finite automata, grammars, pushdown automata, Turing machines, and unsolvable problems and computable functions.

S.K.P.

Engineering College, Tiruvannamalai V SEM

SKP Engineering College


Tiruvannamalai – 606611

A Course Material
on
Theory of Computation

By

S.Parvathi
Assistant Professor
Computer Science and Engineering Department

Computer Science Engineering Department 1 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Quality Certificate

This is to Certify that the Electronic Study Material

Subject Code:CS6503

Subject Name:Theory of Computation

Year/Sem: III/V

Being prepared by me and it meets the knowledge requirement of the University


curriculum.

Signature of the Author

Name: S.Parvathi

Designation: Assistant Professor

This is to certify that the course material being prepared by Mrs.S.Parvathi is of the
adequate quality. He has referred more than five books and one among them is from
abroad author.

Signature of HD Signature of the Principal

Name: Mr.K.Baskar Name: Dr.V.Subramania Bharathi

Seal: Seal:

Computer Science Engineering Department 2 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

CS6503 THEORY OF COMPUTATION LTPC 3003

OBJECTIVES:
The student should be made to:
Understand various Computing models like Finite State Machine, Pushdown Automata,
and Turing Machine.
Be aware of Decidability and Un-decidability of various problems.
Learn types of grammars.

UNIT I FINITE AUTOMATA 9


Introduction- Basic Mathematical Notation and techniques- Finite State systems – Basic
Definitions – Finite Automaton – DFA & NDFA – Finite Automaton with €- moves –
Regular Languages- Regular Expression – Equivalence of NFA and DFA – Equivalence
of NDFA‘s with and without €-moves – Equivalence of finite Automaton and regular
expressions –Minimization of DFA- – Pumping Lemma for Regular sets – Problems
based on Pumping Lemma.

UNIT II GRAMMARS 9
Grammar Introduction– Types of Grammar – Context Free Grammars and Languages–
Derivations and Languages – Ambiguity- Relationship between derivation and
derivation trees – Simplification of CFG – Elimination of Useless symbols – Unit
productions – Null productions – Greiback Normal form – Chomsky normal form –
Problems related to CNF and GNF.

UNIT III PUSHDOWN AUTOMATA 9


Pushdown Automata- Definitions – Moves – Instantaneous descriptions – Deterministic
pushdown automata – Equivalence of Pushdown automata and CFL – pumping lemma
for CFL – problems based on pumping Lemma.

UNIT IV TURING MACHINES 9


Definitions of Turing machines – Models – Computable languages and functions –
Techniques for Turing machine construction – Multi head and Multi tape Turing
Machines – The Halting problem – Partial Solvability – Problems about Turing machine-
Chomskian hierarchy of languages.

Computer Science Engineering Department 3 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

UNIT V UNSOLVABLE PROBLEMS AND COMPUTABLE FUNCTIONS 9


Unsolvable Problems and Computable Functions – Primitive recursive functions –
Recursive and recursively enumerable languages – Universal Turing machine.
MEASURING AND CLASSIFYING COMPLEXITY: Tractable and Intractable problems-
Tractable and possibly intractable problems – P and NP completeness – Polynomial
time reductions.
TOTAL: 45 PERIODS

OUTCOMES:
At the end of the course, the student should be able to:
Design Finite State Machine
Understand the concept og Grammars
Design Pushdown Automata
Design Turing Machine
Explain the Decidability or Undecidability of various problems

TEXT BOOKS:
1. Hopcroft J.E., Motwani R. and Ullman J.D, ―Introduction to Automata Theory,
Languages and Computations‖, Second Edition, Pearson Education, 2008. (UNIT 1,2,3)
2. John C Martin, ―Introduction to Languages and the Theory of Computation‖, Third
Edition, Tata McGraw Hill Publishing Company, New Delhi, 2007. (UNIT 4,5)

REFERENCES:
1. Mishra K L P and Chandrasekaran N, ―Theory of Computer Science – Automata,
Languages and Computation‖, Third Edition, Prentice Hall of India, 2004.
2. Harry R Lewis and Christos H Papadimitriou, ―Elements of the Theory of
Computation‖, Second Edition, Prentice Hall of India, Pearson Education, New Delhi,
2003.
3. Peter Linz, ―An Introduction to Formal Language and Automata‖, Third Edition,
Narosa Publishers, New Delhi, 2002.
4. Kamala Krithivasan and Rama. R, ―Introduction to Formal Languages, Automata
Theory and Computation‖, Pearson Education 2009

Computer Science Engineering Department 4 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

CONTENTS

S.No Particulars Page

1 Unit – I 7

2 Unit – II 40

3 Unit – III 64

4 Unit – IV 91

5 Unit – V 121

Computer Science Engineering Department 5 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Prerequiste

You must know the basic concenpts of design and analysis of algorithm and
basic knowledge about the programming, data structure concepts with the time
and space complexity of various algorithms used in the computation.

Computer Science Engineering Department 6 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

UNIT – I
FINITE AUTOMATA
TWO MARKS

1. Define Theory of Computation.[CO1-L1]


The theory of computation describes the basic ideas and models underlying
computing. Computation is the process of execution of an algorithm such that it
involves taking some inputs and performing required operation on it to produce an
output.

2. Define: (i) Finite Automaton (FA) (ii) Transition Diagram[CO1-L1-


Apr/May2007][Nov/Dec2012]
FA consists of a finite set of states and a set of transitions from state to state that occur
on input symbols chosen from an alphabet ∑.
Finite Automaton is denoted by a 5- tuple (Q, ∑, δ, q0, F),

Where

Q is the finite set of states ,

∑ is a finite input alphabet,

q0 in Q is the initial state,

F is the set of final states and

δ is the transition mapping function Q* ∑ to Q.

Transition diagram is a directed graph in which the vertices of the graph


correspond to the states of FA. If there is a transition from state q to state p on input a,
then there is an arc labeled ‗a ‗from q to p in the transition diagram. One state is
designated as ―Start‖ state. The final state will be indicated by double circles.

Computer Science Engineering Department 7 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

3. What are the Applications of Automata theory?[CO1-L1] [May - 2008, 2011,


2012, Dec-2011, 2012]
1. It is base for the formal languages and these formal languages are useful of the
programming languages.
2. It plays an important role in complier design.
3. To prove the correctness of the program automata theory is used.
4. In switching theory and design and analysis of digital circuits automata theory is
applied.
5. It deals with the design finite state machines

4. What is meant by DFA. [CO1-L1][MAY/JUNE 2013]


A Deterministic Finite Automata (DFA) consists of finite set of states and a finite set of
input symbols.
In DFA, only a single transition occurs form one state to another state on single
input symbol.
A Deterministic Finite Automata (DFA) is defined by five tuples

M = (Q, ∑, δ, q0, F)

Where

• M-Finite automata
• Q - finite, non empty set of states
• ∑- finite set of alphabets
• q0 ЄQ - the start state
• F C Q- a set of final states
• δ a transition function (Q Х ∑)

5.What is a Non-Deterministic Finite Automaton (NDFA)?[CO1-L1] (Nov/Dec 2013)


An NFA has finite set of states, a finite set of input symbols with one start state and a
set of accepting states. In NFA, δ is a transition function that takes a state and a set of
input symbols as argument, but returns a set of zero, one or more states. NFA allows
zero, one or more transitions

Computer Science Engineering Department 8 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

from a state on the same input symbol.

The Non-deterministic Finite Automata (NFA) is defined by a five tuple

M = (Q, ∑, δ, q0, F)

Where, Q - finite, non empty set of states

1. ∑- an input alphabet
2. q0 ЄQ - the start state
3. FXQ- a set of final states
4. δ a transition function (Q Х ∑ = 2Q (2Q is the power set of Q)

6. Define NFA with ε -transition. Is the NFA‟s with ε -transitions are more powerful
than the NFA‟s without ε -transition? [CO1-L2]
The NFA with ε moves defined by 5 tuple or quadruple as similarly as NFA,
except ε. M = (Q, ∑, δ, q0, F) with all components as before: Q x (δ U{ ε }) = 2Q
No, NFA with ε transition and NFA without ε -transition have the same power.

7.What is Induction principle? Give an example.[CO1-L1][NOV/DEC 2012 ]


The simplest form of mathematical induction infers that a statement involving a
natural number n holds for all values of n. The proof consists of two steps:
1. The basis (base case): prove that the statement holds for the first natural
number n. Usually, n = 0 or n = 1.
2. The inductive step: prove that, if the statement holds for some natural
number n, then the statement holds for n + 1.
The hypothesis in the inductive step that the statement holds for some n is called
the induction hypothesis (or inductive hypothesis). To perform the inductive
step, one assumes the induction hypothesis and then uses this assumption to prove
the statement for n + 1.

8. Define Formal proof? What are the methods of Formal Proof?[CO1-L2]


Formal proof is one in which step by step procedure is used to solve the problem. Here
the truth of the statement is shown by a detailed sequence of steps and reasons.
Testing the program is so important, but if our program is very complex that involving
the recursion of iteration, then our code may become incorrect.

Computer Science Engineering Department 9 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Methods of formal proof involves,


a. Deductive proof
b. Reduction to definitions
c. Other theorem forms
d. Theorems that appear not to be If-then

9.What are the Additional Forms of Proof?[CO1-L1][Dec 2011, 2012]


The additional forms f proof are as follows,
• Proof about sets
• Proofs by contradiction
• Proofs by counter example

10. Define Structural Inductions.[CO1-L2] [NOV/DEC 2011]


In Automata, there are several recursively defined structures such as trees and
expressions. The structural induction deals with the recursive definition that has a basis
case in which one or more elementary structures are defined. There is an inductive
step, where more complex structures are defined in terms of previously defined
structures.

11.Obtain the ε closure of states q0 and q1 in the following NFA with ε


transition?[CO1-CO1][Dec 2014]

Solution:
Є – Closure (q0) = {q0, q1, q2}
Є – Closure (q1) = {q1, q2}

Computer Science Engineering Department 10 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

12. Difference between DFA and NFA[CO1-L2]

DFA NFA

All transitions are deterministic Transitions could be nondeterministic

Each transition leads to a exactly one A transition could lead to a subset of

state states

For each state, transition on all


possible For each state, not all symbols

symbols (alphabet) should be defined necessarily have to be defined in the

transition

Accepts input if one of the last states


Accepts input if the last state is in F is

in F

Sometimes harder to construct


because Generall y easier than a DFA number

of the function of states

Practical implementation is feasible Practical implementation has to be

deterministic (so needs conversion to

DFA)

Computer Science Engineering Department 11 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

13. Design DFA to accept the language L = {w|w has both even number of
0‟s and even number of 1‟s}[CO1-H3]

q0 1 q1

0 0
0 0

1
q2 q3
1

14. Construct the DFA that accepts input string of 0‟s and 1‟s that end with
00.[CO1-H3]

1
1 1
0 q1 0
q0 q2

Computer Science Engineering Department 12 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

15. Construct NFA with 011 as substring.[CO1-H3]

1
1/0 1/0
0 1 1
q0 q1 q2 q3

16. Draw the transition diagram (automata) for an identifier?[CO1-H3] (Nov/Dec


2013)

17. What is a Regular Expression?[CO1-L1] [NOV/DEC 2012]


A regular expression is a string that describes the whole set of strings according to
certain syntax rules. These expressions are used by many text editors and utilities to
search bodies of text for certain patterns etc.
Definition: Let Σ be an alphabet. The regular expression over Σ and the sets they
denote are:
i. Φ is a r.e and denotes empty set.
ii. ε is a r.e and denotes the set { ε }
iii. For each ‗a‘ in Σ, a+ is a r.e and denotes the set {a}.
iv. If ‗r‘ and ‗s‘ are r.e denoting the languages R and S respectively then
(r+s), (rs) and (r*) are r.e that denote the sets RUS, RS and R* respectively.

Computer Science Engineering Department 13 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

18. What is Arden‟s Theorem?[CO1-L1]


Arden‘s theorem helps in checking the equivalence of two regular expressions. Let P
and Q be the two regular expressions over the input alphabet Σ. The regular
expression R is given as: R=Q+RP Which has a unique solution as R=QP*.

19. Write a regular expression to denote a language L which accepts all the
strings which begin or end with either 00 or 11.[CO1-L2] [NOV/DEC 2012]
The R.E consists of two parts:
L1 = (00+11) (any no of 0‘s
and 1‘s) = (00+11)(0+1)*
L2 = (any no of 0‘s and
1‘s)(00+11) =
(0+1)*(00+11)
Hence Reg.Exp R=L1+L2 = [(00+11)(0+1)*] + [(0+1)* (00+11)]

20. Construct a R.E for the language which accepts all strings with atleast two c‟s
over the set Σ = {c,b}. [CO1-L1]
Ans: (b+c)* c (b+c)* c (b+c)*

21. Define Pumping Lemma.[CO1-L2]


The pumping lemma is a way to prove that certain languages are not regular. The
closure properties are used to those languages that are regular.
1. Given a infinite regular language L
2. There exists an integer m critical length)
with length | w | ≥
For any string w є L m

we can write w x yz

Computer Science Engineering Department 14 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

with | x y | ≤ m and
| y|≥1
such that: x y i z ∈
L i 0, 1, 2, ...

22.Define equivalent state.[CO1-L2] [NOV/DEC 2007]


The states p and q are equivalent if ―for all input strings q, δ(p,w) is an accepting state if
and only if δ(q,w) is also an accepting state
.
23. List down the operators of the regular expression.[CO1-L1]
The three operators involved in the Regular Expression are,
(i) The UNION of two languages L and M, denoted by LUM, is the set of
strings that are either in L or M or both.
(ii) The CONCATENATION of languages L and M is the set of strings
that can be formed by taking any string in L and concatenating it
with any string in M.
(iii) The regular expression involving the CLOSURE operation and its types
are,
• Kleene or star closure
• Positive closure

24. What are the applications of pumping lemma?[CO1-L1] [NOV/DEC 2007]


Every language of finite size has to be regular (we can easily construct an NFA that
accepts every string in the language).Therefore, every non-regular language has to be
of infinite size (contains an infinite number of strings)

25. What are the applications of Regular expressions and Finite automata?[CO1-
L1]
Lexical analyzers and Text editors are two applications.
Lexical analyzers: The tokens of the programming language can be expressed using

Computer Science Engineering Department 15 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

regular expressions. The lexical analyzer scans the input program and separates the
tokens. For eg identifier can be expressed as a regular expression as: (letter)
(letter+digit)*
If anything in the source language matches with this regular expression then it is
recognized as an identifier. The letter is{A,B,C,………..Z,a,b,c….z} and digit is
{0,1,…9}.Thus regular expression identifies token in a language.
Text editors: These are programs used for processing the text. For example UNIX
text editors uses the regular expression for substituting the strings such as: S/bbb*/b/
Gives the substitute a single blank for the first string of two or more blanks in a given
line. In UNIX text editors any regular expression is converted to an NFA with ε –
transitions, this NFA can be then simulated directly.

1. For the finite state machine M given in the following table, test whether the
strings 101101, 111111 are accepted by M [CO1-L3-Apr/May 2007]

state 0 1

Start & * q0 [q0] [q1]

q1 [q3] [q0]

q2 [q0] [q3]

q3 [q1] [q2]

Solution

Computer Science Engineering Department 16 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Check whether the string 101101 is accepted by the above DFA.


δ(q0, w) = δ(q0,Є ) = q0
The string w=101101
δ (q0,1) = δ( (q0,Є),1) = δ(q0,1) = q1
δ (q0,10) = δ( (q0,1),0) = δ(q1,0) = q3
δ (q0,101) = δ( (q0,10),1) = δ(q3,1) = q2
δ (q0,1011) = δ( (q0,101),1) = δ(q2,1) = q3
δ (q0,10110) = δ( (q0,1011),0) = δ(q3,0) = q1
δ (q0,101101) = δ( (q0,10110),1) = δ(q1,1) = q0
So the string is accepted.
The string w=1111
δ (q0,11111) = δ( (q0,1),1111)
= δ( (q1,1),111)
= δ( (q0,1),11)
= δ( (q1,1),1)
= δ( (q0,Є),1)
= δ(q0,1)= q1 is Final State

Computer Science Engineering Department 17 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

2. Construct an NFA for the set of strings with {0, 1} ending with 01 and draw
the transition table for the same and check whether the input string 00101 is
accepted by above NFA. [CO1-L3]
Solution:
The transition diagram is,

0
1
Start
q
0 q1

1/0
Input String = 00101
The transition table is,
0 1

0 {q0,q1} {q0}

q
1 Φ {q2}
*q2 Φ Φ

δ(q0,0) = { q0, q1 }
δ‘(q0,00) = δ (δ(q0,0),0)
= δ ({ q0, q1 },0)
= δ (q0,0) U δ(q1,0)
= { q0, q1 } U Φ = {q0, q1}
δ‘(q0,001) = (δ‘(q0,00),1)

Computer Science Engineering Department 18 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

= δ ({ q0, q1 }, 1)
δ(q0,1) U
= (q0,1)
= {q0,q2) U Φ
= {q0,q2}
δ(δ‘(q0,001),
δ‘(q0,0010) = 0)
= δ( { q0,q2},0
= δ(q0,0) U δ(q2,0)
= {q0,q1} U Φ = {q0,q1}
δ‘(q0,00101 δ(δ‘(q0,0010),1
) = )
= δ({q0,q1},1)
δ(q0,1) U
= (q1,1)
= {q0} U {q2} = {q0,q2}
δ‘ (q0,00101) ∩ F = {q0,q2} ∩ {q2} = {q2}

The string “00101” is accepted.

3.Briefly discuss about the additional forms of proof and inductive proof ? [CO1-
L2-Nov/Dec2012]
FORMAL PROOF
Formal proof is one in which step by step procedure is used to solve the problem.
Testing the program is so important, but if our program is very complex that involving
the recursion or iteration, then our code may become incorrect.
To make the iteration or recursion to be correct, then we need to set up an inductive
hypothesis and it helps to reason that the hypothesis is consistent with iteration or
recursion.

Computer Science Engineering Department 19 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

This process of analyzing the working of a correct program is similar to proving the
theorems by inductive or induction.
Automata theory is used to cover the methodologies of formal proof. The proof may be
of,
Deductive Formal Proof
O Consists of a sequence of justified steps.
• Inductive Formal Proof
O Recursive proof of a parameterized statement that use the statement
itself with
lower values of the parameter.
Methods of formal proof involves,
 Deductive proof
 Reduction of definition
 Other theorem forms
 Theorems that appear not to be If-then

DEDUCTIVE PROOF
A deductive proof consists of a sequence of statements whose truth leads from some
initial statements called ―hypothesis‖ or the given statement to a conclusion statement.
Each step in the proof must follow by some accepted logical principle from the given
facts or some of the previous statements in the deductive proof.
Hypothesis consists of independent statements connected by a logical AND. The format
is,
“If H then C”
Where,
H is Hypothesis, C is Conclusion
Example:
If x>=4, then 2 x >= x 2

Computer Science Engineering Department 20 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

REDUCTION TO DEFINITION
If we are not sure how to start a proof, then convert all terms in the hypothesis to their
definitions.

OTHER THEOREM FORMS


If – Then forms:
The ―If-then‖ forms can be expressed in four ways and they are,
O H implies C
O H only if C
O C if H
O Whenever H holds, C follows
THEOREMS NOT TO BE IF-THEN STATEMENTS
There are some theorems in which there is no hypothesis. For example,
Sin 2 Ө + cos 2 Ө =1
But in this example, the hypothesis is indirectly present, that is an angle.
So if ―Ө is an angle ―, then ―Sin2 Ө + cos2 Ө =1‖

ADDITIONAL FORMS OF PROOF


• Proofs about sets
• Proofs by contradiction
• Proofs by counter example
Proving Equivalence about sets
If E and F are two expressions representing sets, the statement E=F means that the two
sets represented are the same. We can prove the equivalence of sets E=F in the
following ways,
• By commutative law of union of two sets R and S as RUS=SUR and E is
the expression for RUS and F is the expression for SUR and by the
commutative law of union, we can conclude that E=F.

Computer Science Engineering Department 21 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

• We can prove the set equality E=F by if and only if statement, that is ―an
element x is in E if and only if x is in F‖
• The set equality E=F can be proved by two if statements as follows,

O If x is in E, then x is in F

O If x is in F, then x is in E

Contrapositive
The contrapositive of the statement “if H then C”, is “if not C then not H‖, and it is
represented as, H
To prove that ―if H then C‖ and ―if not C, then not H‖ are logically equivalent, there are
four cases to consider.

• H and C both true

• H true and C false

• C true and H false


Proof by Contradiction
To prove the statement ―if H then C‖ is by the form,

H and not C implies Contradiction


Here we assume that the hypothesis and the negation of the conclusion as true and
then we complete the proof by showing the something known to be false. This form of
proof is called ―Proof by contradiction‖

Theorems means that the statements about an infinite number of


cases. Observations means that the statements with finite number
of values.
Counter examples
Sometimes we need to prove whether the theorem itself is true or false. It is easy to
prove that a statement is not a theorem than to prove it is a theorem.

Example I:
All primes are odd.

Computer Science Engineering Department 22 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Proof: If x is integer and x is prime, then x is odd.

Disproof: The integer 2 is prime, but 2 is even.

Example II:

Theorem: There is no pair of integers a and b such that ―a mod b = b mod a‖.

Proof: If a and b are integers, then

A mod ≠ b mod a.

INDUCTIVE PROOF
Inductive proof deals with recursively defined objects like trees and expressions of
various sorts, such as regular expression.

• Induction on integers

• Structural Inductions

• Mutual Induction

STRUCTURAL INDUCTIONS
In Automata, there are several recursively defined structures such as trees and
expressions. The structural induction deals with the recursive definition that has a
basis case in which one or more elementary structures are defined. There is an
inductive step, where more complex structures are defined in terms of previously
defined structures.
Example 1: The recursive definition of a tree.
Basis: A single node is a tree, and that node is the root of the tree.
Induction: If T1, T2…. Tk are trees, and then we can form a new tree as follows,
• Begin with a new node N, which is the root of the tree
• Add copies of all the trees T1, T2, … Tk
• Add edges from node N to the roots of each of the trees T1,T2, … Tk

Computer Science Engineering Department 23 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

T1 T2 Tk

STRUCTURAL INDUCTION (Formal Definition)


Let s(x) be a statement about the structures ‗x‘ that are defined by some particular
recursive definition.

By Basis, prove S(x) for the basis structure x.

For inductive step, take a structure ‗x‘ that the recursive definition says that it is formed
from y1, y2,…,yk. Assume the statements s(y1),s(y2)…s(yk) and use these to prove s(x).

Our conclusion is that s(x) statement is true for all x.

MUTUAL INDUCTIONS
Sometimes, we cannot prove a single statement by induction and we may need to
prove thegroup of statements S1(n),S2(n),….Sk(n) together by the induction „n‟.

Automata theory involves proving the group of statements, one for each state. Proving
the group of statements is similar to proving the conjunction of all the statements.
For example, the group of statements, S1(n), S2(n)….Sk(n) can be replaced by the
single statement as,
S1(n) AND S2(n) AND … AND Sk(n)
When there are several independent statements to prove, then we keep the statements
separate and to prove them all in their own parts of the basis and inductive step. This
form of proof is called Mutual Inductions.

Computer Science Engineering Department 24 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

4. Convert to a DFA, the following NFA.[CO1-L3-MAY/JUNE 2013]

δ a b

P {p} {p,q}

Q {r} {r}

*r {Φ} {Φ}

Solution
NFA transition diagram for the above NFA is,

Step 2

Computer Science Engineering Department 25 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

(B,a) δN ({p,q},a) = δ (p,a) U δ (q,a)


= {p} U {r}
= {p,r}
So δD ([p,q] , a) = [p,r] -
(B,b) δN({p,q},b) = δ(p,b) U(q,b)
= {p,q} U {r}
={p,q,r}
So δD ([p,q],b) = [p,q,r] -

Step 3
(C,a) δN({p,r},a) = δ (p,a)
U δ(r,a) =
{p} U ф
={p}

So δD N({p,r},b)= δ(p,b) U δ(r,b)

{p,q} U Φ
F {p,q}

So δD([p,r],b) = [p,q] -

Step 4
(D,a) δN({p,q,r},a) = δ(p,a) U δ(q,a) U δ{r,a}
= {p} U {r} U Φ
= {p,r}

So δD([p,r],0) = [p,r] -

Computer Science Engineering Department 26 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

(D,b) δN({p,q,r},b) = δ(p,b) U δ (q,b) U δ(r,b)


= {p,q} U {r} U Φ
= {p,q,r}
So δD([p,q,r],b) = [p,q,r] -

The transition table for the DFA is shown


below,

A b

[p] [p] [p,q]

[p,q] [p,r] [p,q,r]

*[p,r] [p] [p,q]

*[p,q,r] [p,r] [p,q,r]

Computer Science Engineering Department 27 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

5. Convert to a DFA, the following NFA.[CO1-L3]


0 1

p {p,q} {p}

q {r {r}

r {s} Φ

*s {s} {s}

Computer Science Engineering Department 28 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Starting state = {p}


For DFA,
{q0}= [q0] in NFA - A
Step 1
(A,0) δN({p},0) = {p,q} So δD ([p],0) = [p,q] -B
(A,1) δN ({p} ,1) = {p} So δD([p],1) = [p] -A
Step 2
(B,0) δN ({p,q},0) = δ (p,0) U δ (q,0) = {p,q} U {r}
= {p,q,r} So δD ([p,q] , 0) = [p,q,r] -C
(B,1) δN({p,q},1) = δ(p,1) U(q1,1) = {p} U {r} ={p,r} -D
So δD ([p,q],1) = [p,r] -C
Step 3
(C,0) δN({p,q,r},0) = δ (p,0) U δ(q,0) U δ(r,0) = {p,q} U {r} U {s}
= {p,q,r,s} So δD ([p,q,r],0) = [p,q,r,s] -E

(C,1) δN({p,q,r},1) = δ(p,1) U δ(q,1) U δ(r,1) = {p} U {r} U Φ

Computer Science Engineering Department 29 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

= {p,r} So δD([p,q,r],1) = [p,r] -D


Step 4
(D,0) δN({p,r},0) = δ(p,0) U δ{r,0} = {p,q} U {s} = {p,q,s} -F

(D,1) δN({p,r},1) = δ(p,1) U δ(r,1)


= {p U Φ }
={p}
So δD([p,r],1) = [p] A
Step 5
(E,0) δN({p,q,r,s},0) = δ (p,0) U δ(q,0) U δ(r,0) U δ(s,0)
= {p,q} U {r} U {s} U {s}
So δD([p,q,r,s],0) = [p,q,r,s]
(E,1) δN({p,q,r,s},1) = δ(p,1) U δ(q,1) U δ(r,1) U δ(s,1)
= {p} U {r} U {s} = {p,r,s}

So δD([p,q,r,s],1) =[p,r,s]

Step 6
δN({p,q,s},0) = δ(p,0) U δ{q,0) U
(F,0) δ(s,0)
= {p,q} U {r} U {s} = {p,q,r,s}
So δD([p,q,s],0) = [p,q,r,s] E
(F,1) δN({p,q,s},1) = δ(p,1) U δ{q,1) U δ(r,1) U δ (s,1)
= {p} U {r} U {s} = {p,r,s}
So δD([p,q,s],1) = [p,r,s]

Computer Science Engineering Department 30 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Step 7

(G,0) δN({p,r,s},0) = δ(p,0) U δ(r,0) U δ(s,0)


= {p,q} U {s} = {p,q,s}
So δD([p,r,s],0) = [p,q,s]
(G,1) δN({p,r,s},1) = δ(p,1) U δ(r,1) U δ(s,1)
= Φ U {p} U {s} ={p,s}
So δD([p,r,s],1) = [p,s] H

Step 8
(H,0) δN({p,s},0) = δ(p,0) U δ(s,0)
= {p,q} U {s} ={p,q,s}
So δD({p,s},0) = [p,q,s] F
{H,1) δN({p,s},1) = δ(p,1) U δ(s,1)
= {p} U {s} = {p,s}
So δD([p,s],1) = [p,s]
The transition table for the DFA is shown below,

0 1

[p] [p,q] [p]

[p,q] [p,q,r] [p,r]

[p,q,r] [p,q,r,s] [p,r]

[p,r] [p,q,s] [p]

*[p,q,r,s] [p,q,r,s] [p,r,s]

Computer Science Engineering Department 31 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

*[p,q,s] [p,q,r,s] [p,r,s]

*[p,r,s] [p,q,s] [p,s]

*[p,s] [p,q,s] [p,s]

6. Convert the following NFA with Є to NFA without Є[CO1-L3]

a b c Є

{q} {p} Φ Φ

q {r} Φ {q} Φ

*r Φ Φ Φ {r}

Solution:
The NFA with Є transition diagram

Computer Science Engineering Department 32 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

is,

Step 1
Є – Closure (p) = { p, q, r }
Є – Closure (q) = { q, r }
Є – Closure (r) = { r }
Step 2: Processing state p
δ (p, a) = Є – closure (δ (δ‘ ( p, Є ) , a) )
= Є – closure (δ ({ p, q, r }), a) )
= Є – closure (δ (p, a) U δ (q, a) U δ (r, a) )
= Є – closure ({ p })
δ (p, a) = { p, q, r}
δ (p, b) = Є – closure (δ (δ‘ (p, Є), b) )
= Є – closure (δ ({ p, q, r }, b) )
= Є – closure (δ (p, b) U δ (q, b) U δ (r, b) )
= Є – closure (q)
δ (p, b) = { q, r }
δ (p, c) = Є – closure (δ (δ‘ (p, Є) , c) )
= Є – closure (δ ({ p, q, r } , c ) )
= Є – closure (δ (p, c) U δ (q, c) U δ (r, c) )
= Є – closure ( r )

Computer Science Engineering Department 33 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

δ (p, c) ={r}
Processing state q
δ (q, a) = Є – closure (δ ( δ‘ (q, Є ), a) )
= Є – closure (δ ({ q, r }, a) )
= Є – closure (δ (q, a) U δ (r, a) )
= Є – closure (Φ)
δ (q, a) =Φ
δ (q, b) = Є – closure ( (δ ( δ‘ (q, Є ), b) )
= Є – closure (δ ({ q, r }, b) )
= Є – closure (δ (q, b) U δ (r, b) )
= Є – closure (q)
δ (q, b) = { q, r }
δ (q, c) = Є – closure ( (δ ( δ‘ (q, Є ), c) )
= Є – closure (δ ({ q, r }, c) )
= Є – closure (δ (q, c) U δ (r, c) )
= Є – closure (r)
δ (q, c) ={r}
Processing state r
δ (r, a) = Є – closure ( (δ ( δ‘ (r, Є ), a) )
= Є – closure (δ ({ r }, a) )
= Є – closure (Φ)
δ (r, a) =Φ
δ (r, b) = Є – closure ( (δ ( δ‘ (r, Є ), b) )
= Є – closure (δ ({ r }, b) )
= Є – closure (Φ)

Computer Science Engineering Department 34 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

δ (r, b) =Φ
δ (r, c) = Є – closure ( (δ ( δ‘ (r, Є ), c) )
= Є – closure (δ ({ r }, c ) )
= Є – closure ( r )

δ (r, c) ={r}

NFA Transition Table:

0 1 2

{p, q, r} {q, r} {r}

*q Φ {q, r} {r}

*r Φ Φ {r}

NFA Transition Diagram


b c
a
a,b
b,c
r
p q

a,b,c

Computer Science Engineering Department 35 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

7. Explain the extended transition function for NFA, DFA and NFA – ε[CO1-L2-
Nov/Dec 2007]
Extended Transition Function for DFA
The transition function ‗δ‘ is extended to δ‘ (or) δ‘ extended transition function that
operates on strings and states. The extended transition function describes what
happens when we start in any state and follow any sequence of inputs. If δ is the
transition function, then the extended transition function constructed from δ is called δ‘.
The extended transition function δ‘ that takes a state ‗q‘ and a string ‗w‘ and reaches
the state ‗s‘. That is δ‘ reaches the state‗s‘ from ‗q‘ after processing the sequence of the
string ‗w‘.

Basis:

Induction:

Suppose ‗w‘ is the string of the form ‗xy‘, then

δ‘ (q, w) = δ‘ (q, xy) = δ (δ‘ (q, x), y)

For example if the string w=1101. Then the last symbol ‗1‘ is given to y and the
remaining is x. So x=110 and y=1.

Extended Transition Function for NFA


Extending the transition function δ of an NFA to an extended transition function δ‘ that
takes a state q and a string of input symbols w and returns the set of states that the
NFA is in starting state q and process the string w.

Basis:
δ‘ (q, Є) = { q } => If we are in state q and read no input then we are in state q itself.
Without reading input NFA remains in the same state.

Induction:

Suppose ‗x‘ is the string of the form ‗wa‘, then

Computer Science Engineering Department 36 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

δ‘ (q, x) = δ‘ (q, w) = { P1, P2……Pk }

and

Therefore
δ‘ (q, wa) = δ‘ (q, x) = { R1, R2…..Rn }
The language accepted by NFA M= (Q, Є, δ, q0, F) is denoted by L (M) and it is
defined as,

L (M) = { w/ δ‟ (q0, w) contains a state in F) }

(Or)

L (M) = { w/ δ‟ (q0, w) ∩ F ≠ Φ }

Extended Transition Function for NFA-Є


Q,
The NFA – such that δ‘ (q0,
w) reaches the states p such that, NFA with Є from state q0 on input symbol w reaches
p including Є as transition for an empty input string.
The Є – closure of q0 is (Є – closure (q0)) denotes the set of all states p such that
there is a path (or) transition from q0 to p on Є transition.

Let P be the set of all states { P1, P2,….Pi}

Then Є – closure (p) = Ū Є – Closure (q)

δ‘ transition function is defined as follows,


Basis:
δ (q, Є) = Є – Closure (q)
Induction:

δ‘ (q, wa) = Є – Closure (P)


Where P= {P/for some r in δ (q, w), P in δ (r, a) }
δ (q, w) = Є- Closure (R)

Computer Science Engineering Department 37 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

δ (R, a) = Є – Closure (P)


δ (q, wa) = Ū Є closure (P)
p in R
The language accepted by NFA with Є transition is,
L (M) = { w/ δ (q0, w) contains a state is F }

8. Convert the following NFA – ε to DFA directly.[CO1-L3]


` a b

q0 q1
Solution:

The NFA – ε transition table is shown below:


a b ε

q
0 {q0} Φ {q1}
*q1 Φ {q1} Φ

Step 1:
ε – closure(q0) ={q0,q1}
ε – closure(q1)= {q1}

Step 2:
ε – closure(q0) = {q0,q1}
(A,a) δN({q0,q1},a) = ε – closure(δ(q0,a) U δ(q1,a))
= ε – closure(q0) =
{q0,q1}
So δD([q0,q1],a) = [q0,q1]
(A,b) δN({q0,q1},b) = ε – closure(δ(q0,b) U δ(q1,b))
= ε – closure(q1) = {q1}

Computer Science Engineering Department 38 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

So δD([q0,q1],b) = [q1]

Step 3:
(B,a) δN({q1},a) = ε – closure(δ(q1,a)) = ε – closure(Φ)
So δD([q1],a) = Φ

δN({q1},b) = ε – closure(δ(q1,b))= ε – closure(q1) =


(B,b) {q1}
So δD([q1],b) = [q1]

DFA Transition Table:


a B

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

*[q1] Φ [q1]

UNIT –II
GRAMMARS

Computer Science Engineering Department 39 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

PART-A

1. Define a Context Free Grammar.[CO2-L1] [May/June 2010]


A CFG is a way of describing languages by recursive rules called productions. A CFG
consists of a set of variables, a set of terminal symbols, and a start variable, as well as
the productions. Each production consists of a head variable and a body consisting of a
string of zero or more variables and /or terminals

A CFG is denoted as G = (V, T, P, S)


Where,
V finite set of variables
T finite set of terminals
P finite set of productions
S Start symbol

2. What are the applications of Context free languages?[CO2-L1] [Dec 2009]


1.Context free languages are used in:
2.programming languages.
3.Formalizing the notion of parsing.
4.Translation of programming languages.
5.String processing applications.

3. What are the uses of Context free grammars?[CO2-L1]


Construction of compilers.
• Simplified the definition of programming languages.
• Describes the arithmetic expressions with arbitrary nesting of balanced
parenthesis
• Describes block structure in programming languages.
• Model neural nets

4. What is the language generated by CFG or G? [CO2-L1]


The language generated by Grammar G is (L(G) ) = {w | w in T* and S *=>w }
(1) The string contains only terminals.

Computer Science Engineering Department 40 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

(2) The string can be derived from start symbol S.

5. What is: (a) CFL (b) Sentential Form? [CO2-L1]


The language of the CFG is the set of terminal strings we can so derive; it is called a
context-free language. The language generated by Grammar G is:
(L(G)) = {w | w in T* and S => w }
Any step in a derivation is a string of terminal and / or variables. We call such a string a
sentential form. If the derivation is leftmost, then the string is a left sentential form.
6. What is the language generated by the grammar G=(V, T, P, S)Where P={ S
aSb,S ab} ? [CO2-L1]
Solution: S aSb aaSbb ……………… an bn
Thus the language L(G)={anbn | n≥1}.
The language has strings with equal number of a‘s and b‘s.

7. What is: (a) Derivation (b) Sub tree [CO2-L1]


(a)Let G = (V, T, P, S) be the context free grammar. Beginning with the start symbol, we
derive terminal strings by repeatedly replacing a variable by the body of some
production with that variable in the head.
If A β is a production of P and a and b are any strings in (VUT)* then α A γ αβγ
(b) A subtree of a derivation tree is a particular vertex of the tree together with all its
descendants, the edges connecting them and their labels. The label of the root may not
be the start symbol of the grammar.

8. If S aSb | aAb, A bAa | ba .Find out the CFL[CO2-L1]


S aAb abab
S aSb aaAbb aababb (sub S aAb)
S aSb aaSbb aaaAbbb aaababbb
Thus L = {anbmambn , where n, m>=1}

9. What is meant by left and right sentential form?[CO2-L1]


Left Sentential Form: If the string a can be generated from the starting symbol by using
left most derivation, such that S==> α is left sentential form.

Computer Science Engineering Department 41 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Right Sentential Form: If the string a can be generated from the starting symbol by
using rightmost derivation, such that S==> α is right sentential
10. What is an ambiguous grammar?[CO2-L1-Dec 2009]
A grammar is said to be ambiguous if it has more than one derivation trees for a
sentence or in other words if it has more than one leftmost derivation or more than one
rightmost derivation.
11. Consider the grammar P= {S aS | aSbS | є} is ambiguous by constructing: (a)
Two parse trees (b) two leftmost derivation (c) rightmost derivation [CO2-L1-
Nov/Dec 2007]
Let w =
aab
(b) (i) SaS (ii) SaSbS
aaSbS aaSbS
aabS aabS
aab aab
(c) (i) SaS (ii) SaSbS
aaSbS aSb
aaSb aaSbS
aab aaSb
aab

12. What are the properties of the CFL generated by a CFG? [CO2-L1]
Each variable and each terminal of G appears in the derivation of some word
in L There are no productions of the form A B. where, A and B are variables.

13. Find the grammar for the language L = {a2nbc, where n>1}[CO2-L3]
Let G = ({S, A, B}, {a, b, c} ,P , {S})
Where Production is:
S Abc
A aaA | є
Find the language generated by:
S 0S1 | 0A | 0 |1B | 1

Computer Science Engineering Department 42 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

A 0A | 0
B 1B | 1

14. Construct the grammar for the language L= { anban | n>=1}.[CO2-H3]


The grammar has the production P as:
S aAa
A aAa | b
The grammar is: G ={ {S, A} ,{a, b} ,P, S)}
15. Construct a grammar for the language L which has all the strings which are
all palindrome over S = {a, b}. [CO2-H3]
G = ({S}, {a, b}, P, S )
Production { S aSa , S bSb, S a, S b, S є } which is in palindrome.

16. Differentiate sentences Vs sentential forms[CO2-L2]


A sentence is a string of terminal symbols. A sentential form is a string containing a mix
of variables and terminal symbols or all variables. This is an intermediate form in doing
a derivation.

17. What is a formal language? [CO2-L1]


Language is a set of valid strings from some alphabet. The set may be empty, finite or
infinite. L(M) is the language defined by machine M and L( G) is the language defined
by Context free grammar.
The two notations for specifying formal languages are:
1. Grammar or regular expression (Generative approach)
2. Automaton (Recognition approach)
18. What is Backus-Naur Form (BNF)? [CO2-L1]
Computer scientists describe the programming languages by a notation called Backus-
Naur Form. This is a context free grammar notation with minor changes in format and
some shorthand.

19. Let G= ( {S, C} ,{a, b}, P, S) where P consists of S aCa , C aCa |b. Find L(G).
[CO2-L2]

Computer Science Engineering Department 43 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

S aCa aba
S aCa aaCaa aabaa
S aCa aaCaa aaaCaaa aaabaaa
Thus L(G) = { a ban , whre n>=1
n

20. Find L(G) where G= ( {S} ,{0, 1}, {S 0S1 ,S є}, S )[CO2-L2-Dec 2010]
S є , є is in L(G)
S 0S1 0є1 01
S 0S1 00S11 0011
Thus L(G)= { 0n1n | n>=0}

21. What is meant by inherent ambiguous grammar? [CO2-L1]


A context free language L is said to be inherently ambiguous if all its grammars are
ambiguous grammar. If even one grammar for L is unambiguous then L is an
unambiguous language. Inherent ambiguous grammars are one for which unambiguous
grammars do not exist.

22. Find the language generated by a CFG. G = ( {S}, {0 , 1}, { S 0/1/є, S


0S0/1S1/S} ) [CO2-L2]
(i) S0S0 (ii) S1S1
01S10 10S01
10001 L= { w; w is a palindrome}
01010
Consider G whose productions are S aAS/a, A SbA/SS/ba. Show that S
aabbaa.S S
Aas aAS
aSbAS A SbA
aabAS S a
aabbaS A ba
aabbaa S a

23. What is a parser? [CO2-L1]


A parser for grammar G is a program that takes as input a string w and produces as
output either a parse tree for w, if w is a sentence of G or an error message indicating

Computer Science Engineering Department 44 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

that w is not a sentence of G.

24. Define left most derivation and right most derivation. [CO2-L1]
Left most derivation: In this method, we replace the left most non-terminal by one of its

production in the grammar. Such a derivation is known as left most derivation and it‘s
represented by using the relation *=> and *=> for one or more steps respectively.
lm lm
Right most derivation: In this method, we replace the right most nonterminal by one
of its production in the grammar. Such a derivation is known as left most derivation
and it‘s represented by using the relation *=> and *=> for one or more steps
respectively.
rm rm

25. Construct a context free grammar for generating the language L ={anbn/n≥1}
[CO2-L3] (Nov/Dec-2004, 2010, 2013, May-05, 06)
G = {V, T, P, S}
P = { S aSb, S ab}

26.Construct a CFG over {a,b} generating a language consisting of equal no. of


a‟s and b‟s. [CO2-L1-MAY 2011]
Let G={V, T,
P, S} V = {
S, A, B}
T = { a, b}, S is a Start symbol.
P = { S aB | bA, A a | aS | bAA, B b | bS | aBB }

27. Convert the following grammar into an equivalent one with no unit
productions and no useless symbols S ABA, A aAA | aBC | bB, B A | bB | Cb, C
CC | cC[CO2-L2-Nov/Dec 2011]
As we can clearly observe that B A is a unit production. So we will eliminate it
Similarly, C CC |cC does not lead to any terminating string. Hence it is considered as
useless symbol. Same is true for the other non-terminal symbols. i.e. A and B. hence all
A, B and C and hence S are useless symbols.

Computer Science Engineering Department 45 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

28.When a grammar is said to be ambiguous?[CO2-L1-May 2013]


• A string w is derived ambiguously in context-free grammar G if it has two or more
different leftmost derivations. Grammar G is ambiguous if it generates some
string ambiguously.
• Sometimes an ambiguous grammar can be transformed into an unambiguous
grammar for the same language.
• Some context-free grammars can be generated only by ambiguous grammars.
These are known as inherently ambiguous languages.
• L = { ai b j ck | i = j or j = k }

29.Define Chomsky Normal Form? [CO2-L1]


CFGs in certain standard forms are quite useful for some computational problems.
A context-free grammar is in Chomsky normal form (CNF) if every rule is either of
the form. A BC or A a
Given a CNF: there are no useless symbols, and every production body consists of
either two variables or one terminal
Where a is a terminal and A, B and C are variables – except B and C may not be the
start variable. In addition, we allow the rule S Є if necessary.

30.Derive the rules to remove Є productions with an suitable example [CO2-L2-


Dec‟09]
• If Є L, then we cannot do much. S Є is needed for this.
• For all rules of the type A Є and A is not the start symbol, we proceed as follows:
• For occurrence of an A on the right-hand side of a rule, we add a rule with that
occurrence deleted.
• For a rule like R uAv, we add the rule R uv (either u or v not Є)
• For a rule like R A, we add R Є, unless we removed R Є earlier.
• For a rule with multiple occurences of A, we add one rule for each
combination. R uAvAw would add R uvAw, R uAvw, and R uvw.

31. Define Parse Tree (Or) Derivation Tree[CO2-L1]


The strings that are derived from the CFG can be represented in a tree format known as

Computer Science Engineering Department 46 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Parse tree or derivation tree. This parse tree clearly shows how the symbols of a
terminal strings are grouped into substrings, each of which belongs to the language of
one of the variables of the grammar.

32.What are the two major normal forms for context-free grammar? [CO2-L1]
The two Normal forms are
i. Chomsky Normal Form (CNF)
ii. Greibach Normal Form (GNF)

33. How do you simplify the context - free grammar? [CO2-L1]

not
appear in any derivation of a terminal string from the start symbol.
-productions which is of the form A ε for some variable A.
B for variables A, B.
of the normal forms to get the simplified CFG

34. What is GNF? [CO2-L1]


Every CFL L without ε cab be generated by a grammar for which every production is
of the form A aα, where A Є V(Variable), a Є T(Terminal), is a string of variables.

PART – B

1. Derive the strings a*(a+b00) using leftmost and rightmost derivation for the
following[CO2-H2]
production.

Computer Science Engineering Department 47 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

1. E I
2 .E E+E
3. E E*E
4. E (E)
5. I a
6. I b
7. I Ia
8. I Ib
9. I I0
10 I I1

Solution
Leftmost Derivation
E E*E
I*E (E I)
a*E (I a)
a*(E) (E(E))
a*(E+E) (E E+E)
a*(I+E) (E I)
a*(a+E) (I a)
a*(a+I) (E I)
a*(a+I0) (I I0)
a*(a+I00) (I I00)

a*(a+b00) (I b)

Rightmost

Derivation

E E*E

E*(E) (E (E))

E*(E+E) (E E+E)

Computer Science Engineering Department 48 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

E*(E+I) (E I)

E*(E+I0) (I I0)

E*(E+I00) (I I0)

E*(E+b00) (I b)

E*(I+b00) (E I)

E*(a+b00) (I a)

I*(a+b00) (E I)

a*(a+b00) (I a)

2. Show that the grammar S aSbS | bSaS | e is ambiguous and what is the
language generated by this grammar?[CO2-L2-Nov/Dec 2006]
Solution - Input string w = aabbab
Leftmost Derivation
S aaSbSbS
aaєbSbS
aabSbS
aabєbS
aabbS
aabbaSbS
aabbaєbS
aabbabS
aabbabє
aabbab
Rightmost Derivation

S aSbS

aSbaSbS

Computer Science Engineering Department 49 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

aSbaSbЄ

aSbaSb

aSbaЄb

aSbab

aaSbSbab

aaSbЄbab

aaSbbab

aaєbbab

aabbab

3 . Let G be the grammar S aB|bA, A a|aS|bAA, B b|b|S|aBB. For the string


aaabbabbba find a leftmost derivation. (CO2-L3) (May/June‟07)(Apr/May „08)
Solution SaB
aaBB (B aBB)
aaaBBB (B aBB)
aaabBB (B b)
aaabbB (B b)
aaabbaBB (B aBB)
aaabbabB (B b)
aaabbabbS (B bS)

Computer Science Engineering Department 50 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

aaabbabbbA (S bA)
aaabbabbba (A a)

4.If G is a grammar S SbS | a prove that G is ambiguous [CO2-L3-Apr/May 2004]


Solution - Let w = abababa
Leftmost Derivations

S SbS
abS (S a)
abSbS (S SbS)
ababS (S a)
ababSbS (S SbS)
abababS (S a)
abababa (S a)
→ SbS
SbSbS
abSbS
ababS
ababSbS
abababS
abababa

5.Consider the following grammar, S aS | aSbS | Є Show that this grammar is


ambiguous. For the string “aab” that has two parse trees, two left most derivation
and two leftmost derivation and two right most derviations. [CO2-L3]
Solution:
Production: S aS| aSbS| Є

Left Most Derivation

S aSbS
aaSbS
aaєbS

Computer Science Engineering Department 51 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

aabS
aabє
S aab

Parse tree for left most derivation

S
S

S
a
b
S
a

S
b
a S

Є
a

Є Є
Є

First Rightmost Derivation

S aS

Computer Science Engineering Department 52 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

aaSbS
aaSbЄ
aaSb

aaЄb

aab

Second Rightmost Derivation

S aSbS

aSbЄ

aSb

aaSb

aaЄb

aab

Parse tree for Rightmost Derivation

6. Eliminate all ε rules A → ε, where A is not the start variable[CO2-L2]


For each rule with an occurrence of A on the right-hand side, add a new rule with
the A deleted
R → uAv becomes R → uAv | uv

Computer Science Engineering Department 53 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

R → uAvAw becomes R → uAvAw | uvAw | uAvw | uvw


R → ε unless we had already removed R
- If we have R → A, replace it with → ε

Eliminate all unit rules of the form A → B

- For each rule B → u, add a new rule A → u, where u is a string of terminals


and variables, unless this rule had already been removed
- Repeat until all unit rules have been replaced
- 3. All non-terminal strings of CFG required the exact length 2, so the CFG is in
CNF.
Chomsky Normal Form
Example1
Convert the following CFG to Chomsky Normal Form (CNF):
S aX | Yb
X S|Є
Y bY | b

Solution 1
Step 1 - Kill all Є productions
By inspection, the only nullable non-terminal is X. Delete all Є productions and add new
productions, with all possible combinations of the nullable X removed.
The new CFG, without Є productions, is:

S aX | a | Yb
X S
Y bY | b

Step 2 - Kill all unit productions


The only unit production is X S, where the S can be replaced with all S‘s non-unit
productions (i.e. a, aX & Yb).
The new CFG, without unit productions, is:

Computer Science Engineering Department 54 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

S aX | a | Yb

X aX | a | Yb
Y bY | b

Step 3 - Replace all mixed strings with solid nonterminals


Create extra productions that produce one terminal, when doing the replacement.
The new CFG, with a RHS consisting of only solid non-terminals or one terminal is:

S AX | YB | a
X AX | YB | a
Y BY | b
A a
B b

Step 4 - Shorten the strings of nonterminals to length 2.


All non-terminal strings on the RHS in the above CFG are already the required length,
so the CFG is in CNF.
Example2
Convert the following CFG to Chomsky Normal Form (CNF):

S AA
A B | BB
B abB | b | bb
Solution 2
Step 1 - Kill all Є productions
There are no Є productions, so none of the non-terminals is nullable.
The CFG remains unchanged.
Step 2 - Kill all unit productions
The only unit production is A B, where the B can be replaced with all B‘s non-unit
productions (i.e. all of them).
The new CFG, without unit productions, is:
S AA

Computer Science Engineering Department 55 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

A BB | abB | b | bb
B abB | b | bb
Step 3 - Replace all mixed strings with solid nonterminals.
Create extra productions that produce one terminal, when doing the replacement.
The new CFG, with a RHS consisting of only solid nonterminals or one
terminal is: S AA
A BB | XYB | b |
YY B XYB | b |
YY
X
a
Y

Step 4 - Shorten the strings of nonterminals to length 2.


Create new, intermediate nonterminals to accomplish this.
The new CFG, in CNF is:
S AA
A BB | RB | b | YY
B RB | b | YY
R XY
X a
Y b
Example3
Convert the following CFG to Chomsky Normal Form (CNF):
S XaX | bX | Y
X XaX | XbX | Є
Y ab

Solution 3
Step 1 - Kill all Є productions
By inspection, the only nullable nonterminal is X. Delete all Є productions and add new
productions, with all possible combinations of the nullable X removed.
The new CFG, without Є productions, is:

Computer Science Engineering Department 56 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

S XaX | aX | Xa | a | bX | b | Y
X XaX | aX | Xa | a | XbX | bX | Xb | b
Y ab
Step 2 - Kill all unit productions
The only unit production is S Y, where the Y can be replaced with all Y‘s non-unit
productions (i.e. ab). Furthermore, the Y-production can be completely removed, since
its only purpose is to turn S into ab in one particular production sequence: S Y ab.
The new CFG, without unit productions, is:
S XaX | aX | Xa | a | bX | b | ab
X XaX | aX | Xa | a | XbX | bX | Xb | b
Step 3 - Replace all mixed strings with solid nonterminals.
Create extra productions that produce one terminal, when doing the replacement.
The new CFG, with a RHS consisting of only solid nonterminals or one terminal is:

S XAX | AX | XA | BX | AB | a | b
X XAX | AX | XA | XBX | BX | XB | a | b
A a
B b
Step 4 - Shorten the strings of nonterminals to length 2.
Create new, intermediate nonterminals to accomplish this.
The new CFG, in CN is:
S XR | AX | XA | BX | AB | a | b
R AX
X XR | AX | XA | XQ | BX | XB | a | b
Q BX
A a
B b

7. Find the Chomsky normal form equivalent to the following grammar,[CO2-


L2-Dec 2003/May 2007]

S aAbB

Computer Science Engineering Department 57 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

A aA/a
B bB/b
Solution 11
Eliminate useless symbols
• a,b generates itself, So a,b are generating symbol.
• A a,A generates a, So ‗A‘ is generating symbol.
• B b,B generates ‗B‘, So ‗B‘ is also generating symbol.
• S aAbB, So S is also generating symbol.
Generating symbol={S,A,B,a,b}
• S is always reachable
• S aAbB, So a,A,b,B are reachable.

Reachable symbol={S,A,B,a,b}

Since all the variables and terminals are both generating and reachable, the
grammar does not have useless symbols.
Eliminate ε-production
There is no nullable symbol and ε-production.
Eliminate Unit production
There is no unit production and the grammar has only the (S,S) (A,A) (B,B) as
unit pair b basis.
CNF Grammar:
S C1C3
A C1A/a
B C2B/b
C1 a
C2 b

Computer Science Engineering Department 58 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

C3 AC4
C4 C2B

8.Convert the following grammar into GNF[CO2-L3]


S AA/0
A SS/1
Solution 16
Step 1: The grammar
definition is G
= (V, T, P, S)

G = ({A, S}, {0, 1}, P,


S) Where the production P
consists of,
S AA/0
A SS/1

Step 2: The given grammar is Chomsky normal form such that each production is
of the form A B or A a
So the given grammar is in CNF
Step 3: Replace the variables S and A as A1 and
A2 such that S = A1
A = A2
Now the grammar after replacing the variable becomes,
A1 A2 A2/0
A2 A1 A1/1
Step 4: Now process each production for each variable.
(i) A1 A2A2/0
(Here i=1, j=2 and i<j(1<2) So leave the production

Computer Science Engineering Department 59 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

as such) Since i<j(1<2), the production remains


the same,

A1 A2A2/0 . ......(1)

(ii) A2 A1 A1/1

Here i=2,j=1 and i>j So apply rule 1 to replace the production body of
A1 to A2. So the production of A2 becomes,
A2 A1A1/1
Becomes,
A2 A2A2A1/0A1/1 ...........(2)
(iii) Now consider A2 A2A2A1/0A1/1
Here i=j such that i=2, j=2, So apply rule 2 to introduce
new variable B2 as
follows,
B2 A2A1/A2A1B2
A2 0A1/1/0A1B2/1B2 .........(3)
(iv) Now apply the production A2 in (3) to (1) So the production
becomes, A1 A2A2/0

Becomes,
A1 0A1A2/1A2/0A1B2A2/1B2A2/0 .....(4)
Apply the production A2 in (3) to B2 in (3)
B2 A2A1/A2A1B2
Becomes,
B2 0A1A1/1A1/0A1B2A1/1B2A1/ 0A1A1B2/1A1B2/0A1B2A1B2/1B2A1B2
After applying all the rules of GNF, the resulting production of the variables A 1,A2,B2
results in the required form of GNF and the resulting grammar is ,

Computer Science Engineering Department 60 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

GNF Grammar
A1 0A1A2/1A2/0A1B2A2/1B2A2/0
A2 0A1/1/0A1B2/1B2
B2 0A1A1/1A1/0A1B2A1/1B2A1/ 0A1A1B2/1A1B2/0A1B2A1B2/1B2A1B2

9.Convert the following grammar to GNF[CO2-L3]


S AB
A BS/b
B SA/a
Solution 17
Step 1: The grammar
definition is, G
=({S,A,B},{a,b}, P,S)

Where the production P


consists of S AB
A BS/b
B SA/a
Step 2: The above grammar is in Chomsky normal form. Introduce new variables
A1, A2, A3
Step 3: Replace S = A1, A = A2, B = A3, So the grammar
becomes, A1 A2A3
A2 A3A1/b
A3 A1A2/a
Step 4: Process each production of the grammar
(i) A1 A2A3

(Here i=1,j=2 and


i<j(1<2) So leave the production as such)

Computer Science Engineering Department 61 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Since i<j (1<2), the production remains the same,


.......(1
A1 A2A3 )
(ii) A2 A3A1/b
(Here i=2,j=3 and
i<j(2<3) So leave the production as such)
Since i<j (2<3), the production remains the same,
........(2
A2 A3A1/b )
(iii) A3 A1A2/a
(Here i=3,j=1 and i>j(3>1)
So apply rule 1 to replace the production body of A 1 to A3, So the
production of A3 becomes,
A3 A1A2/a
Becomes

A3 A2A3A2/a
Now again i=3,j=2 and i>j(3>2) So apply rule 1 to replace the production
body of A2 to A3, So the production of A3 becomes,
A3 A2A3A2/a
Becomes,
A3 A3A1A3A2/bA3A2/a
Here i=j such that i=3,j=3 So apply rule 2 to introduce new variable B 3 as,
B3 A1A3A2/A1A3A2B3
A3 bA3A2/a/bA3A2B3/aB3
(iv) Now apply the production (4) to (2) as follows,

A2 A3A1/b
Becomes,
` A2 bA3A2A1/aA1/bA3A2B3A1/aB3A1/b

Computer Science Engineering Department 62 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

……………………….….(5)
Now apply the production (5) to (1) as follows,
A1 A2A3
Becomes,
A1 bA3A2A1A3/aA1A3/bA3A2 B3A1A3/aB3A1A3/bA3
………………..(6)
Now apply the production (6) to (3) as follows
B3 A1A3A2/A1A3A2B3
Becomes,
B3 b A3A2A1A3A3A2 | aA1A3A3A2 | bA3A2B3A1A3A3A2 |
aB3A1A3A3A2| bA3A3A2 | bA3A2A1A3A3A2B3 | aA1A3A3A2B3 |
bA3A2B3A1A3A3A2B3 | aB3A1A3A3A2B3 | bA3A3A2B3

UNIT III

PUSHDOWN AUTOMATA

PART-A

Computer Science Engineering Department 63 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

1. Define Pushdown Automata.[CO3-L1]


A pushdown Automata M is a system (Q, Σ, Ґ, δ, q0, Z0, F)
Where
Q is a finite set of states.
Σ is an alphabet called the input
alphabet. Ґ is an alphabet called
stack alphabet.
q0 in Q is called initial
state. Z0 in Ґ is start
symbol in stack. is the set
of final states.
δ is a mapping from Q X ( Σ U {Є} ) X Ґ to finite subsets of Q X Ґ*.

2. Specify the two types of moves in PDA.[CO3-L2]


The move dependent on the input symbol (a) scanned is:
δ(q,a,Z)={ (p1, γ1), (p2,γ2),…..(pm,γm ) }
Where q and p are states , a is in Σ ,Z is a stack symbol and γi is in Ґ*. PDA is in state
q, with input symbol a and Z the top symbol on state enter state p i and replace symbol Z
by string γi

The move independent on input symbol is (Є-move):


δ(q,Є,Z) = { ( p1,γ1 ), ( p2,γ2 ),…………( pm,γm ) }.
Is that PDA is in state q , independent of input symbol being scanned and with Z
the top symbol on the stack enter a state pi and replace Z by γi.

3. What are the different types of language acceptances by a PDA and define
them.[CO3-L1]
For a PDA M=(Q, Σ ,Ґ ,δ ,q0 ,Z0 ,F ) we define :
i) Language accepted by final state L(M) as:
{ w | (q0 , w , Z0 ) |-- ( p, Є , γ ) for some p in F and γ in Ґ * }.
ii) Language accepted by empty / null
stack N(M) is: { w | (q0,w ,Z0) |----( p, Є,
Є ) for some p in Q}.
4. Is it true that the language accepted by a PDA by empty stack and final states

Computer Science Engineering Department 64 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

are different languages. [CO3-L2]


No, because the languages accepted by PDA‗s by final state are exactly the languages
accepted by PDA‘s by empty stack.

5. Define Deterministic PDA.[CO3-L1]


A PDA M =( Q, Σ ,Ґ ,δ ,q0 ,Z0 ,F ) is deterministic if:
1. For each q in Q and Z in Ґ, whenever δ(q, Є, Z) is nonempty then δ(q, a, Z) is
empty for all a in Σ.
2. For no q in Q, Z in Ґ, and a in Σ U {Є} does δ(q, a, Z) contains more than
one element. (E.g): The PDA accepting {wcw R | w in ( 0+1 ) * }.

6. Define Instantaneous description (ID) in PDA.


ID describe the configuration of a PDA at a given instant ID is a triple such as (q, w ,γ )
, where q is a state , w is a string of input symbols and γ is a string of stack symbols.
If M = ( Q, Σ ,Ґ ,δ ,q0 ,Z0 ,F ) is a PDA we say that
(q,aw,Zα) |---( p, w, βα) if δ(q,a,Z) contains (p, β ).
Machine ‗a‘ may be Є or an input symbol. Example: (q1, BG) is in δ(q1, 0 , ) ells that
(q1, 011, GGR )|-- ( q1, 11,BGGR).

7. What is the significance of PDA?[CO3-L1]


Finite Automata is used to model regular expression and cannot be used to represent
non regular languages. Thus to model a context free language, a Pushdown Automata
is used.

8. When is a string accepted by a PDA?[CO3-L2]


The input string is accepted by the PDA if:
The final state is reached.
The stack is empty.
9. Give examples of languages handled by PDA.[CO3-L2]
L = {anbn | n>=0 },here n is unbounded , hence counting cannot be done by finite
memory. So we require a PDA, a machine that can count without limit.
L = {wwR | wЄ{a,b}*}, to handle this language we need unlimited counting capability .

Computer Science Engineering Department 65 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

10. Is NPDA (Nondeterministic PDA) and DPDA (Deterministic PDA)


equivalent?[CO3-L2]
The languages accepted by NPDA and DPDA are not equivalent.
For example: wwR is accepted by NPDA and not by any DPDA.

11.State the equivalence of acceptance by final state and empty stack.[CO3-L1]


If L = L(M2) for some PDA M2 , then L = N(M1) for some PDA M1.
If L = N(M1) for some PDA M1 , then L = L(M2 ) for some PDA M2
Where L(M) = language accepted by PDA by reaching a final state. N(M)
= language accepted by PDA by empty stack.
12.Construct a PDA that accepts the language generated by the grammar[CO3-L1]
S aSbb / aab Solution: The PDA A = ({q}, {a,b}, {S,a,b}, δ, q,S}
Where:
i) (q, z0, S) = {(q, aSbb), (q, abb)}
ii) (q, a, a ) = {(q, ε)}
Q (q, b, b ) = {(q, ε)}

13.Construct a PDA that accepts the language generated by the grammar[CO3-L3]


S aABB , A aB / a , B bA / b Solution: The PDA A = ({q}, {a, b}, {S, A, B, Z, a, b},δ,
q,S} Where:
(q, z, S) = {(q, aABB)}
(q, z, A ) = {(q, aB, (q,a)}
(q, z, B ) = {(q, bA, (q,b)}
(q, a, a ) = {(q, ε)}
(q, b, b ) = {(q, ε)}

14. How do you convert CFG to a PDA.[CO3-L2]


Let G = (V, T, P, S) be a CFG. Then construct a PDA P that accepts L(G) by empty
stack as follows
P = ({q}, T, V U T, δ, q, S) Where δ
given by 1) For each variable A,
(q, ε, A) = {(q, β) | A β is a production

Computer Science Engineering Department 66 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

of P} 2) For each terminal a,


(q, a, a) = {(q, ε)}

15. Is it true that NDPA is more powerful than that od DPDA? Justify your
answer.[CO3-L2]
No, NPDA is not powerful than DPDA. Because NPDA may produce ambiguous
grammar by reaching its final state or by emptying its stack. But DPDA produces only
unambiguous grammar.

16. What is the additional feature PDA has when compared with NFA? Is PDA
superior over NFA in the sense L acceptance? Justify your answer.[CO3-L1]
PDA is superior NFA by having the following additional features.
• Stack which is used to store the necessary tape symbols and use the state to
remember the conditions.
• Two ways of L acceptances, one by reaching its final state and another by
emptying its stack.
17.What are the components of PDA?[CO3-L1]
The PDA usually consists of four components:
• A control unit.
• A Read Unit.
• An input tape.
• A Memory unit.

18. What is the informal definition of PDA? [CO3-L1]


A PDA is a computational machine to recognize a Context free language.
Computational power of PDA is between Finite automaton and Turing machines.
The PDA has a finite control, and the memory is organized as a stack.

19. What is meant by equivalence of PDA and CFG?[CO3-L1]


The context free grammar is recognized by the pushdown automata. The languages
defined by PDA‘s are the context free language. There are three classes of languages
that are equivalent to each other and they are,
1. The context free languages (i.e) the languages defined by CFG

Computer Science Engineering Department 67 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

2. The languages that are accepted by final state by some PDA


3. The language that are accepted by empty stack by some PDA.

20. Compare NFA and PDA. [CO3-L2]


NFA PDA

The language accepted by PDA is


The language accepted by NFA is the Context
regular language. free language.

NFA has no memory. PDA is essentially an NFA with a stack


(memory).

It can store only limited amount of It stores unbounded limit of information.


information.

A language/string is accepted only by It accepts a language either by empty


reaching the final state. Stack or by reaching a final state.

21.State the pumping lemma for CFLs.[CO3-L1]


Let L be any CFL. Then there is a constant n, depending only on L, such that if z is in L
and |z| > = n, then z = uvwxy such that :
(i) |vx| >=1
(ii) |vwx| <=n and
(iii) for all i>=0 uvi wx i y is in L.

22. What is the main application of pumping lemma in CFLs?[CO3-L1]


The pumping lemma can be used to prove a variety of languages are not
context free. Some examples are:
L1 = {a ibici | i>=1} is not a CFL.

Computer Science Engineering Department 68 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

L2 = {ai bj ci dj | i>=1 and j >=1 } is not a CFL.

PART-B
1. Design PDA to accept the language L={wcwR / w={0,1}*}[CO3-H3]
Solution:
The PDA P is defined as,
P = ( {q0,q1,q2}, {0,1,c}, Γ, δ, q0, z0, {q2})
The transition function is given as,
δ(q0, 0, z0) = (q0,0z0)
δ(q0, 0 ,0) = (q0,00)
δ(q0, 1 ,z0) = (q0,1z0)
δ(q0, 1 ,1) = (q0,11)
δ(q0, 0 ,1) = (q0,01)
δ(q0, 1 ,0) = (q0,10)
δ(q0, c ,z0) = (q1,z0)
δ(q0, c ,0) = (q1,0)
δ(q0, c ,1) = (q1,1)
δ(q1, 0 ,0) = (q1,ε)
δ(q1, 1 ,1) = (q1,ε)
δ(q1, ε ,z0) = (q2,ε)

Computer Science Engineering Department 69 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Let us take the string w= 011c110


(q0, 011c110,z0) ├ ρ (q0, 11c110, 0z0)
├ ρ (q0, 1c110,10z0)
├ ρ (q0, c110, 110z0)
├ ρ (q1, 110, 110z0)
├ ρ (q1, 10, 10z0)
├ ρ (q1, 0, 0z0)
├ ρ (q1, ε, z0)
├ ρ (q2, ε, ε) Accepted
The string 011c110 is accepted.

2. Design a PDA to accept the language L={ an+1bmcn / m, n ≥ 1}[CO3-H3]


Solution:
The PDA p is defined as,
P = ({q0,q1,q2,q3,q4}, {a,b,c}, Γ, δ, q0, z0, {q4})
The transition function is defined as,
δ(q0, a, z0) = (q0,z0)
δ(q1, a, z0) = (q1,az0)
δ(q1, a, a) = (qa, aa)
δ(q1, b, a) = (q2,a) δ(q2,
b, a) = (q2,a) δ(q2, c, a)
= (q3,ε) δ(q3, c, a) =
(q3,ε) δ(q3, ε, z0) =
(q4,ε)

Computer Science Engineering Department 70 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

The transition diagram is shown below,


a, a/aa a, a/aa a, a/aa
a,
a, z0/az0 a, z0/az0 z0/az0

q
q0 q1 q2 q3 4

a, z0/z0
b, a/a
c, a/ε ε, z0/ε

w = aabcc

(q0, aabcc,z0) ├ ρ (q1,abcc,z0)


T ρ (q1,bcc,az0)
U ρ (q2,cc,az0)
V ρ (q3,c,z0)
The string aabcc is not accepted since there is no transition δ(q3, c, z0).

3. Design PDA for the language L = { anb2n | n ≥0 }[CO3-H3-Nov/Dec 2008]


Solution
Here the idea to design this PDA is that when we read single ‗a‘ we insert or push two
a‘s on the stack. Then when we read ‗b‘, we each ‗a‘ on the top of the stack and when
reading z0 on the stack. We reach final state.

The transition function is, as follows;


δ(q0, a, z0) = (q0,aaz0)
δ (q0, a ,a) = (q0,aaa)
δ (q0, b ,a) = (q1,Є)
δ (q1, b ,a) = (q1,Є)
δ (q1, Є ,z0) = (q2, Є)
δ (q0, Є ,z0) = (q2, Є)

Computer Science Engineering Department 71 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Here after entering the final state, we can have any symbols on the stack since we
are designing PDA by reaching final state. The transition diagram for L= { an b2n | n
≥ 0 } is shown below,
Let us take w = aabbbb
(q0, aabbbb,z0) ├ ρ (q0,abbbb,aaz0)
├ ρ (q0,bbbb,aaaaz0)
├ ρ (q1,bbb,aaazo)

a, a/ aaa b, a / Є
a, z0 / aaz0

b, a / Є Є, a / Є
q
Start
q
0 2
q1

Є, z0/ Є

P= ( {q0, q1, q2}, {a, b}, G , d, q0, z0, {q2} )

4. Design a PDA to accept the language L = { a m bncm / n, m ≥ 1}


[CO3-H3]
The idea to design this PDA is that when we read ‗a‘ with ‗a‘ or ‗z0‘ as the top
stack symbol, push a on to the stack.
When reading b with a on the top stack symbol just change the state and don‘t
perform any operation.

Then while reading ‗a‘ with ‗a‘ on the top stack symbol pop the symbol.
The PDA p is defined as,

Computer Science Engineering Department 72 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

P= ({q0, q1, q2, q3}, {a, b, c}, Γ, δ , q0, z0, {q3})

The transition function is given as,

2. (q0, a, z0) = (q0,az0)


3. (q0, a ,a) = (q0,aa)
4. (q0, b ,a) = (q1,a)
5. (q1, b ,a) = (q1,a)
6. (q1, c ,a) = (q2,Є)
7. (q2, c ,a) = (q2,Є)
8. (q2, Є , z0) = (q3, Є)

The transition diagram is shown below,

W=aabbcc

(q0, aabbcc, z0) ├ ρ (q0,abbcc,az0)


1. ρ (q0,bbcc,aaz0)
2. ρ (q1,bcc,aaz0)
3. ρ (q1,cc,aaz0)
4. ρ (q2,c,az0)
5. ρ (q2, Є, z0)
6. ρ r (q3, Є, Є)

The string aabbcc is accepted

Computer Science Engineering Department 73 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

W=abcc

(q0, abcc, z0) ├ ρ (q0,bcc,az0)


├ ρ (q1,cc,az0)
├ ρ (q2,c,zo) Not Accepted

5. Construct a transition table for PDA which accepts the Language


L= { a3nbn| n ≥ 0 }[CO3-H3-Nov/Dec 2012]
Solution
The idea to design this PDA is that reading three a, single a is pushed on to the
stack. Then when we read ‗b‘ with ‗a‘ on the top stack symbol, pop the symbol ‗a‘
form the stack. When we read ‗Є‘ with z0 at the top stack symbol, then reached the
final state.

The transition function is defined as,


F (q0, Є, z0) = (q4, Є)
G (q0, a ,z0) = (q1,z0)
H (q1, a ,z0) = (q2,z0)
I (q1, a ,a) = (q2, a)
J (q2, a ,z0) = (q3, az0)
K (q2, a, a) = (q3, aa)
L (q3, b, a) = (q3, Є)
M (q3, a, a) = (q1, a)
δ(q3,Є,z0) = (q4, Є)

w = aaaaaabb

Computer Science Engineering Department 74 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

(q0, aaaaaabb, z0) ├ ρ (q1,aaaaaabb,z0)


├ ρ (q2,aaaabb,z0)
├ ρ (q3,aaabb,az0)
├ ρ (q1,aabb,az0)
├ ρ (q2,abb,az0)
├ ρ (q3, bb, aaz0)
├ ρ (q3, b, az0)
├ ρ (q3, Є, z0)
├ ρ (q4, Є, Є) Accepted

n
6. Construct the PDA accepting the language { (ab) | n ≥1 } by empty stack.
[CO3-H3-Nov/Dec 2012]
The transition function is defined as,
δ (q0, Є, z0) = (q3, Є)
δ (q0, a ,z0) = (q1,z0)
δ (q1, a ,z0) = (q2,az0)
δ (q1, a ,a) = (q2, aa)
δ (q2, b ,a) = (q2,Є)
δ (q2, a, a) = (q1, a)
δ (q2, Є ,z0) = (q3, Є)

w = abab

Computer Science Engineering Department 75 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

(q0, abab, z0) ├ ρ (q1,bab,az0)


├ ρ (q2,ab,az0)
├ ρ (q1,b,aaz0)
├ ρ (q2, Є, aaz0)
├ ρ (q2, Є, az0)
├ ρ (q2, Є, z0)
├ ρ (q2, Є, Є) Accepted

7.Convert the PDA P = ({q,P},{0,1},{X,Z0},δ,q,z0) to a CFG if δ is given by[CO3-H3]


T δ(q,1,z0) = {(q,Xz0)}
U δ(q,1,X) = {(q,XX)}
V δ(q,0,X) = { (P,X)}
W δ(q,ε,X) = {(q,ε)}
X δ(P,1,X) = {(P,ε)}
Y δ(P,0,z0) = {(q,z0)}
Solution:
For the above PDA,
States = {q,P}
State Symbols = {X, z0}
Bottom initial stack symbol = z0
Initial state = q
The CFG for the above PDA is G = (V,T,P,S)
Where, V Nonterminals

V=(S,[q,X,q], [q,X,P], [P,X,q], [P,X,P], [q,z0,q], [q,z0,P], [P,z0,q], [P,z0,P])


T={0,1}
S=S
The productions of the grammar by using the PDA are as follows,
Productions of CFG
P1: S [ q, z0, q]
P2: S [q, z0, P]
1. δ(q,1, z0) = {(q,X z0)}
P3: [q, z0,q] 1 [q,X,q]
[q,z0,q] P4 : [q, z0,q] 1
[q,X,P] [P,z0,q] P5 :

Computer Science Engineering Department 76 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

[q,z0,P] 1 [q,X,q] [q,z0,P]


P6 : [q,z0,P] 1 [q,X,P]
[P,z0,P]

2. δ(q,1,X) = {(q,XX)}
P7 : [q, X,q] 1 [q,X,q] [q,X,q]
P8 : [q, X,q] 1 [q,X,P] [P,X,q]
P9 : [q,X,P] 1 [q,X,q] [q,X,P]
P10 : [q,X,P] 1 [q,X,P] [P,X,P]

3.δ(q,0,X) = { (P,X)}
P11: [q, X,q] 0
[P,X,q] P12 :
[q,X,P] 0 [P,X,P]

3. δ(q,ε,X) = {(q,ε)}
P13 : [q,X,q] ε

4. δ(P,1,X) = {(P,ε)}
P14 : [P,X,P] 1

5. δ(P,0,Z0) = {(q,z0)}
P15 : [P, z0,q] 0 [q, z0,q]
P16 : [P, z0,P] 0 [q, z0,P]

8. Construct the PDA for the following grammar E E+E | E * E | a[CO3-H3]


Solution:
The PDA is given below:
For Non terminal ‗E‘:
δ(q,ε,E) = { (q, E+E), (q, E*E),
(q, a) } For terminal +,*,a
δ( q,+,+)={(q,ε)}
δ(q,*,*)={(q,ε)}
δ(q,a,a)={(q,ε)}

Computer Science Engineering Department 77 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Take any string w= a*a+a

(q,a*a+a,E) ├ ρ (q, ε a*a+a, E)


├ ρ (q, a*a+a, E*E)
├ ρ (q, a*a+a, a*E)
├ ρ (q, *a+a, *E)
├ ρ (q, a+a, E)
├ ρ (q, a+a, E+E)
├ ρ (q, a+a, a+E)
├ ρ (q, +a, +E)
├ ρ (q, a, E)
├ ρ (q, a, a)
├ ρ (q, ε, ε)
Thus the CFG accepts the string a*a+a and it‟s accepted by PDA by empty stack.
9.Construct a PDA for the given grammar and check for the validation of aa×a0
and (a0+a). [CO3-H3]
E I | E + E | E × E | (E)
I a | Ia | 0 | I0

Solution:

The PDA is given below:

For Non terminal „E‟:


δ(q,ε,E) = { (q,I), (q,E+E),
(q,E×E),(q,(E)) } δ(q,ε,I) = { (q,a),
(q,Ia), (q,0),(q,I0) }

For terminal +, ×,(,),0,a


(q, aa×a0,E) ├ ρ (q, ε aa×a0,E)
1. ρ (q, aa×a0, E × E)

Computer Science Engineering Department 78 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

2. ρ (q, aa×a0, Ia × E )
3. ρ (q, aa×a0, aa ×E)
4. ρ (q, a×a0,a × E)
5. ρ (q, ×a0, ×E)
6. ρ (q, a0, E)
7. ρ (q, a0, I0)
8. ρ (q, a0, a0)
9. ρ (q, 0, 0)
10. ρ (q, ε, ε)

w= aa×a0

(q, aa×a0,E) ├ ρ (q, ε aa×a0,E)


11. ρ (q, aa×a0, E × E)
12. ρ (q, aa×a0, Ia × E )
13. ρ (q, aa×a0, aa ×E)
14. ρ (q, a×a0,a × E)
15. ρ (q, ×a0, ×E)
16. ρ (q, a0, E)
17. ρ (q, a0, I0)
18. ρ (q, a0, a0)
19. ρ (q, 0, 0)
20. ρ (q, ε, ε)

Thus the CFG accepts the string aa×a0 and it‟s accepted by PDA by empty stack.
W= (a0+a)
(q, (a0+a),E) ├ ρ (q, ε (a0+a),E)
- ρ (q, (a0+a), (E))
- ρ (q, a0+a), E) )
- ρ (q, a0+a), E+E))
- ρ (q, a0+a), I0+E ))

Computer Science Engineering Department 79 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

- ρ (q, a0+a), a0+E) )


- ρ (q, 0+a), 0+E))
- ρ (q, +a), +E))
- ρ (q, a), E))
- ρ (q, a), a))
- ρ (q, ), ))
- ρ (q, ε, ε)
Thus the CFG accepts the string (a0+a) and it‟s accepted by PDA by empty stack.

10.Construct the CFG for L = { 0n10 n | n ≥ 0} and use it to construct PDA. [CO3-
H3]

Solution:

The CFG for L = { 0 n 1 0 n | n ≥ 0}

S A1 A
A 0A | ε

Non terminals = S,A


Terminals = 1,0, ε

The transition function for the PDA is,

δ( q, ε, S) = {
(q,A1A)} δ(q, ε, A) =
{(q,0A), (q,ε) } δ(q,
0, 0) = {(q,ε)}
δ(q, 1, 1) =
{(q,ε)} δ(q,
ε, ε) =
{(q,ε)}

Computer Science Engineering Department 80 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Let the string w = 0001


(q, 0001,S) ├ ρ (q, ε 0001,S)
├ ρ (q, 0001, A1A)
├ ρ (q, 0001, 0A1A)
├ ρ (q, 001, A1A)
├ ρ (q, 001, 0A1A)
├ ρ (q, 01, A1A)
├ ρ (q, 01, 0A1A)
├ ρ (q, 1, A1A)
├ ρ (q, 1, 1A)
├ ρ (q, ε, A)
├ ρ (q, ε, ε)

Thus the CFG accepts the string 0001 and it‟s accepted by PDA by empty stack.

11.Show that the language L = {anbncn /n ≥ 0} is not a context


free language. Solution: [CO3-H3]
a. Assume that the language ‗L‘ is a context free language
b. Let us take the string Z= anbncn
c. The length of the string, |Z| = n+n+n=3n
|Z|≥ n==>3n >n
So,the string Z can be break into five strings as Z=uvwxy
d. Break the string Z as
Z=uvwxy Let us make
the assumption that
u= an vwx = bn vx = bn-m y = cn
Now check
Z = uvwxy = anbncn

Computer Science Engineering Department 81 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

So our assumption is correct. The two conditions are,


i. |vwx| ≤ n
|bn|≤ n → n ≤ n
ii. |vx| ≥ 1 → |bn-m| ≠ ≥ 1 → n-m ≥ 1
(Or)
vx ≠ ε => bn-m ≠ ε
So the conditions are satisfied.
iii. Since the both the condition are true, for all i ≥ 0, the string
uviwxiy is also in „L‟.
Z = uviwxiy
= uvvi-1wxxi-1y
Z = uvwxy = anbncn
So our assumption is correct. The two conditions are,
i. |vwx| ≤ n
|bn|≤ n → n ≤ n
ii. |vx| ≥ 1 → |bn-m| ≠ ≥ 1 → n-m ≥ 1
(Or)
vx ≠ ε => bn-m ≠ ε
So the conditions are satisfied.

iii. Since the both the condition are true, for all i ≥ 0, the string uviwxiy is
also in „L‟.
Z = uviwxiy
a. uvvi-1wxxi-1y
b. anbn(bn-m)i-1cn
Z = anbn(bn-m)i-1cn

Computer Science Engineering Department 82 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Put i=0
Z = anbn(bn-
m)i-1cn Z =
anbn(bn-
m)0-1cn
Z = anbn(bm-n)cn
Z = an bm cn not in L

Put i=1
Z = anbn(bn-m)i-1cn
Z = anbn(bn-m) 1-1cn
n n n
Z = a b c is in L
Put i=2
Z = anbn(bn-
m)i-1cn Z =
anbn(bn-
m)2-1cn
Z = an b2n-mcn ≠ L

Since we lead to a contradiction that for the value of i=0,2 the string does not
belong to the language. So the language L = {anbncn /n ≥ 0} is not a context
free language.

12.Show that the language L = { anbncndn /n ≥ 0} is not a context free language.


[CO3-H3]
Solution:
a. Assume that the language ‗L‘ is a context free language
b. Let us take the string Z= anbncndn
c. The length of the string, |Z| = n+n+n+n = 4n
|Z| ≥ n ==> 4n > n
So, the string Z can be break into five strings as Z= uvwxy

Computer Science Engineering Department 83 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

d.Break the string Z as Z=


uvwxy Let us make the
assumption that

u = an vwx = bn vx = bn-m
y = cndn
Now check
Z = uvwxy = anbncndn
So our assumption is correct. The two conditions are,
i. |vwx| ≤ n
|bn|≤ n → n ≤ n

ii. |vx| ≥ 1 → |bn-m| ≥ 1 → n-m ≥ 1


(Or)
vx ≠ ε => bn-m ≠ ε

iii. Since the both the conditions are true,


for all i ≥ 0, the string uviwxiy is also in
„L‟.
Z = uviwxiy
= uvvi-1wxxi-1y
= anbn(bn-
m)i-1cndn Z =
anbn(bn-m)i-
1cndn
Put i=0
Z = anbn(bn-m)i-1cndn
G = anbn(bn-m)0-1cndn

Computer Science Engineering Department 84 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

H = anbn(bm-n)cndn
I = anbn(bm-
n
) cndn Z=
anbm cndn ≠
L

Put i=1
F = anbn(bn-m)i-1cndn
G = anbn(bn-m)1-1cndn
n n n n
H= a b c d = L
Put i=2
Z = anbn(bn-m)i-1cndn
Z = anbn(bn-m)2-1cndn
n 2n-m n n
Z=a b c d ≠L
Since we lead to a contradiction that for the value of i=0,2 the string does
not belong to the language. So the language L = {anbncndn /n ≥ 0} is not a context
free language.

14.Show that the language L = { 0n /n = i2 and n ≥ 0 } is not a context free


language. [CO3-H3]
Solution:
a. Assume that the language ‗L‘ is a context free language
b. Let us take the string Z = 0p where p=i2
c. The length of the string, |Z|= |0p| = p
|Z| ≥ i ==> i2≥i
So, the string Z can be break into five strings as
Z= uvwxy d. Break the string Z as Z= uvwxy
Let us make the
assumption that
u = 0q
vwx = 0r vx = 0s

Computer Science Engineering Department 85 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

y = 0p-(q+r)

Now check
Z = uvwxy = 0q0r0p-(q+r)0p

So our assumption is correct. The two


conditions are, i. |vwx| ≤ n
|0r|≤ p => r ≤ p

ii. |vx| ≥ 1 =>|0s| ≥ 1 → s ≥ 1


(Or)
vx ≠ ε => 0s ≠ ε

iii. Since the both the conditions are true,


for all i ≥ 0, the string uviwxiy is also in „L‟.
Z = uviwxiy
= uvvi-1wxxi-1y
= uvwx(vx) i-1y
= 0q0r (0s)i-10p-(q+r)

Z = 0p (0s)i-1

Put i=0
Z = 0p (0s)i-1

Z = 0p (0s)0-1

Z = 0p-s ≠L
Put i=1
Z = 0p (0s)1-1
Z =0p (0s)0
Z = 0p = L
Put i=2

Computer Science Engineering Department 86 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Z =0p (0s)2-1 Z = 0p (0s)1 Z = 0p


(0s) Z= 0P+S ≠ L
Since we lead to a contradiction that for the value of i=0,2 the string does
not belong to the language. So the language L = { 0p/ i ≥ 1 }is not a context free
language.

15.Prove that If L=N (P) for some PDA where P = (Q, Σ, Γ, δN, q0, z0), then there
is a PDA P such that L=L(P ) [CO3-H3-Nov/Dec 2003, 2004,2005,2006, 2007]
(Apr/May 2005)
Converting a Language Accepted by Empty Stack to Final State
Generally it is possible to show that the classes of language that are L(p) for some PDA
P is the same as the classes of languages that are N(p) for some PDA P. Here we first
construct a PDA P that accepts a language by empty stack and then by using PN we
construct a PDA p that accepts L by final state.
Theorem:
If L=N (PN) for some PDA where PN = (Q, Σ, Γ, δN, q0, z0), then there is a PDA P
F such that L=L(P )

To prove: If there exists a PDA P that accepts a language by empty stack, then there
exists
PDA pF that accepts a language by reaching final state.
Proof: To prove this theorem, we use a new symbol x 0 which must not be a
symbol in (i.e.,) (x0 є Γ*).
• Here we are going to simulate P from P that is we are going to construct P F.
• Here x0 is used as the starting top symbol of the stack
• And x0 is the symbol marked on the bottom of the stack for PN.
• PN goes on processing the input if it sees any other symbol on the
stack except the symbol x0.
• If PN sees x0, then it finishes processing the string.
• So now we are going to construct PF with a new starting state P 0 and new
final state PF.
• P0, which is the starting state of PF is used to push the z0x0 symbol on the
stack to make x0 at the bottom of the stack, when it reads x symbol and

Computer Science Engineering Department 87 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

enters the initial state q0 of PN.

(Fig) PF simulates PN and accepts if PN empties its stacks


The function of the new starting state P0 is to push the symbol z0 to the stack
when it sees x0 on the top of the stack and enters the state q0. The q0 state is the
initial state of PN. So PF simulates PN until PN empties its stack. PF detects that
the PN emptied it stack when PF sees x0 on the top of stack. So when the PDA PF
sees x0 on the top of the stack,it
reaches the new final state P of PDA PF. S the PDA PF
is given by, PF = (Q U {p0, pf}, Σ, Γ U {x0}, δF, p0x0,
{pf} )

Where δ is defined as,


1. δF (p0 ,e, x0) = (q0, z0, x0)=> PF pushes x0 to the bottom of the stack.
2. For all states q in Q, input a in Σ or a = є and any stack
symbol y in Γ, δF (q, a, y) contains all the pairs in d(q, a, y).
3. δF (q, є, x0)=(pf, є) accepts the string by reaching the final stat.

So from the above theorem, we can conclude that w is in L(PF) if and only if w is in
N(PN). So by combining all the moves, the instantaneous description of the p F after
simulating P is given
by,
(p0,w, x0)├ (p0, w, z0, x0)├ (q, є, x0)├ (pf, є, є)

Computer Science Engineering Department 88 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Thus the PDA PF accepts w by final state.

16.Converting a language accepted by reaching a final state empty stack[CO3-H3]


Here, we are going to take a PDA PF that accepts a language L by reaching final state
and use the same PDA to construct another PDA P N that accepts a same language L
by empty stack. Here we are going to simulate PN from PF by adding new start state and
accepting state.
Theorem
If L is a PDA L(p ) for some PDA PF = (Q, Σ, Γ, δN, q0, z0),, there exists a
PDA PN such that L=N(PN).
To Prove:
We have to prove that there exists a PDA PN if and only if there exists a PDA
PF .
Proof:
To prove this theorem, here also we use a stack symbol x0 on the bottom of the stack.
Here we are going to construct PN from PF. Here q0 is the initial state of PF and we have
a set of final state for PF. So the PDA PF enters the final state after accepting the string
‗w‘ with leaving any symbols of the stack. So now our target is to delete all the stack
symbol of PDA PF after entering the final state in order to construct P N by empty stack.
Now construct PN with P0 as the new start state and p as new final state. The transition
diagram of PN shown below,

(Fig) PN simulates p and empties its stacks if and only if the PDA PF enters the
accepting states
Here the initial state P0 of PN. Push the stack symbol z0 which is the start symbol of PF
on to the stack. So x0 is on the bottom of the stack and z0 is on the top which is the top
stack symbol for PF. P0 after pushing z0 on to the stack, it enters the state q0, initial state
of PF
Then PF after consuming its input w, it enters any one of the final state.
O For each accepting states of PF, add a transition to the final state P on e

Computer Science Engineering Department 89 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

with any symbol on stack and delete the stack symbol.


δ Thus whenever PF enters the final state after consuming w, P will empty its
stack after consuming the string ‗w‘.
So the PDA PN is defined as, PN = (Q U {p0, pf}, Σ, Γ U {x0}, δN, p0x0, {pf})

Where
P0 Initial state
X0 starting stack symbol
And the transition function δ is defined by
1. δN (p0, є, x0)={ (q0,z0, x0) } => push the start symbol z of PF on to the stack
and enters the state q , initial state of p.
2. For all states q in Q, input symbol a in Σ or a=є and Y in Γ δ N(q, a, y) contains
every pair of δN (q, a, y) since PN simulates PF.
3. For all accepting states of q in F and stack symbol Y in Γ or y=x0, δN (q, є, y)
contains (p, є)
4. For all stack symbol y in Γ or y=x δN (q, є, y) = { (p, є) }
So from above theorem, we can conclude that w is in N(P N) if and only if w is in
L(PF). So by combining all the moves, the instantaneous description of p after
simulating p is given by,

(p0, w, x0)├* (q0, w, z0x0)├* (q, є, αx0)├* (p, є, є)


So the PDA PN accepts w by emptying its stack.

Computer Science Engineering Department 90 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

UNIT IV

PUSHDOWN AUTOMATA
PART A

1.Define Turing Machines.[CO4-L1-May-14,Dec-03,may-06,07]


Definition of turing Machines(TM)
The turing machine is a collection of following components.
M=(Q,∑, Γ,δ,q0,∆ or B,F)
1. Q is a finite set of states.
2. Γ is finite set of external symbols.
3. ∑ is a finite set of input symbols.
4. ∆ or B or B ∈ Γ is a blank symbol majority used as end marker for input.
5. Δ is a transition or a 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.
For example: (q0 ,a )  (q1 , A, L)
This means that if currently we are reading the input symbol ‗a‘ and we are in q 0 state
then we can go to q1 state by replacing or printing ‗a‘ by A and now move ahead to left.
6. q0 be the initial state where q0 ∈ Q
7. F is a set of final states. The final state is such a state where turing machines
halts.
Thus turing machine is such machine which is used compute a large class of
language.

2.Define instantaneous description and move of a turing machine.[CO4-L1-Dec-


03]
The instantaneous description can be given as follows.

1. q is the state of the turing machine.


2. The tape head is scanning the ith symbol from left.
3. A1 A2 ….An is the input tape portion.
This portion is between leftmost and rightmost cell.

Computer Science Engineering Department 91 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Move of turing machine can be given as-

δ (q, Ai) = (q1,Y,L) that means move left then you will enter in state q 1 , with output Y on
tape A1 A2 ….Ai-1q Ai Ai+1….An - A1 ,A2 ….Ai-2 q1 Ai-1Y Ai+1….An.

3.Is it possible that a turing machine could be considered as a computer of


functions from integers to integers? If yes, justify your answer.[CO4-L3-dec-
04,may-05]
Yes, turing machines simulate computer of functions from intergers to integers. That
means it is the device for computing integer valued functions. In this scheme integers
were represented in unary as blocks of a single character and machine computed by
changing the lengths of blocks or by constructing new blocks on the input tape. Turing
machine can perform operations
addition , multiplication, subtraction, division of two untegers. Thus it acts as a
computing device.

4.Explain the basicturing machine model and explain in one move. What are the
actions take place in move in turing machine?[CO4-L2-Dec-04,May-05]
In one move there are three cases either the tape head is pointing any intermediate cell
or it is pointing at the first cell or it pointing at the last cell.

Suppose δ (q, Ai)= (q1,Y,L) then


A1 A2 ….Ai-1q Ai Ai+1….An - A1 ,A2 ….Ai-2 q1 Ai-1Y Ai+1….An
If i=1 then moving left to the blank.
A1 A2 ….Ai-1q Ai Ai+1….An - q1 by A2….An
If i=n andY=B then
A1 A2 ….An-1q An - A1 ,A2 ….An-2 q1 An-1 . Similarly the tape head can be moved to
right.

5. What are the two classes of problems that are solved by the Turing
machine?[CO4-L1]
The two classes of problems that can be solved by the Truing machines are as follows.
1. Those problems that have an algorithm
2. Those that are only solved by Turing machine.

Computer Science Engineering Department 92 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

6. What is the various representation of TM?[CO4-L1]


We can describe TM using:
 Instantaneous Description.
 Transition Table.
 Transition Diagram.

7. What do you mean by universal turing machine?[CO4-L1-Dec-05,13]


Universal turing machine is a type of TM which is capable of doing anything that any
other TM can do. That means universal TM is a TM that imitates any TMT.

8. What are the features of universal turing machine?[CO4-L1-May-07]


Following are the features of universal turing machine(UTM)
i) Universal turing machine is a kind of turing machine can simulate any other turing
machine. In other words,UTM is a single machine used to compute any
computable sequence.
ii) Another interesting feature of UTM is that it has an ability to manipulate an
unbounded amount of data in finite amount of time.

9.What are the applications of turing machine?[CO4-L1- Dec-12)


Turing is machine is used as a model of general purpose computer. Turing machine is
used for solving computational, recursive, non recursively enumerable problems. Turing
machines can also be used for representing subroutines.

10.List out techniques for turing machine construction.[CO4-L1-Dec -13]


1. Storage in finite control
2. Multiple tracks
3. Checking off symbols
4. Subroutine.
Various techniques machine construction.

11. What are the possibilities of a TM when processing an input string?[CO4-L1]


TM can accept the string by entering accepting state.
 It can reject the string by entering non-accepting state.

Computer Science Engineering Department 93 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

 It can enter an infinite loop so that it never halts.


12. What are the techniques for Turing machine construction?[CO4-L1]
 Storage in finite control.
 Multiple tracks.
 Checking off symbols.
 Shifting over
 Subroutines.

13. What is the storage in FC?[CO4-L1]


The Finite control (FC) stores a limited amount of information. The state of the Finite
control represents the state and the second element represents a symbol scanned.

14. Differentiate multitape and multitrack machines.[CO4-L2-Dec-08]


1. Multiple tracks
If the input tape is divided into multiple tracks then the intput tape will be as
follows
For example :

# 1 1 1 1 1 $
B B B B 1 1 B ……
B 1 1 1 B B B

Finite
control
]

As shown in fig the input tape has multiA m

1. Multi-tape turing machine

 It can enter an infinite loop so that it never halts.

Computer Science Engineering Department 94 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

The multitape turing machine is atype of turing machine in which there are more than
one input tapes. Each tape is divided into cells and each can hold any symbol of finite
tape alphabet. The multitape turing machine is as shown in figure.

This TM is more powerful than the basic turing machine. Because finite control reads
more than one input tape and more symbols can be scanned at a time.

15. When is checking off symbols used in TM?[CO4-L2]


Checking off symbols is useful method when a TM recognizes a language with repeated
strings and also to compare the length of substrings.

16. When is shifting over Used?[CO4-L2]


A Turing machine can make space on its tape by shifting all nonblank symbols a finite
number of cells to the right. The tape head moves to the right, repeatedly storing the
symbols in the FC and replacing the symbols read from the cells to the left. The TM can
then return to the vacated cells and prints symbols.

Computer Science Engineering Department 95 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

17. What is a multi-head TM?[CO4-L1]


A k-head TM has some k heads. The heads are numbered 1 through k, and move of the
TM depends on the state and on the symbol scanned by each head. In one move, the
heads may each move independently left or right or remain stationary.

18. What is a 2-way infinite tape TM?[CO4-L1]


In 2-way infinite tape TM, the tape is infinite in both directions. The leftmost square is
not distinguished. Any computation that can be done by 2-way infinite tape can also be
done by standard TM.

19. How can a TM used as a transducer?[CO4-L2]


A TM can be used as a transducer. The most obvious way to do this is to treat the entire
nonblank portion of the initial tape as input, and to treat the entire blank portion of the
tape when the machine halts as output.

20. What is a multi-tape Turing machine?[CO4-L1-Dec-03,14,May-05,06,08]


A multi-tape Turing machine consists of a finite control with k-tape heads and k tapes;
each tape is infinite in both directions. On a single move depending on the state of finite
control and symbol scanned by each of tape heads, the machine can change state print
a new symbol on each cells scanned by tape head, move each of its tape head
independently one cell to the left or right or remain stationary.

21.State the halting problem of TMs.[CO4-L1]


The halting problem for TMs is: Given any TM M and an input string w, does M halt on
w? This problem is undecidable as there is no algorithm to solve this problem

22.Construct a turing machine to compute „n mod 2‟ where n is represented in the


tape in unary form consisting of only 0‟s.[CO4-L2-may – 11]
The unary number is represented by 0‘s. The turing machine for the language { L= n
mod 2} is as follows.
(0,0,R)
Start
q q

(0,0,R
(Δ,

Halt

Computer Science Engineering Department 96 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

23. Design a turing machine with not more than states that accepts languages a
(a+b)* . Assume ∑={a,b}[CO4-H3-may-05].
Let r.e = a (a+b)*.The corresponding turing machine will be

(a,a,R)
(b,b,R)

(Δ, Δ,R)
Start (a,a,R)
qo q1 Halt

24. Design a TM that accepts the language of odd integers written in binary.[CO4-
H3]
This is a simple TM in which the logic is used as follows- the binary string that ends with
1 is always an odd integer. Hence the TM will be
(1,1R (0,0,R)

Start (0,0,R
q q

(Δ, (1,1,R)

Halt

25. Design a TM for finding 1‟s complement of a given binary number. [CO4-H3]
Dec-11]

(1,0,R)
(0,1,R)

(Δ, Δ,R)
Start (1,0,R)
qo q1 Halt

Computer Science Engineering Department 97 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

26.. Define Power of turing Machine[CO4-L1]


As we have discussed with the help many interesting examples that turing machine has
a great computational capabilities. And hence it can be used as a general mathematical
model for modern computers.

Turing machine can model even recursive enumberable languages. Thus the advantage
of turing machine is that it can model all the computable functions as well as the
languages for which the algorithm is possible.

27. What are the Comparison of FM, PDA and TM? [CO4-L1]
Basically have discussed three models Viz. finite automata or Finite Machines (FM),
Pushdown Automata (PDA) and turing Machine(TM). We will now discuss the
comparison between these models ,

1. This finite machine is of two types – deterministic finite state machine and non
deterministic finite state machine. Both of these DFA and NFA accept regular
language only. Hence both the machines have equal power i.e. DFA = NFA.

We have then learnt push down automata again, pushdown automata consists of two
types of models deterministic PDA and non deterministic PDA. The advantage of PDA
over FA is that PDA has a memory and hence PDA has a memory and hence PDA
accepts large class of languages than FA hence PDA has more

Computer Science Engineering Department 98 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

PART B

1. Explain about Basic Model of turing machine. [CO4-H3]


The turing machine can be modeled with the help of following representation.
1. The input tape having infinite number of cells, each cell containing one input
symbol, and thus the input string can be placed on a tape. The empty tape is
filled by blank characters.
2.

….. a b c ∆ ∆ ∆ ∆ ∆ ∆ …..

Input tape
Fig input tape

3. 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.
4. A finite set of states through which machine has to undergo.
5.

….. ∆ ∆ a b A A B b a ∆ ∆ ∆ …..

Finite
control

Turing machine

6. Finite set of symbols called external symbols which are used in building the logic
of turing machine.

Computer Science Engineering Department 99 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

2.State the techniques for turing machine construction. Illustrate with a simple
language. [CO4-H3-Dec-11)
Write briefly about the programming techniques for TM. (Dec-12,May-13) (or)

Write short note on multiple tracks TM. (Dec-13)

Techniques for turing machine construction


Construction of turing machine is a process of writing out the complete set of
states and next move function. This is a totally conceptual phenomenon. The turing
mschine can be designed with the help of some conceptual tools. Let us discuss
some of these tools.
1. Storage in finite control
As shown in above figure the model of turing machine has a finite control. This finite
control can be used to hold some amount of information. This finite automata stores
the by the tape head. This is just a conceptual arrangement.
For example – The function δ can be written as follows.
δ([q0,0])([ q1,1], ∆ ,R)
This means that if finite control shows the initial state is q0 and stores the current
symbol 0 if it reads the symbol 1 then the machine goes to next state q 1 replace that
1 by ∆ and moves to right. This helps in building the transition graph of the language.
Example: Construct a turing machne M for ∑ = { a, b} which will convert lower case
letters to upper case.
Solution:
The turing machine is designed such that we will get the equivalent upper case
string to the input. The q0 is a initial state and q3 is final state. The δ function is
wriiten along each edge. For the sake of understanding consider the transaction
between q0 and q1 , if the finite control reads on the tape A will be printed there and

Computer Science Engineering Department 100 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

the move will be in right direction. After converting the given string to upper case we
reach to the final state q3 is a halt state.
2. Multiple tracks
If the input tape is divided into multiple tracks then the intput tape will be as
follows
For example :

# 1 1 1 1 1 $
B B B B 1 1 B ……
B 1 1 1 B B B

Finite
control
As shown in fig the input tape has multiple tracks on the first track.
The input which is placed is surrounded by # and $. The unary number
equivalent to 5 is placed on the input tape, on the first track. On the second track
unary 2 is placed. If we construct a TM which subtracts 2 and 5 we get the
answer on the third track and that is 3, in unary form. Thus this TM is for
subtracting two unary numbers with the help of multiple tracks.
3. Checking Off Symbols
Checking off symbols is an effective way of recognizing the language by TM. The
symbols are to be placed on the input tape. The symbol which is read is marked
by any special character.
The tape head can be moved to the right or left. Let us take some example and
we will see how to build turing machine by checking of symbols.
Example: Construct a turing machine M=(Q, ∑, Γ,δ,q0,B,F) which recognizes the
language L={wcw\w ∈ (a+b)+ }
Solution: In this language the input set is ∑ ={a,b}. The string which when will be
placed on the input tape it will have two distinct parts separated by letter c, such
as
In the checking off symbols, each symbol is marked special character. The
simple logic in construction of this TM will be we mark the first letter and then

Computer Science Engineering Department 101 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

move to the right till we not get c, the first letter after c will be compared with the
marked letter. If it is same as which we have marked then mark this symbol
otherwise goto reject state. It can be shown as below,

* * a c * b a ∆ ∆ …

Computer Science Engineering Department 102 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Now machine goes to accept state. Thus in this TM we are scanning each symbol and
trying to recognize the string.

4.Subroutine

In this high level languages use of subroutines built the modularity in the program
development process. The same type of concept can be introduced in
construction of TM. We can subroutines as a turing machine. Let us see how it
works with the help of some example.

3.Explain briefly about Two way Turing Machine[CO4-L2-May-04,05,Dec-


05,08,09,12,13]
The turing machine is widely accepted as a model of computational because of
its versatility in the power of computation. You have already experienced in the
previous section, how powerful this model is the TM can perform striing
operations, arithmetic computations, recognizing languages either regular or non
regular languages. In this section we are going to see more power of TM by
allowing the two way infinite tape as shown in fig.

∆ 0 1 1 0 1 1 ∆ …

Two Way infinite tape

The turing machine with infinite tape can also be denoted by .

M=(Q,∑, Γ,δ,q0,∆ or B,F)

Q is a finite, nonempty set of states.

Γ is finite set of external symbols.


∑ is a nonempty finite set of input symbols.
B is a blank symbol.In the two way infinite tape it is placed at both the ends of the
tape. In between these blank symbols the input sting has to be placed.
F denotes the final state. The TM halts after reaching in final state.

Computer Science Engineering Department 103 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Theorem: L is recognized by a TM with two way infinite tape if and only if it is


recognized by a TM with one way infinite tape.

Proof:

As theorem states, if any language is recognized by a TM with one way infinite tape
then it should also be recognized by a TM with two way infinite tape.
Let M1 be a TM with one way infinite tape and can be denoted by
M1=(Q1,∑1, Γ1,δ1,q1, B1,F1)
Similarly,M2 be a TM with two way infinite tape
M2=(Q2,∑2, Γ2,δ2,q2, B2,F2)
The input tapes are as shown by fig.
…. ∆ a5 a4 a3 a2 a1 a0 ∆ …
In above fig. the TM with one way infinite tape has a external symbol # placed in the
very first cell from left side. This symbol is used as indicator for the left side termination.
If we want a language l={ q0, q1, q2, q3, q4, q5} sequence then in the two way infinite tape
the tape head is fixed at the rightmost symbol.
The TM M2 can be

(a2,a2,L
(a0,a0,L) (a1,a1,L
Start q )
q q ) q

(a3,a3,L)

Halt q q q
(∆,∆,S (a5,a5,L) (a4,a4,
δ(q6, ∆) (HALT, ∆,S) leads to HALT state. L)

Computer Science Engineering Department 104 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

…. ∆ a5 a4 a3 a2 a1 a0 ∆ …

Similarly , with one way infinite tape the machine M1 will be

Even we can make the TM with one way infinite tape as a multitrack tape to
simulate it as a two way infinite tape. Let us now solve some interesting problems
to see the working of two way infinite tape in the turing machine.

(a2,a2,L
(a0,a0,L) (a1,a1,L
Start q )
q q ) q

(a3,a3,L)

Halt q q q
#,#,S) (a5,a5,L) (a4,a4,
L)

5.Explain about Types of Turing machine.[CO4-L2]


Multiple head turing machine
 A multihead TM is a single tape TM having n heads reading symbols on
the same tape. In one step all the heads sense the scanned symbols and
move or write independently.
 The heads are numbered 1 through n and move of TM depends upon the
state and the symbol scanned by each head.

Computer Science Engineering Department 105 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

 In one move the heads may move left, right or remain stationary.
 This type of turing machine is as powerful as one tape turing machine.
 The multi head turing machine is as shown in the following figure

input Accept/reject
Finite
Control

Head 1 Head n
….. …..

1.
2. Multi-tape turing machine
The multitape turing machine is atype of turing machine in which there are more
than one input tapes. Each tape is divided into cells and each can hold any
symbol of finite tape alphabet. The multitape turing machine is as shown in
figure.This TM is more powerful than the basic turing machine. Because finite
control reads more than one input tape and more symbols can be scanned at a
time.
Finite
Control

Input tape 1

….. …..

Input tape 2

….. …..

Computer Science Engineering Department 106 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

6.Show that there exists a TM for which the halting problem is unsolvable.(May-
08,Dec-10)
Prove that the halting problem is undecidable.[CO4-L3-Dec-12]
Halting Problem
To state halting problem we will consider the given configuration of a turing machine
.The output of TM can be
i) Halt: The machine starting at this configuration will halt after a finite number of
states.
ii) No Halt : The machine starting at this configuration never reaches a halt
state, no matter how long it runs.

Now the question arises based on these two observation : Given any functional matrix,
input data tape and initial configuration , then is it possible to determine whether the
process will ever halt? This is called halting problem. That means we are asking for a
procedure which enable us to solve the halting problem for every pair (machine,
tape).The answer is ―no‖. That is the halting problem is unsolvable. Now we will prove
why It is unsolvable. Let, there exists a TM M1 which decides whether or not any
computation by a TM T will ever halt when a description dT of T and tape t of T is given
[That means the input to machine M1 will be (machine, tape) pair]. Then for every input (
t, dT) to M1 if T halt for input t, M1 also halts which is called accept halt. Similarly if T
does not halt for input t then the M1 will halt which is called reject halt.

This is shown in figure below


When T halts for
M1 t
( t, dT)
Accept

Accept
input
When Tdoes not
halts for t

Computer Science Engineering Department 107 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Now we will consider another turing machine M2 which takes an input dT . It first copies
dT and duplicates dT on its tape and then this duplicate tape information is given as
input to machine M1 . But machine M1 is a modified machine with the modification that
whenever M1 is supposed to reach an accept halt, M2 loops forever. Hence behavior of
M2 is as given. It loops if T halts for input t = dT and halts if T does not halt for T = dT .
The T is any orbitary turing machine.
M2 When T halts for
t
( t, dT) Accept
Copy (dT, dT)
T Accept
input When Tdoes not
halts for t

M1

As M2 itself is one turing machine we will take M2 = T . That means we will replace T by
M2 from above given machine.
M2 halts for input dM2
M2
( t, dM2)
Loop

halt
input
M2 does not halts for
input dM2

Thus machine M2 halts for input dM2 if M2 does not halt for input dM2 .This is a
contradiction .That means a machine M1 which can tell whether any other TM will halt
on particular input does not exist. Hence halting problem is unsolvable.

Computer Science Engineering Department 108 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

7.Explain briefly about Chomsky Hierarchy of Languages[CO4-L2]


Chomsky Hierarchy of Languages
The chomsky‘s hierarchy represents the class of languages that are accepted by
different machine. The category in Chomsky is as given below
Language class Language Grammar Machine One example

Type3 Regular Regular FSM i.e. NFA a*b*


grammar or DFA

Type2 Context free Context free PDA anbn


grammar

Type1 Decidable Context Linear bounded anbncn


languages sensitive automata
grammar

Type0 Computable Unrestricted Turing Machine n!


languages grammar

This is a hierarchy therefore every language of type 3 is also of type 2,1 and 0. Similarly
every language of type 2 is also of type a and 0 etc.

Computable languages

Context sensitive languages

Context free languages

Regular language

Chomsky hierarchy

Computer Science Engineering Department 109 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Type 3 – Regular languages

Regular languages are those languages which can be described using regular
expressions. These languages can be modeled by NFA or DFA.

Type 2- Context free languages

The context free languages are the languages which can be represented by context free
grammar (CFG). The production rule is of the form

Aα

Where A is any single non-terminal and α is any combination of terminals and non-
terminals.

A NFA or DFA cannot recognize strings of this language because these automata are
not having ―stack‖ to memorize. Instead of it the Push Down Automata can be used to
represent these languages.

Type 1- Context sensitive languages

The context sensitive grammars are used to represent context languages. The context
sensitive grammar is follows the following rules-

1. The context sensitive grammar may have more than one symbol on the left hand
side of their production rules.
2. The number of symbols on the left hand side must not exceed the number of
symbols on the right hand side.
3. The rule of the form Aє is not allowed unless A is a start symbol. It does not
occur on the right hand side of any rule.
The automation which recognizes context sensitive languages is called linear
bounded automaton. While deriving using context sensitive grammar the
sentential form must always increase in length every time a production rule is
applied. Thus the size of a sentential form is bounded by a length of the sentence
we are deriving.

Computer Science Engineering Department 110 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Type 0 – Unrestricted languages

There is no restriction on the grammar rules of these type of languages. These


languages can be effectively modeled by turing machines.

8.Explain how a turing machine can be viewed as a computing device on


functions involving integers. [CO4-L2-Dec-13)
The TM accepts the computable functions such as addition, multiplication, subtraction,
division, power function, square function, logarithmic function and many more.
For example: Construct a TM for a successor function for a unary number i.e. f(n) =
n+1.

For construction of such TM ∑ ={ 1} . We will go on moving towards right end of the


input tape. The input on the input tape is kept as it is. Only the last symbol Δ is replaced
by 1. Considered the input tape consists of 4. The successor function will give output 5.

1 1 1 1 Δ Δ Δ Δ

(1,1,R)

(Δ, Δ,R)

Start (1,1,R) (Δ,1,R)


q2 Halt
q1
qo

Computer Science Engineering Department 111 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

9.Construct TM for the language L = {an bn } where n≥1[CO4-H3-May – 09]

(B,B,L
(a,a,R)
(B,B,L
(a,a,R)
Start (a,A,R) (b,B,L) q2
qo q1

(B,B,R) (A,A,R)

q3 (B,B,R)

(Δ, Δ,L)

Halt

10.Construct a TM for L = {an bn cn / n≥0 }.[CO4-H3- May – 08,May – 11]

(B,B,R) (C,C,R)
(a,a,R) (b,b,R)
(b,b,L)
(a,a,L)
(c,C,L)
Start (a,A,R) (b,B,R) q2 q3 (B,B,L)
qo q1
(C,C,L)

(B,B,R)
(A,A,R)
(Δ, Δ,R) (B,B,R)
q4
(C,C,R)

(Δ, Δ,R)

Halt

Computer Science Engineering Department 112 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

11.Construct a TM for L = {1n 0n 1n / n≥0 }.[CO4-H3- May – 12]

(B,B,R) (C,C,R)
(0,0,R) (1,1,R)
(1,1,L)
(0,0,L)
(0,C,L)
Start (0,A,R) (1,B,R) q2 q3 (B,B,L)
qo q1
(C,C,L)

(B,B,R)
(A,A,R)
(Δ, Δ,R) (B,B,R)
q4
(C,C,R)

(Δ, Δ,R)

Halt

12.Construct TM for performing subtraction of two unary numbers f(a-b) = c


where a is always greater than b. [CO4-H3- Dec -03,Dec – 05,May -11]

(Δ, Δ,S)
Halt
(*,*,R)
(1,1,R) (*,*,L)
(*,*,L)
(1,*,L) (-,-,L)
Start (1,1,R) (-,-,R) q2 q3 q4
qo q1

(1,*,R)

Computer Science Engineering Department 113 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

13.Construct TM for the addition function for the unary number system. [CO4-H3-
May -07]

(1,1,R) (1,1,R)
(Δ, Δ,L)
Start (1,1,R) (+,1,R) q2 q3
qo q1
(1, Δ,R)

q4

(Δ, Δ,R)

Halt

14.Construct a TM for a successor function for a unary number i.e. f(n) = n+1.
[CO4-H3]

(1,1,R)

(Δ, Δ,R)
Start (1,1,R) (Δ,1,R) q2
qo q1 Halt

Computer Science Engineering Department 114 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

15.Design a TM which reverses the given string {abb}.[CO4-H3-Dec – 12]


(b,b,R)
(a,a,R) (b,b,L)
(b,b,R) (B,B,R)
(Δ, b,L)
(Δ, Δ,R)
Start (Δ, Δ,L) (b,B,R) q2 q3 q4
qo q1

(Δ, Δ,L)

(b,B,R (B,B,L)
q5

(a, A,R)

(Δ, a,R) (Δ, Δ,R)


q7 q6 (B,B,R)
Halt

(b,b,R)
16.Design a TM to accept the language L = {0n 1n / n≥1 } and simulate its action on
the input 0011. [CO4-H3- May – 14]

(B,B,L)
(0,0,R)
(B,B,L)
(0,0,R)
Start (0,A,R) (1,B,L) q2
qo q1

(B,B,R) (A,A,R)

q3
(B,B,R)

(Δ, Δ,L)

Halt

Computer Science Engineering Department 115 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

17.Design a TM for reversing a string on the input tape.(TB) [CO4-H3]

18.Design a TM to accept the language L = { ss | s Є { a, b} *}.(TB)


(or)
Construct a TM for a language having equal number of a‟s and b‟s[CO4-H3]
A TM Accepting { ss | s Є { a, b} *}

(q0, Δaba) ├ (q1, Δaba) ├ (q2, ΔAba) ├* (q2, ΔAbaΔ)

├ (q3, ΔAba) ├ (q4, ΔAbA) ├ (q4, ΔAbA)

├ (q1, ΔAba) ├ (q2, ΔABA) ├ (q3, ΔABA)

├ (hr, ΔABA) (reject)

Computer Science Engineering Department 116 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

(q0, Δabaa) ├ (q1, Δabaa) ├ (q2, ΔAbaa) ├* (q2, ΔAbaaΔ)

├ (q3, ΔAbaa) ├ (q4, ΔAbaA) ├ *(q4, ΔAbaA)

├ (q1, ΔAbaA) ├ (q2, ΔABaA) ├ (q2, ΔABaA)

├ (q3, ΔABaA) ├ (q4, ΔABAA) ├ (q1, ΔABAA)

├ (q5, ΔABAA) ├ (q5, ΔAbAA) ├ (q5, ΔabAA)

(first phase completed)

├ (q6, ΔabAA) ├ (q8, ΔAbAA) ├ (q8, ΔAbAA)

├ (q9, ΔAbΔA) ├ (q9, ΔAbΔA) ├ (q6, ΔAbΔA)

├ (q7, ΔABΔA) ├ (q7, ΔABΔA) ├ (hr, ΔABΔAΔ) (reject)

(q0, Δabab) ├* ………………

(same as previous case, upto 3rd-from-last move)

├ (q6, ΔAbΔA) ├ (q7, ΔABΔB) ├ (q7, ΔABΔB)

├ (q9, ΔABΔ) ├ (q9, ΔAB) ├ (q6, ΔABΔ)

├ (hr, ΔABΔ) (accept)

Computer Science Engineering Department 117 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Figure

A Turing machine to accept { ss | s Є {a, b}*}

Computer Science Engineering Department 118 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

19.Design a TM to implement the function multiplication using the subroutine


copy a string. [CO4-H3-Dec-14)

Computer Science Engineering Department 119 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

20.Construct a TM for checking the palindromes of a string of Odd or even


palindromes for Σ = {a,b}.(TB) [CO4-H3]
A TM Accepting Palindromes over {a, b}

(q0, Δaa) ├ (q1, Δaa) ├ (q2, ΔΔa) ├* (q2, ΔΔaΔ)

├ (q3, ΔΔa) ├ (q4, ΔΔ) ├ (q1, ΔΔΔ)

├ (ha, ΔΔΔΔ) (accept)

a/a, R

b/b, R Δ/Δ, R
Δ /Δ, L (
q2 q3

a/Δ, L
a/Δ, R
Δ/Δ, R Δ/Δ, R a/a, L
q4 ha
q0 q1 b/b, L
b/Δ, R

Δ/Δ, L
q5 q6 Δ/Δ, R

UNIT V
a/a, R
UNSOLVABLE PROBLEMS AND COMPUTABLE FUNCTIONS
b/b, R
PART A
Δ /Δ, R

Computer Science Engineering Department 120 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

1. When a problem is said to be decidable and give an example of


undecidable problem.[CO5-L1-Dec-03,12,may-06,05]
The class of problems which can be answered as ―Yes‖ are called solvable or
decidable. And the class of problems which can be answered as ―No‖ are called
undecidable problems.
Following are undecidable problems.
i) Whether the language accepted by a TM is empty.
ii) Whether the language accepted by a TM is regular language.
iii) Whether the language accepted by a TM is context free language.

2. Show that the complement of a recursive language is recursive.[CO5-L1-


Dec-04,may-05]
Theorem (9.4): If both a language L and its complement are RE, then L is
recursive.
Proof: Let L=L(M1) and L‘= L(M2). Both M1 and M2 are simulated in parallel by a
TM M. We can make M a two-tape TM, and then convert it to a one-tape TM, to
make the simulation easy and obvious. One tape of M simulates the tape of M 1,
while the other tape of Al simulates the tape of M2. The states of M1 and M2 are
each components of the state of M.

Figure (9.4) Simulation of two TM's accepting a language and its


complement
If input w to M is in L, then Mi will eventually accept. If so, M accepts and halts. If
w is not in L, then it is in L‘, so M2 will eventually accept. When M2 accepts, M
halts without accepting. Thus, on all inputs, M halts, and L(M) is exactly L. Since
M always halts, and L(M) = L, we conclude that L is recursive.

Computer Science Engineering Department 121 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

3.Give two properties of Recursively Enumerable Sets which are


undecidable.[CO5-L2]
The languages accepted by Turing machines are called recursively enumerable (RE)
language. Every non-trivial property of RE is undecidables. Here are two such
properties.

i) Whether the language accepted by a TM is regular language.


ii) Whether the language accepted by a TM is context free language.
These are the two properties that are undecidable.

4.When a language is said to be recursive? Is it true that every regular set is not
recursive?[CO5-L2-Dec -05]
A language is said to be recursive if there exists a turing machine that accepts the
strings belonging to that language and rejects on every string that are not belonging to
that language. Every regular set is not recursive.

5.Differentiate between recursive and recursively enumerable languages.(May-07)


Recursive[CO5-L2]
 A language L is said to be recursive if there exists a Turing machine M that
accepts L, and goes to halt state or else M rejects L.
W
Accept
TM
Reject
Input
 The recursive
string languages are called turing decidable languages
 Recursive language will halt on every input.
 Every recursive language is also a recursively enumerable.
 If L is recursive language then its complement L‘ is also recursive.

Recursively Enumerable
A language is recursively enumerable if there exists a Turing Machine that
accepts every string of the language and does not accept strings that are not in
the language.

W
Accept
TM
Loops for ever
Input string

Computer Science Engineering Department 122 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

The RE are called turing acceptable languages.

RE may not halt on every input, it may fall into an infinite loop.

Every RE language is not recursive.

There exist RE language L whose complement L‘ may not be RE. If L and L‘ both
are recursively enumberable then that L is definitely a recursive language.

6. When do you say a problem is NP-hard?[CO5-L1-Dec-09]


A problem is said to be NP-hard is an algorithm for solving it can be translated into a
problem which is a NP problem. Thus NP-hard is a algorithm for a problem which is at
least as hard as any NP-problem.
For example – Sum of subset problem, travelling salesperson problem.

7. Sate two languages, which are not recursively enumerable.[CO5-L1-Dec-09]


Diagonalization language is not recursively enumerable.
The partially decidable languages or undecidable languages are not recursively
enumerable.
For instance: for given input whether the TM will eventually halt is or not? This is
undecidable and hence is not recursively enumerable.
8. Mention the difference between P and NP problems. [CO5-L2-May-12]
P class problems are those problems which can be solved in polynomial time. For
example – The sorting and searching problems are P class problems. NP class
problems are those problems which can be solved in non-deterministic polynomial time.
For example – Traveling salesperson problems, graph coloring problem. The NP class
problems can be NP-Complete and NP – hard problems.

Computer Science Engineering Department 123 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

9.What is recursively enumerable? [CO5-L1-may-12,Dec-13]


A language is recursively enumerable if there exists a Turing Machine that accepts
every string of the language and does not accept strings that are not in the language.

W
Accept
TM
Loops for ever
Input string

10.What are a)recursively enumerable b) Recursive sets? [CO5-L1-Dec-13]


Recursively Enumerable
A language is recursively enumerable if there exists a Turing Machine that accepts
every string of the language and does not accept strings that are not in the language.

W
Accept
TM
Loops for ever
Input string
Recursive sets

A language is said to be recursive if there exists a turing machine that accepts the
strings belonging to that language and rejects on every string that are not belonging to
that language. Every regular set is not recursive.

11.Define the class NP problem.[CO5-L1-Dec-13]


The NP stands fo non-deterministic polynomial time. That means all the problems in this
class are solved in non deterministic polynomial time. The problems belonging ti this
class are - Traveling salesperson problems, graph coloring problem and Knapsack
problem.

12.What do you mean by universal turing machine?[CO5-L1-Dec-05,13]


Universal turing machine is a type of TM which is capable of doing anything that any
other TM can do. That means universal TM is a TM that imitates any TMT.

13.Define the classes P and NP problems. [CO5-L1-May-14]

Computer Science Engineering Department 124 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

P class problems - problems that can be solved in ―polynomial time‖are called P class
problems. For example – The sorting and searching problems.
NP class problems - problems that can be solved in non-deterministic polynomial time.
For example – Traveling salesperson problems, graph coloring problem. The NP class
problems can be NP-Complete and NP – hard problems.

14.Give examples for NP-complete Problems.[CO5-L2]


The NP- complete problems are – Traveling salesperson problems, graph coloring
problem.0/1 knapsack problem, Hamiltonian circuit problem .

15.When we say a problem is decidable (Or) Define Decidable Problem?[CO5-L2]


A problem whose language is recursive is said to be decidable. Otherwise the problem
is said to be undecidable. Decidable problems have an algorithm that takes as input an
instance of the problem and determines whether the answer to that instance is ―yes‖ or
―no‖.

16. Define Undecidable language? [CO5-L1]


The problem is Undecidable if there is no algorithm and we cannot predict whether the
input is accepted by Turing machine or not. Undecidable problem exist if and only if
there exists no algorithm to solve the problem in finite time.

17. Define (a) Recursively Enumerable languages (b) Recursive Sets? [CO5-L1]
The languages accepted by Turing machines are called recursively enumerable (RE),
and the subset of RE languages that are accepted by a TM that always halts are called
recursive. Enumerable means that the strings in the language can be enumerated by
the TM. The class of recursively enumerable languages includes CFL‘s.
The recursive sets include languages accepted by at least one TM that halts on all
inputs.

18. Define Recursive language? [CO5-L1]

Computer Science Engineering Department 125 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

The languages accepted by Turing machines are called recursively enumerable


(RE), and the subset of RE languages that are accepted by a TM that always halts
are called recursive.

19. When a recursively enumerable language is said to be recursive? Is it true


that the language accepted by a non-deterministic Turing machine is different
from recursively enumerable language? [CO5-L2]
A language L is recursively enumerable if there is a TM that accepts L and
recursive if there is a TM that recognizes L. Thus recursively enumerable language
is Turing acceptable and recursive language is Turing decidable languages. No, the
language accepted by non-deterministic Turing machine is same as recursively
enumerable language.

20. What is Church‟s Hypothesis? [CO5-L1]


The notion of computable function can be identified with the class of partial
recursive functions is known as Church-hypothesis or Church-Turing thesis. The
Turing machine is equivalent in computing power to the digital computer.

21. Give an example of undecidable problem? [CO5-L1]


The undecidable problems are
 Halting problem of the TM.
 Undecidability of ambiguity of CFG‘s
 Complement of a list language
 Problems about problem

22. Define Universal Turing machines? [CO5-L1]


It is easiest to describe U as a multitape Turing machine. In the case of U, the
transitions of M are stored initially on the first tape, along with the string w. A second

Computer Science Engineering Department 126 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

tape will be used to hold the simulated tape of M, using the same format as for the code
of M. That is, tape symbol Xi of M will be represented by 0i, and tape symbols will be
separated by single l's. The third tape of U holds the state of M, with state qi
represented by i 0's. A sketch of U is in Fig

Universal TMs are TMs that can be programmed to solve any problem, which can be
solved by any Turing machine. A specific Universal Turing machine U is:
Input to U: The encoding ―M‖ of a TM M and encoding ―w‖ of a string w.
Behavior: U halts on input ―M‖ ―w‖ if and only if M halts on input w.

23. What properties of recursive enumerable sets are not decidable? [CO5-L1]
o Emptiness
o Finiteness
o Regularity
o Context-freedom.

24. What is a universal language Lu? [CO5-L1]


The Universal Language: The language Lu consists of strings that are interpreted as a
TM followed by an input for that TM. The string is in Lu if the TM accepts that input. Lu
is a good example of a language that is RE but not recursive.

Computer Science Engineering Department 127 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

25. What is a Diagonalization language Ld? [CO5-L1]


The language Ld, the ―diagonalization‖ language, is the set of strings wi such that wi is
not in L(Mi). That is, Ld consists of all strings w such that the TM M whose code is w
does not accept when given w as input.
The reason La is called a "diagonalization" language can be seen if we consider Fig (1).
This table tells for all i and j, whether the TM Mi accepts input string wj: 1 means "yes it
does" and 0 means "no it doesn't." We may think of the ith row as the characteristic
vector for the language L(Mi); that is, the 1's in this row indicate the strings that are
members of this language.

26.What properties of r.e sets are recursively enumerable? [CO5-L1]


 L ≠Φ
 L contains at least 10 members.
 w is in L for some fixed w.
 L ∩ Lu ≠ Φ

27.What properties of recursively enumerable sets are not recursively


enumerable? [CO5-L1]
 L=Φ

Computer Science Engineering Department 128 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

 L = Σ *.
 L is recursive
 L is not recursive.
 L is singleton.
 L is a regular set.
 L - Lu ≠ Φ

28. What are the different types of grammars/languages? [CO5-L1]


1. Unrestricted or Phase structure grammar.(Type 0 grammar).(for TMs)
2. Context sensitive grammar or context dependent grammar (Type1) (for
Linear Bounded Automata )

3. Context free grammar (Type 2) (for PDA)


4. Regular grammar (Type 3) (for Finite Automata).
This hierarchy is called as Chomsky Hierarchy.

29. Show that AMBIGUITY problem is undecidable? [CO5-L2]


Consider the ambiguity problem for CFGs. Use the ―yes-no‖ version of AMB. An
algorithm for FIND is used to solve AMB. FIND requires producing a word with two or
more parses if one exists and answers ―no‖ otherwise. By the reduction of AMB to FIND
we conclude there is no algorithm for FIND and Hence no algorithm for AMB.

30. Define PCP or Post Correspondence Problem? [CO5-L1]


An instance of Post's Correspondence Problem (PCP) consists of two lists of strings
over some alphabet Σ; the two lists must be of equal length. We generally refer to the A
and B lists, and write A=w1, w2…. wk and B=x1, x2…. xk for some integer k. For each i,
the pair (wi , xi) is said to be a corresponding pair.
We say this instance of PCP has a solution, if there is a sequence of one or more
integers i1 ,i2,…. im that, when interpreted as indexes for strings in the A and B lists,
yield the same string.
That is, wi1, wi2….. wim, = xi1, xi2,….. xim. We say the sequence i1 ,i2,…. im is a solution to
this instance of PCP, if so.

Computer Science Engineering Department 129 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

31. Define MPCP or Modified PCP. [CO5-L1]


It is easier to reduce Lu to PCP if we first introduce an intermediate version of PCP,
which we call the Modified Post's Correspondence Problem, or MPCP. In the modified
PCP, there is the additional requirement on a solution that the first pair on the A and B
lists must be the first pair in the solution. More formally, an instance of MPCP is two
lists. A=w1, w2…. wk and B=x1, x2…. xk and a solution is a list of 0 or more integers i1
,i2,…. im such that
wi1, wi2….. wim, = xi1, xi2,….. xim.

Notice that the pair (w1, x1) is forced to be at the beginning of the two strings, even
though the index 1 is not mentioned at the front of the list that is the solution. Also,
unlike PCP, where the solution has to have at least one integer on the solution list, in
MPCP, the empty list could be a solution if wi = xi (but those instances are rather
uninteresting and will not figure in our use of MPCP).

32. What is the difference between PCP and MPCP? [CO5-L1]


This question asks, given two lists of the same number of strings, whether we can pick
a sequence of corresponding strings from the two lists and form the same string by
concatenation. PCP is an important example of an undecidable problem. PCP is a good
choice for reducing to other problems and thereby proving them undecidable.
The difference between MPCP and PCP is that in the MPCP, a solution is required to
start with the first string on each list.

33. What are the concepts used in UTMs? [CO5-L1]


 Stored program computers.
 Interpretive Implementation of Programming languages.
 Computability.

34. When a language is said to be recursively enumerable or recursive? [CO5-L2]


(may-05,13,14)
A language is recursively enumerable if there exists a Turing Machine that accepts
every string of the language and does not accept strings that are not in the language.

W
Accept
TM
Loops for ever
Input string

Computer Science Engineering Department 130 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

A language L is said to be recursive if there exists a Turing machine M that accepts L,


and goes to halt state or else M rejects L.
W
Accept
TM
Reject
Input
string

35. Define trivial property? [CO5-L1]


A property is trivial if it is either empty of is all RE languages.

36. Define Rice Theorem? [CO5-L1]


Rice's Theorem: Any non trivial property of the languages accepted by Turing
machines is undecidable. For instance, the net of codes for Turing machines whose
language is empty is undecidable by Rice's theorem. In fact, this language is not RE,
although its complement — the set of codes for TM's that accepts at least one string —
is RE but not recursive.

38. Let A and B be lists of three strings each, as defined in the following table?
[CO5-L2]

Find the instance of post correspondence Problem.


Solution:
Let Σ = {0,1}, and let the A and B lists be as defined in Fig. 9.12. In this case, PCP has
a solution. For instance, let m = 4, i1 = 2, i2 = 1, i3 = 1, and i4 = 3; (i.e.,) the solution is the
list 2, 1, 1, 3. We verify that this list is a solution by concatenating the corresponding
strings in order for the two lists.

Computer Science Engineering Department 131 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

That is, w1, w2…. wk and x1, x2…. xk = 01111110. Note this solution is not unique. For
instance, 2, 1, 1, 3, 2, 1, 1, 3 is another solution.

37. Define problem solvable in polynomial Time? [CO5-L1]


A Turing Machine M is said to be of time complexity T(n) if whenever m given an input w
of length n, m halts after making atmost T(n) moves, regardless of whether or not m
accepts.

38. Define the classes P and NP? (May-14) [CO5-L1]


P consists of all those languages or problems accepted by some Turing Machine that
runs in some polynomial amount of time, as a function of its input length. NP is the class
of languages or problems that are accepted by Nondeterministic TM‘s with a polynomial
bound on the time taken along any sequence of non – deterministic choices.

39. Define NP – Complete Problem? [CO5-L1]


A language is NP-complete if it is in NP, and there is a polynomial-time reduction from
each language in NP to the language in question. We believe strongly that none of the
NT-complete problems are in P, and the fact that no one has ever found a polynomial-
time algorithm for any of the thousands of known NP-complete problems is mutually re-
enforcing evidence that none are in P.

A language L is NP – complete if the following statements are true.


a. L is in NP
b. For every language L‘ in NP there is a polynomial time reduction of L‘ to L

40. What are the properties of recursive and Recursively Enumerable Language?
[CO5-L1]
1. The complement of a Recursive language is Recursive.
2. The union of two recursive languages is recursive.
The union of two RE languages is RE.
3. If a language L and complement L are both RE, then L is recursive.

41. Define Unsolvability. [CO5-L1]


Unsolvability

Computer Science Engineering Department 132 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

In the theory of computation we often come across such problems that are answered
either yes or no. The class of problems which can be answered as yes are called
solvable or decidable, otherwise the class of problems is said to be unsolvable or
undecidable.

42. Define Tracetable and intracetable problems[CO5-L1]


Tracetable and intracetable problems
The class of solvable problems is known as decidable problems.That means decidable
problems can be solved in measurable amount of time or space.
The tracetable problems are the class of problems that can be within reasonable time
and space.
The intracetable problems are the class of problems that can be solved within
polynomial time. This has lead to two classes of solving problems – P and NP class
problems.

PART B

1. Explain briefly about Primitive Recursive Functions. [CO5-L2]


Primitive Recursive Functions
Recursive function is class of functions those are turing computable. The theory of
recursive functions is just converse to church‘s hypothesis. Recursive function theory
begins with some very elementary that are intuitively effective. Then it provides a few
methods for building more complicated functions from simpler functions. That means
the computability of given function can be proved using the initial function and some
building operations which are few in number.

Initial Function
The initial functions are the elementary functions whose values are independent of their
smaller arguments. The following functions comprise the class of recursive functions.
The Zero function : Z(X) = 0
The successor function : S(X) = successor of X (roughly,‖X+1‖)
The identity function : id(X) = X
The zero functions returns zero regardless of this argument.
The successor functions returns the successor of its argument. Since successorship is
a more primitive notion.

Computer Science Engineering Department 133 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

The zero and successor functions take only one argument each. But the identity
functions is designed to take any number of arguments. When it takes one argument
(as above ) it returns its argument as its value. When it takes more than one
argument,it returns one of them. That means,
id(X,Y) = X
id(X,Y) = Y

Building operations: We will build more complex functions from the initial set by using
only three methods that are,
I) Compostion
II) Primitive recursion
III) Minimization

i) Composition
We will start with the successor function,
S(X) = X+1
Then we mayreplace its argument , X, with a function. If we replace the arguments,
X, with the zero function.
Z(X)
Then the result is the successor of zero,
S(Z(X)) = 1
S(S(Z(X))) = 2 and so on.
In this way, with the help of the initial functions we can describe the natural
numbers. This building operations is called ―composition‖. It should be clear that
when composition is applied to computable functions, only computable functions
will result.
ii) Primitive recursion
The second building operation is called primitive recursion is a method of
defining new functions from old function. The function f is defined through
functions f and g by primitive recursion when
h(x,0) = f(x)
h(x,s(y)) = g(x,h(x,y))
where f and g are known computable functions. There are two equations. When
h‘s second argument is zero, the first equation applies; when it is not zero, we
use the second. Use of successor function in second equation enforces the
condition that the argument be greater than zero. Hence, the equation applies in
the minimal case and the second applied in every other case.

Computer Science Engineering Department 134 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

To solve the function by primitive recursion


1) When the second argument of h is zero, the function is equivalent to some
known function f and we compute it;
2) Otherwise it is equivalent to some known function g and we compute it.

Thus the function obtained will be computable in nature. For example, we can calculate
the factorial function using recursion as:
Initally 1! = 1 and to calculate n! if we multiply n by (n-1) then it will generate a
nonrecursion series. Instead of that we will multiply n by (n-1)! We can express the
calculation of factorial function by following two equations.
n! + n*(n-1)
A strict definition of the factorial function, f(n) then,consists of these two equations:
f(n) = 1 When n = 1 1
f(n) = n*(f(n-1)) When n > 1 2
Consider n= 5 then using equation 2 we will get,
f(5) = 5* f(4)
f(4) = 4* f(3)
f(3) = 3* f(2)
f(2) = 2* f(1)
By putting the value of equation 1 for calculating f(2) we will get,
f(2) = 2* 1 = 2
Then f(3) = 3* f(2)
= 3*2 = 6
Then f(4) = 4* f(3)
= 4*6 = 24
Then f(5) = 5* f(4)
= 5*24 = 120
Primitive recursion is like mathematical induction. The first equation defines the basis,
and the second defines the induction step.
iii) Minimization
If g(x) is a function that computes the least x such that f(x) = 0, then we know that g is
computable. And then we can say that g is produced from f by minimization. But we can
build g by minimization only if f(X) is already known to be computable.
For example: Suppose we want to obtain least x which makes f(x) = 0 then we will try
the natural numbers 0,1,2,…. Until we reach the first value that gives f(x) = 0. Now if
such search for x never gets terminated then it is called unbounded minimization. While

Computer Science Engineering Department 135 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

unbounded minimization has the disadvantages of a partial function which may never
terminate, bounded minimization has the disadvantage of sometimes failing to minimize.

2. Explain about Class of Recursion Function[CO5-L2]


Class of Recursion Function
The classification of recursive function is as shown in figure.
1. Partial Recursive Function
The function is called partial recursive function if it can be obtained by applying
composition, primitive recursion and minimization as building operations.
2. General recursive Function
The function is called general recursive if it is obtained by applying composition,
primitive recursive and an unbounded minimization that happen to terminate. The
general recursive function is a larger
class than partial recursive functions.
3. Primitive Recursive Function
The function is called primitive recursive if it is obtained by applying composition,
primitive recursion and unbounded minimization that does not terminate. The set of
general recursive function is the same as the set of turing computable functions. The
example of general recursive function is an Ackermann‘s function.The Ackermann‘s
function can be defined as follows
A(0,y) = y+1
A(x+1) = A(x,1)
A(x+1,Y+1) = A(x,A(x+1,y))
Then we can calculate A(x,y) for every pair of (x,y)
Example: Compute A(1,1) A(1,2), A(2,1), A(2,2) using Ackermann‘s function.
Soltuion : Let
A(0,y) = y+1
A(x+1) = A(x,1)
A(x+1,Y+1) = A(x,A(x+1,y))
To compute A(1,1) put x=0, y=0, then
A(1,1) = A(0+1,0+1)
= A(0, A(0+1,0))
= A(0, A(0,1))
= A( A(0,1)
= A(0,2)
Hence A(1,1)

Computer Science Engineering Department 136 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Hence A(1,1) = 3
To compute A(1,2) put x = 0 and y = 1
A(1,2) = A(0+1,1+1)
= A(0, A(1,1))
= A(0,3) = 4
To compute A(2,1) put x = 1 and y = 0
A(2,1) = A(1+1,0+1)
= A(1, A(2,0))
= A(1, A(1,1))
= A(1,3)
= A(0+1,2+1)
= A(0, A(1,2))
= A(0,4)
=5
To compute A(2,2) put x = 1 and y = 1
A(2,2) = A(1+1,1+1)
= A(1, A(2,1))
= A(1,5)
Now we will compute A(1,5) where in x = 0 and y = 4
A(1,5) = A(0+1,4+1)
= A(0, A(1,4))
= A(0, A(0+1,3+1))
= A(0, A(0,A(1,3)))
= A(0, A(0,A(0+1,2+1)))
= A(0, A(0,A(0,A(1,2))))
= A(0, A(0,A(0,4)))
= A(0, A(0,5))
= A(0,6)
A(1,5) = 7
Hence A(2,2) = A(1,5)
A(2,2) = 7
3. Show that if a language L and its complement L‟ are both recursively
enumerable then L is recursive. (Nov/Dec 2003) (Nov/Dec 2004) (Apr/May2005)
(May/June 2006) (Nov/Dec 2006) (May/June 2007) (Nov/Dec 2007) [CO5-L2]

1 Recursive Languages

Computer Science Engineering Department 137 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

We call a language L recursive if L = L(M) for some Turing machine M such that:

If w is in L, then M accepts (and therefore halts).

If w is not in L, then M eventually halts, although it never enters an accepting state.


A TM of this type corresponds to our informal notion of an "algorithm," a well-defined
sequence of steps that always finishes and produces an answer.

If we think of the language L as a "problem," as will be the case frequently, then


problem L is called decidable if it is a recursive language, and it is called undecidable if
it is not a recursive language.

Figure (9.2) Relationship between the recursive, RE, and non-RE languages
The existence or nonexistence of an algorithm to solve a problem is often of
more importance than the existence of some TM to solve the problem. As mentioned
above, the Turing machines that are not guaranteed to halt may not give us enough
information ever to conclude that a string is not in the language, so there is a sense in
which they have not "solved the problem."
Thus, dividing problems or languages between the decidable — those that are
solved by an algorithm — and those that are undecidable is often more important than
the division between the recursively enumerable languages (those that have TM's of
some sort) and the non-recursively-enumerable languages (which have no TM at all).

1. The recursive languages.

Computer Science Engineering Department 138 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

2. The languages that are recursively- enumerable but not recursive.

3. The non-recursively-enumerable (non-RE) languages.

We have positioned the non-RE language L5 properly, and we also show the language
L, or "universal language," that we shall prove not to be recursive, although it is RE.

3. Complements of Recursive and RE languages[CO5-L2]


A powerful tool in proving languages to belong in the second ring of Fig. 9.2 (i.e., to be
RE, but not recursive) is consideration of the complement of the language.

We shall show that the recursive languages are closed under complementation. Thus, if
a language L is RE, but L‘, the complement of L, is not RE, and then we know L cannot
be recursive. For if L were recursive, then L‘ would also be recursive and thus surely
RE.

We now prove this important closure property of the recursive languages.

Theorem 9.3: If L is a recursive language, so is L‘.

Proof: Let L=L(M) for some TM Al that always halts. We construct a TM M‘ such that
L‘= L(M‘)
1. The accepting states of M are made non-accepting states of M with no
transitions; i.e., in these states M will halt without accepting.
2. M‘ has a new accepting state r; there are no transitions from r.
3. For each combination of a nonaccepting state of M and a tape symbol of Al such
that M has no transition (i.e., M halts without accepting), add a transition to the
accepting state r.

Computer Science Engineering Department 139 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Construction of a TM accepting the complement of a recursive language

Since M is guaranteed to halt, we know that M is also guaranteed to halt.


Moreover, M‘ accepts exactly those strings that M does not accept. Thus M‘ accepts L‘.

4. That if a language L and its complement L‟ are both recursively enumerable


then L is recursive. [CO5-L2]
(Nov/Dec 2003) (Nov/Dec 2004) (Apr/May2005) (May/June 2006) (Nov/Dec 2006)
(May/June 2007) (Nov/Dec 2007)
Theorem (9.4): If both a language L and its complement are RE, then L is recursive.
Proof: Let L=L(M1) and L‘= L(M2). Both M1 and M2 are simulated in parallel by a TM M.
We can make M a two-tape TM, and then convert it to a one-tape TM, to make the
simulation easy and obvious. One tape of M simulates the tape of M 1, while the other
tape of Al simulates the tape of M2. The states of M1 and M2 are each components of
the state of M.

Computer Science Engineering Department 140 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Simulation of two TM's accepting a language and its complement

If input w to M is in L, then Mi will eventually accept. If so, M accepts and halts. If w is


not in L, then it is in L‘, so M2 will eventually accept. When M2 accepts, M halts without
accepting. Thus, on all inputs, M halts, and L(M) is exactly L. Since M always halts, and
L(M) = L, we conclude that L is recursive.

A language L and its complement L in the diagram of Fig. 9.2, only the following four are
possible:

1. Both L and I, are recursive; i.e., both are in the inner ring.
2. Neither L nor L‘ is RE; i.e., both are in the outer ring.
3. L is RE but not recursive and L‘ is not RE; i.e., one is in the middle ring and the
other is in the outer ring.
4. L‘ is RE hut not recursive, and L is not RE; i.e., the same as (3), but with L and L‘
swapped.
In proof of the above, Theorem (9.3) eliminates the possibility that one language (L or L)
is recursive and the other is in either of the other two classes. Theorem (9.4) eliminates
the possibility that both are RE but not recursive.

Example: As an example, consider the language Ld, which we know is not RE. Thus, Ld
could not be recursive. It is, however, possible that Ld‘ could be either non-RE or RE but
not-recursive. Ld‘ is the set of strings wi such that Mi accepts wi.

This language is similar to the universal language Lu, consisting of all pairs (M, w) such
that M accepts w, the same argument can be used to show Ld‘ is RE.

5.Prove that the Universal language is recursively enumerable but not recursive.
(Nov/Dec 2009) [CO5-L3]
The Universal Language (Nov/Dec 2003), (Apr/May 2005) (Nov/Dec 2005)
(May/June 2006) (Nov/Dec 2006)
We already discussed how a Turing machine could be used to simulate a computer that
had been loaded with an arbitrary program. That is to say, a single TM can be used as a
"stored program computer," taking its program as well as its data from one or more
tapes on which input is placed.

Computer Science Engineering Department 141 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

In this section, we shall repeat the idea with the additional formality that comes with
talking about the Turing machine as our representation of a stored program.

We define Lu, the universal language, to be the set of binary strings that encode, a pair
(M, w), where M is a TM with the binary input alphabet, and w is a string in (0+1)*, such
that w is in L(M). That is, Lu is the set of strings representing a TM and an input
accepted by that TM. We shall show that there is a TM U, often called the universal
Turing machine, such that, Lu = L(U). Since the input to U is a binary string, U is in fact
some M in the list of binary-input Turing machines.

It is easiest to describe U as a multitape Turing machine. In the case of U, the


transitions of M are stored initially on the first tape, along with the string w. A second
tape will be used to hold the simulated tape of M, using the same format as for the code
of M. That is, tape symbol Xi of M will be represented by 0i, and tape symbols will be
separated by single l's. The third tape of U holds the state of M, with state qi
represented by i 0's. A sketch of U is in Fig.

Computer Science Engineering Department 142 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Organization of a universal Turing machine

The operation of U can be summarized as follows:

Examine the input to make sure that the code for M is a legitimate code for some TM. If
not, U halts without accepting. Since invalid codes are assumed to represent the TM
with no moves, and such a TM accepts no inputs, this action is correct.
Initialize the second tape to contain the input w, in its encoded form. That is, for each 0
of w, place 10 on the second tape, and for each 1 of w, place 100 there.

Note that the blanks on the simulated tape of M, which are represented by 1000, will not
actually appear on that tape; all cells beyond those used for w will hold the blank of U.
However, U knows that, should it look for a simulated symbol of M and find its own
blank, it must replace that blank by the sequence 1000 to simulate the blank of M.

1. Place 0, the start state of M, on the third tape, and move the head of Ws second
tape to the first simulated cell.
2. To simulate a move of M, U searches on its first tape for ,a transition
0i10i10k10110m, such that 0i is the state on tape 3, and 0j is the tape symbol of M
that begins at the position on tape 2 scanned by U. This transition is the one M
would next make.

(a) Change the contents of tape 3 to 0k that is, simulate the state change of M. To
do so, U first changes all the 0's on tape 3 to blanks, and then copies 0k from tape 1
to tape 3.

(b) Replace 0j on tape 2 by 0k; that is, change the tape symbol of M. If more or less
space is needed (i.e., i =1), use the scratch tape and the shifting-over technique to
manage the spacing.

Computer Science Engineering Department 143 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

(c) Move the head on tape 2 to the position of the next 1 to the left or right,
respectively, depending on whether m = 1 (move left) or m= 2 (move right). Thus, U
simulates the move of M to the left or to the right.

3. If M has no transition that matches the simulated state and tape symbol, then in
(4), no transition will be found. Thus, M halts in the simulated configuration, and
U must do likewise.
4. If M enters its accepting state, then U accepts.

5. In this manner, U simulates M on w. U accepts the coded pair (M,w) if and only if
M accepts w.

Undecidability of the Universal Language

A problem that is RE but not recursive; it is the language Lu. Knowing that Lu is
undecidable (i.e., not a recursive language) is in many ways more valuable than our
previous discovery that Ld is not RE. The reason is that the reduction of Lu to another
problem P can be used to show there is no algorithm to solve P, regardless of whether
or not P is RE. However, reduction of Ld to P is only possible if P is not RE, so Ld
cannot be used to show undecidability for those problems that are RE but not recursive.
On the other hand, if we want to show a problem not to be RE, then only Ld can be
used; Lu is useless since it is RE.

6.Define the Language Lu. Show that Lu is recursively enumerable but not
recursive. (Nov/Dec 2003) (Apr/May 2004) (Apr/May 2005) (Nov/Dec 2005)
(May/June 2006) (Nov/Dec 2006) (May/June 2009) [CO5-L2]
Theorem 9.6: Lu is RE but not recursive.

Proof: We Suppose Assume Lu is recursive. Then Lu, is the complement of Lu‘, would
also be recursive. However, if we have a TM M to accept Lu‘, then we can construct a

Computer Science Engineering Department 144 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

TM to accept Ld. Since we already know that Ld is not RE, we have a contradiction of
our assumption that Lu is recursive

Suppose L(M) = Lu. As suggested by Fig (9.6), we can modify TM M into a TM M' that
accepts Ld as follows.

1. Given string w on its input, M' changes the input to w111w. You may, as an
exercise, write a TM program to do this step on a single tape. However, an easy
argument that it can be done is to use a second tape to copy w, and then convert the
two-tape TM to a one-tape TM.

2. M' simulates M on the new input. If w is wi in our enumeration, then M'


determines whether Mi accepts wi. Since M accepts Lu‘, it will accept if and only if Mi
does not accept wi (i.e.,) wi is in Ld.

3. Thus, M accepts w if and only if w is in Ld. we conclude that Lu, is not


recursive.

7.Write a note on NP problems.(Dec – 12) (or) [CO5-L2]


Write the classes and definition of NP problems.(Dec-13)
Tracetable and possibly intracetable problems : P and NP
There are two groups in which a problem can be classified. The first group of the
problems that can be solved in polynomial time. For example: searching of an element
from the list O(logn), sorting of elements O(logn).

Computer Science Engineering Department 145 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

The second group consists of problem that can be solved in non-deterministic


polynomial time. For example : Knapsack problem O(2n/2) and travelling salesperson
problem (O(n22n)).
 Any problem for which answer is either yes ar no is called decision problem. The
algorithm for decision problem is called decision algorithm.
 Any problem that involves the identification of optimal cost(minimum or maximum) is
called optimization problem. The algorithm for optimization problem is called
optimization algorithm.
 Definition of P – problems that can be solved in polynomial time. (―p‖ stands for
polynomial).
Examples – searching of key element, sorting of elements, All pair shortest path.
 Definition of NP – It stands for ― non-deterministic polynomial time‖. Note that NP
does not stand for ―non-polynomial‖.
Examples – Travelling Salesperson problem, graph coloring problem, Knapsack
problem, Hamiltonian circuit problems.

Computational complexity
problems

P- class NP- class

NP- complete NP-hard

A problem D is called NP-complete if –


i) It belongs to class NP
ii) Every problem in NP can also solved in polynomial time.
If an NP-hard problem can be solved in polynomial time then all NP-complete
problems can also be solved in polynomial time.
All NP-complete problems are NP-hard but all NP-hard problems cannot be Np-
complete.
The NP class problems are the decision problems that can be solved by non-
deterministic polynomial algorithm.
(1) Example of P class problem

Computer Science Engineering Department 146 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Kruskal‘s algorithm: In kruskal‘s algorithm the minimum weight is obtained. In this


algorithm also the circuit should not be formed. Each has to be selected, from the
graph. It has edges of minimum weights to be adjacent. Let us solve one
example by Kruskal‘s algorithm

Example 1:

(1) (2)
1
1
2
V1

V1
8 1
V6
V6
2 3 V2
1
13
2
12 2 8 V2
3

V5 V3
7 12
7
10 9 V8
V3
V4 V4

V
1
V
6
1
2
3 V
2

7
V V
8 3
V
4

Computer Science Engineering Department 147 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Weight = 21

Find the minimum spanning tree for the following figure using Kruskal‘s algorithm.

a c
1 b 2

2 1
2
2 1
e
1 1
3 f d 2

3 3
3
2 3

3 3
g h i

In kruskal‘s algorithm, we will start with some vertex and will cover all the vertices with
minimum weight. The vertices need not be adjacent.

a b

a b

1
1

a b
c

1 1

e d
1
Computer Science Engineering Department 148 Theory of Computation
S.K.P. Engineering College, Tiruvannamalai V SEM

a b
c

1 1

f e d 2
1

Example of NP class problem

Travelling salesman‘s problem (TSP) : This problem can be stated as ― Given a set of
cities and cost to travel between each pair of cities, determine whether there is a path
that visits every city once and returns to the first city. Such that the cost travelled is the
tour will be a-b-d-e-c-a and total cost of tour will be 16.

This problem is NP problem as there may exist some path with shortest distance
between the cities. If you get the solution by applying certain algorithm then travelling
salesman problem is NP complete problem. If we get no solution at all by applying an
algorithm then the travelling salesman belongs to NP hard class.

For Example:
3
a b
6
5
7
1

4
8 c d 2

2 3
e

Computer Science Engineering Department 149 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

NP Completeness
As we know, P denotes the class of all deterministic polynomial language problems and
NP denotes the class of all non-deterministic polynomial language problems. Hence
P € NP
The question of whether or not P= NP
Holds, is the most famous outstanding problem in the computer science.
Problems which are known to lie in P are often called as tracetable. Problems which lie
outside of P are often termed as intracetable . Thus, the question of whether P=NP or P
= NP is the same as that of asking whether there exist problems in NP which are
intracetable or not.

9.Explain briefly about Polynomial Time Reductions.(Dec-10,11,May-12) [CO5-L2]


Polynomial Time Reductions
To prove whether particular problem is NP complete or not we use polynomial time
reducibility. That means if
The reduction is an important task in NP completeness proofs.

Poly Poly Poly


A B and B C then A C

Computer Science Engineering Department 150 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Every problem in NP

CIRCUIT -
SAT

CNF - SAT

3- SAT

VERTEX
COVER

CLIQUE SET - COVER SUBSET SUM HAMILTONIAN - CYCLE

KNAPSACK TSP

Reduction in NP completeness

Various types of reductions are


Local replacement – In this reduction A  B by dividing input to a in the form of
components and then these components can be converted to components of B.
Component design – In this reduction A B by building special component for input B
that enforce properties required by A.

Computer Science Engineering Department 151 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

10.Explain Proving NP complete Problem. [CO5-L2]


Proving NP complete Problem
The satisfiability problem
1. CNF – SAT problem
This problem is based on Boolean formulas. The Boolean formula has various
Boolean operations such as OR(+), AND(.) and NOT. There are some notations
such as  (means implies) and (means if and only if).
A boolean formula is in Conjuctive Normal Form (CNF) if it is formed as
collection of subexpressions. These subexpressions are called clauses.
For example
(a+b+d+g) (c+e) (b+d+f+h) (a+c+e+h)
This formula evaluates to 1 if b,c,d are 1.
The CNF – SAT is a problem which takes Boolean formula in CNF form checks
whether any assignment is there to Boolean values so that formula evaluates to
1.
Theorem: CNF – SAT is in NP complete.
Proof: Let S be the Boolean formula for which we can construct a simple non-
deterministic algorithm which can guess the value of variables in Boolean
formula and then evaluates each clause of S. If allthe clause evaluate S to 1 then
S is satisfied. Thus CNF – SAT is in NP-complete.

2. A 3- SAT problem
A 3 SAT problem is a problem which takes a Boolean formula S in CNF form with
each clause having exactly three literals and check whether S is satisfied or not.
[Note that CNF means each literal is ORed to form a clause, and each clause is
ANDed to form Boolean formula S].

Theorem: 3SAT is in NP complete


Proof: Let S be the Boolean formula having 3 literals in each clause for which we
can construct a simple non-deterministic algorithm which can guess an
assignment of Boolean values to S. If the S is evaluated a s1 then S is satisfied.
Thus we can prove that 3SAT is in NP-complete.

Some other NP complete problems


1. The 0/1 Knapsack problem – It can be proved as NP complete by reduction
from SUM OD SUBSET problem.

Computer Science Engineering Department 152 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

2. Hamiltonian Cycle – It can be proved as NP complete, by reduction from


vertex cover.
3. Travelling Salesperson problem – It can be proved as NP proved as NP
complete, by reduction from Hamiltonian cycle.

1. HAMILTONIAN CYCLE : This is a problem in which graph G is accepted


as input and it is asked to find a simple cycle in G that visits each vertex of G
exactly once and returns to its starting vertex. Such a cycle is called an
Hamiltonian cycle.
Theorem : HAMILTONIAN CYCLE is in NP.
Proof : Let A be some non-deterministic algorithm to which graph G is given
as input. The vertices of graph are numbered from 1 to N. We have to call the
algorithm recursively in order to get the sequence will have all the vertices
without getting repeated. The vertex from which the sequence starts must be
ended at the end. This check on the sequence S must be made in polynomial
time n. Now if there is a Hamiltonian cycle in the graph then algorithm will
output ―yes‖. Similarly if we get output of algorithm as ―yes‖ then we could
guess the cycle in G with every vertex appearing exactly once and the first
visited vertex getting visited at the last. That means A non-deterministic ally
accepts the language HAMILTONIAN CYCLE. It is therefore proved that
HAMILTONIAN CYCLE is in NP.
2. CIRCUIT –SAT : This is a problem in which a Boolean circuit is taken as
input with single output node, And then finds whether there is an assignment
of values to the circuit‘s input so that we its output value as ―1‖. This
assignment of values is called satisfying assignment.
Theorem : CIRCUIT- SAT is in NP
Proof: The logic gates that are used are
A which works in polynomial time. Then we should have choose ( ) method
which can guess the vales of input node as well as the output, then the
algorithm says ―no‖ if we get output of Boolean circuit as 0. Similarly the
algorithm says ―yes‖ if we get output of Boolean circuit as 1.
Now from the output of algorithm we can guess the inputs to logic gates in the
circuit. If the algorithm has output ―yes‖ than we say that Boolean circuit has
satisfying input values. Thus we can say that

AND NOT OR

Computer Science Engineering Department 153 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

1 0
1

1
0
1
1

0 0 1
1

Definition of Post's Correspondence Problem


An instance of Post's Correspondence Problem (PCP) consists of two lists of strings
over some alphabet Σ; the two lists must be of equal length. We generally refer to the A
and B lists, and write A=w1, w2…. wk and B=x1, x2…. xk for some integer k. For each i,
the pair (wi , xi) is said to be a corresponding pair.
We say this instance of PCP has a solution, if there is a sequence of one or more
integers i1 ,i2,…. im that, when interpreted as indexes for strings in the A and B lists,
yield the same string.
That is, wi1, wi2….. wim, = xi1, xi2,….. xim. We say the sequence i1 ,i2,…. im is a solution to
this instance of PCP, if so.

The Post's correspondence problem is: Given an instance of PCP, tell whether this
instance has a solution.

Example 9.13: Let Σ = {0,1}, and let the A and B lists be as defined in Fig. 9.12. In this
case, PCP has a solution. For instance, let m = 4, i1 = 2, i2 = 1, i3 = 1, and i4 = 3; (i.e.,)
the solution is the list 2, 1, 1, 3. We verify that this list is a solution by concatenating the
corresponding strings in order for the two lists.
That is, w2w1w1w3 = x2x1x1x3 = 01111110. Note this solution is not unique. For
instance, 2, 1, 1, 3, 2, 1, 1, 3 is another solution.

Computer Science Engineering Department 154 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

An instance of PCP
Example 9.14: Here is an example where there is no solution. Again we let E = {0,1},
but now the instance is the two lists given in Fig. 9.13.
Suppose that the PCP instance of Fig. 9.13 has a solution, say for some m ≥ 1. We
claim i1 = 1. For if i1 = 2, then a string beginning with w2 = 011 would have to equal a
string that begins with x2 = 11. But that equality is impossible, since the first symbols of
these two strings are 0 and 1, respectively. Similarly, it is not possible that i 1 = 3, since
then a string beginning with w3 = 101 would have to equal a string beginning with x3 =
011.
If i1 = 1, then the two corresponding strings from lists A and B would have to begin:
A=10…
B=101…
Now, let us see what i2 could be.
1. If i2 = 1, then we have a problem. Since no string beginning with w1w1 = 1010 can
match a string that begins with x1x1=101101;they must disagree at the fourth position.

Another PCP instance


2. If i2 = 2, we again have a problem, because no string that begins with w1w2 = 10011
can match a string that begins with x1x2 = 10111; they must differ at the third position.
3. Only i2 = 3 is possible.
If we choose i2 = 3, then the corresponding strings formed from list of integers ii,i3 are:
A: 10101 …..

Computer Science Engineering Department 155 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

B: 101011….
There is nothing about these strings that immediately suggests we cannot extend list 1,
3 to a solution. However, we can argue that it is not possible to do so. The reason is
that we are in the same condition we were in after choosing i 1 = 1. The string from the B
list is the same as the string from the A list except that in the B list there is an extra 1 at
the end. Thus, we are forced to choose i3 = 3, i4 = 3, and so on, to avoid creating a
mismatch. We can never allow the A string to catch up to the B string, and thus can
never reach a solution.
2 The "Modified" PCP
It is easier to reduce Lu to PCP if we first introduce an intermediate version of PCP,
which we call the Modified Post's Correspondence Problem, or MPCP. In the modified
PCP, there is the additional requirement on a solution that the first pair on the A and B
lists must be the first pair in the solution. More formally, an instance of MPCP is two lists
A=w1, w2…. wk and B=x1, x2…. xk and a solution is a list of 0 or more integers i1 ,i2,…. im
such that
wi1, wi2….. wim, = xi1, xi2,….. xim.
Notice that the pair (w1, x1) is forced to be at the beginning of the two strings, even
though the index 1 is not mentioned at the front of the list that is the solution. Also,
unlike PCP, where the solution has to have at least one integer on the solution list, in
MPCP, the empty list could be a solution if wi = xi (but those instances are rather
uninteresting and will not figure in our use of MPCP).

Example 9.15: The lists of Fig. 9.12 may be regarded as an instance of MPCP.
However, as an instance of MPCP it has no solution. In proof, observe that any partial
solution has to begin with index 1, so the two strings of a solution would begin:
A:1…..
B:111….
The next integer could not be 2 or 3, since both w2 and w3 begin with 10 and thus
would produce a mismatch at the third position. Thus, the next index would have to be
1, yielding:
A:11….
B:111111….
We can argue this way indefinitely. Only another 1 in the solution can avoid a mismatch,
but if we can only pick index 1, the B string remains three times as long as the A string,
and the two strings can never become equal.
An important step in showing PCP is undecidable is reducing MPCP to PCP. Later, we
show MPCP is undecidable by reducing Lu to MPCP. At that point, we will have a proof

Computer Science Engineering Department 156 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

that PCP is undecidable as well; if it were decidable then we could decide MPCP, and
thus Lu.
Given an instance of MPCP with alphabet Σ. we construct an instance of PCP as
follows. First, we introduce a new symbol * that, in the PCP instance, goes between
every symbol in the strings of the MPCP instance. However, in the strings of the A list,
the *'s follow the symbols of E, and in the B list, the *'s precede the symbols of E. The
one exception is a new pair that is based on the first pair of the MPCP instance; this pair
has an extra * at the beginning of w1, so it can be used to start the PCP solution.
A final pair (*, *$) is added to the PCP instance. This pair serves as the last in a PCP
solution that mimics a solution to the MPCP instance.
Now, let us formalize the above construction. We are given an instance of MPCP with
lists A=w1, w2…. wk and B=x1, x2…. xk. We assume * and $ are symbols not present in
the alphabet Σ of this MPCP instance. We construct a. PCP instance C = y 0, y1….
yk+1 and D = z0, z1…. zk+1 follows:
1. For i = 1, 2, ... , k, let yi be wi with a * after each symbol of wi, and let zi be xi with
a * before each symbol of xi.
2. y0 = * y1, and z0 = z1. That is, the 0th pair looks like pair 1, except that there is an
extra * at the beginning of the string from the first list. Note that the 0th pair will
be the only pair in the PCP instance where both strings begin with the same
symbol, so any solution to this PCP instance will have to begin with index 0.

12.Explain briefly about Post's Correspondence Problem.(Dec-07,10,11,12,14)


[CO5-L2]
Post's Correspondence Problem
In this section, we begin reducing undecidable questions about Turing machines to
undecidable questions about "real‖ things, that is, common matters that have nothing to
do with the abstraction of the Turing machine. We begin with a problem called "Post's
Correspondence Problem" (PCP), which is still abstract, hut it involves strings rather
than Turing machines. Our goal is to prove this problem about strings to be
undecidable, and then use its undecidability to prove other problems undecidable by
reducing PCP to those. We shall prove PCP undecidable by reducing Lu to PCP. To
facilitate the proof, we introduce a "modified" PCP, and reduce the modified problem to
the original PCP. Then, we reduce Lu to the modified PCP. The chain of reductions is
suggested by Fig. Since the original Lu is known to be undecidable, we conclude that
PCP is undecidable.

Computer Science Engineering Department 157 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Figure: Reductions proving the undecidability of Post's Correspondence


Problem
1 Definition of Post's Correspondence Problem
An instance of Post's Correspondence Problem (PCP) consists of two lists of strings
over some alphabet Σ; the two lists must be of equal length. We generally refer to the A
and B lists, and write A=w1, w2…. wk and B=x1, x2…. xk for some integer k. For each i,
the pair (wi , xi) is said to be a corresponding pair.
We say this instance of PCP has a solution, if there is a sequence of one or more
integers i1 ,i2,…. im that, when interpreted as indexes for strings in the A and B lists,
yield the same string.

That is, wi1, wi2….. wim, = xi1, xi2,….. xim. We say the sequence i1 ,i2,…. im is a solution to
this instance of PCP, if so.

The Post's correspondence problem is: Given an instance of PCP, tell whether this
instance has a solution.

Example: Let Σ = {0,1}, and let the A and B lists be as defined in Fig. 9.12. In this case,
PCP has a solution. For instance, let m = 4, i1 = 2, i2 = 1, i3 = 1, and i4 = 3; (i.e.,) the
solution is the list 2, 1, 1, 3. We verify that this list is a solution by concatenating the
corresponding strings in order for the two lists.

That is, w2w1w1w3 = x2x1x1x3 = 01111110. Note this solution is not unique. For
instance, 2, 1, 1, 3, 2, 1, 1, 3 is another solution.

Computer Science Engineering Department 158 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

An instance of PCP

Example: Here is an example where there is no solution. Again we let E = {0,1}, but
now the instance is the two lists .

Suppose that the PCP instance has a solution, say for some m ≥ 1. We claim i 1 = 1. For
if i1 = 2, then a string beginning with w2 = 011 would have to equal a string that begins
with x2 = 11. But that equality is impossible, since the first symbols of these two strings
are 0 and 1, respectively. Similarly, it is not possible that i1 = 3, since then a string
beginning with w3 = 101 would have to equal a string beginning with x3 = 011.

If i1 = 1, then the two corresponding strings from lists A and B would have to begin:

A=10…

B=101…

Now, let us see what i2 could be.

1. If i2 = 1, then we have a problem. Since no string beginning with w1w1 = 1010


can match a string that begins with x1x1=101101;they must disagree at the
fourth position.

Computer Science Engineering Department 159 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Another PCP instance

2. If i2 = 2, we again have a problem, because no string that begins with w1w2 = 10011
can match a string that begins with x1x2 = 10111; they must differ at the third position.

3. Only i2 = 3 is possible.

If we choose i2 = 3, then the corresponding strings formed from list of integers ii,i3 are:

There is nothing about these strings that immediately suggests we cannot extend list 1,
3 to a solution. However, we can argue that it is not possible to do so. The reason is
that we are in the same condition we were in after choosing i 1 = 1. The string from the B
list is the same as the string from the A list except that in the B list there is an extra 1 at
the end. Thus, we are forced to choose i3 = 3, i4 = 3, and so on, to avoid creating a
mismatch. We can never allow the A string to catch up to the B string, and thus can
never reach a solution.

2 The "Modified" PCP

It is easier to reduce Lu to PCP if we first introduce an intermediate version of


PCP, which we call the Modified Post's Correspondence Problem, or MPCP. In the
modified PCP, there is the additional requirement on a solution that the first pair on the
A and B lists must be the first pair in the solution. More formally, an instance of MPCP is
two lists A=w1, w2…. wk and B=x1, x2…. xk and a solution is a list of 0 or more
integers i1 ,i2,…. im such that

wi1, wi2….. wim, = xi1, xi2,….. xim.

Computer Science Engineering Department 160 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Notice that the pair (w1, x1) is forced to be at the beginning of the two strings, even
though the index 1 is not mentioned at the front of the list that is the solution. Also,
unlike PCP, where the solution has to have at least one integer on the solution list, in
MPCP, the empty list could be a solution if wi = xi (but those instances are rather
uninteresting and will not figure in our use of MPCP).

Example :The lists of Fig. may be regarded as an instance of MPCP. However, as an


instance of MPCP it has no solution. In proof, observe that any partial solution has to
begin with index 1, so the two strings of a solution would begin:

A:1…..

B:111….

The next integer could not be 2 or 3, since both w2 and w3 begin with 10 and thus
would produce a mismatch at the third position. Thus, the next index would have to be
1, yielding:

A:11….

B:111111….

We can argue this way indefinitely. Only another 1 in the solution can avoid a mismatch,
but if we can only pick index 1, the B string remains three times as long as the A string,
and the two strings can never become equal.

An important step in showing PCP is undecidable is reducing MPCP to PCP. Later, we


show MPCP is undecidable by reducing Lu to MPCP. At that point, we will have a proof
that PCP is undecidable as well; if it were decidable then we could decide MPCP, and
thus Lu.

Given an instance of MPCP with alphabet Σ. we construct an instance of PCP as


follows. First, we introduce a new symbol * that, in the PCP instance, goes between
every symbol in the strings of the MPCP instance. However, in the strings of the A list,
the *'s follow the symbols of E, and in the B list, the *'s precede the symbols of E. The

Computer Science Engineering Department 161 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

one exception is a new pair that is based on the first pair of the MPCP instance; this pair
has an extra * at the beginning of w1, so it can be used to start the PCP solution.

A final pair (*, *$) is added to the PCP instance. This pair serves as the last in a PCP
solution that mimics a solution to the MPCP instance.

Now, let us formalize the above construction. We are given an instance of MPCP with
lists A=w1, w2…. wk and B=x1, x2…. xk. We assume * and $ are symbols not present in
the alphabet Σ of this MPCP instance. We construct a. PCP instance C = y 0, y1….
yk+1 and D = z0, z1…. zk+1 follows:

3. For i = 1, 2, ... , k, let yi be wi with a * after each symbol of wi, and let zi be xi with
a * before each symbol of xi.
4. y0 = * y1, and z0 = z1. That is, the 0th pair looks like pair 1, except that there is an
extra * at the beginning of the string from the first list. Note that the 0th pair will
be the only pair in the PCP instance where both strings begin with the same
symbol, so any solution to this PCP instance will have to begin with index 0.
5. yk+1 = $ and zk+1=*$.
Example: Suppose Fig. is an MPCP instance. Then the instance of PCP constructed by
the above steps is shown in Fig. 9.14.

Constructing an instance of PCP from an MPCP instance

Theorem: MPCP reduces to PCP.

Computer Science Engineering Department 162 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

PROOF: The construction given above is the heart of the proof. First, suppose that
1,i2,,rn is a solution to the given MPCP instance with lists A and B.

Then we know wiwi1 wi2….. wim = xi xi1 xi2….. xim. If we were to replace the w's by y's and
the x's by z‘s, we would have two strings that were almost the same; y1yi1 yi2….. yim =
z1zi1 zi2….. zim. The difference is that the first string would be missing a * at the
beginning, and the second would be missing a * at the end. That is,

*y1yi1 yi2….. yim = z1 zi1 zi2….. zim*

However, y0 = * y1, and z0 = z1, so we can fix the initial * by replacing the first index by 0.
We then have:

y0yi1 yi2….. yim = z0zi1 zi2….. zim*

6. We can take care of the final * by appending the index k + 1. Since y k+1 = $ and
zk+1=*$. we have:
y0yi1 yi2….. yim yk+1 = z0zi1 zi2….. zim zk+1

We have thus shown that 0, i1 ,i2 ….. iim ik+1 is a solution to the instance of PCP.

Now, we must show the converse, that if the constructed instance of PCP has a
solution, then the original TVIPCP instance has a solution as well. We observe that a
solution to the PCP instance must begin with index 0 arid end with index k + 1, since
only the 0th pair has strings yo and zo that begin with the same symbol, and only the (k
+ 1) st pair has strings that end with the same symbol. Thus, the PCP solution can be
written 0, i1 ,i2 ….. iim ik+1

We claim that i1 i2, is a solution to the MPCP instance. The reason is that if we remove
the *'s and the final $ from the string y0yi1 yi2….. yim yk+1 WC get the string w1wi1 wi2…..
wim. Also, if we remove the *'s and $ from the string z0zi1 zi2….. zim zk+1 we get x1 xi1
xi2….. xim We know that

y0yi1 yi2….. yim yk+1 = z0zi1 zi2….. zim zk+1

Computer Science Engineering Department 163 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

so it follows that

wiwi1 wi2….. wim = xi xi1 xi2….. xim

Thus, a solution to the PCP instance implies a solution to the MPCP instance.

We now see that the construction described prior to this theorem is an algorithm that
converts an instance of MPCP with a solution to an instance of PCP with a solution, and
also converts an instance of MPCP with no solution to an instance of PCP with no
solution, Thus, there is a reduction of MPCP to PCP, which confirms that if PCP were
decidable, MPCP would also be decidable.

Completion of the Proof of PCP Undecidability


We now complete the chain of reductions of Fig by reducing Lu to MPCP. That is, given
a pair (M, w), we construct an instance (A, B) of MPCP such that TM AI accepts input w
if and only if (A, B) has a solution
The essential idea is that MPCP instance (A, B) simulates, in its partial solutions, the
computation of M on input w. That is. partial solutions will consist of strings that are
prefixes of the sequence of ID's of M: #αi#α2#α3#...., where αi is the initial ID of M with
input w, and αi├ αi+1 for all i. The string from the B list will always be one ID ahead of
the string from the A list, unless M enters an accepting state. In that case, there will be
pairs to use that will allow the A list to "catch up" to the B list and eventually produce a
solution. However, without entering an accepting state, there is no way that these pairs
can be used, and no solution exists.

To simplify the construction of an MPCP instance, we shall invoke Theorem, which says
that we may assume our TM never prints a blank, and never moves left from its initial
head position. In that case, an ID of the Turing machine will always be a string of the
form αqβ, where α and β are strings of nonblank tape symbols, and q is a state.
However, we shall allow β to be empty, if the head is at the blank immediately to the
right of α, rather than placing a blank to the right of the state. Thus, the symbols of α
and β will correspond exactly to the contents of the cells that held the input, plus any
cells to the right that the head has previously visited.

Computer Science Engineering Department 164 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Let M (Q, Σ, Γ, δ, qo, B, F) be a TM satisfying Theorem 8.12, and let w in Σ* be an input


string. We construct an instance of MPCP as follows. To understand the motivation
behind our choice of pairs, remember that the goal is for the first list to be one ID behind
the second list, unless M accepts.The first pair is:

List A List B

# #qow#

This, pair, which must start any solution according to the rules of MPCP, begins the
simulation of M on input w. Notice that initially, the B list is a complete ID ahead of the A
list.

Tape symbols and the separator # can be appended to both lists. The pairs

List A List B

X X for each X in Γ

# #

allow symbols not involving the state to be "copied." In effect, choice of these pairs lets
us extend the A string to match the B string, and at the same time copy parts of the
previous ID to the end of the B string. So doing helps to form the next ID in the
sequence of moves of M, at the end of the B string.

3. To simulate a move of M, we have certain pairs that reflect those moves. For all q in
Q - F (i.e., q is a nonaccepting state), p in Q, and X, Y, and Z in Γ we have:

List A List B
qX Yp if δ(q, X) = (p, Y, R)
ZqX pZY if δ(q, X) = (p, Y, L); Z is any tape symbol
q# Y p# if δ(q, B) = (p, Y, R)
Zq# pZY # if δ(q, B) = (p, Y, L); Z is any tape symbol

Computer Science Engineering Department 165 Theory of Computation


S.K.P. Engineering College, Tiruvannamalai V SEM

Like the pairs of (2), these pairs help extend the B string to add the next ID, by
extending the A string to match the B string. However, these pairs use the state to
determine the change in the current ID that is needed to produce the next ID. These
changes — a new state, tape symbol, and head move — are reflected in the ID being
constructed at the end of the B string.

. If the ID at the end of the B string has an accepting state, then we need to allow the
partial solution to become a complete solution. We do so by extending with "ID's" that
are not really ID's of M, but represent

what would happen if the accepting state were allowed to consume all the tape symbols
to either side of it. Thus, if q is an accepting state, then for all tape symbols X and Y,
there are pairs:List A List B

X qY q

Xq q

qY q

5 Finally, once the accepting state has consumed all tape symbols, it stands alone as
the last ID on the B string. That is, the remainder of the two strings (the suffix of the B
string that must be appended to the A string to match the B string) is q#. We use the
final pair:

List A List B

q## #

to complete the solution.

In what follows, we refer to the five kinds of pairs generated above as the pairs from rule
(1), rule (2), and so on.

Computer Science Engineering Department 166 Theory of Computation

You might also like