Theory of Computation 1
Theory of Computation 1
(Computer Science)
SEMESTER - III (CBCS)
THEORY OF COMPUTATION
: Prof.Smt.Rohini Daund
K.R.T. Arts, B.H. Commerce and
A.M. Science (K.T.H.M.) College, Nashik
Unit- I
1. Automata Theory 01
2. Formal Languages 14
Unit - II
3. Regular Sets and Regular Grammar 22
4. Context-free Languages 42
5. Pushdown Automata 67
Unit - III
6. Linear Bound Automata 85
7. Turing Machines 94
8. Undecidability 112
SEMESTER III
THEORY
Textbook(s):
1) Theory of Computer Science, K. L. P Mishra, Chandrasekharan, PHI,3rd Edition
2) Introduction to Computer Theory, Daniel Cohen, Wiley,2nd Edition
3) Introductory Theory of Computer Science, E.V. Krishnamurthy,Affiliated East-West Press.
Additional Reference(s):
1) Theory of Computation, Kavi Mahesh, Wiley India
2) Elements of The Theory of Computation, Lewis, Papadimitriou, PHI
3) Introduction to Languages and the Theory of Computation, John E Martin, McGraw-Hill
Education
4) Introduction to Theory of Computation, Michel Sipser, Thomson
Unit I
1
AUTOMATA THEORY
Unit Structure
1.0 Objectives
1.1 Introduction
1.2 Definition of an Automaton
1.3 Finite Automaton
1.4 Transition Systems
1.4.1 Properties of Transition Systems
1.5 Acceptability by Finite Automaton
1.6 Non-deterministic Finite StateMachines
1.7 DFA and NDFA equivalence
1.8 Mealy and Moore Machines
1.8.1 Converting Mealy model to Moore model
1.8.2 Converting Moore model to Mealy model
1.9 Minimizing Automata
1.10 Summary
1.0 OBJECTIVES
After going through this chapter, the learner will be able to:
1.1 INTRODUCTION
Automata theory is the study of abstract machines and computational
problems. It is a theory in theoretical computer science and the
1
Theory of Computation word automata originate from a Greek word that means "self-acting, self-
willed, self-moving". In this chapter, we will discuss much in detail about
what is an automaton, its types, and conversion among the types.
(i) Input: It indicates the value taken from the input alphabet and
applied to the automaton at the discrete instant of time. Represented as
I1, I2, …., In.
(ii) Output: It refers to the response of the automaton based on the input
taken. Represented as O1, O2, …., On.
(iii) State: At a specific instant of time, the automaton can take any state.
Represented as q1, q2, …., qn
(iv) State Relation: It refers to the next state of automaton based on the
current state and current input.
(v) Output Relation: The output is related to either state only or boththe
input and the state.
Note:
Automaton with memory: Output of automaton depends only on input
Automaton without memory: Output of automaton depends on input as
well as states
2
(ii) = finite non empty set of input symbols (input alphabet) Automata Theory
(iii) = direct transition function (a function that maps Q× into Q which
describes the change of states)
(iv) q0= initial state
Symbol Description
Start state
Intermediate state
Final state
State\Input 0 1
q0 q2 q1
q1 q3 q0
q2 q0 q3
*q3 q1 q2
Note: → near the state represents “start” state and * represents the final
state
3
Theory of Computation Solution: The automaton is represented using the transition diagram given
below,
0010 is an accepted string whereas 101 is not an accepted string. For any
string to be accepted, it should commence from the start state (vertex) and
end atthe final state (vertex).
1.4.1 Properties of Transition Systems
Property 1: The state of thesystem can be changed only by an input
symbol.
Property 2: For all strings xand input symbols b,
State\Input 0 1
q0 q2 q1
q1 q3 q0
q2 q0 q3
*q3 q1 q2
4
Which among the following string is/are acceptable? Automata Theory
(i) 101010 (ii) 11100
Solution:
The automaton is represented using the transition diagram given below,
(i) 101010
Since the final state q3 is reached at the end of the string, the string
“101010” is acceptable by the finite automaton.
Note: The ↓ indicates the current input symbol which is in the process by
the automaton.
(ii) 11100
5
Theory of Computation Since the state q1 is not a final state which was reached at the end of the
string, the string “11100” is not acceptable by the finite automaton.
(iii) = direct transition function (a function that maps Q× into Q which
describes the change of states)
(iv) q0 = initial state
6
The transition state table for Figure 1.6 is given below, Automata Theory
State\Input 0 1
q0 {q1, q2} -
*q1 q0 q2
q2 - {q1, q2}
Note: The above transition state table for and NDFA contains a subset of
states at {q0,0} and {q2, 1}
• For one input symbol, NDFA can have zero, one, or more than one
move for a specific state.
Consider a non-deterministic finite automaton (NDFA) is represented
using 5-tuple such as (Q, , , q0, F) and deterministic finite automaton
(DFA) is represented using 5-tuple such as (Q’, , , q0, F’). The steps for
converting an NDFA to DFA are listed below.
Step 1: Initially let Q’ be an empty set
Step 2: Add the start vertex q0 to Q’
Step 3: For every state which is added to Q’, find the possible set of states
for each input symbol with the help of the transition state table. Two of the
following actions are to be performed.
• If the state is a new entrant, then add it to Q’
• If the state is not a new entrant, then ignore
Step 4: Repeat Step 3 until no new states are met
Step 5: All the states that contain the final state of NDFA are marked as
the final state.
Example 1.7.1 Construct a DFA from the NFA given below.
State\Input 0 1
q0 {q1, q3} {q2, q3}
q1 q1 q3
q2 q3 q2
*q3 - -
7
Theory of Computation Solution:
Step 1: Initially let Q’ be an empty set
State\Input 0 1
- - -
Step 2: Add the start vertex q0 to Q’
State\Input 0 1
q0 {q1, q3} {q2, q3}
Step 3: For every state which is added to Q’, find the possible set of states
for each input symbol with the help of the transition state table. Two of the
following actions are to be performed.
State\Input 0 1
q0 {q1, q3} {q2, q3}
{q1, q3} q1 q3
State\Input 0 1
q0 {q1, q3} {q2, q3}
{q1, q3} q1 q3
{q2, q3} q3 q2
New state 3: q1
State\Input 0 1
q0 {q1, q3} {q2, q3}
{q1, q3} q1 q3
{q2, q3} q3 q2
q1 q1 q3
8
New state 4: q2 Automata Theory
State\Input 0 1
q0 {q1, q3} {q2, q3}
{q1, q3} q1 q3
{q2, q3} q3 q2
q1 q1 q3
q2 q3 q2
New state 5: q3
State\Input 0 1
q0 {q1, q3} {q2, q3}
{q1, q3} q1 q3
{q2, q3} q3 q2
q1 q1 q3
q2 q3 q2
*q3 - -
Step 5: All the states that contain the final state of NDFA are marked as
the final state.
State\Input 0 1
q0 {q1, q3} {q2, q3}
*{q1, q3} q1 q3
*{q2, q3} q3 q2
q1 q1 q3
q2 q3 q2
*q3 - -
9
Theory of Computation Solution: The transition state table is generated as,
State\Input 0 1
q0 {q0, q1} {q0}
q1 - q2
*q2 - -
State\Input 0 1
- - -
State\Input 0 1
q0 {q0, q1} {q0}
Step 3: For every state which is added to Q’, find the possible set of states
for each input symbol with the help of the transition state table. Two of the
following actions are to be performed.
State\Input 0 1
q0 {q0, q1} {q0}
{q0, q1} {q0, q1} {q0, q2}
State\Input 0 1
q0 {q0, q1} {q0}
{q0, q1} {q0, q1} {q0, q2}
{q0, q2} {q0, q1} {q0}
10
Step 5: All the states that contain the final state of NDFA are marked as Automata Theory
the final state.
State\Input 0 1
q0 {q0, q1} {q0}
{q0, q1} {q0, q1} {q0, q2}
*{q0, q2} {q0, q1} {q0}
Next State
Present State Input 0 Input 1
State Output State Output
q1 q4 0 q2 1
q2 q1 1 q4 0
q3 q3 1 q3 0
*q4 q2 0 q1 1
For an input string “1011”, the transition states are given by q1 ->q2 ->q1 ->
q2 -> q4. The output string is “1110”
Consider the example given below where the Moore machine is
represented using the transition state table.
Next State
Present State Output
Input 0 Input 1
q1 q4 q2 1
q2 q1 q4 0
q3 q3 q3 0
*q4 q2 q1 1
For an input string “1011”, the transition states are given by q1 ->q2 ->q1 ->
q2 -> q4. The output string is “1010”
11
Theory of Computation
1.9 MINIMIZING AUTOMATA
Suppose there is a DFA (Q, , , q0, F) which recognizes a language
LThen the minimized DFA (Q’, , , q0, F’) can be constructed for
language L as:
Step 1: We will divide Q (set of states) into two sets.
One set will contain all final states and the other set will contain non-final
states.
Step2: Initialize k = 1
Step 3: Find Qk by partitioning the different sets of Qk-1. In each set of Qk-
1, take all possible pairs of states. If two states of a set are distinguishable,
we will split the sets into different sets in Qk.
Step 4: Stop when Qk = Qk-1 (No change in the partition)
Step 5: All states of one set are merged into one. No. of states in
minimized DFA will be equal to no. of sets in Qk.
Consider the example of DFA given below:
Step1. Q0 will have two sets of states. One set will contain the final states
of DFA and another set will contain the remaining states. So,Q0 =
{{q1,q2,q4}, {q0, q3, q5}}.
Step 2. To calculate Q1, check whether sets of partition Q0 can be
partitioned or not.
i) For set { q1, q2, q4} :
Since q1 and q2 are not distinguishable and q1 and q4 are also not
distinguishable, So q2 and q4 are not distinguishable. So, { q1, q2, q4} set
will not be partitioned in Q1.
1.10 SUMMARY
• Automata theory is the study of abstract machines and computational
problems.
• Automaton refers to a system that transforms and transmits the
information which is used for some action without any human
intervention.
• A finite automaton is represented using 5-tuple such as (Q, , , q0, F).
• An automaton can be represented diagrammatically through a
transition system, also called a transition graph.
• A string (finite sequence of symbols) is said to be acceptable if and
only if the last state is a final state.
• In non-deterministic finite state machines, the next state of automaton
cannot be precisely defined which means that for the same input
symbol there may exist a different next state.
• A model in which the output function depends both on present state qi
and input value xi is called a Mealy machine.
• A model in which the output function depends only on present state qi
and is independent of input value xi is called a Moore machine.
❖❖❖❖
13
2
FORMAL LANGUAGES
Unit Structure
2.0 Objectives
2.1 Introduction
2.2 Defining Grammar
2.3 Derivations and Languages generated by Grammar
2.3.1 Derivations generated by Grammar
2.3.2 Languages generated by Grammar
2.4 Chomsky Classification of Grammar and Languages
2.5 Languages and Their Relations
2.6 Recursive Enumerable Sets
2.7 Operations on Languages
2.8 Languages and Automata
2.9 Summary
2.0 OBJECTIVES
After going through this chapter, the learner will be able to:
2.1 INTRODUCTION
Linguists were trying in the early 1950s to define preciselyvalid sentences
and give structural descriptions of sentences. They wanted todefine formal
grammar (i.e. to describe the rules of grammar in a rigorousmathematical
way) to describe English. Itwas Noam Chomsky who gave a mathematical
model of grammar in 1956.Although it was not useful for describing
natural languages such as English, itturned alit to be useful for computer
languages.
14
2.2 DEFINING GRAMMAR Formal Languages
1. S ⇒ ab
2. S ⇒bb
3. S ⇒aba
4. S ⇒aab
Hence, the language generated by G, L(G) = {ab, bb, aba, aab}.
xαy⇒Gxβy
15
Theory of Computation Let us consider the grammar,
G = ({S, A}, {a, b}, S, {S → aAb, aA → aaAb, A → ε })
Some of the strings that can be derived are,
L(G)={W|W ∈ ∑*, S ⇒G W}
If L(G1) = L(G2), the Grammar G1 is equivalent to the Grammar G2.
Suppose we have the following grammar,
G = {S, A, B} T = {a, b} P = {S → AB, A → aA|a, B → bB|b}
The language generated by this grammar L(G) = {ab, a2b, ab2,
a2b2, ………}
= {am bn | m ≥ 1 and n ≥ 1}
16
Formal Languages
where A ∈ N (Non-terminal)
17
Theory of Computation Example 2.4.2
S→Aa
A→a
A → aA
A → abc
A→ε
Type - 1 Grammar
Type-1 grammars generate context-sensitive languages.
The productions must be in the form, α A β → α γ β
where A ∈ N (Non-terminal)
Regular Languages are the most restricted types of languages and are
accepted by finite automata. Regular Expressions are used to denote
regular languages. An expression is regular if,
2. Intersection
If L1 and If L2 are two regular languages, their intersection L1 ∩ L2 will
also be regular.
For example,
L1= {am bn | n ≥ 0 and m ≥ 0} and L2= {am bn ∪ bn am | n ≥ 0 and
m ≥ 0}
L3 = L1 ∩ L2 = {am bn | n ≥ 0 and m ≥ 0} is also regular.
19
Theory of Computation 3. Concatenation
If L1 and If L2 are two regular languages, their concatenation L1.L2 will
also be regular.
For example,
L1 = {an | n ≥ 0} and L2 = {bn | n ≥ 0}
L3 = L1.L2 = {am . bn | m ≥ 0 and n ≥ 0} is also regular.
4. Kleene Closure
If L1 is a regular language, its Kleene closure L1* will also be regular.
For example,
L1 = (a ∪ b)
L1* = (a ∪ b)*
5. Complement
If L(G) is a regular language, its complement L’(G) will also be regular.
The complement of a language can be found by subtracting strings that are
in L(G) from all possible strings.
For example,
L(G) = {an | n > 3}
L’(G) = {an | n <= 3}
2.9 SUMMARY
• A grammar is a quadruple G = (N, Σ, P, S)
• Strings may be derived from other strings using the productions in
grammar.
• The set of all strings that can be derived from a grammar is said to be
the language generated from that grammar.
20
• According to Noam Chomsky, there are four types of grammar such Formal Languages
as Type 0 (Unrestricted grammar), Type 1 (Context-sensitive
grammar), Type 2 (Context-free grammar), and Type 3 (Regular
grammar)
• Regular Languages are the most restricted types of languages and are
accepted by finite automata.
• Regular Expressions are used to denote regular languages.
• Recursive Enumerable languages or type-0 languages are generated
by type-0 grammars.
• Union, Intersection, Concatenation, Kleene Closure, Complement are
some of the operations on languages.
❖❖❖❖
21
Unit II
3
REGULAR SETS AND REGULAR
GRAMMAR
Unit Structure
3.0 Objectives
3.1 Introduction
3.2 Regular Grammar
3.3 Regular Expressions
3.3.1 Regular Expressions Identities
3.3.2 Regular Language Definition and Examples
3.4 Finite automata and Regular Expressions
3.5 Pumping Lemma and its Applications
3.6 Closure Properties
3.7 Regular Sets and Regular Grammar
3.8 Summary
3.9 References
3.10 Review Questions
3.0 OBJECTIVES
After the end of this unit, Students will be able to:
22
3.1 INTRODUCTION Regular Sets and Regular
Grammar
In this chapter we are learn the concept of the Regular Expression. We
first describe regular expressions as a means of representing subsets of
strings over ∑ and prove that regular sets are exactlythose accepted by
finite automata (FA) or transition systems. We study about pumping
lemma for regularLanguages to prove that certain Languages are not
regular. Then we studyclosure properties of regular sets. At the and we
discuss the relation between regular sets and regular grammars
23
Theory of Computation 3.3 REGULAR EXPRESSIONS
The regular expressions are useful for representing certain sets of strings
in an Algebraic fashion. Here, these describe the languages accepted by
finite state automata. We give a formal recursive definition of regular
expressions over ∑ as follows:
1. Any terminal symbol (i.e. an element of ∑), ε and Ø are regular
expressions. When we view a in ∑ as a regular expression, we denote
it by a.
2. The union of two regular expressions R, and R, written as R1 + R2,is
also a regular expression.
3. The concatenation of two regular expressions Rj and R2, written as Rj
R2, is also a regular expression.
4. The iteration (or closure) of a regular expression R written as R*, is
also a regular expression. 5. If R is a regular expression, then (R) is
also a regular expression.
5. The regular expressions over ∑are precisely those obtained
recursively by the application of the rules 1-5 once or several times.
Notes:
1. We use x for a regular expression just to distinguish it from the
symbol (or string) x.
2. The parentheses used in Rule 5 influence the order of evaluation of a
regular expression.
3. In the absence of parentheses, we have the hierarchy of operations as
follows: iteration (closure), Concatenation, and union. That is, in
evaluating a regular expression involving various operations.
3.3.1 Regular Expression Identities
Two regular expressions P and Q are equivalent, then it is written as P = Q
if P and Q represent thesame set of strings.
Following are the identities for Regular Expression:
i. Associativity and Commutativity
1. P+Q=Q +P
2. (P + Q) + N=P+(Q+N)
3. (PQ)N = P(QN)
Here note that PM ≠ MP
ii. Distributivity
1. P(M+N)=PM+PN
2. (M + N)P = MP + NP
24
iii. Identities and Anhilators Regular Sets and Regular
Grammar
1. Ø +P=P+ Ø =P
2. εP = P ε=P
3. ØP=PØ=Ø
iv. Idempotent Law
1. P+P=P
v. Closure Laws
1. (P*)* =P*
2. (Ø)* = ε
3. ε*=ε
4. P+ = PP* = P*P
5. P* = P++ ε
6. P* P* = P*
7. (PQ) * P= P(QP) *
8. (P+Q) *=(P*Q*)*=(L*+M*)*
3.3.2 Regular Language Definition and Examples
Definition:“The languages that are associated with regular expressions are
called languages and are also said to be defined by finite representation.”
Examples:
Q.1 Describe the language defined by the regular expression r=ab*a
Solution:
It is the set of all strings of a's and b's that have at least two letters, that
beginand end with a's, and that have nothing but b'sinside (if anything at
all) language.
L = a, b, c, d
So, from above strings Regular Expression represents Language L
consisting of
Stringsover {a, b}
Q.3 Find out regular language of Regular Expression a*+ b+ + c + d
Solution:
To find out language for Regular Expression = a*+ b+ + c + d
We can take all possible strings from the given expression
Consider, r = a* + b+ + c + d
For our understanding break the given regular expression into parts
Then, the possible strings in language are
a* = ε , a , aa, aaa,…….
a* = ε , a, aa, aaa,…..
26
a*b = b, ab, aab, aaab,…… Regular Sets and Regular
Grammar
c+ = c, cc, ccc, cccc,………
r= (a + b) * a (a + b) *
Q.6 Find out a regular expression for a language L over ∑ *, where ∑ =
{0, 1} such thatevery string in the language begin and end with either 00
or 11.
Solution:
Here given that string must begin and end with either 00 or 11 so we will
denote regularexpression as (00+ 11). Now in between zero or more
occurrences of 0 or 1 is valid so we can denote regular expression (0 +
1)*. After concatenating this we will get regular expression which
represents given language Lr=(00 + 11) (0+1)* (00+ 11)
q0
2. r=
q0 q01
3. r=a
a
q0 q10
q2 q3
ϵ ϵ
q1 q4
ϵ ϵ
b
q6 q5
Case: 2 r = ab
a ϵ b
q q q q
0 1 2 3
Case 3 r = a*
ϵ
ϵ q0 a q1 ϵ q2 q3
28
Examples 1 Regular Sets and Regular
Grammar
Draw FA with ϵ moves for the regular expression given as a (a+b)*.
Solution:
Using the above methods, steps for converting given regular expression to
FA
a
ϵ q1 q2 ϵ
q0 q5
ϵ ϵ
b
Step: 1 q3 q4
a
ϵ q2 ϵq 3
ϵ ϵ
q0 q1 q6 q7
ϵb ϵ
ϵ q4 q5
a
ϵ q5
q4
a ϵ ϵ ϵϵ
ϵ q9
ϵbϵ q0 q1 q2 q3 q8
q7
q6
ϵ
Step: 3
Final FA with ϵ moves for a (a+b)*
29
Theory of Computation Solution:
Using the rules for converting regular expression to FA with ϵ moves we
can get FA with ϵ moves for (a*+b*) as in following figure:
30
Regular Sets and Regular
Grammar
aj+1……….ak
a1………..aj ak+1………..a
qj =qk m qm
q0
m
It qm⊂F i.e. if qmis final state that means, “ a1, a2,…..am” is in L (M), then
“a1,a2………aj,ak+1 ak+2……am” is also in L(m); since there is a path from
q0 to qm that goes through qjbut not around the loop labeled aj+1 …..ak.
Similarly, we can go around the loop as many times as we like and the
resultant string will still be in L (m).
31
Theory of Computation Example: 1
Prove that, the following language is non-regular using Pumping lemma,
∴ L is not regular.
33
Theory of Computation If a class of language is closed under a specificoperation thatfact isentitled
as closure property of the class of language.
Theorems
1. The regular sets are closed under union, concatenation and
Kleene closure. If X and Y are regular sets.
Then X U Y, (X + Y), X Y and X*are also regular
Proof:
X+Y that means the language of all words in either X orY. Regular
expressions forX and Y are r1 and r2 respectively.
Then r1 + r2 is regular expression for X U Y
r1r2 regular expressionfor XY.
r1*is regular expression for X*.
Therefore, all three types of these sets of words adefinable by regular
expressions and so are themselves regular sets.
2. Regular set is closed under complementation. If X is regular set,
then X'is also regular.
34
Regular Sets and Regular
Grammar
Above Finite Automata shows that it accept all strings other than aba and
aTherefore, we can prove that complement of regular set is also regular.
3. The regular sets are closed under intersection. If X and Y are
regular sets.
Then X ∩ Yis also regular
Proof:
By Using De-Morgan’s Law
X ∩ Y = (X' U Y') ' = (X'+Y')’ this can be stated by Venn diagram
(X' U Y')
35
Theory of Computation
(X'+Y')’
39
Theory of Computation A → Bx, A → ε or A→x
Where, 'A' and 'B' are non-terminal and 'x' is a string of terminals.
e.g. Consider the following grammar
G = ({S, B, A), (a, b), P, S)
Where, 'P' contains following set of production rule,
S → Aa │ Bb
A→ Bb
B → Balb
Above grammar is left-linear in each production has only one nonterminal
on the right-hand side and that is the leftmost symbol on the right-hand
side.
2) Right-linear grammar:
A regular grammar contains of productions with at the most onenon-
terminal on the right-hand side and the right most symbol appears on the
right-hand side of the production then the grammar is called right-linear
grammar
Following are forms of productions in right-linear grammar are
A →x B,
A → x or
A→ε
Where, 'A' and 'B' are non-terminal and 'x' is a string of terminals.
E.g. consider the following grammar
G = ({S, B), (a, b, ε), P, S)
Where, 'P' contains following set of production rule,
S → aB
B → aBl ε
3.8 SUMMARY
• Regular set is the set that represents the value of the Regular
Expression.
3.9 REFERENCES
1) Theory of Computer Science, K. L. P Mishra, Chandrasekharan,
PHI,3rdEdition
2) Introduction to Computer Theory, Daniel Cohen, Wiley,2ndEdition
3) Introductory Theory of Computer Science, E.V. Krishnamurthy,
Affiliated East-West Press.
4) Introduction to Languages and the Theory of Computation, John E
Martin, McGraw-HillEducation.
❖❖❖❖
41
4
CONTEXT FREE LANGUAGE
Unit Structure
4.0 Objectives
4.1 Introduction
4.2 Context-free Languages
4.3 Derivation Tree
4.4 Ambiguity of Grammar
4.5 CFG simplification
4.6 Normal Forms
4.6.1 Chomsky Normal Form
4.6.2 Greibach Normal Form
4.7 Pumping Lemma for CFG
4.8 Summary
4.9 References
4.10 Review Questions
4.0 OBJECTIVES:
At the end of this unit, Students will be able to:
42
4.1 INTRODUCTION Context-free Languages
This Chapter deals with concepts of grammar and especially about context
free Grammarand Context-free Languages.It gives details information
about Derivation, Derivation Tree and Ambiguous Grammar.The need of
Simplification of Grammar means we can remove Ambiguity of Grammar.
Also discussion about Normal Forms and Properties of CFL.The property
of CFG is that all productions are of form one Non-terminal→ finite string
or terminals and/or nonterminal. The language created by a CFG is called
a context-free language.
44
=> aaas , rule (1) Context-free Languages
Where, α, β ⊂ (V U T)* and 'α' involving at least one symbol from ‘V’i.e.
at least one non-terminal.
Here we know,
V ∩ T = Ø = null set.
‘α' and 'β' consists of any number of terminals as well asnon-terminals and
they are usually termed as sentential forms. Chomsky had classified the
grammars in four major categories. Outof which there is one, with the
productions of the form
A→α
Where, 'A' is any non-terminal
and 'α' is any sentential formis called as Context-free grammar.
As we can observe in this typeof grammar there is a restriction that, on the
left hand side of eachproduction there should be only one Non-terminal,
e.g. The grammar thatwe have considered generating statement 'Dog runs'
46
can also be consideredas an example of context free grammar, in short, Context-free Languages
termed as CFG.
47
Theory of Computation (ii) Rightmost derivation:
S => a A S by using rule (1)
=> a A a by using rule (2)
=> a S b A a by using rule (4)
=> a S b b a a by using rule (5)
=> a a b b a a by using rule (2)
When a string is to be generated from the given production rules, then it
will be veryconvenient to shown the generation of string pictorially. This
generation (also calledderivation) when drawn graphically takes a tree
form and so it is called derivation tree oralso called parse tree. We observe
the following regarding the derivation tree.
i. The root node in the derivation tree is always labelled with the start
symbol as all strings are imitative from start symbol
ii. All the leaf nodes are labeled with some terminal symbols of the
grammar. (i.e. the elements of Ʃ). So these nodes are called terminal
nodes.
iii. All other nodes are labelled with some non-terminal symbols of the
grammar (i.e. the elements of VN). These are called non-terminal
nodes.
iv. If the string w of the language generated by the grammar has a length
n, then there are n terminal nodes, arranged from left to right.
Example 2. Consider the following Grammar
48
S Context-free Languages
S S
S S
Figure 1.1
Derivation tree for string ab
Draw the derivation tree for a string aabbaa using following Grammar G.
G = (VN, Ʃ, P, S), where
VN = (S, A) Ʃ = {a, b} and
P = S → aAS
S→a
A → SbA
A → SS
A → ba
Solution:As Sis the start symbol, any string generated by the grammer will
be derived from S
So we will use
S → aAS.
Obviously we will not use S → a to start with as then we cannot create
anything other than a
Since we want to generate aabbaa. We should select a proper A-
production such that it generates a string beginning with a followed by
So we select
A → SbA
So we get
S→ aSbAS
S→ aabAS
49
Theory of Computation Now we want that A-production which results in a String that begins with
b followedby a. So we must choose A→ba.
So
S→ aabbas
Which then gives S→ aabbaa the required string. This is shown as below.
S→ aAS
S→ aSbAS by A → SbA
S→ aabAS by S → a
S→ aabbaS by A→ ba
S→ aabbaa by S→ a
The derivation tree is as shown Fig. 2.
S
a s
A a
S A
a b
b a
For the following grammar show the derivation tree for aaabbabbba.
The grammar G = (VN, E, P, S)
P = S → AB│ bA
A → a │As│ bAA
B→ b │bS│ aBB
The derivation of the string is as follows
Start with S
→ aB
→ aaBB
→ aaaBBB
50
→ aaabSBB Context-free Languages
→ aaabbABB
→ aaabbaBB
→ aaabbabB
→ aaabbabbS
→ aaabbabbA
→ aaabbabbba
The derivation tree will be as shown in Fig.4.
S
a B
a B
B b S
a B b A
b S a
b A
51
Theory of Computation It is Crucial however to note that the ordering used does not matter the
generatedstring. e.g. a given string can be derived using either leftmost or
rightmost derivation.Consider the string ab and the above grammar again.
The two derivations are as shownbelow.
S→AB S → AB
S→aB S → Ab
S→aD S → ab
(a) Left most (b) Rightmost
As stated above, the ordering does not disturb the generated string.
However in manyapplications, it is Convenient to use leftmost derivation.
For the grammar,
S → aB 1bA
A → a│aS│bAA
B → b│bS │aBB
Example:1Write leftmost and rightmost derivation for the string
aaabbabbba
Solution:
Leftmost derivation
S→ aB
→ aabB
→ aaaBBB
→ aaabSBB
→ aaabbABB
→ aaabbaBB
→ aaabbabB
→ aaabbabbs
→ aaabbabbbA
→ aaabbabbba
Right most derivation
a→ aB
→ aaB
52
→ aaBbs Context-free Languages
→ aaBbbA
→ aaBbba
→ aaaBBbba
→ aaaBbbba
→ adabSbbba
→ aaabbAbbba
→ aaabbabbba
For the following grammar, give the leftmost and rightmost derivation for
the string
abaabb.
G = (VN, Ʃ, P, S), where
VN =S. X = {a, b}
P = {S → X baa X
S → ax
X→Xa
X→Xb
Give leftmost and rightmost derivation.
Leftmost derivation is as follows.
S→ X b aa X
→ Xa baa X
→ ab aa X
→ abaa Xb
→ abaaa X bb
→ abaa bb
Rightmost derivation is as follows
S→ Xbaa X
→ Xbaa Xb
→ XbaaX bb
→ Xbaa blb
53
Theory of Computation → Xa baa bb
→ ab aa bb
P = S → aS │Sa │a
Now, consider a string a4 (i.e. aaa). This string can be derived in
thefollowing different ways as shown in following figure.
S S S S
a a a a
S S S S
a a a a
S S S S
a a a a
P = {S→ S + S│S + S│ d
This grammar is for generating arithmetic expressions made up of
operators + and *.Undertake the terminal d stands for digit. Now consider
a string 5 + 6 * 7. We knowthatexpression of a grammar. But the string
can be derived in two differentshown in following Figure and so the
grammar is ambiguous. Therefore, the grammar is ambiguous.
54
Context-free Languages
S S
S + S S * S
S S * S
S + S
d d
d d d
Fig. Two ways to derive a string d + d*d
55
Theory of Computation Examples: Simplification of Grammar
i. S → AB │a S
A →a A
S→a
A→a
Step: 2 A is Useless
S → a
ii. S → AB │ BC
A → aAa │ aAa
B → Bb │b
D → a D │d
Step 1 – C Useless
S → AB
A → aAa │aAb
B → Bb │ b
D → d D │d
Step 2 - A & d ARE Useless
A whole grammar is useless
A is useless because no sentence will be derived from D
D is useless because it is not been used in any derived process.
Method 2. Elimination of unit production
A production of the form 'A → B' where, 'A' and 'B' both arenon-
terminals, are called ds. Unit productions. All other productions(including
ϵ - productions) are Non unit productions.
56
Context-free Languages
Elimination Rule:
For every pair of non-terminals 'A' and 'B',
i) If the CFG has a unit production of the form 'A → B' or,
ii) If there is a chain of unit productions leading from 'A' to 'B' such as,
A => X1 => X2 =>…..... => B
Where, all Xis (i > 0) are non- terminals, then introduce newproduction (s)
according to the rule stated as follows:
"If the non-unit productions for 'B' are,
B → α1 │ α2│...
Where, ‘α1, α2’ ... are all sentential forms (not containing only onenon-
terminal)
then, create the productions for 'A' as,
A → α1, │α2│…….
Single capital letters replaced with its production
Examples: Simplification of Grammar
1) A→B
B → a │b
→ A→B
A → a │b
2) S → Sool │ F
F → S ││ O │F
T → OS l │ l │ lSlO
→ S → SOOl│F
F → SllO │OSl │ l│lSlO
3) S → A │bb → S → A │bb
A → B │b A→S│a│b
B → S │a s → S │a │ b │bb
S → a │b │bb
57
Theory of Computation
ii. S → AB
A → A │BB │ Bb
B→b│aA│ϵ
→ S → AB │ A
A → SA │BB │ Bb │b │ B │ S
B → b │aA│ a
iii. S → ABA
A → aA │ ϵ
B → bB │ ϵ
→ S → ABA │AA│ BA│ AB│ B │A│ ϵ
A → Aa │ a
B → bB │ b
S → ABA│ AA │BA │AB│ B │A
A → aA│ a
B → bB│ b
58
4.6 NORMAL FORMS Context-free Languages
59
Theory of Computation Now all the productions except S → ASA and S → BSB are in required
form. Toconvert these productions into the required form we add
additional non-terminals, say
R1, R2………etc
So we get
S→ AR1
S→ AA
S→ BB
S → BR2
S→ a
S→ b
A→a
B→ b
R1 → SA
R2 → SB
The grammar is now in CNF.
Example: 2convert the following grammar into CNF.
S → bA │aB
A → bAA│ as│a
B→ aBB │ aS│b
Solution: In the first step we get
S → bA│XB
A → bAA│aS│a
B → aBB │aS│ b
Note that we leave alone the productions A → a and B → b as it is
because they arealready in required form.
In the next step, we just need to take care of productions.
A → YAA and B → XBB because they are not in required form.
So, A → YR1 and B → XR2
Where R1 → AA andR2 → BB
60
So the grammar in CNF will be, Context-free Languages
S → YA│XB
A → YR1│XS│a
B → XR2 │YS│b
X→a
Y→b
R1 → AA
R2 → BB
Example 3: Convert the following CFG into CNF.
S → aaaaS │ aaaa
Which generates the language a4nfor n = 1, 2, 4……
Solution: In the first step we get
S→ AAAAS
S → AAAA
A→a
Now,
S → AR1
R1 → AR2
R₂ → AR4
R4→ AS
S → AR4
R4 → AR5
R5 → AA
A→a
The grammar is now in CNF.
4.6.2 Greibach Normal Form:
We will now look at one more normal form of the grammar.
61
Theory of Computation Definition:
If each production in a CFG is of the form
A → aB, where
a is a terminal andB is a string of non-terminals (possibly empty), then the
grammar is in GreibachNormal Form (GNF).
For example, the following grammar is in GNF.
S → bA │ aB
A → bAA│aS │a
B → aBB │bS│b
All the productions start with a terminal on RHS and followed by a
string,non-terminals (sometimes ε).
Before looking at how to convert a given grammar into GNF, we have to
discuss two important auxiliary results, which are helpful for converting a
grammar to GNF.
Lemma: 1: If A → Bγ is a production, where A and B are non-terminals
and they are B- production of the form
B → β1 │ β2│……. │βs then
We can replace the production A → Bγ by
A → Bi γ │1 ≤ і ≤ S
For example, take into consideration following grammar
A → Bab
B → aA │ bB │ aa │ AB
So using Lemma. 1 in above, we get
A → aA ab │ bBab │ aaab │ A Bab
Lemma. 2: If a CFG consists of production of the form
A → Aα1│Aα2│....│ Aαr│β1│β2│.......│βs such that each of the Bi’s do
not start with A then an equivalent grammar can be formed as follows:
A → β1│B2│…...│BS
A → β1 Z│β2 Z│......│βS Z
Z → α1│α2│.......│αr
Z → α1 Z│α2 Z│…....│αr Z
For example, consider following grammar. Here
A → aBD│ b DB │c
A → AB│AD
α 1 → B , α2 = D
62
β1 → a BD, β2 = bDB and β4 = C Context-free Languages
So applying Lemma 2 we get
А → a BD│ DB│C
A→ a BDZ│ bDBZ│ cZ
Z → B│D
Z → BZ│ DZ
Lemma 2 is useful in dealing with left-recursive productions i.e. the
productions of form A → Aα.
We will make use of these lemmas to convert CFG to GNF.
Example:1Construct a grammar in GNF equivalent to the grammar S →
AA│a and
A→ SS│b.
Solution:
Observe that the CFG is in CNF. If we rename S as A1, and as A A2 (for
conversion purpose) respectively, the productions will be then
A1 → A2 A2 │α
A2 → A1 A1 │b
and
We leave A2 → b as it is in the required form.
Now consider A2 → A1 A1. To convert this we will use lemma 1 to get
A2 → A2A2A1
A2 → aA1
i.e. by replacing the first A1 on RHS of A2 → A1A1 by definition of A1.
Now the Production A2 → aA1 is in required form. But we need to use
Lemma 2 for
A2→ A2A2A1 as it is of form A → Aα.
Apply Lemma 2 to the productions of A2. A2 productions are
A2 → A2 A2 A1
A2 → aA1
A2 → b
Here, β1 = αA1, β2 = b, α = A2 A1
So we have now by adding new non-terminal.
A2 → aA1│b
A2 → aA1 Z₂│bZ2
Z2 → A₂ A1
Z2 → A2 A1Z2
63
Theory of Computation Now all A2 productions are in required form.
Now we will save to consider the A1, production,
A1 → A2A2│a
Out of these A1 → a is in required form.
So consider, A1 → A₂A₂
Applying Lemma 1, we get
A1 → aA1 A2│ bA₂│ aA1 Z2A2│ bZ2 A2
So adding to this list the A1 → a production, we have retained all A1
productions and they are
A1 → aA1 A2│bA2│ aA1 Z2, A2, │bZ2A2│ a
Now we amend Z2 productions. Applying lemma to Z2 productions we get
Z2 → aA1 A1│ bA1│aA1 Z2 A1│ bZ2 A1
Z2 → aA1 A1 Z2│bA1 Z2│aA1 Z2 A1 Z2│bA1, Z2
So the grammar in GNF will be
G’ = (A1, A2, Z2,}, {a, b}, P’, A1)
P’ =
Where
A1 → a│aA1 A2│bA2│aA1 Z2 A2│ bZ2 A2
A₂ → aA1│b│ aA1 Z2 │b Z₂
Z2 → aA1 A1│bA1│aA1 Z2A1│bZ2A1
Z2 → aA1 A1 Z2│bA1 Z2│aA1 Z2 A1 Z2│bZ2 A1 Z2
64
Lemma: 1 (The pumping lemma for context-free languages): Context-free Languages
4.8 SUMMARY
• Context-free Languages
It is language generated by CFG means Context Free Grammar;
• Ambiguity of Grammar:
A CFG is called ambiguous if for at least one word in the language that it
createsthere are two probable derivations of the word that corresponds to
different syntax trees
• CFG simplification
Method 1. Removal of Useless Symbol:
Method 2. Elimination of unit production
Method 3. Removal of ϵ production:
4.9 REFERENCES
1. Theory of Computer Science, K. L. P Mishra, Chandrasekharan,
PHI,4rd Edition
2. Introduction to Computer Theory, Daniel Cohen, Wiley,2ndEdition
3. Introductory Theory of Computer Science, E.V. Krishnamurthy,
Affiliated East-West Press.
4. Introduction to Languages and the Theory of Computation, John E
Martin, McGraw-Hill Education
65
Theory of Computation 4.10 REVIEW QUESTIONS:
1. Define following terms.
a) Derivation Tree
b) CFG
2. What is Context Free Language (CFL)?
3. Find the Context Free Language (CFL) associated with the CFG.
S → aB | bA
A → a | aS | bAA
B → b | bS | aBB
4. Construct CFG for
L= {a m b n c m | n, m ≥ 1}
5. Remove ambiguity from following grammars.
a) S → aS | Sa |ϵ
b) S → SS | AB
A → Aa | a
B → Bb | b
6. Draw Derivation Tree for a substring “001100”.
7. Construct a grammar to generate stings with no consecutive a’s but
may or may notwith consecutive b’s.
8. Convert following CFG to CNF
S → aAab | Aba
A → aS | bB
B → ASb | a
❖❖❖❖
66
5
PUSHDOWN AUTOMATA
Unit Structure
5.0 Objectives
5.1 Introduction
5.2 Definition of PDA
5.2.1 Elements in PDM
5.2.2 Model of Pushdown Automaton
5.2.3 Pictorial Representation for PDA
5.2.4 Construction of PDA
5.3 Acceptance by PDA
5.3.1 PDA acceptance by Final State
5.3.2 PDA acceptance by Empty Stack
5.4 PDA and CFG
5.5 Summary
5.6 References
5.7 Review Questions
5.0 OBJECTIVES
In this chapter, Students will be able to:
5.1 INTRODUCTION
In this chapter we are learn the concept of the PDA. In case of FSM, we
have seen that it does not have memory to remember arbitrarily long
sequences of the input. So PDM (Pushdown Stack-Memory Machine) is
more powerful than FSM (Finite Automata Machine) and more
capabilities. FSM accept only the regularlanguages and PDM is consider
as a CFL-acceptor or CFL-recognizer. While FA is a mathematical model
67
Theory of Computation of FSM likewise PDA (Push-down Automata) is mathematical model of
PDM.
69
Theory of Computation
Start Start the Process
Or
Push < letter >
70
Pushdown Automata
Example: 1
Construct PDA recognizing the language accepted by the DFA given
in following figure.
b a
a
- +
b
An Example DFA
Solution:
As we know the DFA given in above figure is the acceptor for the regular
language represented by regular expression.
b * a a* (b b* a a*)
We can construct the PDA equivalent to given DFA as shown in
following figure.
Start
b
b
a
b
Read Read
1 2
Accept
b Reject a
71
Theory of Computation moves to ‘accept’ state as shown. Let us simulate the working of the PDA
for the strings ‘bbaaba’ and ‘baaabab’.
Simulation for string bbaaba.
72
Q.2 Construct PDA recognizing the language accepted by the DFA Pushdown Automata
given in the following figure.
An example DFA
PDA can be constructed as shown in above diagram. We can see that state
‘Read1’ of PDA is analog our to state ‘1’ at given DFA. Similarly, ‘Read2’
is analogous to state ‘2’ and ‘Read3’ is with state ‘3’ of the given DFA.
Obviously ‘Read1’ and ‘Read2’ are non-final states and reading blank ‘b’
indicating end of input string in that state causes machine to move to the
reject indicating the input string given is rejected by the PDA.
.
.
R
Top of Stack
74
i.e. Pushdown Automata
.
.
B Top of Stack
R
ii. After we read 1st 'a' we don't change the state, as we have to read
next all 'a's and add 'B' to stack. The variation between 1st ‘a’ and
remaining all ‘a’s can be made by observing at top of stack (TOS)
For 1st ‘a’ - TOS is R
For residual ‘a’s - TOS is B
δ (qo, b, B) → (q1,ϵ)
b. δ (q1, b, B) → (q1, ϵ)
Here we pop all 'B's for all 'b's occurring in input tape and be in self loop
as the same actionis to be recurrent for (n-1) times so no need to change
the state.
c. (Step iii)
δ (q1, ϵ, B) → ERROR
state
vi. After step (v) is over, we may have the condition that current state is
q1 top of stack is R andinput symbols can be a, b, or ϵ
a. If (step i)
b. If δ (q1, ϵ ,(step
R) Accept
ii)
state
δ(qı, b, R) ERROR
c. if δ(qı,(step
a, R) iv)
→ ERROR
state
state
Where, δ is
δ (q0, a, R) =(q0, BR), δ (q0, b, R) = ERROR,
δ (qo, ϵ, R) ERROR, δ (qo, a, B) = (q0, BB),
δ (qo, b, B) (91, E), δ (qo, ϵ, B) = ERROR,
δ (q1, a, B) ERROR, δ (q1, b, B) = (q1, ϵ),
δ (q1, ϵ, B) ERROR, δ (q1, ϵ, R) = ACCEPT,
δ (q1, b, R) ERROR, δ (q1, a, R) = ERROR
76
1. Construct PDA for the language Pushdown Automata
L = {a b │ n ≥ 0}
n n
Solution:
The language set for L
L ={ɛ, ab, aabb, aaabbb....)
This language is same as L = {anbn │n ≥ 1}
In example 2 except the extra string ϵ in this example.
So the same 8 can be written as in 2 example except the transition which
accepts 'ϵ' as a string andgoes to error state in 2 example, so in its place
that transition, we have to write the transition as
77
Theory of Computation 5.3 ACCEPTANCE BY PDA
PDA accepts its input by consuming it and entering an accepting state. We
call this approach “acceptance by final state”.There is a second approach
to defining the language of PDA, we call it. Language “accepted by empty
stack”, i.e. the set of strings that cause the PDA to empty its stack, starting
from initial ID.
5.3.1 PDA acceptance by Final State:
The way of defining a language accepted is similar to the way a finite
automaton receives inputs. Thatis, we designate some states as final states
and define the accepted language as the set of all inputsfor which some
choice of moves causes the Pushdown automaton to enter a final state,
Formal Definition: Language acceptance by Final State
Let P = (Q, Ʃ,Γ, δ, q0, Z0, F) be a PDA then L(P), the language accepted
by P by final state is
{w │(q0, w, Z0) * (q, ϵ, α)} P
For some state q in F and any stack string a. That is, starting in the new ID
with w waiting on theinput, P consumes w from the input and enters an
accepting state. The contents of the stack at thattime are irrelevant.
Examples:
1. Let P to accept the language LwwR.
The language set
L = {00, 11, 0110, 011110, 1001, 110011, 101101,}
The PDA is described as
P = ({q0, q1, q2}, {0,1}, {0, 1, zo}, δ, q0, z0, {q2}).
Where, 8 is defined as
δ(qo, 0, Z0) → (qo, 0Z0), δ (qo, 1, Z0) → (q0, 1Z0),
δ(qo, 0, 0) → (qo,00), δ (qo,0,1) → (qo,01),
δ (qo, 1, 0) → (qo, 10), δ (qo, 1, 1) → (q0, 11),
δ (qo, ϵ, Z0) → (q1, Z0), δ (qo, ϵ, 0) → (q:,0),
δ (qo, ϵ, 1)→ (q, 1), δ (q0, 0, 0) → (q1, ϵ),
δ (qı, 1, 1) → (q1, ϵ), δ(q1, ϵ , Z0) → (q2, Z0)
and accept
Let the string be 1111 where, w = 1,wR= 1
78
Pushdown Automata
79
Theory of Computation 1. Consider the language in above example and consider the PDA in
thatexample.This example never empties the stack.
N(P).
Instead of transition
δ (q1, ϵ, Z0) (q2, Z0) we add
Now P pops the last symbol off its stack as it accepts and L(P) = N(P) =
Lwwr.
Consider the same string 1111.
80
iii. The languages which are accepted in empty stack by some Pushdown Automata
PDA.
Figure
Conversion of a Context FreeLanguage (in GNF) to Push down
Automata (PDA)
Theorem:
If L is a context free language, then we can construct a PDA a accepting
L by empty stack
i.e. L = N(A).
We can construct A by making use of Productions in G.
Step 1: Construction of A
Let L = L(G), where G = (VN ,∑, P, S) is a CFG. In GNF we construct
PDA A as
A = ({q), ∑, VN∪ ∑, δ, q, S, ɸ)
where, transition functionδ is defined by the following rules:
R1: δ(q, a, A) = {(q, α) A →aa is in P}
R2: δ(q, a, A) = {(q, ε)} for every A → a in ∑
The PDA can be constructed as:
1. The Pushdown symbols in A are variables and terminals.
2. If the PDA reads a variable A on the top of PDS, it makes 'a' move by
placing the RHS of any
3. ε -Production (after erasing A).
4. If the PDA reads a terminal a on PDS and if it matches with the
current input symbol, then the
5. PDA erase a.
6. In other cases the PDA halts.
7. If w ∈ L(G) is obtained by a left most derivation,
81
Theory of Computation ThenA can empty the PDS on application ofi/p string w. The first move of
A is by a ε -move corresponding to S →u1 A1 α1. The PDA erasers S and
stores u1 A1 α1, then using R2, the PDA erasers the symbols in ul by
processing a prefix of w. Now the topmost symbol in PDS is A1.
Once again by applying the ε -move corresponding to A1 → u2A2 α2, the
PDA erases A2 and stores u2A2 α2 above α1. Proceeding in this way,
thePDS empties the PDS by processing the entire string w.
Example 1:
Construct a PDA A equivalent to the following context free grammar:
S → OBB, B → OS | 1 | 0.
Test whether 0104 is in N(A).
Solution:
A is defined as:
({q}, {0, 1}, {S, B, 0, 1}, δ, q, S, ɸ)
The transition functionδ is
R1: δ(q, 0, S) = (q, BB)
R2: δ(q, 0, B) = {(q, 0S), (q, ε)} and δ(q, 1, B) → (q, S)
R3: δ (q, 0, B) = {(q, ε)}
(q0, 0104, S) ├ (q, 0104, BB) by Rule R1
├ (q, 104, BB) by Rule R3
├ (q, 104, SB) by Rule R2 since
82
Solution: Pushdown Automata
5.5 SUMMARY:
• PDA is mathematical model of PDM (Pushdown Memory - Stack
Machine).
83
Theory of Computation If S a ϵ then the conversion is given as
δ (q0, a , S) (q0, ϵ)
5.6 REFERENCES:
1. Theory of Computer Science, K. L. P Mishra, Chandrasekharan,
PHI,3rd Edition
2. Introduction to Computer Theory, Daniel Cohen, Wiley,2nd Edition
3. Introductory Theory of Computer Science, E.V. Krishnamurthy,
Affiliated East-West Press.
4. Introduction to Languages and the Theory of Computation, John E
Martin, McGraw-Hill Education.
❖❖❖❖
84
Unit III
6
LINEAR BOUND AUTOMATA
Unit Structure
6.0 Objectives
6.1 Introduction
6.2 Linear Bound Automata
6.3 The Linear Bound Automata Model
6.4 Linear Bound Automata and Languages
6.5 Review Questions
6.6 Summary
6.7 References
6.0 OBJECTIVES
6.1 INTRODUCTION
85
Theory of Computation 6.2 LINEAR BOUND AUTOMATA
Figure 6.1
ML and MR are boundaries for a tape. ML is entered in leftmost end of the
input tape and avoids the Read/Write head from getting off the left end of
the tape. MR is entered in rightmost end of the input tape and avoids the
Read/Write head from getting off the right end of the tape. Both end
markers should be at their respective ends, and Read/Write head should
not write any other symbol over both end-markers.
Examples:
1) {an bncn/ n ≥ 1}
87
Theory of Computation Proof :
If L is a CSL, then L is accepted by some LBA.
Let G = (N, Σ, S, P) be the given grammar such that L(G) = L.
• Productions that can erase everything but the unmodified copy of the
string, provided that the simulated moves of M applied to the other
copy cause M to accept.
Linearly bounded memory machine:
The linearly bounded memory machine is similar to a Turing machine,
except that it has, instead of a potentially infinite tape forcomputation,
only the portion of the tape containing the input string plus two squares on
the tape to hold the end markers. Such a restriction reduces the machine's
88
power to recognize certain types of strings. It has been shown that even Linear Bound Automata
when the length of the tape is increased as a linear function of the length
of the input string, the computational ability of the machine remains the
same. Hence, such a machine is called a linearly bounded memory
machine. It recognizes a class of languages known as context-sensitive
languages.
ID of LBA
89
Theory of Computation In LBA, the ID is denoted by (q, w, k) where q ∈ Q, w ∈ Г and k is some
integer between 1 and n.The transition of the IDs is similar except that if k
changes to (k – 1), then Read/Write head moves to the left and if move to
(k + 1) then head moves to the right.
Languages accepted by LBA is:
The language accepted by LBA is defined as the set :
{w∈{∑ - {ML, MR} )* | (q0, ML, w,MR, 1) * ├ (q, α, i) for some q ∈ F and
for some integer i between 1 and n.
• First it start at initial state with Read/Write head reading the left end
marker (ML), M halt over the right-end marker (MR) in final state,
otherwise w is rejected.
• The production rules for the generative grammar are constructed as in the
case of TM. The following additional productions are needed in the case
of LBA:
90
2 Context-free A→ α Linear Bound Automata
Pushdown
1 Context-sensitive α →β Linear-bounded
enumerable(α, β ∈(V ∪ ∑)
(unrestricted) α contains a variable)
Example
92
12. δ (q3, b) = (q3, b, Left) – by moving the head of the automaton to Linear Bound Automata
the left.
13. δ (q3, C) = (q3, C, Left) – capital letters C,B are skipped in state q3
14. δ (q3, B) = (q3, B, Left) – by moving the head of the automaton to
the left.
15. δ (q0, A) = (q3, A, Right) – the head is positioned after the last A
and the state is changed to q0.
16. δ (q4, B) = (q3, B, Right) – if there is a B after the last A the state is
changed to q4.
17. δ (q4, B) = (q4, B, Right) – in state q4 capital letters B and C are
skipped
18. δ (q4, C) = (q4, C, Right) – by moving the head to the right.
19. δ (qf, MR) = (qf, MR, Accept) – if in q4 there were only capital
letters on the tape, LBA accepts.
6.6 SUMMARY
1. Linear Bound Automata design to accept context-sensitive languages.
2. End markers (ML and MR) is the safety feature of LBA.
6.7 REFERENCES
❖❖❖❖
93
7
TURING MACHINES
Unit Structure
7.0 Objectives
7.1 Introduction
7.2 Turing Machine Definition
7.3 Representations
7.4 Acceptabilityby Turing Machines
7.5 Designing and Description of Turing Machines
7.6 TuringMachine Construction
7.7 Variants of Turing Machine
7.8 Review Questions
7.9 Summary
7.10 References
7.0 OBJECTIVES
7.1 INTRODUCTION
Turing machine (TM) was invented by Alan Turing in Turing 1936, which
is big achievement in the field of finite-state computing machines. Initially
was specially design for the computing of real numbers.TM is very
powerful than Linear Bound Automata. Today this machine use as a
foundational model for computability in theoretical computer science.TM
can effectively represent its configuration using a simple notation like as
ID's of PDA.TM has a infinite size tape, use to accept Recursive
Enumerable Languagegenerated by Type-0 Grammar. The machine can
read/ write and also move in both (left and write) directions. The machine
producesdesired output based on its input.
94
Why Turing Machines? Turing Machines
• They can do everything a computer can do and vice versa. But takes a
lot more time. Is not practical and indeed its not what is implemented
in today’s computer.
95
Theory of Computation 7.3 REPRESENTATIONS
Figure 7.1
Current symbol under Read/Write head is a7.
Suppose, current state: q2
By definition of ID - α β γ, where β is state = q2
∴ The ID is
Figure 7.2
96
Moves in Turing machine Turing Machines
The δ(q, xi) changes the ID of TM. This change is called move.
If δ(q, xi) = (P, y, L)
Take input string x1, x2, ….Xn.
Currentsymbol under Read/Write head- xi
ID before processing symbol xi
x1x2…….xi-1 q xi+1…..Xn
ID after processing symbol xi
x1x2……. xi-2 P xi-1 y xi+1…..Xn
∴It is represented as
x1x2…….xi-1 q xi…..Xn├ x1… xi-2 P xi-1 y xi+1…..Xn.
If δ(q, xi) = (P, y, R)
Then the ID become:
x1x2…….xi-1q xi…..Xn├ x1x2… xi-1 y P xi+1 …..Xn.
Thus Ii ├ Ikdefine the relationship between IDs.
├* denotes reflexive-transitive clousure of relation ├.
ii)Transition table:
a) The transition function δ
Q x Ґ → Q x Ґ x {L,R}
States Q stored in table rows and table column shows each of the tape
symbols Ґ.
b) Each pair (Q , Ґ ) is represented by a triple (Q, Ґ, {L,R}) as:
If δ (qi, a) = (α, β, γ) then we write (α, β, γ) under qith row and ath column.
In transition table we get entry as:
State qi on input symbol 'a' goes to or changes to state ' γ ', by replacing the
input symbol 'a' by ' α' and moves the tape head one cell in direction of ' β'.
97
Theory of Computation iii)Representation by Transition Diagram
a) Every state implies to one vertex.
b) Transition of states represented by directed edges.
Here, the directed edge from state qi to qj with label (α, β, γ) it indicates transition
δ(qi,, α) = (qi, β, γ)
The symbol α will replaced with β and tape head moves to L , or R direction
according to the value ofγ.
Every Edge in the transition diagram is represented by 5-tuple (qi, α, β, γ, qj).
L(M) is the set of strings w ∈ ∑*, such that q0w ├ α p β for some state p
in F and any tape strings α and β. The TM ‘M’ does not accept w if the
machine M either halts in a non-accepting state.
Example :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 q0 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.
98
Hence, Turing Machines
Α BRq2 BRq1
99
Theory of Computation i) We use 2 tape symbols X and Y for a and b respectively.
ii) We replace a by X. Move right replace first b found by Y. move right find last
c replace it Blank `B'.
iii) Repeat step 2 until all a's, b's and c's. If no more a's, b's and c's are
present – Accept otherwise Reject the string.
Replacement Logic:
• Mark 'a' then move right.
• Mark 'b' then move right
• Mark 'c' then move left
• Come to far left till we get 'X'
• Repeat above steps till all 'a', 'b' and 'c' are marked
• At last if everything is marked that means string is accepted.
Let’s consider the string,
Direction aabbccB (String)
→ XaYbccB
← XaYbcBB
→ XYYcB
← XYYBB
→ XYYBA accept
Representation by Transition table method
A B c X Y Z
q3 (q4, B, L)
q6 Accept state
100
7.7 VARIANTS OF TURING MACHINE Turing Machines
Figure 7.3
2) Two-way Turing Machine:
A two-way Turing Machine is an infinite tape with its input tape infinite in
both directions, other components (Tuple) are same as that of the basic
model.
Figure 7.4 (6.3)
Here the input string is a1, a2,a3, ... an. Whenthe input string placed on tape,
it is loaded with all blank symbols (B) to the left of a1 and to the right of
an. So if q0 is the initial state of the TM, ID corresponding to the initial
state will be q0, a1, a2, s….. an,B.
3) MultitapeTuringMachine(MTT):
For every Multitape Turing Machine there is an equivalent single tape
Turing Machine. In MTT number of tapes and read/write heads increased.
All the indivisual tape will have there own respective tape heads. For this
assumption is that all the tapes are two way infinite. It contains finite
control with k number of heads.
101
Theory of Computation
Figure 7.4
Depending upon the state of finite control, in single move the symbols
scanned by each of the tape heads, the machine can
a) Change the state.
b) Print a new symbol on each of the cells scanned by its tape head.
c) Then independently move each tape head, one cell to the left or
right or keep it stable.
4) Single tapeTuring Machine:
A Turing Machine consists of only one tape with infinite length on which
read and write head can be performedoperation. The tape consists of
infinite cells on which each cell either contains input symbol or a special
symbol called blank (B).
Figure 7.5
5) Non-deterministic Turing Machine(NTM):
Non-deterministic TM plays an important role in FAs and PDAs. It is
convenient but not essential in caseofFA.
Deterministic Turing machines have enough computing power that
nondeterministic fails to add any more. Non-deterministic TM differs from
deterministic TM, we have seen earlier, in the function δ, such that for
each state q and tape symbol X, δ (q, x) is a set of triples like,
102
Turing Machines
103
Theory of Computation Suppose the string is ababbabaΔ. The simulation for ababbabaΔ can be
shown as follows:
Now, we will see how this Turing machine will work for ababbabaΔ.
Initially, state is q0 and head points to a as:
104
Move right up to Δ in search of b as: Turing Machines
105
Theory of Computation Replace b by * and move right up to Δ as:
Go to HALT state
106
Solution: Turing Machines
Logic for 1's complement
1. Scan input string from left to right
2. Convert '1' into '0'
3. Convert '0' into '1'
4. When BLANK is reached move towards left(i.e.start of input
string).
Consider, TAPE movement for string "1010111"
Sequential explanation of TAPE movement
1. Input is given as "1010111" (scan string from left to right)
2. Convert '1' into '0' and move one step right
3. Convert '0' into '1' and move one step right
4. Convert '1' into '0' and move one step right
5. Convert '0' into '1' and move one step right
6. Convert '1' into '0' and move one step right
7. Convert '1' into '0' and move one step right
8. Convert '1' into '0' and move one step right
When BLANK (in right) is reached, string is finished. So move to start of
string (optional).
Tape movements are shown below.
107
Theory of Computation Let,
1's complement is written into the TAPE in place of input string.
Input String : 1010111
Output String : 0101000
State Transition Diagram
We have designed state transition diagram for 1's complement as follows:
1. Replace '1' with '0' and vice versa.
2. When BLANK is reached move towards left
3. Using state 'q2' we reach start of the string.
4. When we reach to BLANK in left we move one step right to point start
of string.
5. qf is final state
Example 3:
108
Continue this till converted all symbols on the left part of the string into x Turing Machines
and y and all symbols on the right of string into blanks. When one part is
completely converted but still some symbols in the other half are left
unchanged then the string will not be accepted. If we did not find an x or y
in the second half for a corresponding 0 or 1 respectively in the first half.
Then also string will not be accepted.
State Transition Diagram
Example 4:
Construct a Turing Machine for language L = {02n1n | n>=0}
Solution:
Stepwise Working :
• Step-1:
Given language contains twice number of 0’s as compare with 1’s. So,
we will first make the first two zeros Blank and go from state Q0 to Q1
and from state Q1 to Q2.
• Step-2:
After making them Blank we will traverse to the end of the string till we
get the rightmost 1 in state Q3 and make it Blank reaching state Q4.
• Step-3:
Now we will traverse back till we get the left most zero in the string and
return to the state Q0 from Q4.
109
Theory of Computation • Step-4:
We are just reducing the string by making left most two zeros Blank and
rightmost 1 Blank and if the string belongs to the language then it will be
left empty and hence get accepted at state Q5 which is Final state. If the
string is empty it will also get accepted at Q5.
State Transition Diagram
7.9 SUMMARY
110
3) Variants of TM: Turing Machines
i) Multitrack TM
ii) Two-way TM
iii) Multiple tape TM
iv) Single tape TM
v) Non-deterministic TM
7.10 REFERENCES
❖❖❖❖
111
8
UNDECIDABILITY
Unit Structure
8.0 Objectives
8.1 Introduction
8.2 The Church-Turing thesis
8.3 Universal Turing Machine
8.4 Halting Problem
8.5 Introduction to Unsolvable Problems
8.8Review Questions
8.9 Summary
8.10 References
8.0 OBJECTIVES
8.1 INTRODUCTION
112
Church's theorem, the Church-Turing thesis, and the creation of λ- Undecidability
calculus, or the Church λ operator.
There are various equivalent formulations of the Church-Turing thesis.
Formulation of the Church-Turing thesis is: “ Anything that can be
computed on any computational device can also be computed on a Turing
machine”.
or
“a problem can be solved by an algorithm iff it can be solved by a Turing
Machine”
or
“A common one is that every effective computation can be carried out by
a Turing machine. “
In the 1930s, two researchers– Alan Turing from England and Alonzo
Church from theUS – started analyzing the question of what can be
computed. They used two different approaches to answer this question:
• Alan Turing, with hiscomputational analysis of Turing machines, what
we would now consider computer engineering and computer
architecture;
• On the other hand, Church focused on what can be described – i.e., on
what we would now consider programming languages.
Initially, they came up with two different answers to this question:
• Turing states that a function is computable if and only if it can be
computed on a Turing machine, while
• Church statesthat a function is computable if and only it be described by
a program in his programming language (which is similar to LISP and
Scheme).
These, two statements areshows different answers – until Church proved
that these definitions are actually equivalent:
• if a function can be computed on a Turing machine, then it can also be
described by a program in Church’s programming language, and
• if a function can be described by a program in Church’s programming
language, then it can also be computed on a Turing machine.
Later on, their two statements were merged into one statement, which we
now call Church-Turing thesis.
Turing gave a very convincing argument that a human computer
(performing symbolic manipulations with pen and paper) can be simulated
by an appropriate Turing machine. Clearly, every Turing machine can be
simulated by a human (who will just follow the program of the Turing
113
Theory of Computation machine). Thus, we have an equivalence between the intuitive notion of an
algorithm (of the symbolic-manipulation kind, as discussed above) and the
formal notion of a Turing machine. This equivalence is usually called the
“Church-Turing thesis”
114
● Theorem2: There is a Turing machine UTM called the universal Turing Undecidability
machine that, when run on ⟨M, w⟩, where M is a Turing machine and w is
a string, simulates M running on w.
● The observable behavior of UTM is the following:
Figure 8.1
The output labeled 'loop' shows that the computation of U does not
terminate. If M halts and accepts input w, U does the same.
If M does not halt with w, neither does U. The machine U is called
universal since the computationof any TM M can be simulated by U.
In universal machineconstruction is for to design the string representation
of a turing machine. Because of the ability to encode arbitrary symbols as
strings over {0, 1}, we consider truing machines with input alphabet {0,
1} and tape alphabet {0, 1, B}. The states of turing machine are assumed
to be named {qo, q1 ...... qn} whereqo as start state.
115
Theory of Computation Formal Definition
Turing machine M is defined by its transition function(δ). A transition of a
standard turing machine is given as:
δ (qi, x) = [qj) y, d]
Symbol Encoding
0 1
1 11
B 111
q0 1
q1 11
qn n+1
L 1
R 11
UTM Simulation of T
UTM can simulate T, one step at a time. Steps are as follows:
Step 1:Scan the square (cell) on the state area of the tape and read the
symbol that T reads and initial state of T.
Step 2: Move the tape over program area containing the description of T
find out the row pointed by the symbol, read in step 1.
116
Step 3: Find the column pointed by the statesymbol in which T resides Undecidability
and then read the triple (new state ,symbol to be replace and direction of
the tape movement) in the intersection of this column with the row found
in step 2.
Step 4: Move the tape to the appropriate cell in the data area, replace the
symbol, move the head in required direction, read next symbol and
finally read the state area, replace the state and scanned symbol. And go to
step 1.
Let end(z) denote the encoding of a symbol z. A transition δ (qi, x) = [qj,
y, d] isencodedbythestring end(qi) 0end(x) 0end(qj) 0end(y) 0end(d)
The components of transitions are separated by 0's. Two of consecutive
0’s are used to show separate transition. The beginning and ending of the
representation are designed by 3 0's.
Example: Let M = (Q, {0, 1}, {0, 1, B), δ, q1,B, {q2}) be a TM.
Assume Q = {q1, q2, .....qn}
Let's consider 0, 1, and B as X1, X2, and x3 respectively.
D1 and D2are head movements for directions L and R respectively.
Consider the following move:
δ (qi, Xj) = (qk, Xl, Dm)
The move can be encoded by the binary string:
0i 1 0j 1 0k 1 0l 1 0m
A turing machine M with binary code is
111 code1 11 code2 11 ...... 11 coden111, …..Form(1)
Where each codei is of each above form, this code is beginning and ending
with 111. Each move of M is encoded by one of the codei. Each codeiis
separated by two consecutive 11’s. Thus the encoding will be unique.
Above imitation algorithm have problem. since here we have only one-
dimensional linear tape on the UTM, we require two-dimensional
description of T unless we use some coding to convert the two-
dimensional information into one-dimensional.
:. So we can design a UTM in following way.
117
Theory of Computation The Turing machine is described as a multitape. TM as shown:
Figure 8.2
118
a) Change the contents of third tape to 0k. Means it should able simulate Undecidability
the state change of M, for that change , U first changes all the 0’s on
tape 3 to blanks and then copies 0k from tape 1 to tape 3.
c) Now, move the head on tape2 to the position of the next 1 to the left or
right respectively, depending on weather m = 1 (move left) or m = 2
(move right) . Therefore U makes move of M to the left or to the right.
v) If there is no match in M for transition that matches the simulated state
and tape symbol, then in (iv), no transition will be found. Thus M halts
in the simulated
Configurationand U must do likewise.
vi) If M enters into its accepting state, then U accept.
119
Theory of Computation (I) (Acceptance problem for DFA) Given a DFA does it accept a
given word?
(II) (Emptiness problem for DFA) Given a DFA does it accept any
word?
(III) (Equivalence problem for DFA) Given two DFAs, do they accept
the same language?
If answer to above examples is yes then language generated by above is
decidable.
• Undecidable language –
A decision problem P is said to be undecidable if the language L of
all yes instances to P is not decidable or a language is undecidable if
it is not decidable. An undecidable language maybe a partially
decidable language or something else but not decidable. If a language
is not even partially decidable , then there exists no Turing machine
for such language.
Halting is a situation in the program that tells us on certain input will
accept it and halt or reject it and halt and it would never go into an
infinite loop. Basically halt shows terminating the current program or
algorithm execution on particular input condition.
So can we have an algorithm which will tell us that is given program will
halt or not. In terms of Turing machine, will it terminate when run on
some machine with some particular given input string.
The answer is no we cannot design a generalized algorithm which can
appropriately say that given a program will ever halt or not?The only
way is to run the program and check whether it halts or not.We can solve
the halting problem question in such a way also: Given a program
written in some programming language(c/c++/java) will it ever get into
an infinite loop(loop never stops) or will it always terminate(halt)?
It is an undecidable problem because we cannot have an algorithm which
will tell us whether a given program will halt or not in a generalized way
at certain point in executioni.e by having specific program/algorithm.In
general we can’t always know that’s we can’t have a general algorithm.
The best possible way is to run the program and see whether it halts or
not.In this way for many programs we can see that it will sometimes loop
and always halt.
The halting problem is a decision problem about properties of computer
program on a Turing Machine computation model. The problem is to
determine, for a given program and an input to a program, whether the
program will halt when run with that input.
In this abstract environment, there is no resource limitations of memory or
time on the program’s execution, before halting it can take arbitrary long
storage space. The question is that whether the given program will ever
halt on a particular input.
120
Example Undecidability
Figure 8.3
121
Theory of Computation where,
e(M): Encoding of M
i.e. e(M) is for example a set of 5-tuples (q, X1, p, r, R) that describe the
TM.
Theorem
HALTTM={(M, w) | The Turing machine M halts on input w} is
undecidable.
Proof :
We assume that HALTTMis decidable, and get a contradiction. Let M1
be the TM such that T(M1 ) = HALTTM and let M1halt eventually on all
(M, w).We construct a TM M2 as follows:
1. For M2, (M, w) is an input.
2. The TM M1 acts on (M, w).
3. If M1 rejects (M, w) then M2rejects (M, ,w).
4. If M1 accepts (M, w),simulate the TM M on the input string w until M
halts.
5. If M has accepted w, M2 accepts (M, w); otherwise M2rejects (M, w).
122
When M1 accepts (M, w) (in step 4), the Turing machine M halts on w. Undecidability
In this case either an accepting state q or a state q' such that δ(q', a) is
undefined till some symbol a in w is reached. In the first case (the first
alternative of step 5) M2accepts (M. w). In the second case (the second
alternative of step 5) M2 rejects (M, w).
It follows from the definition of M2that M2halts eventually.
Also,T(M2) = {(M, w) | The Turing machine accepts w} = ATM
This is a contradiction since ATM isundecidable.
123
Theory of Computation 5. Given two TMs, M1 and M2, over the same alphabet, do M1 and M2 halt
on the same set of input strings?
b) Unsolvable Problems about (General) Grammars:
Unsolvability results can also be shown about grammars, using
reductions.These
problems are unsolvable.
8.9 SUMMARY
125
Theory of Computation • A transition of a standard turing machine is given as:
δ (qi, x) = [qj) y, d]
Where qi, qj∈ Q, x, y ∈ Г and d ∈ {L, R)
8.10 REFERENCES
❖❖❖❖
126