Toc Notes
Toc Notes
COURSE MATERIAL
II YEAR - IV SEMESTER
L T P C
UCSC015 COMPUTATIONAL THEORY
3 1 0 4
Course Objectives
Understand various Computing models like Finite State Machine, Pushdown Automata, and
Turing Machine.
Be aware of Decidability and Un-decidability of various problems. Learn types of grammars.
Course Content
Finite Automata
Introduction- Basic Mathematical Notation and techniques- Finite State systems – Basic
Definitions – Finite Automaton – DFA & NDFA – Finite Automaton with €- moves –
Regular Languages- Regular Expression – Equivalence of NFA and DFA – Equivalence of
NDFA’s with and without €-moves – Equivalence of finite Automaton and regular
expressions –Minimization of DFA- Pumping Lemma for Regular sets – Problems based on
Pumping Lemma.
Grammars
Pushdown Automata
Turing Machines
What is TOC?
In theoretical computer science, the theory of computation is the branch that deals
with whether and how efficiently problems can be solved on a model of computation,
using an algorithm. The field is divided into three major branches: automata theory,
computability theory and computational complexity theory.
In order to perform a rigorous study of computation, computer scientists work with a
mathematical abstraction of computers called a model of computation. There are
several models in use, but the most commonly examined is the Turing machine.
Automata theory
In theoretical computer science, automata theory is the study of abstract machines (or
more appropriately, abstract 'mathematical' machines or systems) and the computational
problems that can be solved using these machines. These abstract machines are called
automata.
This automaton consists of
states (represented in the figure by circles),
and transitions (represented by arrows).
As the automaton sees a symbol of input, it makes a transition (or jump) to another
state, according to its transition function (which takes the current state and the recent
symbol as its inputs).
Uses of Automata: compiler design and parsing.
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.
PUSH
Start
OFF ON
PUSH
1.2 Theory of Computation
Here,
When the electric switch = “ON” indicates logic „1‟ when pushed from start state.
t h e n
t th the then
a u t o m a t
a au aut auto autom automa automat
a automata
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
Automata Fundamentals 1.3
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}
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 B =B A
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 [ Ak ]
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.
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 pq 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 pq
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 x2
Conclusion statement 2x x2
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.
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.
Proof about the set includes the proofs on the properties of the sets.
Consider, A = P Q, B = Q P
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:
xϵPQ
Given a statement, that can be proved to be true for a domain of input instances, and
false for other inputs.
Let a = 5 and b = 10
a % b = 5 % 10 = 5
b % a = 10 % 5 = 0
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
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 1 2 2
R.H.S 1
2 2 2
1.10 Theory of Computation
Inductive Step
n n 1
We have 1 2 3 n
2
Sub n = n + 1
n
L.H.S 1 + 2 + 3 +…… + n = i
i1
= i (n 1)
i1
n(n 1)
= (n 1)
2
n(n 1) 2(n 1)
=
2
n2 n 2n 2
=
2
n2 3n 2
= ----- (1)
2
n(n 1)
R.H.S =
2
When we substitute n=n+1,
(n 1)((n 1) 1)
R.H.S =
2
(n 1)(n 2)
=
2
n2 2n n 2
=
2
Automata Fundamentals 1.11
n2 3n 2
= ----- (2)
2
From (1) and (2), L.H.S = R.H.S
So the given hypothesis is proved.
i1 6
Basis of induction
Let n=1 (we can‟t take n=0 since i=1 in L.H.S)
n
L.H.S = i
i1
2
= 12 = 1
Inductive step
n
n(n 1)(2n 1)
i
i1
2
6
n 3 2
= i2 (n 1)2
i1
i2 i2 (3)2
i1 i1
n(n)(2n 1)
= (n 1)2
6
(n 1)(n 2)(2n 2 1)
=
6
(n2 2n n 2)(2n 3)
=
6
3) P.T for every integer, n 0 the number 42n1 3n2 is a multiple of 13.
Solution
Basis of induction
Let n = 0
= 41 32 4 9
= 13 multiple of 13
Inductive proof
Let n=n+1
= 42n21 3n3
Automata Fundamentals 1.13
= 42n3 3n3
= 42n1.42 3n2.31
= 16(42n1 ) 3(3n2 )
Multiple of 13
= 13(42n1 ) 3(13n)
n4 4n2 0 0 0 3 0 Divisible by 3
Hence proved.
Inductive step
Let us assume that n4 4n2 is divisible by 3 for n, and try to prove the same for n=n+1
4n3 6n2 4n 3
Divisible by 3
FINITE AUTOMATA
Basic Definitions
Alphabet ( )
Example:
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.
0
0
a, b, c
1
aaa, bbb, ccc, aab, aac, aba, aca, abb, acc, abc, acb, bba, bbc, bab, bcb, baa, bcc,.....
3
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, * denotes the set of all strings over
the alphabet,
1.16 Theory of Computation
Example:
2) Let 1
0
1
1
11
2
* 0
1 2 .....
,1,11,111,...
Example:
1 ,0
1
*
Substring
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
Palindrome
A palindrome is a string which is same when read in backward or forward direction.
R is a palindrome
Example: 1001
Language (L)
Alphabet finite set of symbols
Example:
1) {a, b}
2) {0,1}
L{Set of stringsending with11}
= {ε, 11, 011, 0011, 1011, 0111, 101111, …}
| xy || x | | y |
y101
Then xy10101Concatenation.
| xy || x | | y |
23
5
Operations of Languages
1. Product or concatenation (L1 L2 )
L1{b, ab, aab, ... }{an b, n 0}
2. Reversal (LR )
The reversal of a language is a set of all string reverses.
LR {wR / wL}
Model of FA
a1 a2 a3 a4 … Infinite tape
Read Head
Finite
CPU
control
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.
q0 Start state
F Set of final states. [FQ]
Representation of FA
1. Transition Diagram
2. Transition Table
1.20 Theory of Computation
Transition Diagram
It can be directed graph with vertices of a graph corresponding to states and edges
indicates transition from one state to another
0 1
q0 q1 q2
Start
Final
state
state
Start state indicated by circle.
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
a b
q0 q1 q2
State a b
(q0 , a) q1
Input
(q 0 , b) q 0
q0 q1 q0
(q1 , b) q 2
q1 q2
*q2
Applications
1. Design of digital circuits.
2. String searching
3. Communication protocols for information exchange.
4. Lexical analysis phase of a compiler.
Types
1. Deterministic Finite Automata [DFA]
2. Non-Deterministic Finite Automata [NFA]
Automata Fundamentals 1.21
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 0La 0 b b n 3La 3 b aaab
n 1La 1 b ab n 4La 4 b aaaab
n 2La 2 b aab n 5La5 b aaaaab
a
b
q0 q1
{a, b}
q0 {q0 }
Transition function,
(q0 , a) {q0 }
1.22 Theory of Computation
(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}
a b a c
q0 q1 q2 q3 q4 Valid input: abac
Trap
3. Design a DFA that accept L {w (0, 1)*, where every 0 in w has 1 immediately to
its right.
0 1 Valid input
q0 q1 q2 01
0 1
q0 q1 q2 0101
0
1 1
101, 1101
0 1
q0 q1 q2 Invalid inputs:
0 00, 10, 1100, 1001, ….
0
Trap q2
0, 1
1 0 1
q0 q1 q2 q3 Invalid inputs:
00, 100, 010, 1010, …
1 0
0 0, 1
q4
Trap 0, 1
a b Valid inputs:
q0 q1 q2 101
a, b
a b aba, abb, abaa, abbb,
q0 q1 q2
abab, ….
b a, b Invalid inputs:
a
q0 q1 q2 ba, aa, aab, bab, aaaa,
baab, ….
b a
q1
Trap
a, b
6. Design a DFA that accepts all strings of length atmost 5 over {0, 1}
Valid inputs:
0, 00, 000, 0000,
0, 1 0, 1 0, 1 0, 1 0, 1 00000, 1, 11, 111,
q0 q1 q2 q3 q4 q5
1111, 1111, 10101,
010, 100, 0010, …
0, 1 0, 1 0, 1 0, 1 0, 1
q0 q1 q2 q3 q4 q5 Invalid inputs:
101010, 0000000,
11110000,
0, 1 100011001,
q1 1111111111, …
Trap
0, 1
1.24 Theory of Computation
a a Valid inputs:
q0 q1 q2
aa
a a
q0 q1 q2
aaaa, aaaaaa, a8, a16, ...
a
a q3 a
Invalid input
q0 q2 a, aaa, aaaaa, a7, a17, ...
a a
q1
8. Design a DFA that accepts a language, L having even number of 0s and 1s.
0 Valid inputs:
q2 q1 00, 11, 0000, 1111, 08, 18, …
0
1 1
q1
0
q2 q1 0110, 0101, 1010, 1100, 110011,
0 10101010, 00001111, ...
1 1 1 1 Invalid inputs
0 0, 1, 000, 111, 101010, 110, 1101,
q1 0100, …
q1 0
Automata Fundamentals 1.25
9. Design a DFA that accepts all strings containing atleast two zeroes.
Valid inputs:
0 0
q0 q1 q2 00
0, 1
0 0 00, 000, 0000, 00000, 0001,
q0 q1 q2
000111, …
1
0, 1
0 0 100, 1100, 11100000,
q0 q1 q2
110011, 1001, 11000010, …
10. Design a DFA that accepts strings ending with „00‟ over {0, 1}
Valid inputs:
0 0
q0 q1 q2 00
0
0 0 000, 0000, 00000, …
q0 q1 q2
1
0
q0 0 0 100, 1000, 1100 110000, …
q1 q2
1
0 0100, 101000, 110100, …
0 0
q0 q1 q2
1
1
0 00100, 1000, 1010100, 001000
0 0
q0 q1 q2
1 Invalid input:
1 001, 0011, 101, 10011, 1, 01, ..,
1.26 Theory of Computation
11. Design a DFA that accepts a language, L which has the number of zeroes is of
multiples of 3.
Valid inputs:
0 0
q0 q1 q2 000, 000000, 09, 012, …
0
1
0 0
q0 q1 q2 1, 1000, 100000, …
0
1 1
0 0
q0 q1 q2 10100, 11000, 101100, …
0
1 1
1
0 101010, 1101010, 1101110110, …
0
q0 q1 q2 Invalid inputs: 0,00,10000,01011, …
12. Design a DFA that accepts L 01 12j , i 1; j 1
i
1 1
q0 0 1 q3 Valid inputs:
q1 q2 q4
i = 1, j = 1 L = 0111
0 1 1 1 i = 1, j = 2, L = 011111
q0 q1 q2 q3 q4
i = 1, j = 3, L = 01111111
1
i = 2, j = 1 L = 010111
1 1 1
q0 0 q1 q2 q3 q4 i = 3, j = 1, L = 01010111
i = 2, j = 2 L = 01011111
0 1 i = 3, j = 3, L = 010101111111
Automata Fundamentals 1.27
Invalid inputs:
0 1 1 1
q0 q1 q2 q3 q4 1, 00, 0100, 0110, 01110,
1 011110, 1000, 101, …
0 0
0
Trap
0, 1
13. Design a DFA that accepts L a, b / n b mod 3 1
*
Valid inputs:
b b
q0 q1 q2 bb, b5, b8, b11 [No. of b%3]
bb 2b‟s 2% 3 2
b > 1
a bbbbb 5b‟s 5%
3 2 > 1
b b bbb 3b‟s 3% 3 0
q0 q1 q2
> 1
b
abb, abbbbb, ab8, …
a a
a-b2, a3b5, ...
b b
q0 q1 q2 bab, abab, aabbbbb, …
a a a
bba, bbaa, abba, abbaa, ….
b b Invalid inputs:
q0 q1 q2
b, b3, b4, b6, b7, b9, a, aa, aaa, aba,
b baa, ababb
1.28 Theory of Computation
14. Design a DFA accepting strings containing exactly one 1 over {0, 1}
1 Valid inputs:
q0 q1 1
0 0
0 0
1
q0 q1 Invalid inputs:
11
1
Trap q2
0 0
1
q0 q1 0110, 0101, 1111, 101,…
Trap q2 0, 1
15. Design a DFA that accepts all strings with exactly two 1s over {0, 1}
Valid inputs:
1 1
q0 q1 q2 1
0
1 1
q0 q1 q2 011, 0011, 00011, …
0 0
1 1
q0 q1 q2 0101, 1001, 101, …
Automata Fundamentals 1.29
0 0 0
1 1
q0 q1 q2 110, 01010, 1010, 11000 ….
0 0 0
Invalid inputs:
1 1
q0 q1 q2 111, 1110, 0111, 10101, 1111,
1110101, …
1
Trap q3 0, 1
0 0
q0 q1 q2 000, 0000, 00000, …
1 0
0 0
q0 q1 q2 100, 1100, 11000, …
1 0
0 0
q0 q1 q2 10100, 1101100, 101101, ….
1
0, 1
1
0 0 Invalid inputs:
q0 q1 q2
1001, 11001, 101001, …
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:
0 0 0 1
q0 q1 q2 q3 q4 0001
q0 0, 1 0 0 1 1001
q1 q2 q3 q4
0, 1 0 0 1
q0 q1 q2 q3 q4 0011, 1011
0, 1
0, 1 0 0, 1 1 00010, 10010, 00110, 00011,
q0 q1 q2 q3 q4
10011, 00111, …
0, 1
0, 1 Invalid inputs:
0 0, 1 1
q0 q1 q2 q3 q4 0101, 0100, 0010, 1100,
0000, 1000, …
1 0
q5
Trap
0, 1
0, 1 0 0, 1 1
q0 q1 q2 q3 q4 01010, 01000, 11111,
0100000, …
1 0
q5
Trap
0, 1
Automata Fundamentals 1.31
a a a Valid inputs:
q0 q1 q2 q3 aaa, a, aa,
b
a a a
q0 q1 q2 q3 b, ba, baa, baaa, bb, bba, bbba,
…
b b
a a a
q0 q1 q2 q3 bab, abba, abbbaa, babba,…
b b b
a a a
q0 q1 q2 q3 babab, aabb, aaba, bbbaaba, …
b b b b
a a a
q0 q1 q2 q3 aaabbb, aaabab, baaaba, …
b b b b
a a a Invalid inputs:
q0 q1 q2 q3 aaaa, aaabab, baaaba,
a, b q4
Trap
1.32 Theory of Computation
19. Design a DFA that accepts all strings containing substring „101‟.
Valid inputs:
1 0 1
q0 q1 q2 q3 101
0
1 0 1
q0 q1 q2 q3 0101, 000101, …
0 1
1 0 1
q0 q1 q2 q3 011101
0 1 0, 1
1 0 1
q0 q1 q2 q3 10100, 101, 101101, …
0
0 1 0,1
Complementary
1 0 1
q0 q1 q2 q3 [Does not accept
substring with „101‟]
0
20. Design a DFA to check whether a string over {a, b}contains „abb‟ is accepted.
a Valid inputs:
b b
q0 q1 q2 q3 abb
b
a b b
q0 q1 q2 q3 babb, bbabb, …
b a
a b b
q0 q1 q2 q3 aabb, baaabb, …
b a
a b b
q0 q1 q2 q3 ababb, abaabb, bababb, …
a
b a a, b
abba, abbb, abbab
a b b
q0 q1 q2 q3
a Invalid inputs:
ab, bbb, aaaa,abab, aaba,…
Automata Fundamentals 1.33
21. Design a DFA that accepts odd number of ones over {0, 1}
1 Valid inputs:
q0 q1 1
1
q0 q1 111, 11111, 17, 19, 111, …
1
0
1
q0 q1 01, 001, 0000111, …
1
0 0
10, 1011, 10001111, 01101,…
1
q0 q1
Invalid inputs:
1 11, 1111, 0110110, 111100110, …
1
0 0 1
q0 q1 q2 q3 1001, 11001, 111001, …
1 0
0 0 1
q0 q1 q2 q3 0001, 000001, …
1 0 0, 1
0 0 1
q0 q1 q2 q3 00010, 110011, 110010, …
1 0 0, 1
0 0 1
q0 q1 q2 q3 101001, 10101001, …
1
Invalid inputs:
1111, 0111, 0101, 000, …
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, …
q0
Valid inputs:
1 1 1 1
q0 q1 q2 q3 q4 1111
0 0 0 0 0
q0
1 0 0 1 01111, 010100, 01010101,
q1 q2 q3 q4
1111000, 1101100, …
0 0 0 0 0
q0
1 1 1 1 Invalid inputs:
q1 q2 q3 q4
111101, 10101011, 110111, …
1
Trap q5
0 0 0 0 0
q0
1 1 1 1 1111010, 1111011, …
q1 q2 q3 q4
1
q5
0, 1
Automata Fundamentals 1.35
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 ,)q 0
PROBLEMS
1) Consider the transition diagram
b a a, b
a b
q0 q1 q2
2) Consider the FA
a
a
q0 b
q1
b
1.36 Theory of Computation
(q0 , bba)
(q1 , ba)
(q0 , a)
q0 Final state string is accepted
2) 2 aba
(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
a b
a
q0 q1 q2
a, b
1) 1 ababba
PROBLEMS
1) Sketch the NFA state diagram for M = (q 0 , q1 , q 2 , q3 , 0,1 , , q 0 , q 3 ) with given as
∑ 0 1
Q
q0 (q0 , q1 ) (q0 , q2 )
q1 q3
q2 q3
* q3 q3 q3
Solution:
0, 1 0, 1
0 q1 0
q0 q3
1 q2 1
Automata Fundamentals 1.39
2) Design a NFA with no more than five states for the set abab n : n 0 aba n : n 0 .
b
a b a
q0 q1 q2 q3
a
q4
a
a a, b
q0 q1 q2
q0 0
q1
1
0
1 1
q2
0
:
∑ 0 1
Q
q0 q 0 , q1 q 2
q1 q1 , q 2
* q2 q0 , q 2 q1
Solution:
(q0 , 0100)
(q0 , 0) q 0 , q1 (q 0 , q1 , 0) q 0 , q1 q 0 , q1
(q0 , 01) ((q0 , 0),1)
(q 0 , q1 ,1)
1.40 Theory of Computation
q1 , q 2
(q0 , 010) ((q0 , 01), 0)
(q1 , q 2 , 0)
(q1 , 0) (q2 , 0)
q 0 , q 2
q 0 , q 2
(q0 , 0100) ((q0 , 010), 0)
(q 0 , q2 , 0)
(1, b) (2, b)
1 3
1, 3
(1, aba) ((1, ab), a)
(1,3, a)
(1, a) (3, a)
1, 24
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
0 1 0 1
q0 q q q q
1 2 3 4
1.42 Theory of Computation
Q q 0 , q1 , q 2 , q3 , q 4
0,1
q 0 q 0
F q 4
is given as
∑ 0 1
Q
q0 q 0 , q1 q 0
q1 q 2
q2 q 3
q3 q 4
* q4 q 4 q 4
7) Construct a NFA that accepts L x a, b/ x ends with 'aab'
a, b
a a
q0 q1 q2
b q3
M(Q, , , q0 , F) is given as
Q q 0 , q1 , q 2 , q 3
∑ a b
a, b Q
q0 q 0 , q1 q 0
q 0 q 0
q1 q 2
F q3
q2 q3
* q3
Automata Fundamentals 1.43
8) Design a NFA to accept strings over alphabet0, 1, such that the third symbol from
right end is 0.
0, 1
0 0, 1 0, 1
q0 q1 q2 q3
Q q 0 , q1 , q 2 , q3 The transition function, is given as
0,1
∑
0 1
q0 q0 Q
F q3
q0 q 0 , q1 q 0
q1 q 2 q 2
q2 q3 q3
* q3
L x {a, b, c : x contains exactly one b immediately
*
9) Construct a NFA for
following c}.
a, b a, b, c
c a, c
q0 q1 q1
1 b
Trap
a b
q4
10) Construct a NFA that accept L x {0, 1 : x is starting with 1 and | x | is divisible
*
by 3}.
0, 1
q4 Trap
0
q1
1 0, 1
q1
0, 1
q3 0, 1
1.44 Theory of Computation
11) Design a NFA for L = L {x {a, b}* / x contains any number of a‟s followed by
atleast one b}.
a b a, b
b a
q0 q1 q2
Trap
12) Design a NFA for a binary number where the first and the last digits are same.
0, 1
q2
0 q4
0
q1
1 1
q3 q5
0, 1
13) Construct a NFA over 0, 1 such that each „0‟ is immediately preceded and
immediately followed by 1.
1
1
q0 q1
0 1
0
q3 0
q2
0, 1
Trap
0,1
0 1
q0 q1 q2
Every language that can be described by some NFA can also be described by some
DFA. It involves subset construction process for conversion.
Automata Fundamentals 1.45
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
0 1
q0 q1 q2
Here ,
QN q 0 , q1 , q 2
0,1 ∑ 0 1
Q
q 0 q 0
q0 q 0 , q1 q 0
F q 2
q1 q 2
* q2
1.46 Theory of Computation
Subsets
QD , q 0 , q1 , q2 q0 , q1 q0 , q 2 q1 , q 2 q 0 , q1 , q 2
Transition table: D
∑
0 1
Q
→ q 0 q 0 , q1 q 0
q1 q 2
* q 2
Minimised DFA
1 0
0 1
{q0} {q0 , q1} {q0 , q 2}
0
1
Automata Fundamentals 1.47
QN p, q, r,s N : ∑
0 1
Q
0,1 →p p, q p
q0 p q r r
FN s r s
0 0, 1 0, 1
p q r s
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
QD 24 16
Transition table
∑
0 1
Q
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
1.48 Theory of Computation
0 0
{p} {p, q} {p, q, r}
1 0
1
1 0
{p, r} {p, q, r, s}
0
0 1
1
{p, q, s} {p, r, s}
0
0 1
{pp,,ss}
1
Automata Fundamentals 1.49
∑
0 1
Q
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,s
0
p
0, 1 0, 1 0
r r s
1 1
1
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:-
∑
0 1
Q
p q, s q
*q r q, r
1.50 Theory of Computation
*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:-
∑
0 1
Q
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
DFA diagram:-
1
1
{q, s} {p, q, r}
0 0
{p}
1 {r}
0 1 {r, s}
1 0 1
0
1 {q} 1 {q, r, s}
0
1 {q, r}
{s} 0
0
0 1
q0 q1 q2
0 1
Solution:-
QN = {q0, q1, q2} ∑
0 1
Q
∑ = {0, 1}
→{q0} {q0, q1}
q0 = {q0} {q0} {q2}
FN = {q2} *{q2} {q2}
Construction of DFA:-
QD = {, {q0}, {q1}, {q2}, {q0, q1}, {q0, q2}, {q1, q2}, {q0, q1, q2}}
q0 = {q0}
1.52 Theory of Computation
Transition table:-
∑
0 1
Q
{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 DFA:-
1
0
0 1
{q0} {q0 , q1} {q2}
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
Solution:
Given NFA,
1 0
0
0 0,1
q1 q2 q3
0
1
∑
0 1
Q
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:-
∑
0 1
Q
→
{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
Minimized DFA:-
0
1
0 0,1 0
{q1} {q2 , q3} {q1, q2} {q1, q 2 , q3}
1
1
D ({q0 }, w) N ({q0 }, w)
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)
Automata Fundamentals 1.55
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,
D ({q0}, x) N ({q0}, x)
k
{P1, P2 ,.....Pk } subsets Pi
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}
Ui1
K
N (Pi, a) Bysubset construction
0 0 2
Є 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
Є Є
3 6
2
Є
1 Є
a 7
4 5
Є
c) (s, a) U ECLOSE(r ) m
D j1 j
PROBLEMS
1) Design a DFA that eliminates Є transition from the following Є- NFA.
∑
Є a b c d
QE
q0 q0, q1
q1 q1 q1, q2 q3
q2 q2, q3 q2 q2
*q3 q3 q3 q3
a c,d
Є a
q0 q1 q2
b Є
q3
a,b
Solution :-
Start state of DFA:-
ECLOSE (q0) = {q0, q1} → Start State of DFA
1.58 Theory of Computation
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
D ({q1, q2 , q3}, a) :
E (q2 , a)
{q1}U{q2 , q3}U{q3}
D ({q1, q2 , q3}, b) :
E (q1, b) {q3}
E (q2 , b)
E (q3 , b) {q3}
D ({q1, q2 , q3}, c) :
E ( q1 , c)
E (q2 , c) {q2}
E ( q3 , c)
D ({q1, q2 , q3}, d) :
E ( q2 , d)
E (q3 , d) {q2}
E ( q3 , d)
1.60 Theory of Computation
Transition Table:-
∑
a b c d
QD
Equivalent DFA:-
a
{q0 , q1} {q1, q2 , q3}
b
b
c, d
{{{qq33}}
a, b {q2 , q3}
a, b c, d
Є C e
q0 q1 q2 q3
Є
b C
q0 Є
q5
a
Solution:-
Start of DFA:-
ECLOSE(q0) ={q0, q1}
↓
Start State of DFA
U{q4} {q1}
Automata Fundamentals 1.63
∑
Є a b c d e
QD
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 , 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 ({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 ()
{q1}U
{q4}
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}, c) :
D ({q2}, c)
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}
D ({q3,q5}, a) :
D ({q3,q5}, d) : V
Transition Table:-
∑
a b c d e
QD
{q0,q1} {q1} {q4,q5} {q2}
{q1} {q1} {q4,q5} {q2}
{q4,q5} {q4,q5} {q2}
{q2} {q2} {q3,q5}
*{q3,q5} {q3,q5}
a
{q0 , q1} {q1}
b
b c e
c
{q4, q5} {q2 , q3} {q3, q5}
a
d
1.68 Theory of Computation
*r
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}
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}, c) :
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
Transition diagram:-
b
{p,q,r} {q,r}
a,c
{r}
UNIT – 2
REGULAR EXPRESSIONS AND LANGUAGES
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
a b
{ab} R.E = a.b = ab
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 .
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.
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
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)ij, 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)ij 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
ij =
a) If there is no such symbol, a then R (0)
6.2 Theory of Computation
c) If there are symbols a1, a2,….ak that label arcs from state i to j then R (0)
ij = 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
ij
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
ik
(R k1
kk
)* Rk1
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
R kik R k1
ij
Rk1
ik
(Rk1
kk
)* Rki
kj
Regular Expressions and Languages 2.7
PROBLEMS
1) Obtain the RE that denotes the language as accepted by the following DFA.
AU DEC 2004
1 0,1
0
1 2
i → start state = 1
j → Final state = 2
Basis Step
Step 1:
R(0) a a a ..... a ;i j
ij 1 2 3 k
a1 a 2 a k ; i j
R11(0) 1 [i j]
R12(0) 0 [i j]
(0)
R12 [i j]
R22 0 1 [i j]
Inductive Step
ij R ij
R (k) R k1
(k1) k1 k1
ik (R kk )*R kj
k takes the values 1 and 2. For k =0, basis step has already been calculated.
Step 2:
k=1
(1) [(1)(1)*(1)]
i=1; j=2 R
21
(1)
R
12
(0)
R11(0) (R11(0) )*R(0)
12
0 [(1)(1)*(0)]
0 [((1)1*(0)] (1)*1*
0 [1*0] (1)*1*
01 Somethingsomething
Step 3:
k=2
To find R(2)
22
1 0
q1 q2 q3
0 1
Solution:
Here;
i=1
j=1
k=3
To find: Rij(k) R(3)11
Basis
Step 1:
a a ..... a ;i j
ij a 1
R(0) 2 3 k
a1 a2 ..... ak ; i j
R 11(0) 0 22
R (0) 0
(0)
R 31
12 1
R(0) 22 1
R(0) 32 1
R(0)
R 13(0)
23 0
R(0) 33
R(0)
Inductive step
R ij(k) R (k1)
ij R (k1)
ik (R (k1) (k1)
kk )*R kj
k = 0 computed in step 1
Step 2:
k=1
0* ( 0) 0*0*
(1)
R12 R 12
(0)
R 11
(0) (0)
(R11 (0)
)*R 12
[ ( 0) ( 0) *.]
. a
22 R 22 R 21 (R11 )*R 12
R (1) (0) (0) (0) (0)
[.( 0) *.( 0) . a
22 R 22 R 21 (R11 )*R 12
R (1) (0) (0) (0) (0)
(1) a a
12. Theory of Computation
2
23 R 23 R 21 (R11 )*R 13
R (1) (0) (0) (0) (0)
0 [.( 0)*.]
0 . a
0 a a
(1)
R 31 R 31
(0)
R 31
(0) (0)
(R11 (0)
)*R 11
0[0.0*.] 0 00*00*
(1)
R 32 R 32
(0)
R 31
(0) (0)
(R11 (0)
)*R 12
1[( 0)*1]
1 00*1
(1)
R 33 R 33
(0)
R 31
(0) (0)
(R11 (0)
)*R 13
[0.( 0)*
Step 3:
k =2
[(1 00*1)(1)*(0)]
q1 0 q1 1 q3
0
0,1
14. Theory of Computation
2
Solution:
To find: Rkij
i = 1 ←Initial State.
j = 2 ← Final state
k = 3 ← No of states.
R1z
(3)
R(2)
12 R 13(R 33)*R 32
(2) (2) (2)
Basis
Step 1:
(0)
Rij a1 a2 ... ak ;i j
a1 a 2 a k ;i j
11
R (0) 21 0
R (0) 31
R(0)
12 0
R (0) 22
R (0) 32 0 1
R(0)
13 1
R(0) 23 1
R(0) 33
R(0)
Inductive step
Step 2:
k=1
(1)
R11 R11
(0)
R11
(0) (0)
(R11 (0)
)*R 11
Regular Expressions and Languages 2.15
R R
(1)
12
(0)
12 R (R )*R
(0)
11
(0)
11
(0)
12
0 0 0 0 0
0
(1)
R13 R13
(0)
R11
(0) (0)
(R11 (0)
)*R 13
11 111
1
21 R 21 R 21 (R 22 )*R 11
R (1) (0) (0) (0) (0)
0 [0()*]
0 0 .* None
0 0 0 0
22 R 22 R 21 (R11 )*R 12
R (1) (0) (0) (0) (0)
[0()*.0]
00 *.0 0
23 R 23 R 21 (R11 )*R 13
R (1) (0) (0) (0) (0)
1[0()*1]
1 01 *.1 1
(1)
R 31 R 31
(0)
R 31
(0) (0)
(R11 (0)
)*R 11
[ () *.]
.a
16. Theory of Computation
2
(1)
R 32 R 32
(0)
R 31
(0) (0)
(R11 (0)
)*R 12
(0 1) . a
(0 1) . a
(1)
R 33 R 33
(0)
R 31
(0) (0)
(R11 (0)
)*R 13
[ ()*1]
0 . a
Step 3:
k=2
12 R12 R
(3) (2) (2) (2) (2)
To find : R 13 (R33 )*R32
ij R ij R i2( R 22
R (2) (1) (1) (1) (1)
)*R 2j
(2)
R12 R12
(1)
R 12
(1)
( R (1)
)*R
22
(1)
22
0(00)*
(2)
R13 R13
(1)
R 12
(1)
( R (1) (1)
22 )* R 23
(2)
R 33 R 33
(1)
R 32
(1)
( R (1) (1)
22 )* R 23
(2)
R 32 R 32
(1)
R 32
(1)
(R (1) (1)
22 )* R 22
Step 4:
k=3
(3)
R12 R 12
(2)
R 13
(1) (2)
(R 33 )* R (2)
32
(2)
4. Find the RE, R23 from the following DFA. AU DEC 2007
1
1
1 2 1 3
Solution:
To find: R23(2) R(1)
23 R 22(R 22)* R 23
(1) (1) (1)
Basis
Step 1:
R a1 a 2 ...a k; i j
(0)
ij
a1 a2 ...ak ; i j
18. Theory of Computation
2
R(0)
11 21 0
R(0) 31
R(0)
j2 1
R (0) 22
R(0) 32
R(0)
1
23 1 33
(0)
R13 R (0) R(0)
Inductive Step
(1)
We Require R23 , R(1)22
Step 2:
k=1
Rij(1) R ij(0) R i1(0) (R11
(0)
)* R1j(0)
23 R 23 R 21 (R11 )* R13
R (1) (0) (0) (0) (0)
1 01
22 R 22 R 21 (R11 )* R12
R (1) (0) (0) (0) (0)
[ 0() *1]
01 * None
Step 3:
k=2
23 R 23 R 22 (R 22 )* R 23
R (2) (1) (1) (1) (1)
q1 1 q2 0
q3
0,1
0 1
Regular Expressions and Languages 2.19
Solution:
(2)
The language accepted by the above DFA is R11 R(2)12[since there are two final states]
The state q3 is eliminated since it is a trap/dead state.
0 1
q1 1 q2
Basis
Step 1:
R (0) a a a ...... a ;ij
ij 1 2 3 k
ij R ij R 12 (R 22 )* R 21
R (1) (1) (1) (1) (1)
(2)
R12 R(1)
12 R 12 (R 22 )*R 22
(1) (1) (1)
(1)
R11 R11
(0)
R11
(0) (0)
(R11 (0)
)*R 11
0* (1) 1* 1*
(1)
R12 R12
(0)
R11
(0) (0)
(R11 (0)
)*R 12
0 *1 1 0 * 1 0 *1
21 R 21 R 21 (R 11 )*R 11
R (1) (0) (0) (0) (0)
.a
22 R 22 R 21 (R 11 )*R 12
R (1) (0) (0) (0) (0)
(1) .a
(1) a a
Step 3:
k =2
(2)
R11 R11
(1)
R12
(1) (1)
(R 22 (1)
)*R 21
0* a a
12 R 12R 12
R(2) (1) (1)
(R(1) )*R
22
(1)
22
0 *11* 0 1*01*
The R.E R(2) R(2) 0 * 0 *11*
11 12
Rij
qi pj
Qj
Pj
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.
Rij Qi S*Pj
q0 S
q
T
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;
( R SU*T)* SU*
(iii)If the start state and the accepting state are the same, then we are left with one –
state automaton.
R
q0
PROBLEMS
1) Construct the transition diagram for the given DFA and give a R.E by using state
elimination technique.
∑
0 1
Q
q1 {q2} {q1}
q2 {q3} {q1}
* q3 {q3} {q2}
1 0
0
q1 1 q2 0
q3
1
Solution:-
Here, q1 → Start State
q3 → Final State
The state to be eliminated is q2.
We use the Regular Expression: Rij Qi S*Pj is labeled for the arcs from q1 to q3.
To be found
q1-q2-q3
q3-q2-q1
q1-q2-q1
q3-q2-q1
q1 q2 q3
Qi Pj
S
Regular Expressions and Languages 2.23
Here,
Rij =
Qi = 0
S =
Pj = 0
r1 0. * 0
00 * None
r1 00 aa
r4 = 0 + 10 ← * = None
00
1+01 0 + 10
R
S U
q1
q3 q1
q3
11
T
∑
0 1
Q
*p s p
q p s
r r q
s q r
1 0
P 0 q 1 r 1 s
Solution:-
[First eliminate the state that is far away from start state].
States to be eliminated: s, r, q
Regular Expressions and Languages 2.25
Rij Rij =
Q1 = 0
p - s -q S =
Pj = 0
Qi Pj
S
r1 0*0
s 00 * None
r1 00 a a
Rij Rij = 0
Qi = 1
q - s - p S =
Pj =
Qi Pj
S
26. Theory of Computation
2
r2 0 1*
0 x a
0 a a
Rij
Rij r3 Rij QiS* Pj
p - s - r Qi 0 0*1
S 01 * None
Qi S Pj
Pj 1 r3 01 a a
Qi
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
Qi Pj
S 11 * None
S P 1 r 11 a a
j 5
r - s - q Qi 1 .*0
Qi Pj S 1 1 .a
S P 0 r 1 a a
j 6
Regular Expressions and Languages 2.27
p - s - p Qi 0
1 .*
Qi Pj S 1 .a
S
Pj r7 1 a a
q - s - q Qi 1 1*0
Qi Pj S 1 0 None
S
r - s -r Qi 0 *1
Qi Pj
S 0 .a
S
Pj 1 r9 0 a a
Reduced Automaton
10
1 0
00 11
P q r
1
0
Pj
01
Qi Pj
s0 r 2 0 . a a
S Pj
q - r - q Qi 11
S 0
Qi Pj
S Pj 1
p q
0
Regular Expressions and Languages 2.29
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
A 1 B 0, 1 0, 1
C D
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
30. Theory of Computation
2
r7 : D – B - D
r8 : C – B - D
r9 : D – B – C
Step 1:
Elimination of state – B can be done using Rij Q1 S*Pj
A - B - D Qi 1
Qi Pj
S r3
S Pj
D - B - A Qi
Qi S r4
Pj
S Pj
Regular Expressions and Languages 2.31
C - B- C Qi
Qi Pj S r6
S Pj 0 1
D - B- D Qi
S r7
Qi Pj Pj
S
Eliminating B from C – B – D (r8)
Rij Rij 0 1 r8 (0 1) *
C - B - D Qi r8 (0 1)
Qi Pj S r8 0 1
S
Pj
D - B -C Qi
Qi Pj S r9
S Pj 0 1
32. Theory of Computation
2
Reduced Automaton
0+1
1 ( 0 + 1)
0+1
A C D
Step 2:
Case – 1: Eliminating of „C‟
r1 : A - C - D
r2 : D - C - A
r3 : A - C - A
r4 : D - C - D
D - C - A Qi
Qi S r2 0
Pj
S Pj
D-C- D Qi
S r4
Qi
Pj
Pj
S
Resulting Automaton
0, 1
1(0+1) (0+1) R U
S
A D
1 2
T
A - C - D
D - C - A
A-C- A
A-D- C Qi 0 1
S r2
Qi Pj
S
Pj
A - D- A Qi ( 0 1)
S r3 0 1
Qi Pj Pj
S
Eliminating D from C – D – A (r4)
Rij
Rij r4 ( 0 1) *
C-D- A Qi 0 1
S r4
Qi Pj
S Pj
The Reduced Automaton
0+1 R
1 (0+1)
S U
A C
1 2
T
Regular Expression (RE1) = (R + Su*T)* SU*
RE1 = (0+ 1) + (1 (0 + 1)) * )* (1 (0 + 1)) *
RE2 = (0 + 1) * 1 (0 + 1)
Final Automaton
R = RE1 + RE2
= (0 + 1)* 1(0+1)(0 + 1) + ( 0 + 1)* 1( 0 + 1)
Regular Expressions and Languages 2.35
(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 =
a
(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 = {}
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.
(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
R Є S
(c) R*
Є R Є
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)
Since the parenthesis does not change the language defined by the expression.
PROBLEMS
1) Convert the RE: (0 + 1)* 1(0+1) to -NFA
(0+1)
0
2 4 Є
Є
6
1
1
Є 3 5 Є
38. Theory of Computation
2
(0+1)*
0
Є
3 5
Є
7 8
Є
1 2
Є
1
4 6
Є
Є
(0+1)*1
0
Є
3 5
Є Є 1
8 9 10
Є 7
1 2
Є
1
4 6
Є
Є
(0+1)* 1(0+1)
0
0 1 1
Є
3 5 Є Є 1
Є 1
Є
Є 7 8 9 1 1
1 2 Є Є
1 1
4 6 Є 1 1 Є
Є
Regular Expressions and Languages 2.39
1 2
1*
Є
Є 1 Є
1 2 3
4
Є
01*
Є
Є Є Є 1 Є
1 2 3 4 5 6
1 Є 0
1 2 3
4
40. Theory of Computation
2
0+11
0
Є
Є 2 4
8
1 Є Є
1 Є 1
3 5 6 7
0*1
Є
Є Є Є 1
1 2 0 3 4 5 6
Є
10 + (0 + 11)0*1
1 Є 0
2 3 4 5
Є
Є
1
1
Є 0
8 9
Є
1
Є Є 0 Є
1 1 1 1
6 7 11
Є Є
1 Є 1 Є Є
1
1 1 1
(0+1)*
0
3 4 Є
Є
Є Є
1 2 7 8
Є Є
1
5 6
Є
00(0+1)*
Є
0
9
8
0 Є 0 Є Є Є Є
13
1 2 3 4 5 6 7 12
Є
Є
1
10 11
Є
(0+1)*
Є 0
3 4
Є Є
1 2 7 8
Є Є
1
5 6
Є
42. Theory of Computation
2
00+11:-
0 Є 0
2 3 4 5
Є
Є
1 10
Є
Є 1 Є 1
6 7 8 9
(0+1)*(00+11)(0+1)*
Є 0 Є 0
0
1 Є
Є
Є Є
Є Є Є
Є
1 Є
1 Є 1
Є
Є
Є Є
1
Є Є
Є
Regular Expressions and Languages 2.43
w = xyz
such that
(i) y ≠
(ii) xy n
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 (q 0 ,( a1 , a 2 , .. 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. Theory of Computation
2
(ii) y = ai+1, a1+2,….aj
(iii) z = aj+1, aj+2,….am
P0 Pi Pn-1
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 q 0 (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
y and then goes to the accepting state on input, z Thus for any k ≥ 0, xykz is also accepted by
k
PROBLEMS
1) By using pumping lemma, P.T the language, L{on on / n 1} is not a regular
language.
Solution:
Assume that L is a regular language Let n be a constant and w on on 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
Since y≠
x= 0 n-1 // since xy n
Regular Expressions and Languages 2.45
y=0 // Since y 1
z = 10n
Thus
(i) y≠
(ii) xy ( n 1) 1 xy n
(2) xy n
x = an-1 xy = an-1. a1 = an xy n
y=a y 1
z = bn+1 cn+2
At k = 0:-
xy0z = xz
= an-1.bn+1.cn+2 = an-1.bn+1.cn+2
At k = 1:-
xy1z = an-1.a. bn+1.cn+2
= an.bn+1.c n+2
At k = 2:-
xy2z = an-1.a.a. bn+1.cn+2
46. Theory of Computation
2
= an+1.bn+1.cn+2
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 k 0
Let,
Y = am where m ≥ 1 y m n
Consider xykz = xyz . yk-1 Let w= ak ; k → first prime
xykz = xyz.yk-1 k+1 → Next prime > P
= P + (k-1) y =k+k y
= P + Pm ← k = p+1 P = k -1
= P(1+m)
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 1n | 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 1n 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.
2
5) Show that L = {ai / i 1} is not a regular
AU MAY 2006, DEC 2006, DEC 2007, DEC 2008
2
When , i = 1; L = { a1 } = a1
2
i = 2; L = { a 2 } = aaaa = a4
2
i = 3; L = { a3 } = aa
The no of a‟s is always a perfect square. Consider L = an where length = n2
2
w n2
At k = 2 xy2 z x y2 z n2 3
From 2 and 3,
x y z > x y2 z
xy2 z n2 4
Since xy n;
4 xy2 z \ n2
48. Theory of Computation
2
When y n and x
xy2 z n2 n
xy z < n + n + n +1
2 2
[For inequality removal]
From 4 and 5,
n2 < xy2 z < (n 1)2
Example: n = 2; n2 = 22 = 4
(n+1) = 2+1 (n+1)2 = 32 = 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)
k
xyz L since xy z 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 omn
w m n (m n) 2(m n) n
xy n
x o m1
y=0
z = 1n om+n
7) Using pumping lemma, P.T:- L ww R | w(0, 1) * is not regular. AU DEC 2006
(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 xy0z xz = an-1.b.b.an
Since the powers of a does not match, L is not regular.
L = {ww} = {anbanb}
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. Theory of Computation
2
z =b.an b.
At k = 0:-
Proof
Let, L = L [R]
M = L[S]
= L [R+S]
Example
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.
Proof
Let L= L [A] for some DFA, where
A = (Q, ∑, , q0, F)
i.e., B is exactly like A, but the accepting states of A have not become the accepting
states of B and vice-versa.
0,1
0 1
q0 q0
q0
52. Theory of Computation
2
Here,
QN = {q0, q1, q2}
N → ∑
∑= {0, 1} QN 0 1
D
∑
0 1
QN
{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}
0
1
0 1
{q0,q1}
{q0} {q0,q2}
0
1
Regular Expressions and Languages 2.53
Complement of DFA
0
1
1
{q0} 0
{q0,q1} {q0,q2}
Proof
where,
By induction on w ,
(q M , w) must be in FM
Example
Construct A B where A and B is given by
54. Theory of Computation
2
1
0,11
0
A p
q
0 0,11
1
r s
B
A B(Q A x QB , , (q A x qB , FA x F B )
Where
Here,
QA x QB = {p,q} x {r,s}
Z = {0, 1}
qA x qB = {p, r}
FA x FB = {q, s}
δ(qA, qB) ∑
Q↓ 0 1
Finite Automata
{p, r } 1 {p, s }
0 0
{q, r } 1
0, 1
{q,s}
0
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. Theory of Computation
2
Then, L (E1. E2) = {0100, 0110, 11100, 11110}
L(E1)R = {10,111}
L (E2)R = {00,01}
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,
w R w Rn w Rn1............ w1R
w1, w2,…wn, where each w1 is the reversal of a string in L(E1). The reversal of the string,
wR wR .......wR is therefore a string in L ( E* ) which is L (E).
n n1 1 1
Example: If L = (0 + 1)0*
LML M
Proof
Let M1 ( s, , 1 , so , F)
Theorem
If two languages, M1 and M2 are regular, then L (M1) . L (M2) is also regular.
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.
Homomorphism
A string homomorphism is a function on strings that works by substituting a particular
string for each symbol.
h(1) =
h(0011) = h(0)h(0)h(1)h(1)
= abab
Theorem
If E = or , then
H(E) = or
L[h(E)] = L (E)
L[h(G)] = h [L(G)]
Similarly concatenation and closure is also true,
Inverse homomorphism
Theorem
If h is a homomorphism from alphabet, ∑ to alphabet T and L is regular language over
T, then h1 (L) is also a regular language.
Proof
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,
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)
= h-1(a)h-1(ba)h-1(ba)
= 022.
60. Theory of Computation
2
2. 7 EQUIVALENCE AND MINIMISATION OF AUTOMATA
Two finite automata m1 and m2 are said to be equivalent if they accept the same
language.
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.
c c
d
q1 d q4 q5
d q1
d
c d
d c c
q1 c q6 q7
M1 M2
Step 1:
Here the start states are <q1> and <q4>
We shall start with <q1, q4> over ∑ ={c, d}
3 ( q1 , q4 c) 1 (q1, c), 2 (q4 , c)
q1 , q4
3 ( q1, q4 d) 1 (q1, d), 2 (q4 , d)
q2 , q6
Regular Expressions and Languages 2.61
Step 2:
State <q2, q6> over ∑ ={c, d}
3 ( q2 , q6 , c) 1 (q2 , c), 2 (q6 , c)
q3 , q7
3 ( q2 , q6 , d) 1 (q2 , d), 2 (q6 , d)
q1 , q4
Step 3:
State <q3,q7> over ∑ ={c, d}
3 ( q3 , q7 , c) 1 (q3 , c), 2 (q7 , c)
q2 , q5
3 ( q3 , q7 , d) 1 (q3 , d), 2 (q7 , d)
q3 , q7
Step 4:
State <q2, q5> over ∑ ={c, d}
3 ( q2 , q5 , c) 1 (q2 , c), 2 (q5 , c)
q3 , q7
3 ( q2 , q5 , d) 1 (q2 , d), 2 (q5 , d)
q1 , q4
Thus every state in m3 has been expanded.
The construction of combine DFA is as follows.
d
<q1 , q4> <q2 , q5>
d c
d c
c
62. Theory of Computation
2
<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.
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
PROBLEMS
0 1 0
q0 q1 q2 q3
0 1
1
0 1
1 1 0
q4 q5 q6 q7
1
0
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 [ block1]
( q1 , 0) q 6 [ block1]
( q3 , 0) q2 [ block 2]
Equivalent states since the transition
( q4 , 0) q 7 [ block1] result in same output.
64. Theory of Computation
2
( q5 , 0) q2 [ block 2]
( q6 , 0) q6 [ block1]
( q7 , 0) q6 [ block1]
From the above process of input o and 1, {q3, q5}-, {q1, q7} are a set of equivalent
states.
1 – equivalence partitioning
2 – Equivalence partitioning:
Block – 11
( q3 , 0) q 2
Mapped to the same block, Block 2
( q5 , 0) q 2
( q3 , 0) q 6
Mapped to the same block, Block 12
( q5 , 0) q 6
Block – 12
( q1 , 0) q 6
Mapped to the same block, Block 13
( q7 , 0) q 6
Regular Expressions and Languages 2.65
( q1 ,1) q 2
( q7 ,1) q2 Mapped to the same block, Block 2
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
0 1
0 1 < q2>
< q0, q4> < q1, q7>
0
1
0
1
<q6>
< q1, q7 >
1 0
Input
a b
States
→ q0 q0 q3
q1 q2 q5
q2 q3 q4
q3 q0 q5
q4 q0 q6
q5 q1 q4
* q6 q1 q3
66. Theory of Computation
2
0- equivalence partitioning
P0 = (q0, q1, q2, q3, q4, q5) (q6)
block 1 block 2
1 – equivalence partitioning
( q0 , a ) q0 ( q0 , b) q3
( q1 , a) q2 ( q1 , b) q5
( q2 , a) q3 ( q2 , b) q4
( q3 , a ) q0 ( q3 , b) q5
( q4 , a) q0 ( q4 , b) q6
( q5 , a) q1 ( q5 , b) q4
There are no equivalent states. But still, q4 is the distinguishable state from the rest of
the states
2 – equivalence partitioning:
From the above transitions, there are no equivalent states but there are distinguishable
states, {q2, q5} since
( q2 , a ) q3
→ block 11
( q5 , a) q1
( q2 , b) q3
→ block 12
( q3 , b) q0
3- Equivalence partitioning:
( q0 , a) q 0 block 112
( q1 , a) q 2 block 111
( q3 , a) q0 block 112
( q0 , b) q0 block 112
( q1 , b) q0 block 111
( q3, b) q 0 block 111
Since no states are equivalent, q0, q1, q3 are distinguishable from each other.
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.
Inputs
0 1
States ↓
→ q0 q1 q4
q1 q2 q5
* q2 q3 q7
q3 q4 q7
q4 q5 q8
* q5 q6 q1
q6 q7 q1
q7 q8 q2
* q8 q0 q4
68. Theory of Computation
2
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) q 2 Block 2
( q3 , 0) q4 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) q 7 block 1
( q4 , 1) q8 block 2
( q6 , 1) q1 block 1
( q7 , 1) q2 block 2
2- Equivalence partitioniong:
( q0 , 0) q1 ( q0 , 1) q 4
( q3 , 0) q 4 Block 12 ( q3 , 1) q 7 Block 12
( q6 , 0) q 7 ( q6 ,1) q1
( q1 , 0) q 2 ( q1 , 1) q 5
( q4 , 0) q 5 Block 2 ( q4 , 1) q 8 Block 2
( q7 , 0) q 8 ( q7 , 1) q 2
Regular Expressions and Languages 2.69
( q2 , 0) q 3 ( q2 , 1) q7
( q5 , 0) q6 Block 11 ( q5 , 1) q1 Block 11
( q8 , 0) q 0 ( q8 , 1) q 4
0, 1
0
GRAMMAR
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
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
Sa
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 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
Example
Most programming languages are context sensitive.
2
o Eg: L = {an bn cn |n ≥ 1|}, L = { a i |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
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 Aa
o Ab
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
Chomsky hierarchy
The four forms of grammars follows the given hierarchy,
Unrestricted Recursively
Enumerable Grammars
Content-sensitive
Grammars
Content-free
Grammars
Regular
Grammars
Chomsky hierarchy
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.
3.11 Theory of Computation
Vi V
i {VUT}
Notations
The terminals are denoted by digits (0, … , 9) or lower case letters (a, … , z) etc. It
also includes special characters like +, -, *, (, ) etc.
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
Sa
Sb
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
*
L(G) w | w T * and Sw
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
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,
3.13 Theory of Computation
Sc
S aSa
S bSb
The possible productions are {, ab, aabb, aaabbb, a4 b4, 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}
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
2. S aSb
3. S aAb
4. S aAb
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
SS+S
SS*S
S (S)
S a | b |
Example: Consider ( ) ( )
S SS [using S SS]
()S [using S ]
()() [using S ]
S AaA
P
A bA |
3.17 Theory of Computation
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
S AaA
P
A aA | bA |
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
S AaAaAaA
P
A aA | bA |
where,
V = {S, A}
T = {a, b}
S = {S}
11. Construct CFG for G = {an b an | 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
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
Sentential form is the derivation derived from the start symbol, by applying the
rules/productions.
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
Example
Consider the grammar given below
S A1B
A 0A |
B 0B | 1B |
1B [Using A ]
m
1001 [Using B ]
m
Thus the string „1001‟ is thus derived using left sentential form.
It is denoted as
*
S
rm
Example
Consider
S A1B
A 0A|
B 0B|1B|
Generate the right-most derivation for the string „1001‟.
Solution:
SA1B [Using S A1B]
rm
A1001 [Using B ]
rm
1001 [Using A ]
rm
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 [Since S aSb]
m
abab [Since S ]
m
aSab [Since S ]
rm
abab [Since S ]
rm
2. Consider the CFG, G = {{S, I}, T, P, S} where T = {+, *, (,), a, b, 0,1} and P is given as
SI Ia I I1
S S+S Ib I I0
S S*S I Ia
S (S) I Ib
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 * (I + S) [Using S I]
m
a * (a + S) [Using I a]
m
a * (a + I) [Using S I]
m
a * (a + b00) [Using I b]
m
S * (S+I) [Using S I]
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
aaBB aaBB
m rm
aabB aaBbS
m rm
aabbS aaBbbA
m rm
aabbbA aaBbbbAA
m rm
aabbbbAA aaBbbbAa
m rm
aabbbbaA aaBbbbaa
m rm
aabbbbaa aabbbbaa
m rm
0110 [Using A 0]
rm
Context Free Grammar and Languages 3.24
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.
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
S Using S 0S1
0 S 1 Using S 0S1
0 S 1 Using S 01
0 1 00011
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:
A 1 B
A 1 B Using A
S
A 1 B Using B 0B
0 B
A 1 B
0 B Using B 0B
0 B
A 1 B
0 B
0 B Using B 1B
1 B
3.27 Theory of Computation
A 1 B
0 B
0 B
1 B Using B
1001
m
A 1 B
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
Context Free Grammar and Languages 3.28
A 1 B
0 B
0 B
1 B Using B
A 1 B Using A
0 B
0 B
1 B
1001
rm
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
Parse tree
0 S 1
0 S 1
0 1
000111
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]
Context Free Grammar and Languages 3.30
Parse tree
X Y
a X Y
a X Y
Y Y b
b b
aabbb
(a + b) * a + b [Using F b]
E + T
T F
T * F b
F a
( E )
E + T
T F
F b
a (a + b) * a + b
S [Using S aAS]
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 ]
Context Free Grammar and Languages 3.32
0 S 1
01
2. S 0S1
0011 [Since S ]
0 S 1
0 S 1
0011
000111 [Using S ]
0 S 1
0 S 1
0 S 1
000111
L(G) = {0n 1n | n ≥ 0}
3.33 Theory of Computation
0110 [Using A 0]
rm
0 B
1 S
1 A
aaabBB [Using B b]
m
aaabbB [Using B b]
m
Context Free Grammar and Languages 3.34
aaabbabB [Using B b]
m
aaabbabbba [Using A a]
m
a B
a B B
a B B a B B
b b b b S
b A
aaabbabbba
a
001B [Using B 1]
m
00110101 [Using B 1]
m
3.35 Theory of Computation
0 B
0 B B
1 1 S
0 B
1 S
0 B
1
SS+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 S * S
a b a b
a*b+a*b
Context Free Grammar and Languages 3.36
Solution:
Example
S S
a S b S
a Y b
a X b a X b
b Y
a a
b
abbb aaabb aab
P S aSa
S aba
S S S
a b a a S a a S a
aba a b 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.
Solution:
Consider a + b + a, then the parse trees are derived as,
E E
E + E E + E
a E + E E + E a
b a a b
a+b+a 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.
Context Free Grammar and Languages 3.38
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,
EE+T|T
Ta|b
Thus the parse tree for a + b + a will be
E
E + T
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 bn cm dm | n ≥ 1, m ≥ 1}
= {an bm cm dn | 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
aAbB [A aAb]
m
aabbB [A ab]
m
aabbcBd [B cBd]
m
aabbccdd [B cd]
m
2) LMD-2
S C [S C]
m
aCd [C aCd]
m
aaDdd [C aDd]
m
aabDcdd [D bDc]
m
aabbccdd [D bc]
m
2
S
C
1
S
a C d
A B a D d
a A b c B d b D c
a b c d b c
Context Free Grammar and Languages 3.40
1
2
E E
E + E E * E
I E * E E + E I
a I 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.
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 S
0 S 1 S 0 S 1 S
1 S 0 S
0 S 1 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.
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 S b S S b
S a a a S a
a aaab a aaab
From the above parse trees, we have proved that the grammar is ambiguous.
S
a A b
a b S b b s
a a
abab abab
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 S b S
a a a a
ababa ababa
From the above two different parse trees, we conclude that the grammar is ambiguous.
Solution:
LMD
S SS [Using S SS]
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
Proof
*
Let S be a variable, where S V, 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, „‟.
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
S
Height of the
tree = 1
a1 a2 a3 An
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.
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
a b
md = 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
Context Free Grammar and Languages 3.46
a S b Terminal
Terminal
a S b Non terminal root of the sub-tree
a b
aaabbb
LMD
This is assumed.
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.
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.
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
*
= 1 2 … k
where,
*
Xi i
*
A X1 X2 …. Xk
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,
A
Height of the
tree = 1
1 2 n
r
Here the root is labeled A, AV and the children are the leaf nodes 1, 2 , .....n
which are read from the left to right to generate .
Inductive step
*
We assume the height of the tree as n | n>1, then A stands.
m
X1 X2 Xn
1 2 n
*
2. If A i V, then there must be some leftmost derivation, Ai i
*
1 2 .....ai1 Ai Ai1 Ai2 ....... An
m
If Ai is a terminal, then
*
Ai 1 2 ........ ai Ai1 Ai2 ...... An
m
If Ai is a variable, then
and
1 2 ,.....1
*
Ai Ai1 ...... An 1 2 2 Ai
m
*
Ai1 ...... An 1 2 , 3 ..... i Ai1 Ai2 ...... An
m
Thus,
*
A 1 2 ,.....i Ai1 Ai2 ........ An
m
*
m
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
Finite
Accept/
a0 a1 a2 … an … state
Reject
control
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
3.51 Theory of Computation
So, based on the inputs and stack symbols, transitions are made.
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.
Transition function
q, q1 states in Q
a input symbol in or
x, x1 stack symbol in
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 × *
Current state [ q Q ]
Next input [ ]
(q1, a, b ) = (q2, b )
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.
Where,
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.
The above transition cancels the top most stack symbol by the input processing.
Example
Algorithm
Consider n = 3
w = a3 b3 = a a a b b b
Transitions/moves
Initially, the stack is empty. On processing the first „a‟, the PDA pushes it onto the
stack.
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, 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.
a, z0 | az0
a, a | aa b, a |
b, a | , z0 | z 0
q0 q1 q2
where,
Graphical representation
Tuple format
(instantaneous description)
State Stack
control
q0 z0
State Stack
control
a a a b b b … Input
tape (q0, bbb, aaaz0)
a a a b b b … Input
tape
(q1, bb, aaz0)
Stack
a a a b b b … Input
tape
(q1, b, az0)
a a a b b b … Input
tape (q1, , z0 )
q1 z0
q2 z0
State Stack
control
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.
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, , ) }
P
where,
q0 initial state
input string
z0 initial stack symbol
q Q final state [q F]
stack symbols
Context Free Grammar and Languages 3.58
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,
PF = (Q U {P0, Pf} , U {x0}, F, p0, x0, {Pf})
where,
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
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 ]
PN
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]
PN
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.
0 1 0 1
q A
z0
1 1 0 0
q B
z0
0 0 1 1
A
q A
z0
1 0 1 0
B
As „0‟ is processed
q -1
z0
0 1 0 1
A
As „1‟ is processed
q -1
z0
1 1 0 0
B
q B
z0
0 0 1 1
Empty stack
q z0
0 0 1 1
Final state
q x0
Let L be L(PF) for some PDA PF = (Q, , , F , q0, z0, F). Then there is a PDA, PN
such that L = N(PN).
Proof:
, any /
, any /
To prove
For a string, is in N(PN) if and only if is in L(PF).
If-CASE
*
(q, , z0) (q, |) [Assume]
PF
Where
q final state
stack symbols
*
(q, , a x0 ) [Rule 3]
PN
*
(p, , ) [Rule 4]
PN
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).
Context Free Grammar and Languages 3.64
Thus PN becomes,
It does not have any choices of transition in any state for the inputs.
Formal definition
DPDA, P is given by
P = (Q, , , , q0, z0, F)
where,
Q Finite set of states
Set of input symbols
Finite set of stack symbols
3.65 Theory of Computation
Example
DPDA for L = {0n1n | n > 1}
0, 0 | 00
0, z0 | 0 z0 1, 0 |
1,0 | , z0 | z 0
q0 q1 q2
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, P1.
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.
Context Free Grammar and Languages 3.66
Instantaneous description
Let m = 2 and n = 1 (2 > 1)
= a2 b1 = aa b
b
a a a
Input z0
a
a a a
z0 z0
Stack z0 z0 z0 z0
State q0 q0 q0 q0 q1 q1
Transition functions
where,
Q {q0, q1}
{a, b}
{z0, a, b}
N As shown in the transition diagram
q0 {q0}
z0 {z0}
3.67 Theory of Computation
a, z 0 | az0 b, a |
a, a | a a , a |
, z0 |
b,a |
q0 q1
where,
Q {q0, q1, q2}
{a, b}
{z0, a, b}
N As shown in transition diagram
q0 {q0}
z0 {z0}
F {q2}
a, z 0 | az0
a, a | a a b,a |
b,a | , a |
q0 q1 q2
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
a
z0 z0
q0 q0
b
z0 z0
q0 q0
a
z0 z0
q0 q0
b
z0 z0
q0 q0
b
b
b
z0 z0
q0 q0
Transition function
PDA accepting through an empty stack PDA accepting through final state
Transition diagram a, a | aa
b, z 0 | b z0 b, b | bb
a, a | aa b, z 0 | b z 0
a, z 0 | a z0
a, b | bb a, z 0 | a z 0
a, b |
q0
b, a | a, b |
b, a |
, z0 |
q0
, z |z q1
0 0
PDA: PDA:
M = ({q0}, {a, b}, {a, b, z0}, , q0, z0, ) 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)
Context Free Grammar and Languages 3.70
Let w = a3 bb a
aaabba
a
z0 z0
q0 q0
Step 3: When input = x [xb], pop out the „a‟ from the stack.
(q0, b, a) = (q1, )
aaabba | aaabba
b a
b a
a
a a
a a a
z0 z0 z0
q0 q1 q1
(q1, a, a) = (q1, )
(q1, b, a) = (q1, )
3.71 Theory of Computation
aaabba
a a
a
z0 z0
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
z0
q1 q1
Thus the PDA is given by
M = ({q0, q1}, {a,b}, { z0, a}, , q0, z0, )
Where is given as
(q0, a, z0) = (q0, a z0)
Accepting an
(q0, a, a) = (q0, a a)
(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
a, z0 | a z0 a, a |
a, a | aa b, a |
, z0 | , a |
b, a | , z0 |
q0 q1
4. Let L = {an bn cm dm | 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
a
a a
z0 z0 z0
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
a
a a
z0 z0 z0
q0 q1 q1
W = aabbcd
c
z0 z0
q1 q2
d c
c
z0 z0
q2 q3
z0
z0
q3 q3
Solution:
Algorithm:
Step 1: For every input symbol, „a‟, symbol „x‟ is pushed onto the stack.
i = 2, j = 2 k = 2 + 2 = 4
W = aabbcccc
a a
x
x x
z0 z0 z0
q0 q0 q0
Step 2: For every input symbol, „b‟ symbol „x‟ is pushed onto the stack.
(q1, b, x) = (q, x x)
W = aabbcccc w = aabbccdd
b
b x
x x
x x x
x x x
z0 z0 z0
Step 3: For every input symbol, „c‟, „x‟ is popped out from the stack.
(q1, c, x) = (q2, )
(q2, c, x) = (q2, )
3.75 Theory of Computation
W = aabbcccc w = aabbcccc
c x
c x
x c x
c x
x x
x x x
x x x x
z0 z0 z0 z0 z0
q1 q2 q2 q2 q2
W = aabbcccc
z0
z0
q2 q3
M = ({q0, q1, q2, q3}, {a, b, c}, {x, z0,}, M = ({q0, q1, q2}, {a, b, c}, {x, z0,}, q0, z0,
q0, z0, , {q3}) , )
where is given as where is given as
(q0, a, z0) = (q0, x z0) (q0, a, z0) = (q0, x z0)
(q0, a, x) = (q0, x x) (q0, a, x) = (q0, x x)
(q0, b, z0) = (q1, x z0) (q0, b, z0) = (q1, x z0)
(q0, b, x) = (q1, x x) (q0, b, x) = (q1, x x)
(q1, b, x) = (q1, x x) (q1, b, x) = (q1, x x)
(q1, c, x) = (q2, ) (q1, c, x) = (q2, )
(q2, c, x) = (q2, ) (q2, c, x) = (q2, )
(q2, , z0) = (q3, z0) (q2, , z0) = (q2, )
(q0, , z0) = (q3, z0) (q0, , z0) = (q0, )
Context Free Grammar and Languages 3.76
Transitions:
Instantaneous description
Let n = 2, m = 1
Push No-op Pop „a‟
Then w = a a b cc
a b
a c
Input a a
c
a
a a
z0
a a a a
Stack z0 z0 z0 z0 z0 z0
State q0 q0 q0 q1 q2 q2 q2
Therefore, M = ({q0, q1, q2}, {a, b, c}, {z0, a, c}, , {q0}, {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.
a b
a
a b
Input a a
b c
a
a a a
a a a a a
z0
Stack z0 z0 z0 z0 z0 z0 z0
State q0 q0 q0 q0 q1 q1 q1 q2
c
z0
z0
q2 q2
PDA, M = ({q0, q1, q2}, {a, b, c}, {z0, a, b}, , {q0}, {z0}, }
where is given by
Instantaneous description
Instantaneous Description PUSH NO-OP POP „a‟
a a a a a a
Stack
z0 z0 z0 z0 z0 z0 z0 z0
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
Instantaneous description
Let w = baa; L = baacaab
c
a
a No-op a a
b a a
Input a a
a
b b
a a a
b b b a b b
Stack
z0 z0 z0 z0 z0 z0 z0 z0
State q0 q0 q0 q0 q1 q2 q2 q2 q2
PDA:
is given by
(q0, c, a) = (q1, a)
Step 2
(q0, c, b) = (q1, b)
(q1, a, a) = (q2, )
(q1, b, b) = (q2, )
Instantaneous description
Let n = 2 L = a2 b4 = aabbbb
b
b
a State
a change b a State
Input
change b
a
a a
z0
a a a a a
Stack
z0 z0 z0 z0 z0 z0 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)
Step 1
(q0, a, a) = (q0, aa)
(q0, b, a) = (q1, a) Step 2
(q1, b, a) = (q2, )
M = ({q}, T, VUT, , q, S, )
where is defined by
PDA by PDA by
CFG empty final
stack state
IF – CASE
Let is in L(G).
The left most derivation of is given as,
S 1 2 n
m m m
Basis of Induction:
Let us assume the length i=1
Then,
S = i
Rule 1:
Since i = xi i (q, , A) {(q, ) | A is in P}
1 = x1 1 [x1 = ] Rule 2:
1 = 1 For aT
And xi yi = (q, a, a) (q, )
x1 y1 =
y1 = [since x1 = ]
*
(q, ,S) (q, y , ) becomes,
i i
P
*
(q, ,S) (q, y1, 1 )
*
(q, , S) [since S = 1 = 1 ; 1 = S and y1 = ]
Thus,
*
(q, ,S) (q, , S) on x1 = moves.
Inductive step
* *
Assume the (q, ,S) (q, y , ) is true for „i' and prove that (q, ,S) (q, y , )
i i i1 i1
P P
is true for „i+1‟.
This is done by Rule – 1 on the construction of P that replaces A by [at the top of the
stack].
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) (q, , ) , then
P
*
Ax
G
Basis of induction
Since A
Inductive step
Assume that P takes „n‟ moves, where n>1. The first move must be of rule – 1 (as
given above).
Suppose
A Y1, Y2 …. Yk | Yi {TUV}
Context Free Grammar and Languages 3.84
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.
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 i1 ....x k ,) 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
.
.
.
*
x1 x2 x3 .......x k
*
(i.e) Ax
3.85 Theory of Computation
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, )}
where is
(q, , S) {(q, 0BB)}
3.87 Theory of Computation
Acceptance of 0104 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, )
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, ) }
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
To prove
*
*
[q X p] if and only if (q, , X) (p, , ) .
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
[q X p]
Thus basic of induction if proved
Inductive step
*
(p, , )
Where
= ax [a in or a = ]
where
1. rk = p
2. r1, r2, …. rk-1 Q
Y1
Y2
Yk
Pk
x1 x2 xk
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
states.
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
ONLY-IF-CASE
The proof is done on the induction on the number of steps in the derivations.
Basis of induction
There is only one transition of P with X being popped out with the state change from q
to P.
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
*
(r2, 3 4 … k, Y3, Y4 … Yk)
*
(rk, , )
Since, rk = p
*
(q, , X) (p, , )
PROBLEMS
1. Convert PDA to CFG (AU – May 2004, 2007, 2008, Dec 2008, 2009, 2012)
(p, 0, z) = (q, z)
Solution:
z
(1) S [q q] Using S [Initial state Initial stack symbol States]
z z
S [q p] S [q Q]
Renaming variables
z x
[q q] A [q q] E
z x
[q p] B [q p] F
z x
[p q] C [p q] G
z x
[p p] D [p p] H
Production
SA|B F 1EF | 1FH | 0H
A 1EA | 1FC H1
B 1EB | 1FD C 0A
3.95 Theory of Computation
E 1EE | D 0B
E 1FG | 0G 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:
z0
(1) S [q0 q0] Using S [Initial state Initial stack symbol States]
z0
S [q0 q1] z
S [q Q]
(2) (q0, 1, z0) = (q0, xz0)
z x z (qi, a, B) (qj, C1, C2)
[q 0 q ] 1 [q q ] [q 0 q ] Using
0 0 0 0 0 0
z0 x z0 B
[q0 q0] 1 [q0 q1] [q1 q0] [qi p1] a [aj
C1
p2] [p2
C2
p1]
z0 x z0
[q0 q1] 1 [q0 q0] [q0 q1]
z0 x z
[q0 q1] 1 [q0 q1] [q1 0 q1] p1, p2 Q
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
Initial stack symbol
Using the rule, S [Start symbol States in Q]
z
S [q0 0 q0] ------- (1)
z
S [q0 0 q1] ------- (2)
(2) Production for (q0, , z0) = (q1, )
Rule: (qi, a, B) (qj, )
B
[qi qj] a
z0
[q0 q1] ---- (3)
B C1 C2
[qi Q] a [aj Q] [Q Q]
Q = {q0, q1}
Context Free Grammar and Languages 3.98
Final productions
S 0F
F 0FH | 1JH
H0
J 1JL | 0
L0
The production generates a language of the form,
L = {0n 1m 0n+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
Thus, useless symbols are those variables/terminals that do not appear in any
derivation of a terminal string from the start string.
Non-reachable symbols
Example
S aA | bB | a | b
A Aa | a
B bB
Example
Consider the example given above. After eliminating the production for B bB , the
resulting grammar includes.
A aA | a
Example
Consider the above mentioned problem. The dependency graph is given by
Properties of Context Free Languages 4.4
A Sb
A bCC E aC
S A C E
S aAa
Non reachable
from S
Here all the variables are having a directed edge from the starting symbol, S except the
variable, E.
B
S 0B0 A BCC
DE
S B 1CC A D E
S BC C A 0C
Properties of Context Free Languages 4.6
A
S AB
S B AA
S AB B
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
S A B C
S aAa A bBB C aB
SAB
But C is a non-reachable symbol that doesn‟t have any derivation from S.
4.7 Theory of Computation
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.
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
B eliminated
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
S ABC | AB | BC | AC | A | B | C
A BC | B | C | a
B bAC | bA | bC | b
C cAB | cA | cB | C
The left hand side and right hand side contains only one variable each side.
Example
S A | bb
AB|b
BS|a
Here
S A,
A B,
B S are all the unit productions.
Example
S A | bb
AB|b
BS|a
The unit productions are
SA
AB
BS
4.1 Theory of Computation
1
Elimination of unit productions
S bb | b | a
A b | a | bb
B a | bb | b
The epsilon production in the grammar is S and the variable S has its impact on A
and B productions.
4.1 Theory of Computation
3
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
BS
BA
CS
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]
S C
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.
S A1
S A
A bSCa A bSCa
C
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 |
Bb|A
Solution:
(i) Elimination of production
S a | Ab | aBa | b | aa
Ab
Bb
PROBLEMS ON GNF
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 Theory of Computation
9
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
[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)
S AB
A BS | b
B SA | a
Solution:
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 Theory of Computation
1
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
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
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 Theory of Computation
3
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.
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
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
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
bA5 A5 A1 | bA5 A4
A5 c
Properties of Context Free Languages 4.26
S ABAb | ab
B ABA | a
Aa|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
Aa|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 Theory of Computation
7
6. Find the GNF equivalent to the following CFG
S AB
A aA | bB | b
Bb
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
Bb
S = {S}
The grammar need not be renamed if the substitution is simple and is done for one
production alone.
Solution:
Every CFG without null productions can be easily converted into its equivalent CNF.
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.
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 Theory of Computation
9
A Ca A
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
Aa
B Ca BB B Ca C2
C2 C2 BB
Bb
where,
V = {S, A, B, Ca, Cb, C1, C2}
T = {a, b}
S = {S}
S aAD
A aB | bAB
Bb
Dd
4.3 Theory of Computation
1
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
S Ca AD
A Ca B | Cb AB
Bb
Dd
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
Bb
Dd
C1
S A C1
C1 BA
S AB
S BA
S Ca A; where Ca a
Sa
S Cb B; where Cb b
Sb
S AA
A Ca A
Aa
B Cb B
Bb
4.3 Theory of Computation
3
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
Solution:
Step 1: Simplificaiton of grammar
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.
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
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
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 Theory of Computation
7
B Cb Cb
Ca a
Cb b
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}
A bAB | bB | bA | b
B BAa | Ba | Aa | a
2) Conversion to CNF
Ba
Properties of Context Free Languages 4.38
A Cb B, where Cb b
A Cb A, where Cb b
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 C6 A | c
D C4 B | a
Yf
The grammar above is converted to CNF as,
ABCD A B Ca
Ca Ca CD
4.3 Theory of Computation
9
Ab C1 a
B YC3 C2 b
Bd C3 c
C C6A C4 d
Cc C5 f
D C4 B C6 g
Da
Yf
(ii) vy
i i
(iii) for all i 0, uv xy z is in L
This parse tree will have the vertices ≥ M + 2, since there must be two vertices on an
edge.
The tree is divided with the root = Aj of the subtree for the string as,
Ai = Aj
Aj
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
A A
A
x
u v x y z u y
i i
The tree has a yield uxy and corresponds to the case, i=0 in uv xy z [as given above]
4.4 Theory of Computation
1
S
u v x y z
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,
i i
yielding the parse tree of the form uv xy z.
Applications
(i) Used to check if a language is context-free or not.
PROBLEMS
1. Prove that L = {ai bi ci | 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
(i) |vxy| ≤ n
(ii) vy
i i
(iii) for all i, uv xy z L.
Solution:
Let us assume that L is CFL
Let = ai bi cj , where i, j is a constant.
can be rewritten as uvxyz, where
(i) |vxy| ≤ n
(ii) vy
i i
(iii) uv xy 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
2 2
Then uv xy z = (ab)2 c2
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
2 2
uv xy z = (a)2 (bc)2 . Here j ≥ i is not true.
j is power of c
4.4 Theory of Computation
3
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 = a n |n 1 is context free. If so, enumerate some member of the equivalent CFL.
2
Let = a n
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
i i 2 2
i = 2 uv xy z uv xy z = (ab)2 2c --------- (1)
(ii) If v = a and y = bc
2 2
At i = 2, uv xy z = a2(bc)2------------------------- (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.
Solution:
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
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
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 bn cn | n 0}. From pumping lemma for CFL, a string of the form
n n n
a b c cannot be generated by a CFG.
The class of CFL is not closed under intersection.
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.
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, PR, 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 PR
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.
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]
Empty cell
Finite control
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
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.
Properties of Context Free Languages 4.52
a / a, R
b / b, R
a / x, R B/ B, L
q0 q1 q2
y / y, L
y / y, N b/ y, L
x / x, R
q4 b / b, L
q3
a / a, L
Instantaneous description
aabbB xabbB xabbB xabbB xabbB
q0 q1 q1 q1 q1
PROBLEMS
1. Design a Turing machine that accepts all strings of the form an bn 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.
4.5 Theory of Computation
3
Instantaneous description
Let n = 3 = a3 b3 = aaabbb
… B a a a b b b B …
… B x a a b b b B …
… B x a a b b b B …
L 1
B x a a b b b B
B x a a b b y B
3 1
2 R
L
B x a a b b y B
1 2
x 3
R
4
L
Properties of Context Free Languages 4.54
B x x a b b y B
2 1
y
L
3
R
B x x a b y y B
1
x 2
R
3
L
B x x x b y y B
2 1
y
L
3
N
R
Turing machine
a / a, R
b / b, R
a / x, R B/ B, L
q0 q1 q2
y / y, L
y / y, N b/ y, L
x / x, R
q4 q3
b / b, L
a / a, L
4.5 Theory of Computation
5
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
a b x y B
Q
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
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 Theory of Computation
7
Instantaneous description
Let = 0101
B 0 1 0 1 B … B 0 1 0 1 B …
1 2 1
R 2
L
B 0 1 0 1 B B 0 1 0 B B …
1 1
2 3 2
B 1
R
L L
B 0 1 0 B 1 … B 0 1 B B 1 …
1 1
2 3 2
B 0
R
L L
B 0 1 B 0 1 … B 0 B B 0 1 …
1 1
2 3 2
B l
R
L L
B 0 B 1 0 1 … B B B 1 0 1 …
1 1
2 3 2
B 0
R
L L
No input Halt
Properties of Context Free Languages 4.58
Turing Machine
0 / 0, R q2
0 / B, R B/ 0, L
1/1, R
B/ B, L
q0 q1 q4
1/ B, R B/1, L
q3
B/ B, N
B/ B, L
q5
is given by
0 1 B
Q
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 Theory of Computation
9
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.
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.
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
q1
0 / x, R B/ 0, L
# / #, L
q0 q3 1/1, L
1/ y, R B/1, L 0 / 0, L
q2
# / #, N
# / #, R
q4 1/1, R
x / 0, R
0 / 0, R
y /1, R
Representation of „101‟
1 0 1 # B
y 0 1 # B
y 0 1 # 1
2 1
L
4.6 Theory of Computation
1
1 0 1 # 1
1 x 1 # 1 B
R 2
1
1 x 1 # 1 0
1 0 1 # 1 0
1 R
2
1 0 y # 1 0 B
1 0 y # 1 0 1
R 2
1
1 0 1 # 1 0 1
1 R
Stop
2
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}
Properties of Context Free Languages 4.62
is given by
0 1 # B x y
Q
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
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.
Turing Machine
x / x, R
a / a, R
b / b, R
q2
a / x, R B/ a, L
a / a, R x / x, L
b / b, R a / a, L
q0 q1 q4 b / b, L
b/ x, R B/ b, L
B/ B, R
q3
a / a, N
q6 q5
b/ b, N a / a, R x / x, L
b / b, R
x / B, R
x / x, R
a b b B a b x B a x x b B
1 R 1 R
2 L 1 2
a x x b b B x x x b b B x x x b b a B
L 1 1 L 1
R
2 2
2
B x x b b a B B B x b b a B B B B b b a B
1 1 1
R No movement
R R
4.6 Theory of Computation
5
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
a b b a xR b 2 b a x1 x b a
2
x 1 R
x x x a B x x x x B Halt
1 R 1 N
R
2
Turing Machine
x / x, R
a / a, R
x / x, R a / x, R q1 b/ x, L
a / a, L
q0 q3 b / b, L
b / x, R a / x, L x / x, L
q2
B/ B, N
x / x, R
b/ b, R
q4
B/ B, R
Properties of Context Free Languages 4.66
is given by,
a B x B
Q
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 Theory of Computation
7
5. Design a TM that recognizes strings of the form an bn cn | 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
y / y, R b / b, R
a / a, R z / z, R
a / x, R b/ y, R
q0 q1 q2
y / y, R c / z, L
y / y, R x / x, R z / z, L
q4 q3 b / b, L
z / z, R
a / a, L
B/ B, N
y / y, L
q5
is given by
a b c x y z B
Q
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
= 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 Theory of Computation
9
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 / a, R
b / b, R
a / a, R
b / b, R B/ B, L
q1 q3 q5
a / B, R a / B, L
B/ B, N
a / a, L
q0 B/ B, N q8 q7
b / b, L
b/ B, R B/ B, N b/ B, L
B/ B, L
q2 q4 q6
a / a, R
b / b, R a / a, R
b / b, R
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
q2
B/ B, L
q4 q6
a / a, R
b / b, R
B/ B, R
a / a, R B/ B, L
q0 q1 q2
0 / 0, R 0 / 0, R
q0 q1 q2
1/1, R
1/1, R B/ B, L
q3
Properties of Context Free Languages 4.72
The machine starts the computation corresponding to the input string, in the domain
of „f‟ and halts with the output string f().
Turing machine
with „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 : 1 2
* *
M = (Q, ∑, , , q0, B, F)
.
*
Input string, where
1
f() Output string after computation of „‟ by f f
*
2 .
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
10
2 00
3 000
4 0000
5 00000
.
.
.
n „n‟ number of zeros [0n]
*
(q 0, 0n ) (q ,f 0f (n) )
M
where,
q0 initial state
0n input string
qf final state
0f(n) output of M, after computing 0n in „f‟
PROBLEMS
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:
01011010000
No change
101001
10100110000
4.7 Theory of Computation
5
1/1, R 0 /1, L
0 / 0, R 0 / 0, L 1/ 0, L
1/1, L
q0 q1 q2
B/ B, L
B/ B, R
B/ B, R
q3
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,
0 1 B
Q
* q3
Instantaneous description for = 10100
Properties of Context Free Languages 4.76
– B01100B Halt
q3
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
B / B, L
q0 q1
M = (Q, ∑, , , q0, B, F)
where, Q = {q0, q1}
= {0, 1}
4.7 Theory of Computation
7
= {0, 1, B}
Transition table,
0 1 B
Q
* q1
Example: 4 0000
10
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
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
# / 0, R B / B, L 0 / B, N
q0 q1 q2 q3
Transition table,
0 # B
Q
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 Theory of Computation
9
– B00#000B – B00#000B – B00#000B
q0 q0 q0
– B000000B – B000000B – B000000B
q1 q1 q1
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
# / #, R
0 / 0, R M>n
0 / B, R B / B, L # / 0, N
q0 q1 q2 q5
0 / B, L
B / B, N
0 / B, R q3 0 / 0, L
# / #, L
M≤n
q4
M = (Q, ∑, , , q0, B, F)
where,
Q = {q0, q1, q2, q3, q4, q5}
= {0, #}
= {0, #, B}
q0 = {q0}
B = {B}
F = {q5}
is given by
0 # B
Q
q0 (q1, B, R) (q4, B, R) -
q2 (q3, B, L) (q5, 0, N) -
q4 (q4, B, R) - (q5, B, N)
* q5
# / #, R
0 / 0, R 0 / 0, R
0 / x, R # / #, R 0 / x, R
q0 q1 q2 q3
# / #, L B / 0, L
# / #, N
x / 0, R x / 0, R
q5 q4 0 / 0, L
0 / 0, L # / #, L
q6
# / #, L
M = (Q, ∑, , , q0, B, F)
where,
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 Theory of Computation
3
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
Turing Machine
0 / 0, R
# / #, R
0 / x, R B / 0, L # / #, L
q0 q1 q2 0 / 0, L
# / #, R
x / 0, R
0 / 0, R
q3
# / #, R # / #, R
B / #, L 0 / 0, R 0 / 0, R
0 / 0, L B / B, R 0 / x, R # / #, R x / 0, R
q4 q5 q6 q7 q8
# / #, L
# / #, L B / 0, L
x / 0, L 0 / x, R
# / #, N q10 q9
q11 0 / 0, L
# / #, L
B / B, L 0 / 1, N
q0 q1 q2
B / 1, N
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
B / 0, N
q0 q1
0 / x, R B / 0, L # / #, L
q0 q2 0 / 0, L
# / #, L x / x, R
x / 0, L q3 q4
# / #, R
Properties of Context Free Languages 4.86
TM for multiplication
B / B, R 0 / 0, L
q5
0 / B, R
0 / 0, L
# / #, L
0 / 0, R q0 q0 Copy q4 q7 q8
# / #, R Routine 0 / 0, L
B / B, R
# / B, R
q12 q11 q10
# / B, R
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 | 0y. 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
B / 1, L 1 / 0, L 0 / 0, L
q0 q1 q2 1 / 1, L
1 / 1, R
B / B, R 0 / 0, R 0 / 0, R
0 / x, R 1 / 1, R 0 / x, R
q3 q4 q5 q6
1 / 1, L
x / 0, R
# / #, N B / 0, L
x / 0, R
q9 q8 q7
0 / 0, L 1 / 1, L
1 / 1, L 0 / 0, L
4.8 Theory of Computation
7
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.
(q, a) = (q’’, b, R)
(q, a) = (q’b, M)
where,
M=L/R/N
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.
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.
Multitrack Turing machine contain multiple tracks (k) each with set of elements
leading to k symbols on the tape.
… …
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 Theory of Computation
9
… …
…
Subroutine
Turing
called by
machine
main ( )
…
The Turing machine is designed to process the subroutine of a program.
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.
B a0 a1 a2 … ai … ak B
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.
… a1 a2 a3 b1 b2 b3 c1 c2 … Input
tape
Head 1 Head 2
Head 3
Finite control
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)
4.9 Theory of Computation
1
Multi-tape Turing machine
A multitape TM is finite control with more than one tape for reading/writing symbols,
storing states etc.
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
Head 3 Head 2
Head 1
Finite control
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
a1 a2 … am B … B b1 b2 … bm B … B … …
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
4.9 Theory of Computation
3
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
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
Example
a / a, R
a / x, R
q0 q1
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
wєL
YES
M
wL Loops Forever
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.
wєL
YES
M
wL NO
If w L, then M halts with answer, “NO”. This is also called as Turing Decidable
language.
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
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.
RE
YES
M1
YES
wєΣ* M3
RE
M2 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 w L1 or w L2 . Else M3 loops
forever if both M1 and M2 loop forever.
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.
w L1 YES
M1
wєΣ* YES
M3
M2
w L YES
From the above design of TM, if w L, if w L, then M1 accepts w 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.
5.4 Theory of Computation
M1 M2
w L NO YES
R R
Let w L, then M1 accepts w and halts with “YES”.
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.
YES
YE
M1 NO
wєΣ*
M3
YES
M2 NO
NO
Undecidability 5.5
If w L1 , then M1 accepts and thus M3 also accepts since L(M3) = L(M1) u L(M2).
Hence M3, M2, M1 halt with either YES or NO on all possible inputs.
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.
NO
NO
wєΣ*
M1
YES
M3 L M3 L(M1 ) L(M 2 )
YES
YES
M2
NO
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”.
5.6 Theory of Computation
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 w L1 then M1 returns “YES” accepting the input. Else will not halt after
rejecting w L1 .
Similarly if a string, w L2 , then M2 returns “YES” else rejects „w‟ and loop forever.
M1
wЄ∑* YES
YES
M3
YES Loop Forever
M2
Rejects & Never Halts
w L 2
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 w L1 and w L2 . Else M3
loops forever along with M1 or M2 or both
Hence the intersection of two recursively enumerable languages is recursively
enumerable.
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.
Theorem
Halting problem of Turing machine is unsolvable / undecidable.
Proof
The theorem is proved by the method of proof by contradiction.
Let us assume that Turing machine is solvable / decidable.
Construction of H1
M Halting HALT
machine H1 LOOP FOREVER
5.8 Theory of Computation
Construction of H2
M Halting HALT
machine H2 LOOP
Construction of H3
M HALT LOOP
H2 H3
LOOP HALT
H3
H3
PARTIAL SOLVABILITY
Problem types
There are basically three types of problems namely
Decidable / solvable / recursive
Undecidable / unsolvable
Semi decidable / partial solvable / recursively enumerable
Undecidable problem
A problem, P is said to be undecidable if there is a Turing machine, TM that doesn‟t
decides P.
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)
Theorem
A language L ∑* is recursively enumerable if and only if L can be enumerated by
some TM.
Proof
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) >
k(M1)].
M2 compares its input symbols to that of the symbols before „#‟ while, M1 is in pause.
HALTS HALTS
∑* M1 M2
( L) ( L)
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.
5.12 Theory of Computation
EXAMPLE
1. For Σ = {a, b} with A = {a, aba3, ab} and B = {a3, 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
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
RICE‟S THEOREM
Rice‟s theorem is named after the inventor Henry Gordon Rice.
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
Explanation
Consider a language L such that
L = {M | L(M) has some property P}
where,
P is non-trivial of TM,M
Then the language L is undecidable.
That is, every non-trivial property of the language of Turing machines is Undecidable
Proof
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
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.
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.
Concept of UTM
The universal Turing machine, Tu takes over the program and the input set to process
the program.
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.
Finite
control
Tape of M 0001010000101….
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.
S(ε) = 0
Undecidability 5.15
T → terminal symbol
where
q → initial state
m → transition of TM
Example
b / b, R b / b, L
/ , R a / b, L / , S
q0 q1 q2 q3
/ , L
Inputs = {a,b}
Moves = {L,R,S}
States = {q0,q1,q2,q3}
→ 0 [since s() = 0]
TM q 0 , I, w
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)
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).
Operation of UTM
UTM checks the input to verify whether the code for TM=<T,w> is a legitimate
for some TM.
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 1oj 1ok 1ol 1om on the first tape, with oi (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 ok as in the
transition.
Replace o j by o on tape-2 to indicate the input change.
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.
T Turing machine
w any input string accepted by T
It can also be represented as = e(T) e(w) .
Theorem
Proof
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 Lu (complement of Lu) is Turing
acceptable.
Undecidability 5.19
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.
This is the algorithm for Ld. Hence Lu is Recursively Enumerable but not recursive.
Example: If the language L1 Time (f), then L is tractable and is less complex in
nature
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
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
PSpace U Space(Cnk )
c>0, k 0
The SAT problem is to find if there is any interpretation that satisfies the given
Boolean equation.
If there is no assignment obtained and the evaluation gives out „FALSE‟, then the
Boolean equation is said to be unsatisfiable.
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 Boolean clause is said to be Horn clause if it contains at most one positive literal.
x1 Vx2
x1 ( x1 x2 x3 ) (x1 x2 ) CNF
where,
C1 , C2 , C3 Conjuncts / clauses.
K-satisfiability
Generalized form of CNF with each clause containing upto „k‟ literals.
The encoding scheme omits parenthesis and OR (V) notation. The variables are
denoted by unary notation of their subscripts.
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
P AND NP COMPLETENESS
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
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