<<

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CS6503 THEORY OF COMPUTATION L T P C 3 0 0 3 OBJECTIVES: The student should be made to: • Understand various Computing models like Finite State Machine, Pushdown Automata, and . • 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- – 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. 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, Pushdown Automata, and Turing Machine. • Explain the Decidability or Undecidability of various problems TEXT BOOKS: 1. Hopcroft J.E., Motwani R. and Ullman J.D, “Introduction to , 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 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

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 1

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

UNIT-I

FINITE AUTOMATA

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 2

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

UNIT I

FINITE AUTOMATA

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.

Introduction- Basic Mathematical Notation

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 3

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 4

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 5

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 6

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Proof Techniques

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 7

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 8

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 9

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 10

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 11

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Finite Automata

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 12

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 13

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Finite Automata Model

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 14

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 15

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

What does an FA DO?

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 16

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 17

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Deterministic Finite Automata

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 18

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 19

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Constructing a DFA Example:

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 20

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Example DFA

NON DETERMINISTIC FINITE AUTOMATA

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 21

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

EXAMPLE

EXAMPLES OF NFA

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 22

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

NFA with ε Closure

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 23

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Another Example

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 24

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Theorem

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 25

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 26

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 27

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 28

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 29

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 30

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 31

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 32

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 33

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Constructing NFA for regular expressions

Find a regular expression for a DFA

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 34

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Pumping Lemma

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 35

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

i i 1. Prove { 0 1 |i>= 0} is not regular.

i 2. Prove {1 |i is prime} is not regular.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 36

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

UNIT-II

GRAMMERS

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 37

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

UNIT II

GRAMMARS

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.

CONTEXT FREE GRAMMAR

Definition − A context-free grammar (CFG) consisting of a finite set of grammar rules is a quadruple (N, T, P, S) where

• N is a set of non-terminal symbols.

• T is a set of terminals where N ∩ T = NULL.

• P is a set of rules, P: N → (N ∪ T)*, i.e., the left-hand side of the production rule P does have any right context or left context.

• S is the start symbol.

Example

• The grammar ({A}, {a, b, c}, P, A), P : A → aA, A → abc.

• The grammar ({S, a, b}, {a, b}, P, S), P: S → aSa, S → bSb, S → ε

• The grammar ({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F | ε

Generation of Derivation Tree

A derivation tree or parse tree is an ordered rooted tree that graphically represents the semantic information a string derived from a context-free grammar.

Representation Technique

• Root vertex − Must be labeled by the start symbol.

• Vertex − Labeled by a non-terminal symbol.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 38

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

• Leaves − Labeled by a terminal symbol or ε.

If S → x1x2 …… xn is a production rule in a CFG, then the parse tree / derivation tree will

be as follows −

There are two different approaches to draw a derivation tree −

Top-down Approach −

• Starts with the starting symbol S

• Goes down to tree leaves using productions

Bottom-up Approach −

• Starts from tree leaves

• Proceeds upward to the root which is the starting symbol S

Derivation or Yield of a Tree

The derivation or the yield of a parse tree is the final string obtained by concatenating the labels of the leaves of the tree from left to right, ignoring the Nulls. However, if all the leaves are Null, derivation is Null.

Example

Let a CFG {N,T,P,S} be

N = {S}, T = {a, b}, Starting symbol = S, P = S → SS | aSb | ε

One derivation from the above CFG is “abaabb”

S → SS → aSbS → abS → abaSb → abaaSbb → abaabb

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 39

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Sentential Form and Partial Derivation Tree

A partial derivation tree is a sub-tree of a derivation tree/parse tree such that either all of its children are in the sub-tree or none of them are in the sub-tree.

Example

If in any CFG the productions are −

S → AB, A → aaA | ε, B → Bb| ε

the partial derivation tree can be the following −

If a partial derivation tree contains the root S, it is called a sentential form. The above sub- tree is also in sentential form.

Leftmost and Rightmost Derivation of a String

• Leftmost derivation − A leftmost derivation is obtained by applying production to the leftmost variable in each step.

• Rightmost derivation − A rightmost derivation is obtained by applying production to the rightmost variable in each step.

Example

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 40

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Let any set of production rules in a CFG be X → X+X | X*X |X| a over an alphabet {a}.

The leftmost derivation for the string "a+a*a" may be − X → X+X → a+X → a+ X*X → a+a*X → a+a*a

The rightmost derivation for the above string "a+a*a" may be −

X → X*X → X*a → X+X*a →X+a*a → a+a*a

The stepwise derivation of the above string is shown as below −

Left and Right Recursive Grammars

In a context-free grammar G, if there is a production in the form X → Xa whereX is a non-terminal and ‘a’ is a string of terminals, it is called a left recursive production. The grammar having a left recursive production is called a left .

And if in a context-free grammar G, if there is a production is in the form X → aX where X is a non-terminal and ‘a’ is a string of terminals, it is called a right recursive production. The grammar having a right recursive production is called a right recursive grammar.

Example

The sentence ( a + b ) * c has a unique leftmost derivation, a unique (different) rightmost derivation and the unique parse tree shown below:

Parse Tree: ( a + b ) * c

E E /|\ /|\ E * E / | \ /|\ \ / | \ ( E ) c / | \

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 41

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

/|\ / | \ E + E E | E | | /|\ | | a b / | \ | | / E \ | | / /|\ \ | | | / | \ || | | E | E || | | | | | || |

( a + b )* c

AMBIGUITY

If a context free grammar G has more than one derivation tree for some stringw •∈ L(G), it is called an ambiguous grammar. There exist multiple right-most or left-most derivations for some string generated from that grammar.

Problem

Check whether the grammar G with production rules −

X → X+X | X*X |X| a is ambiguous or not.

Solution

Let‟s find out the derivation tree for the string "a+a*a". It has two leftmost derivations.

Derivation 1 −

X → X+X → a +X → a+ X*X → a+a*X → a+a*a

Parse tree 1 −

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 42

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Derivation 2 −

X •→ X*X• → X+X*X• → a+ X*X •→a+a*X → a+a*a

Parse tree 2 −

As there are two parse trees for a single string "a+a*a", the grammar G is ambiguous.

For example, the very simple sentence a + b * c. The table looks at leftmost derivations and parse trees:

2nd Leftmost Der. 1st Leftmost Der.

E ===> E + E E ===> E * E ===> a + E ===> E + E * E ===> a + E * E ===> a + E * E

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 43

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

===> a + b * E ===> a + b * E ===> a + b * c ===> a + b * c

1st Parse Tree 2nd Parse Tree

E E /|\ /|\ / | \ / | \ E + E E * E | /|\ /|\ | | / | \ / | \ | a E * E E + E c | | | |

b c a b

For example, the above sentence a + b * c that caused a problem has the following leftmost derivation and parse tree on the left (along with the its twin a * b + c on the right):

Leftmost Derivations

a + b * c a * b + c

E ===> E + T E ===> E + T

===> T + T ===> T + T

===> F + T ===> T * F + T

===> a + T ===> F * F + T

===> a + T * F ===> a * F + T

===> a + F * F ===> a * b + T

===> a + b * F ===> a * b + F

===> a + b * c ===> a * b + c

1st Parse Tree 2nd Parse Tree

E E

/|\ /|\

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 44

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

/ | \ / | \ E + T E + T | /|\ | | T T * F T F | | | /|\ | F F c / | \ c | | T * F a b | | F b | a

SIMPLIFICATION OF CFG

In a CFG, it may happen that all the production rules and symbols are not needed for the derivation of strings. Besides, there may be some null productions and unit productions. Elimination of these productions and symbols is called simplification of CFGs. Simplification essentially comprises of the following steps −

• Reduction of CFG

• Removal of Unit Productions

• Removal of Null Productions

Reduction of CFG

CFGs are reduced in two phases −

Phase 1 − Derivation of an equivalent grammar, G’, from the CFG, G, such that each variable derives some terminal string.

Derivation Procedure −

Step 1 − Include all symbols, W , that derive some terminal and initialize i=1. 1 Step 2 − Include all symbols, W , that derive W . i+1 i Step 3 − Increment i and repeat Step 2, until Wi+1 = Wi.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 45

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Step 4 − Include all production rules that have Wi in it.

Phase 2 − Derivation of an equivalent grammar, G”, from the CFG, G’, such that each symbol appears in a sentential form.

Derivation Procedure −

Step 1 − Include the start symbol in Y1 and initialize i = 1.

Step 2 − Include all symbols, Yi+1, that can be derived from Yi and include all production rules that have been applied.

Step 3 − Increment i and repeat Step 2, until Yi+1 = Yi.

Problem

Find a reduced grammar equivalent to the grammar G, having production rules, P: S → AC | B, A → a, C → c | BC, E → aA | e

Solution

Phase 1 −

T = { a, c, e } W1 = { A, C, E } from rules A → a, C → c and E → aA W2 = { A, C, E } ∪ { S } from rule S → AC W3 = { A, C, E, S } ∪ ∅ Since W2 = W3, we can derive G‟ as −

G‟ = { { A, C, E, S }, { a, c, e }, P, {S}}

where P: S → AC, A → a, C → c , E → aA | e

Phase 2 −

Y1 = { S }

Y2 = { S, A, C } from rule S → AC Y3 = { S, A, C, a, c } from rules A → a and C → c Y4 = { S, A, C, a, c } Since Y3 = Y4, we can derive G” as −

G” = { { A, C, S }, { a, c }, P, {S}}

where P: S → AC, A → a, C → c

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 46

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Removal of Unit Productions Any production rule in the form A → B where A, B ∈ Non-terminal is called unit production.

Removal Procedure

Step 1 To remove A → B, add production A → x to the grammar rule wheneverB → x occurs in the grammar. [x ∈ Terminal, x can be Null]

Step 2 Delete A → B from the grammar.

Step 3 Repeat from step 1 until all unit productions are removed.

Problem

Remove unit production from the following −

S → XY, X → a, Y → Z | b, Z → M, M → N, N → a

Solution −

There are 3 unit productions in the grammar −

Y → Z, Z → M, and M → N

At first, we will remove M → N.

As N → a, we add M → a, and M → N is removed.

The production set becomes

S → XY, X → a, Y → Z | b, Z → M, M → a, N → a

Now we will remove Z → M.

As M → a, we add Z → a, and Z → M is removed.

The production set becomes

S → XY, X → a, Y → Z | b, Z → a, M → a, N → a

Now we will remove Y → Z.

As Z → a, we add Y → a, and Y → Z is removed.

The production set becomes

S → XY, X → a, Y → a | b, Z → a, M → a, N → a

Now Z, M, and N are unreachable, hence we can remove those.

The final CFG is unit production free −

S → XY, X → a, Y → a | b

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 47

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Removal of Null Productions

In a CFG, a non-terminal symbol ‘A’ is a nullable variable if there is a production A → ε or there is a derivation that starts at A and finally ends up with ε: A → ...... … → ε

Removal Procedure Find out nullable non-terminal variables which derive ∈.

Step 1 For each production A → a, construct all productions A → x where x is obtained Step 2 from ‘a’ by removing one or multiple non-terminals from Step 1.

Combine the original productions with the result of step 2 and remove∈-productions.

Step 3

Problem

Remove null production from the following − S → ASA | aB | b, A → B, B → b | ∈ Solution −

There are two nullable variables − A and B

At first, we will remove B → ε.

After removing B → ε, the production set becomes − S → ASA | aB | b | a, A → B| b | ∈, B → b

Now we will remove A → ε.

After removing A → ε, the production set becomes −

S → ASA | aB | b | a | SA | AS | S, A → B| b, B → b

This is the final production set without null transition. CHOMSKY NORMAL FORM

A CFG is in Chomsky Normal Form if the Productions are in the following forms −

• A → a

• A → BC

• S → ε

where A, B, and C are non-terminals and a is terminal.

Algorithm to Convert into Chomsky Normal Form −

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 48

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Step 1 If the start symbol S occurs on some right side, create a new start symbol S’ and a new production S’ → S.

Step 2 Remove Null productions. (Using the Null production removal discussed earlier)

Step 3 Remove unit productions. (Using the Unit production removal algorithm discussed earlier)

Step 4 Replace each production A → B1…Bn where n > 2 with A → B1C whereC →

B2 …Bn. Repeat this step for all productions having two or more symbols in the right side.

Step 5 If the right side of any production is in the form A → aB where a is a terminal and A, B are non-terminal, then the production is replaced byA → XB and X → a. Repeat this step for every production which is in the form A → aB.

Problem

Convert the following CFG into CNF S → ASA | aB, A → B | S, B → b | ∈

Solution (1) Since S appears in R.H.S, we add a new state S0 and S0 → S is added to the production set and it becomes −

(2) Now we will remove the null productions − B → ε and A → ε

After removing B → ε, the production set becomes −

(3) Now we will remove the unit productions.

After removing S → S, the production set becomes −

S0 → S, S → ASA | aB | a | AS | SA, A → B | S, B → b

After removing S0 → S, the production set becomes − S0 → ASA | aB | a | AS | SA, S → ASA | aB | a | AS | SA

A → B | S, B → b

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 49

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

After removing A → B, the production set becomes − S0 → ASA | aB | a | AS | SA, S → ASA | aB | a | AS | SA

A → S | b

B → b

After removing A → S, the production set becomes − S0 → ASA | aB | a | AS | SA, S → ASA | aB | a | AS | SA A → b |ASA | aB | a | AS | SA, B → b

(4) Now we will find out more than two variables in the R.H.S Here, S0 → ASA, S → ASA, A → ASA violates two Non-terminals in R.H.S.

Hence we will apply step 4 and step 5 to get the following final production set which is in CNF −

S0 → AX | aB | a | AS | SA

S → AX | aB | a | AS | SA

A → b |AX | aB | a | AS | SA

B → b

X → SA (5) We have to change the productions S0 → aB, S → aB, A → aB And the final production set becomes −

S0 → AX | YB | a | AS | SA

S → AX | YB | a | AS | SA

A → b |AX | YB | a | AS | SA

B → b

X → SA

Y → a

Greibach Normal Form

A CFG is in Greibach Normal Form if the Productions are in the following forms − A → b

A → bD1…Dn S → ε where A, D1,....,Dn are non-terminals and b is a terminal.

Algorithm to Convert a CFG into Greibach Normal Form

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 50

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Step 1 If the start symbol S occurs on some right side, create a new start symbol S’ and a new production S’ → S.

Step 2 Remove Null productions. (Using the Null production removal algorithm discussed earlier)

Step 3 Remove unit productions. (Using the Unit production removal algorithm discussed earlier)

Step 4 Remove all direct and indirect left-recursion.

Step 5 Do proper substitutions of productions to convert it into the proper form of GNF.

Problem

Convert the following CFG into CNF S → XY | Xn | p

X → mX | m

Y → Xn | o

Solution

Here, S does not appear on the right side of any production and there are no unit or null productions in the production rule set. So, we can skip Step 1 to Step 3.

Step 4

Now after replacing

X in S → XY | Xo | p with mX | m we obtain

S → mXY | mY | mXo | mo | p.

And after replacing X in Y → Xn | o with the right side of

X → mX | m we obtain

Y → mXn | mn | o.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 51

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Two new productions O → o and P → p are added to the production set and then we came to the final GNF as the following −

S → mXY | mY | mXC | mC | p

X → mX | m

Y → mXD | mD | o

O → o

P → p

Example:

CNF

S → XA | BB

B → b | SB

X → b

A → a

NEW LABELS

S = A1

X = A2

A = A3 B = A4

UPDATED CNF

A1 → A2A3 | A4A4

A4 → b | A1A4

A2 → b A3 → a

GNF

Consider the following example:

Here's the grammar:

S -> AA | 0

A -> SS | 1 first rename the variables:

A1 -> A2 A2 | 0

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 52

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

A2 -> A1 A1 | 1 three of the four productions are just fine, only A2 -> A1 A1 is a problem.

Apply algorithm 1:

A2 -> A2 A2 A1 | 0 A1

So, now we have still have one problem production:

A2 -> A2 A2 A1

Apply algorithm 2:

A2 -> 1 | 0 A1 | 1 B | 0 A1 B

B -> A2 A1 | A2 A1 B so now our grammar looks like:

A1 -> A2 A2 | 0

A2 -> 1 | 0 A1 | 1 B | 0 A1 B

B -> A2 A1 | A2 A1 B

Now we must fix A1, so that is only starts with terminals:

A1 -> 1 A2 | 0 A1 A2 | 1 B A2 | 0 A1 B A2 | 0

Then we must B in a similar fashion (replacing initial occurences of A2) B -> 1 A1 | 0 A1 A1 | 1 B A1 | 0 A1 B A1 | 1 A1 B | 0 A1 A1 B | 1 B A1 B | 0 A1 B A1 B and now we have the following grammar:

A1 -> 1 A2 | 0 A1 A2 | 1 B A2 | 0 A1 B A2 | 0

A2 -> 1 | 0 A1 | 1 B | 0 A1 B

B -> 1 A1 | 0 A1 A1 | 1 B A1 | 0 A1 B A1 | 1 A1 B | 0 A1 A1 B | 1 B A1 B | 0 A1 B A1 B which is in GNF.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 53

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

UNIT-III

PUSHDOWN AUTOMATA

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 54

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

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.

Basic Structure of PDA

A is a way to implement a context-free grammar in a similar way we design DFA for a . A DFA can remember a finite amount of information, but a PDA can remember an infinite amount of information.

Basically a pushdown automaton is −

"Finite state machine" + "a stack"

A pushdown automaton has three components −

• an input tape,

• a control unit, and

• A stack with infinite size.

• Push − a new symbol is added at the top.

• Pop − the top symbol is read and removed.

A PDA may or may not read an input symbol, but it has to read the top of the stack in every transition.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 55

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

A PDA can be formally described as a 7-tuple (Q, ∑, S, δ, q0, I, F) −

• Q is the finite number of states

• ∑ is input alphabet

• S is stack symbols * • δ is the transition function − Q × (∑ ∪ {ε}) × S × Q × S • q0 is the initial state (q0 ∈ Q) • I is the initial stack top symbol (I ∈ S) • F is a set of accepting states (F ∈ Q) The following diagram shows a transition in a PDA from a state q1 to state q2, labelled as a,b → c

This means at state q1, if we encounter an input string ‘a’ and top symbol of the stack is ‘b’, then we pop ‘b’, push ‘c’ on top of the stack and move to stateq2.

Terminologies Related to PDA

Instantaneous Description

The instantaneous description (ID) of a PDA is represented by a triplet (q, w, s) where

• q is the state • w is unconsumed input • s is the stack contents Turnstile Notation

The "turnstile" notation is used for connecting pairs of ID's that represent one or many moves of a PDA. The process of transition is denoted by the turnstile symbol "⊢". Consider a PDA (Q, ∑, S, δ, q0, I, F). A transition can be mathematically represented by the following turnstile notation −

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 56

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING (p, aw, Tβ) ⊢ (q, w, αb)

This implies that while taking a transition from state p to state q, the input symbol ‘a’ is consumed, and the top of the stack ‘T’ is replaced by a new string‘α’. Note − If we want zero or more moves of a PDA, we have to use the symbol(⊢*) for it. There are two different ways to define PDA acceptability.

Final State Acceptability

In final state acceptability, a PDA accepts a string when, after reading the entire string, the PDA is in a final state. From the starting state, we can make moves that end up in a final state with any stack values. The stack values are irrelevant as long as we end up in a final state.

* For a PDA (Q, ∑, S, δ, q0, I, F), the language accepted by the set of final statesF is − L(PDA) = {w | (q0, w, I) ⊢ (q, ε, x), q ∈ F} for any input stack string x.

Empty Stack Acceptability

Here a PDA accepts a string when, after reading the entire string, the PDA has emptied its stack.

* For a PDA (Q, ∑, S, δ, q0, I, F), the language accepted by the empty stack is − L(PDA) = {w | (q0, w, I) ⊢ (q, ε, ε), q ∈ Q}

Example

Construct a PDA that accepts L= {0n 1n | n ≥ 0}

Solution

This language accepts L = {ε, 01, 0011, 000111, ...... }

Here, in this example, the number of ‘a’ and ‘b’ have to be same.

• Initially we put a special symbol ‘$’ into the empty stack.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 57

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

• Then at state q2, if we encounter input 0 and top is Null, we push 0 into stack. This may iterate. And if we encounter input 1 and top is 0, we pop this 0.

• Then at state q3, if we encounter input 1 and top is 0, we pop this 0. This may also

iterate. And if we encounter input 1 and top is 0, we pop the top element.

• If the special symbol „$‟ is encountered at top of the stack, it is popped out and it finally goes to the accepting state q4.

Example

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 58

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Example

Construct a PDA that accepts L= { wwR | w = (a+b)* }

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 59

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Solution

Initially we put a special symbol „$‟ into the empty stack. At state q2, the w is being

read. In state q3, each 0 or 1 is popped when it matches the input. If any other input is given, the PDA will go to a dead state. When we reach that special symbol „$‟, we go to the

accepting state q4.

If a grammar G is context-free, we can build an equivalent nondeterministic PDA which accepts the language that is produced by the context-free grammarG. A parser can be built for the grammar G.

Also, if P is a pushdown automaton, an equivalent context-free grammar G can be constructed where L(G) = L(P).

In the next two topics, we will discuss how to convert from PDA to CFG and vice

versa.

Algorithm to find PDA corresponding to a given CFG

Input − A CFG, G= (V, T, P, S) Output − Equivalent PDA, P= (Q, ∑, S, δ, q0, I, F)

Step 1 Convert the productions of the CFG into GNF.

Step 2 The PDA will have only one state {q}.

Step 3 The start symbol of CFG will be the start symbol in the PDA.

Step 4 All non-terminals of the CFG will be the stack symbols of the PDA and all the terminals of the CFG will be the input symbols of the PDA.

Step 5 For each production in the form A → aX where a is terminal and A, Xare combination of terminal and non-terminals, make a transition δ (q, a, A).

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 60

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Problem

Construct a PDA from the following CFG.

G = ({S, X}, {a, b}, P, S)

where the productions are −

S → XS | ε , A → aXb | Ab | ab

Solution

Let the equivalent PDA,

P = ({q}, {a, b}, {a, b, X, S}, δ, q, S)

where δ −

δ (q, ε , S) = {(q, XS), (q, ε )}

δ(q, ε , X) = {(q, aXb), (q, Xb), (q, ab)}

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

δ(q, 1, 1) = {(q, ε )}

Algorithm to find CFG corresponding to a given PDA

Input − A CFG, G= (V, T, P, S)

Output − Equivalent PDA, P = (Q, ∑, S, δ, q0, I, F) such that the non- terminals of the grammar G will be {Xwx | w,x ∈ Q} and the start state will be Aq0,F.

Step 1 For every w, x, y, z ∈ Q, m ∈ S and a, b ∈ ∑, if δ (w, a, ε) contains (y, m) and (z, b, m) contains (x, ε), add the production rule Xwx → a Xyzb in grammar G.

Step 2 For every w, x, y, z ∈ Q, add the production rule Xwx → XwyXyx in grammar G.

Step 3 For w ∈ Q, add the production rule Xww → ε in grammar G.

Parsing is used to derive a string using the production rules of a grammar. It is used to check the acceptability of a string. Compiler is used to check whether or not a string is syntactically correct. A parser takes the inputs and builds a parse tree.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 61

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 62

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 63

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

NON DETERMINISTIC PUSH DOWN AUTOMATA

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 64

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DETERMINISTIC PUSH DOWN AUTOMATA

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 65

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

A parser can be of two types −

• Top-Down Parser − Top-down parsing starts from the top with the start-symbol and derives a string using a parse tree.

• Bottom-Up Parser − Bottom-up parsing starts from the bottom with the string and comes to the start symbol using a parse tree.

For top-down parsing, a PDA has the following four types of transitions −

• Pop the non-terminal on the left hand side of the production at the top of the stack and push its right-hand side string.

• If the top symbol of the stack matches with the input symbol being read, pop it.

• Push the start symbol „S‟ into the stack.

• If the input string is fully read and the stack is empty, go to the final state „F‟.

Example

Design a top-down parser for the expression "x+y*z" for the grammar G with the following production rules −

P: S → S+X | X, X → X*Y | Y, Y → (S) | id

Solution If the PDA is (Q, ∑, S, δ, q0, I, F), then the top-down parsing is − (x+y*z, I) ⊢ (x +y*z, SI) ⊢ (x+y*z, S+XI) ⊢ (x+y*z, X+XI) ⊢(x+y*z, Y+X I) ⊢ (x+y*z, x+XI) ⊢ (+y*z, +XI) ⊢ (y*z, XI) * ⊢ (y*z, X*YI) ⊢ (y*z, y*YI) ⊢ ( z,*YI) ⊢ (z, YI) ⊢ (z, zI) ⊢ (ε, I) Design of a Bottom-Up Parser

For bottom-up parsing, a PDA has the following four types of transitions −

• Push the current input symbol into the stack.

• Replace the right-hand side of a production at the top of the stack with its left-hand side.

• If the top of the stack element matches with the current input symbol, pop it.

• If the input string is fully read and only if the start symbol „S‟ remains in the stack, pop it and go to the final state „F‟.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 66

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Example

Design a top-down parser for the expression "x+y*z" for the grammar G with the following production rules −

P: S → S+X | X, X → X*Y | Y, Y → (S) | id

Solution If the PDA is (Q, ∑, S, δ, q0, I, F), then the bottom-up parsing is − (x+y*z, I) ⊢ (+y*z, xI) ⊢ (+y*z, YI) ⊢ (+y*z, XI) ⊢ (+y*z, SI) ⊢ (y*z, +SI) ⊢ (*z, y+SI) ⊢ (*z, Y+SI) ⊢ (*z, X+SI) ⊢ (z, *X+SI) ⊢(ε, z*X+SI) ⊢ (ε, Y*X+SI) ⊢ (ε, X+SI) ⊢ (ε, SI)

Pumping Lemma for CFLs

In any sufficiently long string in a CFL, it is possible to find at most two short, nearby sub- strings that we can “pump” i times in tandem, for any integer i, and the resulting string will still be in that language. Pumping lemma for CFLs: Let L be a CFL. Then there exists a constant n such that if z ∈ L with |z| ≥ n, then we can write z = uvwxy,

Subject to the following conditions:

1. |vwx| ≤ n.

2. vx 6= .

3. For all i ≥ 0, we have uviwxiy € L.

If the string z is sufficiently long, then the parse tree produced by z has a variable symbol that is repeated on a path from the root to a leaf. Suppose Ai = Aj, such that the overall parse tree has yield z = uvwxy, the subtree for root Aj has yield w, and the subtree for root Ai has yield vwx.

We can replace the subtree for root Ai with the subtree for root Aj, giving a tree with yield uwy (corresponding to the case i = 0), which also belongs to L.

We can replace the subtree for root Aj with the subtree for root Ai, giving a tree with yield uv2wx2y (corresponding to the case i = 2), which also belongs to L. Etc. Examples

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 67

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

While CFLs can match two sub-strings for (in) equality of length, they cannot match three such sub-strings.

m m m Example 1: Consider L = {0 1 2 | m ≥ 1}. Pick n of the pumping lemma. Pick z = 0n1n2n. Break z into uvwxy, with |vwx| ≤ n and vx 6=

. Hence vwx cannot involve both 0s and 2s, since the last 0 and the first 2 are at least n + 1 po-sitions apart. There are two cases:

• vwx has no 2s. Then vx has only 0s and 1s. Then uwy, which would have to be in L, has n 2s, but fewer than n 0s or 1s.

• vwx has no 0s. Analogous.

Hence L is not a CFL.

CFLs cannot match two pairs of sub-strings of equal lengths if the pairs interleave.

Example 2: Consider L = {0i1j2i3j | i, j ≥ 1}.

Pick n of the pumping lemma. Pick z = 0n1n2n3n. Break z into uvwxy, with |vwx| ≤ n and vx

6= . Then vwx contains one or two different sym-bols. In both cases, the string uwy cannot be in L.

CFLs cannot match two sub-strings of arbi-trary length over an alphabet of at least two symbols. Example 3: Consider L = {ww | w ∈ {0, 1}∗ }. Pick n of the pumping lemma. Pick z = 0n1n0n1n. In all cases, the string uwy cannot be in L.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 68

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

UNIT IV

TURING MACHINE

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 69

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

UNIT IV

TURING MACHINES

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.

A Turing Machine is an accepting device which accepts the languages (recursively enumerable set) generated by type 0 grammars. It was invented in 1936 by Alan Turing.

Definition

A Turing Machine (TM) is a mathematical model which consists of an infinite length tape divided into cells on which input is given. It consists of a head which reads the input tape. A state register stores the state of the Turing machine. After reading an input symbol, it is replaced with another symbol, its internal state is changed, and it moves from one cell to the right or left. If the TM reaches the final state, the input string is accepted, otherwise rejected.

A TM can be formally described as a 7-tuple (Q, X, ∑, δ, q0, B, F) where −

• Q is a finite set of states

• X is the tape alphabet

• ∑ is the input alphabet

• δ is a transition function; δ : Q × X → Q × X × {Left_shift, Right_shift}. • q0 is the initial state

• B is the blank symbol

• F is the set of final states

Comparison with the previous automation

The following table shows a comparison of how a Turing machine differs from Finite Automaton and Pushdown Automaton.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 70

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Machine Stack Data Structure Deterministic?

Finite Automaton N.A Yes

Pushdown Automaton Last In First Out(LIFO) No

Turing Machine Infinite tape Yes

Example of Turing machine Turing machine M = (Q, X, ∑, δ, q0, B, F) with

• Q = {q0, q1, q2, qf}

• X = {a, b}

• ∑ = {1}

• q0= {q0}

• B = blank symbol

• F = {qf }

• δ is given by −

Tape alphabet Present State Present State Present State symbol ‘q0’ ‘q1’ ‘q2’

a 1Rq1 1Lq0 1Lqf

b 1Lq2 1Rq1 1Rqf

Here the transition 1Rq1 implies that the write symbol is 1, the tape moves right, and the next state is q1. Similarly, the transition 1Lq2 implies that the write symbol is 1, the tape moves left, and the next state is q2.

Time and Space Complexity of a Turing Machine

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 71

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

For a Turing machine, the time complexity refers to the measure of the number of times the tape moves when the machine is initialized for some input symbols and the space complexity is the number of cells of the tape written.

Time complexity all reasonable functions −

T(n) = O(n log n)

TM's space complexity −

S(n) = O(n)

A TM accepts a language if it enters into a final state for any input string w. A language is recursively enumerable (generated by Type-0 grammar) if it is accepted by a Turing machine.

A TM decides a language if it accepts it and enters into a rejecting state for any input not in the language. A language is recursive if it is decided by a Turing machine.

There may be some cases where a TM does not stop. Such TM accepts the language, but it does not decide it.

Designing a Turing Machine

The basic guidelines of designing a Turing machine have been explained below with the help of a couple of examples.

Example 1

Design a TM to recognize all strings consisting of an odd number of α’s.

Solution

The Turing machine M can be constructed by the following moves − • Let q1 be the initial state. • If M is in q1; on scanning α, it enters the state q2 and writes B (blank). • If M is in q2; on scanning α, it enters the state q1 and writes B (blank). • From the above moves, we can see that M enters the state q1 if it scans an even

number of α’s, and it enters the state q2 if it scans an odd number of α’s. Hence q2 is

the only accepting state.

Hence, M = {{q1, q2}, {1}, {1, B}, δ, q1, B, {q2}} where δ is given by −

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 72

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Tape alphabet symbol Present State ‘q1’ Present State ‘q2’

a BRq2 BRq1

Example 2

Design a Turing Machine that reads a string representing a binary number and erases all leading 0‟s in the string. However, if the string comprises of only 0‟s, it keeps one 0.

Solution

Let us assume that the input string is terminated by a blank symbol, B, at each end of the string.

The Turing Machine, M, can be constructed by the following moves − • Let q0 be the initial state. • If M is in q0, on reading 0, it moves right, enters the state q1 and erases 0. On reading 1, it enters the state q2 and moves right. • If M is in q1, on reading 0, it moves right and erases 0, i.e., it replaces 0‟s by B‟s. On reaching the leftmost 1, it enters q2 and moves right. If it reaches B, i.e., the string comprises of only 0‟s, it moves left and enters the state q . 3 • If M is in q2, on reading either 0 or 1, it moves right. On reaching B, it moves left and enters the state q4. This validates that the string comprises only of 0‟s and 1‟s. • If M is in q3, it replaces B by 0, moves left and reaches the final stateqf. • If M is in q4, on reading either 0 or 1, it moves left. On reaching the beginning of the

string, i.e., when it reads B, it reaches the final state qf.

Hence, M = {{q0, q1, q2, q3, q4, qf}, {0,1, B}, {1, B}, δ, q0, B, {qf}} where δ is given by −

Tape

Present Present Present Present Present alphabet State ‘q0’ State ‘q1’ State ‘q2’ State ‘q3’ State ‘q4’

symbol

0 BRq1 BRq1 ORq2 - OLq4

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 73

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

1 1Rq2 1Rq2 1Rq2 - 1Lq4

B BRq1 BLq3 BLq4 OLqf BRqf

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 74

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Turing machine computable functions

A Turing machine computes a function f : Σ∗ → Σ∗ if, for any input word w, it always stops in a configuration where f(w) is on the tape. The functions that are computable by an effective procedure are those that are computable by a Turing machine

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 75

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 76

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 77

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 78

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 79

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 80

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 81

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 82

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 83

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 84

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 85

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Techniques for Turing Machine Construction

Designing a Turing machine to solve a problem is an interesting task. It is somewhat similar to programming. Given a problem, different Turing machines can be constructed to solve it. But we would like to have a Turing machine which does it in a simple and efficient manner. Like we learn some techniques of programming to deal with alternatives, loops etc, it is helpful to understand some techniques in Turing machine construction, which will help in designing simple and efficient Turing machines. It should be noted that we are using the word „efficient‟ in an intuitive manner here.

1. Considering the state as a tuple. In an earlier example we considered a Turing machine which makes a copy of a given string over Σ = {a, b}. After reading a „a‟, the machine remembers it by going to qa and after reading a „b‟, it goes to qb. In general we can represent the state as [q, x] where x ∈ Σ denoting that it has read a „x‟.

2. Considering the tape symbol as a tuple. Sometimes we may want to mark some symbols without destroying them or do some computation without destroying the input. In such cases it is advisable to have multiple tracks on the tape. This is equivalent to considering the tape symbol as a tuple.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 86

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

There is only one tape head. In the above figure there are three tracks. The head is pointing to a cell which contains A on the first track, B on the second track and C on the third track. The tape symbol is taken a 3-tuple [A, B, C]. Some computation can be done in one track by manipulating the respective component of the tape symbol. This is very useful in checking off symbols.

3. Checking off symbols. We use one track of the tape to mark that some symbols have been read without changing them.

Example Consider a Turing machine for accepting w#w#w, w ∈ {a, b}∗ A tape having two tracks is considered.

The first track contains the input. When the TM reads the first a in state q0, it stores it in its memory (by taking the state as a pair), checks off „a‟ by printing a √ in the second track below a, moves right and after the # symbol checks whether the symbol is a „a‟. If so marks it by putting a √ in the second track, moves right and again checks the first symbol after # is a „a‟ and if so it marks also. It then moves left and repeats the process with each unmarked leftmost symbols in each block. When all the symbols in the first block match with the second and third blocks, the machine halts accepting the string.

The mappings can be defined as follows:

δ(q0, [a, 6b]) = ([q, a], [a, √], R)

δ(q0, [b, 6b]) = ([q, b], [b, √], R)

The machine reads the leftmost symbol marks it and remembers whether it is a „a‟ or „b‟ by storing it in the state

δ([q, a], [a, 6b]) = ([q, a], [a, 6b], R)

δ([q, a], [b, 6b]) = ([q, a], [b, 6b], R)

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 87

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

δ([q, b], [a, 6b]) = ([q, b], [a, 6b], R)

δ([q, b], [b, 6b]) = ([q, b], [b, 6b], R)

The head passes through symbols in the first block to the right δ([q, a], [#, 6b]) = ([p, a], [#, 6b], R) δ([q, b], [#, 6b]) = ([p, b], [#, b], R)

When the head encounters a # in the first track, the first component of the state is changed to p. δ([p, a], [a, √]) = ([p, a], [a, √], R)

δ([p, a], [b, √]) = ([p, a], [b, √], R)

δ([p, b], [a, √]) = ([p, b], [a, √], R)

δ([p, b], [b, √]) = ([p, b], [b, √], R)

δ([p, a], [a, 6b]) = ([r, a], [a, √], R)

δ([p, b], [b, 6b]) = ([r, b], [b, √], R)

When it encounters a first unchecked symbol it marks it by putting a √ in the second track and changes the first component of the state to r.

δ([r, a], [a, 6b]) = ([r, a], [a, 6b], R)

δ([r, b], [a, 6b]) = ([r, b], [a, 6b], R)

δ([r, a], [b, 6b]) = ([r, a], [b, 6b], R)

δ([r, b], [b, 6b]) = ([r, b], [b, 6b], R)

The head moves through the second block without changing symbols, when the first component of the state is r

δ([r, a], [#, 6b]) = ([s, a], [#, 6b], R)

δ([r, b], [#, 6b]) = ([s, b], [#, 6b], R)

When it encounters a # in the first track it moves right into the third block changing the first component of the state to s

δ([s, a], [a, √]) = ([s, a], [a, √], R)

δ([s, a], [b, √]) = ([s, a], [b, √], R)

δ([s, b], [a, √]) = ([s, b], [a, √], R)

δ([s, b], [b, √]) = ([s, b], [b, √], R)

It moves right looking for the unchecked symbol δ([s, b], [b, 6b]) = (t, [b, √],L) δ([s, a], [a, 6b]) = (t, [a, √],L)

When it encounters an unchecked symbol in the third

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 88

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING it marks it by putting a √ in the second track and starts moving left.

δ(t, [a, √]) = (t, [a, √],L)

δ(t, [b, √]) = (t, [b, √],L)

δ(t, [#, √]) = (t0, [#, √],L)

It moves into the second block in state t0

δ(t0, [a, 6b]) = (t0, [a, 6b],L)

δ(t0, [b, 6b]) = (t0, [b, 6b],L)

δ(t0, [a, √]) = (t0, [a, √],L)

δ(t0, [b, √]) = (t0, [b, √],L)

It moves left in the second block.

δ(t0, [#, 6b]) = (t00, [#, b],L)

It moves left into the first block in state t00

δ(t00, [a, 6b]) = (t00, [a, 6b],L)

δ(t00, [b, 6b]) = (t00, [b, 6b],L)

It moves left in the first block through unchecked symbols. When it encounters a checked symbol it moves right in state q0 and the whole process repeats

δ(t00, [a, √]) = (q0, [a, √], R)

δ(t00, [b, √]) = (q0, [b, √], R)

This way the machine checks for same symbols in the first, second and third blocks.

Finishing When the machine encounters a # in the first track in state q0, it means it has checked all symbols in the first block. Now it has to check that there Introduction to Fare ormal Languages, no Automata more symbols in the second and third block.

δ(q0, [#, 6b]) = (q1, [#, 6b], R)

δ(q1, [a, √]) = (q1, [a, √], R)

δ(q1, [b, √]) = (q1, [b, √], R)

If it encounters an unchecked symbol, it halts without accepting δ(q1, [a, 6b]) = (qn, [a, 6b], R)

δ(q1, [b, 6b]) = (qn, [b, 6b], R) If it finds all symbols are checked in the second block, it moves to the third block in state q2

δ(q1, [#, 6b]) = (q2, [#, 6b], R) In the third block it checks whether all symbols have already been checked. If so, it halts in accepting state qy.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 89

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Otherwise halts in nonaccepting state qn

δ(q2, [a, √]) = (q2, [a, √], R)

δ(q2, [b, √]) = (q2, [b, √], R)

δ(q2, [a, 6b]) = (qn, [a, 6b], R)

δ(q2, [b, 6b]) = (qn, [b, 6b], R)

δ(q2, [6b, 6b]) = (qy, [6b, 6b], R)

If the input has more symbols in the first block (than second block) it moves in the second block in state [p, a] or [p, b] and encounters [#, 6b]. Then it halts rejecting the input

δ([p, a], [#, 6b]) = (qn, [#, 6b], R)

δ([p, b], [#, 6b]) = (qn, [#, 6b], R)

If the input has equal symbols in the first and second block but less symbols in the third block, the machine encounters [6b, 6b] an state [s, b] or [s, a] and halts without accepting

δ([s, a], [6b, 6b]) = (qn, [6b, 6b], R)

δ([s, b], [6b, 6b]) = (qn, [6b, 6b], R) Thus we find that having two tracks and using the second track to check off symbols is a useful technique. When we consider a single tape multitrack TM, we really take the tape symbol as a tuple. This need not be considered as a variation of Turing machine.

4. Shifting over.

Sometimes we may have to shift symbols on the tape to the right or left to allow for some symbols to be written.

Suppose the contents of the tape are a1 . . . ai−1Aai+1 . . . an at some instant. A has to be replaced by abcd say. Then ai+1 . . . an have to be shifted three cells to the right and then in the space created abcd can be printed. We can use the state as a tuple to store some information and shift symbols. Suppose the head is reading ai+1 in state q and the shifting process has to start. Then the TM reads ai+1 and goes to a state [q, −, −, ai+1] and prints X over ai.

The ID

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 90

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 91

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

In q0 it moves left till it finds AXXX and replaces it by abcd. A similar method can be used for shifting symbols to the left. Thus storing some information in some components of the state and cyclically moving the components helps in the technique of shifting off symbols.

5. Subroutines.

Just as a computer program has a main procedure and subroutines, the TM can also be programmed to have a main TM and TMs which serve as subroutines. Suppose we have to make n copies of a word w. Input is #w# and the output is #w# w| ww{z. . . w} n times . In this case we can write the mappings for a TM Msub which when started on #w#x ends up with #w#xw. The main TM will call this Msub x times.

Multitape Turing Machine

Multi-tape Turing Machines have multiple tapes where each tape is accessed with a separate head. Each head can move independently of the other heads. Initially the input is on tape 1 and others are blank. At first, the first tape is occupied by the input and the other tapes are kept blank. Next, the machine reads consecutive symbols under its heads and the TM prints a symbol on each tape and moves its heads.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 92

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

A Multi-tape Turing machine can be formally described as a 6-tuple (Q, X, B, δ, q0, F) where −

• Q is a finite set of states

• X is the tape alphabet

• B is the blank symbol

• δ is a relation on states and symbols where

δ: Q × Xk → Q × (X × {Left_shift, Right_shift, No_shift k }) where there is k number of tapes • q0 is the initial state

• F is the set of final states

Note − Every Multi-tape Turing machine has an equivalent single-tape Turing machine. Multi Head Turing Machine

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 93

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 94

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 95

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Halting Problem

Halting Problem is Undecidable

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 96

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Example Problems on Turing Machine

Example 1

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 97

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 98

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 99

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Chomsikan Hierarchy of Languages

According to Noam Chomosky, there are four types of grammars − Type 0, Type 1, Type 2, and Type 3. The following table shows how they differ from each other −

Grammar Language Grammar Accepted Automaton Type Accepted

Recursively Turing Type 0 enumerable Machine language

Context- Linear- Type 1 Context-sensitive grammar sensitive bounded language automaton

Context-free Pushdown Type 2 Context-free grammar language automaton

Regular Finite state Type 3 Regular grammar language automaton

Take a look at the following illustration. It shows the scope of each type of grammar −

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 100

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Type - 3 Grammar

Type-3 grammars generate regular languages. Type-3 grammars must have a single non- terminal on the left-hand side and a right-hand side consisting of a single terminal or single terminal followed by a single non-terminal.

The productions must be in the form X → a or X → aY where X, Y ∈ N (Non terminal) and a ∈ T (Terminal)

The rule S → ε is allowed if S does not appear on the right side of any rule.

Example

X → ε

X → a

X → aY

Type - 2 Grammar

Type-2 grammars generate context-free languages. The productions must be in the form A → γ where A ∈ N (Non terminal) and γ ∈ (T∪N)* (String of terminals and non-terminals).

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 101

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

These languages generated by these grammars are be recognized by a non-deterministic pushdown automaton.

Example

S → X a, X → a, X → Ax, X → abc, X → ε

Type - 1 Grammar

Type-1 grammars generate context-sensitive languages. The productions must be in the form

α A β → α γ β where A ∈ N (Non-terminal) and α, β, γ ∈ (T ∪ N)* (Strings of terminals and non-terminals) The strings α and β may be empty, but γ must be non-empty.

The rule S → ε is allowed if S does not appear on the right side of any rule. The languages generated by these grammars are recognized by a .

Example

AB → AbBc

A → bcA

B → b

Type - 0 Grammar

Type-0 grammars generate recursively enumerable languages. The productions have no restrictions. They are any phase structure grammar including all formal grammars. They generate the languages that are recognized by a Turing machine.

The productions can be in the form of α → β where α is a string of terminals and non- terminals with at least one non-terminal and α cannot be null. β is a string of terminals and non-terminals.

Example

S → ACaB

Bc → acB

CB → DB aD → Db

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 102

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

UNIT V

UNSOLVABLE PROBLEMS AND

COMPUTABLE FUNCTIONS

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 103

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

UNIT V

UNSOLVABLE PROBLEMS AND COMPUTABLE FUNCTIONS

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

Decidability

A language is called Decidable or Recursive if there is a Turing machine which accepts and halts on every input string w. Every decidable language is Turing-Acceptable.

A decision problem P is decidable if the language L of all yes instances to P is decidable. For a decidable language, for each input string, the TM halts either at the accept or the reject state as depicted in the following diagram −

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 104

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Example 1

Find out whether the following problem is decidable or not − Is a number „m‟ prime?

Solution

Prime numbers = {2, 3, 5, 7, 11, 13, …………..}

Divide the number ‘m’ by all the numbers between „2‟ and ‘√m’starting from „2‟.

If any of these numbers produce a remainder zero, then it goes to the “Rejected state”, otherwise it goes to the “Accepted state”. So, here the answer could be made by „Yes‟ or „No‟.

Hence, it is a decidable problem.

Example 2 Given a L and string w, how can we check if w ∈ L?

Solution

Take the DFA that accepts L and check if w is accepted

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 105

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Some more decidable problems are −

• Does DFA accept the empty language? • Is L1 ∩ L2=∅ for regular sets?

• If a language L is decidable, then its complement L' is also decidable.

• If a language is decidable, then there is an enumerator for it.

Undecidable Language

For an undecidable language, there is no Turing Machine which accepts the language and makes a decision for every input string w (TM can make decision for some input string though). A decision problem P is called “undecidable” if the language L of all yes instances to P is not decidable. Undecidable languages are not recursive languages, but sometimes, they may be recursively enumerable languages.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 106

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Example

• The halting problem of Turing machine

• The mortality problem

• The mortal matrix problem

• The Post correspondence problem, etc.

Recursively Enumerable Languages

Recursive Languages

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 107

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Relationship between

Recursively Enumerable and Recursive Languages

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 108

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Decision Problems

Decidable or Solvable problems

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 109

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Self Accepting

NSA is not recursively Enumerable

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 110

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SA is recursively enumerable

SA is not recursive

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 111

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 112

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Reduction

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 113

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Properties of Reduction

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 114

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Using reduction to prove decidability

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 115

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Universal Turing Machine

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 116

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Encoding Function

Example of Encode TM

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 117

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Universal Turing Machine

How UTM works

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 118

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Rice’s Theorem

Theorem

L= { | L (M) ∈ P} is undecidable when p, a non-trivial property of the Turing machine, is undecidable.

If the following two properties hold, it is proved as undecidable − Property 1 − If M1 and M2 recognize the same language, then either ∈ L or ∉ L

Property 2 − For some M1 and M2 such that ∈ L and ∉ L Let there are two Turing machines X1 and X2. Let us assume ∈ L such that L(X1) = φ and ∉ L. For an input „w‟ in a particular instant, perform the following steps − • If X accepts w, then simulate X2 on x.

• Run Z on input .

• If Z accepts , Reject it; and if Z rejects , accept it.

If X accepts w, then L(W) = L(X ) and ∉ P 2 If M does not accept w, then L(W) = L(X1) = φ and ∈ P

Here the contradiction arises. Hence, it is undecidable.

POST CORRESPONDENCE PROBLEM (PCP)

The Post Correspondence Problem (PCP), introduced by Emil Post in 1946, is an undecidable decision problem. The PCP problem over an alphabet ∑ is stated as follows − Given the following two lists, M and N of non-empty strings over ∑ −

M= (x1, x2, x3,………, xn)

N= (y1, y2, y3,………, yn)

We can say that there is a Post Correspondence Solution, if for some i1, i2,………… ik, where 1 ≤ ij≤ n, the condition xi1…….xik = yi1....yik satisfies.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 119

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Example

Find whether the lists

M = (abb, aa, aaa) and N = (bba, aaa, aa) have a Post Correspondence Solution?

Solution

x1 x2 x3

M Abb aa aaa

N Bba aaa aa

Here,

x2x1x3 = ‘aaabbaaa’ and y2y1y3 = ‘aaabbaaa’

We can see that x2x1x3 = y2y1y3

Hence, the solution is i=2, j =1, and k=3.

Example 2

Find whether the lists M = (ab, bab, bbaaa) and N = (a, ba, bab) have a Post Correspondence Solution?

Solution

x1 x2 x3

M ab bab bbaaa

N a ba bab

In this case, there is no solution because –

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 120

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

| x2x1x3 | ≠ | y2y1y3 | (Lengths are not same)

Hence, it can be said that this Post Correspondence Problem is undecidable.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 121

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 122

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

P, NP-Complete, NP, and NP-Hard

NP problems have their own significance in programming, but the discussion becomes quite hot when we deal with differences between NP, P , NP-Complete and NP-hard.

P and NP- Many of us know the difference between them.

P- Polynomial time solving . Problems which can be solved in polynomial time, which take time like O(n), O(n2), O(n3). Eg: finding maximum element in an array or to check whether a string is palindrome or not. so there are many problems which can be solved in polynomial time.

NP- Non deterministic Polynomial time solving. Problem which can't be solved in polynomial time like TSP( travelling salesman problem) or An easy example of this is subset sum: given a set of numbers, does there exist a subset whose sum is zero?. but NP problems are checkable in polynomial time means that given a solution of a problem

, we can check that whether the solution is correct or not in polynomial time. So till now you have got what is NP and what is P.

Now we will discuss about NP-Complete and NP- hard. but first we need to know what is reducibility .

Take two problems A and B both are NP problems.

Reducibility- If we can convert one instance of a problem A into problem B (NP problem) then it means that A is reducible to B.

NP-hard-- Now suppose we found that A is reducible to B, then it means that B is at least as hard as A.

NP-Complete -- The group of problems which are both in NP and NP-hard are known as NP-Complete problem.

Now suppose we have a NP-Complete problem R and it is reducible to Q then Q is at least as hard as R and since R is an NP-hard problem. therefore Q will also be at least NP-hard , it may be NP-complete also.

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 123

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Polynomial Time Reduction

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 124

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 125

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 126

VELTECH HIGHTECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SEMESTER: V CS6503-THEORY OF COMPUTATION

VELTECH HIGHETCH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE Page 127