department of computer science and engineering · 2019-07-20 · m.i.e.t. engineering college...
TRANSCRIPT
M.I.E.T. ENGINEERING COLLEGE
(Approved by AICTE and Affiliated to Anna University Chennai)
TRICHY – PUDUKKOTTAI ROAD, TIRUCHIRAPPALLI – 620 007
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING
COURSE MATERIAL
CS6503-THEORY OF COMPUTATION
III YEAR - V SEMESTER
M.I.E.T. ENGINEERING COLLEGE
(Approved by AICTE and Affiliated to Anna University Chennai)
TRICHY – PUDUKKOTTAI ROAD, TIRUCHIRAPPALLI – 620 007
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
SYLLABUS THEORY Sub. Code : CS6503 Branch/Year/Sem : CSE/III/V Sub Name : THEORY OF COMPUTATION Batch : 2017-2021 Staff Name : G.MEGALA Academic Year : 2019-2020
UNIT I FINITE AUTOMATA CS6503 Syllabus Theory of Computation
Introduction- Basic Mathematical Notation and techniques- Finite State systems – Basic Definitions
– Finite Automaton – DFA & NDFA – Finite Automaton with €- moves – Regular Languages-
Regular Expression – Equivalence of NFA and DFA – Equivalence of NDFA‟s with and without €-
moves – Equivalence of finite Automaton and regular expressions –Minimization of DFA- –
Pumping Lemma for Regular sets – Problems based on Pumping Lemma.
UNIT II GRAMMARS CS6503 Syllabus Theory of Computation TOC
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
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
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
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.
M.I.E.T. ENGINEERING COLLEGE
(Approved by AICTE and Affiliated to Anna University Chennai)
TRICHY – PUDUKKOTTAI ROAD, TIRUCHIRAPPALLI – 620 007
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Sub. Code : CS6503 Branch/Year/Sem : CSE/III/V Sub Name : THEORY OF COMPUTATION Batch : 2017-2021 Staff Name : G.MEGALA Academic Year : 2019-2020
COURSEOBJCTIVES:
Student will be able to,
Understand various computing models like finite automata, Push down data and turning machine.
Understand grammar for a language. Prove equivalance of language described by automata. Acquire awareness of decidability and undecidability of various problem.
COURSE OUTCOME:
S.NO DESCRIPTION BLOOM’S TAXONOMY (BT)KNOWLEDGE LEVEL
CO1 Student will be able to design automata and prove a statement
apply
CO2 Construct regular expression for a pattern apply
CO3 Correlate different types of automata to real world applications
apply
CO4 Design a turning machine to solve problem on mathematical foundations
apply
CO5 Decide whether a problems is decidabel or not understand
CO6 Identify different computational complexities understand
UNIT –1 AUTOMATA FUNDAMENTALS
INTRODUCTION
The study of abstract devicea is the called automata theory.
Finite Automata is a mathematical model that accepts a set of inputs, process
through a set of states, generates an output.
Automation helps in
o Designin and checking the behavior of digital circuits
o Pattern searching in websites
o Verifying systems like communication protocols for secure data transfers
o Designing lexical analyzers in compliers
Finite automation model is a study of machines.
History
Alan Turing introduced an abstract machine in 1930‟s. This machine had all the
Start OFF ON
capabilities of today‟s computer.
In 1940‟s and 1950‟s, the machines were simplified to finite automation machines.
Researchers proposed the automation model in order to model the functions of human brain.
The linguist Noam Chomsky made a study on formal grammars in late 1950‟s .These
grammars provided the basis of compliers.
S.Cook extended Turing‟s study in 1969, which separated the problems as solvable
and unsolvable. He classified problems as NP-Hard and as NP-Complete.
Example of a Finite state system: Switch operation
PUSH
PUSH
1.2 Theory of Computation
t t
h th
e the
n then
a a
u au
t aut
o auto
m autom
a automa
t automat
Here,
When the electric switch = “ON” indicates logic „1‟ when pushed from start state.
Goes to „OFF‟ state when pushed from „ON‟ state.
Example: Pattern searching of sting = “then”
Example: Pattern searching of sting = “automata
BASIC MATHEMATICAL NOTATION AND TECHNIQUES
Basic Mathematical
Objects Sets [A]
A set is collection of elements of finite number.
Example:
A = {10, 01, 00, 11}
B = {w | w is a set of prime numbers less than 100}
w B
C = {r | r is a set of strings generated from vowels}
r C
Subset [ ]
Let A1and A2 are two sets, then A1 is a subset of A2 [indicated as A1 A2 ], if every
element of A1 is in A2.
Example:
A1 = {1, 2, 3, 4}
A2 = {1, 2, 3, 4, 5}
A1 A2
a automata
Automata Fundamentals 1.3
Complement of a Set [A']
Let A be a set of finite number of elements. Then A' is a set of elements that are not
the elements of set A. [A' A]
Example:
A1 = {a, e, i, o, u}
A' = { all alphabets except vowels}
Operations on Sets
Union [ ]
The union of two sets results in a set containing the elements of both the sets (without
repeatition of elements).
Example:
A1 = {1, 3, 5, 7}
A2 = {1, 2, 3, 4,}
A1 A2 = {1, 2, 3, 4, 5, 7}
Intersection [ ]
The intersection of two sets contains a set of elements that are available in both the
sets.
Example:
A1 = {1, 3, 5, 7}
A2 = {1, 2, 3, 4}
A1 A2 = {1, 3}
Difference [ - ]
The difference of two sets A and B results in the set of elements that are in A and not
in B.
Example:
A1 = {1, 3, 5, 7}
A2 = {1, 2, 3, 4}
A1 – A2 = {5, 7} [A1 – A2 = A1 A2']
1.4 Theory of Computation
Laws on Sets
Commutative Laws
A
A
B = B A
B = B A
Associative Laws
A ( B C) = (A B) C
A ( B C) = (A B) C
Distributive Laws
A (B C) = (A B) (A C)
A (B C) = (A B) (A C)
Idempotent Laws
A A= A
A B = A
Absorptive Laws
A (A B) = A
A (A B) = A
De Morgan‟s Laws
(A B) ' = A' B'
(A B) ' = A' B'
Other Laws
(A') ' = A
A A' =
A A' = Universal set, ‟U‟
A = A
A =
A U = U
A U = A
Automata Fundamentals 1.5
Equality of sets [ = ]
Two sets are set to be equal if both the sets contain the same elements.
Example:
A = {1, 3, 5, 7}
B = {set of odd numbers less than 8}
Then, A = B
Empty set [ ]
A set containing no elements is said to be an empty set.
Example:
A = { set of natural numbers between 1 and 2}
A = or { }
Power of a set [ A
k ]
The power of a set A, denoted by A k is the set containing all elements of A, whose
length is less than or equal to k.
Example:
A = {1, 2}
A2 = { , 1, 2, 12}
Relationship of Sets
Relation of two sets is the association of one set with other.
Types based on the properties of sets
1) Reflexive: Every element of the set is associated with itself.
2) Symmetric: Let a, b ϵ A, then a is related to b, and b is related to a [aRb = bRa]
3) Transitive: If a b and b c, then a c. It is denoted as: aRb, bRc then aRc.
Logic
Logic deals with the logical proportions and connectives.
Proposition
Proposition is a meaningful, objective, declarative statement that has a truth value as
true / false.
1.6 Theory of Computation
Example:
0 1, New Delhi is the capital of India, 5 is an even number, Chennai is in AP.
Connective
Logical connective is a compound statement formed from simple proportions using a
connective such as
and connective /conjunction
or connective /disjunction
Not / Negative connective
Example
p Q p ^ q p q p q
T T T T F F
T F F T F T
F T F T T F
F F F F T T
Implication
It is a conditional statement of the form, “if p then q” (p implies q), where p and q are
simple proportions.
It is denoted as p q = p q
p q p q
T T T
T F F
F T T
F F T
Proofs
A proof is single line / multiline derivation that provide a convincing argument to
make a statement true.
Proofs can be derived from assumptions or facts or existing derivations.
Automata Fundamentals 1.7
Forms of Proofs
There are basically two forms of proofs. Thery are,
1) Deductive Proofs
2) Inductive Proofs
Deductive Proofs
These are sequence of statements which are derived from any assumption (hypothesis)
or a given initial statement to a conclusion statement.
The proofs are generated using some facts / accepted logics.
Example: If x 4, 2x x
2
The hypothesis statement x 4
Conclusion statement 2x x
2
This can be provided as substitution statement as:
When x = 4:- 24 4
2 16 16 True
When x = 5:- 25 5
2 32 25 True
When x = 6:- 26 6
2 16 36 True
When x = 3:- 23 3
2 16 9 False
Hence proved.
Inductive Proof
It has a sequence of recursive / parametrical statements that handle the statement with
lower values of its parameters.
There are two types of inductions, namely -
Mathematical Induction
Structural Induction
Mathematical Induction
This follows induction principle that follows two steps, namely-
Basis of Induction: It considers „n‟ of f(n) as the lowest possible integer (n = 0 or 1)
and prove the given statement using substitution.
1.8 Theory of Computation
Inductive step: Assume that the given statement is true for nth
value and prove the
statement for n = n+1.
This applies for all integer related statement.
Structural Induction
Structural Induction follows the mathematical induction concept but applies for trees
and expressions.
Example: Every tree has one node than it has edges, balancing parenthesis etc.
Additional forms of proofs
Proofs about sets
Proof by contradiction
Proof by counter example
Direct Proofs
Proofs about set
Proof about the set includes the proofs on the properties of the sets.
Example: Commutative law, Distributive law etc
Consider, A = P Q, B = Q P
Since P Q = Q P ( by Commutative law), we prove that A = B.
This proof follows “if and only if” type. This is A= B is true if and only if
P Q = Q P is true.
Proof by contradiction
The proof p q is true, then the method follows a contradictory assumption that
p q and from the result, it proves the assumption is false and thus proves, p q.
Example:
To prove P Q = Q P, we first assume that P Q ≠ Q P.
Now consider an element, x from P that is also in Q.
x ϵ P Q
Thus all the element of P is taken and compared with that of Q.
Automata Fundamentals 1.9
Finally , P Q = Q P is proved [contradiction to our assumption]
Proof by Counter Example
Given a statement, that can be proved to be true for a domain of input instances, and
false for other inputs.
Example: a mod b ≠ b mod a
Let a = 5 and b = 10
a % b = 5 % 10 = 5
b % a = 10 % 5 = 0
Hence a % b b % a, the statement is true only if a = b.
Thus proved.
Inductive Proofs
These are special type of proofs used to prove recursively defined objects.
It consists of sequence of parameterized statement that uses the statement itself with
lower values of its parameter.
Two steps
1. Basis of Induction We start with the lowest possible value.
Example: To prove f(n) . We take n = 0 or 1 initially.
2. Inductive step Here we prove if f(n) is true, f(n+1) is also true.
SAMPLE PROBLEMS:
1. Prove that: 1+2 +3+ ………+n (n+1) / 2 using method of induction for (n>0).
Proof:
Basis of Induction
Let n = 1 [We cannot take n = 0 since U.H.S starts from 1]
L.H.S = 1
111 12 2
R.H.S 1 2 2 2
1.10 Theory of Computation
n
Since L.H.S = R.H.S Proved.
Inductive Step
We have 1 2 3 n n n 1
2
Sub n = n + 1
L.H.S 1 + 2 + 3 +…… + n = i i1
When we substitute n = n+1,
n 1
L.H.S = i i1
Eg: i i 5 i1 i1
= i (n 1) i1
= n(n 1)
(n 1) 2
= n(n 1) 2(n 1)
2
=
=
R.H.S = n(n 1)
2
n2 n 2n 2
2
n2 3n 2
2
----- (1)
When we substitute n=n+1,
R.H.S = (n 1)((n 1) 1)
2
= (n 1)(n 2)
2
= n2 2n n 2
2
n
5 4
Automata Fundamentals 1.11
n
= n2 3n 2
2 ----- (2)
From (1) and (2), L.H.S = R.H.S
So the given hypothesis is proved.
2. For all n 0 i2
n(n 1)(2n 1)
i1 6
Basis of induction
Let n=1 (we can‟t take n=0 since i=1 in L.H.S)
L.H.S = i2 = 12 = 1
i1
R.H.S = 1(1 1)(2(1) 1)
1(2)(2 1)
2(3)
6 1
6 6 6 6
L.H.S = R.H.S.
Hence basis of induction is proved.
Inductive step
i2
i1
n(n 1)(2n 1)
6
L.H.S: Sub n=n+1
n 1
= i2
i1
= i2 (n 1)2
i1
i2 i2 (3)2
i1 i1
= n(n)(2n 1)
(n 1)2
6
= n(n 1)(2n 1) 6(n 1)2
6
= n(2n2 n 2n 1) 6(n2 2n 1)
6
3 2 n
n
n
1.12 Theory of Computation
= 2n3 n2 2n2 n 6n2 12n 6
6
= 2n3 9n2 13n 6
6
----- (1)
R.H.S: Sub n=n+1
n(n 1)(2n 1)
(n 1)((n 1) 1)(2(n 1))
6 6
= (n 1)(n 2)(2n 2 1)
6
= (n2 2n n 2)(2n 3)
6
= 2n3 3n2 4n2 6n 2n2 3n 4n 6
6
= 2n3 9n2 13n 6
6
From (1) and (2) L.H.S = R.H.S
The hypothesis is proved.
3) P.T for every integer, n 0 the number 42n1
3n2
is a multiple of 13.
Solution
Basis of induction
Let n = 0
L.H.S = 42(0)1 3(0)2
= 41 3
2 4 9
= 13 multiple of 13
Inductive proof
Let n=n+1
42n1 3n2 42(n1)1 3(n1)2
= 42n21 3n3
Automata Fundamentals 1.13
= 42n3 3n3
= 42n1.42 3n2.31
= 16(42n1
) 3(3n2
)
= 13(42n1
) 3(42n1
) 3(3n2
)
= 13(42n1
) 3(42n1
3n2
)
Multiple of 13
= 13(42n1
) 3(13n)
= 13(42n1
3n) multiple of 13
The hypothesis is proved.
4) Show that n4 4n
2 is divisible by 3 for n 0
Basis of induction
Consider n = 0
n4 4n
2 0 0 0 3 0 Divisible by 3
Hence proved.
Inductive step
Let us assume that n4 4n
2 is divisible by 3 for n, and try to prove the same for n=n+1
n4 4n
2 (n 1)
4 4(n 1)
2
Using induction hypothesis,
(n 1)4 4(n 1)
2 n
4 4n
2 ((n+1) - n
th term = a multiple of 3)
n4 4n
3 6n
2 4n 1 4n
2 8n 4 n
4 4n
2
n4 4n
3 2n
2 4n 3 n
4 4n
2
4n3 6n
2 4n 3
4n(n2 1) 6n
2 3
1.14 Theory of Computation
4n(n 1)(n 1) 6n2 3 Multiple of 3 for all n 0
Divisible by 3
FINITE AUTOMATA
Basic Definitions
Alphabet ( )
An alphabet is a finite, non empty set of symbols.
Example:
String (w)
0,1 Binary alphabet
a, b, c, .... zLower - case letters set
1, 2,3 Digits
A string is a finite sequence of symbols chosen from some alphabet.
Example:
01101 is a string from 0,1
aa, bb, ab, ba are strings from a, b
Empty string () / Null string / /
An empty string is the string with zero occurrences of symbols.
Length of a string ( )
Let be the string, then the length of the string, is the number of symbols
composing the string.
Example:
010 ; 3
; 0
abcba ; 5
Automata Fundamentals 1.15
Concatenation of strings
The concatenation of two strings '' and ' v ' is the string obtained by appending the
symbols of v to the right end of .
Example:
010
v 111
v 010111
Power of an alphabet ( k )
If is an alphabet, we can express the set of all string of a certain length from that
alphabet by using an exponential notation.
We define, k to be the set of strings of length k, each of whose symbol is in
Example:
0
Let a, b, c
0
1 a, b, c
2 aa, bb, cc, ab, ac, ba, bc, ca, cb
3 aaa, bbb, ccc, aab, aac, aba, aca, abb, acc, abc, acb, bba, bbc, bab, bcb, baa, bcc, .....
Reversing a string ( R
)
The reverse of the string is obtained by writing the string in reverse order.
Example:
abc
R cba
Kleene closure ( * )
Let be an alphabet. Then the kleen closure, *
the alphabet,
denotes the set of all strings over
1.16 Theory of Computation
Example:
1) Let
2) Let
a, b
* , a, b, aa, bb, ab, ba, aaa, aab,...
1
0
1 1
2 11
* 0 1 2 .....
,1,11,111,...
Kleene plus / Positive closure ( )
Let be an alphabet. Then the positive closure,
over the alphabet, except null string () .
Example:
denotes the set of all strings
Substring
Let 1, 0
1 2 3 ...
1 1,0
2 11, 00,10, 01
3 111, 000,110,101, 011,...
1, 0,11, 00,10, 01,111, 000,101,...
*
The string, v if it appears within another string , then v is called as the substring
of .
Automata Fundamentals 1.17
Example:
Let v111 ; 010111
v is a substring of
Let 1, 2,3
Prefixes ,1,12,123
Suffixes ,3, 23,123
Palindrome
A palindrome is a string which is same when read in backward or forward direction.
R is a palindrome
Example: 1001
R 1001 equal is a palindrome.
Language (L) Alphabet finite set of symbols
String collection of alphabets
Language collection of appropriate strings
A set of strings taken from an alphabet is called a language.
Example:
1) {a, b}
L{an
b, n 0}{b, ab, aab, aaab, aaaab, .... }
2) {0,1}
L{Set of stringsending with11}
= {ε, 11, 011, 0011, 1011, 0111, 101111, …}
Properties of String Operations
1. Concatenation of a string is always associative.
(uv) w u(vw)
2. If u and v are two strings, then the length of this concatenation is the sum of the
langth of the individual lengths.
1.18 Theory of Computation
2
1
| xy || x | | y |
Example: Let x 10
y101
Then xy10101Concatenation.
| xy || x | | y |
2 3
5
Operations of Languages
1. Product or concatenation (L1 L2 )
L {b, ab, aab, ... }{an b, n 0}
L {1, 01, 001, .... }{0n1, n 0}
L L {an b 0
n1, a
n bL & 0
n1L }
1 2 1 2
2. Reversal (LR )
The reversal of a language is a set of all string reverses.
LR {w
R / wL}
3. Kleen Star/ Star Closure (L* )
L* L
o U L
1 U L
2 U....
4. Positive Closure
L L
* {}
L1 U L
2 U L
3 U....
Other Operation on Set/Language
1. Union
L1 L2 = {x | x ϵ L1 or x ϵ L2}
2. Intersection
L1 L2 {w, wL1 and wL2 }
3. Difference:
L1 L2 {w, wL1 and wL2 }
Automata Fundamentals 1.19
Definition of Finite Automata [FA]
Finite Automata is a mathematical model of system with certain input and it is
processed through various intermediate states and finally gives certain output.
Model of FA
Infinite tape
Read Head
CPU
Input Tape
It is divided into number of cells.
Each cell holds one symbol.
Read Head
Reads one cell at a time and moves ahead.
Finite Control
Acts like a CPU.
Depending on current state and input symbol read from tape, it changes state.
Formal Definition of FA
M(Q, , , q0 , F)
FA consist of 5 Tuples,
Q Set of finite states.
Set of input alphabets
Transition function [Q Q]
q0 Start state
F Set of final states. [FQ]
Representation of FA
1. Transition Diagram
2. Transition Table
Finite
control
a1 a2 a3 a4 …
1.20 Theory of Computation
q0 0
q1 1
q2
q0 a
q b
1 q2
Transition Diagram
It can be directed graph with vertices of a graph corresponding to states and edges
indicates transition from one state to another
Start state indicated by circle.
Start
state Final
state
Start transition indicated by arrow.
Final state indicated by double circle.
Transition Table
It is a tabular listing of the transition function which by implication tells us, set of
states and input alphabet.
b
Applications
1. Design of digital circuits.
2. String searching
3. Communication protocols for information exchange.
4. Lexical analysis phase of a compiler.
(q0 , a) q1
(q0 , b) q0
(q1 , b) q2
Types
1. Deterministic Finite Automata [DFA]
2. Non-Deterministic Finite Automata [NFA]
State
Input
a b
q0 q1 q0
q1 q2
*q2
Automata Fundamentals 1.21
q0 b
q1
DETERMINISTIC FINITE AUTOMATA [DFA] /FA
The word deterministic refers to the fact the transition is deterministic.
There is only one path for specific input from current state to next state.
DFA is defined by 5 tuples,
A(Q, , , q0 , F)
where,
Q Finite set of states.
Finite set of input symbols.
q0 Initial state
Transition function
F Final state / Accepting state
Properties of DFA
There is only one path for an input from current state to next state.
It contains only one final state.
PROBLEMS
1. Design a DFA for the language, L {anb; n 0}
If n 0La0
b b
n 1La1 b ab
n 2La2
b aab
n 3La3
b aaab
n 4La4
b aaaab
n 5La5 b aaaaab
a
Here, Q{q0 , q1};
{a, b}
q0 {q0 }
Transition function,
(q0 , a) {q0 }
1.22 Theory of Computation
q0 0
q1 1
q2
0 0
Trap q2
0, 1
(q0 , b) {q1}
(q1 , a)
(q1 , b)
F{q1}
2. Design a DFA that accept L {w / w (a, b, c), and w contains only abac}
Valid input: abac
Invalid inputs: aa, abc,
abaca, bbca, …
3. Design a DFA that accept L {w (0, 1)*, where every 0 in w has 1 immediately to
its right.
Valid input
01
0101
1 1 101, 1101
Invalid inputs:
00, 10, 1100, 1001, ….
4. Design a DFA that accept only „101‟.
Valid input: 101 q0
1 q
0 1 q
1 2 q3
q 0 1
0 q1 q2
q0 0
q1 1
q2
0
q0 a
q b
1 q a
2 q c
3 q4
a, c b, c
b, c
a, b
a, b, c
q2
Trap
Automata Fundamentals 1.23
Invalid inputs:
00, 100, 010, 1010, …
5. Design a DFA that accepts string that starts with „ab‟.
a, b
Valid inputs:
101
aba, abb, abaa, abbb,
abab, ….
q0
a b q1 q2
a, b
Invalid inputs:
ba, aa, aab, bab, aaaa,
baab, ….
Trap
b a
q1
a, b
6. Design a DFA that accepts all strings of length atmost 5 over {0, 1}
0, 1 q0 q1
0, 1 q2
0, 1 q3
0, 1 q4
0, 1 q5
Valid inputs:
0, 00, 000, 0000,
00000, 1, 11, 111,
1111, 1111, 10101,
010, 100, 0010, …
Invalid inputs:
101010, 0000000,
11110000,
Trap
q1
0, 1
0, 1 100011001,
1111111111, …
q0 a
q b
1 q2
q0 0, 1
q1 0, 1
q2 0, 1
q3 0, 1
q4
0, 1 q5
q0 a
q b
1 q2
q0 1 q
0 1 1 q 2 q3
1 0
0 0, 1
q4
Trap 0, 1
1.24 Theory of Computation
q2 0
0
1 1 0
q1 0 q1
7. Design a DFA that accepts even number of a‟s over {a}
Valid inputs:
aa
aaaa, aaaaaa, a8, a
16, ...
Invalid input
a, aaa, aaaaa, a7, a
17, ...
8. Design a DFA that accepts a language, L having even number of 0s and 1s.
Valid inputs:
00, 11, 0000, 1111, 08, 18, …
q1 0110, 0101, 1010, 1100, 110011,
10101010, 00001111, ...
1 1 Invalid inputs
0, 1, 000, 111, 101010, 110, 1101,
0100, …
0
q2 q1
0
1 1
q1
q0 a
q a
1 q2
q0 a
q a
1 q2
a
a q3 a
q0 q2
a q1 a
Automata Fundamentals 1.25
9. Design a DFA that accepts all strings containing atleast two zeroes.
Valid inputs:
00
0, 1
1
0, 1
1 1
0, 1
00, 000, 0000, 00000, 0001,
000111, …
100, 1100, 11100000,
110011, 1001, 11000010, …
1010, 10110, 11011011,
0101, 1101101101, …
Invalid inputs:
1, 10, 111, 0111, 101, 11011,
01, …
10. Design a DFA that accepts strings ending with „00‟ over {0, 1}
Valid inputs:
00
0 000, 0000, 00000, …
1
0 100, 1000, 1100 110000, …
1
0 0100, 101000, 110100, …
1
0 00100, 1000, 1010100, 001000
Invalid input:
001, 0011, 101, 10011, 1, 01, ..,
q0 0
q 0
1 q2
q0 0
q1 0
q2
q0 0
q 0
1 q2
q0 0
q 0
1 q2
1
q0 0
q 0
1 q2
1 1
q 0 0
0 q1 q2
q0 0
q 0
1 q2
q0 0
q 0
1 q2
q0 0
q 0
1 q2
1.26 Theory of Computation
q0 0
q1 0
q2
0
q0 0
q1 1
q 1
2 q3
1 q4
1
11. Design a DFA that accepts a language, L which has the number of zeroes is of
multiples of 3.
0 0 q0 q1 q2
Valid inputs:
000, 000000, 09, 0
12, …
0 1
1, 1000, 100000, …
1 1
10100, 11000, 101100, …
101010, 1101010, 1101110110, …
Invalid inputs: 0,00,10000,01011, …
12. Design a DFA that accepts L 01i 1
2j , i 1; j 1
Valid inputs:
i = 1, j = 1 L = 0111
i = 1, j = 2, L = 011111
i = 1, j = 3, L = 01111111
i = 2, j = 1 L = 010111
i = 3, j = 1, L = 01010111
i = 2, j = 2 L = 01011111
i = 3, j = 3, L = 010101111111
q0 0
q1 1
q2 1 q3
1 q4
q0 0
q1 1
q 1
2 q3
1 q4
0 1
q0 0
q1 0
q2
0
1 1 1
q 0
0 q 0
1 q2
0
Automata Fundamentals 1.27
q q
b
1
q0 0
q1 1
1 3 q4
1
Invalid inputs:
1, 00, 0100, 0110, 01110,
011110, 1000, 101, …
0 0
0
Trap
Q
0, 1
13. Design a DFA that accepts L a, b*
/ n mod 3 1
Valid inputs:
bb, b5, b8, b11 [No. of b%3]
bb 2b‟s 2% 3 2
> 1
a
a a
a a a
bbbbb 5b‟s 5%
3 2 > 1
bbb 3b‟s 3% 3 0
> 1
abb, abbbbb, ab8, …
a-b2, a3b5, ...
bab, abab, aabbbbb, …
bba, bbaa, abba, abbaa, ….
Invalid inputs:
b, b3, b4, b6, b7, b9, a, aa, aaa, aba,
baa, ababb
q0 b
q1 b
q2
b
q0 b
q1 b
q2
b
q0 b
q1 b
q2
b
q0 b
q1 b
q2
b
2
1.28 Theory of Computation
14. Design a DFA accepting strings containing exactly one 1 over {0, 1}
Valid inputs:
1
0
01, 001, 0001, …
0 0
10, 100, 1000, 010, 0100, …
0 0
Invalid inputs:
11
0 0
0110, 0101, 1111, 101,…
0, 1
15. Design a DFA that accepts all strings with exactly two 1s over {0, 1}
Valid inputs:
1
0
011, 0011, 00011, …
0 0
0101, 1001, 101, …
q0 1
q1
q0 1
q1
q0 1
q1
q0 1
q1
1
Trap q2
q0 1
q1
1
Trap q2
1 q0 q1
1 q2
q0 1
q1 1
q2
1 q0 q1
1 q2
Automata Fundamentals 1.29
q0 0
q1 0
q2
0 1
110, 01010, 1010, 11000 ….
Invalid inputs:
111, 1110, 0111, 10101, 1111,
1110101, …
0, 1
16. DFA that accepts strings containing „00‟ as substring.
Valid inputs:
00
0
000, 0000, 00000, …
1 0
100, 1100, 11000, …
10100, 1101100, 101101, ….
1
q 0
q 0
1
0, 1
q2
Invalid inputs:
1001, 11001, 101001, …
0 0 0
1 q0 q1
1 q2
0 0 0
1 q0 q1
1 q2
1
Trap q3
q0 0
q1
0 q2
q0 0
q1
0 q2
1 0
q0 0
q1 0
q2
1
1.30 Theory of Computation
17. Design a DFA that accepts string, such that its second symbol is zero and fourth
symbol is 1.
Valid inputs:
0001
1001
0011, 1011
0, 1 0
q0 q1
0, 1 1
q2 q3
0, 1
q4
00010, 10010, 00110, 00011,
10011, 00111, …
0, 1 0 0 q1
0, 1 1 q2 q3
0, 1
q4
Invalid inputs:
0101, 0100, 0010, 1100,
0000, 1000, …
1 0
Trap q5
0, 1 0
q0 q1
0, 1 1
q2 q3
0, 1
q4
01010, 01000, 11111,
0100000, …
1 0
Trap q5
0, 1
q0 0
q1 0
q 0
2 q3
1 q4
q0 0, 1
q 1 0
q2 0
q 1
3 q4
q0 0, 1
q1
0 q2
0 q3
1 q4
q
Automata Fundamentals 1.31
18. Design a DFA that accepts atmost 3 a‟s.
Valid inputs:
aaa, a, aa,
b
b, ba, baa, baaa, bb, bba, bbba,
…
bab, abba, abbbaa, babba,…
babab, aabb, aaba, bbbaaba, …
aaabbb, aaabab, baaaba, …
Invalid inputs:
aaaa, aaabab, baaaba,
b b b b
a a a q0 q1 q2 q3
a
a, b q4
Trap
a a a q0 q1 q2 q3
a a a q0 q1 q2 q3
b b
a a a q0 q1 q2 q3
b b b
a a a q0 q1 q2 q3
b b b b
a a a q0 q1 q2 q3
1.32 Theory of Computation
1
0 2
q 1 0 1
0 q1 q2 q3
q a b b
0 q1 q2 q3
19. Design a DFA that accepts all strings containing substring „101‟.
0
0 1
0 1 0, 1
q0
1 q q
1
0
0 1
q3
0,1
20. Design a DFA to check whether a string over {a, b}contains „abb‟ is accepted.
b
b a
b a
a b q0 q1
a
q2 b
q3
b a a, b
a b q0 q1
a
q2 b
q3
q0 a
q b
2 b
1 q q3
q0 a
q1 b
q2 b
q3
q0 1
q 0
1 q 1
2 q3
q0 1
q 0
1 q 1
2 q3
q0 1
q1 0
q2 1
q3
0
Valid inputs:
101
0101, 000101, …
011101
10100, 101, 101101, …
Complementary
[Does not accept
substring with „101‟]
Valid inputs:
abb
babb, bbabb, …
aabb, baaabb, …
ababb, abaabb, bababb, …
abba, abbb, abbab
Invalid inputs:
ab, bbb, aaaa,abab, aaba,…
Automata Fundamentals 1.33
q 0 0 1
0 q1 q2 q3
21. Design a DFA that accepts odd number of ones over {0, 1}
Valid inputs:
1
q0
1 q
1
111, 11111, 1
7, 1
9, 1
11, …
0
01, 001, 0000111, …
0 0
10, 1011, 10001111, 01101,…
Invalid inputs:
11, 1111, 0110110, 111100110, …
22. Design a DFA that accepts a substring “001”.
1
1 0
1 0
0 0 1 0 q1 q2
1 0
0 0 1 0 q1 q2
1
0, 1
q3
0, 1
q3
q0 0
q1 0
q2 1
q3
q0 0
q 0
1 q 1
2 q3
Valid inputs:
001
1001, 11001, 111001, …
0001, 000001, …
00010, 110011, 110010, …
101001, 10101001, …
Invalid inputs:
1111, 0111, 0101, 000, …
q0 1
q1
q0 1
q1
1
q0 1
q1
1
1
q
q
1.34 Theory of Computation
23. Design a DFA that recognize words which do not end in „b‟ over {a, b}
a Valid inputs:
a, aa, aaa, aaaa, …
a
q0 b
q1
a
a b
q0 b
q1
a
aaba, ba, aba, …
aabbaa, bba, bbabba, …
Invalid inputs:
b, bbb, abb, aabb, abab, …
24. Design a DFA that accepts exactly 4 ones over {0, 1}
Valid inputs:
1111
01111, 010100, 01010101,
1111000, 1101100, …
Invalid inputs:
111101, 10101011, 110111, …
1111010, 1111011, …
q0 1 q1
1 q
1 2
q3
1 q4
0 0 0 0 0
q0 1
q 0 1
1 0
q 2 q 3 q4
0 0 0 0 0
q0 1
q 1 1
1 1
q 2 q 3 q4
1
Trap q5
0 0 0 0 0
q0 1
q 1 1
q 1 1
2 q 3 q4
1
q5 0, 1
q0
Automata Fundamentals 1.35
a q0 q1
b q2
a a
q0 b
q1
b
Extended transition function: δ
Delta Cap/Hat
The extended transition function, takes two parameters, state and string.
It is a mapping from Q * Q
Formally cann be defined as
o (q0 ,) q0
o (q0 , a) ((q0 , ), a)
PROBLEMS
1) Consider the transition diagram
b a
a, b
Check whether the input string,”aab” is accepted by the finite automata.
(q0 ,) q0
(q0 , aab) ((q0 , aa), b)
(((q0 , a), a), b)
((((q0 ,), a), a), b)
(((q0 , a), a), b)
((q1 , a), b)
(q1 , b)
q2 Final state
Hence the given string “aab” is accepted by FA.
2) Consider the FA
1.36 Theory of Computation
a b
q0 q1 a
q2
a, b
Show how the string ω1 =aabba and ω2 =aba is processed.
1) 1 aabba
(q0 , aabba) (q0 , abba)
(q0 , bba)
(q1 , ba)
(q0 , a)
q0 Final state string is accepted
2) 2 aba
(q0 , aba) (q0 , ba)
(q1 , a)
q1 Not a final state string not accepted
3) Check whether the strings “ababba” ,”baab” are accepted by the DFA?
AU – May / June, Nov / Dec 2009
1) 1 ababba
(q0 , ababba) ((q0 , ababb), a)
(((q0 , abab), b)a)
((((q0 , aba), b), b), a)
(((((q0 , ab), a), b), b), a)
((((((q0 , a), b), a), b), b), a)
((((((((q0 , ), a), b), a), b), b), a)
(((((((q0 , a), b), a), b), b), a)
Automata Fundamentals 1.37
((((((q0 , b), a), b), b), a)
(((((q1 , a), b), b), a)
((((q2 , b), b), a)
(((q1 , b), a)
((q1 , a)
(q3 , )
q3 Not a Final state Not accepted
2) 2 baab
(q0 , baab) ((q0 , baa), b)
(((q0 , ba), a), b)
((((q0 , b), a), a), b)
(((((q0 , ), b), a), a), b)
((((q0 , b), a), a), b)
(((q1 , a), a), b)
((q2 , a), b)
(q1 , b)
q1 Final state string accepted
NON DETERMINISTIC FINITE AUTOMATA AU - Nov/Dec 2013
A NFA has 5 tuples defined as,
M = ( Q, , , q0 , F)
where,
Q a finite set of states
A finite set of inputs
Transition function from Q Q
q0 Start / initial state
F A set of final / accepting states FQ
Subset
1.38 Theory of Computation
0, 1 0, 1 0 q1 0
q0 q3
1 q2 1
Difference between DFA and NFA
S.NO DFA NFA
1. The transition is deterministic The transition is non-deterministic
2. It has more number of states than
NFA It has less number of states. 2
Q
3. It provides only one state transition
for a given input
It may lead to more than one states for
given input
4. It has only one final state It has more than one final states
5. More difficult to design It is easy to design
6. It has a single path for an input
from a state
It may have several paths for a single
input from a state. Backtracking is
required.
PROBLEMS
1) Sketch the NFA state diagram for M = (q0 , q1 , q2 , q3 ,0,1, , q0 ,q3 ) with given as
∑
Q
0 1
q0 (q0 , q1 ) (q0 , q2 )
q1 q3
q2 q3
* q3 q3 q3
Solution:
Automata Fundamentals 1.39
a q0 q1
a, b q2
2) Design a NFA with no more than five states for the set ababn
: n 0aban
: n 0 .
b
3) Determine a NFA that accepts L(aa* (a b)) .
a
4) For NFA shown, check whether „0100‟ is accepted or not.
0
:
∑
Q
0 1
q0 q0 , q1 q2
q1 q1 , q2
* q2 q0 , q2 q1
Solution:
(q0 , 0100)
(q0 , 0) q0 , q1 (q0 , q1, 0) q0 , q1 q0 , q1
(q0 , 01) ((q0 , 0),1)
(q0 , q1,1)
0 1
q0 0
q1
0 1
1 1 q2
a b a q0 q1 q2 q3
a
q4 a
1.40 Theory of Computation
(q0 ,1) (q1 ,1)
q2 q1 , q2
q1 , q2
(q0 , 010) ((q0 , 01), 0)
(q1 , q2 , 0)
(q1 , 0) (q2 , 0)
q0 , q2
q0 , q2
(q0 , 0100) ((q0 , 010), 0)
(q0 , q2 , 0)
(q0 , 0) (q2 , 0) q0 , q1 q0 , q2
q0 , q1 , q2 Since q0 is final state string accepted
5) NFA with states 1, 2,3, 4,5 and a, bhas as,
Calculate 1) (1, abab)
2) (2, baba)
∑
Q
a b
1 1, 2 1
2 3 3
3 4 4
4 5
5 5
Solution:
1)
(1, abab)
(1, a) 1, 2
(1, ab) ((1, a), b)
(1, 2, b)
Automata Fundamentals 1.41
1 2
(1, b) (2, b)
1 3
1, 3
(1, aba) ((1, ab), a)
(1,3, a)
(1, a) (3, a)
1, 2 4
1, 2, 4
(1, abab) ((1, aba), b)
(1, 2, 4, b)
(1, b) (2, b) (4, b)
1 3 1,3
2) (2, baba) :
(2, b) 3
(2, ba) ((2, b), a)
(3, a)
4
(2, bab) ((2, ba), b)
(4, b)
(2, baba) ((2, bab), a)
(, a)
6) Design a NFA to accept strings containing the substring,”0101”.
0, 1
0, 1
q0
0 q
1 q
0 q
1 q
3 4
1.42 Theory of Computation
q0 a
q1 a
q2 b
q3
NFA: M (Q, , , q0 , F) where,
Qq0 , q1 , q2 , q3 , q4
0,1
q0 q0
Fq4
is given as
∑
Q
0 1
q0 q0 , q1 q0
q1 q2
q2 q3
q3 q4
* q4 q4 q4
7) Construct a NFA that accepts Lx a, b/ x ends with 'aab'
a, b
M(Q, , , q0 , F)
Qq0 , q1 , q2 , q3
a, b
q0 q0
Fq3
is given as
∑
Q
a b
q0 q0 , q1 q0
q1 q2
q2 q3
* q3
Automata Fundamentals 1.43
q4 Trap 0
q1
1 q1
0, 1
0, 1
q3 0, 1
q0 0
q 0, 1 0, 1
1 q2 q3
8) Design a NFA to accept strings over alphabet0, 1, such that the third symbol from
right end is 0. 0, 1
Qq0 , q1 , q2 , q3
0,1
q0 q0
Fq3
The transition function, is given as
9) Construct a NFA for
following c}.
L x {a, b, c*
: x contains exactly one b immediately
a, b
q0
c
a, c
q1
a, b, c
q1
1 b Trap
a b
10) Construct a NFA that accept
by 3}.
q4
L x {0, 1*
: x is starting with 1 and | x | is divisible
0, 1
∑
Q 0 1
q0 q0 , q1 q0
q1 q2 q2
q2 q3 q3
* q3
1.44 Theory of Computation
q0 b
q1
a q2
q5
11) Design a NFA for L =
atleast one b}.
L {x {a, b}* / x contains any number of a‟s followed by
a b a, b
Trap
12) Design a NFA for a binary number where the first and the last digits are same. 0, 1
13) Construct a NFA over 0, 1 immediately followed by 1.
0, 1
such that each „0‟ is immediately preceded and
1
1 q0 q1
0 0 1
0, 1
q3 0
q2
Trap
14) Construct NFA for the RE: (0+1)01.
0,1
EQUIVALENCE OF DFA AND NFA
Every language that can be described by some NFA can also be described by some
DFA. It involves subset construction process for conversion.
q0 0
q1 1
q2
q2 0
q4
0
q1
1 q3
1
Automata Fundamentals 1.45
0 1 2
Let N (QN , , N, q0 , FN ) be the given NFA.
We can construct DFA,
D = (QD , , D ,q0 , FD )
where, QD Set of all subsets of QN
FD Set of subsets,S of QN such that
S FN
Procedure
1. If
D For each set, S contained in QN , i.e., S QN and each input a in be
defined as D (s, a) N (p, a); p s
Qq0 , q1 , q2 , then create subsets, q0,q1,q2,q0 , q1, q0 , q2,q1, q2,
q , q , q and . Thus if no of states = n; the no of subsets =2n.
2. Construct the transition table for the subsets over the given language, .
3. Minimise the table by considering only the reachable states from the start state.
4. Draw the DFA from the minimised transition table.
PROBLEMS
1) Construct a DFA from the following NFA that accepts all strings of 0‟s and 1‟s that
end with „01‟. AU - NOV/DEC 2013, NOV/DEC 2003, MAY/JUNE 2006
Solution:
NFA
0,1
Here ,
QN q0 , q1 , q2
0,1
q0 q0
F q2
q0 0
q1 1
q2
∑
Q
0 1
q0 q0 , q1 q0
q1 q2
* q2
1.46 Theory of Computation
0 {q0} {q0 , q1}
1 {q0 , q2}
0 1
Subsets
QD ,q0 ,q1,q2 q0 , q1q0 , q2 q1 , q2 q0 , q1, q2
Transition table: D
∑
Q 0 1
→q0 q0 , q1 q0
q1 q2
*q2
q0 , q1 q0 , q1 q0 , q2
*q0 , q2 q0 , q1 q0
*q1 , q2 q2
*q0 , q1 , q2 q0 , q1 q0 , q2
Minimised transition table
∑
Q 0 1
q0 q0 , q1 q0 q0 Processed
q0 , q1 q0 , q1 q0 , q2 q0 , q1Processed
* q0 , q2 q0 , q1 q0 q0 q0 , q1 both are processed
Minimised DFA
1 0
Automata Fundamentals 1.47
D
2) Convert the given NFA to DFA.
QN p, q, r,s
0,1
q0 p
FN s
NFA for the above specification
0, 1
N :
0, 1
Subsets QD ,p,q,r,s,p, q, p, r,p,sq, r,q,s,r,s,
p, q, r,p, q,s,q, r,s,p, r,s,p, q, r,s
Q 24 16
Transition table
∑
Q 0 1
p p, q p
q r r
r s
*s s s
p, q p, q, r p, r
p, r p, q,s p
*p, s p, q,s p, s
q, r r, s r
*q, s r, s r, s
*r, s s s
p 0
q 0, 1
r 0, 1
s
∑
Q 0 1
→p p, q p
q r r
r s
*s s s
1.48 Theory of Computation
p, q, r p, q, r,s p, r
*p, q,s p, q, r,s p, r,s
*p, r,s p, q,s p, s
*q, r,s r, s r, s
*p, q, r,s p, q, r,s p, r,s
Minimised transition table
∑
Q 0 1
p p, q p
p, q p, q, r p, r
p, q, r p, q, r,s p, r
p, r p, q,s p
p, q, r,s p, q, r,s p, r,s
p, q,s p, q, r,s p, r,s
p, r,s p, q,s p, s
* p, s p, q,s p, s
DFA
1
0
{p} 0
{p, q} 0
{p, q, r}
1 1
0
1
{p, r} {p, q, r, s}
0 0 1
{p, q, s} 1
{p, r, s}
0
0 1
{pp,,ss}
1
Automata Fundamentals 1.49
3) Convert the following NFA to DFA. AU NOV/DEC 2004
∑
Q 0 1
p q, s q
q* r q, r
r s p
* s p
Solution:-
Here,
QN p, q, r,s
0,1
q0 p
FN q,s0
Subsets:
QD ,p,q,r, s,p, q,p, r,p,s ,q, r,q,s,r,s,p, q, r ,
p, q,sp, r,s,q, r,s,p, q, r,s
FD q,s,p,s,q, r,q,s, r,s,p, q, r,p, q,s, p, r,s,q, r,s,p, q, r,s
Transition table:-
∑
Q 0 1
p q, s q
*q r q, r
p 0, 1
r 0, 1
r 0
s
1 1
1
1.50 Theory of Computation
-r s {p}
*s p
p, q q, r,s p, q, r
p, r q, s p, q
*p, s q, s p, q
q, r r, s p, q, r
*q, s r p, q, r
*r, s s p
p, q, r q, r,s p, q, r
*p, q,s q, r,s p, q, r
*p, r,s q, s p, q
*q, r,s r, s p, q, r
*p, q, r,s q, r,s p, q, r
Minimised transition table:-
∑
Q 0 1
p q, s q
q, s r p, q, r
q r q, r
r s p
p, q, r q, r,s p, q, r
q, r r, s p, q, r
s p
q, r,s r, s p, q, r
*r, s s p
Automata Fundamentals 1.51
q0 0
q1 1
q2
{q, s} 1
0 {p, q, r}
0
{p} 1
{r} 0 1 {r, s}
1 0 1 0
1 {q} 1 {q, r, s}
1 {q, r} 0
{s} 0
0
DFA diagram:-
1
4) Construct a DFA for the following NFA. AU MAY/JUN 2009
Solution:-
0 1
QN = {q0, q1, q2}
∑ = {0, 1}
q0 = {q0}
FN = {q2}
Construction of DFA:-
QD = {, {q0}, {q1}, {q2}, {q0, q1}, {q0, q2}, {q1, q2}, {q0, q1, q2}}
FD = {{q2}, {q0, q2}, {q1, q2}, {q0, q1, q2}}
q0 = {q0}
∑
Q 0 1
→{q0} {q0, q1}
{q0} {q2}
*{q2} {q2}
1.52 Theory of Computation
0 {q0} {q0 , q1}
1 {q2}
Transition table:-
∑
Q 0 1
{q0} {q0, q1}
{q1} {q2}
*{q2} {q2}
{q0, q1} {q0, q1} {q2}
*{q0, q2} {q0, q1} {q2}
*{q1, q2} {q2}
{q0, q1, q2} {q0, q1} {q2}
Minimized transition table:-
∑
Q
0
1
→{q0} {q0, q1}
{q0, q1} {q0, q1} {q2}
*{q2} {q2}
Minimized DFA:-
0 1
5) Convert M = ({q1, q2, q3}, {0, 1}, {q1}, {q3}), where is given by,
(q1, 0) = {q2, q3} (q1, 1) = {q1}
(q2, 0) = {q1, q2} (q2, 1) =
(q3, 0) = {q2} (q3, 1) = {q1, q2}
Construct an equivalent DFA
Automata Fundamentals 1.53
1 0
0
q1 0
q2 0,1
q3
0
1
Solution:
Given NFA,
∑
Q
0
1
q1 {q2, q3} {q1}
q2 {q1, q2}
* q3 {q2} {q1, q2}
DFA Construction:-
∑ = {0, 1}
QD = {, {q1}, {q2}, {q3}, {q1, q2}, {q1, q3}, {q2, q3}, {q1, q2, q3}}
q0 = {q1}
FD = {{q3}, {q1, q3}, {q2, q3}, {q1, q2, q3}}
Transition Table:-
∑
Q 0 1
→
{q1} {q2, q3} {q1}
{q2} {q1, q2}
* {q3} {q2} {q1, q2}
{q1, q2} {q1, q2, q3} {q1}
* {q1, q3} {q2, q3} {q1, q2}
* {q2, q3} {q1, q2} {q1, q2}
* {q1, q2, q3} {q1, q2, q3} {q1, q2}
1.54 Theory of Computation
1 0
0 {q1} {q2 , q3}
0,1 {q1, q2}
0 {q1, q2 , q3}
1 1
Minimized DFA table:-
∑
Q 0 1
→ {q1} {q2, q3} {q1}
{q2, q3} {q1, q2} {q1, q2}
{q1, q2} {q1, q2, q3} {q1}
* {q1, q2, q3} {q1, q2, q3} {q1, q2}
Minimized DFA:-
THEOREM AU – MAY 2005, DEC 2006, DEC 2013
A Language, L is accepted by some NFA if and only if it is accepted by some DFA
(or)
For every NFA, there exists an equivalent DFA
Proof:-
This theorem has two parts:-
1) If L is accepted by a DFA (M2), then L is accepted by some NFA (M1)
2) If L is accepted by a NFA (M1), then L is accepted by some DFA (M2)
To prove the theorem, it is enough to prove that
For an arbitrary string w, we prove this statement, we use induction principle on w .
Basis of Induction:-
Let w =0
Then w = ε
By the definition of extended transition function,
D ({q0 },) {q0 }
----- (1)
D ({q0 }, w) N ({q0 }, w)
Automata Fundamentals 1.55
P
U
D ({q0}, x) N ({q0}, x)
N ({q0 },) {q0 } ----- (2)
Inductive Step:-
is true for w = Є Hence proved that L [D] = L [N].
Let us assume that the theorem is true for all the strings, w of length „n‟. (i.e.) w n .
Now let us prove that the same is true for a string of length, n+1
Let w = xa, where
a → last symbol of w.
By assumption, we have,
k {P1, P2 ,.....Pk } subsets i
i1
Now, by the definition of extended transition function of DFA,
D ({q0}, x) D ({q0}, xa )
= D (({q0}, x), a)
= D ({P1, P2 , .... Pk }
K
i1 N (Pi, a) Bysubset construction
=
By induction principle, the theorem is true for all the strings, w of the given
language.
Thus, L [D] = L [N]
NFA WITH Є TRANSITIONS
The Є [Epsilon] used to indicate null string. The string is used for transition from one
state to other without any input. AU DEC 2007
Formal Notation for Є – NFA:
A non deterministic FA, M with Є –transition is given by E = (Q, ∑, δ, q0, F), where
Q →finite set of states
∑→finite set of input alphabets
D ({q0 }, w) N ({q0 }, w)
N (q0 , w) By definition of of NFA
1.56 Theory of Computation
Є Є
2 3 6
Є
1
a Є
5 7
Є 4
q0 → Initial/ start state
F → F Q →set of final/accepting states
δ → Transition/ Mapping function from
Q X {∑ U {Є}} → 2Q
EPSILON CLOSURE AU MAY 2012, DEC 2010, MAY 2013
Epsilon closure of a state, q is defined as the set of states reachable from q on Є
moves. Є - closure of q0 is denoted as ЄCLOSE (q0).
Example: Obtain Є closure of each state in the following Є – NFA with Є – move.
AU DEC 2005
Є CLOSURE {q0} = {q0, q1, q2}
Є CLOSURE {q1} = {q1, q2}
Є CLOSURE {q2} = {q2}
Find Є- CLOSURE of States 1,2 and 4 from the following transition diagram.
AU MAY 2008
Є –Closure {1} = {1,2,4,3,6}
Є –Closure {2} = {2,3,6}
Є –Closure {4} = {4}
Eliminating Epsilon transitions
Given any epsilon NFA with Є moves, we can find a DFA that accept the same
language as E.
q0 q1 q2
0 0 2
Automata Fundamentals 1.57
q0 Є
q1 a
q2
b Є
q3
a,b
Let E = (QE, ∑,δE, qE, FE)
Then the equivalent DFA,
D = (QD, ∑,δD, qD, FD) is defined as follows.
(i) QD is the set of subsets, s of QE. S QE such that s= ECLOSE (S)
(ii) qD = ECLOSE(qE)
(iii) FD = {s / s is in QD and S n FE ≠ }
(iv) δD(s,Q) is computed for all a in ∑ and sets, S in QD by
a) Let S = P1, P2,….,Pk)
b) Uk (P , a) {r , r ,....r }
i1 i 1 2 m
c) (s, a) Um
ECLOSE(r ) D j1 j
PROBLEMS
1) Design a DFA that eliminates Є transition from the following Є- NFA.
∑
QE Є a b c d
q0 q0, q1
q1 q1 q1, q2 q3
q2 q2, q3 q2 q2
*q3 q3 q3 q3
a c,d
Solution :-
Start state of DFA:-
ECLOSE (q0) = {q0, q1} → Start State of DFA
1.58 Theory of Computation
Successors of {q0, q1} for the input symbols, {a,b,c,d}:-
D ({q0 , q1}, a) :
E (q0 , a)
E (q1, a) {q1, q2}
E ({q0 , q1}, a) {q1, q2}
D ({q0 , q1}, a) ECLOSE(q1)UECLOSE(q2 )
{q1}U{q2 , q3}
D ({q0 , q1}, a) {q1, q2 , q3}
D ({q0 , q1}, b) :
E (q0 , b)
E (q1, b) {q3}
E ({q0 , q1}, b) {q3}
D ({q0 , q1}, b) ECLOSE(q3 )
D ({q0 , q1}, b) {q3}
D ({q0 , q1}, c) :
E (q0 , c)
E (q1 , c)
E ({q0 , q1}, c)
D ({q0 , q1}, c) ECLOSE ()
D ({q0 , q1}, c)
D ({q0 , q1}, d) :
E ( q0 , d)
E ( q1 , d)
E ({q0 , q1}, d)
D ({q0 , q1}, d) ECLOSE ()
D ({q0 , q1}, d)
Automata Fundamentals 1.59
Successors of q1 , q2 , q3 for the input symbols {a,b,c,d}:-
D ({q1, q2 , q3}, a) :
E (q1, a) {q1, q2}
E (q2 , a)
E ({q1, q2 , q3}, a) {q1, q2 , q3}
D ({q1, q2 , q3}, a) CLOSE{q1}UECLOSE{q3}
{q1}U{q2 , q3}U{q3}
D ({q1, q2 , q3}, a) {q1, q2 , q3}
D ({q1, q2 , q3}, b) :
E (q1, b) {q3}
E (q2 , b)
E (q3 , b) {q3}
E ({q1, q2 , q3}, b) {q3}
D ({q1, q2 , q3}, b) ECLOSE{q3}
D{q1, q2 , q3}, b) {q3}
D ({q1, q2 , q3}, c) :
E ( q1 , c)
E (q2 , c) {q2}
E ( q3 , c)
E ({q1, q2 , q3}, c) {q2}
D ({q1, q2 , q3}, c) ECLOSE{q2}
D ({q1, q2 , q3}, d) :
E ( q2 , d)
E (q3 , d) {q2}
E ( q3 , d)
1.60 Theory of Computation
E ({q1, q2 , q3}, d) {q2}
D ({q1, q2 , q3}, d) ECLOSE{q2}
D{q1, q2 , q3}, d) {q2 , q3}
Successors of {q3}for the input symbols,{a,b,c,d}:-
D ({q3}, a) :
E{q3}, a) {q3}
D (q3 , a) ECLOSE{q3}
D (q3 , a) {q3}
D ({q3}, b) :
E{q3}, b) {q3}
D (q3 , b) ECLOSE{q3}
D ({q3}, b) {q3}
D ({q3}, c) :
E ({q3}, c)
D ({q3}, c)
D ({q3}, c)
D ({q3}, d) :
E ({q3}, d)
D ({q3}, d)
D ({q3}, d)
Successors of {q2 , q3} over the input symbols {a,b,c,d}:-
D ({q2 , q3}, a) :
E ({q2 }, a)
E ({q3}, a) q3
E ({q2 , q3}, q) {q3}
Automata Fundamentals 1.61
D ({q2 , q3}, a) ECLOSE{q3}
D ({q2 , q3}, a) {q3}
D ({q2 , q3}, b) :
D ({q3}, b)
E ({q3}, b) {q3}
E ({q2 , q3}, b) {q3}
D ({q2 , q3}, b) ECLOSE{q3}
D ({q2 , q3}, b) {q3}
D ({q2 , q3}, c) :
E ({q2}, c) {q2}
D ({q3}, c)
({q2 , q3}, c) {q2}
D ({q2 , q3}, c) ECLOSE{q2}
D ({q2 , q3}, c) {q2 , q3}
D ({q2 , q3}, d) :
E ({q2}, d) {q2}
D ({q3}, d)
({q2 , q3}, d) {q2}
D ({q2 , q3}, d) ECLOSE(q2 )
D ({q2 , q3}, d) {q2 , q3}
Transition Table:-
∑
QD
a
b
c
d
{q0,q1} {q1,q2,q3} {q3}
{q1,q2,q3} {q1,q2,q3} {q3} {q2,q3} {q2,q3}
* {q3} {q3} {q3}
{q2,q3} {q3} {q3} {q2,q3} {q2,q3}
1.62 Theory of Computation
q0 Є
q1 C
q2 e
q3
b C Є
q0 Є
q5
Equivalent DFA:-
c, d
2) Design a DFA that eliminates Є transitions from the following Є-NFA.
a d e
a
Solution:-
Start of DFA:-
ECLOSE(q0) ={q0, q1}
↓
Start State of DFA
Successors of {q0, q1} over ∑={a,b,c,d,e}:-
D ({q0 , q2 ,}, a) :
E ({q0 , q1}, a) E ({q0}, a)UE ({q1}, a)
U{q4} {q1}
a
a
{q0 , q1} {q1, q2 , q3}
b b
c, d
a, b {{{qq3
3}}
a, b {q2 , q3}
Automata Fundamentals 1.63
∑
QD Є a b c d e
q0 q0, q1
q1 q1 q1 q4 q2
q2 q2 q2 q3
q3 q3, q5 q3
q4 q4, q5 q4 q2
*q5 q5
D ({q0 , q1}, a) ECLOSE(q1)
D ({q0 , q1}, a) {q1}
D ({q0 , q2 ,}, b) :
E ({q0 , q1}, b) E ({q0}, b)UE ({q1}, b)
U{q4}
{q4}
D ({q0 , q1}, b) ECLOSE(q4 )
D ({q0 , q1}, b) {q4 , q5}
D ({q0 , q2 ,}, c) :
E ({q0 , q1}, c) E ({q0}, c)UE ({q1}, c)
U{q2}
{q2}
D ({q0 , q1}, c) ECLOSE(q2 )
D ({q0 , q1}, c) {q2}
D ({q0 , q2 ,}, d) :
E ({q0 , q1}, d) E ({q0}, d)UE ({q1}, d)
D ({q0 , q1}, d)
1.64 Theory of Computation
Successors of {q1} over ∑ = {a, b, c, d, e}:-
D ({q1}, a) :
E ({q1}, a) {q1}
D ({q1}, a) ECLOSE(q1 )
D ({q1}, a) {q1}
D ({q1}, b) :
E ({q1}, b) {q4}
D ({q1}, b) ECLOSE(q4 )
D ({q1}, b) {q4,q5}
D ({q1}, c) :
E ({q1}, c) {q2}
D ({q1}, c) ECLOSE(q2 )
D ({q1}, c) {q2}
D ({q1}, d) :
E ({q1}, d)
D ({q1}, d) ECLOSE ()
D ({q1}, e) :
E ({q1}, e)
D ({q1}, e) ECLOSE ()
Successors of {q4,q5} over ∑ = {a, b, c, d, e}:-
D ({q4 , q5}, a) :
E ({q4 , q5}, a) E ({q4}, a)UE ({q5}, a)
{q1}U
{q4}
D ({q4 , q5}, a) {q4 , q5}
Automata Fundamentals 1.65
D ({q4 , q5}, b) :
E ({q4 , q5}, b) E ({q4}, b)UE ({q5}, b)
D ({q4 , q5 }, b) ECLOSE ()
D ({q4 , q5}, c) :
E ({q4 , q5}, c) E ({q4}, c)UE ({q5}, c) {q2 }U
{q2}
D ({q4 , q5}, c) ECLOSE(q2 )
D ({q4 , q5}, c) {q2}
D ({q4 , q5}, d) :
E ({q4 , q5}, d) E ({q4}, d)UE ({q5}, d)
D ({q4 , q5 }, d) ECLOSE ()
D ({q4 , q5}, e) :
E ({q4 , q5}, e) E ({q4}, e)UE ({q5}, e)
D ({q4 , q5 }, d) ECLOSE ()
Successors of {q2} over ∑ = {a, b, c, d, e}:-
D ({q2}, a) :
E ({q2 }, a)
D ({q2 }, a) ECLOSE ()
D ({q2}, b) :
D ({q2 }, b)
D ({q2}, b) ECLOSE ()
1.66 Theory of Computation
D ({q2}, c) :
D ({q2}, c)
D ({q2}, c) ECLOSE ()
D ({q2}, d) :
E ({q2}, d) {q2}
D ({q2}, d) ECLOSE( q2 )
D ({q2}, d) {q2}
D ({q2}, e) :
E ({q2}, e) {q3}
D ({q2}, e) ECLOSE( q3 )
D ({q2}, e) {q3}
Successors of {q3, q5} over ∑ = {a, b, c, d, e}:-
D ({q3,q5}, a) :
E ({q3,q5}, a) E ({q3}, a)UE ({q5}, a)
U
D ({q3 , q5 }, a) ECLOSE ()
D ({q3,q5}, b) :
E ({q3,q5}, b) E ({q3}, b)UE ({q5}, b)
U
D ({q3 , q5 }, b) ECLOSE ()
D ({q3,q5}, c) :
E ({q3,q5}, c) E ({q3}, c)UE ({q5}, c)
U
D ({q3 , q5 }, c) ECLOSE ()
Automata Fundamentals 1.67
{q0 , q1} a
{q1}
c
b b c e
c {q4, q5} {q2 , q3} {q3, q5}
a d
D ({q3,q5}, d) : V
E ({q3,q5}, d) E ({q3}, d)UE ({q5}, d)
U
D ({q3 , q5 }, d) ECLOSE ()
D ({q3,q5}, e) :
E ({q3,q5}, e) E ({q3}, e)UE ({q5}, e)
{q3} U
{q3}
D ({q3 , q5}, e) ECLOSE(q3 )
D ({q3 , q5}, e) {q3,q5}
Transition Table:-
∑
QD a b c d e
{q0,q1} {q1} {q4,q5} {q2}
{q1} {q1} {q4,q5} {q2}
{q4,q5} {q4,q5} {q2}
{q2} {q2} {q3,q5}
*{q3,q5} {q3,q5}
DFA – Transition Diagram :-
1.68 Theory of Computation
3) Consider the following Є – NFA
∑
QE Є a b c
p {q,r} {q} {r}
q {p} {r} {p,q}
*r
a) Complete the Є – closures of each state
b) Convert the automation to DFA
Solution:-
a) – closure of states:-
State - Closure
p {p,q,r}
q {q}
r {r}
b) Є NFA → DFA:-
Start state of DFA, qD
ECLOSE (P) = {p, q, r}
Successors of {p, q, r} over ∑ = {a, b, c}:-
D ({p, q, r}, a) :
E ({p, q, r}, a) E ({p}, a)UE ({q}, a)UE ({r}, a)
= U{p}U
{p}
D ({p, q, r}, a) ECLOSE(p)
D ({p, q, r}, a) {p, q, r}
D ({p, q, r}, b) :
E ({p, q, r}, b) E ({p}, b)UE ({q}, b)UE ({r}, b)
{q} U {r} U
{q, r}
Automata Fundamentals 1.69
D ({p, q, r}, b) ECLOSE(q) UECLOSE(r)
D ({p, q, r}, b) {q, r}
D ({p, q, r}, c) :
E ({p, q, r}, c) E ({p}, c)UE ({q}, c)UE ({r}, c)
{r} U {p, q} U
{p, q, r}
D ({p, q, r}, c) ECLOSE(p) U ECLOSE(q)U ECLOSE(r)
{p, q, r}U{q}U{r}
D ({p, q, r}, c) {p, q, r}
Successors of {q, r} over ∑ = {a, b, c}:-
D ({q, r}, a) :
E ({q, r}, a) E ({q}, a)UE ({r}, a)
{p} U
{p,}
D ({q, r}, a) ECLOSE(p)
D ({q, r}, c) {p, q, r}
D ({q, r}, b) :
E ({q, r}, b) E ({q}, b)UE ({r}, b)
{r} U
{r}
D ({q, r}, b) ECLOSE(r)
D ({q, r}, b) {r}
D ({q, r}, c) :
E ({q, r}, c) E ({q}, c)UE ({r}, c)
{p, q} U
{p, q}
1.70 Theory of Computation
D ({q, r}, c) ECLOSE(p) U ECLOSE(q)
D ({q, r}, c) {p, q, r} {p, q, r}U{q}{p, q, r}
Successsors of {r} over ∑ = (a,b,c):-
D ({r}, a) :
E ({r}, a)
D ({r}, a) ECLOSE ()
D ({r}, b) :
E ({r}, b)
D ({r}, b) ECLOSE ()
D ({r}, c) :
E ({r}, c)
D ({r}, c) ECLOSE ()
Transition Table:-
∑
QD a b c
{p,q,r} {p,q,r} {q,r} {p,q,r}
{q,r} {p,q,r} {r} {p,q,r}
* {r}
Transition diagram:-
b
{p,q,r} {q,r}
a,c
b
{r}
a
UNIT – 2
REGULAR EXPRESSIONS AND LANGUAGES
REGULAR LANGUAGES AND REGULAR EXPRESSIONS
The languages accepted by a finite automation can easily be described by a simple
expression called Regular Expressions.
Regular Expressions serve s the input language for many systems that process strings
Regular expression uses a set of operators (like +,., *,….) to represent the language.
Search systems convert RE into DFA/NFA that stimulates the automation on the file
being searched.
Example: Lexical Analyzer accepts tokens as inputs and produces a DFA that
recognizes which token appears next on the input. The set of Stings accepted by finite
automata is known as regular language.
Representation
DFA Language Regular Expression
{Є} R.E = ε
{a} R.E = a
b {a, b} R. E = a + b
{ab} R.E = a.b = ab
a
a b
2.2 Theory of Computation
Formal Definition of a RE
Let ∑ be a given alphabet. Then,
(i) , , and a belongs to ∑, are all regular expressions. [primitive Regular
Expressions]
(ii) If r1 and r2 are regular expressions, then r1+r2, r1r2, r1*, (r1) are also regular
expression if and only if it can be derived from the primitive RE by the finite
number of applications of the rules in (ii).
Operators of Regular Expressions
The union of t wo languages, L and M is denoted by LUM which are a set of strings
that are either in L or M or both.
Example:
L = {001, 10, 111}
M = {, 001}
LUM = {, 00, 10,111}
The concatenation of languages L and M, denoted L.M which are a set of strings that
can be formed by taking any string in L and concatenating with any string in M.
Example:
L = {001, 10, 111}
M = {, 001}
LM = {001, 001001, 10, 1000, 111, 111001}
The closure of a language, L is denoted as L* represent the set of those strings that can
be formed by taking any number of strings from L, possibly with repetitions and
concatenating all of them.
Example:
L = {0, 1}
L0 = {}
L1 = {0, 1}
L2 = {01, 00, 11, 10}
PROBLEMS
1) What is {10, 11}*? Write atleast first seven terms. AU DEC 2009
(10, 11)* = (10, 11)) U (10, 11)‟U (10, 11) 2 U (10, 11) 3 U…..
= {} U {10, 11} U {1011, 1110} U {101110, 101011,…}
= { , 10, 11, 1011, 1110, 101110, 101011,….}
Regular Expressions and Languages 2.3
2) Write a RE for the language accepting all combinations of a‟s over the set ∑ = {a}
L = {all combination of a‟s over {a}}
= {Є, a, aa, aaa,…….}
R = a*
3) Design the RE for the language accepting all combinations of a‟s except the null string
over
{}
L = {all combination of a over {a }except }
= {a, aa, aaa,aaaa…….}
R = a+
1) Design a RE for the languages containing all thes strings containing any number of
a‟s and b‟s .
L = { a,b, aa, bb, ab,aab,….}
R = (a+b)*
2) Construct the RE for the language contai9ning all strings having any number of a‟s
and b‟s except the null string.
L = {a, ab, b, aa, abb, aabb ,..}
R = (a+b)+
3) Construct a RE for the language accepting all strings which are ending with 00 over
the set, ∑ = {0,1}
L = {00, 000, 100, 0100, 1000, 01000, 01000, 11100,..}
R = (0+1)* 00.
4) Write a RE for the language accepting the strings which are starting with 1 and
ending with 0, over the set ∑ = {0,1}
L = {10, 100, 110, 1000, 10101, 111000 ….s,}
R = 1(0+1)*0
5) Write a RE to denote a language, L over ∑* where ∑ = {a, b} that the third character
from right end of the string is always a.
L = aab, aba, aaa, abb, babb, aaaa,…
R = (a+b)* a(a+b)(a+b)
4.2 Theory of Computation
6) Construct a RE for the language, L which accepts all the strings with at least 2 b‟s
over the alphabets ∑ = {a,b}
L = {bb, abb, bba, bab,bbb,bbaa, ababa,…}
R =(a+b)*b(a+b)*b(a+b)*
7) Construct a RE for the language which consist of exactly 2 b‟s over the set, ∑ = {a, b}
L = {bb, abb, baba, bba, aaaabab,….}
R = a*ba*ba*
8) Construct a RE which denotes a language, L over the set , ∑ = {0}, having even length
of string.
L = {00,, 0000, 000000,…}
R = (00)*
9) Write a RE which denotes a language, L overt the set, ∑ = {1}, having odd length of
strings
L = {1, 111, 11111, 1111111,…}
R = (11)*1
10) Write the RE to denote the language, L over ∑ = {a, b}, such that all the strings do
not contain the substring ab.
L = {bbaa, ba, bbbaaa, bb, aa,….}
R = b* a*
11) Write a RE which contains L having the strings which have at least one 0 and 1.
L = {01,001,101,010,011, 10101,10,..}
→ R = (0+1)* 0(0+1)* 1(0+1)* + (0+!)* 1(0+1)*0(0+1)*
12) Describe the following by RE
a) L1 = the set of all strings of 0‟s and 1‟s ending in 00.
b) L2 = the set of all strings of 0‟s and 1‟s beginning with 0 and ending with 1
AU MAY 2004
a) R1 = (0+1)* 00
b) R2 = 0(0+1)* 1
Regular Expressions and Languages 2.5
ij
ij
ij
13) Find the RE for
a) The language of all strings containing exactly two 0‟s
` b) The language of all strings containing at least two 0‟s
c) The language of all string that do not end with 01.
Solution
a) R1 = 1* 0 1* 0 1*
b) R2 = 1* 0 1* 0 (1+0)*
c) R3 = (1+0)* (00 + 11 + 10)
CONVERTING DFA TO RE
THEOREM
If L is a language defined by some DFA, then there is a regular expression defining L.
(or)
If L = L(A) for some DFA, then there is a RE, R such that L = L [R]
PROOF
Let a be a DFA which defines a language L Assume that A has „n‟ states.
Let us construct the RE of the form, R(k)
, whose language is the set of strings, w that
takes the DFA from state I to j without going to any state, numbered greater than k.
We construct R(k)
by induction, k as follows
Basis
Let k = 0 [since all the states are numbered 1]. The Restriction on paths is that the path
must have no intermediate states at all.
There are only two kinds of paths that need such a condition,
An arc from state I to state j.
A path of length zero, that consists of only some state i.
Case 1:- if i≠j
Only (i) is possible. We must examine the DFA, A and find those input symbol, a
such that there is a transition from state, I to j on symbol, a
a) If there is no such symbol, a then R(0)
=
6.2 Theory of Computation
ij
ij
ij
kj ik kk ik ij Rk Rk1 Rk1 (Rk1)* Rki
b) If there is exactly one such symbol, a then R(0)
= a
c) If there are symbols a1, a2,….ak that label arcs from state i to j then R(0)
= a1 + a2
+ a3 + …. + ak
Case 2:- if i = j
The legal paths are the path of length zero and all loops from I to itself.
The path of length zero is represented by the regular expression, since the path has
no symbols along it. Thus we add, Є to the various expressions derived in (a) through (c)
above.
i.e., Case (a) → No symbol „a‟ → The expression becomes .
Case (b) → One symbol „a‟→ The expression becomes +a
Case (c) → Multiple symbols „a‟→ The expression becomes + a1 + a2 +a3 +….+ ak
Induction
Suppose there is a path from state i to state j that goes through no state higher than K.
There are two possible cases to consider.
Case 1
The path does not go through state k at all. In this case, the label of the path is in the
language of Rk1
Case 2
The path goes through state k at least once. Then we can break the path into several
pieces. The first goes from state I to state k without passing through k.
The Last piece goes from k to j without passing through k and all the pieces in the
middle go from k to itself without passing through k. If a path goes through state k only once,
then there are not middle pieces just a path from I to k and a path from k to I.
The set of labels for all paths of this type is represented by the regular expression
Rk1 (Rk1 )* Rk1
ik kk kj , i.e., the first expression represents the part of the path that gets to state k, the first time, the second represents the quotient that goes from k to itself „0„ times, once or
more than once. And the third expression represents the part of the path that leaves k for the
last time and goes to state, j.
When we combine the expressions for the paths of the two types, we have the
expression
Regular Expressions and Languages 2.7
12 R(2)
R
1j
ij
ij
1 0,1
0
1 2
Therefore, by induction hypothesis, we can conclude that the RE n
can be
constructed for all values of I,j and n. The RE for the language defined by the DFA is the sum
of all expressions of all expressions Rn such that state j is an accepting state.
Some Simple Results of RE
1. 1* 1* 9. .0 * 17. 11
2. 1*1* 1* 10. .1* 18.
3. 1* 1* 11. 19..* Nothing
4. 1 1* 1* 12. 0 0 20.
5. 0 0*1 1*0 13. 1 1 21. 0 0 0
6. 0 01*01* 14. 0* 0 * 22. 111
7. .0 15. 1* 1* 23. 0.0 00
8. .1 16. 0 0 24. 1.111
25. 0.* 0 26. 1.*1 27. * None
PROBLEMS
1) Obtain the RE that denotes the language as accepted by the following DFA.
AU DEC 2004
Solution: To find Rn
i → start state = 1
j → Final state = 2
k → 0 to n [n→no of states] = 0,1,2
So, we have to find
8.2 Theory of Computation
R
R R R (R )*R
R R R (R )*R
11 11 11 11 11
Basis Step
Step 1:
R(0) a a a ..... a ;i j
ij 1 2 3 k
a1 a2 ak ; i j
i takes the values 1,2
j takes the values 1,2
R(0) 1 [i j] 11
R(0) 0 [i j] 12
(0)
12 [i j]
R22 0 1 [i j]
Inductive Step
(k) (k1) k1 k1 k1
ij ij ik kk kj
k takes the values 1 and 2. For k =0, basis step has already been calculated.
Step 2:
k = 1
(1) (0) (0) (0) (0)
ij ij i1 11 1j
i=1; j=1 R(1) R
(0) R
(0) (R
(0) )*R
(0)
(1) [(1)(1)*(1)]
(1) [(1)*(1)] (1)*1*
(1) 1*(1) (1)*1*
(1) 1* 1*(1)*1*
1* (1)*1* 1*
Regular Expressions and Languages 2.9
21 12 11 11 12
21 21 21 11 11
22 22 21 11 12
R R R (R )*R
i2 12 12 22 22
22 R(2)
i=1; j=2 R(1) R
(0) R
(0) (R
(0) )*R
(0)
0 [(1)(1)*(0)]
0 [((1)1*(0)]
0 [1*0]
=
i=2; j=1 R()
R(0) R
(0) (R
(0) )*R
(0)
[ (1) *(1)]
[.1* (1) ]
[.1*]
(1)*1*
(1)*1*
0 1*) 1*0
( 1) * 1*
1* (1) *1*
.1*
=
i=2; j=2:- R(1) R
(0) [R
(0) (R
(0) )*R
(0) ]
( 0 1) [ .(1) *.0]
( 0 1) . anything
Step 3:
k = 2
To find
01
(2) (1) (1) (1) (1)
ij ij i2 22 2 j
Somethingsomething
i=1; j=2 R(2) R
(1) R
(1) (R
(1) )*R
(1)
1*0 [(1*0)( 0 1)*( 0 1)]
1*0 [(1*0)(0 1)*( 0 1)]
1*0 [(1*0)(0 1)*]
(1)*1*
1*(1) 1*
0 01* 01* (1*0)(0 1)*
1*0
10.
2
Theory of Computation
ij 11
12 22 32
23 33
R R R (R )*R
The Required regular expression is
2) Construct a RE corresponding to the state diagram given in the following figure.
AU MAY 2005
0
Solution:
Here;
0 1
i = 1
j = 1
k = 3
To find: R(k) R
(3)
Basis
Step 1:
R(0) a
a a
..... a ;i j
ij 1 2 3 k
a1 a2 ..... ak ; i j
(0)
11 0
(0)
22
(0)
31
R(0) 1 R(0) 1 R(0) 1
(0)
13 R(0) 0 R(0)
Inductive step
(k) (k1) (k1) (k1) (k1)
ij ij ik kk kj
k = 0 computed in step 1
k = 1, 2, 3 computed in step 2, 3 and 4
i takes the values 1, 2, 3
j takes the values 1, 2, 3.
1
q1 q2
0 q3
1
12 R
(2) 1*0 (0 1) *
R R R
R
0
Regular Expressions and Languages 2.11
R R R (R )*R
R R R (R ) * R
R R R (R )*R
R R R (R )*R
R R R (R )*R
R R R (R )*R
Step 2:
k = 1
(1) (0) (0) (0) (0)
ij ij i1 11 1j
(1) (0) (0) (0) (0)
11 11 11 11 11
0[( 0) ( 0)*( 0)]
0[ 00* 0]
0[ 00*]
0[ 0*]
0*
(1) (0) (0) (0) (0)
12 12 11 11 12
1 [ 0 ( 0)*(1)]
1 [ 0 (0)* 1
1[0*1]
0*1
(1) (0) (0) (0) (0)
13 13 11 11 13
[ ( 0) ( 0) *.]
0*
0*( 0) 0*
( 0)0* 0*
( 0) 0*0*
( 0)* 0
( 0)0* 0*
1 0*10*1
. a
(1) (0) (0) (0) (0)
22 22 21 11 12
[.( 0) *.( 0) . a
(1) (0) (0) (0) (0)
22 22 21 11 12
(1) [.( 0)*.1]
( 0) . a
(1) a a
12.
2
Theory of Computation
R R R (R )*R
R R R (R )*R
R R R (R )*R
R R R (R )*R
11 11 13 33 31
11 13 33 31
(1) (0) (0) (0) (0)
23 23 21 11 13
0 [.( 0)*.]
0 . a
0 a a
(1) (0) (0) (0) (0)
31 31 31 11 11
0 [ 0.0 * ( 0)*( 0)]
0 [ 0.0*( 0)]
0[0.0*.]
(1) (0) (0) (0) (0)
32 32 31 11 12
1[( 0)*1]
( 0)*0*
0 00*00*
1[ 0.0*1]
1 00*1
( 0)*0*
Step 3:
k =2
(1) (0) (0) (0) (0)
33 33 31 11 13
[0.( 0)*
To find: R(3) R
(2) R
(2) (R
(2) )*R
(2)
So at k = 2; it is enough to find R(2)
, R(2)
, R(2)
, R(2)
R(2) R(1) R(1) (R(21) )*R(2) R(2) R(1) R(1) (R(1) )*R(1)
ij ij i2 22 2 j 11 11 12 22 21
0 *[(0 *1)( 0)*()]
Regular Expressions and Languages 2.13
11
R R R (R )*R
R R R (R )*R
R R R (R )*R
11
11 11 13 33 31
q1 0 q1 1 q3
0
R(2) 0 * . a
0 * a a
(2) (1) (1) (1) (1)
13 13 12 22 23
[ 0 *1)(1) *.0]
[(0*1)(1*)] ( 1) * 1*
0 *11* 0
(2) (1) (1) (1) (1)
33 33 32 22 23
a a
[(1 00*1)(1)*(0)]
[(1 00*1)1*0]
[(1 00*1)1*0
(2) (1) (1) (1) (1)
31 31 32 22 21
1*1*
a a
(00*) [(100 *1)(1) *()]
00 * .a
00* a
Step 4:
k = 3
To find: R(3) R
(2) R
(2) (R
(2) )*R
(2)
0*[(0*11*0)( (1 00*1)1*0)*(00*)]
The Required Regular Expression is given as,
R(3) 0*(0*11*0)( (1 00*1)1*0)*00*
3) Obtain a Regular Expression that denotes the language accepted by
AU MAY 2005, DEC 2006, DEC 2009
1
0,1
14.
2
Theory of Computation
ij
1z 12 13 33 32
11
12
13
21
22
23
31
32
33
R R R (R )*R
R R R (R )*R
Solution:
To find: Rk
i = 1 ←Initial State.
j = 2 ← Final state
k = 3 ← No of states.
R(3) R(2) R(2) (R(2) )*R(2)
Basis
Step 1:
(0)
Rij a1 a2 ... ak ;i j
a1 a2 ak ;i j
R(0)
R(0) 0
R(0) 1
R(0) 0
R(0)
R(0) 1
R(0)
R(0) 0 1
R(0)
Inductive step
i takes the values 1,2,3
j takes the values 1,2,3
k takes the values 0,1,2,3
Step 2:
k = 1
(1) (0) (0) (0) (0)
ij ij i1 11 1j
(1) (0) (0) (0) (0)
11 11 11 11 11
[ ()*]
. None
Regular Expressions and Languages 2.15
R R R (R )*R
R R R (R )*R
R R R (R )*R
R R R (R )*R
R R R (R )*R
R R R (R )*R
(1) (0) (0) (0) (0)
12 12 11 11 12
0 [ ()*0]
.* None
0 0 0 0 0
0
(1) (0) (0) (0) (0)
13 13 11 11 13
1[ ()*1] .* None
11 111
1
(1) (0) (0) (0) (0)
21 21 21 22 11
0 [0()*]
0 0
0
.* None
0 0 0
(1) (0) (0) (0) (0)
22 22 21 11 12
[0()*.0]
00
(1) (0) (0) (0) (0)
23 23 21 11 13
1[0()*1]
*.0 0
1 01 *.1 1
(1) (0) (0) (0) (0)
31 31 31 11 11
[ () *.]
.a
16.
2
Theory of Computation
R R R (R )*R
R R R (R )*R
12 12 13 33 32
12 13 33 32
R R R ( R )*R
R R R ( R )*R
R R R ( R )* R
(1) (0) (0) (0) (0)
32 32 31 11 12
(0 1) [() *.0]
(0 1) . a
(0 1) . a
(1) (0) (0) (0) (0)
33 33 31 11 13
[ ()*1]
0 . a
Step 3:
k = 2
To find : R(3) R
(2) R
(2) (R
(2) )*R
(2)
At k = 2; It is enough to find R(2)
, R(2)
, R(2)
and R(2)
(2) (1) (1) (1) (1)
ij ij i2 22 2 j
(2) (1) (1) (1) (1)
12 12 12 22 22
0 [0( 00)*( 00)]
0 [ 0(00)*( 00)
0 [0(00)*]
0(00)*
0(00)*
(1)* 1*
1*(1) 1*
0 01* 01*
(2) (1) (1) (1) (1)
13 13 12 22 23
1[0( 00)*(1 01)]
1[ 0(00)*(1 01)]
( 00)* (00)*
Regular Expressions and Languages 2.17
R R R ( R )* R
R R R (R )* R
R R R (R )* R
23
23 23 22 22 23
1
1 1 2 1 3
0
1 0 (00)*(1 01)
(2) (1) (1) (1) (1)
33 33 32 22 23
[(0 1)( 00)* (1 01)]
1[ 0(00)*(1 01)]
1 0(00)*(1 01)
( 00)*(00)*
(2) (1) (1) (1) (1)
32 32 32 22 22
(0 1) [(0 1) ( 00)*( 00)
(0 1) [(0 1) (00)*( 00)]
(0 1) [(0 1)(00)*]
(0 1)(00)*
( 00)*(00)*
(00)*( 00) (00)*
0 01*01*
Step 4:
k = 3
(3) (2) (1) (2) (2)
12 12 13 33 32
0(00)*[(1 0(00)*(1 0) ( (0 1) (00)*(101) *(0 1) (00)*]
4. Find the RE, R(2)
from the following DFA. AU DEC 2007
Solution:
To find: R(2) R
(1) R
(1) (R
(1) )* R
(1)
Basis
Step 1:
R(0)
a a
...a ; i j
ij 1 2 k
a1 a2 ...ak ; i j
18.
2
Theory of Computation
q1 1 q2
0 q3
11
j2
13
21
22
23
31
32
33
23 22
R R R (R )* R
R R R (R )* R
R R R (R )* R
R R R (R )* R
R(0)
R(0) 1
R(0) 1
R(0) 0
R(0)
R(0) 1
R(0)
R(0)
R(0)
Inductive Step
We Require R(1)
, R(1)
Step 2:
k = 1
Step 3:
k = 2
(1) (0) (0) (0) (0)
ij ij i1 11 1j
(1) (0) (0) (0) (0)
23 23 21 11 13
1[ 0 ()*1]
1 01
(1) (0) (0) (0) (0)
22 22 21 11 12
[ 0() *1]
01
(2) (1) (1) (1) (1)
23 23 22 22 23
* None
* None
(1 01) [ ( 01) ( 01) *(1 01)]
(1 01) [ ( 01) (01) * (1 01)]
R.E. (1 01)[ ( 01)( 01)*]
5. Find the language accepted by the following automaton
0 1 0,1
Regular Expressions and Languages 2.19
11 12
21
R R R (R )* R
11 12 21 22
R R R (R )*R
R R R (R )*R
Solution:
The language accepted by the above DFA is R(2) R
(2) [since there are two final states]
The state q3 is eliminated since it is a trap/dead state.
Basis
Step 1:
R
(0) a a a ...... a ; i j
ij 1 2 3 k
a1 a2 a3 ak ; i j
(0)
11 0 R(0)
Step 2:
k = 1
(0)
12
(0)
22 1
(1) (1) (1) (1) (1)
ij ij 12 22 21
R(2) R(1) R(1) (R(1) )*R(1)
12 12 12 22 22
It is enough to find R(0)
, R(1)
, R(1)
, R(1)
(1) (0) (0) (0) (0)
11 11 11 11 11
( 0) [ ( 0) ( 0)* ( 0)]
( 0) [( 0)(0)* ( 0)]
( 0) [ ( 0)(0)*]
(1) (0)*
0*
(1) * 1*
1* (1) 1*
1* (1) 1*
(1) 1* 1*
(1) (0) (0) (0) (0)
12 12 11 11 12
1 [ ( 0) ( 0)*1]
0 1
q1 1
q2
R
R R 1
20.
2
Theory of Computation
R R R (R )*R
R R R (R )*R
R R R (R )*R
1[( 0)(0)* 1 ]
1[(0)*1]
( 0)* 0 *
( 0) 0 *0 *
0 *1
(1) (0) (0) (0) (0)
21 21 21 11 11
.a
(1) (0) (0) (0) (0)
22 22 21 11 12
(1) [( 0)*1]
(1) .a
1 0 * 1 0 *1
Step 3:
k =2
(1) a a
(2) (1) (1) (1) (1)
11 11 12 22 21
0 *[0 *1(1) *]
0* .a
0 * a a
R(2) R(1) R(1) (R(1) )*R(1)
12 12 12 22 22
0 *1 [(0 *1) (1) *(1)]
0 *1 [(0 *1) (1) *(1)]
0 *1 [(0 *1) (1*)]
0 *11*
( 1) * 1*
1*( 1) 1*
0 1*01*
The R.E R(2)
R(2) 0 * 0 *11* 11 12
CONSTRUCTING DFA BY ELIMINATING STATES
Consider the following setup in which the state „S‟ is to be eliminated.
Regular Expressions and Languages 2.21
Rij Qi S*Pj
( R SU*T)* SU*
S
Assume that the state, qi is predecessors of the state S. Pj is the successor of s. When we
eliminate the state s, the regular expression, is labeled to R from Qi to Pj. Note that Qi and Pj
may also be same.
The construction of a R. E. from F.A. is as follows:
(i) For each accepting state Q, eliminate all states except Q, on the start state q0. By
applying the above reduction process to produce an equivalent automataton with
regular expressions labels on the arcs.
(ii) Suppose, if Qq0 i.e., accepting state and start state are distinct, then finally we
have two state automaton.
R U
We can represent the accepting string of this two-state automaton in many ways.
In general, we take the string accepted by this automaton, of the form;
(iii) If the start state and the accepting state are the same, then we are left with one –
state automaton.
The regular expression denoting the language that it accepts is,
R
q0
q0 S
q
T
qi Rij
pj
Qj Pj
s
R*
22.
2
Theory of Computation
(iv) The derived regular expression is the sum of all expressions from the reduced
automaton for each accepting states by rules (ii) and (iii)
PROBLEMS
1) Construct the transition diagram for the given DFA and give a R.E by using state
elimination technique.
∑
Q 0 1
q1 {q2} {q1}
q2 {q3} {q1}
* q3 {q3} {q2}
1 0 0
q1 1
q2 0
3
1
Solution:-
Here, q1 → Start State
q3 → Final State
The state to be eliminated is q2.
We use the Regular Expression: Rij
To be found
q1-q2-q3
q3-q2-q1
q1-q2-q1
q3-q2-q1
Step -1:- Eliminating q2 from q1-q2-q3
Rij
q1 q2 q3
Qi S*Pj is labeled for the arcs from q1 to q3.
S
Qi Pj
q
Regular Expressions and Languages 2.23
Here, Rij =
Qi = 0
S =
Pj = 0
r1 0. * 0
00
*
None
r1 00
Step 2: Eliminating q2 from q3 – q2 – q1
Rij
q3 q2 q1
Rij =
Qi = 1
S =
Pj = 1
a a
S
r2 = + 1 * 1
= + 11 ← * = None
r2 = 11 ← + a = a
Step 3: Eliminating q2 from q1 - q2 – q1
Rij
q1 q2 q1
S
Step 4: Eliminating q2 from q3 - q2 - q3
Rij =
Qi = 1
S =
Pj = 1
Rij
q3 - q2 - q3
Qi Pj
S
Rij = 0
Qi = 1
S = Ǿ
Pj = 0
Qi Pj
Qi Pj
24.
2
Theory of Computation
r4 = 0 + 1. * . 0
r4 = 0 + 10 ← * = None
The $ Two – State Automaton:-
1+01
00
q1 q3
11
0 + 10
The R.E = ( R + SU* T)* SU*
R.E = ((1 + 01) + (00) (0 + 10)* (11))* 00 (0 + 10
2) Convert the following DFA to RE using state elimination technique.
∑
Q 0 1
* p s p
q p s
r r q
s q r
1 0
Solution:-
[First eliminate the state that is far away from start state].
States to be eliminated: s, r, q
P 0 q 1 r 1 s
1
0
R S U
q1 q3
T
Regular Expressions and Languages 2.25
Initial / start state = Final start = P cannot be eliminated
R.E = Rij + QiS* Pj
To find:
r1 p s q
Step 1:
r2 q s p
r3 p s r
r4 r s p
r5 q s r
r6 r s q
r7 p s p
r8 q s r
r9 r s r
Elimination of s from p - s - q (r1)
Rij
p - s - q
Rij =
Q1 = 0
S =
Pj = 0 Qi Pj
S
r1 0*0
s 00
* None
r1 00 a a
Elimination of s from q - s - p (r2)
Rij
Rij = 0
Qi = 1
q - s - p S =
Pj =
S
Qi Pj
26.
2
Theory of Computation
Rij
Pj
Rij
Qi Pj
r2 0 1*
0 x a
0 a a
Eliminating s from p - s - r (r3)
Rij
p - s - r
Rij
Qi 0
r3 Rij QiS* Pj
0*1
Qi S Pj S 01 * None
Pj 1 r3 01 a a
Elimination of s from r- s- p (r4)
Rij r3 Rij QiS* Pj
r - s - p Qi *
S . a
S Pj r3
Elimination of s from q – s – r (r5)
Rij R r R Q S* P ij 5 ij i j
q - s - r Qi 1 1*1
S 11 * None
S P 1 r 11 a a j 5
Elimination of s from r – s- q (r6)
R 1 r R Q S* P ij 6 ij i j
r - s - q Qi 1 .*0
S 1 1 .a
S P 0 r 1 a a
j 6
Qi Pj
Qi Pj
Regular Expressions and Languages 2.27
Rij
Rij
Elimination of s from p – s – p (r7)
R 1 r R Q S* P ij 7 ij i j
p - s - p Qi 0 1 .*
S 1 .a
Pj r7 1 a a
Elimination of s from q – s – q (r8)
q - s - q
Rij
Qi
0 r8 Rij QiS* Pj
1 1*0
Qi S
Pj S 1 0 None
Elimination of s from r – s- r (r9)
R 0 r R Q S* P ij 9 ij i j
r - s - r
S
Qi 0 *1
S 0 .a
Pj 1 r9 0 a a
Reduced Automaton
0
Step 2: Elimination of „r‟.
r1 : p – r – q
r2 : q - r - p
r3 : p – r – p
r4 : q – r – q
Qi Pj
1 10
00 11 P q r
0 1
01 Pj
Rij
Qi Pj S
28.
2
Theory of Computation
j
Elimination of r from p – r- q ( r1)
R
ij
00 r1 00 01(0) *1
p - r - q
S
Qi 01
S 0
Pj 1
Elimination of r from q – r – p (r2) Rij
q - r - p
Rij 0 r2
Qi 11
0 11(0) *
0 . a
Qi Pj s 0 r 2 0 . a a
S Pj
Elimination of r from p – r –p (r3)
Rij R 1 r 1 01(0) *ij 3
p - r - p Qi 11 0 . a
Qi P S 0 r3 1 a a
S Pj
Elimination of r from q – r- q (r4)
Rij
q - r - q
S
Rij
Qi
S
Pj
10 r4 10 11 (0)*1
11
0
1
The Reduced Automaton is given as
00 + 01(0)*1 1
10 + 11(0)*1
p q
0
Qi Pj
Qi Pj
Rij
Regular Expressions and Languages 2.29
A 1 B 0, 1 C
0, 1 D
Rij
Step 3: Elimination of „q‟
Elimination of q from p – q – p (r1)
Rij 1 r1 Rij Q1 S*Pj
p - q - p
S
Qi 00 01(0)*1
S 10 11(0) *1
Pj 0
1 [ 00 01(0) *1 (10 11(0) *1) * 0]
The Reduced Automaton is given as
1 + (00 + 01(0)* (10 + 11(0)*)*0
The RE = r*
R = [ 1 + (00 + 010*1) ( 10 + 110*1) * 0]*
3) Convert the following NFA that accepts all strings of 0‟s and 1‟s such that either the
second or third position from right end has 1 to a R.E using state elimination
technique.
AU NOV/DEC 2013
0, 1
Solution:-
Eliminate B. A → Start state cannot be eliminated
C, D → Final states cannot be eliminated first
To find:
Elimination of state B
r1 : A – B - C
r2 : C – B - A
r3 : A – B - D
r4 : D – B - A
r5 : A – B - A
r6 : C – B - C
p
Qi Pj
30.
2
Theory of Computation
Qi Pj
P
Step 1:
r7 : D – B - D
r8 : C – B - D
r9 : D – B – C
Elimination of state – B can be done using Rij
Q1 S*Pj
Eliminating B from A – B – C (r1)
Rij Rij r2 *
A - B - C Qi 1
S r2
S Pj 0 1
Eliminating B from C – B – A (r2)
Rij Rij r2 *
C - B - A Qi
S
S Pj
r2
Eliminating of B from A – B – D (r3)
Rij
A - B - D
Rij
Qi 1
r3 1*
S
S Pj
r3
Eliminating B from D – B – A (r4)
Rij Rij r4 *
D - B - A Qi
Qi S
j
S Pj
r4
Qi Pj
Qi Pj
Regular Expressions and Languages 2.31
- B -
Eliminating B from A – B – A (r5)
Rij 0 1 r5 (0 1) 1*
Qi 1
S
( 0 1)
r5 0 1
Pj S
Eliminating B from C – B – C- (r6)
Rij Rij r6 *(0 1)
C - B - C Qi
S r6
S Pj 0 1
Eliminating B from D – B – D (r7)
Rij R r * ij 7
D - B - D Qi
Qi Pj
S
S
Pj
r7
Eliminating B from C – B – D (r8)
Rij Rij 0 1 r8 (0 1) *
C - B - D Qi r8 (0 1)
S S
Pj
r8 0 1
Eliminating B from D – B – C (r4)
Rij R r * ( 0 1) ij 9
D - B - C Qi
S r9
S Pj 0 1
Rij
A C
Qi Pj
Qi Pj
Qi Pj
Qi Pj
32.
2
Theory of Computation
A - C - D
Qi Pj
Qi Pj
j
Reduced Automaton
0 + 1
Step 2:
Case – 1: Eliminating of „C‟
r1 : A - C - D
r2 : D - C - A
r3 : A - C - A
r4 : D - C - D
Elimination of C from A – C – D (r1)
Rij
Rij
Qi
1( 0 1)
r1 1 ( 0 1 ) *( 0 1)
1 ( 0 1 )( 0 1)
S r1 1 ( 0 1 )( 0 1) S
P 0 1
Elimination of C from D – C – A (r2)
Rij R r * ij 2
D - C - A Qi
S
S Pj
r2 0
Elimination of C from A – C – A (r3) Rij
Rij 0 1 r3 ( 0 1) 1(0 1) *
A - C - A Qi 1 (0 1) ( 0 1)
S r3 0 1
Pj
1 ( 0 + 1) 0 + 1
A C D
Qi Pj S
Regular Expressions and Languages 2.33
Eliminating C from D – C – D:- (r4)
Rij R r * (0 1) ij 4
D - C - D Qi
S r4
Resulting Automaton
Pj
0, 1
R U S
1 2 T
Regular Expression (RE1) = (R + SU*T)* SU*
= ((0+1) + (1(0+1) (0 +1) (Ǿ)*Ǿ)*
= (1(0+1) (0+1) Ǿ*)
= ((0+1) + Ǿ)* (1(0 + 1) (0+ 1))
= (0+1)* (1 (0 + 1) (0+ 1))
Case – 2: Elimination of D from Automation obtained from step – 1
A - C - D
D - C - A
A - C - A
Eliminating D from A- D – C (r1)
R
ij
1(0 1) r1 1 ( 0 1) *
A - D - C Qi 1( 0 1)
Qi Pj S
S
Pj
r1 1(0 1)
1(0+1) (0+1)
A D
Rij
Qi Pj
S
34.
2
Theory of Computation
Rij
Eliminating D from C – D – A (r2)
Rij
R
r ( 0 1) *
ij 2
A - D - C Qi 0 1
Qi S
Pj S
Pj
r2
Eliminating D from A – D – A (r3)
Rij Rij 0 1 r3 ( 0 1) *
A - D - A Qi ( 0 1)
S r3 0 1 Qi Pj
S Pj
Eliminating D from C – D – A (r4)
Rij
r4 ( 0 1) *
C - D - A Qi 0 1
S
The Reduced Automaton
S
Pj
r4
R
S U
1 2
T
Regular Expression (RE1) = (R + Su*T)* SU*
Final Automaton
RE1 = (0+ 1) + (1 (0 + 1)) * )* (1 (0 + 1)) *
RE2 = (0 + 1) * 1 (0 + 1)
R = RE1 + RE2
= (0 + 1)* 1(0+1)(0 + 1) + ( 0 + 1)* 1( 0 + 1)
0+1 1 (0+1)
A C
Qi Pj
Regular Expressions and Languages 2.35
CONVERTING R.E TO AUTOMATA
THEOREM
Every language defined by a regular expression is also defined by definite automata
(or)
Let „r‟ be a regular expression, then there exists a NFA with Є – transition that accepts
L (r).
Proof
Suppose L = L[R] for a regular expression, R. we show that L = L[E] for some – NFA,
with
(i) Exactly one accepting state.
(ii) No arcs into the initial state.
(iii) No arcs out of the accepting state.
Structural Induction on R
Basis
(a) r =
(b) r =
(c) r = a
In (a) → the RE, is handled. The language = {}, since the only part from the start
state to the accepting state, labeled Є.
In (b) → It shows the construction of clearly there are no paths from start state
L = {}
In ( c) → It gives the automaton for a RE ,‟a‟. The language of their automaton
evidently consists of the one string, „a‟ which is also L [a]. These automaton satisfy conditions
(i), (ii), (iii) of the induction hypothesis.
a
Є
36.
2
Theory of Computation
R Є S
Inductive Step
Assume that the theorem is true for the immediate sub-expressions of a given regular
expression. The languages of Є- NFA‟s with a single accepting state.
The expression is R + S for some smaller expression R and S.
It starts at a new state, can go to the start state of either R or S, and then reach the
accepting state of one of these automata following a path labeled by some string L[R]
or L[S] respectively.
Once we reach the accepting state of the automaton for R or S.
(a)
Є
R + S
Є
We can follow one of the Є – arcs to the accepting state of the new automaton.
Thus the language of this automaton is L [R] U L [S].
(b) R.S
The expression is R. S for some smaller expression R and S.
The start state of the first automaton becomes the start state of the whole and the
accepting state is the accepting state of the whole.
The idea is that the only paths from start state to accepting state, go first through the
automaton for R where it must follow a path labeled by a string in L[R], and then
prove the automaton for S where it follows a path labeled by a string in L [S].
Thus the language is the concatenation of two regular expressions, L[R] L[S].
Є Є
Є Є
Regular Expressions and Languages 2.37
0
Є 2 4 Є
1 6
1
Є 3 5 Є
(c) R*
Є
The expression is R* for some smaller expression, R.
This automaton allows is to go either:
o Directly from the start state to the accepting state along a path labeled . That
path which is in L [R*] doesn‟t matter what expression R is.
o Goes to the start state of the automaton for R and processes through that automaton one or more times and then to the accepting state. This set of paths allows us to accept strings in Є, L[R], L[R]L[R], L[R]L[R]L[R and so on.
Thus covering all strings in L[R*] except perhaps which was covered by the direct
arc to the accepting state mentioned in (c)
(d) The expression is (r) for some smaller expression, R.
The automaton for R also serves as the automaton for (r).
Since the parenthesis does not change the language defined by the expression.
It is a simple observation that the constructed automaton satisfies the three
conditions given the inductive hypothesis, one accepting state with no arcs into the
initial state or out of the accepting state.
PROBLEMS
1) Convert the RE: (0 + 1)* 1(0+1) to -NFA
(0+1)
Є R Є
Є
38.
2
Theory of Computation
0
Є 3 5
Є
Є 7 8
1 2
1 Є
Є 4 6
Є
0 Є
3 5 Є Є 1
10 Є 7 8 9
1 2 Є
1
Є 4 6
Є
0 0 1 1
Є
3 5 Є Є 1 Є
Є 1
Є 1 1
1 2 7 8 9
Є 1
Є 1
4 6 Є 1 1 Є
Є
(0+1)*
(0+1)*1
(0+1)* 1(0+1)
Regular Expressions and Languages 2.39
1 2
Є Є Є 1 Є
1 2 3 4 5 6
Є
1 Є 0 1 2 3
4
2) Convert the RE: 01* to -NFA
0
0
1*
Є
01*
Є
3) Convert the RE:- (0+1)0 to – NFA
0
2 4 Є
Є
1
1
Є 3 5
Є 0
6 7 8
Є
4) Convert the RE: 10 + (0 + 11) 0*1
10
Є 1 Є
1 2 3 4
Є
40.
2
Theory of Computation
Є Є
2 4
8 1 Є
1 Є 1 Є
3 5 6 7
Є Є Є 1 1 2 0 3 4 5 6
Є
1 Є 0 2 3 4 5
Є
Є
1 1
Є 0
Є 8 9
Є Є 0 Є 1
6 7 11 1 1 1 1
Є Є
1 Є 1 Є 1
Є
1 1 1
0 Є 0
1 2 3 4
0+11
0
0*1
Є
10 + (0 + 11)0*1
5) Convert the RE: 00(0+1)* to – NFA
00
Regular Expressions and Languages 2.41
Є 3 4 Є
Є Є
1 2 Є Є
7 8
1
5 6
Є
0
8 9
0 Є 0 Є Є Є Є
13
1 2 3 4 5 6 7 12 Є
1 Є
10 11
Є
Є 0
3 4
Є Є
1 2 Є Є
7 8
5 6
Є
1
(0+1)*
0
00(0+1)*
Є
6) Convert the RE:- (0+1)* (00+11) (0+1)* to – NFA
(0+1)*
42.
2
Theory of Computation
1
Є Є
Є
00+11:-
0 Є 0
(0+1)*(00+11)(0+1)*
Є
0
Є
Є
Є Є
Є 0 Є 0 0
Є Є
1
Є Є Є Є Є
Є 1 Є
1 Є 1
2 3 4 5
Є
Є
1 10
Є
Є 1 Є 1
6 7 8 9
Regular Expressions and Languages 2.43
PROVING LANGUAGES NOT TO BE REGULAR
The following theorem, known as “Pumping Lemma for Regular Languages” is
used as a tool to prove that certain languages are not regular. The principle behind this lemma
is “Pigeon Hole principle.”
PIGEON – HOLE PRINCIPLE
If „n‟ objects are put into „m‟ containers, where n > m, then at least one container must
hold more than one object.
Theorem: // Pumping Lemma AU NOV/DEC 2013
Let L be a regular language, then there exist a constant n (which depends on L) such
that for every string w in L such that w
w = xyz
such that
(i) y ≠
n , we can break „w‟ into 3 strings:
(ii) xy n
(iii) For all k ≥ 0, the string xykz is also in L.
i.e., we can always find a non – empty string y not too far from the beginning of w that
can be pumped (repeating y any number of times or deleting it keeps the resulting string in the
language, L.
PROOF
Suppose L is regular, then L = L [A] for some DFA, A. Suppose A has n states.
Consider any string, w of length „n‟or more, say w = a1, a2,… am where m ≥ n and ai is
an input symbol. For i = 0,1,2,….,n define state Pi (q0 ,( a1 , a2 , .. an )) where is the
transition function of A and q0 is the start state of A. i.e, Pi is the state A after reading the
first I symbols of w.
Assume that, q0 = P0 = (q0, Є). Since we have only n states, it is easy to see that
every string consisting „n‟ or more symbols must cause at least a state to repeat. (This
principle is called as Pigeon hole principle).
Therefore, we can find two different integers I and j such that 0 ≤ i < j ≤ n with Pi = Pj.
By assuming the loop occurs are Pi, we can break the string w as,
w = xyz such that
(i) x =x a1, a2, ….ai
44.
2
Theory of Computation
x = a1, a2, …, ai z = aj+1, aj+2, …, am
P0 Pi Pn-1
(ii) y = ai+1, a1+2,….aj
(iii) z = aj+1, aj+2,….am
y = ai+1, a1+2, ... ,aj
That is, x takes us from P0 to Pi once, y takes us from Pi back to Pi (since Pi is also Pj )
and z takes us from Pi to Pn-1. Note that x may be empty in the case that i = 0, also z may be
empty if j = n = m. However y cannot be empty since I is strictly less than j. Now consider
what happens if the automaton A receives the input xykz for any k ≥ 0.
Case 1:
If k = 0, then the automaton from the start state q0 (which is also Po) to Pi on input x.
Since Pi is also Pj it must be that A goes from Pi to the accepting state on input z. Thus
accepts xz.
Case 2:
If k > 0 , then A goes from q0 to Pi on input, x circles from Pi to Pi, k – times on input
yk and then goes to the accepting state on input, z Thus for any k ≥ 0, xy
kz is also accepted by
A that xykz is in L. Therefore we can say that L is a regular language.
PROBLEMS
1) By using pumping lemma, P.T the language,
language.
Solution:
L{on
on / n 1}
is not a regular
Assume that L is a regular language Let n be a constant and w on o
n such that
w n . By pumping lemma, w can be splitted into 3 parts such that w = xy with
(i) y ≠ [ i.e, y 1 ]
(ii) xy n
(iii) xykz L
Since y≠
k 0
x= 0 n-1
// since xy n
Regular Expressions and Languages 2.45
Thus
y = 0 // Since
z = 10n
(i) y≠
y 1
(ii) xy ( n 1) 1 xy n
(iii) When k = 0; xykz = xy
0z = xz = 0
n-110
n Є L.
The number of zeroes should be equal. Here it is a contradiction.
L is not a regular language
2) Show that the language, L = {aib
j,c
k/ 0 ≤ i < j < k} is not regular.
Assume that L is regular and let n be any integer. Consider the string, w = anb
n+1c
n+2.
Then w > n.
By pumping lemma, w can be splitted as w = xyz such that
(1) y 0, y
(2) xy n
(3) xykz L for k ≥ 0
Since xy n
x = an-1
xy = an-1
. a1 = a
n xy n
y = a y 1
z = bn+1 cn+2
At k = 0:-
xy0z = xz
At k = 1:-
= an-1.bn+1.cn+2 = an-1.bn+1.cn+2
xy1z = a
n-1.a. b
n+1.c
n+2
= an.bn+1.c n+2
At k = 2:-
xy2z = a
n-1.a.a. b
n+1.c
n+2
46.
2
Theory of Computation
= an+1.bn+1.cn+2
Comparing the powers of a, b and c, the language L is not regular.
3) Show that L = {ap | p is a prime number} is not regular. AU – DEC 2007, NOV 2011
Assume that L is regular and n be any integer. Let P ≥ n is a prime number and w = ap
Є L. By pumping lemma, w can be written as w = xyz such that
(i) y 1 y
(ii) xy n
(iii) xykz L
Let,
k 0
Y = am where m ≥ 1
Consider xykz = xyz . y
k-1
xykz = xyz.y
k-1
y m n
Let w= ak ; k → first prime
k+1 → Next prime > P
xyk z xyz y
k1 xyk1z
xyz yk
= P + (k-1) y = k + k y
= P + (k-1)m = k(1+ y
= P + Pm ← k = p+1 P = k -1
= P(1+m)
) → not prime
Both p and (1+m) are greater than 1. The product of P and (1 + m) cannot be prime
xykz L for k = p+1
L is not regular
4) Show that L = {0n 1
n | n ≥ 1} is not regular. AU MAY 2004, DEC 2004, DEC 2005
Assume that L is a regular language. Let n be a constant and w = 0n 1
n such
that w n . By pumping lemma, w can be splitted into 3 parts such that w = xyz with
(i) y ≠ (i.e y 1 )
(ii) xy n
(iii) xykz L k 0
Regular Expressions and Languages 2.47
Since xy n and y ≠ ,
x= 0n-1
y = 0
z = 1n
By pumping lemma, xykz ≥ L for k ≥ 0
When k = 0;
xy0z = xz
0n-1.1n
L [Comparing the power of 0 and 1 with w]
L is not a regular language.
5) Show that L = {ai 2
/ i 1} is not a regular
AU MAY 2006, DEC 2006, DEC 2007, DEC 2008
When , i = 1; L = { a1 2
} = a1
i = 2; L = { a2 2
} = aaaa = a4
i = 3; L = { a3 2
} = aa
The no of a‟s is always a perfect square. Consider L = an 2
where length = n2
w n2
By pumping lemma, w =xyz, where y
At k = 1 xy1z = xyz = a
n 2
1 , xy n, and xykz L
1
k 0
xyz x y z n2
2
At k = 2 xy2 z x y
2 z n
2 3
From 2 and 3,
x
y z
> x y2 z
xy2 z n
2 4
Since xy n;
4 xy2 z \ n
2
48.
2
Theory of Computation
When y n and x
xy2 z n
2 n
xy2 z < n
2 + n + n +1 [For inequality removal]
xy2 z
From 4 and 5,
< (n 1)2
n2 < xy
2 z < (n 1)
2
Example: n = 2; n2 = 2
2 = 4
(n+1) = 2+1 (n+1)2 = 3
2 = 9
There is no perfect square between 4 and 9. Henceforth, xy2 z is not a perfect square
since there is no perfect square between n and (n+1)
xyz L since xykz L
L is not regular.
6) Using Pumping lemma, prove that L = om,n
omn
m 1 and n 1 is not regular.
AU DEC 2006, DEC 2013, MAY 2013
Let us assume that L is regular and L is accepted by a FA with „n‟ states.
Let w = om,n o
mn
w m n (m n) 2(m n) n
Let w = xyz such that,
y > 0 and
xy n
x o m1
y = 0
z = 1n om+n
At k = 0; xykz xyoz = o
m-1.1
n.o
m+n 1
From (1), the powers of O is not equal with that of L
Hence xyz L
L is not regular language.
Regular Expressions and Languages 2.49
7) Using pumping lemma, P.T:- L wwR
| w(0, 1) * is not regular. AU DEC 2006
Let us assume that L is regular and is accepted by a FA with n states.
Let w = anb
wR = ba
n
L = wwR ={anbba
n}
Consider w = xyz such that
(i) y > 0
(ii) xy n
(iii) xyk z L k 0
Let w be splitted as,
x = a n-1
y = a
z = b.b.an
At k = 0:-
xykz xy
0z xz = a
n-1.b.b.a
n
Since the powers of a does not match, L is not regular.
8) Using Pumping lemma, P.T:- {w.w | w (0, 1)*} is not regular.
Let us assume that L is a regular and is accepted by a FA with „n‟ states.
Let w = anb
L = {ww} = {anba
nb}
Let w =xyz such that
(i) y > 0
(ii) xy n
(iii) xyk z L k 0
w can be splitted as
x = a n-1
y = a
50.
2
Theory of Computation
z =b.an b.
At k = 0:-
xykz xy
0z xz = a
n-1.b.a
n b
Since the powers of a does not match with w, L is not regular.
CLOSURE PROPERTIES OF REGULAR LANGUAGES
AU MAY 2012, MAY 2013, NOV 2010, NOV 2011, NOV 2013, NOV 2012
If an operation on regular languages generates a regular language, then we say that the
class of regular languages is closed under the above operation.
Some of the important closure properties of RL are as follows.
1. Union
2. Difference
3. Concatenation
4. Intersection
5. Complementation
6. Transpose / Reversal
7. Kleene star
8. Homomorphism
9. Inverse homomorphism
Closure under Union
Theorem
If L and M are regular languages, then LUM is also regular.
Proof
Let, L = L [R]
M = L[S]
LUM = L[R] U L [S]
= L [R+S]
Example
Let m1 = ( S, ∑, 1 , So, F) and
Regular Expressions and Languages 2.51
0 1
q0 q0 q0
m2 = (Q, ∑, 2 , q0, G) be two given automata
Now m3 = L [m1] U L [m2]
= (R, ∑, 3 , ro, H),
Where r0 → New start state having two Є – moves from r0 → s0 and r0 → q0 being added,
R = SUQ U{r0}
H = FUG
3 1 U 2 U{(ro , , so ), (ro ,, qo )}
Hence, L [m3] = L[m1] U L [m2]
Thus, Regular Language is closed under Union.
Closure under Complementation
Theorem
If L is a regular language over the alphabet, ∑ then L * L
language.
Proof
Let L= L [A] for some DFA, where
A = (Q, ∑, , q0, F)
Then, L = L [B], where
B = (Q, ∑, , q0, Q-F)
is also a regular
i.e., B is exactly like A, but the accepting states of A have not become the accepting
states of B and vice-versa.
Then w is in L [B], if and only if,
Example
(q0 , w) is in {Q F}, which occur if and only if w is not in L [A].
Find the complement of (0+1)*01
0,1
52.
2
Theory of Computation
1 0
0 1
{q0} {q q } 0, 1
{q q } 0, 2
0
1
Here,
QN = {q0, q1, q2}
∑= {0, 1}
q0 = {q0}
FN = {q2}
N
→
By subset construction technique,
QD = {, {q0}, {q1},{q2}, {q0, q1}, {q0, q2}, {q1, q2},{q1, q2,
q3}} FD = {{q2}, {q0, q2},{q1, q2},{q1, q2, q3}}
D
Reachable states from {q0}/ Minimised D is
∑
QN
0
1
q0 {q0, q1} {q0}
q1 {q2}
*q2
∑
QN 0 1
{q0} {q0, q1} {q0}
{q1} {q2}
* {q2}
{q0, q1} {q0, q1} {q0, q2}
* {q0, q2} {q0, q1} {q0}
* {q1, q2} {q2}
* {q0, q1, q2} {q0, q1} {q0, q2}
Regular Expressions and Languages 2.53
{q0} 0 1
{q q } 0, 1 {q q } 0, 2
0
1
Complement of DFA
0 1
Closure under Intersection
Theorem
If L and M are two regular languages, then L M is also regular language.
Proof
We define A = (QL x QM, ∑, (qL , qm ), qL xqm , FL xFm )
where,
((p, q), a) L(p, a), M(q, a)
L[A] L [AL ]
By induction on w ,
((qL , qM ), w) M(qM , w))
But A accepts w if and only if ((qL , qM ), w)
is a pair of accepting states
(i.e.,) (qL , w)
(qM , w)
must be in FL and
must be in FM
Putting in another way, “ w is accepted by A if and only if both AL and AM accepts w.
Thus A accepts the intersection of L and M.
Example
Construct A B where A and B is given by
L[AM ]
54.
2
Theory of Computation
1 0,11
A
0 0,11
B
Where
AA = (QA, ∑, δA, qa, FA)
AB = (QB, ∑, δB, qB, FB)
A B(QA x QB , , (qA x qB , FA x F B )
(( p, q), a) L (p, a), M (q, a))
Here,
QA x QB = {p,q} x {r,s}
={(p, r), (p, s), (q, r), (q, s)}
δ(qA, qB)
Z = {0, 1}
qA x qB = {p, r}
FA x FB = {q, s}
0
p q
1
r s
∑
Q ↓
0
1
→ {p, r} {q, r} {p, s}
{p, s} {q, s} {p, s}
{q, r} {q, r} {q, s}
* {q, s} {q, s} {q, s}
Regular Expressions and Languages 2.55
{p, r } 1 {p, s }
0 0
{q, r } 1
{q,s} 0, 1
0
Finite Automata
Closure under Reversal
Theorem
If L is a regular language, then L R is also regular.
Proof
Assume L i9s defined by regular expression. The proof is a structural induction on the
size of E. We show that there is another regular expression.
ER such that L(ER) = (L(E))R.
That is the language ER is the reversal of the language of E.
Basis
If = Є / / a, then
ER = / / a
E = ER
Induction
There are 3 cases depending on the form of E.
1) E = E1 + E2 then ER = E1R + E2 R
The justification is that the reversal of the union of two languages is obtained by
computing the reversals of the two languages and taking the union of those languages.
2) E = E1.E2 then ER = E2R. E1R
Example: L (E1) = {01,111}; L (E2) = {00, 10}
56.
2
Theory of Computation
1
1
Then, L (E1. E2) = {0100, 0110, 11100, 11110}
L (E1 . E)R = {0010, 0110, 00111, 01111} 1
L(E1)R = {10,111}
L (E2)R = {00,01}
L(E2)R. L (E1)R = {0010, 0110, 00111, 01111} 2
From (1) and (2) L (E1E2) R = L(E2)R. L (E1)R
3) E, E1* then ER , ( ER )*
The justification is that any string w in L (E) can be written as w1, w2,…, wn where each
w1 is in L(E). But,
wR wR wR ............ wR
Each
n n1 1
wR is in L (E
R), so w
R is in ( ER ) Conversely any string in L ( ER )* is of the form
1 1 1
w1, w2,…wn, where each w1 is the reversal of a string in L(E1). The reversal of the string, w
R w
R .......w
R is therefore a string in L ( E* ) which is L (E). n n1 1 1
Thus a string is in L (E) if and only if its reversal is in L (ER
) *
Example: If L = (0 + 1)0*
Then LR = 0* (0 + 1) [By applying closure under concatenation, and union]
Closure under Difference
Theorem
If L and M are regular languages then L – M is also regular
L M L
Proof
Since M is a regular language, M is also a regular language by closure under
complementation. Since L and M are regular, the L
intersection.
L – M is a regular Language.
is also regular by closure under
M
M
Regular Expressions and Languages 2.57
Closure under Concatenation
Let M1 ( s, , 1 , so , F)
and M2
Theorem
( Q, , 2 , qo , G) be two given automata.
If two languages, M1 and M2 are regular, then L (M1) . L (M2) is also regular.
L(M3) = L (M1). L (M2)
M3 is given by, M3 = (R, ∑, δ3, so, G)
δ3 = δ1 U δ2 U {Є – move from every final state of M1 to start state of M2}
Closure under kleene star
Theorem
Let M1 = (Q, ∑, δ, q0, F) be the given automata. Then M2 can be constructed such that,
L(M2) = L (M1)*.
M2 is constructed as:
(i) A new start state so is added with an – move is from so to qo.
(ii) A new final state f0 is added with – move is added from s0 to fo as id s
member of L(M1)*.
M2 = (qU{s0, f0}, ∑, δ , s0, {f0})
Homomorphism
A string homomorphism is a function on strings that works by substituting a particular
string for each symbol.
Example: Let n(0) = ab
h(1) =
h(0011) = h(0)h(0)h(1)h(1)
= abab
Theorem
If L is a regular language over an alphabet, ∑ then h is a homomorphism function on
∑then h (L) is also regular.
58.
2
Theory of Computation
Proof
To prove: L [h(E)] = hb [L(E)]
Basis
If E = or , then
H(E) = or
L[h(E)] = L (E)
If E is or , then L(E) contains either no strings or a string with no symbols.
Thus,
H[l (E)] = l(E) = L[h(E)]
If E = a, then
L(E) = {a}
h[(E)] = {{h(a)} = L[h(E)] for E = {a}
Induction
There are 3 cases in induction part → Union, Concatenation and closure.
Let E = F + G
L[h(E)] = L[h(F + G)]
= L[h(F) U h (G)]
= L[h(F)] U L[h(G)] 1
H[L(E)] = h[L(F + G)]
= h [L(f) U L (G)]
= h [L(f)] U h [L(G)] 2
Therefore by induction, from (1) and (2)
L[h(f)] = h[L(f)] and
L[h(G)] = h [L(G)]
Similarly concatenation and closure is also true,
L[h®] = h[L(R)] Hence proved
Regular Expressions and Languages 2.59
Inverse homomorphism
h1 (L)
Theorem
is the set of strings, w in ∑* such that h(w) is in L.
If h is a homomorphism from alphabet, ∑ to alphabet T and L is regular language over
T, then h1 (L)
Proof
is also a regular language.
We construct a DFA, A for L, then a DFA for h1 (L) by using A and h. This DFA uses
the states of A but translates the input symbol according to h before deciding on the next state.
Let L be L(A) where DFA, A |(Q, ∑,δ, q0, F) define a DFA, B = (Q, ∑, γ, q0, F) where the
transition function γ is constructed by,
(q, a) (q, h (a)). It is a easy induction on w to show that
(q, h (w)) (q, w)
Since the accepting states of A and B are same, B accepts w if and only if A accepts
h(w) i.e., B accepts exactly those strings w that are in h1 (L)
Example: Suppose h is a homomorphism from the alphabet {0, 1, 2} to the alphabet
{a, b} defined by h(0) = a, h(1) = ab, h(2) = ba.
(i) h (0120) = h (0)h(1)h(2)h(0)
= aabbaa
(ii) h(21120) = h(2)h(1)h(1)h(2)h(0)
= baababbaa
(iii) L[01*2]
H(L) = h(01*2)
= L[h(01*2)]
= L[h(0)h(1*)h(2)]
= L[a(ab)*ba]
(v) h1
(L) h-1
(ababa) where L = ababa
= h-1
(a)h-1
(ba)h-1
(ba)
= 022.
60.
2
Theory of Computation
M1 M2
2. 7 EQUIVALENCE AND MINIMISATION OF AUTOMATA
Two finite automata m1 and m2 are said to be equivalent if they accept the same
language.
i.e., L (m1) = L(m2)
Two FA m1 and m2 are not equivalent over ∑ if there exists a string w such that,
w L(m1 ) and w L(m2 )
(or)
wL(m1 ) and w L(m2 )
EXAMPLE:
1) Show that the automata given below are equivalent or not.
d
d
Step 1:
Here the start states are <q1> and <q4>
We shall start with <q1, q4> over ∑ ={c, d}
3 ( q1 , q4 c)
3 ( q1, q4 d)
1 (q1, c), 2 (q4 , c)
q1 , q4
1 (q1, d), 2 (q4 , d)
q2 , q6
c
q1
d
c
q1
q1 c
c d
q4 q5
d d
c c
q6 q7
c
Regular Expressions and Languages 2.61
d
<q1 , q4> <q2 , q5>
d d c
c
<q2 , q6> <q3 , q7>
c
Step 2:
State <q2, q6> over ∑ ={c, d}
Step 3:
3 ( q2 , q6 , c)
3 ( q2 , q6 , d)
1 (q2 , c), 2 (q6 , c)
q3 , q7
1 (q2 , d), 2 (q6 , d)
q1 , q4
State <q3,q7> over ∑ ={c, d}
Step 4:
3 ( q3 , q7 , c)
3 ( q3 , q7 , d)
1 (q3 , c), 2 (q7 , c)
q2 , q5
1 (q3 , d), 2 (q7 , d)
q3 , q7
State <q2, q5> over ∑ ={c, d}
3 ( q2 , q5 , c)
3 ( q2 , q5 , d)
1 (q2 , c), 2 (q5 , c)
q3 , q7
1 (q2 , d), 2 (q5 , d)
q1 , q4
Thus every state in m3 has been expanded.
The construction of combine DFA is as follows.
62.
2
Theory of Computation
<q1 , q4> → both are final states
<q2 , q5> → Non final states
<q2 , q6> → Non final states
<q3 , q7> → Non final states
From the state obtained from the above states, m1 and m2 are equivalent.
Example: c, cccc, dd, ddcc, dcdcd, dcddcdcc, …. are accepted by both m1 and m2.
MINIMIZATION OF DFA
Minimization of DFA is a process of constructing an equivalent DFA with minimum
number of states.
Equivalent states/ k – equivalence
Let qi and qj are k – equivalent states if and only if no string of length ≤ k can
distinguish them.
If there exists a string of length k, which can distinguish qi and qj, the states qi and qj
are said to be k – distinguishable.
Algorithm
Algorithm for minimization divides states of a DFA into blocks such that:
1. States in a block are equivalent.
2. No two states taken from two different blocks are equivalent.
The first step is to partition the states of M into blocks such that all states in a block are o-
equivalent. This is done by placing,
a. Final states into one block
b. Non-final states into another block
c. The next step is to obtain the partition, P1 whose blocks consists of the set of states
which are 1- equivalent [length = 1]
Regular Expressions and Languages 2.63
0 1 0 q0 q1 q2 q3
0 1
1
0 1
1 1 0 q4 q5 q6 q7
1
0 0
PROBLEMS
1) Minimise the following DFA
0
Solution:
0 - Equivalence partitioning:
The given set of states in the DFA are classified into two,
1) States that are not final states bock 1
2) Final states block 2
P0
block1 = {q0, q1, q3, q4, q5, q6, q7}
block2 = {q2}
1 – Equivalence partitioning:
This divides the existing non final states based on the inputs that the states accept.
Processing input „0‟:
( q0 , 0) q1
( q1 , 0) q6
( q3 , 0) q2
( q4 , 0) q7
[ block1]
[ block1]
[ block 2]
[ block1]
Equivalent states since the transition
result in same output.
64.
2
Theory of Computation
( q5 , 0) q2 [ block 2]
( q6 , 0) q6 [ block1]
( q7 , 0) q6 [ block1]
Processing Output –„1‟:-
( q0 ,1) q5
( q1 ,1) q2
( q3 ,1) q6
( q4 ,1) q5
( q5 ,1) q6
( q6 ,1) q4
( q7 ,1) q2
[ block1]
[ block 2]
[ block 1]
[ block1]
[ block1]
[ block1]
[ block 2]
Equivalent states
states.
From the above process of input o and 1, {q3, q5}-, {q1, q7} are a set of equivalent
1 – equivalence partitioning
P1 = (q3 , q5) , (q1, q7), (q0,q4,q6), (q2)
Block 11 Block 12 Block 13 Block 2
2 – Equivalence partitioning:
Block – 11
( q3 , 0) q2
( q5 , 0) q2
( q3 , 0) q6
( q5 , 0) q6
Block – 12
( q1 , 0) q6
( q7 , 0) q6
Mapped to the same block, Block 2
Mapped to the same block, Block 12
Mapped to the same block, Block 13
Regular Expressions and Languages 2.65
0 1
0 1 < q2> < q0, q4> < q1, q7>
1 0
0
1
< q1, q7 > <q6>
1 0
( q1 ,1) q2
( q7 ,1) q2
Block – 13
( q0 , 0) q1
( q4 , 0) q7
( q6 , 0) q6
Mapped to the same block, Block 2
Mapped to the same block, Block 12
→ belong to the same block Block 12
( q0 ,1) q5 → belong to the same block Block 11
( q4 ,1) q5
( q6 ,1) q4
Here q6 doesn‟t map with any other states hence it is distinguished from {q0, q4}
2 – Equivalence partitioning
P2 = (q3, q5), (q1, q7) (q0, q4) (q6) (q2)
Since further partitioning cannot be done, the final minimized DFA is given by
2) Minimise the DFA given below AU MAY 2007
Input
States a b
→ q0 q0 q3
q1 q2 q5
q2 q3 q4
q3 q0 q5
q4 q0 q6
q5 q1 q4
* q6 q1 q3
66.
2
Theory of Computation
0- equivalence partitioning
P0 = (q0, q1, q2, q3, q4, q5) (q6)
block 1 block 2
1 – equivalence partitioning
( q0 , a ) q0
( q1 , a) q2
( q2 , a) q3
( q3 , a ) q0
( q4 , a) q0
( q5 , a) q1
( q0 , b) q3
( q1 , b) q5
( q2 , b) q4
( q3 , b) q5
( q4 , b) q6
( q5 , b) q4
There are no equivalent states. But still, q4 is the distinguishable state from the rest of
the states
P1 (q0, q1, q2, q3, q5) (q4) (q6)
block 11 block 12 block 2
2 – equivalence partitioning:
From the above transitions, there are no equivalent states but there are distinguishable
states, {q2, q5} since
( q2 , a ) q3
( q5 , a) q1
( q2 , b) q3
( q3 , b) q0
→ block 11
→ block 12
P2 = (q2, q5) (q0, q1, q3) (q4) (q6)
Block 111 Block 112 Block 12 Block 2
Regular Expressions and Languages 2.67
3- Equivalence partitioning:
( q0 , a) q0
( q1 , a) q2
( q3 , a) q0
( q0 , b) q0
block 112
block 111
block 112
block 112
( q1 , b) q0
( q3, b) q0
block 111
block 111
Since no states are equivalent, q0, q1, q3 are distinguishable from each other.
P3 (q2 , q5 ) (q0 ) (q1 ) (q3 ) (q4 ) (q6 )
Block 111 B-1121 B1122 B1123 B12 Block 2
4 - Equivalence partitioning:
But again in 4 – equivalence partitioning, (q2, q5) is considered.
(q2, a) = q3 → block 1123
(q5, a) = q1 → block 1122
(q2, b) = q4 → block 12
(q3, b) = q4 → block 12
P4 = (q2) (q5) (q0) (q1) (q3) (q4) (q6)
Thus the given DFA cannot be minimized since no equivalent states are obtained.
3) Construct a min –DFA for
Inputs
States ↓ 0 1
→ q0
q1
* q2
q3
q4
* q5
q6
q7
* q8
q1
q2
q3
q4
q5
q6
q7
q8
q0
q4
q5
q7
q7
q8
q1
q1
q2
q4
68.
2
Theory of Computation
0- equivalence Partitioning:-
P0 =(q0, q1, q3,q4,q6,q7) (q2,q5,q8)
Block 1 Block 2
1- equivalence partitioning:-
( q0 , 0) q1 Block 1
( q1 , 0) q2
( q3 , 0) q4
Block 2
Block 1
( q4 , 0) q5 Block 2
( q6 , 0) q7 Block 1
( q7 , 0) q8 Block 2
( q0 , 1) q4 block 1
( q1 , 1) q5 block 2
( q3 , 1) q7
( q4 , 1) q8
block 1
block 2
( q6 , 1) q1 block 1
( q7 , 1) q2 block 2
From the above transitions,
P1 = (q0, q3, q6) (q1, q4, q7) (q2, q5, q8)
block 11 block 12 block 2
2- Equivalence partitioniong:
Block 12
Block 2
( q0 , 0) q1
( q3 , 0) q4
( q6 , 0) q7
Block 12
( q0 , 1) q4
( q3 , 1) q7
( q6 ,1) q1
( q1 , 0) q2
( q4 , 0) q5
( q7 , 0) q8
Block 2
( q1 , 1) q5
( q4 , 1) q8
( q7 , 1) q2
Regular Expressions and Languages 2.69
0, 1
{q0, q3, q0} {q1, q4, q7}
0, 1
0
{q2, q5, q8} 1
( q2 , 0) q3
( q5 , 0) q6
( q8 , 0) q0
Block 11
( q2 , 1) q7
( q5 , 1) q1
( q8 , 1) q4
Block 11
P2 = (q0,q3,q6) (q1,q4,q7) (q2,q5,q8)
The States cannot be further subdivided.
The minimized DFA is given by
UNIT – 3 CONTEXT FREE GRAMMAR AND LANGUAGES
GRAMMAR
Every language is generated by some grammar.
It produces strings of a language by means of replacing symbols.
In general, a grammar is a set of production rules for generating strings in a language.
A grammar can also be called as formal grammar, which generates strings in a formal
language.
A grammar generates a language with the aid of four elements, G=(V,T,P,S) such as,
o Terminal symbol
o Non terminal symbol
o Start symbol
o Production rules
Terminal refers to the symbols of the alphabet being used.
o There are several types of terminals namely –
Binary alphabet 0 and 1
Uppercase alphabet A, B, … , Z
Lowercase alphabet a, b, … , z
Decimal alphabets 0, 1, 2, … , 9 etc.
o Most commonly used terminals are binary and lowercase types.
The non-terminal symbols are those variables that denote/produce some terminals in-
turn.
o Non-terminals are equivalent to the states of automation.
o They are usually represented by uppercase alphabets like. A, B, … , Z.
The start symbol is a special non-terminal that works similar to the start state of a
finite automation.
o It is represented by an element in variable set.
The production rules are the translation functions/replacement rules of the variables.
Context Free Grammar and Languages 3.2
Example: (Informal)
Consider the string: „sentence‟.
A sentence is formed by „subject‟, „verb‟, „object‟.
So,
Start symbol: <sentence>
and,
<sentence> <subject> <verb> <object>
Now the above statement forms a production rule, which requires some strings to be
substituted /replaced for subject, verb and object.
Let
<subject> fruits
<verb> are
<object> fresh
Therefore,
<sentence> fruits are fresh one string in the language
where
Terminals: fruits, are, fresh
Nonterminals: <sentence>, <subject>, <verb>, <object>
Start symbol: <sentence>
Production rules:
<sentence> <subject> <verb> <object>
<subject> fruits
<verb> are
<object> fresh
A grammar thus generates a formal language, which is a set of strings formed by
applying production rules that is derived from the start symbol.
Example: (formal) (AU – Dec 2004, May 2005, June 2006)
Consider, the terminals a and b, the start symbol, S and the production rules:
S aSb
S ab
3.3 Theory of Computation
This may generate,
S aSb by applying rule 1.
aaSbb by applying rule 1 on S.
aaabbb by applying rule 2 on S.
Thus the above production rules may generate,
S {ab, aabb, aaabbb, a4b
4, … }
Therefore the production rules generate a language containing equal number of a`s and
b`s and is defined as
L = {an b
n | n ≥ 1}
PROBLEMS ON GRAMMARS
1. Construct the grammar representing the set of palindromes over (0 + 1)*.
Solution:
The first possibility is having a string of single / zero length.
S 0 |1|
For other strings,
S 0S0 first and last data are equal
S 1S1 first and last data are equal
Thus the grammar is given by
S 0 |1|
S 0S0
S 1S1
2. Let G = {{s}, {a,b}, P, S} with the productions,
P: S aSa
S bSb
S
Find the language generated by the grammar.
Solution:
a) S aSa [since S aSa]
aa [since S ]
b) S bSb [since S bSb]
bb [since S ]
Context Free Grammar and Languages 3.4
c) S aSa [since S aSa]
aaSaa [since S aSa]
aaaa [since S ]
d) S aSa [since S aSa]
abSba [since S bSb]
abba [since S ]
e) S bSb [since S bSb]
bbSbb [since S bSb]
bbbb [since S ]
f) S bSb [since S bSb]
baSab [since S aSa]
baab [since S ]
g) S aSa [since S aSa]
abSba [since S bSb]
abaSaba [since S aSa]
aba aba [since S ]
w wR
From the above examples, the language can be defined as,
L = {wwR | w (a, b)*}
Every string, when broken into two, contains the reverse of the first half as second
half. The language represents a set of palindromes over {a, b}.
3. Construct a grammar for L = {an | n is odd}.
Solution:
When n = 1 S = a1 = a [S a]
When n = 3 S = a3 = aaa [S aSa]
When n = 5 S = a5 = aaaaa [S aSa]
.
.
.
Therefore the production shall be defined as,
3.5 Theory of Computation
S a
S aSa
The grammar G = (V, T, P, S) is given by
V = {S}
T = {a}
P = {S aSa | a}
S = {S}
TYPES OF GRAMMARS
Noam Chomsky formalized grammars in 1956.
He classified them into four types which are collectively called as „the Chomsky
hierarchy‟.
Chomsky hierarchy consists of the following four levels, namely
o Type 0 grammars / unrestricted grammars
o Type 1 grammars / context sensitive grammars
o Type 2 grammars / context free grammars
o Type 3 grammars / regular grammars
Type 0 grammars
These are free / unrestricted grammars that have no restrictions on the production
rules.
All formal grammars belong to this class.
The grammars, G is defined by four tuples as G = (V, T, P, S) where
V finite set of variable / non terminals
T finite set of terminals
P set of production rules
S start symbol
The production rule is of the form,
| ≠
where , can be strings composed of terminals and non-terminals.
The most important property is that the left hand side () should contain at least one
non-terminal and cannot be empty.
Context Free Grammar and Languages 3.6
Type 0 grammar are also called as Recursively enumerable/Turing acceptable since
the machine halts if the input is accepted with the answer, „YES‟, else may loop
forever.
Properties of type 0 grammars
These grammars are closed under,
o Kleene star
o Concatenation
o Intersection
o Union
Example
o All computable functions are examples of type 0 grammars.
o Almost all natural languages fall under this category.
Type 1 grammar
The languages that follow type 1 grammars are called as context-sensitive grammars
(CSG).
A language is said to be context sensitive if the production rules are of the form,
A
non-empty
, can be empty
where,
A is a non-terminal symbol
, , are any combination of terminals and non-terminals.
Thus CSG possess two properties
o At least one symbol on the left hand side should be a non terminal.
o The number of symbols on the left hand side does not exceed the number of
symbols on the right hand side.
The machine/automaton that recognizes a CSG is called linear-bounded
machine/automaton which is a finite automaton with memory to store symbols.
Most importantly, every language described by context free grammars can be
described by a context sensitive grammar.
3.7 Theory of Computation
Properties
The type 1 grammar are closed under
o Union
o Concatenation
o Complementation
o Intersection
o Substitution
o Inverse homorphism
o Kleene plus
Example
Most programming languages are context sensitive.
o Eg: L = {an b
n c
n |n ≥ 1|}, L = { a
i2
|i ≥ 1|} are all context sensitive.
Type 2 gram1mars
Type 2 grammars are context free grammars.
A grammars, G = (V, T, P, S) is said to be context free if the production rule is of the
form,
A
The transition allows
A [i.e., ]
where,
A is a non terminal symbol
is any terminal or non-terminal symbol
Here, the left hand side of the transition rule consists of only one non-terminal.
Type 2 grammars are the basis of the syntax of most of the programming languages
such as XML.
Properties
The CFG is closed under
o Union
o Concatenation
o Kleene closure
Context Free Grammar and Languages 3.8
It is not closed under complementation, substitution, reversal.
Example
Consider the production, P {S aSa, S bSb, S }
Since
S aSa
aaSaa [as S aSa]
aabSbaa [as S bSb]
aabbaa [as S ]
Thus, S may generate
S = {, aa, bb, abba, aabbaa, abaaba, …}
Thus, the language is defined as
L(G) = {w wR | w {a, b}*}
CFG can be handled using the pushdown automaton that uses stack memory to
store the symbols.
Type 3 grammars
Type 3 grammars are regular grammars that describe regular / formal languages.
These grammars contain production rules consisting of
o only one non-terminal at the left hand side
o the right hand side having a single terminal and may or may not be followed by non terminals
Example
o A
o A a
o A b
o A aA etc
Types
There are two types of regular grammars namely,
o Right linear / Right regular grammar
o Left linear / Left regular grammar
3.9 Theory of Computation
Right linear grammar
This is a regular grammar with the production rules of the form
A (or) A B
where,
A, B non terminal symbols
terminal symbol
These grammars are right-branching in nature since the substitution is done for the
right most non terminal, if available.
Left linear grammar
This is a regular grammar with the production rules of the form:
A (or) A B
where,
A and B is non terminals and
is a terminal symbol
Since the substitution is done for the left most non-terminal, these grammars are left
branching in nature.
Properties of Regular grammars
The regular grammars generates regular languages which are closed under,
o Union
o Concatenation
o Intersection
o Complementation
o Difference
o Reversal
o Closure
o Homomorphism
o Inverse homomorphism
Chomsky hierarchy
The four forms of grammars follows the given hierarchy,
Type 3 grammar type 2 grammar type 1 grammar type 0 grammar
Context Free Grammar and Languages 3.10
Unrestricted Recursively
Enumerable Grammars
Regular grammars context free grammars context sensitive grammars
unrestricted grammars
Chomsky hierarchy
CONTEXT-FREE GRAMMARS AND LANGUAGES
A grammar is a means of representing a language.
A computational language is represented using a set of recursive equations.
The equations are called as productions.
Formal Definition (JUNE – 2013)
A context free grammar (CFG) is described by four tuples (quadruple) as,
G = (V, T, P, S)
where,
V is a finite set of variables / non terminals
T is a finite set of terminals / terminal symbols
P is a finite set of transitions / production rules
S is the start symbol, where S V.
The production rules are the recursive definitions of the languages.
The production is stated as,
Head of the production body of the production
where,
Head of the production is any variable defined in V.
Body of the production is set of zero or more terminals and/or non terminals.
Content-sensitive
Grammars
Content-free
Grammars
Regular
Grammars
3.11 Theory of Computation
Thus the production is of the form,
Vi i ,
where,
Notations
Vi V
i {VUT}
The terminals are denoted by digits (0, … , 9) or lower case letters (a, … , z) etc. It
also includes special characters like +, -, *, (, ) etc.
Non terminals are denoted by uppercase letters (A, ….., Z)
Example
Generation of palindromes over the terminals {a, b} (AU – June 2014)
Basis: {, a, b}
Induction: {aa, bb, aba, bab, aaa, bbb, aaaa, abba, bbbb, baab, ….}
To generate the basics form of strings, we get
S
S a
S b
Other strings, specified by the induction are given by
S aSa generates aa, aaa, aba, aaaa, abba etc.,
S bSb generates bb, bbb, bab, bbbb, baab, etc,
Thus the grammar, G = (V, T, P, S) is given by G = ({S}, {a,b}, P, {S}) where P is
given by
S | a | b | aSa | bSb
The language of a grammar
The language of a grammar, G = (V, T, P, S) is denoted as L(G).
Every CFG generates its corresponding language.
Every string of a language is generated by applying the production rules, a finite
number of times.
Each string is derived from the start symbol and ends with a final string, consisting of
a set of terminals.
Context Free Grammar and Languages 3.12
w | w T * and Sw G
Formal definition (AU – Nov/Dec 2009, May/June 2013)
The language L(G) of a grammar, G is a set of strings derived from the start symbol.
It is denoted as
*
„*‟ denotes that the grammar, G is applied over S several times.
If the grammar is applied once, it is denoted as S w G
The language generated from a context - free grammar is called a context - free
language.
Example
Design the language for the CFG given. The grammar, G = ({S}, {0,1}, P, {S}) where
P: S 0S1
S 01
Solution:
For the given grammar, we can generate the strings as
S 01 S 0S1 S 0S1 S 0S1 ……
0011 00S11 00S11
000111 000S111
00001111
Thus the strings are {01, 0011, 000111, 04 1
4, 0
5 1
5, …}
The language, L = {0n 1
n | n ≥ 1} generates equal number of zeroes and ones.
PROBLEMS ON CFG
1. Construct a CFG for the language, L = {w C wR | w is a string in (a+b)*}.
(AU – Nov / Dec 2010)
Solution:
The intermediate symbol is „C‟. The string written on the left of „C‟ is reversed at the
right side.
The possible strings generated from L can be {c, aca, bcb, aacaa, bbcbb, abcba, bacab,
abacaba, babcbab, aaacaaa, etc.}
The production rules are defined to be,
L(G)
3.13 Theory of Computation
S c
S aSa
S bSb
Thus the grammar, G = (V, T, P, S) where
V = {S}
T = {a, b, c}
P = {S aSa | bSb | c}
S = {S}
2. Construct a CFG representing the set of palindromes over (0+1)*.
Solution:
The possible productions are {, 0, 1, 00, 11, 000, 111, etc}
So the productions are
P S
S 0
S 1
S 0S0
S 1S1
Thus the CFG is given by G = (V, T, P, S) where
V = {S}
T = {0, 1}
P = { S 0S0 | 1S1 | 0 | 1 | }
S = {S}
3. Construct a CFG for L = {an b
n | n ≥ 0} (AU May/June 2009, Dec 2013)
Solution:
The possible productions are {, ab, aabb, aaabbb, a4 b
4, etc}
So the productions are,
S
S aSb
Thus, the CFG, G = (V, T, P, S) is given by
V = {S}
Context Free Grammar and Languages 3.14
T = {a, b}
P = { S aSb | }
S = {S}
4. Construct a grammar for L = {an b
k | k > n ≥ 0 } with T = {a, b} and V = {S, B}
Solution:
The possible strings generated from L can be {b, bb, bbb, b7, abb, aabbb, abbbbb, …}
The number of b‟s are greater than the number of a‟s, where the number of a‟s can be
zero more.
The production P is given by
S b
S bS
B S
B aBb
where, V = {S, B}, T = {a, b}, S = {S}
5. Let G = {{S, c}, {a,b}, P, S} where S aCa, C aCa|b. Find L(G).
(AU – Nov/Dec 2003, Nov/Dec 2005)
Solution:
1. S aCa aba
2. S aCa aaCaa aabaa
3. S aCaa aaaCaaa aaabaaa
.
.
.
L(G) = {an b a
n | n ≥ 1}
6. Find out the CFL for, (AU – Dec 2009, Dec 2011, June 2009)
S aSb | aAb
A bAa
A ba
Solution:
1. S aSb
3.15 Theory of Computation
aaAbb [using S aAb]
aababb [using A ba]
2. S aSb
aaAbb [using S aAb]
aabAabb [using A bAa]
aabbaabb [using A ba]
3. S aAb
abab [using A ba]
4. S aAb
abAab [using A bAa]
abbaab [using A ba]
From the above strings, the CFL is given as,
L(G) = {an b
m a
m b
n | m, n ≥ 1}
7. Consider = {a, b, c, (,), +, *, … , }. Construct a CFG that generate all strings in *
over {a, b}. (AU – Nov/Dec 2006)
Solution:
The CFG is given as
S S + S
S S * S
S (S)
S a | b |
8. Write a CFG that generates string of balanced parenthesis.
Solution:
Example: ( ), ( ) ( ), ( ) ( ) ( ), etc
The productions for the above set of strings are given by
S SS
S (S)
S
Context Free Grammar and Languages 3.16
A aA | bA |
A bA |
Example: Consider ( ) ( )
S SS [using S SS]
(S) S [using S (S)]
( ) S [using S ]
( ) (S) [using S (S)]
( ) ( ) [using S ]
9. Design a CFG for the regular expression, r = (a + b)* aa (a + b)*
Solution:
The grammar consists of at least two a‟s and may have any number of „a‟ or „b‟ before
and after it.
Example: aa, aaa, aab, baa, aaab, baaaabb, bbbaaa, etc,
The production is given by
P S AaaA
where,
V = {S, A}
T = {a, b}
S = {S}
10. Design a CFG for = (a, b) that generates
(i) All strings with exactly one a.
(ii) All strings with at least one a
(iii) All strings with at least 3 a‟s
Solution:
(i) All strings with exactly one a
Example: a, b, bbb, ba, ab, abbbb, babb, bbbabbb, etc
The productions are given as
P S AaA
3.17 Theory of Computation
A aA | bA |
A aA | bA |
where,
V = {S, A}
T = {a, b}
S = {S}
(ii) All strings with at least one a
Example: a, aaa, aabb, baaa, aabbaa, etc
The productions are given by
P S AaA
where,
V = {S, A}
T = {a, b}
S = {S}
(iii) All strings with atleast 3 a‟s
Example: aaa, baaa, ababa, aabbaa, etc
The production rules are given by
P S AaAaAaA
where,
V = {S, A}
T = {a, b}
S = {S}
11. Construct CFG for G = {an b a
n | n ≥ 1} (AU – May/June 2009)
Solution:
Example: aba, aabaa, aaabaaa, etc
The production rule is given by G = (V, T, P, S) where
P {S aSa | b}
where,
V = {S}
T = {a, b}
S = {S}
Context Free Grammar and Languages 3.18
DERIVATION AND LANGUAGES
Derivations
Derivations are the set of strings that are derived from the start symbol, after applying
the production rules, a finite number of times.
*
S | T* G
Representation of derivations
Derivations are represented in either of the two forms.
They are,
o Sentential form
o Parse tree form
Types of derivations (AU – May/June 2009)
There are two types of derivations, namely
o Leftmost derivations
o Rightmost derivations
Sentential form
Sentential form is the derivation derived from the start symbol, by applying the
rules/productions.
Let the G = (V, T, P, S) be a CFG then
*
S
is in sentential form, where (TUV)*
Example:
Let G = (V, T, P, S) be given by G = ({S, A, B}, {0, 1}, P, {S}). (AU – June 2009)
P: S A1B
A 0A |
B 0B | 1B |
Solution:
Consider the string 00101, which is to be generated by the given grammar using
sentential form.
3.19 Theory of Computation
S A1B [Using S A1B]
0A1B [Using A 0A]
00A1B [Using A 0A]
001B [Using A ]
0010B [Using B 0B]
00101B [Using B 1B]
00101 [Using B ]
Thus the string 00101 ϵ L(G).
Types of sentential form
There are two types of sentential forms namely,
Left sentential forms
Right sentential forms
Left sentential form
When the derivations are generated by expanding the leftmost symbol is called left
sentential form.
*
It is denoted as, S m
Example
Consider the grammar given below
S A1B
A 0A |
B 0B | 1B |
Give the leftmost derivation for the string 1001.
Solution:
SA1B m
1B m
10B m
100B m
[Using S A1B]
[Using A ]
[Using B 0B]
[Using B 0B]
Context Free Grammar and Languages 3.20
1001B m
1001 m
[Using B 1B]
[Using B ]
Thus the string „1001‟ is thus derived using left sentential form.
At each step, the left most variable is expanded.
Right sentential form
The derivation generated by performing substitution on the right most variable is
called right sentential form.
It is denoted as
Example
Consider
S A1B
A 0A|
B 0B|1B|
*
S rm
Generate the right-most derivation for the string „1001‟.
Solution:
SA1B rm
A10B rm
A100B m
A1001B rm
A1001 rm
1001 rm
[Using S A1B]
[Using B 0B]
[Using B 0B]
[Using B 1B]
[Using B ]
[Using A ]
Thus the string „1001‟ is derived from the start symbol using right most derivation.
The right most variable is expanded at each step.
The left most and right most derivations are equivalent to the left and right sentential
forms.
3.21 Theory of Computation
PROBLEMS ON DERIVATIONS
1. Construct a CFG for the language having equal number of a‟s and b‟s over {a, b}.
Derive LMD and RMD for the string. „abab‟.
Solution:
The possible productions are {, ab, aabb, abab, baba, bbaa, baab, abba, aaabbb, etc}
The productions are given as
S
S SS
S aSb
S bSa
where, V = {S}, T = {a, b}, S = {S}
Left most derivations for S = abab
SaSb m
abSab m
abab m
Right most derivations for S = abab
SSS rm
SaSb rm
aSab rm
aSbab
rm
abab rm
[Since S aSb]
[Since S bSa]
[Since S ]
[Since S SS]
[Since S aSb]
[Since S ]
[Since S aSb]
[Since S ]
2. Consider the CFG, G = {{S, I}, T, P, S} where T = {+, *, (,), a, b, 0,1} and P is given as
S I I a I I1
S S+S
S S*S
S (S)
I b
I Ia
I Ib
I I0
Context Free Grammar and Languages 3.22
Derive a * (a + b00).
Solution
Left most derivations
S S * S [Using S S * S] m
I * S [Using S I] m
a * S [Using I a] m
a * (S) [Using S (S)] m
a * (S+S) [Using S (S + S)] m
a * (I + S) [Using S I] m
a * (a + S) [Using I a] m
a * (a + I) [Using S I] m
a * (a + I0) [Using I I0] m
a * (a + I00) [Using I I0] m
a * (a + b00) [Using I b] m
Right most derivations
S S * S [Using S S * S] rm
S * (S) [Using S (S)] rm
S * (S+S) [Using S S+S] rm
S * (S+I) [Using S I] rm
S * (S+I0) [Using I I0] rm
S * (S + I00) [Using I I0] rm
S * (S + b00) [Using I B] rm
S * (a + b00) [Using S a] rm
3.23 Theory of Computation
I * (a + b00) [Using S I] rm
a * (a + b00) [Using I a] rm
3. Let G be the grammar. P = {S aB | bA, A a | aS | bAA, B b | bs | aBB}. For
the string aabbbbaa, find LMD and RMD. (AU – Apr/May 2012, Nov/Dec 2010)
Solution:
Leftmost derivation Rightmost derivation
S aB S aB m
aaBB m
aabB m
aabbS m
aabbbA m
aabbbbAA m
aabbbbaA m
aabbbbaa m
rm
aaBB rm
aaBbS rm
aaBbbA rm
aaBbbbAA rm
aaBbbbAa rm
aaBbbbaa rm
aabbbbaa rm
4. Let the productions of grammar be (AU – Apr/May 2007)
S 0B | 1A
A 0 | 0S | 1AA
B 1 |1S | 0BB
For the string 0110, find RMD.
Solution:
S 0B [Using S 0B] rm
01S [Using B 1S] rm
011A [Using S 1A] rm
0110 [Using A 0] rm
Context Free Grammar and Languages 3.24
PARSE TREES (AU – May 2004)
The representation of a derivation in the form of a tree is called a parse tree/derivation
tree.
The tree representation provides a clear view of the expansions / substitutions done on
the variables.
The tree representation also facilitates easy conversion / translation of the source code
in to executable code, when used in compilers.
Formal definition and construction of parse tree
A set of derivations substituted / applied to generate a string can be represented using a
tree, called parse tree.
Parse tree provides clear understanding of recursive function, grouping of symbols
especially parenthesis balancing etc.
The parse trees can be constructed by using the following three conditions.
1. The root node of the tree being the start symbol and the interior nodes [non – leaf
nodes] is represented by the variable, V of grammar G.
2. The leaf nodes are labeled by either the terminals T or by .
3. If the interior node is labeled V1 where V1 V, having its children, X1, X2, … , Xn
are the production of V1 which are represented from left to right.
Note:
V1 where V1 V and T, then V1 can have any number of children based on its
production rules.
If V1 , then should be the only child of V1.
Example
For the grammar S 0S1|01, generate the derivation of the string „000111‟.
Solution:
The derivation is given by
S 0S1 [Using S 0S1]
S 00S11 [Using S 0S1]
S 000111 [Using S 01]
3.25 Theory of Computation
The parse tree is given by
S
0 S 1
Using S 0S1
Using S 0S1
Yield of a parse tree
0 S 1
0 1
Using S 01
00011
The terminals present at the leaf nodes are concatenated from left to right generate a
string, derived from the grammar.
The generated string is called the yield of a parse tree.
Example
o In the above mentioned parse tree, the terminal symbols in the leaf nodes, taken from left to right generate the string, „000111‟.
o Thus 000111 is the yield of the parse tree.
Types of parse tree
There are two types of parse tree. They are,
o Leftmost derivation tree / left derivation tree
o Rightmost derivation tree / right derivation tree
Leftmost derivation tree
A derivation tree representing A is called a leftmost derivation tree if the
production rule is applied only to the leftmost variable at every step.
Rightmost derivation tree
A derivation / parse tree representing A is said to be a rightmost derivation tree if
the production rule is applied only to the rightmost variable at every step.
Example
For the grammar given below give the parse tree for leftmost and rightmost derivation
of the string „1001‟. (AU – Apr/May 2006)
S A1B
A 0A |
B 0B | 1B |
Context Free Grammar and Languages 3.26
Solution:
Leftmost derivative tree
S
A 1 B
Using S A1B
S
A 1 B Using A
S
A 1 B Using B 0B
0 B
S
A 1 B
0 B Using B 0B
0 B
S
A 1 B
0 B
0 B Using B 1B
1 B
3.27 Theory of Computation
S
A 1 B
0 B
0 B
1 B Using B
Rightmost derivative tree
1001 m
S
A 1 B
S
Using S A1B
A 1 B Using B 0B
0 B
S
A 1 B
0 B Using B 0B
0 B
S
A 1 B
0 B
0 B Using B 1B
1 B
Context Free Grammar and Languages 3.28
S
A 1 B
0 B
0 B
1 B Using B
S
A 1 B Using A
0 B
0 B
1 B
1001 rm
Recursive production / inference (AU – Dec 2007)
A production is said to be recursive if the left side variable occurs on the right hand
side that substitutes the same production for „n‟ number of times.
This may lead to infinite language since the production rule is applied recursively.
Example:
S aS thus would lead to application of the same rule recursively as S aS
aaS aaaS …..
3.29 Theory of Computation
PROBLEMS ON PARSE TREES
1. For the grammar, S 0S1 | 01, give the derivation of 000111.
Solution:
Derivation in sentential form
S 0S1 [Using S 0S1]
00S11 [Using S 0S1]
000111 [Using S 01]
Parse tree
S
0 S 1
0 1
000111
2. Find whether S = aabbb is in L = L(G) where G is given by S XY; X YY | a; Y
XY | b.
Solution:
Sentential form
S XY [Using S XY]
aY [Using X a]
aXY [Using Y XY]
aaY [Using X a]
aaXY [Using Y XY]
aaYYY [Using X YY]
aabYY [Using Y b]
aabbY [Using Y b]
aabbb [Using Y b]
0 S 1
Context Free Grammar and Languages 3.30
Parse tree
S
X Y
a X Y
a X Y
Y Y b
b b
aabbb
3. For the below given grammar, generate the derivation of (a+b)*a+b.
E E + T | T
T T * F | F
F (E) | a | b
Solution:
Sentential form
E E + T [Using E E+T]
T + T [Using E T]
T * F + T [Using T T * F]
F * F + T [Using T F]
(E) * F + T [Using F (E)]
(E + T) * F + T [Using E E+T]
(T + T) * F + T [Using E T]
(F + T) * F + T [Using T F]
(a + T) * F + T [Using F a]
(a + F) * F + T [Using T F]
(a + b) * F + T [Using F b]
(a + b) * a + T [Using F a]
(a + b) * a + F [Using T F]
3.31 Theory of Computation
S b A a
( E )
(a + b) * a + b [Using F b]
E
E + T
T F
b
F a
E + T
T F
F b
a (a + b) * a + b
4. Construct a parse tree for S aabbaa, given the productions: S aAS|a; A
SbA|SS|ba. (AU – Dec 2003, Dec 2009)
Solution:
S
a A S
[Using S aAS]
[Using A SbA and S a]
[Using S a and A ba]
a b a
5. Find L(G) where G = ({S}, {0, 1}, {S 0S1, S }, S). (AU – Apr/May 2004)
Solution:
1. S 0S1
01 [Since S ]
T * F
Context Free Grammar and Languages 3.32
S
0 S 1
2. S 0S1
01
00S11 [Since S 0S1]
0011 [Since S ]
S
0 S 1
0 S 1
0011
3. S 0S1 [Since S 0S1]
00S11 [Using S 0S1]
000S111 [Using S 0S1]
000111 [Using S ]
S
0 S 1
0 S 1
0 S 1
L(G) = {0n 1
n | n ≥ 0}
000111
3.33 Theory of Computation
6. Let G be given as, P = {S 0B|1A, A 0|0S|1AA, B 1|1S|0BB}. Find the
rightmost derivation for „0110‟. (AU – Apr/May 2007)
Solution:
S 0B [Using S 0B] rm
01S [Using B 1S] rm
011A [Using S 1A] rm
0110 [Using A 0] rm
S
0 B
1 S
1 A
0
7. Find the leftmost derivation for S = aaabbabbba.
Given the productions (AU – June 2007, May 2008, June 2013)
S aB|bA
A aS|bAA|a
B bS|aBB|b
Solution:
Leftmost derivation
S aB [Using S aB] m
aaBB [Using B aBB] m
aaaBBB [Using B aBB] m
aaabBB [Using B b] m
aaabbB [Using B b] m
Context Free Grammar and Languages 3.34
aaabbaBB [Using B aBB] m
aaabbabB [Using B b] m
aaabbabbs [Using B bS] m
aaabbabbbA [Using S ba] m
aaabbabbba [Using A a] m
S
a B
b b b b S
b A
aaabbabbba a
8. Let G be the grammar, S 0B|1A, A 0|0S|1AA, B 1|1S|0BB. For S=00110101,
find the leftmost derivation. (AU – May 2004)
Solution:
S 0B [Using S 0B] m
00BB [Using B 0BB] m
001B [Using B 1] m
0011S [Using B 1S] m
00110B [Using S 0B]
m
001101S [Using B 1S] m
0011010B [Using S 0B]
m
00110101 [Using B 1] m
a B B
a B B a B B
3.35 Theory of Computation
S
0 B
0 B B
1 1 S
0 B
1 S
0 B 1
9. Find the derivation tree of a*b + a*b where G is given by S S + S | S * S | a | b.
(AU – June 2007)
Solution:
S S + S [Using S S + S]
S * S + S [Using S S * S]
a * S + S [Using S a]
a * b + S [Using S b]
a * b + S * S [Using S S * S]
a * b + a * S [Using S a]
a * b + a * b [Using S b]
S
S + S
S
a
* S S * S
b a b
a * b + a * b
Context Free Grammar and Languages 3.36
abbb
a
b
10. Construct a CFG for (AU – June 2009)
(a) L(G) = {am
bn | m ≠ n; m, n > 0}
(b) L(G) {an b a
n | n ≥ 1}
Solution:
(a) The string generated by the grammar are,
S {aab, abb, aabbb, aaab, abbbb, etc}
The production rules are given as,
P S aSb | aXb | aYb X aX | a
Y bY | b
Example
S
a Y b
b Y
S
a S b
a X b
S
a X b
a
aaabb aab
Where, V = {S, X, Y}, T = {a, b}, S = {S}
(b) The string generated by the grammar are,
S {aba, aabaa, aaabaaa, etc.,}
The production rules are given as,
P S aSa
S aba
Where V = {S}, T = {a, b}, S = {S}
3.37 Theory of Computation
S
a b a
aba
S
a S a
a b a
S
a S a
a S a
aabaa a b a
aaabaaa
AMBIGUITY
Ambiguous grammars (AU – June 2009, June 2013, Dec 2012, June 2014)
A grammar, G is said to be ambiguous if there exists two different parse trees for at
least one string „‟ where T*, each parse tree with the same symbol.
If each string has at most one parse tree in the grammar, then it is unambiguous
grammar.
Example: Ambiguous grammar
Consider the grammar given below, prove that it is ambiguous.
E E + E | a | b
Solution:
Consider a + b + a, then the parse trees are derived as,
E E
a E + E E + E a
b a
a + b + a
a b
a + b + a
Since there are two different parse trees with same start symbol, leads to the same
string, T*, the grammar is ambiguous.
Removing ambiguity from grammar
The only possibility of removing ambiguity from grammar is to introduce one or more
different variables.
E + E E + E
Context Free Grammar and Languages 3.38
E + T
Example
Consider the above mentioned example that contains ambiguity. Remove the ambiguity
from the grammar, E E + E | a | b.
Solution:
Given grammar, E E + E | a | b
The grammar can be re-written by introducing a new variable T as,
E E + T | T
T a | b
Thus the parse tree for a + b + a will be
E
E + T a
T b
a a + b + a
Thus there is no standard algorithm for removing ambiguity, other than rewriting the
grammar by some new variables.
Inherent ambiguity
A context free language, L is internally ambiguous if all its grammars are ambiguous.
Example
Let L = {an b
n c
m d
m | n ≥ 1, m ≥ 1}
= {an b
m c
m d
n | n ≥ 1, m ≥ 1}
Here L consists of
Equal number of a and b and equal number of c and d.
Equal number of a and d and equal number of b and c.
The production can be defined as,
S AB | C
A aAb | ab equal no of a and b
3.39 Theory of Computation
B cBd | cd equal no of c and d
C aCd | aDd equal no of a and d
D bDc | bc equal no of b and c
Consider the string “aabbccdd” | n = m = 2, then there can be two possible leftmost
derivations as,
1) LMD-1
2) LMD-2
S AB [S AB] m
aAbB [A aAb] m
aabbB [A ab] m
aabbcBd [B cBd] m
aabbccdd [B cd] m
S C [S C] m
aCd [C aCd] m
aaDdd [C aDd] m
aabDcdd [D bDc] m
aabbccdd [D bc] m
The parse tree of LMD-1 and LMD-2 are given as
S
A B
a A b c B d
S
C
a C d
a D d
b D c
a b c d b c
1
2
Context Free Grammar and Languages 3.40
PROBLEMS ON AMBIGUITY IN GRAMMARS
1. Consider the grammar,
E E + E | E * E | (E) | I
I a + b
(a) Show that the grammar is ambiguous
(b) Remove the ambiguity.
Solution:
(a) Ambiguity in grammar
E
E + E
I E * E
a I I
E
E * E
E + E I
I I a
b a a b
From and we have generated two different parse trees for the same string, a+b*a
using the same production.
Thus, the grammar is ambiguous.
(b) Removing ambiguity
Introducing new variable T to remove
ambiguity, we get the production rules as
E E + T | T
T T * F | F
F (E) | a | b
The only parse tree for a + b * a using the
modified grammar is given as
E
E + T
T T * F
F F a
a b
a + b * a
1 2
3.41 Theory of Computation
2. Test whether the following grammars are ambiguous. (AU – Dec 2006, Dec 2013)
(a) S 0S1S | 1S0S |
(b) S AA
A aAb | bAa |
Solution:
(a) Consider S = 0101, the parse tree for LMD and RMD are given as,
S
0 S 1 S
0 S 1 S
S
0 S 1 S
1 S 0 S
0101
0101
Since there are two different parse trees for the same string, the grammar is
ambiguous.
(b) Consider S = abab, the parse trees are,
S
S
A A
A A
a A b a A b
a A b
b A a
abab abab
There are two different parse trees for the same string S = abab. Hence the grammar is
ambiguous.
3. Show that id + id * id can be generated by two distinct leftmost derivations in the
grammar, E E + E | E * E | (E) | id. (AU – May 2005, Dec 2005, Dec 2010)
Solution:
Leftmost derivation – 1
E E+E m
id + E m
Context Free Grammar and Languages 3.42
id+E*E m
id+id*E m
id + id * id m
Leftmost derivation – 2
E E * E m
E + E * E m
id + E * E m
id + id * E m
id + id * id m
4. Show that S a | Sa | bSS | SSb | SbS is ambiguous. (AU – Dec 2007, Dec 2008)
Solution:
Consider the string S = aaab.
S
S S b
S a a
S
S S b
a S a
a aaab a aaab
From the above parse trees, we have proved that the grammar is ambiguous.
5. Show that the given grammar is ambiguous (AU – June 2007)
S a | abSb | aAb
A bS | aAAb
Solution:
Consider the string S = abab.
S
S a A b
a b S b
abab
b s
a
abab
a
3.43 Theory of Computation
From the above parse trees, we have proved that the given grammar is ambiguous.
6. If G is the grammar defined by, S SbS|a. Show that G is ambiguous.
(AU – May 2004, Dec 2009, June 2004)
Solution:
Consider the string S = ababa.
S S
S b S S b S
S b S a
a a
a S b S
a a
ababa ababa
From the above two different parse trees, we conclude that the grammar is ambiguous.
7. Is the grammar S SS | (S) | S(S)S | is ambiguous? (AU – Nov/Dec 2011)
Solution:
LMD
S SS [Using S SS] m
(S) S [Using S (S)] m
( ) S [Using S ] m
( ) [Using S ] m
RMD
S S(S) S rm
S(S) [Using S ] rm
S( ) [Using S ] rm
( ) [Using S ] rm
Since the grammar generates two different derivations for the same string, it is
ambiguous.
Context Free Grammar and Languages 3.44
RELATIONSHIP BETWEEN DERIVATION AND DERIVATION TREE
From Trees To Derivations
Theorem (AU – Dec 2003, Dec 2004, May 2005, Dec 2005)
*
Let the grammar, G = (V.T, P, S) be context free. Then S if and only if there is a G
derivation tree in grammar G, that generates the string „‟.
Proof
*
Let S be a variable, where SV, then S if and only if there is a parse tree called G
S-tree, starting from the root node (S) to generate the string, „‟.
The problem can be easily proved by the principle of mathematical induction.
Basis of Induction
Consider the lowest possible input value and prove the theorem given.
So we assume that there is only one interior node [height = 1] which forms the root
node, that yields the string a, by deriving the leaf nodes of S as S a1 a2 a3 … an.
*
The derivation tree for the generation of S is given as
Height of the
tree = 1 a1
S
a2 a3
An
Thus S a1 a2 …. an is the input string generated from S.
Inductive Step
Assume that the theorem is true for „nth
‟ input data and we need to prove the same for
n = n+1 data.
Hence we assume that the derivation tree can be drawn for (n – 1) interior nodes.
And we need to prove that the derivation tree can be drawn for „n‟ interior nodes to
derive „a‟ from S.
Here, we need to analyze that certain nodes shall be leaf nodes, whereas others can be
interior nodes.
Hence if a node Xi T, then I = XI
3.45 Theory of Computation
*
If the node Xi V, then Xi i m
is in G.
The derivation tree is given as, [By inductive hypothesis]
S
a1 a2 a3 an
where,
a1, a3 V
a2, an T
Thus, S a1 a2 …. an can be obtained.
Hence the theorem is proved by mathematical induction.
Example
Let G = (V, T, P, S) be a CFG with productions, S aSb | ab
Let us consider the basis of induction according to it, the lowest possible integer height
of the tree should be considered.
Let the height of the tree = 1
The grammar, S yields only one possible string S ab which is given by
S
a b
= ab [S ab]
According to the inductive step let us assume that we can generate a derivation tree of
height (n – 1) and we need to prove that it is true for the tree of height, n.
Let n – 1 = 2
S aSb m
aaSbb m
aaabbb m
md
Context Free Grammar and Languages 3.46
S
a S b
Terminal a S b
Terminal
Non terminal root of the sub-tree
This is assumed.
a b
LMD
aaabbb
When n = 3 [(n – 1) + 1] the derivation becomes,
S aSb m
aaSbb m
aaaSbbb m
aaaabbbb m
S
S
a S b
a S b
a S b
a b S
aaaabbbb
Thus if the theorem is true for a tree of height (n – 1), then it is true for height n.
From Derivation To Recursive Inferences
Theorem
Let G = (V, T, P, S) be a CFG and if there is a derivation *
A , where is in T*. m
Then the recursive inference procedure applied to G determines that is in the language of
non-terminal „A‟.
3.47 Theory of Computation
Proof
We shall prove the theorem by the principle of mathematical induction.
Basis of induction
Let us consider the lowest possible input of the grammar.
Let A , then there must be a production A in G. then we can infer that is in
the language of variable A.
Inductive step
Assume that the theorem holds for the derivation has (n-1) number of steps and we
need to prove that it holds for „n‟ no. of steps.
Let the derivation be stated as,
A X1 X2 X3 ….. Xk
*
Then can be broken as
= 1 2 … k
where,
*
Xi i
*
The derivation X i can take at most „n-1‟ number of steps to generate.
Now we have a production,
A X1 X2 …. Xk
Infer i to be in the language of Xi
1 2 ….k is inferred as in the language of A.
Leftmost derivation of a tree yields string
Theorem
Let G = (V, T, P, S) be a CFG. Suppose there is a parse tree with root labeled by A | A *
V and with yield where T*. Then there is a leftmost derivation A in G. m
Context Free Grammar and Languages 3.48
A
X1 X2 Xn
Proof
The theorem is proved by mathematical induction on the height of the parse tree.
Basis of induction
Let the height of the tree be 1, the tree looks like,
Height of the
tree = 1
A
1 2
n
r
Here the root is labeled A, AV and the children are the leaf nodes
which are read from the left to right to generate .
Thus A is proved. m
1, 2 , ..... n
Inductive step
We assume the height of the tree as n | n>1, then
We need to prove the same for n = n+1.
There are two issues:
The child of A can be terminal / variable as given
*
A stands. m
1 2
1. If A i T, then X i i [i terminal]
n
*
2. If A i V, then there must be some leftmost derivation, Ai i
For n nodes, i 1 2 3 ....... an
3.49 Theory of Computation
*
AA1 A2 A3 ........An m
(for i 1, 2,3, .... n)
*
1 2 .....ai1 Ai Ai1 Ai2 ....... An m
If Ai is a terminal, then
*
Ai 1 2 ........ ai m
If Ai is a variable, then
Ai1 Ai2 ...... An
and
A1 2 i
m m m m
A 1 2 .....ai1 Ai Ai1 ....... An
1 2 ,.....1
*
Ai Ai1 ...... An 1 2 2 Ai m
Thus,
*
Ai1 ...... An 1 2 , 3 ..... i Ai1 Ai2 ...... An m
*
A 1 2 ,.....i Ai1 Ai2 ........ An m
*
m
Thus the theorem has been proved.
PUSHDOWN AUTOMATA
A Push down automata shortly called as a PDA is a ϵ -NFA with stack, which is
used to define regular languages.
Any grammar can be converted into a PDA and vice-versa. [CFL can be
recognized by PDA].
The stack in the PDA provides memory to store intermediate states and results,
which increases the capability of the automaton.
Context Free Grammar and Languages 3.50
Due to the availability of stack, the PDA becomes more powerful than a finite
automaton.
Notion of a PDA
Accept/
Reject
Input tape
Stack
The input tape contains the input symbols that are to be processed by the PDA.
The input symbols are defined by the alphabet () of the language.
The finite state control reads one input at a time from the input tape.
Based on the symbol on the top of the stack and with the input symbol,
o Transition is made to the current state
(or)
o Transition is made to the next state
(or)
o Performs spontaneous transition using „‟ as its input at some times. (no input is read).
The transition of a PDA thus, depends on
o Current state
o Current input /.
o Top symbol of the stack.
The stack stores a set of stack symbols generated from the input tape using the
operations
o PUSH – Inserts an element into the stack
o POP – Deletes an element out of the stack
o CHECK EMPTY – checks if the stack is empty
o NOP – performs No operation
Finite
state
control
a0 a1 a2 … an …
3.51 Theory of Computation
So, based on the inputs and stack symbols, transitions are made.
The finite state control performs the state changes.
If the final state is reached, the PDA accepts the input symbol.
After recognizing all the inputs, if the final state is not reached, it means that the PDA
rejects the input.
Definition of Pushdown Automata
A PDA can be formally defined by 7-tuples.
The specification of a PDA, P is given by,
P = (Q, , , , q0, z0, F ) where,
Q Finite set of states
Finite set of input symbols [alphabet]
Finite set of stack symbols
Transition function
: Q × ( ) × Q × * defined as, (q, a, x) = (q
1, x
1)
where,
q, q1 states in Q
a input symbol in or
x, x1 stack symbol in
q0 Initial/ start state [q0 Q]
z0 Initial start symbol [z0 ]
f finite set of final/ accepting states [f ≤ Q ]
Informal definition of PDA
A PDA is a non-deterministic FA with stack. The stack is to store the string taken from
the input tape.
The PDA accepts / rejects the input based on the transitions / moves made by the finite
state system
Context Free Grammar and Languages 3.52
MOVES / TRANSITIONS
A transitions is given by,
Q × ( ) × Q × *
Here, Q × ( ) × implies that a transition is based on,
Current state [ q Q ]
Next input [ ]
Stack symbol [ top- most element of the stack ] [ ]
and Q × * implies,
The next state reached after transition [ q1 Q]
q1 current /another state defined in Q
Stack symbol remaining after [ *]
o PUSH
o POP
o NOP
Transitions with stack operations
1) Read input with no-operation on stack
The transition that reads an input from the input tape , but performs no-operations on
the stack is given as ,
(q1, a, b ) = (q2, b )
Current
State
Input
symbol
stack
symbol
Transitted state
current/next state
replaced symbol in stack
[b E ]
[q1 Q] [a ] [b E ] [q2 Q]
Since, there is no-operation performed on stack, the stack symbols are neither added
up / deleted off.
Thus the transition becomes (q2 b) since the move does n‟t modify the stack.
2) Push operation on stack
Consider that the input = „a‟ is pushed on to the stack. Then the transition becomes,
3.53 Theory of Computation
Where,
(q1, a, b) = ( q2, a b) „a‟ is accepted by the stack.
q1, q2 current and next state [q1 , q2 Q]
A input symbol to be processed [a ]
B stack symbol [b ]
Here the top most symbols on stack is „b‟ is replaced by „a‟ followed by „b‟, since the
input „a‟ is pushed / inserted on to the stack.
3) Pop operation on stack
The transition of a pop operation is given as, (q1, a, b) = (q2, ) „a‟ cancels „b‟ from
the stack.
The above transition cancels the top most stack symbol by the input processing.
Example
L = {w (a, b)* | is of the form an b
n, n ≥ 1}
Algorithm
Consider n = 3
w = a3 b
3 = a a a b b b
Step-1: Push „n‟ number of a‟ s into the stack.
Step-2: For every „b‟, pop out an „a‟ from the stack.
Step-3: At the end of the string, the machine stops as it reaches the final state.
Transitions/moves
Initially, the stack is empty. On processing the first „a‟, the PDA pushes it onto the
stack.
(q0, a, z0) = (q0, az0)
Initial input top element „a‟ becomes the top most element in the stack.
state symbol on the stack
On processing subsequent (n-1) number of a‟s, the stack pushes each „a‟ being
processed.
Context Free Grammar and Languages 3.54
q0 b, a |
q1 , z | z
0 0 q2
(q0, a, a) = (q0, a a)
On processing the first „b‟, with the top most element of the stack, being „a‟, the
topmost element in the stack gets popped out of the stack.
This is indicated by a state change from q0 to q1.
(q0, b, a) = (q1, )
For each subsequent (n-1) b‟s, pop every „a‟ out of the stack.
(q1, b, a) = (q1, )
Finally when there are no more input strings, the input becomes „‟ and stack becomes
„z0‟, which is considered as the end of the process, denoted by a state change [q1
q2].
(q1, , z0) = (q2, z0)
Q2 is considered as the final state.
Thus the PDA transition diagram becomes,
a, z0 | az0
a, a | aa
b, a |
where,
The PDA, p = (Q, , , , q0, z0, F) with
Q = {q0, q1, q2}
= {a, b}
= {z0, a, b}
is given as,
(q0, a, z0) = (q0, a z0)
(q0, a, a) = (q0, aa)
(q0, b, a) = (q1, )
(q1, b, a) = (q1, )
(q1, , z0) = (q2,z0)
Q0 = {q0}
Z0 = {z0}
F = {q2}
3.55 Theory of Computation
INSTANTANEOUS DESCRIPTION OF A PDA
Instantaneous description of a PDA is an informal notation or description of a PDA.
It is a pictorial / diagrammatic representation of a string processed by a PDA.
This is used to depict the processing of a string by a PDA.
Example
L = {an b
n | n ≥ 1} [before mentioned example]
Let n = 3, then, L = a3 b
3 = aaabbb
Graphical representation
(instantaneous description)
Tuple format
…
Input
tape
(q0,
Current
state
aaabbb,
z0)
q0
Input string
(yet to be
processed)
Stack
elements
State
control
Stack
q0
State
control
Stack
…
Input
tape
(q0,
Current
state
aabbb,
Remaining
input
az0)
Stack
elements
q0
State control
Stack
…
Input
tape
(q0,
Current
state
abbb,
Remaining
input
aaz0)
Stack
elements
a a a b b b
z0
a a a b b b
a
z0
a a a b b b
a
a
z0
Context Free Grammar and Languages 3.56
q0
State
control
a
a
a
z0
Stack
…
Input
tape
(q0,
Current
state
bbb,
Remaining
input
aaaz0)
Stack
elements
q1
State
control
a
a
z0
Stack
…
Input
tape
(q1,
Current
state
bb,
Remaining
input
aaz0)
Stack
elements
q1
State
control
a
z0
Stack
…
Input
tape
(q1,
Current
state
b,
Remaining
input
az0)
Stack
elements
q1
State control
z0
Stack
…
Input
tape
(q1,
Current
state
,
No input
z0)
No Stack
elements
a a a b b b
a a a b b b
a a a b b b
a a a b b b
3.57 Theory of Computation
*
P
q2
State
control
z0
Stack
…
Input
tape
(q2,
Final
state
,
Empty input
tape
z0)
Initial Stack
elements
THE LANGUAGE OF A PDA
The language of a PDA can be accepted of two ways
Acceptance by final state
Acceptance of empty stack
The two methods are equivalent that the language that is accepted by a PDA by final
state if and only if the language is accepted by empty stack.
However the PDA, P generated for a language is different that is accepted by final
state is different with that of the PDA generated by empty stack.
Acceptance By Final State
A PDA that accepts its input and enters the final / accepting state is called as a PDA
accepted by final state.
Let the PDA, P = {Q, , , , q0, z0, F}. Then the language of the PDA, P is L(P)
which is accepted by final state is given by,
L(P) = { | (q0, , z0 ) (q, , ) }
where, q0 initial state
input string
z0 initial stack symbol
q Q final state [q F]
stack symbols
a a a b b b
Context Free Grammar and Languages 3.58
Acceptance By Empty Stack
The PDA that accepts its input by emptying the stack is the PDA accepted by empty
stack. Let P be a PDA defined by {Q, , , , q0, z0, F}. The language of the PDA accepted
by empty stack is defined by,
N(P) = { | (q0, , z0 ) *
(q,
, ) }
where,
Final state No input to process Stack empty
q0 initial state
input string
z0 initial stack symbol
q final state
Important Theorems
From Empty Stack To Final State (AU – Nov/Dec 2011)
Theorem
If L = N (PN) for some PDA PN = {Q, , , N, q0, z0, F} then there is a PDA, PF such
that L=L (PF).
Proof
The theorem states that if there is a PDA, which is accepted by empty stack, then there
should be a PDA, which is accepted by final state.
Consider, x0 , that is the start symbol of PF. Since x0 is the start-symbol, it will be
the bottom-most element of the stack (when processed).
Let P0 be a new start state of PN. P0 pushes z0 onto the stack and enters q0 state . [p0
start state of PN]
Let PF be the final state of PF, which stimulates PN until the stack of PN is empty.
PF is stated as,
where,
PF = (Q U {P0, Pf} , U {x0}, F, p0, x0, {Pf})
Q U {P0, Pf} – finite set of states including p0, pf
- set of input symbols
U {x0} – stack symbols including x0
3.59 Theory of Computation
*
PN
PF
, x0 /
Start p0
, x / z x 0 0 0
q0 PN , x /
0 Pf
, x0 /
F – transition function
p0 – start state of the PDA
x0 – initial stack symbol
Pf – final state of the PDA
And F is defined by
1. F(p0, , x0) = (q0, z0 x0)
This shows a spontaneous transition that pushes the symbol z0 on to the stack.
2. For all q Q, a { U } and stack symbols Y , F (q, a, Y) contains all the
pairs in N (q, a, Y).
3. For every state q Q , F(q, , x0) contains (pf, )
To prove
The string is in L(PF) if and only if is in N(PN).
IF CASE
Given (q0, , z0) (q, , ) [q0, Q, q0 initial state ]
(p0, , x0) (q0, , z0 x0) [Using transition rule 1 as given above]
(q, ,x0) [Using rule 2]
(pf, ,) [Using rule 3]
Thus PF accepts by final state.
, x0 /
PF stimulates PN and accepts if PN empties its stack
*
PF
*
PF
Context Free Grammar and Languages 3.60
ONLY – IF CASE
The rule – 1 of the transition rules causes PN to enter the initial computation of PF
except that PF will have its own bottom-of stack marker z0. [which are the symbols of PF
stack]
It is proved that
(q0, , z0)
(q, , ) [Rule 3]
Thus is in N(PN).
Example
Design a PDA that process equal number of 0‟s and 1‟s.
Algorithm
Step 1: Push the initial set of 0‟s / 1‟s onto the stack, indicating 0 as A and 1 as B.
Step 2: Pop out equivalent set of 0‟s / 1‟s for every incoming 1/0 present in the input
tape.
PDA by empty stack PDA by final state
1) N (q, 0, z0) = (q, Az0)
q
or
q
0) F (p, , x0) = (q, z0 x0)
Simulates PN and pushes z0 into the
stack with x-as bottom of stack
marker.
1) F (q, 0, z0) = (q, Az0)
Pushes „A‟ onto the stack as „0‟ is
processed as the first input.
2) N (q, 1, z0) = (q, Bz0) 2) F (q, 1, z0) = (q, Bz0)
*
PN
0 0 1 1
A
z0
0 1 0 1
A
z0
3.61 Theory of Computation
B
z0
q
1 1 0 0
3) N (q, 0, A) = (q, AA)
4) N (q, 0, B) = (q, )
3) F (q, 0, A) = (q, AA)
0 0 1 1
4) F (q, 0, B) = (q, )
B
As „0‟ is processed
5) N (q, 1, A) = (q, )
0 1 0 1
5) F (q, 1, A) = (q, )
A
As „1‟ is processed
6) N (q, 1, B) = (q, BB)
7) N (q, , z0) = (q, )
7) F (q, 1, B) = (q, BB)
1 1 0 0
8) F (q, , z0) = (q, )
B
B
z0
q
A
A
z0
q
z0
-1
z0
-1
q
q
1 0 1 0
Context Free Grammar and Languages 3.62
Final state
9) F (q, , x0) = (q, )
Empty stack
x0
z0
q
q
0 0 1 1
0 0 1 1
From Final State To Empty Stack
Theorem (AU – Nov/Dec 2011)
Let L be L(PF) for some PDA PF = (Q, , ,
such that L = N(PN).
F , q0, z0, F). Then there is a PDA, PN
Proof:
Let PF simulates PN as,
PN = (Q U {P0, P}, , U {x0}, N, p0, x0, F)
Where N can be defined as
1) N (p0, , x0) = (q0, z0 x0)
The start symbol of PF is pushed onto the stack.
2) For all states q Q, input symbols a { U } and stack symbols Y ,
N (q, a, y) = F (q, a, y) for all pairs of transitions.
Every transition in PN should also be in PF
3) N (q, , y) = (p, ), for all final states qF, stack symbols y{ U x0}
PN starts to empty the stack contents whenever PF accepts.
4) N (p, , y) = (p, ), for all stack symbols y{ U x0}
PN pops every stack symbols, when PF has accepted at some state, P.
3.63 Theory of Computation
PN stimulates PF empties its stack, when PN enters final state.
Start
, x0 / z0x0
0 q0 PF
, any /
P
, any /
, any /
To prove
For a string, is in N(PN) if and only if is in L(PF).
If-CASE
Where
(q, , z0)
*
(q, |) [Assume]
PF
q final state
stack symbols
Invoking x0 be the bottom of stack symbols,
(q0, , z0 x0)
(p0, , x0)
(q, , x0)
(q0, , z0 x0) [Rule 1]
(q, , a x0 ) [Rule 3]
(p, , ) [Rule 4]
Thus is accepted by PDA by empty stack, PN.
ONLY-IF CASE
The only way for emptying the stack of PN is by entering into the state, P which is
stimulated by PF (entering final state).
*
PN
PN
*
PN
*
PN
p
Context Free Grammar and Languages 3.64
Thus PN becomes,
(p0, , x0)
(q0, , z0 x0) [Rule 1]
(q, , x0 ) [Rule 3]
(p, , )
Where, q Final state of PF
PF.
Between (q0, , z0 x0)
Thus is in L(PF)
(q, , x0), all the transitions are the transition moves of
DETERMINISTIC PUSHDOWN AUTOMATA (AU – Nov/Dec 2010)
PDA is classified into two types based on the transitions.
1. Deterministic PDA [DPDA]
2. Non-deterministic PDA [NPDA]
Deterministic PDA
A PDA that has only one transition for an input at any particular state is called as
DPDA.
It does not have any choices of transition in any state for the inputs.
(q, a, x) is always singleton for the input symbol, a in and state, q in Q.
Formal definition
DPDA, P is given by
where,
P = (Q, , , , q0, z0, F)
Q Finite set of states
Set of input symbols
Finite set of stack symbols
PN
*
PN
*
PN
*
PN
3.65 Theory of Computation
Example
Transition functions given by
(q, a, x) has at most one transition for any q in Q, a in or , X in
.
If (q, a, x) is non empty for a in , then (q, , x) must be empty.
q0 Start state
z0 Initial stack symbol
F Final state
DPDA for L = {0n1
n | n > 1}
0, 0 | 00
0, z0 | 0 z0
1, 0 |
Note
1) If L is a regular language, then L = L(P) for some DPDA, P.
2) A language L is N(P) for some DPDA, P if and only if L has the prefix property and L
is L(P1) for some DPDA, P
1.
3) If L = N(P) for some DPDA, P then L has an unambiguous context-free grammar.
4) If L = L(P) for some DPDA, P then L has an unambiguous CFG.
5) The two modes of accepting a deterministic PDA is
a. Empty stack
b. Final state
Problems
1. Let L = {am
bn | m > n}. Construct a PDA
a) Accepted by empty stack
b) Accepted by final state
Solution:
Algorithm
1. Push the „m‟ number of a‟s onto the stack.
q0 1, 0 |
q1 , z | z
0 0 q2
Context Free Grammar and Languages 3.66
2. Pop out every „a‟ for each „b‟
3. Pop out the extra (m-n) number for a‟s from the stack
4. Pop out z0 from the stack to make the stack empty.
Instantaneous description
Let m = 2 and n = 1 (2 > 1)
= a2 b
1 = aa b
Input
a a b
a
z0
Stack
State q0
q0 q0 q0
q1 q1
Transition functions
Acceptance by empty stack Acceptance by final state
1. (q0, a, z0) = (q0, a z0)
2. (q0, a, a) = (q0, a a)
3. (q0, b, a) = (q1, )
4. (q1, b, a) = (q1, )
5. (q1, , a) = (q1, )
6. (q1, , z0) = (q1, )
1. (q0, a, z0) = (q0, a z0)
2. (q0, a, a) = (q0, a a)
3. (q0, b, a) = (q1, )
4. (q1, b, a) = (q1, )
5. (q1, , a) = (q2, )
PDA accepted by empty stack,
PN = (Q, , , N , q0, z0)
where,
Q {q0, q1}
{a, b}
{z0, a, b}
N As shown in the transition diagram
q0 {q0}
z0 {z0}
z0 z0
a
a
z0
a
z0
a
z0
z0
3.67 Theory of Computation
a, z0 | az0
a, a | a a
b, a |
, a |
, z0 |
PDA accepted by final state
PF = (Q, , , F , q0, z0, F)
where,
Q {q0, q1, q2}
{a, b}
{z0, a, b}
N As shown in transition diagram
q0 {q0}
z0 {z0}
F {q2}
a, z0 | az0
a, a | a a
b,a |
2. Design a PDA that accepts a set of strings over {a, b} with equal number of a‟s and
b‟s. (AU – June 2007)
Solution:
Step 1: Stack contains z0 at top. When input = a, it is pushed into stack.
(q0, a, z0) = (q0, a z0)
If a = 2; b = 2
a Eg: ab
q0 q0
Step 2: Stack contains z0 at top. When input = b, it is pushed into stack.
a
z0
z0
q0 b,a |
q1 , a |
q2
q0 b,a |
q1
Context Free Grammar and Languages 3.68
(q0, b, z0) = (q0, b z0)
b
Eg: ba
q0 q0
Step 3: Stack contains top = a and if input = a, then excess a is increased by 1.
(q0, a, a) = (q0, aa)
a
q0 q0
Step 4: Stack contains top = a and if input = b, then excess a is decreased by 1.
(q0, b, a) = (q0, )
b
q0 q0
Step 5: Stack with top = b, input = a, excess b is decreased by 1.
(q0, a, b) = (q0, )
a
q0 q0
Step 6: Stack contains top = b, input = b, excess b is increased by 1.
(q0, b, b) = (q0, bb)
z0
z0
b
z0
z0
a
a
a
z0
a
z0
b
z0
z0
3.69 Theory of Computation
b
Transition function
q0 q0
PDA accepting through an empty stack PDA accepting through final state
(q0, a, z0) = (q0, a z0) (q0, a, z0) = (q0, a z0)
(q0, b, z0) = (q0, b z0) (q0, b, z0) = (q0, b z0)
(q0, a, b) = (q0, ) (q0, a, b) = (q0, )
(q0, b, a) = (q0, ) (q0, b, a) = (q0, )
(q0, a, a) = (q0, aa) (q0, a, a) = (q0, aa)
(q0, b, b) = (q0, bb) (q0, b, b) = (q0, bb)
(q0, , z0) = (q0, ) (q0, , z0) = (q1, z0)
Transition diagram
a, a | aa
a, b | bb
q0
, z0 |
b, z0 | b z0
a, z0 | a z0
a, b |
b, a |
a, a | aa
b, b | bb
b, z0 | b z0
a, z0 | a z0
a, b |
b, a |
q0
, z |z 0
0
q1
PDA:
M = ({q0}, {a, b}, {a, b, z0}, , q0, z0, )
PDA:
M = ({q0, q1}, {a, b}, {a, b, z0}, ,
q0, z0, { q1})
3. Give the transition table for PDA recognizing the language, L = {an x | n ≥ 0 & x
{a,b}* & |x| ≤ n.
Solution:
Algorithm:
Step 1: Push the first „a‟ into the stack
(q0, a, z0) = (q0, a z0)
b
b
z0
b
z0
Context Free Grammar and Languages 3.70
z0
a
z0
Let w = a3 bb a
aaabba
a
q0 q0
Step 2: Push (n-1) a‟s on to the stack
(q0, a, a) = (q0, a a)
aaabba aaabba
a
q0 q0 q0
Step 3: When input = x [xb], pop out the „a‟ from the stack.
(q0, b, a) = (q1, )
x
aaabba | aaabba
b a
b a
q0 q1 q1
Step 4: On subsequent a‟s or b‟ in x, and „a‟ should be popped out.
(q1, a, a) = (q1, )
(q1, b, a) = (q1, )
a
z0
a
a
z0
a
a
a
a
z0
a
a
z0
a
z0
a
a
a
z0
3.71 Theory of Computation
a
a
z0
z0
aaabba
a
q1 q1
Step 5: After the end of input string, contents of the stack are popped out one by one.
(q1, , a) = (q1, )
(q1, , z0) = (q1, )
aaabba
z0
Thus the PDA is given by
q1 q1
M = ({q0, q1}, {a,b}, { z0, a}, , q0, z0, )
Where is given as
(q0, a, z0) = (q0, a z0)
(q0, a, a) = (q0, a a)
Accepting a
n
(q0, , z0) = (q0, ) for null string
(q0, b, a) = (q1, ) pop out a for every b
(q1, a, a) = (q1, )
(q1, b, a) = (q1, )
(q1, , a) = (q1, ) Emptying the stack
(q1, , z0) = (q1, )
z0
Context Free Grammar and Languages 3.72
a, z0 | a z0
a, a | aa
, z0 |
q0
b, a |
a, a |
b, a |
q1
, a |
, z0 |
4. Let L = {an b
n c
m d
m | n, m ≥ 1}. Find a PDA for L.
Solution:
Algorithm:
Step 1: Push „a‟ onto the stack
(q0, a, z0) = (q0, a z0)
(q0, a, a) = (q0, a a)
Let n = 2 and m = 1
W = aabbcd
a a
q0 q0 q0
Step 2: For every „b‟ as input, „a‟ should be popped out.
(q0, b, a) = (q1, )
(q1, b, a) = (q1, )
W = aabbcd w = aabbcd
b a
b a
q0 q1 q1
Step 3: Push „c‟ onto the stack
(q1, c, z0) = (q0, c z0)
(q2, c, c) = (q0, c c)
z0
a
z0
a
z0
z0
a
a
z0
a
a
z0
3.73 Theory of Computation
W = aabbcd
c
q1 q2
Step 4: For every „d‟ as input „c‟ should be popped out
(q2, d, c) = (q3, )
(q3, d, c) = (q3, )
W = aabbcd
d c
Step 5: String is accepted as,
q2 q3
(q3, , z0) = (q3, )
W = aabbcd
z0
The PDA is given as
q3 q3
M = ({q0, q1, q2, q3}, {a, b}, {z0, a, c}, , q0, z0, )
with as given in the above algorithm.
5. Let L = {ai b
j c
k | i, j, k ≥ 0 & i + j = k}. Provide the transition function.
(i) Accepted by final state
(ii) Accepted by empty stack
z0
z0
c
z0
c
z0
z0
Context Free Grammar and Languages 3.74
x
x
z0
Solution:
Algorithm:
Step 1: For every input symbol, „a‟, symbol „x‟ is pushed onto the stack.
(q0, a, z0) = (q0, x z0)
(q0, a, x) = (q0, x x)
i = 2, j = 2 k = 2 + 2 = 4
W = aabbcccc
a a
q0 q0 q0
Step 2: For every input symbol, „b‟ symbol „x‟ is pushed onto the stack.
(q0, b, z0) = (q1, x z0)
(q0, b, c) = (q1, x x)
(q1, b, x) = (q, x x)
W = aabbcccc w = aabbccdd
b
b
Step 3: For every input symbol, „c‟, „x‟ is popped out from the stack.
(q1, c, x) = (q2, )
(q2, c, x) = (q2, )
x
z0
z0
x
x
z0
x
x
x
x
z0
x
x
x
z0
3.75 Theory of Computation
W = aabbcccc w = aabbcccc
c x
c x
c x c x
q1 q2 q2 q2 q2
Step 4: (q2, , z0) = (q3, z0)
(q0, , z0) = (q3, z0) Null string
W = aabbcccc
z0
q2 q3
PDA: Through final state PDA: Through empty stack
M = ({q0, q1, q2, q3}, {a, b, c}, {x, z0,},
q0, z0, , {q3})
where is given as
(q0, a, z0) = (q0, x z0)
(q0, a, x) = (q0, x x)
(q0, b, z0) = (q1, x z0)
(q0, b, x) = (q1, x x)
(q1, b, x) = (q1, x x)
(q1, c, x) = (q2, )
(q2, c, x) = (q2, )
(q2, , z0) = (q3, z0)
(q0, , z0) = (q3, z0)
M = ({q0, q1, q2}, {a, b, c}, {x, z0,}, q0, z0,
, )
where is given as
(q0, a, z0) = (q0, x z0)
(q0, a, x) = (q0, x x)
(q0, b, z0) = (q1, x z0)
(q0, b, x) = (q1, x x)
(q1, b, x) = (q1, x x)
(q1, c, x) = (q2, )
(q2, c, x) = (q2, )
(q2, , z0) = (q2, )
(q0, , z0) = (q0, )
z0
z0
x
x
x
x
z0
x
x
x
z0
x
x
z0
x
z0
Context Free Grammar and Languages 3.76
c
6. Design a PDA for the language {am
bn c
m | m, n ≥ 1} using empty stack.
(AU – Dec 2008)
Algorithm:
1) Push the sequence of a‟s onto the stack.
2) Skip the sequence of b‟s by doing no-operation
3) For every „c‟, pop out an „a‟ from the stack.
Transitions:
(q0, a, z0) = (q0, a z0)
(q0, a, a) = (q0, a a)
(q0, b, a) = (q1, a)
(q0, b, a) = (q1, a)
(q1, c, a) = (q2, )
(q2, c, a) = (q2, )
Push „m‟ number of a‟s onto the stack
Skip „n‟ no. of b‟s and do no operation on stack
Pop out every „a‟ for each „c‟ from the stack
(q2, , z0) = (q2, ) Empty the stack
Instantaneous description
Let n = 2, m = 1
Push No-op Pop „a‟
Then w = a a b cc
a b
Input a a
a
z0
Stack
State q0 q0 q0 q1 q2 q2 q2
Therefore, M = ({q0, q1, q2}, {a, b, c}, {z0, a, c}, , {q0}, {z0}, )
7. Construct a PDA for the language accepting by empty stack.
L = {am
bm
cn | m, n ≥ 1} (AU – June 2007)
c a
z0
z0
a
a
z0
a
a
z0
a
z0
a
z0
3.77 Theory of Computation
a b a
z0
Algorithm
1. Push „m‟ number of a‟s onto the stack
2. For every „b‟ being processed, pop out an equivalent „a‟ from the stack.
3. Skip „c‟ and make the stack empty.
Instantaneous Description PUSH POP „a‟ NO-OP
Let m = 3, n = 2 aaa bbb cc
a
a b
Input a
Stack
State q0 q0 q0
q0 q1
q1 q1 q2
c
z0
q2 q2
PDA, M = ({q0, q1, q2}, {a, b, c}, {z0, a, b}, , {q0}, {z0}, }
where is given by
(q0, a, z0) = (q0, a z0)
(q0, a, a) = (q0, a a)
(q0, b, a) = (q1, )
(q1, b, a) = (q1, )
(q1, c, z0) = (q2, )
(q2, c, z0) = (q2, )
(q2, , z0) = (q2, )
Step 1
Step 2
Step 3
z0
b
z0 z0
a
c a
a
a
z0
a
a
z0
a
a
z0
a
z0
a
z0
Context Free Grammar and Languages 3.78
8. Construct a PDA accepting L = {an b
m a
n | m, n ≥ 1} by null store.
Algorithm
1. Push „n‟ number of a‟s onto the stack
2. Skip „m‟ number of b‟s
(AU – Dec 2004)
3. For every „a‟ after „b‟, pop out on „a‟ from the stack and empty the stack elements
Instantaneous description
Instantaneous Description PUSH NO-OP POP „a‟
Let m = 3, n = 2 then w = aa bbb aa
Input
Stack
b
a
a No-op
b
No-op
b
No-op
a a
State q0 q0 q0 q1 q1 q1 q2 q2 q2
PDA M = ({q0, q1, q2}, {a, b}, {z0, a}, . {q0}, {z0}, )
where is given by
(q0, a, z0) = (q0, a z0)
(q0, a, a) = (q0, a a)
(q0, b, a) = (q1, a)
(q1, b, a) = (q1, a)
(q1, a, a) = (q2, )
(q2, a, a) = (q2, )
(q2, , z0) = (q2, )
Step 1
Step 2
Step 3
9. Design a PDA for accepting L = {WCWR | {a, b}}.
(AU – Nov/Dec 2010, Nov/Dec 2013)
Algorithm
1. Push the string containing a‟s and b‟s onto the stack
2. Skip „c‟.
3. Pop out every symbol of the stack by the input string.
a
a
z0
a
z0
a
z0 z0
a
a
a
z0
a
a
z0
a
a
z0
a
z0
3.79 Theory of Computation
Instantaneous description
Let w = baa; L = baacaab
a
c
a No-op
Input b
b b
Stack
State q0
q0 q0
q0 q1
q2 q2 q2 q2
PDA:
M = ({q0, q1, q2}, {a, b, c}, {z0, a, b}, , {q0}, {z0}, )
is given by
(q0, a, z0) = (q0, a z0)
(q0, b, z0) = (q0, b z0)
(q0, a, a) = (q0, aa)
(q0, a, b) = (q0, ab)
(q0, b, a) = (q0, ba)
(q0, b, b) = (q0, bb)
(q0, c, a) = (q1, a)
(q0, c, b) = (q1, b)
(q1, a, a) = (q2, )
(q1, b, b) = (q2, )
(q2, a, a) = (q2, )
(q2, b, b) = (q2, )
(q2, , z0) = (q2, )
Step 1
Step 2
Step 3
a a
a
z0 z0
a a
a
a
z0
a
a
b
z0
a
b
z0
a
b
z0
b
z0
b
z0
Context Free Grammar and Languages 3.80
10. Construct a PDA for the language L = {an b
2n | n ≥ 0}
(AU – May 2007, Dec 2007, Dec 2008)
Algorithm
1. Push „n‟ number of „a‟ on to the stack.
2. For every odd no of „b‟ make a state change indicating that one „b‟ is processed.
3. For every even no. of „b‟, pop out an „a‟ from the stack, indicating that a pair of „b‟
is processed.
Instantaneous description
Let n = 2 L = a2 b
4 = aabbbb
Input
Stack
b
a State
a change
b
State
change b a
z0
State q0 q0 q0 q0 q1 q2 q1 q2
PDA: M = ({q0, q1, q2}, {a, b}, {z0, a}, , {q0}, {z0}, )
is given by
(q0, a, z0) = (q0, a z0)
(q0, a, a) = (q0, aa)
(q0, b, a) = (q1, a)
(q1, b, a) = (q2, )
(q2, b, a) = (q2, )
(q2, , z0) = (q2, )
Step 1
Step 2
Step 3
EQUIVALENCE OF PDA AND CFG
(AU – May/June 2013, Nov/Dec 2010, Nov/Dec 2013)
For a given CFG, G = (V, T, P, S), we can construct a PDA, M that simulates the
leftmost derivation of G.
a
a
z0
a
a
z0
a
z0
b a
z0 z0
a
z0
a
z0
3.81 Theory of Computation
CFG
PDA by
empty
stack
PDA by
final
state
The PDA accepting L(G) by empty stack is given by
M = ({q}, T, VUT, , q, S, )
where is defined by
1. For each variable A V, include a transition
(q, , A) {(q, ) | A is a production in G}
2. For each terminal, a T, include a transition
(q, a, a) {(q, )}
From Grammar To PDA
Theorem
If PDA, P is constructed from CFG, G then N(P) = L(G)
(Or)
If L is generated by a CFG, then there exists a PDA accepting L.
Proof
To prove
is in N(P) if and only is in L(G).
IF – CASE
Let is in L(G).
The left most derivation of is given as,
S 1 2 n m m m
Let us use mathematical induction principle on i to show that
(q, ,S) *
(q, y , )
where
P i i
i = xi i [i is the tail of i]
Context Free Grammar and Languages 3.82
1 1
and,
yi, i Left sentential forms of i
xi i = [string]
Basis of Induction:
Let us assume the length i=1
Then,
S = i
Since i = xi i
1 = x1 1 [x1 = ]
1 = 1
And xi yi =
x1 y1 =
y1 = [since x1 = ]
(q, ,S) *
(q, y , ) becomes,
P i i
Thus,
(q, ,S) *
(q, y , )
* (q, , S)
[since S = 1 = 1 ; 1 = S and y1 = ]
(q, ,S) *
(q, , S) on x1 = moves.
Basis of induction is proved.
Inductive step
Assume the (q, ,S) *
(q, y , ) is true for „i' and prove that (q, ,S) *
(q, y , )
is true for „i+1‟.
P i i P
i1 i1
Consider a variable A that the derivation i = i+1 replaces the variable A by a
production, .
This is done by Rule – 1 on the construction of P that replaces A by [at the top of the
stack].
Rule 1:
(q, , A) {(q, ) | A is in P}
Rule 2:
For aT
(q, a, a) (q, )
3.83 Theory of Computation
By applying Rule – 2, we shall match any terminal on the top of the stack by next
input symbol.
Thus, ID (q, yi+1, i+1) gives the value for i+1 .
Here
n = [since tail of n = empty]
n = xn =
Thus (q, ,S) *
(q, , ) since yn = and n = .
Therefore, the inductive step is proved that PDA, P accepts by empty stack.
ONLY-IF CASE
Let A be a variable, that is to be popped out of a stack while executing a PDA, P then,
A derives a the input string, x in G if,
(q, x, A)
*
A x G
* (q, , ) , then
P
This is proved by mathematical induction principle on the number of moves taken by
P.
Basis of induction
When grammar has the production A , then the PDA becomes,
X = [by rule 1] [ (q, , A) {(q, ) | A is in P}]
Since A
Thus basis of induction is proved.
Inductive step
Assume that P takes „n‟ moves, where n>1. The first move must be of rule – 1 (as
given above).
Here A is replaced by another production.
Suppose
A Y1, Y2 …. Yk | Yi {TUV}
Context Free Grammar and Languages 3.84
i1 k
The next (n – 1) moves of P, takes on „x‟ from the input tape, that pops out.
x is broken into x1, x2, … xk, where x1 is the input processed until y1 is popped out of
the stack.
Let x2 is the next input portion that is processed, popping out y2 off the stack, and so
on.
B
a
C
x1 x2 x3
PDA consuming x and popping BaC out off stack
Here, x is divided into 3 parts x1, x2, x3 with x2 = a.
In general, if yi is a terminal, then xi must be that terminal.
Thus,
(q, xi xi1 ....... xk , yi ) *
(q, x ....x ,) for x = 1, 2, … k
None, of these sequences can exceed (n-1) moves, so, *
yi xi
*
Here each symbol of xi matches against Yi and thus, Yi xi
Therefore, the derivation becomes,
A Y1 Y2 Y3 ….Yk
*
x1 Y2 Y3 ............. Yk
.
.
.
(i.e)
*
x1 x2
*
A x
x3 .......xk
3.85 Theory of Computation
Let A = S and x = , N(P) is given as,
(q, ,S) *
(q, , ) *
[since S ]
is in L(G)
Hence the theorem is proved by induction principle.
PROBLEMS
1. Find a PDA for the given grammar S 0S1 | 00 | 11
Solution:
M = ({q}, T, VUT, , q, S, )
= ({q}, {0, 1}, {0, 1, S}, , q, S, ) where is
(q, , S) = {(q, 0S1), (q, 00), (q, 11)}
(q, 0, 0) = {(q, )}
(q, 1, 1) = {(q, )}
2. Convert the grammar to PDA that accepts the same language by empty stack.
S 0S1 | A
A 1A0 | S |
Solution:
a) For each variable A V, include a transition
(q, , A) {(q, ) | A is a production in G}
(q, , S) {(q, 0S1), (q, A)}
(q, , A) {(q, 1A0), (q, S), (q, )}
b) For each terminal a T, include a transition
(q, a, a) {(q, )}
(q, 0, 0) {(q, )}
(q, 1, 1) {(q, )}
Therefore the PDA is given by
M = ({q}, {0, 1}, {S, A, 0, 1}, , q, S, )
Context Free Grammar and Languages 3.86
where is
(q, , S) {(q, 0S1), (q, A)}
(q, , A) {(q, 1A0), (q, S), (q, )}
(q, 0, 0) {(q, )}
(q, 1, 1) {(q, )}
3. Let G be the grammar given by
S aABB | aAA
A aBB | a
B bBB | A
Construct PDA for the above grammar
Solution:
The equivalent PDA, M is given by
M = ({q}, {a, b}, {a, b, S, A, B}, , q, S, )
where is given by
(q, , S) {(q, aABB), (q, aAA)}
(q, , A) {(q, aBB), (q, a)}
(q, , B) {(q, bBB), (q, A)}
(q, a, a) {(q, )}
(q, b, b) {(q, )}
4. Let G be the grammar given by
S 0BB
B 0S | 1S | 0
Construct a PDA. Test if 0104 is in the language.
Solution:
The equivalent PDA, M is given by
M = ({q}, {0, 1}, {0, 1, S, B}, , q, S, )
where is
(q, , S) {(q, 0BB)}
3.87 Theory of Computation
(q, , B) {(q, 0S), (q, 1S), (q, 0)}
(q, 0, 0) {(q, )}
(q, 1, 1) {(q, )}
Acceptance of 010
4 by M
(q, 010000, S) (q, 0100000, 0BB)
(q, 10000, BB)
(q, 10000, 1SB)
(q, 0000, SB)
(q, 0000, 0BBB)
(q, 000, BBB)
(q, 000, 0BB)
(q, 00, BB)
(q, 00, 0B)
(q, 0, B)
(q, 0, 0)
(q, )
The string 0104 is accepted by M L.
5. Construct a PDA for the given grammar.
S 0AB
A 1A | 1
B 0B | 1A | 0
Solution:
M = ({q}, {0, 1}, {0, 1, S, A, B}, , q, S, )
where is given by
(q, , S) {(q, 0AB)}
(q, , A) {(q, 1A), (q, 1)}
(q, , B) {(q, 0B), (q, 1A), (q, 0)}
(q, 0, 0) {(q, )}
(q, 1, 1) {(q, )}
Context Free Grammar and Languages 3.88
6. Construct a PDA for the grammar (AU – Dec 2007, Dec 2008)
S aB | bA; A a | aS | bAA; B b | bS | aBB
Solution:
The equivalent PDA, M is given by
M = ({q}, {a, b}, {a, b, S, A, B}, , q, S, )
where is given by
(q, , S) ({q, aB},{q, bA})
(q, , A) {(q, a), (q, aS), {q, bAA})
(q, , B) {(q, b), (q, bS), (q, aBB)}
(q, a, a) {(q, )}
(q, b, b) {(q, )}
7. Construct a PDA for S aAA; A aS | bS | a. (AU – Dec 2008)
Solution:
The equivalent PDA, M is given by
M = ({q}, {a, b}, {a, b, S, A}, , q, S, )
where is given by
(q, , S) {(q, aAA)}
(q, , A) {(q, aS), (q, bS), (q, a)}
(q, a, a) {(q, )}
(q, b, b) {(q, )}
8. Construct a PDA for S aSbb | abb. (AU – June 2009)
Solution:
The equivalent PDA, M is given by
M = ({q}, {a, b}, {a, b, S}, , q, S, )
where is given by
(q, , S) {(q, aSbb), (q, aab)}
(q, a, a) {(q, )}
(q, b, b) {(q, )}
3.89 Theory of Computation
9. Construct a PDA for G = ({S, A}, {a, b}, P, S) with P : S AA | a; A SA | b.
(AU – June 2009)
Solution:
The equivalent PDA, M is given by
M = ({q}, {a, b}, {a, b, S, A}, , q, S, )
where is given by
(q, , S) {(q, AA), (q, a)}
(q, , A) {(q, SA), (q, b)}
(q, a, a) {(q, )}
(q, b, b) {(q, )}
From PDAS To Grammars
Theorem
Let P = {Q, , , , q0, z0, } be a PDA. Then there exists a context free grammar, G
such that L(G) = N(P).
Proof
Let G = (V, T, P, S) where,
V set of variables having
(i) Special start symbol, S
(ii) All symbols of the form [p q], where p and q are in Q, X being the
stack symbol in .
T set of terminals in
P production rules to the form,
(i) S [q0 Z0 P] [for p Q]
(ii) [q, a, X] contain (r, y1, y2 … yk) where
a. a { U }
b. k 0, in which the pair is (r, )
then, the production rules are given by
[q X rk] [r Y1 r1] [r1 Y2 r2] ….. [rk-1 Yk rk]
Context Free Grammar and Languages 3.90
0 1 2 3 k
To prove
*
[q X p]
if and only if (q, , X)
*
(p, , ) .
Both the if-case and only-if-case can be proved by induction principle.
IF-CASE
Suppose (q, , X) *
(p, , )
To prove
*
[q X p] by induction on the number of transitions made by the PDA.
Basis of induction
Let single symbol or
Then (q, , X) contain (p, )
G is constructed as,
G [q X p] is a production
[q X p]
Thus basic of induction if proved
Inductive step
Assume that (q, , X) (p, , ) consumes „n‟ number of steps
The first move shall be,
(q, , X)
*
(r , x, Y Y Y ... Y )
*
(p, , )
Where
= ax [a in or a = ]
The pair (r0 , X Y1 Y2 ... Yk ) must be in (q, a, X)
G can be constructed using,
[q X rk] a [r0 Y1 r1] [r1 Y2 r2] ….. [rk-1 Yk rk]
3.91 Theory of Computation
where 1. rk = p
2. r1, r2, …. rk-1 Q
Y1
Y2
Yk Pk
x1 x2 xk
states.
PDA makes a sequence of moves that pops a symbol out of the stack
In the above diagram, each Y1, Y2, … Yk gets popped off the stack at each P1, P2 … Pk
Let x = 1, 2, …. k where wi input symbol consumed while Yi is popped off
when P = Pi
(r , , Y )
* (r , , )
i1 i i i
This sequence of transitions can take at most „n‟ moves, hence
*
[ri1 Yi ri ] i
Thus the final productions are given by
[q X rk] a [r0 Y1 r1] [r1 Y2 r2] ….. [rk-1 Yk rk]
*
a 1 [r1 Y2 r2] [r2 Y3 r3] ….. [rk-1 Yk rk]
*
a 1 2 [r2 Y3 r3] ….. [rk-1 Yk rk]
where, rk = p
*
a 1 2 ….. k =
Context Free Grammar and Languages 3.92
ONLY-IF-CASE
The proof is done on the induction on the number of steps in the derivations.
Basis of induction
This involves only one step of derivation
[q X p]
There is only one transition of P with X being popped out with the state change from q
to P.
(i.e) (p, ) must be in (q, a, X) [a = ]
Then, (q, , X) (p, , ) [ = and „X‟ popped out]
Thus basis of induction is proved
Inductive step
*
Assume that [q, X p] is derived in „n‟ steps [n > 1]
The first derivations is of the form
[q X rk] a [r0 Y1 r1] [r1 Y2 r2] ….. [rk-1 Yk rk]
*
Where rk = p
Let be broken into = a 1 2 ….. k such that,
*
[ri1 Yi ri ] i [for i = 1, 2, …., rk]
Using inductive hypothesis on i,
(r , , Y ) *
(r , , ) i1 i i i
The moves beyond i
(r , , .... , Y , Y ...Y ) *
(r , .... , Y ... Y ) i1 i1 k i i1 k i i1 k i1 k
The final sequence becomes
(q, aw1 w2 …wk) (r0, 1 2 … k, Y1, Y2 … Yk)
* (r1, 2 3 … k, Y2, Y3 … Yk)
3.93 Theory of Computation
[q q]
* (r2, 3 4 … k, Y3, Y4 … Yk)
* (rk, , )
Since, rk = p
(q, , X) *
(p, , )
Thus the theorem is proved by mathematical induction as,
* *
PROBLEMS
S if and only [q0 Z0 p]
1. Convert PDA to CFG (AU – May 2004, 2007, 2008, Dec 2008, 2009, 2012)
M = ({p, q}, {0,1}, {x, z}, , q, z)
(q, 1, z) = (q, xz)
(q, 1, x) = (q, xx)
(q, , x) = (q, )
(q, 0, x) = (p, x)
(p, 1, x) = (p, )
(p, 0, z) = (q, z)
Solution:
(1) S z
z [q p]
(2) (q, 1, z) = (q, xz)
[q z q] 1[q
x q] [q
z q]
[q z
q] 1[q x
p] [p z
q]
[q z
p] 1[q x
q] [q z
p]
[q z
p] 1[q x
p] [p z
p] p1, p2 Q
p1] C2
p2] [p2 C1
p1] a [aj [qi B
Using (qi, a, B) (qj, C1, C2)
Using S [Initial state Initial
stack
symbol
States]
S [q z
Q] S
Context Free Grammar and Languages 3.94
x
x
z x
z x
z x
z x
(3) (q, 1, x) = (q, xx)
[q x q] 1[q
x q] [q
x q]
x x x [q q] 1[q p] [p q]
x x x [q p] 1[q q] [q p]
x x x [q p] 1[q p] [p p]
(4) (q, , x) = (q, )
[q q]
(5) (q, 0, x) = (p, x)
[q x
q] 0[p x
q]
[q x
p] 0[p x
p]
(6) (p, 1, x) = (p, )
[p p] 1 [Using the rule state for (4)]
(7) (p, 0, z) = (q, z)
[p z
q] 0[q z
q] [Using the rule stated for (5)]
[p z
p] 0[q z
p]
Renaming variables
[q q] A [q q] E
[q p] B [q p] F
[p q] C [p q] G
[p p] D [p p] H
Production S A | B F 1EF | 1FH | 0H
A 1EA | 1FC H 1
B 1EB | 1FD C 0A
p1] C2
p2] [p2 C1
p1] a [aj [qi B
Using (qi, a, B) (qj, C1, C2)
C Q] a [aj Q] [qi
B
Using (qi, a, B) (qj, C)
Using (qi, a, B) (qj, )
[qi B
aj] a
3.95 Theory of Computation
Using S [Initial state Initial
stack
symbol
States]
S [q z
Q]
E 1EE | D 0B
Eliminate the production
No production for G.
2. Give the CFG for the PDA, (AU – Dec 2003, May 2005, Dec 2011)
M = ({q0, q1}, {0, 1}, {z0, x}, , q0, z0, )
(q0, 1, z0) = (q0, xz0)
(q0, 1, x) = (q0, xx)
(q0, 0, x) = (q1, x)
(q0, , z0) = (q0, )
(q1, 1, x) = (q1, )
(q1, 0, z0) = (q0, z0)
Solution:
(1) S [q0 z0
q0]
S [q0 z0
q1]
(2) (q0, 1, z0) = (q0, xz0)
[q z0
q ] 1 [q x
q ] [q z0
q ] 0 0 0 0 0 0
[q0 z0
q0] 1 [q0 x
q1] [q1 z0
q0]
[q0 z0
q1] 1 [q0 x
q0] [q0 z0
q1]
[q0 z0
q1] 1 [q0 x
q1] [q1 z0
q1]
(3) (q0, 1, x) = (q0, xx)
[q0 x
q0] 1 [q0 x
q0] [q0 x
q0]
[q0 x
q0] 1 [q0 x
q1] [q1 x
q0]
[q0 x
q1] 1 [q0 x
q0] [q0 x
q1]
[q0 x
q1] 1 [q0 x
q1] [q1 x
q1]
p1, p2 Q
p1] C2
p2] [p2 C1
p1] a [aj [qi B
Using (qi, a, B) (qj, C1, C2)
p1, p2 Q
p1] C2
p2] [p2 C1
p1] a [aj [qi B
Using (qi, a, B) (qj, C1, C2)
E 1FG | 0G
Context Free Grammar and Languages 3.96
0G
Using (qi, a, B) (qj, )
[qi B
aj] a
(4) (q0, 0, x) = (q1, x)
[q0 x
q0] 1 [q1 x
q0]
[q0 x
q1] 1 [q1 x
q1]
(5) (q0, , z0) = (q0, )
[q0 z0
q1]
(6) (q1, 1, x) = (q1, ) x
[q1 q1] 1
(7) (q1, 0, z0) = (q0, z0)
[q1 z0
q0] 0 [q0 z0
q0]
[q1 z0
q1] 0 [q0 z0
q1]
Renaming the variable
[q0 z0
q0] A [q0 x
q0] E
[q0 z0
q1] B [q0 x
q1] F
[q1 z0
q0] C [q1 x
q0] G
[q1 z0
q1] D [q1 x
q1] H
Productions
S A | B F 1EF | 1FH | 0H
A 1EA | 1FC H 1
B 1EB | 1FD | C 0A
E 1EE |1FG | D 0B
Since G has no productions, 1FG and 0G are eliminated from E.
Final productions
S A | B F 1EF | 1FH | 0H
A 1EA | 1FC H 1
B 1EB | 1FD | C 0A
E 1EE D 0B
C Q] a [aj Q] [qi
B
Using (qi, a, B) (qj, C)
C Q] a [aj Q] [qi
B
Using (qi, a, B) (qj, C)
3.97 Theory of Computation
3. For the PDA, ({q0, q1}, {0, 1}, {0, 1, z0}, , q0, z0, ) where is
(q0, , z0) = (q1, )
(q0, 0, z0) = (q0, 0 z0)
(q0, 0, 0) = (q0, 00)
(q0, 1, 0) = (q0, 10)
(q0, 1, 1) = (q0,11)
(q0, 0, 1) = (q1, )
(q1, 0, 1) = (q1, )
(q1, 0, 0) = (q1, )
(q1, , z0) = (q1, )
Obtain a CFG accepting the above PDA.
Solution:
(1) Production for the start symbol
Using the rule, S [Start symbol Initial stack symbol
States in Q]
S [q0 z0
q0] ------- (1)
S [q0 z0
q1] ------- (2)
(2) Production for (q0, , z0) = (q1, )
Rule: (qi, a, B) (qj, )
[qi B
qj] a
(q0, , z0) (q1, ) becomes
[q0 z0
q1]
---- (3)
(3) Production for (q0, 0, z0) = (q0, 0 z0)
C1 C2
Rule (qi, a, B) (qj, C1, C2)
[qi B
Q] a [aj C1 Q] [Q
C2 Q]
Q = {q0, q1}
Context Free Grammar and Languages 3.98
(q0, 0, z0) = (q0, 0 z0) becomes
[q0 z0
q0] 0 [q0 0
q0] [q0 z0
q0] ----------- (4)
[q0 z0
q0] 0 [q0 0
q1] [q1 z0
q0] ----------- (5)
[q0 z0
q1] 0 [q0 0
q0] [q0 z0
q1] ----------- (6)
[q0 z0
q1] 0 [q0 0
q1] [q1 z0
q1] ----------- (7)
(4) Production for (q0, 0, 0) = (q0, 0 0)
C1 C2
Using the rule applied for (3), we get
[q0 0
q0] 0 [q0 0
q0] [q0 0
q0]
----- (8)
[q0 0
q0] 0 [q0 0
q1] [q1 0
q0]
----- (9)
[q0 0
q1] 0 [q0 0
q0] [q0 0
q1]
----- (10)
[q0 0
q1] 0 [q0 0
q1] [q1 0
q1]
----- (11)
(5) Productions for (q0, 1, 0) = (q0, 1
C1
0)
C2
Using the rule stated for (3), we get
[q0 0
q0] 1 [q0 1
q0] [q0 0
q0]
----- (12)
[q0 0
q0] 1 [q0 1
q1] [q1 0
q0]
----- (13)
[q0 0
q1] 1 [q0 1
q0] [q0 0
q1]
----- (14)
[q0 0
q1] 1 [q0 1
q1] [q1 0
q1]
----- (15)
(6) Production for (q0, 1, 1) = (q0, 1
C1
1)
C2
Using the rule stated for (3), we get
[q0 1
q0] 1 [q0 1
q0] [q0 1
q0]
----- (16)
[q0 1
q0] 1 [q0 1
q1] [q1 1
q0]
----- (17)
[q0 1
q1] 1 [q0 1
q0] [q0 1
q1]
----- (18)
[q0 1
q1] 1 [q0 1
q1] [q1 1
q1]
----- (19)
3.99 Theory of Computation
(7) Production for (q0, 0, 1) = (q1, )
Using the rule stated in (2), we get
[q0 1
q0] 0----------------------------------- (20)
(8) Production for (q1, 0, 1) = (q1, )
Using the rule specified in (2)
[q1 1
q1] 0----------------------------------- (21)
(9) Production for (q1, 0, 0) = (q1, )
Using the rule specified in (2)
[q1 0
q1] 0----------------------------------- (22)
(10) Production for (q1, , z0) = (q1, )
Using the rule stated for (3), we get
[q1 z0
q1] (23)
Simplification of the grammar
Let, [q0 z0
q0] A [q1 z0
q0] C
[q0 z0
q1] B [q1 z0
q1] D
[q0 z0
q0] E [q1 0
q0] G
[q0 0
q1] F [q1 0
q1] H
[q0 1
q0] I [q1 1
q0] K
[q0 1
q1] J [q1 1
q1] L
By substituting the renamed variables in the productions, we get,
S A | B
B
A 0EA | 0FC
B 0EB | 0FD
E 0EE |0FG
F 0EF | 0FH
Context Free Grammar and Languages 3.10
0
E 1IE |1JG
F 1IF | 1JH
I 1II | 1JK
J 1IJ | 1JL
J 0
L 0
H 0
D
After removing the -productions
The nullable set = {D, B, S}
S A | B
A 0EA | 0FC
B 0EB | 0FD | 0E | 0F
E 0EE |0FG | 1IE |1JG
F 0EF | 0FH | 1IF | 1JH
H 0
I 1II | 1JK
J 1IJ | 1JL | 0
L 0
Removing the non-generating symbols
Non – generating symbols = {A, C, D, E, G, I}
Resultant productions
S B
B 0F
F 0FH | 1JH
H 0
J 1JL | 0
L 0
Removing unit productions
Here S B is a unit production that has to be removed.
3.101 Theory of Computation
Final productions
S 0F
F 0FH | 1JH
H 0
J 1JL | 0
L 0
The production generates a language of the form,
L = {0n 1
m 0
n+m | n, m 1}
UNIT - 4
PROPERTIES OF CONTEXT FREE LANGUAGES
4.
SIMPLIFICATION OF CFG
The need for simplifying CFG is to make it easier to analyze and prove facts about
CFL.
CFGs are simplified to study and analyze the closure and decision properties of CFLs.
Pumping lemma for CFL can also be proved easily with simplified grammar.
Simplification of CFG requires transforming CFG into an equivalent form that satisfies
certain condition/limitations on its form.
The preliminary simplifications, which are applied on grammars to convert them to
normal forms are,
o Elimination of useless symbols
o Elimination of productions
o Elimination of unit productions
Elimination of Useless Symbols (Nov/Dec 2007)
Symbols are of two types
o Useless
o Useful
Useful symbols are the variables and terminals that have some production that leads *
from the starting symbol to a string of the type S .
Consider the derivation
* *
S
where T*
Here , are any terminals and X can be either terminal or variable.
If X has no production, (if X ϵ V) then X is a useless symbol.
If X has some production / if X leads to a string, with zero/more substitution, then it is
a useful symbol.
Properties of Context Free Languages 4.2
G
Thus, useless symbols are those variables/terminals that do not appear in any
derivation of a terminal string from the start string.
Types of useless symbols
Non – generating symbols
Non-reachable symbols
Non generating symbols
*
A symbol X which is an element of V [variable] is generating symbol if X , where
T*.
Generating symbols are variables that generate a string containing terminals.
A non generating symbol is one that cannot generate any terminal.
The presence of production with non-generating symbol is useless.
Example
S aA | bB | a | b
A Aa | a
B bB
Here B bB is a recursive grammar that its substitution to S results in endless
derivation and doesnot give lead to any string of terminals.
S bB bbB bbbB bbbbB …..
Finding non generating symbols
All the terminals symbols are generating symbols.
If there is a production rule, say X and if
symbol.
(VUT)*, then A is a generating
A symbol that is not in the set of generating symbols is said to be non-generating
symbol.
Eliminating Non-Generating Symbols
The only way to simplify a grammar containing non-generating symbol is to eliminate
such symbols directly from the grammar.
4.3 Theory of Computation
Example
Consider the example given above. After eliminating the production for B bB , the
resulting grammar includes.
S aA | a | b
A aA | a
Non reachable symbols
Eliminating the variable B
A symbol is said to be reachable if and only if it can be reached from the start symbol.
*
If S , where contains X [XV] G
Then X is said to be a reachable symbol.
If the derivation from the start symbol to a string of terminals does not depend on a
variable X then X is non-reachable symbol.
Example
Let
S aAa
A Sb | bCC
C abb
E aC
In the given grammar, S depends on A, A depends on S and C, E depends on C, and C
does not depend on any variable.
Here E is the only production which is not being depended by any other productions.
E Non reachable symbol
Finding non reachable symbols
Dependency graph is used to identify the non-reachable symbols.
A dependency graph is drawn for all the production rules.
If there is no path from the start symbol to any variable X then X is non-reachable
symbol.
Example
Consider the above mentioned problem. The dependency graph is given by
Properties of Context Free Languages 4.4
A Sb
Non reachable
from S
Here all the variables are having a directed edge from the starting symbol, S except the
variable, E.
Elimination of Non-Reachable Symbols
A grammar containing non-reachable symbol can be simplified by deleting all the
production containing the non reachable symbol.
Example
After eliminating the non-reachable symbol, E we get the following grammars as,
S aAa
A Sb | bCC
C abb
Problems on elimination of useless symbols
1. Eliminate the non generating symbols from the given grammar.
(AU – Nov / Dec 2008)
Solution:
S AB | CA
B BC | AB
C aB | b
A a
Here A a, C b, hence A and C are generating.
The production with A and C is S CA.
Hence eliminate other productions except A, C and S.
The final grammar is given as
S CA
A a
C b
A bCC S A C
E aC E
S aAa
4.5 Theory of Computation
B
S 0B0 A BCC
S B 1CC A
S BC C A 0C
D D E
E
2. Eliminate the non generating symbols from the given grammar.
P = { S aAa, A Sb | bCC, C abb, E aC }
Solution:
Every production contains terminal symbols and S A C, C contains only
terminals and E C.
Hence all the symbols are generating.
P = { S aAa, A Sb | bCC, C abb, E aC }
3. Find and eliminate the non generating symbols from
S 0A0
A S1|1CC|D0A
C 011|DD
D 0D0
E 0C
Solution:
Given: S A C 011 contains terminals, E C 011.
The symbols S, A, C, E are all generating.
D 0D0 is recursive grammar that will never end.
Hence eliminate the non generating symbol, D and the productions are given as,
S 0A0
A S1|1CC // D is eliminated
C 011| // D is eliminated
E 0C
4. Eliminate the non reachable symbols from the given grammar.
P = {S 0B0|BC, A 0C|BCC, C 0, B 1CC, D E, E 1}
Solution:
We generate dependency graph to identify the non-reachable symbols.
Properties of Context Free Languages 4.6
S A S aAa
B C A bBB C aB
From the above graph, S B C, S C are all reachable from S.
But A, D, E are non reachable from S since they doesn‟t contain any production from
S.
Hence eliminate A, D, E and the resultant production includes
S 0B0
C 0
B 1CC
5. Find out and eliminate the non-reachable symbols from the grammar.
P = {S 0S | AB, A 1A, B AA}
Solution:
The dependency graph is given as
S AB
S
A
B AA
S AB B
Since S A and S B, S, A, B are all reachable from S.
Hence no elimination is required and the resultant grammar is given as
P = {0S | AB, A 1A, B AA}
6. Eliminate the non - reachable symbols from the grammar, P = {S aAa, A bBB,
B ab, C aB}
Solution:
The dependency graph is given as
From the above graph,
S A B
But C is a non-reachable symbol that doesn‟t have any derivation from S.
4.7 Theory of Computation
Hence eliminate C and the resulting grammar includes,
P = {S aAa, A bBB, B ab}
7. Consider P = { S aAa, A aS | bD, B aBa | b, C abb | DD, D aDa }. Find
the useless symbols and eliminate them.
Solution:
From the grammar, D is non - generating since it is recursive in nature as D aDa.
Hence eliminate D.
Therefore, the production rules are
S aAa
A aS
B aBa | b
C abb
Elimination of Null Production ()
Null productions are those productions that are of the form: X .
These are also called as - productions.
Example
S aS |
Here S aS and S are the productions S aS doesn‟t contain null production.
But S is an epsilon production.
S is said to be nullable variable.
Finding and handling productions
Find the nullable variables, which are of the form X .
The nullable variable used in the productions is removed.
Remove the nullable production.
Example
For the above example S aS| , the nullable variable is found to be S since S .
By eliminating the nullable variable, the production becomes,
S aS and on applying S on S aS, then S becomes S a.
Therefore, S aS|a is the final production.
Properties of Context Free Languages 4.8
Problems on elimination of productions
1. For the productions S ABA, A aA | , B bB | , find the nullable symbols.
Solution:
Since A , A is identified as a nullable variable.
Similarly B , hence B is also a nullable symbol.
Since S ABA, then S is also nullable variable since A and B are nullable symbols.
Set of nullable symbols = {S, A, B}
Removal of the variables S, A, and B results in non-existence of the grammar / severe
effect on the grammar.
2. Remove productions from the grammar, P = {S ABA, A , B }
Solution:
Given: A , B , the S ABA .
Therefore A, B and S are nullable variables
After eliminating A , B , we get
S ABA | AB | AA | BA | A | B | can be eliminated
A
B eliminated
3. Find the nullable symbols from the following grammar, P = {S aS | AB, A ,
B } and generate the grammar after addition of effect of nullable symbols.
Solution:
Since A , B , S aS | AB
The nullable set = {A, B, S}
S becomes,
S aS | a | AB | A | B |
A
B
After removing productions, the grammar becomes,
S aS | a
4.9 Theory of Computation
4. Remove - productions from
S ABA, A 0A | , B | B |
Solution:
Here A , and B and hence S ABA
The nullable variable A , B .
Hence S becomes
S ABA | AB | BA | AA | A | B
A 0A | 0
B 1B | 1
5. Remove - productions from the below given grammar.
S AB
A aAA |
B bBB |
Solution:
The nullable set = {A, B, S}
After removing A , B , the grammar becomes,
S AB | A | B
A aAA | aA | a
B bBB | bB | b
6. Consider the grammar
S ABC
A BC | a
B bAC |
C cAB |
Identify the nullable symbols and eliminate them.
Solution:
Given: B , C , A BC , S ABC the nullable variable are {S, A,
B, C}
After removing the nullable symbols, the productions become
Properties of Context Free Languages 4.10
S ABC | AB | BC | AC | A | B | C
A BC | B | C | a
B bAC | bA | bC | b
C cAB | cA | cB | C
Elimination of unit productions
The unit productions are those productions of the form X Y where X and Y are
variables of the grammar.
The left hand side and right hand side contains only one variable each side.
Example
S A | bb
A B | b
B S | a
Here
S A,
A B,
B S are all the unit productions.
Finding and eliminating unit productions
Identify the non-unit productions in the grammar. Let it be Y , where is a
terminal.
Select the unit production, X Y where X and Y are variables.
Add the production X to the grammar since X Y and Y .
Remove the unit production, X Y from the grammar.
Example
S A | bb
A B | b
B S | a
The unit productions are
S A
A B
B S
4.1
1
Theory of Computation
Elimination of unit productions
Since S A and A B and B S, we shall make it as S A B S.
Consider S A | bb. To eliminate A, substitute A‟s production „b‟ [terminal] and
since A B, substitute B‟s production „a‟ [terminal]
Do not consider the unit productions while substitution.
S bb | b | a
Similarly, to eliminate B on A B | b, substitute B‟s production „a‟ and since B S,
substitute S bb.
A b | a | bb
Finally, to eliminate S on B S | a, substitute S‟s production „bb‟ and since S A,
substitute A b
B a | bb | b
Thus the final productions are
S bb | b | a
A b | a | bb
B a | bb | b
Problems on eliminating unit productions
1. Eliminate the unit production from the grammar, P = {S ABA | BA | AA | AB | A |
B, A 0A | 0, B 1B | 1}
Solution:
The unit productions are
S A
S B
The non unit productions are
S ABA | BA | AA | AB | A | B,
A 0A | 0,
B 1B | 1
Since S A, we shall use A‟s productions A 0A | 0 to S.
Similarly, S B, we shall use B‟s productions B 1B | 1to S.
Properties of Context Free Languages 4.12
Thus the grammar becomes
S ABA | BA | AA | AB | 0A | 0 | 1B | 1,
A 0A | 0
B 1B | 1
2. Eliminate unit productions from the grammar
E E + T | T
T T * F | F
F (E) | I
I a | b | Ia | Ib | I0 | I1
Solution:
The unit productions in the given grammar are,
E T
T F
F I
The non unit productions are
E E + T
T T * F
F ( E )
I a | b | Ia | Ib | I0 | I1
After removing the unit productions, we get
E E + T | T * F | (E) | a | b | Ia | Ib | I0 | I1
T T * F | (E) | a | b | Ia | Ib | I0 | I1
F (E) | a | b | Ia | Ib | I0 | I1
I a | b | Ia | Ib | I0 | I1
3. Simplify the following grammar
S ASB |
A aAS | a
B SbS | A | bb
Solution:
Elimination of null production
The epsilon production in the grammar is S and the variable S has its impact on A
and B productions.
4.1
3
Theory of Computation
The nullable variable are {S, A, B}
After removing S , we get the final productions as
S ASB | AB [since S , exclude S]
A aAS | aA | a
B SbS | Sb | bs | b | A | bb
Elimination of unit productions
The unit production in the above grammar is B A.
Hence eliminate the unit production in B and substitute the corresponding production
of A in B. We get,
S ASB | AB
A aAS | aA | a
B SbS | Sb | bs | b | bb | aAS | aA | b
Since the grammar doesn‟t contain any useless symbols, the above production
becomes the final one.
4. Simplify the following grammar
S aAa | bBb | BB
A C
B S | A
C S |
Solution:
Elimination of - productions
The nullable symbol is C since C .
After removing C , the grammar becomes,
S aAa | bBb | aa | bb | BB | B [Since C S | ]
A C
B S | A
C S
Elimination of unit productions
The unit productions in the above grammar are
S B
A C
Properties of Context Free Languages 4.14
A
S C
B
B S
B A
C S
The grammar without the above unit productions are
S aAa | aa | bBb | bb | BB
A aAa | aa | bBb | bb | BB [since A C S]
B aAa | aa | bBb | bb | BB [since B A C S and BS]
C aAa | aa | bBb | bb | BB [since C S]
Elimination of non - generating symbols
From the above grammar
C is a non generating symbol, since it is has no derivation with respect to the start symbol.
Hence C aAa | aa | bBb | bb | BB is removed.
5. Simplify the grammar
S A1
A 0
B C | 1
C D
D E
E 0
Solution:
There is no -production in the given grammar.
Elimination of unit productions
The unit productions in the grammar are
B C
4.1
5
Theory of Computation
S S A1
A
C D
D E
After eliminating the unit productions, we get
S A1
A 0
E 0
B 0 [since B C D E 0]
C 0 [since C D E 0]
D 0 [since D E 0]
Elimination of useless symbols
Every symbol in the grammar is generating.
From the above dependency graph, S and A are reachable symbols.
The symbols other than A and S are non-generating.
They are {B, C, D, E}.
Eliminating the non-generating symbols, we get
S A1
A 0
6. Simplify the grammar equivalent to
S aC | SB
A bSCa
B aSB | bBC
C aBC | ad
Solution:
Here a, b, d are terminals
Since C ad, C is generating symbol and similarly S aC, S is also generating
symbol.
And A bSCa, hence A is generating symbol.
But B aSB | bBC. Thus, B is the only non-generating symbol.
Properties of Context Free Languages 4.16
Eliminate B from the grammar and the production becomes,
S aC
A bSCa
C ad
S aC S C
A bSCa
C A bSCa
The reachable symbols are S and C. A cannot be reached from S. Eliminating A, the
production becomes P = {S aC, C ad}
7. Convert the following grammar into an equivalent with no unit productions and no
useless symbols. (AU – Nov/Dec 2011)
S a | Ab |aBa
A b |
B b | A
Solution:
(i) Elimination of production
A is a null production and since B A and S A , all the symbols
{S, A, B} are nullable variables.
Therefore, after removing A , the production becomes,
S a | Ab | aBa | b | aa
A b
B b
NORMAL FORMS FOR CFG
Let the grammar G = (V, T, P, S) be a context free grammar. If the production rules in
G satisfy some restrictions, then they are said to be in normal form.
There are two types of normal forms
o Chomsky normal form (CNF)
o Greibach normal form (GNF)
4.1
7
Theory of Computation
GREIBACH NORMAL FORM (GNF) (June 2009, June 2013)
Greibach normal form is named after Shella Greibach who constructed GNF grammar
initially.
A grammar G = (V, T, P, S) is said to be in GNF if every production rule is of the
form, X a
where,
T, an element of the set of terminals
V* a string of non terminals/epsilon
X V, an element of the set of variables
The right hand side of every production starts with a terminal, followed by a string of
variables of zero/ more length.
Algorithm to convert a CFG to GNF
1) Eliminate useless symbols, unit productions and null productions from the
grammar, G.
2) The production rule of the form, A X1 X2 … Xn, other than X1 all other symbol
should be a variable, and X1 could be a terminal.
3) Rename the variables in all the productions as A1, A2, A3, … An productions.
4) Modify the productions with the renamed variables.
5) Remove the left recursion from every production of the form Ak Ak, by adding
B productions using left recursion algorithm as given below.
6) Modify Ai productions to the form Ai a, where a is a terminal and is a string
of variables.
7) Modify Bi productions to the form Bi a, where a is a terminal and is string of
variables.
Left recursive algorithm
A production of the form A A is said to be left recursive, with the variable on the
left hand side appears as the first symbol on the right hand side.
Consider a CFG containing productions of the forms.
A A Left recursive grammars
B For terminating the recursive A A
The language generated by above production is
A A [Since A A]
A [Since A A]
Properties of Context Free Languages 4.18
A [Since A A]
.
.
.
An [n
th time substitution]
[Since A ]
The right recursive grammar for n can be written as
A B | [B generates a string an, and A is to terminal the
recursion]
B B |
Thus the left recursive grammar is
A A |
which can be written using right recursive grammar as
A B |
B B |
PROBLEMS ON GNF
1. Construct a grammar in GNF which is equivalent to the grammar.
S AA | a
A SS | b (AU – Dec 2003, Dec 2006, Dec 2007, Dec 2008 Nov / Dec2010)
Solution:
Step 1: Simplification of grammar by removing useless symbols, unit and null
productions. The given grammar is already in simple form.
Step 2: Renaming of variables as A1, A2, … An.
Let S be renamed as A1 and A be renamed as A2 respectively. (The terminals
need not be renamed.)
Therefore the given grammar becomes.
A1 A2 A2 | a
A2 A1 A1 | b
Step 3: Modification of the productions.
The higher numbered production is modified first.
4.1
9
Theory of Computation
Hence,
A2 A1 A1 | b [Substitution of production to the leftmost variable]
[A2 A2 | a] A1 | b
A2 A2 A2 A1 | a A1 | b
Stop substitution and modification when the left side symbol and the first
symbol on the right side are same.
Step 4: Removing left recursion
The modified production, A2 A2 A2 A1 | a A1 | b
Left recursion grammat
The left recursive grammar can be removed by introducing a new variable B2
[B2 is named, since it is substituted for A2.]
Break A2 into two forms
A2 A2 A2 A1 and A2 a A1 | b (1)
A2 a A1 B2 | bB2 (2)
Remove the first symbol at right side
and (2)
A2 a A1 B2 | bB2 | aA1 | b
B2 A2 A1 B1 | A2 A1
The resulting grammar
A1 A2 A2 | a
A2 a A1 B2 | b B2 | a A1 | b GNF
B2 A2 A1 B1 | A2 A1
Step 5: Production in GNF
Substitute A2 in B2
B2 A2 A1 B1 | A2 A1
[a A1 B2 | bB2 | aA1 | b] A1 B2 |
[a A1 B2 | bB2 | aA1 | b] A1
Properties of Context Free Languages 4.20
a A1 B2 A1 B2 | b B2 | A1 B2| a A1 A1 B2 |
b A1 B2 | a A1 B2 A1 | b B2 A1 | a A1 A1 | b A1 GNF
Substitute A2 in A1
A1 A2 A2 | a
[a A1 B2 | aB2 | aA1 | b] A2 | a
A1 a A1 B2 A1 | a B2 A1| a A1 A2| b A2 | a GNF
The final set of productions is
A1 a A1 B2 A2 | a B2 A2| aA1 A2| b A2 | a
A2 a A1 B2 | bB2 | aA1 | b
B2 a A1 B2 A1 B2 | bB2 A1 B2 | aA1 A1 B2 | b A1 B2 |
a A1 B2 A1 | bB2 A1 | aA1 A1 | b A2
G = (V, T, P, S)
V = {A1, A2, B2}
T = {a, b}
S = {A1}
P as given above
2. Given the GNF for the following CFG. (AU – May 2004, Dec 2004, Dec 2005,
June 2006, June 2007, May 2012, Dec
2009, May 2007)
Solution:
S AB
A BS | b
B SA | a
Step 1: Simplification of grammar
The grammar is already simplified.
Step 2: Renaming the variables
Let S, A, B be renamed as A1, A2, A3 respectively.
The productions with renamed variables are
A1 A2 A3
A2 A3 A1 | b
4.2
1
Theory of Computation
A3 A1 A2 | a
Step 3: Modification of grammars
Consider the highest numbered production.
A3 A1 A2 | a [substitute A1 in A3]
[A2 A3] A2 | a
A3 A2 A3 A2 | a [substitute A2 in A3 till we get A3 as the first
symbol in R.H.S]
[A3 A1 | b] A3 A2 | a
A3 A3 A1 A3 A2 | b A3 A2 | a
Left recursive grammar
Step 4: Removing left recursion
Break down A3 production as
A3 A3 A1 A3 A2 (1)
and A3 b A3 A2 | a (2)
Introduce B3 in (2)
A3 b A3 A2 B3 | a B3 | b A3 A2 | a
With B3 in (2) Without B3 in (2)
Eqn (1) becomes
B3 A1 A3 A2 B3 | A1 A3 A2 [A3 is removed in (1)]
Addition of B3 Without B3
The resulting productions are
A1 A2 A3
A2 A3 A1 | b
A3 b A3 A2 B3 | a B3 | b A3 A2 | a GNF
B3 A1 A3 A2 B3 | A1 A3 A2
Properties of Context Free Languages 4.22
Step 5: Find the productions in GNF
Substitute A3 in A2
A2 A3 A1 | b
[b A3 A2 B3 | a B3 | b A3 A2 | a] A1 | b
A2 b A3 A2 B3 | a B3 A1 | b A3 A2 A1 | a A1 | b
Substitute A2 in A1
A1 A2 A3
[b A3 A2 B3 A1 | a B3 A1 | b A3 A2 A1 | a A1 | b] A3
b A3 A2 B3 A1 A3 | a B3 A1 A3 | b A3 A2 A1 A3 | a A1 A3 | b A3
Substitute A1 in B3
B3 A1 A3 A2 B3 | A1 A3 A2
[b A3 A2 B3 A1 A3 | a B3 A1 A3 | b A3 A2 A1 A3 | a A1 A3 | b A3] A3 A2 B3 |
[b A3 A2 B3 A1 A3 | a B3 A1 A3 | b A3 A2 A1 A3 | a A1 A3 | b A3] A3 A2
b A3 A2 B3 A1 A3 A3 A2 B3 | a B3 A1 A3 A3 A2 B3 | b A3 A2 A1 A3 A3 A2 B3 |
a A1 A3 A3 A2 B3 | b A3 A3 A2 B3 | b A3 A2 B3 A1 A3 A3 A2 | a B3 A1 A3 A3
A2 | b A3 A2 A1 A3 A3 A2 | a A1 A3 A3 A2 | b A3 A3 A2
The CFG in GNF is given by
G = (V, T, P, S) where
V = {A1, A2, A3, B3}
T = {a, b}
S = {A1}
P A1 b A3 A2 B3 A1 A3 | a B3 A1 A3 | b A3 A2 A1 A3 | a A1 A3 | b A3
A2 b A3 A2 B3 A1 | a B3 A1 | b A3 A2 A1 | a A1 | b
A3 b A3 A2 B3 | a B3 | b A3 A2 | a
B3 b A3 A2 B3 A1 A3 A3 A2 B3 | a B3 A1 A3 A3 A2 B3 | b A3 A2 A1 A3 A3 A2 B3 |
a A1 A3 A3 A2 B3 | b A3 A3 A2 B3 | b A3 A2 B3 A1 A3 A3 A2 | aB3A1A3
A3A2
| b A3 A2 A1 A3 A3 A2 | a A1 A3 A3 A2 | b A3 A3 A2
4.2
3
Theory of Computation
3. Convert the following CFG to GNF
S AB
A BSB | BB | b
B aAb | a
Solution:
Step 1: Simplification of grammar
The grammar doesn‟t contain useless symbols, null and unit productions.
Step 2: Renaming the variables
Making every symbol except the first symbol as a variable
S AB
A BSB | BB | b
B a A Cb | a, where Cb b
Renaming the variables S, A, B, Cb as A1, A2, A3, A4 respectively.
The production becomes
A1 A2 A3
A2 A3 A1 A3 | A3 A3 | b
A3 a A1 A4 | a
A4 b
Step 3: Modification of grammar
Consider A4 b already in GNF
Consider A3 a A1 A4 | a already in GNF
Consider A2 A3 A1 A3 | A3 A3 | b
Substitute A3 in A2
A2 [a A1 A4 | a] A1 A3 | [a A1 A4 | a] A3 | b
a A1 A4 A1 A3 | a A1 A3 | a A1 A4 A3 | a A3 | b
Substitute A2 in A15
Properties of Context Free Languages 4.24
A1 A2 A3
[a A1 A4 A1 A3 | a A1 A3 | a A1 A4 A3 | a A3 | b] A3
a A1 A4 A1 A3 A3 | a A1 A3 A3 | a A1 A4 A3 A3 | a A3 A3 | b A3
Step 4: Final grammar
G = (V, T, P, S) where
V = {A1, A2, A3, A4}
T = {a, b}
S = {A1}
P A1 a A1 A4 A1 A3 A3 | a A1 A3 A3 | a A1 A4 A3 A3 | a A3 A3 | b A3
A2 a A1 A4 A1 A3 | a A1 A3 | a A1 A4 A3 | a A3 | b
A3 a A1 A4 | a
A4 b
4. Convert the following grammar to GNF
S BS
S Aa
A bc
B Ac
Solution:
Step 1: Simplification of the grammar
The grammar is already in simplified form.
Step 2: Renaming the variables
Making every first symbol in RHS to be a variable
S BS | ACa,
Ca a
A b Cb,
4.2
5
Theory of Computation
Cb c
B A Cb
Renaming S, A, B, Ca, Cb as A1, A2, A3, A4, A5 respectively.
A1 A3 A1 | A2 A4
A2 b A5
A3 A2 A5
A4 a
A5 c
Step 3: Converting the productions to GNF
Here A5 c, A4 a, A2 bA5 are already in GNF
Consider A3 A2 A5
Substitute A2 in A3
A3 b[A5] A5
bA5 A5
Substitute A3 and A2 in A1
A1 [bA5 A5] A1 | [bA5] A4
bA5 A5 A1 | bA5 A4
Step 4: Final production in the grammar G
G = (V, T, P, S), where
V = {A1, A2, A3, A4, A5}
T = {a, b}
S = {A1}
P A1 bA5 A5 A1 | bA5 A4
A2 b A5
A3 bA5 A5
A4 a
A5 c
Properties of Context Free Languages 4.26
5. Find the GNF of the grammar
S ABAb | ab
B ABA | a
A a | b
Solution:
1. The grammar is already in simplified form
2. Conversion of CFG to GNF
S ABA Cb | a Cb , where Cb b
B ABA | a
A a | b
Substitute A in B and S after renaming the variables.
A1 A2 A3 A2 A4 | a A4
A2 a | b
A3 A2 A3 A2 | a
A4 b
Since A2 and A4 are already in GNF,
Substitute A2 in A1 and A3
A1 [a | b] A3 A2 A4 | a A4
a A3 A2 A4 | b A3 A2 A4 | a A4
A3 [a | b] A3 A2 | a
a A3 A2 | b A3 A2 | a
Thus the grammar G = (V, T, P, S), becomes,
V = {A1, A2, A3, A4}
T = {a, b}
S = {A1}
P A1 a A3 A2 A4 | b A3 A2 A4 | a A4
A2 a | b
A3 a A3 A2 | b A3 A2 | a
A4 b
4.2
7
Theory of Computation
6. Find the GNF equivalent to the following CFG
S AB
A aA | bB | b
B b
Solution:
The grammar is already in simplified form and A and B are already in GNF. Hence
substitute A in S.
S AB
[aA | bB | b] B
aAB | bBB | bB
The grammar G = (V, T, P, S) is given by
V = {A, B, S}
T = {a, b}
P S aAB | bBB | bB
A aA | bB | b
B b
S = {S}
The grammar need not be renamed if the substitution is simple and is done for one
production alone.
7. Convert S ab | aS | aaS to GNF
Solution:
The grammar is already in simplified form.
Since there is one production to be converted, there is no need of renaming.
S ab | aS | aaS
is converted as
S a Cb | aS | a Ca S where
Ca a
Cb b
The final grammar G = (V, T, P, S) is given as
Properties of Context Free Languages 4.28
V = {S, Ca, Cb}
T = {a, b}
P S a Cb | aS | a Ca S,
Ca a
Cb b
S = {S}
CHOMSKY NORMAL FORM (CNF) (May/June 2012)
A context free grammar, G = (V, T, P, S) is said to be in CNF if each production in G
is of the form, X YZ; X , where X, Y, Z V and T.
Every CFG without null productions can be easily converted into its equivalent CNF.
Algorithm to convert a CFG to CNF
Eliminate the useless symbols, unit and null productions from the grammar
Each variable should be having a derivation of length 2 or more, having only variable
as of the form, A where || ≥ 2, V.
The production have three more than three variables as derivation, must be broken
down into a cascade of productions with derivations containing at most 2 variables.
PROBLEMS ON CNF
1. Find the CNF for the CFG, S aAbB, A aA, B bB | b.
(AU – Dec 2003, June 2007)
Solution:
Step 1: Simplification of the grammar
The given grammar is already in simple form.
Step 2: Every symbol in in a production of the form A where || ≥ 2 should be
a variable.
This can be done by converting the terminal to variable as,
Ca a
Cb b
The productions after substituting Ca and Cb in the given grammar is given as,
S Ca A Cb B
4.2
9
Theory of Computation
A Ca A
B Cb B | b [B b cannot be changed as B Cb since there
is only one terminal symbol]
Ca a
Cb b
Step 3: Conversion to CNF
S Ca A Cb B S Ca C1
C1
C1 A Cb B C1 A C2
C2
C2 Cb B
A Ca A
B Cb B | b
Ca a
Cb b
where,
V = {S, A, B, Ca, Cb, C1, C2}
T = {a, b}
S = {S}
2. Convert the grammar given to CNF. (AU – Dec 2005, Dec 2006)
S bA | aB
A bAA | aS | a
B aBB | bS | b
Solution:
Step 1: Simplification of grammar
The grammar is already in simplified form.
Properties of Context Free Languages 4.30
Step 2: Every symbol in the production of the form A where || ≥ 2 should be
a variable.
This is done by adding the following two productions.
Ca a
Cb b
The set of productions after substituting Ca a and Cb b is,
S Cb A | Ca B
A Cb AA | Ca S | a (No need to apply variables, since they
are single, with no other variable /
terminals.)
B Ca BB | Cb S | b
Step 3: Conversion to CNF
S Cb A
S Ca B
A Cb AA A Cb C1
C1 C1 AA
AA Ca S
A a
B Ca BB B Ca C2
C2 C2 BB
where,
B b
V = {S, A, B, Ca, Cb, C1, C2}
T = {a, b}
S = {S}
3. Find the CNF equivalent to (AU – May 2004, Dec 2010)
S aAD
A aB | bAB
B b
D d
4.3
1
Theory of Computation
Solution:
Step 1: Simplification of grammar
The grammar is already in simplified form with no useless symbols, unit and
null productions.
Step 2: Conversion of terminals to variables.
Ca a
Cb b
The production becomes
S Ca AD
A Ca B | Cb AB
B b
D d
Step 3: Conversion to CNF
S Ca AD S Ca C1
C1 C1 AD
A Ca B
A Cb AB A Cb C2
C2 C2 AB
B b
D d
4. Convert the grammar given below to CNF
S ABA
A aA |
B bB |
Solution:
Step1: Simplification of grammar
1) Elimination of useless production
There are no non-reachable and non-generating symbols in the given grammar.
Properties of Context Free Languages 4.32
2) Elimination of Epsilon production
Since A , B , and S ABA, the nullable variables are {A, B, S}
Eliminating the productions, we get,
S ABA | AB | BA | A | B
A aA | a
B bB | b
3) Eliminating the unit production
There are two unit productions S A and S B.
Eliminating the unit production, we get
S ABA | AB | BA | aA | a | bB | b | AA
A aA | a
B bB | b
Step 2: Addition of two productions to define the terminals.
Ca a
Cb b
Step 3: Conversion to CNF
S ABA
C1
S A C1
C1 BA
S AB
S BA
S Ca A; where Ca a
S a
S Cb B; where Cb b
S b
S AA
A Ca A
A a
B Cb B
B b
4.3
3
Theory of Computation
5. Convert the following grammar to CNF
S Pba
S aab
Q Pc
Solution:
Step 1: Simplification of the grammar
There is no unit or epsilon production there is a non-reachable symbol, B.
The symbol can be deleted, and resulting production is
S Pba | aab
Step 2: Adding productions to handle terminals
Ca a
Cb b
S P Cb Ca | Ca Ca Cb
Step 3: Conversion to CNF
S P Cb Ca S Ca Ca Cb
C1 C2
S P C1 S Ca C2
C1 Cb Ca C2 Ca Cb
6. Convert the following CFG to CNF
S AACD
A 0A1 |
C 0C | 0
A 0D0 | 1D1 |
Solution:
Step 1: Simplificaiton of grammar
(1) Removing epsilon production
Here A is a nullable production.
Elimination of A:
Properties of Context Free Languages 4.34
S AACD | ACD | CD
A 0A1 | 01
C 0C | 0
A 0D0 | 1D1
(2) Removing non-generating symbol
Here the symbol S and D are non-generating.
Since the start symbol is a non-generating symbol, the grammar becomes invalid.
7. Convert the grammar given to CNF (AU – Dec 2004)
S 0A0 | 1B1 | BB
A C
B S | A
C S |
Solution:
Elimination of - production
Here C is the epsilon production.
Since A C, B A C, S A C, then A, B, S, C are all nullable symbols.
Eliminating the epsilon productions, we get
S 0A0 | 00 | 1B1 | 11 | BB | B
A C
B S | A
C S
Elimination of unit production
The unit productions are S B, A C, B S, C S, B A.
After eliminating the unit productions, we get
S 0A0 | 00 | 1B1 | 11 | BB
A 0A0 | 00 | 1B1 | 11 | BB [since A C S]
B 0A0 | 00 | 1B1 | 11 | BB [since B S]
C 0A0 | 00 | 1B1 | 11 | BB [since C S]
Eliminating of useless symbols
The production „C‟ is non-reachable since S A and S B.
4.3
5
Theory of Computation
After eliminating C we get,
S 0A0 | 00 | 1B1 | 11 | BB
A 0A0 | 00 | 1B1 | 11 | BB
B 0A0 | 00 | 1B1 | 11 | BB
Since S, A and B productions are identical we shall eliminate A and B.
S 0A0 | 00 | 1B1 | 11 | BB which can be written as S 0S0 | 00 | 1S1 | 11 | SS
Conversion to CNF
Add two productions to the terminals {0, 1}
C1 0
C2 1
Substituting C1 and C2 in S, we get
S C1 S C1 | C1 C1 | C2 S C2 | C2 C2 | SS
The above grammar is now converted to CNF as
S C1 S C1 S C1 C3
C3 C3 S C1
S C1 C1
S C2 S C2 S C2 C4
C4 C4 S C2
S C2 C2
S SS
C1 0
C1 1
8. Convert the grammar S AB | aB, A aab | , B bbA into CNF.
(AU – June 2009)
Solution:
Elimination of -production
From the given grammar, A , which is the nullable variable.
After eliminating the epsilon production, w eget
Properties of Context Free Languages 4.36
S AB | B | aB
A aab
B bbA | bb
Elimination of unit production
The unit production is S B.
After eliminating the unit production the resulting grammar becomes,
S AB | bbA | bb | aB
A aab
B bbA | bb
There is no useless symbol in the grammar.
Conversion to CNF
Adding productions to terminals,
Ca a
Cb b
Rewriting the grammar by substituting Ca and Cb, we get
S AB | Cb Cb A | Cb Cb | Ca B
A Ca Ca Cb
B Cb Cb A | Cb Cb
Conversion the grammar to CNF gives,
S AB
S Cb Cb A S Cb C1
C1 C1 Cb A
S Cb Cb
S Ca B
A Ca Ca Cb A Ca C2
C2 C2 Ca Cb
B Cb Cb A B Cb C1
C1 C1 Cb A
4.3
7
Theory of Computation
B Cb Cb
Ca a
Cb b
9. Simplify the following grammar to its equivalent CNF. P { S AB | CA, Aa, B
BC | AB, C aB | b } (AU – Dec 2008, Dec 2007)
Solution:
Step 1: Simplification of the grammar
There are no - production in the grammar.
There are no unit productions.
The non generating symbols in G is {B} since B BC | AB generates a
derivations, that will never end.
Hence after removing B, the grammar becomes,
S CA
A a
C b
Thus the grammar is already in CNF.
10. Convert the grammar into CNF. (AU – Apr / May 2008)
A bAB |
B BAa |
Solution :
1) Elimination of -productions
Here A , B are the productions. Hence the nullable variables are {A,
B}
After removing A , B , the production becomes,
A bAB | bB | bA | b
B BAa | Ba | Aa | a
2) Conversion to CNF
A b are already in CNF
B a
Properties of Context Free Languages 4.38
A bAB A Cb C1 where C1 AB, Cb b
C1
A Cb B, where Cb b
A Cb A, where Cb b
B BBa B B C2 where C2 B Ca, Ca a
C2
B B Ca, where Ca a
B A Ca, where Ca a
11. Construct the following grammar in CNF. (AU – May / June 2013)
A BCD | b
B YC | d
C gA | c
D dB | a, Y f
Solution:
There is no useless symbols, unit and null productions in the grammar.
First, the production rules for terminal symbols is constructed as
C1 a C4 d
C2 b C5 f
C3 c C6 g
After substitution of the above productions in the given grammar, we get
A BCD | b
B YC3 | d
C C6A | c
D C4 B | a
Y f
The grammar above is converted to CNF as,
A B C D A B Ca
Ca Ca CD
4.3
9
Theory of Computation
A b
B YC3
C1 a
C2 b
B d C3 c
C C6A C4 d
C c C5 f
D C4 B C6 g
D a
Y f
PUMPING LEMMA FOR CFG
Let G be a context free grammar. Then there exists a constant „n‟ such that any string,
L(G) with || n can be rewritten as = uvxyz, subject to the following conditions.
(i) |vxy| ≤ n
(ii) vy
i i (iii) for all i 0, uv xy z is in L
PUMPING LEMMA FOR CFL – THEOREM
(AU – May/June 2013, N Nov/Dec 2012, Nov/Dec 2011)
Theorem
Let L be a CFL. Then there exist a constant „n‟ such that if „‟ is any string in L such
that || ≥ n, then can be written as = uvxyz subject to,
i) |vxy| ≤ n
ii) vy or |vy| 0
i i iii) for all i ≥ 0, uv xy z is in L
Proof
The initial step is to find a Chomsky normal grammar, G equivalent to L.
Let grammar G = (V, T, P, S) such that L(G) = L - .
Consider that the grammar has m variable with n = 2M
Assume that „‟ is in L of length, || ≥ n
By theorem
o Any parse tree of length = M for the longest path must have a yield of length = 2
M-1 is n/2 or less.
Properties of Context Free Languages 4.40
Ai = Aj
Aj
A
A
A
x
edge.
o Such a parse tree cannot have yield = z, since z is too large.
Thus a parse tree with yield has a path of length ≥ M + 1.
This parse tree will have the vertices ≥ M + 2, since there must be two vertices on an
The tree is divided with the root = Aj of the subtree for the string as,
S
be .
u v x y z
For the yield „x‟ at „Aj‟ the strings „v‟ and „y‟ are to the left and right of „x‟.
Since there are no unit productions in the chomsky‟s grammar, both „v‟ and „y‟ cannot
At Ai, strings „u‟ and „z‟ are to its left and right of vxy.
When Ai = Aj, the Ai and Aj are replaced by A has yield = x.
The resultant tree is given as,
S S
u v x y z u y
The tree has a yield uxy and corresponds to the case, i=0 in uv i xy
i z [as given above]
4.4
1
Theory of Computation
A
A
A
u v x y z
S
v x y
i i When i=2, uv xy z becomes
2 2 uv xy as given above, by expanding the tree with x = vxy as another subtree.
The tree can be expanded in a similar way for any number of times as „i‟ grows,
yielding the parse tree of the form uv i xy
i z.
Here |vxy| ≤ n with Ai as the base of the tree with k – i ≤ M.
Thus the longest path in of the subtree with root = Ai is no greater than M+1.
yield length is no greater than 2M
= n.
Thus Pumping Lemma is proved.
Applications
(i) Used to check if a language is context-free or not.
PROBLEMS
1. Prove that L = {ai b
i c
i | i ≥ 1} is not CFL.
(AU – May 2004, Dec 2004, May 2005, June 2006, June 2007, Dec 2007,
Dec 2008, Dec 2009, N/D 2012, M/J 2012)
Solution:
(1) Let us assume that L is a CFL
(2) Let us pick up, = an b
n c
n, where n is a constant.
(3) is rewritten as uvxyz, where
Properties of Context Free Languages 4.42
(i) |vxy| ≤ n
(ii) vy
(iii) for all i, uv i xy
i z L.
If vy contains all three symbols a, b, c.
In this case either v = ab / bc / ac or y = ab / bc / ac. The exact ordering of a,b, c will be
broken.
If i = 2, uv i xy
i z uv
2 xy
2 z becomes
Case (i) If v = ab and y = c
uv 2
xy 2
z = (ab)2 c uv
i xy
i z L
Case (ii) If v = a and y = bc
uv 2
xy 2
z = a(bc)2 uv
i xy
i z L
Hence L is not a CFL.
2. Prove that L = {ai b
i c
j | j > i} is not a CFL
Solution:
Let us assume that L is CFL
Let = ai b
i c
j , where i, j is a constant.
can be rewritten as uvxyz, where
(i) |vxy| ≤ n
(ii) vy
(iii) uv i xy
i z is in L(G) for i = 0, 1, 2.
Let vy contains all three symbols a, b, c.
(i) If v = ab and y = c
Then uv 2
xy 2
z = (ab)2 c
2
Power of c should be greater than or equal to powers of ab. But its is not so here.
(ii) If v = a and y = bx
uv 2
xy 2
z = (a)2 (bc)
2 . Here j ≥ i is not true.
j is power of c
4.4
3
Theory of Computation
i is power of a,b
Thus there are unequal no of a‟s and b‟s and the count of either a or b can be increased
from the count of c.
Hence L is not a CFL.
3. A = an2
|n 1 is context free. If so, enumerate some member of the equivalent CFL.
(AU – May 2008).
Solution:
Let L a CFL. Let L = an2
where n is a constant.
Let = an2
We can be written as uvxyz, where
(i) |vxy| ≤ n
(ii) vy
(iii) If i = 0, 1, 2, …… uv i xy
i z L
Here
|uvxyz| = | an2
| = n2
uv 2 xy
2 z = |uvxyz| + |vy| > n
2 ---------------------- (1)
uv 2 xy
2 z ≤ n
2 + n [as |vy| ≤ n from (i)]
≤ n2 + 2n + 1
≤ (n+1)2 -----------------------------------
(2)
From (1) and (2) n2 < | uv
2 xy
2 z | ≤ (n+1)
2
But there is no square between n2 and (n+1)
2.
Hence L is not a CFL.
4. Prove that L = {an b
m c
p | 0 ≤ n < m < p} is not CFL.
Solution:
(i) Let L = {an b
m c
p | 0 ≤ n < m < p}
(ii) Let us pick up a word = an b
n+1 c
n+2, where n is a constant and [since n < m < p]
(iii) can be rewritten as uvxyz, where
i) |vxy| ≤ n
ii) vy
Properties of Context Free Languages 4.44
2 2
i i
2 n-1 n n n
n n n n-1 2 2
i = 2 i i 2 2 2 2
i i iii) If i = 0, 1, 2, …… uv xy z L
Let vy contains all the 3 symbols a, b, c
(i) If v = ab and y = c
uv xy z uv xy z = (ab) c --------- (1)
(ii) If v = a and y = bc
2 2 At i = 2, uv xy z = a (bc) ------------------------- (2)
i i From (1) and (2) uv xy z L since the powers of abc does not match with the L
L is not a CFL.
5. Prove that L = { | is in (0+1)*} is not a CFL
Solution:
Let us assume that L is a CFL.
Let = 0n 1
n 0
n 1
n, where n is a constant and L = { | is in (0+1)*}
can be rewritten as uvxyz, where
i) |vxy| ≤ n
ii) vy
iii) If i = 0, 1, 2, …… uv i xy
i z L
Let vy takes all the values of 0 and 1.
1) If v = 01 and y = 0
n-1 1
n 0
n 1
n
2 2 At i = 2, uv xy z = (0) (0 1 0 1 )------------------------- (1)
2) If v = 0n 1
n 0
n 1
n-1; y = 1
2 2 At i = 2, uv xy z = (0 1 0 1 ) (1)------------------------- (2)
From (1) and (2); uv xy z L since the powers of 0 and 1 and L = does not
match.
6. Prove that L = {0i 1
j 2
i 3
j | i ≥ 1 and j ≥ 1} is not context – free.
(AU – Dec 2003, Dec 2005)
Solution:
Let us assume that L is CFL.
Let = 0n 1
n 2
n 3
n, where n is a constant.
4.4
5
Theory of Computation
2 2 1
2 2 1
Let is rewritten as uvxyz where
i) |vxy| ≤ n
ii) vy
iii) uv i xy
i z L for i = 0, 1, 2, …
Let vy takes the symbols 012 0r 1 2 3
(i) if v = 01 and y = 2
2 2 At i = 2, uv xy z = (01) (2) 3 ------------------- (1)
(ii) if v = 12 and y = 3
2 2 At i = 2, uv xy z = (12) (3) 0 ------------------- (2)
From (1) and (2) there are unequal number of 0 1 2 and 3 in (1) and unequal numbers
of 1 2 3 and 0 in (2).
Hence L is not a CFL.
PROPERTIES OF CFL
(AU – Nov/Dec 2013, Nov/Dec 2010, May/June 2013)
The properties of CFL are classified into 2 groups.
• Closure properties
• Algorithm / Decision properties
Closure properties
A context-free language is closed under the following operations.
• Union
• Concatenation
• Kleene star
Context free language is not closed under
• Intersection
• Complementation
• Reversal
CFL is closed under union (AU – Dec 2007, Dec 2009)
Theorem
If L1 and L2 are CFL, the L1 L2 is also CFL.
Properties of Context Free Languages 4.46
Proof
Let L1 be a CFL generated by a CFG, G1 = (V1, T1, P1, S1)
Similarly, L2 is another CFL generated by a CFG, G2 = (V2, T2, P2, S2)
We can combine, the two grammar G1 and G2 into one grammar, G that will generate
the union of the two languages.
• A new start symbol, S is added to G.
• Two new productions are added to G
S S1
S S2
• The grammar G can be written as
G = (V1, V2 {S}, T1 T2, P1 P2 {S S1|S2}, S)
• S can generate a string of terminals by selecting start symbol S1 of G1 or start
symbol S2 of G2.
• Thus, S can generate a string from L1 or from L2
L(G) = L1 L2
CFL is closed under concatenation
Theorem
If L1 and L2 are context-free language, then L1 L2 is a context-free language.
Proof
Let L1 be a CFL with the grammar G1 = (V1, T1, P1, S1)
Let L2 be a CFL with the grammar G1 = (V2, T2, P2, S2)
A new language L is constructed by combining the two grammars G1 and G2 into one
grammar G that will generate the concatenation of the two languages.
• A new start symbol, S is added to G.
• A new production is added to G
S S1 S2
The start symbol S will generate a string, of the form:
= 1 2
Where 1 L1 2 L2
The grammar G can be written as
G = (V1, V2 {S}, T1 T2, P1 P2 {S S1 S2}, S)
4.4
7
Theory of Computation
CFL is closed under kleene star
Theorem
If L is a context free language, then L* is a context free language.
Proof
Let L1 be a CFL with the grammar, G1 = (V1, T1, P1, S1).
A new language L is constructed from L1, which is L1*.
L = L1*
A new start symbol, S is added to grammar, G of L.
Two new productions are added to G.
S S S1
S
The production S SS1 | will generate a string * where L1.
The grammar G can be written as
G = (V1, T1, P {S S S1 | }, S)
CFL is not closed under intersection
Theorem
Context free language are not closed under intersection.
Proof
Let us consider two context free languages L1 and L2.
Where
L1 = {an b
n c
m | n, m 0}
L2 = {am
bn c
n | n, m 0}
The language L1 is a CFL with set of productions.
S AB
A aAb |
B CB |
The language L2 is a CFL with set of productions.
S AB
A aA |
B bBC |
Properties of Context Free Languages 4.48
A string 1 L1 contains equal number of a‟s and b‟s. A string 2 L2 contains
equal number of b‟s and c‟s.
A string L1 L2 will contain equal number of a‟s and b‟s and equal number of
b‟s and c‟s.
The L1 L2 = {an b
n c
n | n 0}. From pumping lemma for CFL, a string of the form
an b
n c
n cannot be generated by a CFG.
The class of CFL is not closed under intersection.
CFL is not closed under complementation
Theorem
The set of CFL is not closed under complementation.
Proof
This theorem can be proved through contradiction.
Let us assume me that CFL is closed under complementation.
If L1 is context-free, then L1` is also context-free.
If L2 is context-free, then L2` is also context-free.
Now L1 L2 can be written (L1` L2`), which should also be a context-free.
Since, L1 L2 is not generated to be context-free our assumption that CFL is closed
under complementation is wrong.
CFL is closed under reversal
Theorem
If L is a CFL, then so is LR.
Proof
Let us assume that L = L(G) for some CFG, G = (V, T, P, S).
A grammar generating reverse L is given by GR = (V, T, P
R, S)
PR can be obtained from P by reversing the right hand side of the production.
If A is a production in P, then
A R is a production in P
R
Algorithm / decision properties
These are algorithms for testing.
4.4
9
Theory of Computation
The method of finding whether a string belongs to L(G) is known as parsing. There
are two types of parsing.
• Top-down parsing
• Bottom-up parsing
TURING MACHINE
A Turing machine is an „automatic machine‟ that manipulates the input strings
according to the transition rules.
It was invented by Alan Turing in 1936.
Turing machine is also called an „Intelligent machinery‟ is a hypothetical device.
Alan Turing, named Turing machine as „a-machine‟, was stimulated by his lecturer
Newman‟s works and words on „mechanical process‟, made him invented this
when he was doing master degree in UK.
Turing machine is the base for inventing various other machines such as
The Enigma {secret code encryption machine}
Automatic computing engine [AEC]
EDVAC
Turing‟s „a-machine‟ is the base of even today‟s digital computer.
Due to its usage in several ways, Turing machine became the most powerful general
model of computation.
Further, TM is the mathematical model of partial recursive functions and in computing
undecidability.
Definitions of Turing machines
Description of a TM/Informal definition
Turing machine is composed of
1. Input tape with tape head
2. Finite set of transition rules
3. Output tape with tape head
The input tape contains the input symbols. The tape is divided into a number of cells,
each cell storing one symbol.
The input tape can also serve as output tape, that contains the resultant value of the
operation done by TM.
Properties of Context Free Languages 4.50
The tape head is used to read a symbol from the input tape or write a symbol on the
output tape.
The tape is capable of reading/writing one symbol at a time.
Several heads can be used to perform parallel processes/operations on the head.
The transition rules are responsible for performing the required operations using its
rules, and based on the input read.
It performs state transitions based on the processing done.
Nature of TM
The Turing machines are computing machines that are more powerful than the FA and
PDA. It is given as
Finite automata Determination PDA NPDA TM
FA, DPDA, NPDA are less powerful than TM since these machines have no control
over the input and cannot modify their own inputs.
But TM can control and modify their inputs on the tape.
Formal definition of TM
A Turing machine, M is a 7-tuple given by
M = (Q, , , , q0, B, F)
where,
Q Finite set of states
Finite set of inputs symbols on input tape
Finite set of tape symbols [ B] [where B blank symbol]
Transition function given by
(q, a) = (q', b, M) [M movement left, right, no movement]
q0 Initial state [q0 Q]
B Blank symbol representing empty cell [B ]
F Set of final states [F Q]
Model of Turing machine
Turing machine shall be thought of as a finite state machine with a read/write head.
The input is stored on an input tape that is divided into a number of individual cells.
4.5
1
Theory of Computation
B a1 a2 a3 … b1 b2 B B
The cells contains blank symbol, „B‟ when there is no input in it.
Each cell can store one symbol in it.
Read/Write
Infinite input
tape
head Inputs
Empty cell
The read/write head of the system reads from and writes data to the input tape.
The head performs its operation on one cell at a time.
The movement of the head can be
o Left (moving backward)
o Right (moving forward)
o None (no movement)
The head is capable of
o Reading a symbol
o Modifying a symbol
o Move Previous(L) / Move Next(R)
o Halt
INSTANTANEOUS DESCRIPTION FOR TM
Instantaneous description for TM is the snapshot of how the input string is processed
by the Turing machine.
It describes,
The input string
Position of the head
State of the machine
Example
For the Turing machine given below, provide the instantaneous description for the
string aabb.
Finite control
Properties of Context Free Languages 4.52
a / a, R
b / b, R
/ b, L
/ a, L
Instantaneous description
aabbB xabbB xabbB xabbB xabbB
q0 q1 q1 q1 q1
x byB
xxbyB xxbyB xxbyB xxyyB xxyyB
q1 q1 q2 q3 q0
xxyyB [String Accepted ]
q4
PROBLEMS
1. Design a Turing machine that accepts all strings of the form an b
n for n ≥ 1 and
rejects all other string.
AU – JUN 2006, JUN 2009, DEC 2009
Solution:
Algorithm
1. Change the leftmost a to x
2. Move forward right side and change the rightmost b to y.
3. Move left and change the leftmost a, which is at position immediate right of x, to x.
4. Move right and change the rightmost b, which is at immediate left of y, to y.
5. Repeat step 3 and 4 until there are no more b‟s equal to a‟s.
6. Halt the TM.
q0 a / x, R
q1 B/ B, L
y / y, L q2
y / y, N x / x, R
b/ y, L
q4 q3 b
a
abbB
xabyB
xabyB
xabyB
xa
q1 q3 q3 q3 q0
4.5
3
Theory of Computation
3
2
3 1
2
Instantaneous description
Let n = 3 = a3 b
3 = aaabbb
… …
x
… …
R
…
L
B x a a b b b B
y
B x a a b b y B
L
B x a a b b y B
R
L 4
x 1
…
1
2 R
B a a a b b b B
B x a a b b b B
B x a a b b b B
Properties of Context Free Languages 4.54
1 x 2
2
B x x a b b y B
R
B x x a b y y B
R
L
B x x x b y y B
N R
Turing machine
a / a, R
b / b, R
/ b, L
/ a, L
L 3
2
q0 a / x, R
q1 B/ B, L
y / y, L q2
y / y, N x / x, R
b/ y, L
q4 q3 b
a
3
1 y
3
y 1
L
4.5
5
Theory of Computation
Description of the TM
The Turing machine, M is given by
M = (Q, , , , q0, B, F)
where,
Q = {q0, q1, q2, q3, q4}
= {a, b}
= {a, b, B, x, y}
q0 = {q0}
B = {B}
F = {q4}
is given by
Q
a
b
x
y
B
q0 (q1, x, R) - - (q4, y, N) -
q1 (q1, a, R) (q1, b, R) - (q2, y, L) (q2, B, L)
q2 - (q3, y, R) - - -
q3 (q3, a, L) (q3, b, L) (q0, x, R) - -
* q4
Here, q0 replaces leftmost a by x.
q1 skips a, b, and moves right until first B or first y.
q2 replaces rightmost b by y.
q3 skips a, b, and moves left till first x is replaced and enters q0.
q4 halts if a valid an b
n string is processed.
Instantaneous description for the string, = “aaabbb”
bbB
– xaabbbB – aabbbB – xaabbbB – xaabbbB
q1 q1 q1 q2
aaabbbB
– xaabbbB
– xaabbbB
– xaab
q0 q1 q1 q1
Properties of Context Free Languages 4.56
– xaabbyB
q3
– xaabbyB
q3
– xaabbyB
q3
– xaabbyB
q0
– xaabbyB
q3
– xxabbyB
q1
– xaabbyB
q3
– xxabbyB
q1
– xxabbyB – xxabbyB – xxabbyB – xxabyyB
q1 q1 q2 q3
– xxabyyB – xxabyyB – xxabyyB – xxxbyyB
q3 q3 q0 q1
– xxxbyyB – xxxbyyB – xxxbyyB – xxxyyyB
q1 q1 q2 q3
– xxxyyyB – xxxyyyB stop
q0 q4
2. Design a TM that perform right shift over = {0, 1}.
Solution:
To perform: Shift the input string right by one place.
Algorithm
1. Move right side to the last character from the initial character.
2. If the character is „0‟ replace it with „B‟ and move one step right to replace the
immediate „B‟ to „0‟.
3. If the character is „1‟, replace it with „B‟ and move one step right to replace the
immediate „B‟ to „1‟.
4. After processing as above, move left one step to perform step 2 or 3 on the next
right most unprocessed character.
5. Perform step – 4 until all the characters are processed.
4.5
7
Theory of Computation
3 2
2 2 3
2 2 3
1 R
2 3
B 0 1 0 1 B …
B 0 1 0 1 B …
B 0 1 0 1 B
B 0 1 0 B B …
B 0 1 0 B 1 …
B 0 1 B B 1 …
B 0 1 B 0 1 …
B 0 B B 0 1 …
B 0 B 1 0 1 …
B B B 1 0 1 …
2
Instantaneous description
Let = 0101
L
1 2
B 1 R
L L
B 0 R
L L
B l R
L L
1 2
B 0 R
L L
Input string processed
No input Halt
1
1
1
1
1
2 1
1
B B 0 1 0 1 …
Properties of Context Free Languages 4.58
0 / 0, R
1/1, R
q2
0 / B, R B/ 0, L
q0 B/ B, L
q1 q4
1/ B, R B/1, L
B/ B, N q3
q5 B/ B, L
Turing Machine
Description of Turing Machine
The turing machine is given by
M = (Q, , , , q0, B, F)
where, Q = {q0, q1, q2, q3, q4, q5}
= {0, 1}
= {0, 1, B}
q0 = {q0}
B = {B}
F = {q5}
is given by
Q
0
1
B
q0 (q1, 0, R) (q0, 1, R) (q1, B, L)
q1 (q2, B, R) (q3, B, R) (q5, B, N)
q2 - - (q4, 0, L)
q3 - - (q4, 1, L)
q4 - - (q1, B, L)
* q5
4.5
9
Theory of Computation
Here,
q0 skips the initial 0‟s and 1‟s and moves the head of the TM to the last
character.
q1 the unprocessed rightmost character is replaced by B.
q2 on processing the input „0‟, q2 replaces immediate the „B‟ as „0‟ and takes
left.
q3 on processing „1‟, q3 replaces immediate the „B‟ as „1‟ and moves left.
q4 takes a left position to process the next character
q5 halts if no more inputs characters are available.
Instantaneous description for = “0101”
1B
– 0101B
– 010BB
– 010B1
– 010B1
– 01BB1
q1 q3 q4 q1 q2
– 01B01
q4
– 01B01
q1
– 0BB01
q3
– 0B101 – 0B101
q4 q1
– BB101
q2
– B0101
q4
– BB0101 – B0101 Stop
q1 q5
2. Construct a Turing machine to make a copy of a string over = {0, 1}
Solution:
Algorithm
Given
A sequence of input symbols followed by „#‟ at the end.
Steps
1. If the symbol is „0‟, move right until „B‟ is reached. Mark „0‟ as „x‟ to indicate that
it is processed.
2. Replace the symbol „B‟ by „0‟. Move left until „x‟ is reached. Mark x as „0‟ and
move right.
0101B
– 0101B
– 0101B
– 0101B
– 010
q0 q0 q0 q0 q0
Properties of Context Free Languages 4.60
3. If the symbol is „1‟, move right until „B‟ is reached. Mark „1‟ as „y‟ to indicate that
it is processed.
4. Replace „B‟ by 1 and move left until „y‟ is reached. Mark „y‟ as 1 and move right.
5. Process step 1 and 2 if „0‟ is the input symbol; else perform step 4 and 5 to process
„y‟.
6. Stop and reach final state if „#‟ is reached.
Turing machine
# / #, R
1/1, R
0 / 0, R
# / #, L
1/1, L
0 / 0, L
Representation of „101‟
1 0 1 # B
y 0 1 # B
L
y 0 1 # 1
L
2 1
q1
0 / x, R B/ 0, L
q0 q3
1/ y, R B/1, L
q2 # / #, N
# / #, R
q4 x / 0, R
1/1, R
y /1, R 0 / 0, R
4.6
1
Theory of Computation
2
1 0 1 # 1
1 x 1 # 1 B
R
1 x 1 # 1 0
L
1 0 1 # 1 0
1 0 y # 1 0 B
1 0 y # 1 0 1
R
1 0 1 # 1 0 1
R Stop
Description of Turing machine
The Turing machine, M is given by
M = (Q, , , , q0, B, F)
where,
Q = {q0, q1, q2, q3, q4}
= {0, 1}
= {0, 1, x, y, #, B}
q0 = {q0}
B = {B}
F = {q4}
1
2
1
2
1
1 R 2
Properties of Context Free Languages 4.62
is given by
Q 0 1 # B x y
q0 (q1, x, R) (q2, y, R) (q4, #, N) - - -
q1 (q1, 0, R) (q1, 1, R) (q1, #, R) (q3, 0, L) - -
q2 (q2, 0, R) (q2, 1, R) (q2, #, R) (q3, 1, L) - -
q3 (q3, 0, L) (q3, 1, L) (q3, #, L) - (q0, 0, R) (q0, 1, R)
* q4
Instantaneous description for = 101
101#B – y01#B – y01#B – y01#B – y01#B
q0 q2 q2 q2 q2
– y01#1
– y01#B
– y01#B
– y01#B
– 101#1
q3 q3 q3 q3 q0
– 1x1#1 – 1x1#1 – 1x1#1B – 1x1#1B – 1x1#10
q1 q1 q1 q3 q3
– 1x1#10 – 1x1#10 – 101#10 – 10y#10
q3 q3 q0 q2
– 10y#10 – 10y#10B – 10y#10B – 10y#101
q2 q2 q2 q3
– 10y#101 – 10y#101 – 10y#101 – 101#101
q3 q3 q3 q0
– 101#101
stop
q4
3. Design a TM to reserve a string over = {a, b}.
Solution:
Algorithm
1. Move to the last symbol, replace x for a or x for b and move right to convert the
corresponding B to „a‟ or „b‟ accordingly.
4.6
3
Theory of Computation
2. Move left until the symbol left to x is reached.
3. Perform step – 1 and step – 2 until „B‟ is reached while traversing left.
4. Replace every x to B to make the cells empty since the reverse of the string is
performed by the previous steps.
Instantaneous description using the below given TM
Let = abb
abbB – abbB – abbB – abbB – abbB
q0 q0 q0 q0 q1
– abxB – abxbB – abxbB – axxbB
q3 q4 q1 q3
– axxbB
q3
– axxbB
q3
– axxbbB
q4
– axxbbB
q4
– axxbbB – xxxbbB – xxxbbB
q1 q2 q2
– xxxbbB
q2
– xxxbbB
q2
– xxxbbB
q2
– xxxbbaB
q4
– xxxbbaB
q4
– xxxbbaB
q4
– xxxbbaB
q1
– BxxxbbaB
q1
– BxxxbbaB
q1
– BxxxbbaB
q5
– BBxxbbaB
q5 – BBBBbbaB
q6
– BBBxbbaB
q5
Halt
– BBBBbbaB
q5
Properties of Context Free Languages 4.64
1 1
R
1 R
2
1 R 2
a b b B
a b x B
a x x b B
a x x b b B
x x x b b B
x x x b b a B
B x x b b a B
B B x b b a B
B B B b b a B
Turing Machine
x / x, R
a / a, R
b / b, R
a / a, L
b / b, L
Representation for = abb
R R No movement
1
q2
a / a, R
b / b, R x / x, L
a / x, R B/ a, L
q0 q1 q4
b/ x, R
B/ B, R B/ b, L
q3
q6 a / a, N
b/ b, N q5
a / a, R
b / b, R
x / x, R
x / x, L
x / B, R
1 R
2 L 1
2
L 1
2
L 1 2
4.6
5
Theory of Computation
x / x, R a / x, R q1 b/ x, L
q0 q3
b / x, R a / x, L
q2
B/ B, N
b/ b, R x / x, R
q4
B/ B, R
R 2
1
1
2
a b b a
x b b a
x x b a
4. Design a Turing machine to check if there are equal number of a‟s and b‟s over {a, b}
Solution:
Algorithm
1. Process the first symbol
2. If it is „a‟, move right to locate the first „b‟, and replace both by x.
3. If „b‟ occurs, replace it by x and move forward to reach the first „a‟ replace „a‟ by
x.
4. Perform steps 2 and 3 until all inputs are processed.
Representation
Let = abba
x R
Halt
R
Turing Machine
x / x, R
a / a, R
a / a, L
b / b, L
x / x, L
1
2 R
N 1
x x x a B
x x x x B
Properties of Context Free Languages 4.66
Instantaneous description for = abba
– BabbaB – BxbbaB – BxxbaB – BxxbaB
q0 q1 q3 q3
– BxxbaB – BxxbaB – BxxbaB – BxxxaB
q0 q0 q0 q2
– BxxxxB – BxxxxB – BxxxxB – BxxxxB
q3 q3 q3 q3
– BxxxxB – BxxxxB – BxxxxB – BxxxxB
q0 q0 q0 q0
– BxxxxB – BxxxxB Halt.
q0 q4
The Turing machine, M is given by
M = (Q, , , , q0, B, F)
where,
Q = {q0, q1, q2, q3, q4}
= {a, b}
= {a, b, x, B}
q0 = {q0}
B = {B}
F = {q0}
TM as mentioned above.
is given by,
Q a B x B
q0 (q1, x, R) (q2, x, R) (q0, x, R) (q4, B, N)
q1 (q1, a, R) (q3, x, L) (q1, x, R) -
q2 (q3, x, L) (q2, b, R) (q2, x, R) -
q3 (q3, a, L) (q3, b, L) (q3, x, L) (q0, B, R)
* q4
4.6
7
Theory of Computation
5. Design a TM that recognizes strings of the form an b
n c
n | n≥1 over = {a, b, c}.
Solution:
Algorithm
1. Process the leftmost „a‟ and replace it by „x‟.
2. Move right until the leftmost „b‟ is reached. Replace it by „y‟.
3. Move right until the leftmost „c‟ is reached. Replace it by „z‟.
4. Move left to reach the leftmost „a‟ and perform steps 1, 2 and 3 (n – 1) times.
5. Halt if there are „n‟ number of x, y, z.
Turing Machine
a / x, R
0
y / y, R
a / a, R
b/ y, R 1
b / b, R
z / z, R
q2
y / y, R
y / y, R
x / x, R
c / z, L
z / z, L
z / z, R q4
B/ B, N
q3 b / b, L
a / a, L
y / y, L
q5
The Turing machine, M is given by
M = (Q, , , , q0, B, F)
where Q = {q0, q1, q2, q3, q4, q5}
= {a, b, c}
= {a, b, c, x, y, z, B}
Given by the transition diagram of the TM
q0 = {q0}
B = {B}
F = {q5}
q q
Properties of Context Free Languages 4.68
is given by
Q a b c x y z B
q0 (q1, x, R) - - - (q4, y, R) - -
q1 (q1, a, R) (q2, y, R) - - (q1, y, R) - -
q2 - (q2, b, R) (q3, z, R) - - (q2, z, R) -
q3 (q3, a, L) (q3, b, L) - (q0, x, R) (q3, y, L) (q3, z, L) -
q4 - - - - (q4, y, R) (q4, z, R) (q5, B, N)
* q5
Instantaneous description for = a2 b
2 c
2
= aabbcc
– BaabbccB – BxabbccB – BxabbccB
q0 q1 q1
– BxaybccB – BxaybccB – BxaybzcB
q2 q2 q3
– BxaybzcB – BxaybzcB – BxaybzcB
q3 q3 q3
– BxaybzcB – BxxybzcB – BxxybzcB
q0 q1 q1
– BxxyyzcB – BxxyyzcB – BxxyyzzB
q2 q2 q3
– BxxyyzzB – BxxyyzzB – BxxyyzzB
q3 q3 q3
– BxxyyzzB – BxxyyzzB – BxxyyzzB
q0 q4 q4
– BxxyyzzB – BxxyyzzB – BxxyyzzB Halt
q4 q4 q5
4.6
9
Theory of Computation
6. Design a TM which recognizes palindromes over = {a, b}.
Solution:
Algorithm
1. If there is no input, reach the final state and halt.
2. If the input = „a‟, then traverse forward to process the last symbol = „a‟. convert
both a‟s to „B‟.
3. Move left to read the next symbol.
4. If the input = „b‟, replace it by B and move right to process its equivalent „B‟ at the
rightmost end.
5. Convert the last „‟b‟ to „B‟.
6. Move left and process step 2 – 5 until there are no more inputs to process.
7. If the machine reaches the final state after processing the entire input string, then
the string is a palindrome halt the machine.
Turing Machine
a / B, R
a / a, R
q1 b / b, R
a / a, R
b / b, R
q3
B/ B, L q5
a / B, L
q0
B/ B, N
B/ B, N
q8
a / a, L
q7 b / b, L
b/ B, R B/ B, N
q2 a / a, R
q4
b / b, R
B/ B, L
q6
a / a, R
b / b, R
b/ B, L
The Turing machine, M is given by
M = (Q, , , , q0, B, F)
where, Q = {q0, q1, q2, q3, q4, q5, q6, q7, q8}
= {a, b}
= {a, b, B}
Given by the above mentioned transition diagram
Properties of Context Free Languages 4.70
q0 = {q0}
B = {B}
F = {q8}
Instantaneous Description
Let = aabbaa
– BaabbaaB – BBabbaaB – BBabbaaB – BBabbaaB
q0 q1 q3 q3
– BBabbaaB – BBabbaaB – BBabbaaB – BBabbaaB
q3 q3 q3 q5
– BBabbaBB – BBabbaBB – BBabbaBB – BBabbaBB
q7 q7 q7 q7
– BBabbaBB – BBabbaBB – BBBbbaBB – BBBbbaBB
q7 q0 q1 q3
– BBBbbaBB – BBBbbaBB – BBBbbaBB – BBBbbBBB
q3 q3 q5 q7
– BBBbbBBB
q7
– BBBbbBBB
q7
– BBBbbBBB
q0
– BBBBbBBB
q2
– BBBBbBBB
q4
– BBBBBBBB
q8
– BBBBbBBB
q6
Halt
– BBBBBBBB
q7
– BBBBBBBB
q0
Hence the given string aabbaa is a palindrome.
4.7
1
Theory of Computation
q0
a / a, R q
B/ B, L 1 q2
7. Design a TM that recognizes the language of all non-palindromes over {a, b}.
Solution:
Turing Machine
a / B, R
a / a, R
q1 b / b, R
a / a, R
b / b, R
q3
B/ B, L
q5
a / B, L
b/ b, N
q0 q8
a / a, L
q7 b / b, L
b/ B, R
q2
a / a, R
b / b, R
a / a, N
B/ B, L 4
b/ B, L
q6
B/ B, R
8. Design a TM that accepts a(a+b)* over = {a, b}
Solution:
a / a, R
b / b, R
9. Construct a TM for L = {x {0, 1}* | x ends in 00}
Solution:
1/1, R
q0
0 / 0, R
1/1, R
0 / 0, R q1 q2
1/1, R B/ B, L
q3
q
Properties of Context Free Languages 4.72
Turing machine
with „f‟
1 2
COMPUTABLE LANGUAGES AND FUNCTIONS
Turing machine is capable of performing several operations/functions.
It is capable of performing any sort of computations such as
Addition
Subtraction
Multiplication
Division
2‟s complementation
1‟s complementation
Comparing two numbers
Squaring a number
GCD of numbers
Finding binary equivalents.
A Turing machine M can compute a function, f from the subset of ∑*.
The machine starts the computation corresponding to the input string, in the domain
of „f‟ and halts with the output string f().
f()
When „‟ is an input string that cannot be computed by the Turing machine, M with
function f, then the TM should not accept .
The Turing machine, M can handle a function containing several variables.
Representation:
Computation of a function „f‟ is represented as
f : *
*
The function f is Turing computable by the machine,
M = (Q, ∑, , , q0, B, F)
With the transition function of the form,
4.7
3
Theory of Computation
2
*
where,
(q0 , ) M
(qf , f ())
q0 Initial state (q0 Q)
Input string, where *
. 1
qf Final state (qf F)
f() Output string after computation of „‟ by f f * .
Here, the inputs other than ∑* will not be accepted by the TM.
Representation of a number
For simplicity, the unary numbers are represented by a single symbol let „0‟.
The decimal numbers are represented as,
No input B
1 0
2 00
3 000
4 0000
5 00000
.
.
.
n „n‟ number of zeros [0n]
Computing numerical functions
Turing machines can also compute numerical functions.
Numerical functions computes string which are represented as above
It is defined as,
T = (Q, {0},,, q0, B, F)
With „f‟ being the numerical function defined by a set of natural numbers, N.
„f‟ is defined as
Properties of Context Free Languages 4.74
0 f
where,
(q , 0n )
* (q , 0
f (n) )
M
PROBLEMS
q0 initial state
0n input string
qf final state
0f(n)
output of M, after computing 0n in „f‟
1. Design a Turing machine to perform 2‟s complement of a number over ∑ = {0, 1}
(Binary number)
Solution:
Algorithm
1. Traverse right and locate the rightmost bit
2. If the bit = „0‟, perform no replacement and move left
3. If the bit = „1‟ perform no change and move left
4. If the next bit symbol = „0‟, replace it by „1‟ and move left.
5. Else if the next bit symbol = „1‟, replace it by „0‟ and move left.
6. Perform steps 4 and 5 until all the input symbols are processed (from right to left)
7. Halt the machine.
Note:
2‟s complement computation
Do not change the bits from the right towards left until the first „1‟ has been
processed.
Perform complementation to the rest of the bits from right to left (after first 1 is
processed)
Example:
0 1 0 1 1 0 1 0 0 0 0
No change
1 0 1 0 0 1
10100110000
4.7
5
Theory of Computation
1/1, R
0 / 0, R
0 / 0, L
0 /1, L
1/ 0, L
The Turing machine, M is given by
M = (Q, ∑, , , q0, B, F)
where,
Q = {q0, q1, q2, q3}
= {0, 1}
= {0, 1, B}
Given by the above transition diagram
q0 = {q0}
B = {B}
F = {q3}
Transition table,
Q
0
1
B
q0 (q0, 0, R) (q0, 1, R) (q1, B, L)
q1 (q1, 0, L) (q2, 1, L) (q3, B, R)
q2 (q2, 1, L) (q2, 0, L) (q3, B, R)
* q3
Instantaneous description for = 10100
q0 B/ B, L
q1 1/1, L
q2
B/ B, R
B/ B, R
q3
Properties of Context Free Languages 4.76
q0 B / B, L
q1
– B10100B – B10100B – B10100B – B10100B
q0 q0 q0 q0
– B10100B – B10100B – B10100B – B10100B
q0 q0 q1 q1
– B10100B – B10100B – B11100B – B01100B
q1 q2 q2 q2
– B01100B Halt
q3
2. Design a TM to find 1‟s complement of binary inputs.
Solution:
1‟s complement computation
o Complement every bit in the string from right towards left.
o Eg: 0 1 0 1
1010
1 0 1 0
Algorithm
1. On reading the input, if the symbol is „0‟, replace it by 1 and move right.
2. If the symbol read = „1‟ replace it by „0‟ and move right.
3. Halt the TM if all the string symbols are processed by step 1 and 2.
Turing Machine
0 / 1, R
1 / 0, R
The Turing machine, M is given by
M = (Q, ∑, , , q0, B, F)
where, Q = {q0, q1}
= {0, 1}
4.7
7
Theory of Computation
= {0, 1, B}
Given by the above Turing machine transition diagram
q0 = {q0}
B = {B}
F = {q1}
Transition table,
Q
0
1
B
q0 (q0, 1, R) (q0, 0, R) (q1, B, L)
* q1
Instantaneous description for = 0101
– B0101B – B1101B – B1001B – B1011B
q0 q0 q0 q0
– B10100B – B10100B Halt
q0 q1
3. Design a TM to compute addition of two unary numbers.
Solution:
The unary input number „n‟ is represented with a symbol „0‟ n – times.
Example: 4 0000
1 0
5 00000
The separation symbol, „#‟ (any other special character) shall be used to distinguish
between two or more inputs.
Example: 5, 2 are the inputs represented by 00000 # 00.
Algorithm
Properties of Context Free Languages 4.78
q0 # / 0, R
q1 B / B, L
q2 0 / B, N
q3
1. Read the symbols of the first input with no replacements and move right.
2. When the symbol = „#‟ replace it by „0‟ and move right.
3. Traverse right side until the rightmost „0‟ (left to „B‟ – last symbol)
4. Replace the rightmost „0‟ by „B‟
5. Stop the machine.
Turing Machine
0 / 0, R 0 / 0, R
The Turing machine, M is given by
M = (Q, ∑, , , q0, B, F)
where, Q = {q0, q1, q2, q3}
= {0, #}
= {0, #, B}
Given by the above transition diagram
q0 = {q0}
B = {B}
F = {q3}
Transition table,
Q
0
#
B
q0 (q0, 0, R) (q0, 0, R) -
q1 (q1, 0, R) - (q2, B, R)
q2 (q3, B, N) - -
* q3
Instantaneous description for = 2, 3
4.7
9
Theory of Computation
– B00#000B – B00#000B – B00#000B
q0 q0 q0
– B000000B – B000000B – B000000B
q1 q1 q1
– B000000B – B000000B – B00000BB Halt
q1 q1 q1
4. Design a TM to compute subtraction of two unary numbers given by
AU DEC 2003, DEC 2005
f m, n m n, if m n
0, otherwise
Solution:
Algorithm
1. Replace the leftmost „0‟ by B and move right.
2. Replace the rightmost „0‟ by B and move left.
3. Perform steps 1 and 2, (n – 1) times.
4. Halt the machine since remaining „0‟ by B and halt.
Turing Machine
0 / B, R
0
# / #, R
0 / 0, R
q1
B / B, L
# / 0, N
2
M > n
q5
0 / B, R
q4
0 / B, L
q3
0 / 0, L
# / #, L
B / B, N
M ≤ n
The Turing machine, M is given by
q q
Properties of Context Free Languages 4.80
where,
M = (Q, ∑, , , q0, B, F)
Q = {q0, q1, q2, q3, q4, q5}
= {0, #}
= {0, #, B}
q0 = {q0}
B = {B}
F = {q5}
is given by
Q
0
#
B
q0 (q1, B, R) (q4, B, R) -
q1 (q1, 0, R) (q1, #, R) (q2, B, L)
q2 (q3, B, L) (q5, 0, N) -
q3 (q3, 0, L) (q3, #, L) (q0, B, R)
q4 (q4, B, R) - (q5, B, N)
* q5
Instantaneous description for = 000#0 [3 – 1]
– B000#0B – BB00#0B – BB00#0B
q1 q1 q1
– BB00#0B – BB00#0B – BB00#0B
q1 q1 q1
– BB00#0B – BB00#BB – BB00#BB
q2 q3 q3
– BB00#BB – BB00#BB
4.8
1
Theory of Computation
q3 q0
– BB B
– BBB0#BB – BBB00BB Halt
q2 q5
5. Compute a TM that performs multiplication of two unary numbers.
AU MAY 2005, DEC 2007
Solution:
Algorithm
1. Read the leftmost „0‟ replace it by „x‟ and move right to process the immediate
symbol after „#‟.
2. Replace the symbol „0‟ by x and move right reach the first „B‟ after „#‟
3. Replace „B‟ by „0‟ and move left until the nearest „x‟ is reached
4. Replace the „x‟ by 0 and move right to process the next symbol of the
multiplicand.
5. Perform steps 2, 3 and 4 until all the symbols of the multiplicand are processed.
6. Move left to replace the symbol of the multiplier, „x‟ by „0‟.
7. Perform steps 1 to 6 until all the symbols of the multiplier are processed.
Turing Machine
0 / 0, R
# / #, R
0 / 0, R
/ 0, L
/ #, L
The turing machine, M is given by
q0 0 / x, R
q1 # / #, R
q2 0 / x, R
q3
# / #, N # / #, L B / 0, L
x / 0, R x / 0, R
q5 q4
q6 0 / 0, L
# / #, L
0
#
B0#BB
– BBB0#BB
– BBB0#B
q1 q1 q1
Properties of Context Free Languages 4.82
where,
M = (Q, ∑, , , q0, B, F)
Q = {q0, q1, q2, q3, q4, q5, q6}
= {0, #}
= {0, #, x, B}
q0 = {q0}
B = {B}
F = {q6}
given by the above transition diagram.
Instantaneous description
Let = 2, 2 2 x 2 = 4 B00#00#BB
– B00#00#B – Bx0#00#B – Bx0#00#B
q0 q1 q1
– Bx0#00#B – Bx0#x0#B – Bx0#x0#B
q2 q3 q3
– Bx0#x0#B – Bx0#x0#0B – Bx0#x0#0B
q3 q4 q4
– Bx0#x0#0B – Bx0#00#0B – Bx0#0x#0B
q4 q2 q3
– Bx0#0x#0B – Bx0#0x#0B – Bx0#0x#00B
q3 q3 q4
– Bx0#0x#00B – Bx0#0x#00B – Bx0#00#00B
q4 q4 q2
– Bx0#00#00B – Bx0#00#00B – Bx0#00#00B
q5 q5 q5
– Bx0#00#00B – Bx0#00#00B – B00#00#00B
4.8
3
Theory of Computation
q5 q5 q0
– B0x#00#00B – B0x#00#00B – B0x#x0#00B
q1 q2 q3
– B0x#x0#00B – B0x#x0#00B – B0x#x0#00B
q3 q3 q3
– B0x#x0#00B – B0x#x0#000B – B0x#x0#000B
q3 q4 q4
– B0x#x0#000B – B0x#x0#000B – B0x#x0#000B
q4 q4 q4
– B0x#00#000B – B0x#0x#000B – B0x#0x#000B
q2 q3 q3
– B0x#0x#000B – B0x#0x#000B – B0x#0x#000B
q3 q3 q3
– B0x#0x#0000B – B0x#0x#0000B – B0x#0x#0000B
q4 q4 q4
– B0x#0x#0000B – B0x#0x#0000B – B0x#00#0000B
q4 q4 q4
– B0x#00#0000B – B0x#00#0000B – B0x#00#0000B
q5 q5 q5
– B0x#00#0000B – B00#00#0000B – B00#00#0000B
q5 q0 q6 result of 2 x 2 Halt
6. Design a TM to perform n2 for n ≥ 0.
Solution:
n2 can be computed by multiplying „n‟ by „n‟ times.
n2 = n x n
Example: 32 = 3 x 3 = 9
Properties of Context Free Languages 4.84
Turing Machine
0 / x, R
0 / 0, R
# / #, R
B / 0, L
# / #, L
q0 q1 q2 0 / 0, L
# / #, R
0 / 0, R
x / 0, R
# / #, R
0 / 0, L
# / #, L
q3
B / #, L
B / B, R q4 q5
0 / x, R
0 / 0, R
q6
# / #, R
7
x / 0, R
# / #, R
0 / 0, R
q8
x / 0, L
# / #, L
0 / x, R
B / 0, L
# / #, N q10 q9
q11 0 / 0, L
# / #, L
7. Design a TM to compute the quotient and remainder of division of two unary
numbers.
Solution:
0 / 0, R
y / y, R
z / z, R
0 / B, L
z / 0, L
y / B, L
B / B, R
0 / x, R 0
# / #, R q1
# / #, L
2 q6 # / B, L
X / B, L
y / y, L
0 / 0, L
# / #, L
B / 0, L
# / #, R
q4
# / #, R
0 / 0, R
z / y, R
y / y, R
x / 0, R 0 / z, L
q3
z / z, L
0 / 0, L
# / #, L
y / y, L
B / B, N
q7
q
q q
4.8
5
Theory of Computation
q0 B / 0, N
q1
8. Design a TM that increments a binary number by 1.
Solution:
To perform incrimination
Replace the trailing 1‟s to 0.
Replace the first „0‟ from the right end by 1.
Turing Machine
1 / 1, R
0 / 0, R
1 / 0, L
9. The binary equivalent of a positive integer is stored in a tape. Write the transitions to
multiply the integer by 2. AU Dec 2006
Solution:
Append „0‟ to any binary number to get the solution of multiplication of the number by
2.
0 / 0, R
1 / 1, R
10. Design a TM that perform multiplication operation using “copy” subroutine.
AU May 2004, June 2006
Solution:
Let the inputs be 0x and o
y
The inputs are stored as B0x # 0
y # BB.
Turing Machine
Subroutine for copy operation
0 / 0, R
# / #, R
# / #, L
0 / 0, L
q0 B / B, L
q1 0 / 1, N
B / 1, N q2
q 0 / x, R B / 0, L
0 q2
# / #, L x / x, R
x / 0, L q3 q4 # / #, R
Properties of Context Free Languages 4.86
TM for multiplication
0, L
0 / B, R
11. Design a TM for f(x, y) = x * y + x, where x and y are stored in the tape of the form,
0x | 0
y. AU Dec 2008
Solution:
f (x, y) = x * y + x
= x(y + 1)
= Increment y value by 1
= Multiply x with (y + 1)
Turing machine:
1 / 1, R
0 / 0, R
0 / 0, L
1 / 1, L
1 / 1, L
0 / 0, L
q0 B / 1, L
q1 1 / 0, L
q2 0 / 0, L
1 / 1, L
B / B, R 0 / 0, R
1 / 1, R
0 / 0, R
q3 0 / x, R
q4 1 / 1, R
q5 0 / x, R
q6
x / 0, R 1 / 1, L
# / #, N x / 0, R
B / 0, L
q9 q8 q7
q5 B / B, R
0 /
0 / B, R 0 / 0, L
0 / 0, R q0 q0 Copy
Routine
q # / #, L
4 q 7 q # / #, R
8
0 / 0, L
B / B, R
# / B, R q12 q11
# / B, R q10
4.8
7
Theory of Computation
TECHNIQUES FOR TURING MACHINE CONSTRUCTION
Every Turing machine is defined by its transition function, : Q {L, R, S} for
any (q, ).
The transition function is a partial function since it is not defined for all (q, ).
The transition is based on the current state and input symbol.
The process results in a transition/resulting state, tape symbol replacement by right /
left / stop (No change).
The higher level of formal description of a Turing machine is called high level
description / implementation description.
There are some high-level conceptual tools to construct Turing machine easier.
They are,
o Turing machine with stationary head
o Storage in the state
o Multiple track Turing machine
o Subroutines
o Two-way infinite tape.
Turing Machine with Stationary Head
Every Turing machine has a finite state machine that controls the input tape using its
head (movable).
The head is capable of moving left (L) right (R) or remain stationary (N) with no
movement.
For any transition can be defined as, (q, a) = (q’, b, M) where M can be L / R / N.
Thus the head moves left / right / remain unchnged after reading the input symbol, „a‟.
Consider the transition,
(q, a) = (q’, b, N)
The Turing machine after reading „a‟, changes the state from q to q` and replace „a‟ by
„b‟.
The head remains in the same cell since the movement is given as „N‟.
The above transition can also be simulated by the below given transitions.
Properties of Context Free Languages 4.88
(q, a) = (q’’, b, R)
(q’’, B) = (q’, b, L), where B ∑
Thus the turing machine is modeled with a transition given by
(q, a) = (q’b, M)
where,
M = L / R / N
Storage in the State
The states of turing machine are to remember/store a symbol. This is done by the finite
control.
The pain (q, a) defines the state and the tape symbol being stored.
Multiple tracks Turing Machine
The input tape of a Turing machine is an infinite single tape divided into individual
cells.
A multiple track Turing machine is a single tape divided into multiple tracks.
This is also called as k-dimension tape, where k is the number of „tracks‟.
Standard Turing machine contains the tape symbol as the elements of .
Multitrack Turing machine contain multiple tracks (k) each with set of elements
leading to k symbols on the tape.
The transitions are same in both the types.
… …
Track 1 Track 2 Track i Track k
Subroutine
Subroutines are sub functions that can be used to execute repeated tasks for any
number of times depending on the application.
In such case, the Turing machine has to be designed that handles subroutines.
4.8
9
Theory of Computation
The Turing machine is designed to process the subroutine of a program.
The subroutine has two states
o Initial state
o Return state
When the main function is executed, the subroutine is called.
The TM reaches the initial state and follows a series of executions using the transition
rules of the subroutine.
After processing, the TM reaches the return state that returns back to the main function
after a temporary halt.
Two – way infinite tape TM
A turing machine, M = (Q, ∑, , , q0, B, F) is said to be a two-way infinite tape TM if
the input tape is infinite to the left and right.
B a0 a1 a2 … ai … ak B
Infinite length at the
leftside of the tape/symbols
Input symbols
Infinite length at the
rightside of the input
It can be viewed as a finite sequence of input symbols with infinite sequence of blank
symbols to the left and right of the input.
As with the standard TM, there is a fixed left end, the two - way infinite tape TM has
no left end. Hence it can move as far as possible towards left as well as right.
This is no “fall off” of TM towards left.
…
Subroutine
called by
main ( )
…
Turing
machine
…
…
Properties of Context Free Languages 4.90
Multi-head and multi-tape Turing machine
There are several variants of standard Turing machine.
Standard TM has a finite state machine controlling a single input tape.
The transition function of which is given as,
(q, a) = (q`, b, M)
where,
M = R or L or N
The variants of TM include,
o Multi-head turing machine
o Multi-tape turing machine
Multi-head Turing machine
A Turing machine with a single tape can have any number of heads to provide
simultaneous accesses over the tape.
Input
tape
The finite control posses two or more tape heads to access the input tape for
performing multiple reads/writes in a simultaneous and independent manner.
The transition behavior of a 2 headed tape is given as
(q', H1(a), H2(a)) = (q'', H1(b), M1), H2(b), M2))
Where
q', q'' states of Q
H1(a) symbol to be processed (read) by H1
H2(a) symbol to be processed (read) by H2
H1(b) symbol to be replaced by H1
H2(b) symbol to be replaced by H2
M1 movement of H1 (L / R / N)
M2 movement of H2 (L / R / N)
Head 1 Head 2 Head 3
Finite control
… a1 a2 a3 b1 b2 b3 c1 c2 …
4.9
1
Theory of Computation
Multi-tape Turing machine
A multitape TM is finite control with more than one tape for reading/writing symbols,
storing states etc.
Each tape has „n‟ number of individual cells.
The first tape is mostly used to store the input symbols of the string, .
The second and third tapes are normally used to store the states and state-changes.
Tape 1
Tape 2
Tape 3
The transition rules of a two tape TM is given as,
(q, a1, a2) = (q`, H1(b), M1), H2(b), M2)
where,
q current state to be processed
q` next state to be reached
a1 symbol read by tape 1
a2 symbol read by tape 2
H1(b) symbol to be written by tape 1
H2(b) symbol to be written by tape 2
M1 Movement by tape 1 (R / L / N)
M2 Movement by tape 2 (R / L / N)
Each move in a multitape TM depends on the current state and the symbol scanned by
the tape heads.
…
Head 3 Head 2 Head 1
Finite control
… …
… …
…
Properties of Context Free Languages 4.92
Thus, on each move,
o State change
o Replace new symbols on each cells being processed on the tapes.
o Tape heads take right/left movement or remain unchanged.
THEOREM: Equivalent of multitape and single tape TM
If a language, L is accepted by a multitape TM, then it is accepted by a single tape TM.
Proof
Let M1, be a multitape turing machine with k-tapes that accepts the language, L
Let M2 be a single-tape, multi – track turing machine simulated by M1.
The number of tracks on M2 is double the number of tapes of M1 [2 x k tracks]
Thus M2 consists of two tracks for every tape in M1.
At k = 1, M2 has 2 tracks
o The first track records the contents of M1
o The second track (initially kept as blank, B) holds the symbol processed by M1.
This set-up can be further extended to k-tape TM as,
Head 1 X
Tape 1 a1 a2 … … am
Head 2 X
Tape 2 b1 b2 … … bm
Head k X
Tape k 1
2
… … m
M1 k : k – tapes TM
Track 1 Track 2 Track 3 Track 4 Track 5 Track
M2 : 2k tracks TM
To prove that L(M1) = L(M2), let us consider k = 2 (lower most possible n-tape TM)
and prove the above
… … B … B bm … b2 b1 B … B am … a2 a1
4.9
3
Theory of Computation
Further k shall be extended to get the general case.
Let
M1 = (Q1, ∑, 1, 1, q1, B, F1)
M2 = (Q2, ∑, 2, 2, q2, B, F2)
such that
1 2 and ∑ 1 2
The tape symbol of m2 (M2) includes,
= 1 B ordinary symbols
Elements of
NON DETERMINISTIC TURING MACHINE
Non determinism is a powerful feature of Turing machine.
These NTM machines are easy to design and are equivalent to deterministic TM.
A NTM accepts a string, if there exists a least one sequence of moves from the
initial state to final state.
Definition
A NTM is defined as
M = (Q, ∑, , , q0, B, F))
where,
Q Set of states including initial, having rejecting states.
∑ Finite set of input alphabets
Finite set of tape symbols
Transition function defined by
: Q P(Q {L, R, N}) where P power set
q0 initial state
B blank symbol
F set of final states (F ≤ Q)
The transition function takes on the states tape symbols and head movement.
Properties of Context Free Languages 4.94
q0 a / x, R
q1
Example
a / a, R
The above transition takes on two paths for the same input, a
The transition of „a‟ at q0 is defined as,
(q0, a) = {(q0, a, R), (q1, x, R)}
UNIT - 5
UNDECIDABILITY
RECURSIVE AND RECURSIVELY ENUMERABLE LANGUAGES
Recursively Enumerable Language
A language L *
is recursively enumerable if there exist a Turing machine, M that
accepts every string, w Land does not accept strings that are not in L.
If the input string is accepted, M halts with the answer, “YES”.
If the string is not an element of L, then M may not halt and enters into infinite loop.
w є L
w L
YES
Loops Forever
The language, L is Turing Acceptable.
Recursive Language
A language is said to be recursive if there exists of Turing machine, M that accepts
every string, w L and rejects those strings that are not in L.
If the input is accepted, M halts with the answer,” YES”
w є L
w L
F the Turing machine doesn‟t accept the string.
YES
NO
If w L, then M halts with answer, “NO”. This is also called as Turing Decidable
language.
PROPERTIES OF RECURSIVE AND RE LANGUAGES
1. The Union of two recursively enumerable languages is recursively enumerable.
M
M
5.2 Theory of Computation
M1
M2
M3
2. The language L and its complement L are recursively enumerable, then L and I are recursive.
3. The complement of a recursive language is recursive.
4. The union of two recursive language is recursive.
5. The intersection of two recursive language is recursive.
6. The intersection of two recursively enumerable language is recursively enumerable
Proofs on the Properties
Property-1
The union of two recursively enumerable languages is recursively enumerable.
Proof:
Let L1 and L2 be two recursively enumerable languages accepted by the Turing
machines M1 and M2.
If a string w L1 then M1 returns “YES”, accepting the input string: Else loops
forever. Similarly if a string w L2 then M2 returns “YES”, else loops forever.
The Turing machine M3 that performs the union of L1 and L2 is given as
RE
YES
YES
RE YES
RE
Here the output of M1 and M2 are written on the input tape of M3. The turning
machine, M3 returns “YES”, if at least one of the outputs of M1 and M2 is “YES”. The M3
decides on L1UL2 that halts with the answer, “YES” if
forever if both M1 and M2 loop forever.
w L1 or w L2 . Else M3 loops
Hence the union of two recursively enumerable languages is also recursively
enumerable.
wєΣ*
Undecidability 5.3
M2
Property – 2
A language is recursive if and only if both it and its complement are recursively
enumerable.
Proof
Let L and L be two recursively enumerable languages accepted by the Turing
machines M1 and M2. If a string, w L, it is accepted by M1 and M1 halts with answer
“YES”. Else M1 enters into infinite loop.
If a string, w L w L , then it is accepted by M2 and M2 halts with answer “YES”.
Otherwise M2 loops forever.
The Turing machine, M3 that simulates M1 and M2 simultaneously is given as
w L1
YES
w L YES
From the above design of TM, if w L, if w L, then M1 accepts w and halts with
“YES”.
If w L, then M2 accepts ww L and halts with “YES”.
Since M1 and M2 are accepting the complements of each other, one of them is
guaranteed to halt for every input, wєΣ*.
Hence M3 is a Turing machine that halts for all strings.
Thus if the language and its complement are recursively enumerable, then they are
recursive.
Property - 3
The complement of a recursive language is recursive.
Proof
Let L be a recursive language accepted by the turning machine, M1.
Let L be a recursive language accepted by the Turing machine M2.
wєΣ*
M3
M1
YES
5.4 Theory of Computation
M2
M1
The construction of M1 and M2 are given as,
w L
L YES NO
w L NO YES
R R
Let w L, then M1 accepts w and halts with “YES”.
M1 rejects w if w Land halts with “NO”
M2 is activated once M1 halts.
M2 works on L and hence if M1 returns “YES”, M2 halts with “NO”.
If M1 returns “NO”, then M2 halts with “YES”
Thus for all w, where w L or w L , M2 halts with either “YES” or “NO”
Hence the complement of a recursive language is also recursive.
Property – 4
The union of two recursive language is recursive.
Proof:-
Let L1 and L2 be two recursive languages that are accepted by the Turing machines M1
and M2, given by
L(M1) = L1
L(M2) = L2
Let M3 be the Turing machine constructed by the union of M1 and M2. M3 is
constructed as follows.
YE
NO
YES
NO
wєΣ*
M3
YES
NO
M2
M1
Undecidability 5.5
3
The Turing machine M3 first simulates M1 with the input string, w.
If w L1 , then M1 accepts and thus M3 also accepts since L(M3) = L(M1) u L(M2).
If M1 rejects string w L1 , then M3 simulates M2. M3 halts with “YES” if M2
accepts „w‟, else returns “NO”.
Hence M3, M2, M1 halt with either YES or NO on all possible inputs.
Thus the union of two recursive languages is also recursive.
Property – 5
The intersection of two recursive language is recursive.
Proof:-
Let L1 and L2 be two recursive languages accepted by M1 and M2 where
L(M1) = L1
L(M2) = L2
Let M3 be the Turing machine that is constructed by the intersection of M1 and M2, M3
is constructed as follows.
wєΣ*
NO
YES
NO
M LM3 L(M1) L(M2 )
YES YES
NO
The Turing machine M3 simulates M1 with the input string,w.
If w L1 , then M1 halts along with M3 with answer “NO”, since L(M3)=L(M1)
L(M2). If then M1 accepts with the answer “YES” and M3 simulates M2.
If M2 accepts the string, then the answer of M2 and M3 are “YES” and halts. Else, M2
and M3 halts with answer “NO”.
M2
M1
5.6 Theory of Computation
YES
M1
YES
M2
M3
Thus, the intersection of two recursive languages is recursive.
Property – 6
Intersection of two recursively enumerable languages is recursively enumerable.
Proof:-
Let L1 and L2 be two recursively enumerable languages accepted by the Turing
machine M1 and M2.
If a string
rejecting w L1 .
w L1 then M1 returns “YES” accepting the input. Else will not halt after
Similarly if a string, w L2 , then M2 returns “YES” else rejects „w‟ and loop forever.
The Turing machine, M3 = M1 M2 is given as
w L1 Rejects & Never Halts
YES
Loop Forever
w L2
Rejects & Never Halts
Here the output of M1 and M2 are written the input tape of M3. The machine, M3
returns “YES” if both the outputs of M1 and M2 is “YES”.
If at least one of M1or M2 is NO it rejects „w‟ and never halts.
Thus M3 decides on L1 L2 that halts if and only if
loops forever along with M1 or M2 or both
w L1 and w L2 . Else M3
Hence the intersection of two recursively enumerable languages is recursively
enumerable.
THE HALTING PROBLEM
The halting problem is the problem of finding if the program/machine halts or loop
forever.
The halting problem is un-decidable over Turing machines.
wЄ∑*
Undecidability 5.7
Description
Consider the Turing machine, M and a given string , the problem is to determine
whether M halts by either accepting or rejecting , or run forever.
Example
while (1)
{
prinf(“Halting problem”);
}
The above code goes to an infinite loop since the argument of while loop is true
forever.
Thus it doesn‟t halts.
Hence Turing problem is the example for undecidability.
This concept of solving the halting problem being proved as undecidable was done
by Turing in 1936.
The undecidability can be proved by reduction technique.
Representation of the halting set
The halting set is represented as,
h(M, ) 1 if M halts on input
0 otherwise
where,
Theorem
M Turing machine
Input string
Proof
Halting problem of Turing machine is unsolvable / undecidable.
The theorem is proved by the method of proof by contradiction.
Let us assume that Turing machine is solvable / decidable.
Construction of H1
M
HALT
LOOP FOREVER
Halting
machine H1
5.8 Theory of Computation
H2
Halting
machine H2
Consider, a string describing M and input string, for M.
Let H1 generates “halt” if H1 determines that the turing machine, M stops after
accepting the input, .
Otherwise H1 loops forever when, M doesn‟t stops on processing .
Construction of H2
M HALT
LOOP
H2 is constructed with both the inputs being M.
H2 determines M and halts if M halts otherwise loops forever.
Construction of H3
M HALT
LOOP
LOOP
HALT
Let H3 be constructed from the outputs of H2.
If the outputs of H2 are HALT, then H3 loops forever.
Else, if the output of H2 is loop forever, then H3 halts.
Thus H3 acts contractor to that of H2.
H3
Let the output of H3 be given as input to itself.
If the input is loop forever, then H3 acts contradictory to it, hence halts.
And if the input is halt, then H3 loops by the construction.
Since the result is incorrect in both the cases, H3 doesnot exist.
Thus H2 doesnot exist because of H3.
Similarly H1 doesnot exist, because of H2.
Thus halting problem is undecidable.
H3
H3
Undecidability 5.9
PARTIAL SOLVABILITY
Problem types
There are basically three types of problems namely
Decidable / solvable / recursive
Undecidable / unsolvable
Semi decidable / partial solvable / recursively enumerable
Decidable / solvable problems
A problem, P is said to be decidable if there exists a turing machine, TM that
decides P.
Thus P is said to be recursive.
Consider a Turing machine, M that halts with either „yes‟ or „no‟ after computing the
input.
∑*
YES (if L)
NO (if L)
The machine finally terminates after processing
It is given by the function,
F () 1 if p()
p 0 if p()
The machine that applies Fp() is said to be turing computable.
Undecidable problem
A problem, P is said to be undecidable if there is a Turing machine, TM that doesn‟t
decides P.
Semi decidable / partial solvable / recursively enumerable
A problem, P is said to be semi decidable, if P is recursively enumerate.
A problem is RE if M terminates with „YES‟ if it accepts L; and doesn‟t halt if
L. Then the problem is said to be partial solvable / Turing acceptable.
∑* YES (if L)
LOOP FOREVER (if L) M
M
5.10 Theory of Computation
Partial solvability of a machine is defined as,
F () 1 if p()
p undefined if p()
Enumerating a language
Consider a k-tape turing machine. Then the machine M enumerates the language L
(such that L ∑*) if
The tape head never moves to the left on the first tape.
No blank symbol (B) on the first tape is erased or modified.
For all L, where there exists a transition rule, i on tape 1 with contents
1 # 2 # 3 # ... # n # # (for n 0)
Where 1, 2 , 3 , ..... , n , are distinct elements on L.
If L is finite, then nothing is printed after the # of the left symbol
That is,
If L is a finite language then the TM, M either
o Halts normally after all the elements appear on the first tape (elements are processed)
or
o Continue to process and make moves and state changes without scanning/printing other string on the first tape.
If the language, L is finite, the Turing machine runs forever.
Theorem
A language L ∑* is recursively enumerable if and only if L can be enumerated by
some TM.
Proof
k(M1)].
Let M1 be a Turing machine that enumerates L.
And let M2 accepts L. M2 can be constructed as a k-tape Turing machine [k(M2) >
M2 simulates M1 and M1 pauses whenever M2 scans the „#‟ symbol.
Undecidability 5.11
M2
M2 compares its input symbols to that of the symbols before „#‟ while, M1 is in pause.
If the comparison finds a match of the string, M2 accepts L.
Here M2 is a semi acceptor TM for L
Scans the input string,
Runs the transition rules of M1
If M1 outputs , then is accepted and M1 hats
If L, M1 will output and M2 will eventually accept „‟ and halts.
If L, then M1 will never provide an output and so M2 will never halt.
∑*
∑* HALTS
(L)
Infinite loop
HALTS
( L)
LOOP FOREVER
( L)
Never halts
Thus M2 is partially solvable / Turing acceptable for L.
POST CORRESPONDENCE PROBLEM
Post correspondence problem, known as PCP is an unsolvable combinatorial problem.
This Undecidable problem was formulated by Emil Post in 1946.
Since PCP is simpler than halting problem, this is used in proofs of undecidability /
un-solvable problems.
PCP definition
The input instance of the problem has two lists of non-null strings {α1, α2, ........... αn}
and {β1,β2, ................ βn} over an alphabets, Σ. [Σ having at most two symbols].
The task is to find a sequence of integers i1, i2, … ik [ ik for 1 ≤ k ≤ N ] such that
αi, αi2, ……αik = βi1, βi2 = βik
The PCP is a decision problem that whether the above mentioned sequence exists or
not.
M1 LOOP FOREVER
( L)
M2
M1
5.12 Theory of Computation
EXAMPLE
1. For Σ = {a, b} with A = {a, aba3, ab} and B = {a
3, ab, b}, Does the PCP with A and B
have a solution?
Solution:
The sequence obtained from A and B = (2, 1, 1, 3) as,
A2 A1 A1 A3
aba3 a a ab
B2 B1 B1 B3
ab a3 a3 b
Thus A2A1A1A3 = B2B1B1B3 = aba3a
3b = aba
6b
The PCP given has a solution (2,1,1,3) with the two lists of elements.
2. Let Σ = {0, 1}. Let A and B be the lists of three strings defined as
A B
I wi xi
1 1 111
2 10111 10
3 10 0
Solution:
Consider the sequence (2, 1, 1, 3)
A2A1A1A3 => w2w1w1w3 = 101111110
B2B1B1B3 => X2X1X1X3 = 101111110
Thus the PCP has (2, 1, 1, 3) sequences as solution
RICE‟S THEOREM
Rice‟s theorem is named after the inventor Henry Gordon Rice.
This theorem is also called as Rice- Myhill – Shapiro theorem.
Theorem
If R is a property of languages that are satisfied by some but not all recursively
enumerable languages, then the decision problem,
Undecidability 5.13
PR : Given a TM,M, does L(M) have property, R?
is unsolvable.
Explanation
Consider a language L such that
L = {M | L(M) has some property P}
where,
P is non-trivial of TM,M
Proof
Then the language L is undecidable.
That is, every non-trivial property of the language of Turing machines is Undecidable
Consider a Turing machine, that recognizes empty language, that does not have the
property, P. If has the property, P, then complement of P is considered
Thus the language is Undecidable due to complementation.
To arrive at a contradiction, assume that P is decidable
Then there is a Turing machine B that recognizes and halts on processing the
descriptions of B that satisfy P
Using the Turing machine, M another Turing machine, A is constructed that accepts
the language {(M, w) | M) is the Turing machine that accepts w.
As M is Undecidable, then B cannot exist and thus the problem P must be Undecidable
Let MP be a Turing machine that satisfies P [ P → non trivial] A is computed as
(i) On the input x, M run on the string, w until it accepts. If it doesn‟t accept, C (M,w)
will run forever.
(ii) Run MP on x A accepts if and only MP accepts.
Here C(M, w) accepts the same language as Mp if M accepts w. C(M, w) accepts
empty language , if M does not accept w.
If M accepts w, C(M, w) has the property, P, otherwise, C(M, w) is passed to B
If B accepts, C(M, w) accept the input (M, w) If B rejects, C(M, w) reject
5.14 Theory of Computation
UNIVERSAL TURING MACHINE
Motive of UTM
A single Turing machine has a capability of performing a function such as addition,
multiplication etc.
For computing another function, other appropriate Turing machine is used. To do so, the
machine has to be re-written accordingly.
Hence Turing proposed “Stored Program Computer” concept in 1936 that executes the
program/instructions using the inputs, stored in the memory.
The instructions and inputs are stored on one or more tapes.
Concept of UTM
The universal Turing machine,
the program.
Tu takes over the program and the input set to process
The program and the inputs are encoded and stored on different tapes of a multi-tape
Turing machine.
The Tu thus takes up T, w where T is the special purpose Turing machine that passes
the program in the form of binary string, w is the data set that is to be processed by T.
Input
Tape of M
State of M 0000…0BB…
Encoding T and w
The encoding function, “e” is used to encode both T and w to obtain e(T) and e(w).
The encoding is applied such that the reconstruction of e(T) to T and e(w) to w should
also possible without any loss of data.
The encoding function „e‟ is given as,
S(ε) = 0
<T, w>
0001010000101….
Finite
control
Undecidability 5.15
i
i
q0 / , R
q1 a / b, L
/ , L q2
/ , S q3
S(a ) 0i1
S(ha ) 0
S(hr ) 00
S(q ) 0i2
S(S) 0
S(L) 00
S(R) 000
ai S S input symbols / terminals
ha haltingstate
hr Rejecting / Crashstate
qi set of states
S stop
L Left
R Right
We know that the transition move of a Turing machine
(qi , T) (qi , T, M) qi, qj є Q
T → terminal symbol
Which is encoded as, M → movement of head
e(m) 1s(qi)1s(T)1s(q j)1s(T)1s(M)1
Where e(m) → encoded transition, m.
e(w) 1s(w1) S(w2 ) S(w3 ) ........ S(wn )
where e(w) → encoded input, w
For any Turing machine, T,
e(T) s(q) e(m1) e(m2 ) ....... e(mk )
where
Example
q → initial state
m → transition of TM
b / b, R b / b, L
The initial state = {q0}
Final state = {q3}
5.16 Theory of Computation
Inputs = {a,b}
Moves = {L,R,S}
States = {q0,q1,q2,q3}
The code for, q0- first state
q0 → 000 [q since S(qi)=0i+2
S(q0)=01+2
]
Status q1→0000, [S(q1)=02+2
, since q → second state]
q2→00000 [S(q2)=03+2
, since q2 → third state]
q3→0 [since q3→ halt state, ha]
→ 0 [since s() = 0]
Inputs a → 00 [since a(ai) = 0i+1; a → first input; so i = 1 s(a) = 01+1
]
b → 000 [s(b) = 02+1
, since b → second input]
s → 0 [since s(s) = 0]
Moves L → 00 [since s(L) = 00]
R → 000 [since s(R) = 000]
The transition of the above given Turing machine are,
1.(q0 , e) (q1,, R)
2.(q1, b) (q1, b, R)
3.(q1, a) (q2 , b, L)
4.(q1,) (q2 ,, L)
5.(q2 , b) (q2 , b, L)
6.(q2 ,) (q3 ,,s)
The binary string after applying encoding function
1.(q0 ,) (q1,, R) 100010100001010001
2.(q1, b) (q1, b, R) 10000100010000100010001
3.(q1, a) (q2 , b, L) 1000010010000010001001
Undecidability 5.17
4.(q1,) (q2 ,, L) 1000010100000101001
5.(q2 , b) (q2 , b, L) 100000100010000010001001
6.(q2 ,) (q3 ,,s) 100000101010101
TM q0 , I, w
Not specified here
0001000101000010100011000010001000010001
0001100001001000001000100110000101000001
010011000001000100000100010011000001010101011
Input to the Tu
The universal Turing machine, Tu is always provided with the code for Transitions,
e(T) and code for input, e(w) as
TM e(T)e(w)
For example, if the input data, w=”baa”, then
e(w) = 10001001001
This e(w) will be appended to e(T) of Tu.
Construction of Tu
As in the figure for universal Turing machine, there are three tapes controlled by a
finite control component through heads for each tape.
Tape -1 Input tape and also serves as output tape. It contain e(T) e(w).
Tape-2 Tape of the TM/Working tape during the simulation of TM
Tape -3 State of the TM, current state of the T in encoded form.
Operation of UTM
UTM checks the input to verify whether the code for TM=<T,w> is a legitimate
for some TM.
o If the input is not accepted, UTM halts with rejecting, w
Initialize the second tape to have e(w), that is to have the input, w in encoded form.
5.18 Theory of Computation
Place the code of the initial state on the third tape and move the head of the finite
state control on the first cell of second tape.
To simulate a move of the Turing machine, UTM searches for the transition -
oi 1o
j 1o
k 1o
l 1o
m on the first tape, with o
i (initial state/current state) on tape -3 and
o j (input symbol to be processed) on tape- 2.
The state transition is done by changing the tape -3 content as
transition.
Replace o j by o on tape-2 to indicate the input change.
ok as in the
Depending on om
[m=1 stop, m=2 Left, m=3 Right], move the head on
tape-2 to the position of the next 1 to the left/right/stop accordingly
If TM has no transition, matching the simulated state and tape symbol, then no
transition will be found. This happens when the TM stops also.
If the TM, T enters ha(accepting state), then UTM accepts the input, w
Thus for every coded pair <T, w>, UTM simulates T on w, if and only if T accepts the
input string, w.
Definition of Universal Language [Lu]
The universal language, Lu is the set of all binary strings , where represents the
ordered pair <T, w> where
T Turing machine
w any input string accepted by T
It can also be represented as = e(T) e(w) .
Theorem
Lu is the recursively enumerable but not recursive .
Proof
From the definition and operations of UTM, we know that Lu is recursively
enumerable.
Lu accepts the string w if it is processed by the TM,T. Else, rejects „w‟ and the
machine doesn‟t halts forever.
To prove that Lu is not recursive, the proof can be done by contradiction. Let Lu is
Turing decidable [recursive], and then by definition
acceptable.
Lu (complement of Lu) is Turing
Undecidability 5.19
We can show that Lu is Turing acceptable, that leads to Ld to be Turing acceptable.
But we know that Ld is not Turing acceptable.
Hence Lu is not Turing decidable by proof by contradiction.
Proof on Lu is during acceptable Ld is Turing acceptable
w
Suppose “A” is the algorithm that recognizes Lu .
Then Ld is recognizes as follows. Given a string w (0,1)* determined easily, the
value of I such that w = wi.
Integer value, I in binary is the corresponding code for TM, Ti. Provide <Ti, wi> to the
algorithm A and accept, w if and only if Ti accepts wi.
So the algorithm accepts w if and only if w = wi which is in L(Ti).
This is the algorithm for Ld. Hence Lu is Recursively Enumerable but not recursive.
TRACTABLE AND INTERACTABLE PROBLEMS
Tractable Problems/Languages
The languages that can be recognized by a Turing machine in finite time and with
reasonable space constraint is said to be tractable.
nature
Example: If the language L1 Time (f), then L is tractable and is less complex in
Example: If L2 Time (f), L2 is complex and cannot be tractable in limited time.
Tractable problems are those that can be solved in polynomial time period.
Intractable Problems
The languages that cannot be recognized by any Turing machine with reasonable space
and time constraint is called intractable problems.
These problems cannot be solved in finite polynomial time. Even problems with
moderate input size cannot achieve feasible solution
Accept Accept
w111w
Reject Reject T for Ld
Copy
Hypothetical
algorithm
T for Lu
5.20 Theory of Computation
Terminologies
Polynomial Time, PSet
The set of languages that can be recognized by a Turing machine with polynomial time
complexity.
It is given by,
P UTime(Cnk )
c>0, k>0
Polynomial Space, PSpace Set
The set of languages that can be recognized by a TM with polynomial space
complexity is said to be PSpace Set.
It is given by,
PSpace U Space(Cnk )
c>0, k 0
Non-Deterministic Polynomial Time, NP
The set of languages that are recognized by a Nondeterministic TM with polynomial
time is said to be NP.
It is given by,
NP U NTime(Cnk )
c>0, k 0
Non-deterministic Polynomial Space, N Space
The set of languages that can be recognized by a NDTH with polynomial Space
complexity
It is given by,
NP U NSpace(Cnk )
c>0, k 0
Example: The CNF satisfiability Problem
Satisfiability problem is also called as Boolean Satisfiability /Proportional
satisfiability/SAT problem.
Undecidability 5.21
The SAT problem is to find if there is any interpretation that satisfies the given
Boolean equation.
It is to determine the variables of a given boolean formula can be replaced by „TRUE‟
or „FALSE‟ in a form that evaluates the formula to „TRUE‟.
If „TRUE‟ is obtained, the Boolean equation is said to be satisfiable.
If there is no assignment obtained and the evaluation gives out „FALSE‟, then the
Boolean equation is said to be unsatisfiable.
Example: Let a= TRUE
b=FALSE
a AND b FALSE
a AND NOT a FALSE
a AND NOT b TRUE
a OR b TRUE
The operator „AND‟ is denoted by conjuction operator ( )
The OR operator is given by disjunction (V).
NOT operator is denoted by negation ( ) operator
Example: a b FALSE
a (a) FALSE
a (b) TRUE
a b TRUE
Terminologies
The variable used in the Boolean expression is called a literal. Literals can be either
positive or negative.
The normal variable is called positive literal.
The negation of a variable is called negative literal.
The Boolean clause is said to be Horn clause if it contains at most one positive literal.
A formula is in CNF (Conjugative normal form) if it contains/formed by a conjunction
of single classes.
5.22 Theory of Computation
Example: x1 Positive literal
x2 Positive literal
x1 V x2 Clause
x2 V x2 V x3 Not a horn clause
x1
x1 Vx2
horn clause
x1 ( x1 x2 x3 ) (x1 x2 ) CNF
Thus CNF is a conjuction of clauses to form a formula.
C1 C2 C3 Cn CNF
where,
C1 , C2 , C3 Conjuncts / clauses.
K-satisfiability
Generalized form of CNF with each clause containing upto „k‟ literals.
Encoding instances of CNF SAT:-
The instances of CNF SAT can be encoded as,
(x1 x2 ) (x2 x3 x1) (x4 x2 )
x1x11 x11x111x1 x1111x11
The encoding scheme omits parenthesis and OR (V) notation. The variables are
denoted by unary notation of their subscripts.
Thus CNF-Satisfiability is the language over ,1, x, x containing encodings
for all positive literals of the instances in CNF SAT.
When there are k-literals, c-conjucts, v-distinct literals, and n is the length of the string
encoding these instances,
n k (V 1) k2 2k
If CNF – satisfiable NP , then the decision problem CNF-SAT is in NP.
Undecidability 5.23
P AND NP COMPLETENESS
Problems are classified into two, namely P and NP.
P
„P‟ refers to the class of problems that can be solved in polynomial time.
Example: Searching an element in an ordered list, sorting elements in a lists,
Multiplication of integers , finding all- pair –shortest path, finding minimum
spanning tree of a graph, etc
These are also referred as tractable problems
Polynomial – time algorithms are efficient ones that can be solved more rapidly.
NP
„NP‟ refers to the class of problems that are solved by non-deterministic
polynomial time
These types of NP problems are known as interactable problems.
Example: Towers of Hanoi, Traveling Salesman problem, Graph colouring
problem, Hamiltonian Circuit problem, Satisfiability Problem, etc.
If a problem, P is solvable in polynomial time by NDTM, then there is no
guarantee that there exists a Deterministic TM that can solve P in polynomial time.
If P is a set of tractable problem, then P NP
Every deterministic TM is a special case of case of NDTM.
NP complete Problem – Types:-
The NP type class of problems are classified into
o NP – COMPLETE
o NP – HARD
NP- Complete Problem
A problem is said to be NP- complete if it belongs to NP class problem and can be
solved in polynomial time.
They are also called polynomial -time reducible problems.
A NP-complete problem can be transformed into any other in polynomial time.
5.24 Theory of Computation
NP-Hard Problem
A problem is said to be NP hard if there exists an algorithm for solving it and it can
be translated into one for solving another NP-Problem.
A Problem P1 is NP-hard if
o the problem is an NP class problem
o For any other problem , P2 in NP, there is a polynomial time reduction of L2 to L1
Every NP complete problem must be NP- hard problem
Undecidability 5.25