0% found this document useful (0 votes)
448 views107 pages

Tcs Notes For Academy Tcs Notes For Academy

The document is a set of notes on Theoretical Computer Science from the University of Mumbai, focusing on Finite State Machines (FSM) and their applications. It includes various examples and exercises for designing FSMs to check divisibility of numbers, validate string patterns, and manage state transitions. The notes also cover essential concepts such as state functions, machine functions, and the characteristics of start, final, and dead states.

Uploaded by

sawant.hemant11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
448 views107 pages

Tcs Notes For Academy Tcs Notes For Academy

The document is a set of notes on Theoretical Computer Science from the University of Mumbai, focusing on Finite State Machines (FSM) and their applications. It includes various examples and exercises for designing FSMs to check divisibility of numbers, validate string patterns, and manage state transitions. The notes also cover essential concepts such as state functions, machine functions, and the characteristics of start, final, and dead states.

Uploaded by

sawant.hemant11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 107

lOMoARcPSD|43069021

Tcs notes for academy

Theoretical Computer Science (University of Mumbai)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

INDEX
Chapter No Chapter Name Page Nos

1 FSM 2-11

2 RE 12-23

3 DFA & NFA 24-53

4 Moore & Mealy machine 54-65

5 Grammars 66-85

6 PDA 86-99

7 Turing Machine 100-103

8 UNDECIDABILITY 104-106

1
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

CHAPTER NO -1
Finite state machine (FSM):
Definition :
FSM consists of finite set of state(s) that alter on receiving the I/p set (I) to producer the O/p (O)

FSM defines two functions :-


State function :- STF :- S  I → S

Machine function :-MAF :- S  I → O

Q.1. Design FSM to check whether the given decimal number is divisible by 3. (S – 10 m)

Solution :
Step I :- Theory
(Definition of FSM)

Step II :- Logic
Input (I) = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Output (O) = {y, n}
State (S) = {qs, q0, q1, q2}
(qs is the start stage q0 is the state where remainder is 0. q1 is the state where remainder is 1. q2 is the state
where remainder is 2)

Step III :
Implementation
S/I {0 ,3, 6, 9} {1, 4, 7} {2, 5, 8}
R qs q0 q1 q2
0 q0* q0 q1 q2
1 q1 q1 q2 q0
2 q2 q2 q0 q1

STF : S  I → S

S\I {0, 3, 6, 9} {1, 4, 7} {2, 5, 8}


qs y n N
q0* y n N
q1 n n Y
q2 n y N
MAF = S  I → 0

2
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

{0,3,6,9} - y

{2,
5 ,8}
-y
q0
y { 1,
}- 4,7
7} - y
{1,4, , 6,9
}-
n
,3
{0
{0,3,6,9} - n
Start {1,4,7} - n
q3 q1

{2,5,8} - y
{2
,5,
8} n
-y }-
4,7
{1,

{2
q2

,5 ,8
}-
y
{0,3,6,9} - n

Step IV : - eg :-

Q.2. Design FSM to check whether the given decimal number is divisible by 4.

Solution :
Step I :- Theory of FSM
Step II :- Logic
I= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,}
O = {y, n}
S= {qs, q0, q1, q2, q3}
Step III : Implementation
S\I {0, 4, 8} {1, 5, 9} {2, 6} {3, 7}
qs q0 q1 q2 q3
q0* q0 q1 q2 q3
q1 q2 q3 q0 q1
q2 q0 q1 q2 q3
q3 q2 q3 q0 q1
SFI = S  I → S

S\I {0, 4, 8} {1, 5, 9} {2, 6} {3, 7}


qs y n n N
q0* y n n N
q1 n n y N
q2 y n n N
q3 n N y N
MAF = S  I → 0

Step IV : eg ;- {qs, 57246}


t (q1, 7246)
t (q1, 246)
t (q0, 46)
t (q0, 6)
t (q2) → n

3
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q.3. Design a divisibility by 4 tester FSM for Binary Numbers (means to check whether binary
number is divisible by 4)

Solution :
Step I :- Theory of FSM

Step II :- Logic
I= (0, 1)
O = (y, n)
S= (qs, q0, q1, q2, q3)

Step III :- Implementation


S/I 0 1
qs q0 q1
q0* q0 q1
q1 q2 q3
q2 q0 q1
q3 q2 q3

STF : S  I → S

Step IV :- e.g. :(qs, 10 11 000)


(q1, 0 11 000)
(q2, 11 000)
(q1, 1000)
(q3, 000)
(q2, 00)
(q0,0)
(q0) → y

4
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q.4. Design FSM to check whether the given ternary number is divisible by 5

Solution :
Step I : Theory of FSM

Step II : Logic
I= (0, 1, 2)
O = (y, n)
S= (qs, q0, q1, q2, q3, q4)

Step III : Implementation


(3R + 0) mod 4 (3R + 1) mod 4 (3R + 2) mod 4
S\I 0 1 2
R qs q0 q1 q2
*
0 q0 q0 q1 q2
1 q1 q3 q4 q0
2 q2 q1 q2 q3
3 q3 q4 q0 q1
4 q4 q2 q3 q4

SFT : S  I → S

Step IV :- e.g. (qs, 2102)


(q2, 102)
(q2, 0,2)
(q1, 2)
(q0) – y

Q.5. Design FSM in which the input is valid if it ends in “100”  = {0, 1}

Solution :-

Step I : Theory of FSM

Step II : Logic
I= (0, 1)
O = (y, n)
S= (qs, q0, q1, q2, q3)

Step III : Implementation


S\I 0 1
ends in qs q0 q1
0 q0 q0 q1
1 q1 q2 q1
10 q2 q3 q1
100 q3* q0 q1

5
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Step IV :e.g. – (qs, 10100)


t (q1, 0100)
t (q2, 100)
t (q1, 00)
t (q2, 0)
t (q3) – y

Q.6. If it ends in “babb” over  = {a, b}

Solution :
Step I : Theory of FSM

Step II : Logic
I= (a, b)
O = (y, n)
S= (qs, q0, q1, q2, q3, q4)

Step III : Implementation


S\I A b
ends in qs q0 q1
a q0 q0 q1
b q1 q2 q1
ba q2 q0 q3
bab q3 q2 q4
babb q4* q2 q1

Q.7. If it contains “bba” over  = {a, b}

Solution :
S\I A b
contains qs q0 q1
a q0 q0 q1
b q1 q0 q2
bb q2 q3 q2
bba q3* q3 q3 = trap state

6
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q.8. If it does not contain “bbb” over  = {a, b}

Solution :

S/I A b
Does not qs* q0 q1
contains
a q0* q0 q1
b q1* q0 q2
bb q2* q0 q3
bbb q3 q3 q3 =dead state

Points to Remember :
1. There is always one start state.
2. There can be one / more final state.
3. Start state can also be final state. (In such case Blank is valid)
4. Dead state is trap state which is non-final.
5. Every dead state is a trap state. But a trap state may / may not be dead.
6. Start state  initial state.
Final state  Accepting state
Now-final state  Rejecting state
7. Dead state is always rejecting state.
Q.9. Design FSM to accept the strings if it start with three consecutives a’s over  = {a, b}
Solution :
S\I A B
A qs q0 q3
a q0 q1 q3
aa q1 q2 q3
aaa q2* q2 q2
q3 q3 q3

Q.10. Design FSM to accept the strings if it starts either with “011” or “100” over  = {0, 1}

Solution :
S\I 0 1
A qs q0 q3
0 q0 q6 q1
01 q1 q6 q2
011 q2* q2 q2
1 q3 q4 q6
10 q4 q5 q6
100 q5* q5 q5
q6 q6 q6

7
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q.11. Design FSM to accept the strings if it contains ‘a’ at every even position over  = {a, b}
Length of a string S\I A b
|x| qs* q1 q1
Even q0* q1 q1
Odd q1* q0 q2
dead state q2 q2 q2

Q.12. Design FSM to accept the strings if it contains ‘a’ at every even position and ‘b’ at every odd
position over  = {a, b}

Solution :
S\I a b
|x| qs* q2 q1
even q0* q2 q1
odd q1* q0 q2
dead state q2 q2 q2

Q.13. Design FSM to accept the strings if it contains exactly 3a’s over {a, b}

Solution :

S\I a b
No. of a’s qs q1 q0
0 q0 q1 q0
1 q1 q2 q1
2 q2 q3 q2
3 q3* q4 q3
More than 3 q4 q4 q4

Q.14. Design FSM to accept the strings if it contains at the most 3a’s over {a, b}

Solution :

S\I a b
No. of a’s qs* q1 q0
0 q0* q1 q0
1 q1* q2 q1
2 q2* q3 q2
3 q3* q4 q3
More than 3 q4 q4 q4

8
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q.15. Design FSM to accept the strings if it contains at least 3a’s over {a, b}

Solution :

S\I a b
No. of a’s qs q1 q0
0 q0 q1 q0
1 q1 q2 q1
2 q2 q3 q2
3 q3* q4 q3
More than 3 q4* q4 q4

Q.16. Design FSM to accept the strings if it contains odd number of b’s over  = {a, b}

Solution :
S\I a b
nb(x) qs q0 q1
even q0 q0 q1
odd q1* q1 q0

Q.17. Design FSM to accept the strings if it contains even number of a’s and odd number of b’s over  =
{a, b}

Solution :
S\I a b
na(x) nb(x) qs q2 q1
even even q0 q2 q1
even odd q1* q3 q0
odd even q2 q0 q3
odd odd q3 q1 q2

Q.18. Design FSM to O/P the remainder when binary number is divided by 3.
Solution:
I= {0, 1}
O = {0, 1, 2}
S= {qs, q0, q1, q2}

(2R + 0) mod 3 (3R + 1) mod 3


S\I 0 1
Remainder qs q0 q1
0 q0 q0 q1
1 q1 q2 q0
2 q2 q1 q2
STF : S  I → S

9
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

MAF:- S X I O
S\I 0 1
R qs 0 1
0 q0 0 1
1 q1 2 0
2 q2 1 2

Note : If output set (O) is not (y, n) then in such a case there won’t be any final state.

Q.17. Design FSM to implement Binary Adder


OR
Design FSM to add two binary number of same length.

Solution :
I= {(0, 0), (0, 1), (1, 0) (1, 1)}
O = {0, 1}
S= {qs, q0, q1)

S\I (0, 0) (0, 1) (1, 0) (1, 1)


qs q0 q0 q0 q1
NC q0 q0 q0 q0 q1
C q1 q0 q1 q1 q1

STF = S  I → S

S\I (0, 0) (0, 1) (1, 0) (1, 1)


qs 0 1 1 0
NC q0 0 1 1 0
C q1 1 0 0 1
MAF = S  I O

10
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• FSM Properties :-
1) Periodicity:
A FSM do not have the capacity to remember large amount of information because it has
limited number of states and this sets the limit to the length of the sequence it can
remember therefore it repeats some state or states again and again, that means some
sequence of states will be repeated periodically.
2) State Determination:
Since the initial state of a FSM is known and the input sequence is given and hence output
sequence can be determined. So it is always possible to discover some unknown state, in
which the FSM resides at a particular instance.
3) Impossibility of multiplication:
A FSM cannot remember long sequences and for multiplication operation there is a need
to remember two full long sequences, one for multiplier and other for multiplicand. Also
the model should be capable of remembering partial sums that are obtained at intermediate
stages of multiplication. Therefore no FSM can multiply two given arbitrarily long
numbers.

4) Impossibility of Palindrome Recognition:


No FSM can recognize a palindrome since it cannot remember long sentences, so it cannot
remember all the symbols it read until the half way of the input sequence in order to match
them with the symbols in the other half of the input sequence in the reverse order.

5) Impossibility to check well-formedness of parenthesis:


As FSM has no capability to remember all the earlier inputs to it, cannot compare with the
remaining to check well formedness. It is impossible task for any FSM.

11
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

CHAPTER NO 2
Regular Expression(RE)
Fundamentals :
• Alphabet () :
It is defined as the finite set of i/p symbols e.g. :  = {0, 1, 2}

• String / sentence / word :


It is defined as the finite sequence of symbols over the give 
e.g. : 0, 1, 2, 00, 111, 201, 220, …

• String length :
It is defined as the number of symbols present in given string.
e.g. x = 12012
x=5
Note :
The string of length zero (0) would be denoted by E(Epsilon)

• Language :
It is defined as the set of strings defined over the given  (alphabet)
 x ends in " ba"
e.g. : L = x 
 over  a, b 
L = {ba, aba, bba, aaba, ….}

• Operation on languages:-
(1) Union of 2 languages :
 x E L1 
L1  L2 = x, y 
 y E L2 
(2) Concatenation of 2 languages :
 x E L1 
L1 L2 = L1 L2 = x, y 
 y E L2 

• Example on union and concatenation of 2 languages :


e.g.: L1 = {00, 10, 110}
L2 = {aa, ba}
L1  L2 = {00, 10, 110, aa, ba}

L1 L2 = {00aa, 00ba, 10aa, 10ba, 110aa, 110ba}

(3) Closure of a language:



L* =  Li
i =0

12
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

(4) Positive closure of a language :



L* =  Li
i =1

examples:
L = {ba} = L1 L3 = L.L.L. = {bababa}
L2 = L.L. = {baba} L0 = {  }

L* = L0  L1  L2  …… = { , ba, b aba, …..}


L* = L1  L2  L3  …. = {ba, b aba, ba ba ba, ……}

Regulation expression (R.E.) :


Definition
R.E. is used for specifying the strings of regular language and is defined as :-
(1) ‘’ is R.E. for specifying { } (null set)
(2) ‘’ is R.E. for specifying {  } (Epsilon)
(3) ‘a’ is R.E. for specifying {a}
(4) Let R & S be two R.E. for specifying LR & LS respectively.
(i) (R) / (S) is R.E. for respectively LR  LS
(ii) (R) (S) is R.E. for respectively LR . LS
(iii) (R)* is R.E. for respectively LR*
e.g.
R.E. RL
 
r (cr)
a {a}
b {b}
a/b {a}  {b} = {a, b}
a+b {a, b} + is also used for or symbol
a.b {a} . {b} = {ab}
ab {ab}
a *
{, a, aa, aaa, …}
a + {a, aa, aaa, …}
(ab) *
{, ab, abab, …}
(a + b) *
{, a, b, aa, ab, ba, bb, …}

Note :
(a + b)* = (a + b)0  (a + b)1  (a +b)2  (a + b)3…
Where (a +b)2 = (a + b).(a + b) AND (a + b)3 =(a + b).(a + b).(a + b)
r L(r)
(00) *
{, 00, 0000, ….}
0.(00)* {0, 000, 00000 ….}
*
(00) . 0 {0, 000, 00000 ….}
(000)* {,000,000000,000000000…}
a.a * {a, aa, aaa, …..}

13
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Note :
(1) 0. =  . 0 = 0
(2) 1.  = .1 = 1
(3) a.=.a=a
(4) a+ = a . a*
(5) L+ = L.L*

Write R.E. for the following :


Q.1. Set of all strings that start with ‘a’ over  = {a, b}
Ans. r = a . {a + b)*
L(r) = {a, aa, ab, abb, ….}

Q.2. Set of all strings that end either in ‘0’ or ‘11’ over  = {0, 1}
Ans. r = (0 + 1)* 0 + (0 + 1)* 11
L(r) = {0, 00, 10, 000, 11, 011, 111, …..}

Q3. Set of all strings that start with ‘x’ and end with ‘y’ over  = {x, y}
Ans. r = x . (x + y)* . y
L(r) = {xy, xxy, xyy….}

Q.4. Set of all strings that start ‘0’ and end with ‘10’ over  = {0, 1}
Ans. r = 01 . (0 + 1)* . 10 + 010
L(r) = {010, 0110, 01010, 01110, …..}

Note : We have to consider cases of over lapping while solving the problems.

Q.5. Set of all strings that start with ‘xyyy’ and end with ‘yyyx’ over  = {x, y}
Ans. r = xyyy (x + y)* . yyyx + xyyyx + xyyyyx + xyyyyyx
L(r) = {xyyyx, xyyyyyyx, xyyyxyyyx, xyyyyx, xyyyyyx, ….}

Q.6. Set of all strings that start and end with different letter over  = {a, b}
Ans. r = a(a + b)* b + b(a + b)* a
L(r) = {ab, aa, aab, abb, ba, baa, bba, …..}

Q.7. Set of all strings that start and end with same symbol over  = {0, 1, 2}
Ans. r = 0 (0 + 1 + 2)* . 0 + 1.(0 + 1 + 2)* . 1 + 2 (0 + 1 + 2)* . 2 + 0 + 1 + 2
L(r) = {00, 000, 010, 020, 11, 101, 111, 121, 22, 202, 212, 222, 0, 1, 2, …..}

Q8. Set of all strings that contain at least one occurrence of “a” over  = {a, b}
Ans. r = (a + b)* aa (a + b)*
L(r) = {aa, aaa, baa, abbaa, ….}

Q.9. Set of all strings that contain at least two a’s over  = {a, b}
Ans. r = (a + b)* a (a + b)* a (a + b)*
L(r) = {aa, aaaa, ….}

14
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q.10. Set of all strings that contain exactly two a’s over  = {a, b}
Ans. r = b* a b* a b*
L(r) = {aa, babab, …..}

Q.11. Set of all strings that contain at most two a’s over {a, b}
Ans. r = b*ab*ab* + b*ab* + b*
L(r) = {,b,ab,ba,aa,aab…}

Q.12. Set of all strings that contain even numbers of a’s over  = {a, b}
Ans. r = (b*a b*a b*)* + b*
L(r) = {,bbb,babab,aa,aaaa,…}

Q.13. Set of all strings such that no(0) mod 3 = 0 over  = {0, 1}
Ans. r = (1* 0 1* 0 1* 01*)* + 1*
L(r) = {,111,1010101,000,1000,…}

Q.14. Set of all strings that contain at least one ‘x’ and at least one ‘y’ over  = {x, y}
Ans. r = (x + y)* x(x + y)* y(x + y)* + (x + y)* y(x + y)* x(x + y)*
L(r) = {xy,yx,xxy,yyx,…}

Q.15. Set of all strings that contain at least one ‘x’ at least one ‘y’ and at least one ‘z’ over  = {x, y, z}
Ans. (x + y + z)* x(x + y + z)* y(x + y + z)* z(x + y + z)* + (x + y + z)* y(x + y + z)* x(x + y + z)*
z(x + y + z)* + (x + y + z)* z(x + y + z)* y(x + y + z)* x(x + y + z)* + (x + y + z)* y (x + y + z)*
z (x + y + z)* x (x + y + z)* + (x + y + z)* z (x + y + z)* x (x + y + z)* y (x + y + z)* + (x + y + z)*
x (x + y + z)* z (x + y + z)* y (x + y + z)* y
L(r) = {xyz,xzy,yzx,yxz,zxy,zyx,…}

Note : Regular expressions are said to be identical if they specify the same regular language

Q.16. Verify the following :


(i) L(M + N) = LM + LN

Ans. (i) Let L = a*, M = b*, N = c*


p = L(M + N) q = LM + LN
* * *
p = a (b + c ) q = a* b* + a* c*
L(p) = {, a, b, c, aa, bbb, ……}
L(q) = {, a, b, c, aa, bbb, …..}
 l(p) = L(q)
L(M + N) = LM + LN

15
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Closure Properties of R.E. ∑.


1. The union of two regular languages is regular.
2. The intersection of two regular languages is regular
3. The complement of a regular language is regular.
4. The difference of two regular languages is regular.
5. The reversal of two regular languages is regular.
6. The closure (star) of a regular language is regular.
7. The concatenation of regular languages in regular.
8. A homomorphism (Substitution of strings for symbols) of a regular language is regular.
9. The inverse homomorphism of a regular language is regular.

Our First closure properties are the three Boolean operations: Union, intersection and
complementation.
1. Let L and M be languages over alphabet ∑. Then L ∪ M is the language that contains all the
strings that are in either or both L and M.
2. Let L and M be languages over alphabet ∑. Then L ∩ M is the language that contains all the
strings that are in both L and M.
3. Let L and M be languages over alphabet ∑. Then 𝐿̅, the complement of L, is the set of strings
in ∑∗ that are not in L.
➢ Closure under union
Theorem 1: If L and M are regular languages, then so is L ∪ M.
o Closure under Regular Operations
The proof that regular languages are closed under union was exceptionally easy because
union is one of the three operations that define the regular expressions. The same idea as
theorem 1 applies to concatenation and closure as well.
That is:
▪ If L and M are regular languages, the so is LM
▪ If L is a regular language, then so is 𝐿∗
Proof: This proof is simple. Since L and M are regular, they have regular expressions. Say
𝐿 = 𝐿(𝑅) and 𝑀 = 𝐿(𝑆). Then 𝐿 ∪ M = L(R + S) by the definition of the + operator for
regular expressions.

➢ Closure under Complementation


Theorem 2: If L is a regular language over alphabet ∑, then 𝐿̅ = ∑∗ − 𝐿 is also a regular
language.
Proof: Let 𝐿 = 𝐿(𝐴) for some DFA. 𝐴 = (𝑄, ∑ , 𝛿, 𝑞₀ , 𝐹). Then 𝐿 = 𝐿(𝐵), where B is
the DFA(𝑄, ∑ , 𝛿, 𝑞₀ , 𝑄 − 𝐹). That is, B is exactly like A, but the accepting states of A
have become non accepting states of B and vice versa. Then w is in L (B) if and only if
𝛿(𝑞₀ , 𝑤) is in −𝐹 , which occurs if and only if w is not in L (A).

➢ Closure under intersection


Theorem 3: If L and M are regular languages, then so is L ∩ M
Proof: Let L and M be the languages of automata

16
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

𝐴𝐿 = (𝑄𝐿 , ∑, 𝛿𝐿 , 𝑞𝐿 , 𝐹𝐿 ) and 𝐴𝑚 = (𝑄𝑚 , ∑, 𝛿𝑚 , 𝑞𝑚 , 𝐹𝑚 ). Notice that we are assuming that


the alphabets of both automata are the same; that is, ∑ is t, he union of the alphabets of L
and M, if those alphabets are different. The product construction actually works for NFA’s
as well as DFA’s, but to make the argument simple as possible, we assume that 𝐴𝐿 and 𝐴𝑚
are DFA’s.

For 𝐿 ∩ 𝑀 we shall construct an automaton A that simulates both 𝐴𝐿 and 𝐴𝑚 . The states
of A are paits of states, the first from 𝐴𝐿 and the second from 𝐴𝑚 . To design the
transitions of A, Suppose A is in state (p,q) where p is the state of 𝐴𝐿 and q is the state of
𝐴𝑚 . If a is the input symbol, we see what 𝐴𝐿 does on input a; say it goes to state S. We
also see what 𝐴𝑚 does on input a; say it makes a transition to state t. Then the next state
of A will be (s,t). In that manner, A has simulated the effect of both 𝐴𝐿 and 𝐴𝑚
The remaining details are simple. The start state of A is the pair of start states of
𝐴𝐿 and 𝐴𝑚 . Since we want to accept if and only if both automata accept, we select as the
accepting states of A all those pairs (p,q) such that p is an accepting state of 𝐴𝐿 and q is an
accepting state of 𝐴𝑚 . Formally we define:
𝐴 = ( 𝑄𝐿 𝑋 𝑄𝑚 , ∑ , 𝛿, ( 𝑞𝐿 , 𝑞𝑚 ) , 𝐹𝐿 𝑋 𝐹𝑚 )
Where
𝛿((𝑝, 𝑞), 𝑎) = (𝛿𝐿 (𝑝, 𝑎), 𝛿𝑚 (𝑞, 𝑎))
To see why 𝐿(𝐴) = 𝐿(𝐴𝐿 ) ∩ 𝐿(𝐴𝑚 ) , first observe that an easy induction on |w|
proves that 𝛿̂ (( 𝑞𝐿 , 𝑞𝑚 ), 𝑤) = ( 𝛿̂𝐿 ( 𝑞𝐿 , 𝑤), 𝛿̂𝑚 ( 𝑞𝑚 , 𝑤)) But A accepts w if and only if
𝛿̂ (( 𝑞𝐿 , 𝑞𝑚 ), 𝑤) is a pair of accepting states. That is, 𝛿̂𝐿 ( 𝑞𝐿 , 𝑤) must be in 𝐹𝐿 , and
𝛿̂𝑚 ( 𝑞𝑚 , 𝑤) must be in 𝐹𝑚 . Put another way, w is accepted by A if and only if both 𝐴𝐿 and
𝐴𝑚 accept w. Thus, A accepts the intersection of L and M.
➢ Closure under Difference
Theorem 4 : If L and M are regular languages, then so is L-M
Proof : observe that 𝐿 − 𝑀 = 𝐿 ∩ 𝑀̅ . By theorem 2, 𝑀
̅ is regular and by theorem 3 𝐿 ∩ 𝑀
̅.
Therefore 𝐿 − 𝑀 is regular.
➢ Reversal
Theorem 5: If L is a regular language, so is 𝐿𝑅
Proof: Assume L is defined by regular expression E. the proof is a structural induction on
the size of E. we show that there is another regular expression 𝐸 𝑅 such that L(𝐸 𝑅 ) =
(𝐿(𝐸))𝑅 ; that is the language of 𝐸 𝑅 is the reversal of language of E.
Basis: If E is ε, Φ or b then 𝐸 𝑅 is the same as 𝐸. That is, we know {𝐸}𝑅 ={ ε },Φ𝑅 = Φ , and
{𝑎}𝑅 = {𝑎}
Induction: there are three cases, depending on the form of E.
1] 𝐸 = 𝐸1 + 𝐸2 . Then 𝐸 𝑅 = 𝐸1𝑅 + 𝐸2𝑅 . 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] 𝐸 = 𝐸1 𝐸2 . Then 𝐸 𝑅 = 𝐸2𝑅 𝐸1𝑅 Note that we reverse the order of the two languages, as
well as reversing the languages themselves. For instance, if 𝐿(𝐸1 ) =
{01,111} 𝑎𝑛𝑑 𝐿(𝐸2 ) = {00,10}, 𝑡ℎ𝑒𝑛 𝐿(𝐸1 𝐸2 ) = {0100,0110,11100,11110}.
The reversal of the latter languages is {0010,0110,00111,01111}.
If we concatenate the reversals of 𝐿(𝐸2 ) and 𝐿(𝐸1 ) in that order we get
{00,01}{01,111} = {0010,00111,0110,0111}

17
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Which is the same language as (𝐿(𝐸1 𝐸2 ))𝑅 . In general if a word w is 𝐿(𝐸) is the
concatenation of 𝑤1 from 𝐿(𝐸1 ) and 𝑤2 from 𝐿(𝐸2 ) then 𝑤 𝑅 = 𝑤2𝑅 𝑤1𝑅
3] 𝐸 = 𝐸1∗ then 𝐸 𝑅 = (𝐸 𝑅 )∗ . The justification is that any string w in 𝐿(𝐸)can be written
𝑅 𝑅
as 𝑤1 , 𝑤2 , 𝑤3 … 𝑤𝑛 , where each 𝑤1 is in 𝐿(𝐸). But 𝑤 𝑅 = 𝑤𝑛𝑅 𝑤𝑛−1 𝑤𝑛−2 … . 𝑤1𝑅
Each 𝑤𝑖𝑅 is in 𝐿(𝐸 𝑅 ), so 𝑊 𝑅 is in 𝐿((𝐸1𝑅 )∗ ). Conversely, any string in 𝐿((𝐸1𝑅 )∗ ). Is of the
form 𝑤1 𝑤2 … 𝑤𝑛 , where each 𝑤𝑖 is the reversal of a string in 𝐿(𝐸1 ) The reversal of this
𝑅 𝑅
string, 𝑤𝑛𝑅 𝑤𝑛−1 𝑤𝑛−2 … . 𝑤1𝑅 is therefore a string in L(𝐿(𝐸1∗ ) which is L( E ). We have thus
shown that a string is in 𝐿(𝐸) if and only if its reversal is in 𝐿((𝐸1𝑅 )∗ ).
➢ Homomorphism:
A string homomorphism is a function on strings that works by substituting a particular
string for each symbol.
Theorem 6: If L is a regular language over alphabet ∑ and h is a homomorphism on ∑,
then h(L) is also regular.
Proof: Let 𝐿 = 𝐿(𝑅) for some regular expression R. In general, if E is a regular
expression with symbols in ∑, let ℎ(𝐸) be the expression we obtain by replacing each
symbol a of ∑ in E by ℎ(𝑎). We claim that ℎ(𝑅) defines the language ℎ(𝐿)
The proof is an easy structural induction that says whenever we take a
subexpression E of R and apply h to it to get ℎ(𝐸), the language of ℎ(𝐸) is the same
language we get if we apply h to the language 𝐿(𝐸).
Formally, 𝐿(ℎ(𝐸)) = ℎ(𝐿(𝐸))
➢ Inverse Homomorphism:
If h is a homomorphism from alphabet ∑ to alphabet T and L is a regular language over T,
then ℎ−1 (𝐿) is also a regular language.
Proof: The proof starts with a DFA A for L we construct from A and h a DFA for ℎ−1 (𝐿)
using the plan suggested. This DFA uses the states of A but translates the input symbol
according to h before deciding on the next state.
Formally, let 𝐿 𝑏𝑒 𝐿(𝐴)𝑤ℎ𝑒𝑟𝑒 𝐷𝐹𝐴 𝐴 = (𝑄, 𝑇, 𝛿, 𝑞0 , 𝐹). 𝐷𝑒𝑓𝑖𝑛𝑒 𝑎 𝐷𝐹𝐴
𝐵 = (𝑄, ∑ , 𝛾, 𝑞0 , 𝐹)
Where transition function is constructed by the rule

18
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Algebraic laws for RE:-


1) Associativity and commutativity
Commutativity is the property of an operator that’s says we can switch the order of its
operands and get the same result. An example for arithmetic was given above : x + y = y + x.
Associativity is the property of an operator that allows us to regroup the operands when the
operator is applied twice. For example, the associative law of multiplication is (𝑥 𝑋 𝑦) 𝑋 𝑧 =
𝑥 𝑋 (𝑦 𝑋 𝑧 ). Here are three laws of these types that hold for regular expressions:
• L+M=M+L. This law, the commutative law for union says that we may take the union
of two languages in either order.
• (L+M)+N=L+(M+N). this law, the associative law for union says that we may take the
union of three languages either by taking the union of the first two initially or taking
the union, we conclude that we can take the union of any collection of languages with
any order and grouping and the result will be the same. Intuitively, a string is in 𝐿1 ∪
𝐿2 ∪ … ∪ 𝐿𝑘 if and only if it is in one or more of the 𝐿𝑖 ′𝑠
• (LM)N=L(MN). This law, the associative law for concatenation says that we can
concatenate three languages by concatenating either the first two or the last two
initially.
Note : Concatenation is not commutative
i.e. LM is not equal to ML.
2) Identities and Annihilators:-
An identity for an operator is a value such that when the operator is applied to the identity and
some other value, the result is the other value. For instance, 0 is the identity for addition
0+x=x+0=x and 1 is the identity for multiplication, since 1 ∗ 𝑥 = 𝑥 ∗ 1 = 𝑥. An annihilator
for an operator is a value such that when the operator is applied to the annihilator and some
other value the result is the annihilator. For instance, 0 is the annihilator for multiplication
since 0 * x = x * 0=0. There is no annihilator for addition.
There are three laws for regular expressions involving these concepts, we list them below.
• Φ+L=L+Φ=L. This law assets that Φ is the identity for union.
• εL=Lε=L. This law assets that ε is the identity for concatenation.
• ΦL=LΦ=Φ. This law assets that Φ is the annihilator for concatenation.
3) Distributive Laws
A distributive law involves two operators, and assets that one operator can be pushed down to
be applied to each argument of the other operator individually. The most common example
from arithmetic is the distributive law of multiplication over addition, that is, 𝑋 (𝑦 + 𝑧) =
𝑥 𝑋 𝑦 + 𝑥 𝑋 𝑧 . Since multiplication is commutative, it doesn’t matter whether the
multiplication is on the left or right of the sum. However there is an analogous law for regular
expressions that we must state in two forms, since concatenation is not commutative. These
laws are:
• L(M+N)=LM+LN. This law is the left distributive law of concatenation over union.
• (M+N)L=ML+NL. This law is the right distributive law of concatenation over union.
4) The Idempotent Law
An operator is said to be idempotent if the result of applying it to two of the same values of
arguments is that value. The common arithmetic operators are not idempotent; 𝑥 + 𝑥 ≠ 𝑥 in
general and 𝑥 ∗ 𝑥 ≠ 𝑥 in general (although there are some values of x for which the equality

19
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

holds, such as 0+0=0) However, union and intersection are common examples of idempotent
operators. Thus for regular expressions, we may asset the following law:
• L+L=L. This law, the idempotent law for union, states that if we take the union of two
identical expressions, we can replace them by one copy of the expression.

20
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Decision Properties of R.E.


There are different ways of representing a regular language and procedures to convert from one in
to another. Irrespective of the representation there are some fundamental questions that need to be
answered about the the language.
And the questions are:-
➢ Is the given language empty?
➢ Is the language finite?
➢ Does the string belong to the language?
➢ Are the two languages equivalent?
1) Testing Emptiness of Regular languages:-
➢ The language is said to be empty when there exists no path from start to any of the accepting
state {i.e. final state} If any path exist then the language is said to be non empty.
➢ Deciding whether we can reach an accepting state from the start state is a simple instance of a
graph reachability.

2) Testing membership in a regular language


➢ Let us consider a string w and a regular language L, here we need to determine that whether x
is a member of L.
➢ If L is represented by a DFA, simulate the DFA processing the string input symbols x,
beginning in the start state. If the DFA Ends in an accepting state (Final state), then string x is
said to be a member of L or it is not a member of L
➢ If L has any other representation besides a DFA, we could convert it into a DFA and then
decide whether string x is a member of L or not.

3) Testing equivalence of states:-


➢ The two states are said to be equivalent if the two states are not distinguishable.
➢ The table filling algorithm is a recursive discovery of distinguishable pairs in DFA.

4) Testing Equivalence of regular languages:


➢ The table filling algorithm gives us an easy way to test if two regular languages are the same
suppose L and R are two languages represented by their respective DFA’s.
➢ Now test if the start states of the two original DFA’s are equivalent, using the table filling
algorithm. If they are equivalent, then L=R and if not then L≠R.

21
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Pumping Lemma for Regular Sets:


Let L be a regular set. Then there is a constant n, such that if z is in L and |z|>=n, then we may
write z=uvw in such a way that
1. |𝑢𝑣| ≤ 𝑛
2. |𝑣| ≥ 1
3. For all i>=0 𝑢𝑣 𝑖 𝑤 is in L.
Theorem
Let 𝑀 = (𝑄, ∑ , 𝛿, 𝑞0 , 𝐹) be a finite automaton with n states.
Let L be the regular set accepted by M. Let w ∈ L and |𝑤| ≥ 𝑚
If 𝑚 ≥ 𝑛, then there exists p, q and r such that w=pqr, 𝑞 ≠∧ and 𝑝𝑞 𝑖 𝑟 ∈ L for each i>=0
Proof:
Let 𝑤 = 𝑎1 , 𝑎2 , 𝑎3 , … 𝑎𝑚 𝑚 ≥ 𝑛
𝛿(𝑎0 , 𝑎1 , 𝑎2 , … 𝑎𝑖 ) = 𝑞𝑖 𝑓𝑜𝑟 𝑖 = 1,2, … , 𝑚; 𝑄1 = {𝑞0 , 𝑞1 , 𝑞2 , … 𝑞𝑚 }
That is, 𝑄1 is the sequence of states in the path with path value.
𝑤 = 𝑎1 , 𝑎2 , 𝑎3 , … 𝑎𝑚 . As there is only n distinct states, at least two states in 𝑄1 must coincide.
Among various pairs of repeated states, we take the first pair.
Let us take them as 𝑞𝑗 and 𝑞𝑘 (𝑞𝑗 = 𝑞𝑘 ). Then j and k satisfy the condition 0 ≤ 𝑗 < 𝑘 ≤ 𝑛.
The string w can be decomposed into three substrings 𝑎1 , 𝑎2 , … 𝑎𝑗 , 𝑎𝑗+1 , 𝑎𝑗+2 , … 𝑎𝑘 and
𝑎𝑘+1 , 𝑎𝑘+2 , … 𝑎𝑚
Let p,q,r denote these strings 𝑎1 , 𝑎2 , … 𝑎𝑗 , 𝑎𝑗+1 , 𝑎𝑗+2 , … 𝑎𝑘 and 𝑎𝑘+1 , 𝑎𝑘+2 , … 𝑎𝑚
Respectively.
As 𝑘 ≤ 𝑛, |𝑝𝑞| ≤ 𝑛𝑎𝑛𝑑 𝑤 = 𝑝𝑞𝑟 the path with path value w in the transition diagram of m is
shown in the figure.
q

p qj = qk r qm
q0

Fig: String accepted by M

The automaton M starts from the initial state 𝑞0 on applying the string p, it reaches 𝑞𝑗 (= 𝑞𝑘 ).
On applying the string q, it comes back to 𝑞𝑗 (= 𝑞𝑘 ). So after application of 𝑞 𝑖 for each i=0,
the automaton is in the same state 𝑞𝑗 .
On applying z, it reaches 𝑞𝑚 , a final state. Hence 𝑝𝑞 𝑖 𝑟 ∈ L. As every state in 𝑄𝐿 is
obtained by applying an input symbol, 𝑞 ≠∧.

22
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Applications of R.E.
1] Lexical Analysis:-
R.E. is used in specifying the component called “Lexical Analyser”. Lexical analysis is the
first phase out of six phases in compiler. In this phase it scans the source program
characters that belong together logically. Keywords and identifiers are common examples
of tokens.
2] Finding patterns in text:-
We get a fair bit idea of the pattern we want to recognize with the help of R.E. Since R.E.
is used for describing the patterns with little effort and it is easy to modify the description
quickly when things go wrong. Regular expressions are then converted into NFA or DFA
which are stimulated to produce program that recognizes patterns in text.
3] IN UNIX:-
There are several text handling programs commonly available on UNIX systems that make
use of regular expressions to specify portions of text for processing. These includes editors
such as ed and sed, text searching programs like grep and egrep.

23
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

CHAPTER NO 3
DFA & NFA
Finite Automata (FA):-

Finite Automata is considered to be a mathematical model of machine or a system.

* Model of (FA) :
i/p tape
a a b b c c

read head

Finite State
Control

Fig. Model of FA

• Component of FA :
F.A. consists of finite set of states, i/p tape and read head.

• Working of F.A. :
Depending on the state and i/p symbol :
F.A. can change the state or remain in same state.
F.A. moves the head to the right of current cell.

• Variations of F.A. :

F.A.

F.A. with fianl F.A. with o/p


state and no and no final
o/p state

DFA NFA More M/C Mealy M/C

24
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Differentiate between NFA and DFA


DFA NFA
1 In DFA from each state on each input 1 In NFA from each state on each i/p
symbol there is exactly one transition. symbol there can be 0, 1 or more
transition.
2 In DFA the transition function is defined 2 In NFA the transition function is defined
as S : Q x  → Q as S : Q x  → 2Q
3 In DFA we cannot have epsilon () 3 In NFA we can have epsilon ()
transition. transition.
4 The implementation of DFA is simple. 4 The implementation of NFA is difficult
because of its non-determination nature.
5 Example:- 5 Example:-
1
0
1 qB

start
qA 0
1

0 qC

• Deterministic FA (DFA) :
Definition :
DFA consist of finite set of state, one state is called start state and there can be one / more final
states.
In DFA from can state on each i/p symbol there is exactly one transition.

• M = (Q,  , δ, q0, F)
The above expression is known as 5 tuple representation.
Where,
Q = finite set of states
 = i/p alphabet
δ,= transition function δ, : Q x  → Q 1
0
q0 = Start state q0 E Q
F = Finite set of final states FCQ

• e.g. : start
1
Q = {qA, QB, qC} qA qB
 = {0, 1}
q0 = qA
1
F = {a, c}
δ, = 0 0

Q\ 0 1 qC
→ qA qA qB
qB qC qB
qC* qA qB

25
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Working of DFA :
(1) (qA, 110) (2) (qA, 1001)
+ (qB, 10) + (qB, 001)
+ (qB, 0) + (qC, 01)
+ (qC, ) + (qA, 1)
Accept + (qB, )
Reject
• Non-Deterministic FA (NFA) :
Definition
NFA consists of finite set of states. One stat is called start state and there can be one or more final
states.
In NFA, from each sate, on each i/p symbol there can be 0, 1 or more transitions.
• M = (Q,  , δ,, q0, F)
The above expression is known as 5 tuple representation.
Where,
Q = finite set of states 0
 = i/p alphabet
δ, = transition function δ,: Q x  → 2Q
q0 = start state q0 E Q
F= finite set of final states F ⊊ Q

start 1
• e.g. qA qB
Q = {qA, qB, qC}
 = {0, 1} 0
q0 = qA 1
δ, = 0
Q\ 0 1 1
→ qA qA qB qC
qB {qA, qC} {}
*
qC qA qB

• Working of NFA :
(1) (qA, 1010) (2) (qA, 110)
t (qB, 010) t (qB, 10)
Reject

t (qA, 10) t (qC, 10)


t (qB, 0) t (qB, 0)

t (qA, ) t (qC, ) t (qA, ) t (qC, )

26
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Design :
1. RE to NFA guidelines :
• Divide the given R.E. into smaller sub expression and create NFA for each using Rule 1, 2
&3
• Combine the above NFA’s using Rule 4.

Rule 1 : NFA for r =  { }


Start


Rule 2 : NFA for r = E {}
Start

Rule 3 : NFA for r = a { a }


Start a

Rule 4 : (i) NFA for r = (R) / (S) LR  LS

N (R)


Start

 
N (S)

(ii) NFA for r = (R) . (S) LR . LS

Start
N (R)
 N (S)

OR

Start
N (R) N (S)

(iii) NFA for r = (R)* LR* 


Start  N (R)
 3


27
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

(P) Design NFA following :


(1) r=a
Solution :
Start a
0 1
NFA for r = a
(2) r=b
Solution :
Start b
0 1

NFA for r = b

(3) r=a+b
Solution :
1
a
2 

Start

0 5


b
3 4
NFA for r = a + b

(4) r=a.b


Solution :
Start a b
0 1 2 3

OR

Start a b
0 1 3

NFA for r = a . b

(5) r = a* 
Solution :
Start  1
a 2
 3


NFA for r = a*

28
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

(6) r = a . a* which is canal to a+

Solution :

Start a
1
 2
a 3
 4


NFA for r = a+
(7) r = (a + b)*

Solution :

(8) r = 0(0 + 1)* 10

Solution : 

3
0 4
 
Start 0
1
 2 7
 8
1
9
0 10
 
5
1 6


(9) r = 10 + (0 + 10)* 1
Solution :
1 0
 1

2 3

Start
14

0
 6 7

 1
13
4 5 11 12

8 1 9 0 10


29
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

10. Dec 2011 / 5M Q/C


Convert the following RE to NFA whit 
r = (1(00)* 1 + 01* 0)*
Solution :

11) r = (a + b)*abb

a

2 3 
Start   a
8
b
9
b
1 6 7 10
 
b
4 5

•  closure of a state :
It is defined as the set of states that are reachable from that state by walking on -transitions only
concluding the state.
e.g. (1) -closure (0) = {0, 1, 2, 4, 7}
(2) -closire (5) = {1, 2, 4, 5, 6, 7}

30
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

•  closure of set of states :


It is defined as the union of -closures of each state of the set.
e.g. : -closure ({3, 8})
= -closure (3)  - closure (8)
= {1, 2, 3, 4, 6, 7}  {8}
= {1, 2, 3, 4, 6, 7, 8}

2) NFA to DFA
x y =  - closure (x) δ (y, a) δ (y, b)
{0} {0, 1, 2, 4, 7} A {3, 8} {5}
{3, 8} {1, 2, 3, 4, 6, 7, 8} B {3, 8} {5, 9}
{5} {1, 2, 4, 5, 6, 7} C {3, 8} {5}
{5, 9} {1, 2, 4, 5, 6, 7, 9} D {3, 8} {5, 10}
{5, 10} {1, 2, 4, 5, 6, 7, 10} E {3, 8} {5}

δ:

Q\ a b
→A B C
B B D X
C B C
D B E
E* B C

B b
a
a
Start
A a D
a
b
b
C b E
b

DFA for r = (a + b)* abb

31
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

3) DFA to min DFA (classical method):-


States can be merged if
(All state have same transition) and (All are final OR all are non-fina)

δ:
Q\ a B
→X B X
B B D
D B E
E* B X

B
b a a
Start
X a b E
b
D
b

Min DFA for r = (a + b)* abb

32
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q) Construct FA for r = (11 + 10)*


Ans. Step 1 : R.E. to NFA :

1 1
2 3 4
 

Start  
0 1 8 9


1 0
5 6 7

Step 2 : NFA to DFA


x y =  - closure (x) δ (y, 0) δ (y, 1)
{0} {0, 1, 2, 5, 9} A {} {3, 6}
{3, 6} {3, 6} B {7} {4}
{7} {1, 2, 5, 7, 8, 9} C {} {3, 6}
{4} {1, 2, 4, 5, 8, 9} D {} {3, 6}
{} {} E {} {}

δ:
Q\ 0 1
→ A* E B
B C D
C* E B
D* E B
E E E

33
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Step 3 : DFA to min DFA :


Q\ 0 1
→P * E B
B P P
E E E

B
l
O,l
P 0,1
0
E

q) Dec 2009/10 M
Construct NFA from r = (0 + 1)* (0 + 0 + 11) and convert into min DFA.
Ans. Step 1 : R.E. to NFA

Step 2 : NFA to DFA


x y =  - closure (x) δ (y, 0) δ (y, 1)
{0} {0, 1, 2, 4, 7, 8, 11} A {3, 9 } {5, 12}
{3, 9} {3, 6, 7, 8, 11, 1, 2, 9, 4} B {3, 9, 10} {5, 12}
{5, 12} {1, 2, 4, 5, 6, 7, 8, 11, 12} C {3, 9} {5, 12,13}
{3, 9, 10} {1, 2, 4, 3, 6, 7, 8, 11, 9, 10, 14)} D {3, 9, 10} {5, 12}
{5, 12, 13} { 1, 2, 4, 5, 6, 7, 8, 1, 12, 13, 14} E {3, 9} {5, 12, 13 }

δ:

34
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q\ 0 1
→A B C
B D C
C B E
D* D C
E* B E

Step 3 : DFA to min DFA


1
B
0 0
1 C
0
A

0 1
1

D
E

0
1

35
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Note : NFA with  has same number of closure as same number of states.

4) NFA with  to NFA without  guidelines:-


(1) The number of states remain the same.
(2) Start state remains the same.
(3) Final states remain the same.
(4) If the  closure of the start state contains a final state then make the start state also a final
state.

q)
a b

Start
q0  q1

Ans.
x y =  closure (x) P = δ (y, a) -closure (p) q = δ (y, b) -closure (q)
q0 {q0, q1} {q0) {q0, q1} {q1} {q1}
q1 {q1) {} {} {q1} {q2}

δ:
Q\ a B
→ q0* {q0, q1} {q1}
q1* {} {q1}
a b

Start a, b
q0 q1

Dec 2010 / 5M
Q) Convert the following NFA with  to NFA without 

Start 
1 2
a
a a,b
3

b
36
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Ans.
x y =  closure (x) P = δ (y, a) -closure (p) q = δ (y, b) -closure (q)
1 {1, 2} {1, 3) {1, 2, 3} {} {}
2 {2} {1} {1, 2} {} {}
3 {3} {2} {2} {2, 3} {2, 3}

δ:
Q\ a B
→ 1* {1, 2, 3} {}
2* {1, 2} {}
3 {2} {2, 3}

a a
a
Start a
1 2

a a,b
3

37
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

5) NFA without  to DFA

Q) Convert NFA to DFA :


 { p, q, r , s} {0,1}  P {S} 
 , , , ,  (Give tupple representation)
 q   S F 
δ:
Q\ 0 1
p p, q P
q r R
r s -
s s S
(Problem is NFA became it consist of transition 0, 1, 2, and it is seen in the table)
Ans:

0,1
q 0,1
0
Start
P r
0

0,1

x y =  - closure (x) δ (y, 0) δ (y, 1)


{p} {p} A {p, q} {p}
{p, q} {p, q} B {p, q, r} {p, r}
{p, q, r} {p, q, r} C {p, q, r, s} {p, r}
{p, r} {p, r} D {p, q, s} {p}
{p, q, r, s} {p, q, r, s} E {p, q, r, s} {p, r, s}
{p, q, s} {p, q, s} F {p, q, r, s} {p, r, s}
{p, r, s} {p, r, s} G {p, q, s} {p, s}
{p, s} {p, s} H {p, q, s} {p, s}

38
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

δ:
Q\ 0 1
→A B A
B C D
C E D
D F A
E* E G X
F* E G
G* F H
Y
H* F H

δ:
Q\ 0 1
→A B A
B C D
C X D
D X A
X* X Y
Y* X Y Z

S:
S= 0 1
Q\
→A B A
B C D
C Z D
D Z A
Z* Z Z

1 B
0 0
Start
A C
1 1
1
0 D
Z
0
0,1
39
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Dec. 2011 / 5M
Q) Convert the following NFA to DFA :
δ:
Q\ 0 1
→P p, q P
q r, s T
r p, r T
s*  
t*  

Ans. 0

0,1 q
0 0 0
Start 0 r
P
1

0 S
t
1

x y =  - closure (x) S(y, 0) S(y, 1)


{p} {p} A {p, q} {p}
{p, q} {p, q} B {p, q, r, s} {p, t}
{p, q, r, s} {p, q, r, s} C {p, q, r, s} {p, t}
{p, t} {p, t} D {p, q } {p}

δ:
Q\ 0 1
→A B A
B C D
C* C D
D* B A

40
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Start 1
A B

0
1 0

1 C
D
0

41
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

6) Transition Diagram to R.E. :


Step 1:
Write the state equation by 100 king at the incoming transitions
(Add  to the start state equation)

Step 2:
Perform any combination of
* Substitution
* Rearrangement
* Arden’s formula
Until we are able to get find state’s equation in term of  (re)

Arden’s theorem :
Let P and Q be two expressions and if P is -free, then the following equation of R is R = Q + RP
has unique solution given by R = QP

Q) Find RE for the following :


1)

Start b
q0 q1
Ans. Step 1 :
a
q0 = q0a + q1a + 
q1 = q0b

Step 2:
q0 = q0a + q0ba +  (S)
q 0  q0 (a + ba)
= + (R)
R Q R P

Notes : In such problems dead states can be ignored


By Arden’s theorem,
q0 = (a + ba)*
q0 = (a + ba)* C (a + ba)* = (a + ba)*
q1 = (a + ba)* b

42
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

a
2) Start b
q0 q1
a b a

q2 q3
b

Ans. Step 1 :
q0 = q1b + q2a + 
q1 = q0a
q3 = q1a + q2b + q3a + q3b
q2 = q0b

Step 2:
q0 = q1b + q0b +  (S)
q0 = q0ab + q0ba +  (S)
q 0  q0 (ab + ba)
= + (R)
R Q R P
By Arden’s formula
q0 =  (ab + ba)*
q0 = (ab + ba)*
q2 = (ab + ba)* b = r1
q1 = (ab + ba)* a = r2
r = r1 + r 2
r = (ab + ba)* a + (ab + ba)* b

3) Set of all the string that do not contain consecutive 0’s over  {0, 1}
Ans. Step 1 : Find transition diagram :


S\I 0 1
→ qs* q0 q1
q1
0 q0* q2 q1
1 q1* q0 q1
00 q2 q2 q2

S/I 0 1
q0* q2 q1
→ q1* q0 q1
q2 q2 q2

43
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

1
0
Start
q1 q0
1
0

q2

q1

Step 2 : Find RE
Step 1 : q1 = q11+ q01 + 
q0 = q10

Step2 : q1 + q11 + q1 01 +  (S)


q1  + q1 (1 + 01)
= (R)
R Q R P
 By Arden’s formula
q0 = (1 + 01)*
q1 = (1 + 01)* = r1
q1 = (1 + 01)* 0 = r2
r = r1 + r 2
r = (1 + 01)* + (1 + 01)* 0

June 2012 / 5M
Q) Set of all strings such that number of 0’s are odd over  = {0, 1}
Ans. Step 1 : Find TD
S\I 0 1
no (x)
even
→ qS
q0
q1
q1
q0
q0
q0 
odd q1* q0 q1

S/I 0 1
→ q0 q1 q0
q1* q0 q1

1
1
0
Start
q0 q1
0

44
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Step 2 : Find R.E.


Step 1 : q0 = q01 + q10 + 
q1 = q00 + q11
q q00 + q1 1
Step 2 : 1 =
R Q R P
By A.T.
q1 = q00(1)*

q0 = q01 + q 0(1)* 0 +  (S)


q0 =  + q0 (1 + 01* 0) (R)
R Q+R P
By A.T.
q0 =  (1 + 01*0)*
q0 = (1 + 01* 0)*
q1 = (1 + 01*0)* 01*

45
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

7) FSM to DFA :
Dec 2009 / 5M
Q) Design DFA for accepting strings that contain “010” as substring over  = {0, 1}
Ans. Step 1 : Theory of DFA
Step 2 : Logic
S\I 0 1
→ qS

end sin q0 q1
0 q0 q0 q2 q1
1 q1 q0 q1
01 q2 q3 q1
*
010 q3 q3

Step 3 : Implementation
DFA
M = (Q, , δ, q0, F) (Stapple representation)
Q = {q0, q1, q2, q3} {q1, q2, q3, q4}
 = {0, 1}
q0 = q1
F = {q3}
δ=
Q\ 0 1
q0 q0 q1
→q1 q0 q1
q2 q3 q1
q3* q3 q3

1 0 0,1
Start 0 1 0
q1 q0 q2 q3
1

Step 4 : example
(q1, 10100)
(q1, 0100)
(q0, 100)
(q2, 00)
(q3, 0)
(q3, )

46
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q) Dec. 2011 / 8 m
Obtain DFA to accept strings that contain exactly three a’s over  = {a, b}
Ans. Logic
S\I 0 1
→ qS

no (x) q1 q0
0 q0 q1 q0 q0
1 q1 q2 q1
2 q2 q3 q2
3 q3* q4 q3
More than 3 q4 q4 q4

June 2011 / 5 m
Q) Design DFA for the following over  = {0, 1}
L = {w / every odd position of w is 1}
Ans. Logic :
S\I 0 1
x →qs*

q2 q1
even q0* q2 q1 q0
odd q1* q0 q0
Dead state q2 q2 q2

Dec. 2010 / 5 m
Q) Design DFA for set of all string that start and end with different letter over  = {x, y, z}
Ans. Logic :
S\I X y z
SE → qs q0 q4 q8
xx q0 q0 q1 q2
xy
xz
q1*
q2*
q0
q0
q1
q1
q2
q2
q1 

yx q3* q3 q4 q5
yy q4 q3 q4 q5 q3

 q6
yz q5* q3 q4 q5
zx q6* q6 q7 q8
zy q7* q6 q7 q8
zz q8 q6 q7 q8

47
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

8) DFA min (Box Method) :


(Based on Myhill – Nerode theorem)

Step 1 : Remove all those states that are not reachable from start state.

Step 2 : Create a table such that the rows and columns can be identified by state number.

Step 3 : All those cells in which one state is final and other is non-final should be marked with “1” (since
such states represent distinguishable states)

Step 4 : For the remaining unmarked cells perform the following check.

Let (p, q) be an unmraked cell and let ‘a’ be some i/p symbol

* if S(p, q) = r and S(q, a) = r


then no decision can be taken
* if S(p, a) = r and S(q, a) = s.
then if cell (r, s) is marked then mark (p, q) with “2” else list (r, s) → (p, q)

If cell (p, q) is still unmarked then repeat step 4 for (p, q) with another i/p (if available)

Step 5 :
All those cells that are unmarked can be merged into single state respectively (since such states represent
equivalent states)

Q) Min the following DFA


Diagram
Ans. Note : If a DFA is given and we are asked to minimize if then always do it by using box method
else do it, using classical method.

Start 0 0
a b e
1 0 1 1
0 1

c 0 f
d
1
1

48
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Step 1:
 ‘f’ is not reachable from ‘a’ we eliminate f
δ:
Q\ 0 1
→a b C
b b D
c* b C
d b E
e* b C

Step 2 and step 3 :


b 2
c 1 1
d 2 1
e 1 1 1
a B C d
Step 4 :
Let (a,b) be an unmarked cell
δ (a, 0) = b δ (b, 0) = b 
c d
δ (a, 1) = δ (b, 1) = ✓
r s

Let (a,d) be an unmarked cell


δ (a, 0) = b δ (d, 0) = b 
c e
δ (a, 1) = δ (d, 1) =
r s
List (c, e) → (a, d)
(if cell (c, e) gets marked then go back and mark (a, d) with “2”)

Let (b,d) be an unmarked cell


δ (b, 0) = b δ (d, 0) = b 
d e
δ (b, 1) = δ (d, 1) = ✓
r s
Let (c, e) be an unmarked cell
δ (c, 0) = b δ (e, 0) = b 
δ (c, 1) = c δ (e, 1) = c 
0
Step 5 :

b
0 0
1
1
Start
[a, b] 1
[c, e]

49
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

June 2011 / 10 m
Q) Find min FA equivalent to the following :
S:
Q\ 0 1
→a b A
b a C
c d B
d* d A
e d F
f g E
g f G
h g D
Ans.
1
b c
0 1 0
0 0
1
a 1 d

0
1
e
1
0 1
0 f
h g 0

Step 1 :
 e, f, g, and h are non-reachable from start that’s why we eliminate them.
δ:

Q\ 0 1
→a b C
b a C
c d B
d* d A

Step 2 and step 3 :


b 2
c 1 2
d 1 1 1

50
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

a B C
Step 4 :
Let (b,a) be an unmarked cell
a b
δ (b, 0) = δ (a, 0) =
r 5
List (a, b) → (b, a)
δ (b,1)= c δ (a,1)=a

List (c,a) (b,a)

Let (c,a) be an unmarked cell


d b
δ (c, 0) = δ (a, 0) =
r 5

Let (c,b) be an unmarked cell


d a
δ (c, 0) = δ (b, 0) =
r 5

Q) Minimize the following DFA, where q0 is start state and q3 an q5 are final states.
A B
→ Q0 Q1 Q3
Q1 Q0 Q3
Q2 Q1 Q4
Q3* Q5 Q5
Q4 Q3 Q3
Q5* Q5 Q5
Ans.
A
A
Q1 Q2
A B

Start
Q0 B Q3

A,B A,B
B
Q4
Q5
A,B

Step 1 :
 States Q2 and Q4 are not reachable from start state they are removed.

51
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

A
Q1
A B

Q0 B Q3

A,B

Q5
A,B
Step 2 and step 3 :

Q1
Q3 1 1
Q5 1 1
Q0 Q1 Q3

Step 4 :
Let (Q0,Q1) be an unmarked cell
Q1 Q0
δ (Q0, A) = δ (Q1, A) =
r 5
List (Q0, Q1) → (Q1, Q0)

Q3 Q3
δ (Q0, B) = δ (Q1, B) = 
r r

Let (Q3,Q5) be an unmarked cell


Q5 Q5
δ (Q3, A) = δ (Q5, A) = 
r r

Q5 Q5
δ (Q3, B) = δ (Q5, B) = 
r r

Step 5 :

52
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Applications of DFA & NFA:-


Regular Expressions are used to specify Regular Languages and DFA & NFA are used
to recognize Regular Language.
1) Lexical Analyser:-
Regular Expressions are used to describe one of the components named or
termed as Lexical Analyser.
Lexical Analyser scans the source program and identifies the tokens. The
tokens of a particular type are grouped to form a Regular Language.
DFA & NFA are stimulated to recognize the strings whether they belong to a
particular Regular language or not.
2) Searching using Regular Expressions in Text editors:-
The word which is to be searched is stimulated by DFA & NFA and on the
basis of stimulation, it is concluded whether the word was found or not.

53
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

CHAPTER NO 4
Moore and Mealy Machine
Moore M/c :
Definition : It is a FA with no final state and it produces the o/p sequence for the given i/p sequence.
In Moore m/c, the o/p symbol is associated with each state

Six tupple representation is given below :


M = (Q, , , , , q0)
Where
Q = finite set of states
 = i/p alphabet
 = o/p alphabet
 = transition function : Q x  → 2Q
 = o/p mapping  : Q → 
q0 = start state q0 E Q 0
1
e.g. :
Start qA/a 1
qB/b

0 0

1
qC/c
Q = {qA, qB, qC}  = {a, b}
 = {0, 1} q0 = qa
= =
Q\ 0 1  (q, B) = a
→ qA qA qB  (q, B) = b
qB qC qB  (q, C) = a
qC qB qA

Working :
n n+1
(qA, 1101) abbaa
+ (qB, 101)
+ (qB, 01)
+ (qC, 1)
+ (qA, )

54
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Mealy M/c :
Definition : It is a FA with no final state and it produces the o/p sequence for the given i/p sequence.
In mealy m/c, the o/p symbol is associated with each transition.

M = (Q, , , , , q0)
Where,
Q = finite set of states
 = i/p alphabet
 = o/p alphabet
 = transition function  :Qx→Q
 = o/p mapping :Q x→
q0 = start state q0 E Q
0/a
e.g. : 1/c qB

Start 1/b
qA
1/a
0/c

qC

1/C
Q = {qA, qB, qC}  = {a, b, c}
 = {0, 1} q0 = qA
:- =

Q\ 0 1 Q/ 0 1
→ qA qA qB → qA a c
qB qA qC qB b a
qC qA qC qC c c

Working :
n n
(qA, 1101) c acc
(qB, 101)
(qC, 01)
(qA, 1)
(qB)

55
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q) Differentiate between Moore M/c and Mealy m/c.


Moore M/c Mealy M/c.
1 In Moore machine the output symbol is 1 In Mealy machine the output symbol is
associated with each state. associated with each transition.
2 In Moore M/c the output is dependent on 2 In Mealy M/c the output is dependent on
the state. the state and input.
3 In Moore M/c. the output mapping is 3 In Mealy M/c. the output mapping is
defined as  : Q →  defined as  : Q x  → 
4 In Moore M/c if the length of input 4 In Mealy M/c if the length of input
sequence is n then the length of the output sequence is n then the length of the output
sequence is n + 1 sequence is also n.
5 In Moore M/c. we can get the output on 5 In Mealy M/c. we cannot get the output
. on .
6 Example on Moore M/c. 6 Example on Mealy M/c.

* Minimization of Moore machine :


States can be merged if
(1) All states have same transitions and
(2) Output symbols along with the states are also same.

June 2010 / 5m

Q) Design Moore machine to o/p. ‘A’ if i/p ends in “101”, o/p ‘B’ if i/p ends in “110”, otherwise o/p
‘c over  = {0, 1}
Ans. Step 1 : Theory
Definition of Moore machine

Step 2 : Logic
S\I 0 1 o/p
ends
0
→qs
q0
q0
q0
q1
q1  C
C
q0
1 q1 q2 q4 C
10 q2 q0 q3 C
101 q3 q2 q4 A
11 q4 q5 q4 C
110 q5 q0 q3 B

Step 3 : Implementation
M = {Q, , , , , q0)
Q = {q0, q1, q2, q3, q4, q5}
 = {0, 1}
 = {A, B, C}
q0 = q0

56
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

: =
S\I 0 1
→q0 q0 q1  = (q0) = C
q1 q2 q4  = (q1) = C
q2 q0 q3  = (q2) = C
q3 q2 q4  = (q3) = A
q4 q5 q4  = (q4) = C
q5 q0 q3  = (q5) = B

Step 4 : eg :-
(q0, 1101)CCCBA
+ (q1, 101)
+ (q4, 01)
+ (q5, 1)
+ (q3, )
(Since last letter of the output is A sequence ends in 101)
q) Design Moore machine to print residue module 4 for banking numbers.
Ans. Step 1 : Define of Moor machine

Step 2 :
(2R + 0) mod 4 (2R + 1) mod 4
S\I 0 1 o/p
R
0
→qs
q0
q0
q0
q1
q1  0
0
q0
1 q1 q2 q3 1
2 q2 q0 q1 2
3 q3 q2 q3 3

57
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Step 3 : Implementation
M = {Q, , , , , q0)
Q = {q0, q1, q2, q3 }
 = {0, 1}
 = {0, 1, 2, 3}
q0 = q0
= =
Q\ 0 1
→ q0 q0 q1  (q0) = 0
q1 q2 q3  (q1) = 1
q2 q0 q1  (q2) = 2
q3 q2 q3  (q3) = 3

0 0

0
q0/0 1 q1/1 q2/2
1

1 0

q3/3

Step 4 : (q0, 1101) 0


(q1, 101) 1
(q3, 01) 3
(q2, 1) 2
(q1, ) 1

* Search and Replace :


Note :
1) The logic of searching is equal to the logic of ends in without any final state.
2) In Moore and Mealy machine intermediate bits cannot be changed.

58
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q) Design Moor machine to change each occurrence of “1000” to “1001” over  = (0, 1}
Ans. Step 1 : Definition

Step 2 :
S\I 0 1 o/p

0
→qs
q0
q0
q0
q1
q1  0
0 q0
1 q1 q2 q1 1
10 q2 q3 q1 0
100 q3 q4 q1 0
1000 q4 q0 q1 1

Step 3 : Implementation
M = {Q, , , , , q0)
Q = {q0, q1, q2, q3, q4}
 = {0, 1}
 = {0, 1}
q0 = q0
= =
Q\ 0 1
→ q0 q0 q1  (q0) = 0
q1 q2 q1  (q1) = 1
q2 q3 q1  (q2) = 0
q3 q4 q1  (q3) = 0
q4 q0 q1  (q4) = 1

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

1
0 0

q4/1 q3/0

Step 4 : (q0, 110000) 0110010


+ (q1, 10000)
+ (q1, 0000)
+ (q2, 000)
+ (q3, 00)

59
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

+ (q4, 0)
+ (q0, )

Q) Design Moore machine to change each occurrence of “120” to “122” over  = {0, 1, 2}
Ans. Step 1 : Logic
S\I 0 1
ends in →qs
0 q0
q0
q0
q1
q1
0
0
q0

1 q1 q0 q1 1
2
12
120
q2
q3
q4
q0
q4
q0
q1
q1
q1
2
2
2
q2

Q) Design Moore machine to convert each occurrence of “aab” over  = {a, b}

Ans. Step 1 : Logic


S\I a b o/p
ends in →qs q0 q1 
a q0 q2 q1  q1
b q1 q0 q1 
aa q2 q2 q3 
aab q3 q0 q1 0

* Minimization of Mealy Machine :


States can be merged if
1) All states have same transitions
2) AND output symbols along with the transitions are also same.

Q) Design Mealy machine to o/p ‘A’ if i/p ends in “101” otherwise ‘R’ over  = {0, 1}
Ans. Step 1 : Theory (Definition of Mealy machine)

Step 2 : Logic


S\I 0 1 S\I 0 1
ends in → qs q0 q1 → qs R R
q0
0 q0 q0 q1 q0 R R


1 q1 q2 q1 q1 R R
10 q2 q0 q3 q2 R A q1
101 q3 q2 q1 q3 R R

Step 3 : Implementation
M = {Q, , , , , q0)
Q = {q0, q1, q2 }

60
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

 = {0, 1}
 = {A, R}
q0 = q0
= =
S\I 0 1 Q\ 0 1
→ q0 q0 q1 → q0 R R
q1 q2 q1 q1 R R
q2 q0 q1 q2 R A

0/R 1/R

Start 1/R
q0 q1

1/A
0/R 0/R

q2

Step 4 : Example (q0, 1101) RRRA


+ (q1, 101)
+ (q1, 01)
+ (q2, 1)
+ (q1, )

Q) Design Mealy machine to o/p even and odd depending on the number of 1’s are even and odd
over  = {0, 1}
Ans. Step 1 :

Step 2 : Logic
S\I 0 1 S\I 0 1
no. of (1)’s → qs q0 q1 → qs E O
Even q0 q0 q1 q0 E O
Odd q1 q1 q0 q1 O E

Step 3 : Implementation
M = {Q, , , , , q0)
Q = {q0, q1 }
 = {0, 1}
 = {0, E}
q0 = q0

61
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

= =
Q\ 0 1 Q\ 0 1
→ q0 q0 q1 → q0 E O
q1 q1 q0 q1 O E

0/ 0/0

Start 1/0
q0 q1
1/

Step 4 : (q0, 1101) OEEO


+ (q1, 101)
+ (q0, 01)
+ (q0, 1)
+ (q2, )

Dec 09/10 – 5 M
Q) Design Mealy machine to change each occurrence of “abb” to “aba” over  = {a, b}
Ans. Step 1:

Step 2 : Logic
S\I a B S\I a b
→ qs → qs

ends in q0 q1 a b
a q0 q0 q2 q0 a b


b q1 q0 q1 q1 a b q1
ab q2 q0 q3 q2 a a
abb q3 q0 q1 q3 a b

* Moore machine to Mealy machine


# Assign the o/p symbol along with state to all of its state to all of its incoming transitions.

q) Convert Moore machine to Mealy machine.

0
1
Start 0
q0/a q3/a

1 1 1
Ans.
0
q1/b q2/c
0
62
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

0/a
1/a
Start 0/a
q0 q3

1/b 1/a 1/a

0/c
q1 q2
0/b

* Mealy machine to Moore machine :


# If the o/p symbols along with incoming are same, then assign that symbol to that state
# If the o/p symbols along with incoming are not same, then split that state as many times as o/p
symbols, with each state producing difference o/p.
# If there are no incoming transitions then any o/p can be assigned to that state.

Q) Convert the Mealy machine to Moore machine.

1/b
0/a
0/a q1 q3

Start
q0 1/a 1/b 0/a 0/a
1/b
0/b
q2 q4

1/b

Ans.

0 1
q1/a q3a/a
0

Start 1 0 0 0
q0/a 1 0
1
1
1
q2/b q4a/a
0

63
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

June 2011 / 10 M
Q) Design Mealy machine to recognize r = (0 + 1)* (00 + 11) and convert into Moore machine.
Ans. Step 1 : Theory

Step 2 : Logic
S\I 0 1 S\I 0 1
ends in → qs q0 q1 → qs R R
0
1
00
q0
q1
q2
q2
q0
q2
q2
q3
q1
q0
q1
q2
A
R
A
R
A
R
 q0

q1
11 q3 q0 q3 q3 R A

Step 3 : Implementation
M = {Q, , , , , q0)
Q = {qs, q0, q1}
 = {0, 1}
 = {A, R}
q0 = qs
= =
Q\ 0 1 Q\ 0 1
→ qs q0 q1 → qs R R
q0 q0 q1 q0 A R
q1 q0 q1 q1 R A
0/A

Start 0/R
qs q0

0/R
1/R
1/R

q1

1/A

64
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Step 4 : Mealy to Moore :

Start
qs/R
q0A/A q0R/R
0
0
0 0 1
1 0

q1A/A q1R/R
1
1
1

65
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

CHAPTER NO 5
Grammars
Grammar is used for specifying the synthesis of the language and it is defined as follows:

G = (V, T, P, S)
Where,
V = Finite set of variables of non-termination
Rd by → Capital Letters

T = Finite set of Terminals

Rd by → Small Letters
→ Operators

P = Finite set of production / Rewriting


S = Start variable

Chomsky’s Hierarchy / Type of Grammar :


As per Chomsky’s, there are four difference to of G, that have been classified based on the restrictions on
production Rules 6. Page 6 – 4

Type Grammar Language Restrictions Example Machine


0 Unrestricted Recursively - S – AB TM
Grammar Enumerable AB → AC
language A→a
B→c
C→b
1 Context Sensitive CSL K1   S → aAb L.B.A.
Grammar A → aaA
A→ C

2 Context Free CFL A→ S → asbaAb P.D.A.


Grammar A → Abbca
3 Regular Regular A → any LLG : S→A FA
Grammar Language number of T’s R A → Abca
and atmost one RLG : S→aB
variable B → bBab

66
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

1. Unrestricted grammar (Type 0):-


➢ In unrestricted grammar, productions can be written without any restrictions.
➢ Productions of the form α->β with α≠ε are permitted in this type of grammar where
‘α’ and ‘β’ are sentential forms (i.e. any combination of terminals and non-
terminals).
➢ We can construct turing machine to recognize language generated by this type of
grammar.
2. Context sensitive grammar (Type 1):-
➢ In this type of grammar, for each production of the form α->β, length of ‘β’ is
atleast as much as ‘α’ except for S->ε.
➢ The term context sensitive is because of the productions of the form, α1Aα2 ->
α1βα2 (where β≠ε) where the replacement of a non-terminal ‘A’ is allowed by ‘β’
only in the context α1 preceding ‘A’ and α2 succeeds ‘A’.
➢ TM can be constructed to recognize the CSL generated by CSG.
3. Context free grammar (Type 2):-
➢ In this case of grammar the only allowed type of productions is A -> α. Where ‘A’
is a non-terminal and ‘α’ can be sentential form (i.e. any combination of terminals
and variables).
➢ The left hand side of the production should contain only one non-terminal (i.e. only
one variable).
➢ PDA can be constructed to recognize CFL generated by CFG.
4. Regular grammar (Type 3):-
➢ In this type of grammar those productions are allowed which satisfies the
following conditions.
➢ Left hand side of the each production should contain only one non-terminal (i.e.
only one variable).
➢ Right hand side of each production can contain only one variable that too it should
appear on the right most side or the left most side.
➢ In LLG, on the right hand side of the production only one variable is allowed that
too it should appear on the leftmost side.
➢ In RLG, on the right hand side of the production only one variable is allowed that
too it should appear on the rightmost side.
➢ FA can be constructed to recognize RL generated RG.

* Derivation :
It is the process to check whether the given G, can derive / generate the given sentence using any
combination of production rules, starting from start variable’s production rule.

* Left Most Derivation (LMD) :


Derivation LMD is defined as it at every step we select and replace the left most variable by its
production rule.

* Right Most Derivation (RMD) :


……. Right Most ……

67
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q) S → aASa
A → sbA ssba
Derive “aabbaa” using
LMD & RMD
Ans. LMD : S  aAS Using S → aAS
lm

 aSbAS Using A → SbA


lm

 aabAS Using S → a
lm

 aabbaS Using A → ba
lm

 aabbaa Using S → a
lm

RMD : S  aAS Using S → aAS


rm

 aAa Using S → a
rm

 aSbAa Using A → SbA


rm

 aSbbaa Using A → ba
rm

 aabbaa Using S → a
rm

* Derivation / Rule / Parse tree :


It is as graphical representation of derivation process.

S
LMD :
S
a
A

a
S A
b

a b a

S
RMD :
S
a
A

a
S A
b

a b a

68
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q) S → aB  bA
A → aasaBB
Derive using LMD & RMD
(1) aaabbb
(2) bbaaba
(3) aaba
Ans. (1) LMD : S  aB Using S → aB
lm

 aabb Using B → aBB


lm

 aaaBBB Using B → aBB


lm

 aaabBB Using B → b
lm

 aaabbB Using B → b
lm

 aaabbb Using B → b
lm

RMD : S  aB Using S → aB
rm

 aabb Using B → aBB


rm

 aaBb Using B → b
rm

 aaaBBb Using B → aBB


rm

 aaaBbb Using B → b
rm

 aaabbb Using B → b
rm

(2) LMD : S  bA Using S → bA


lm

 bbAA Using A → bAA


lm

 bbaA Using A → a
lm

 bbaaS Using A → as
lm

 bbaaSA Using A → bA
lm

 bbaaba Using A → a
lm

RMD : S  bA Using S → bA
rm

 bbAA Using A → bAA


rm

 bbAa Using A → a
rm

 bbaSa Using A → aS
rm

 bbaaBa Using S → aB
rm

 bbaaba Using S → b
rm

69
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

(3) The grammar cannot derive the given sentence


Notes : If the derivation of the sentence is possible then it can be derived using LMD and
RMD both.

* e.g. of CFG :

G L(G)
S→a {a}
S→b {b}
S → ab {a}  {b} = {a,b}
S → ab {ab}
S → aS {, a, aa, aaa, ….}
S → abS {, ab, abab, …}
S → aSbS {, a, b, aa, ab, ba, …}

Q) Write CFG to generate :


1) Set of all strings that start with ‘a’ over  = {a, b}
Ans. P: S → aA
A → aAbA
G = ({S,A}, {a,b}, P, S)
L(G) = {a, aa, ab, aaa, ….}

2) Set of all strings that end in “11” over  = {0, 1}


Ans. S → B 11
B → 0B  1B 
G = ({S, B}, {0, 1}, P, S)
L(G) = {11, 011, 111, …..}

3) Set of all strings that start and end with different symbol over  = {x, y}
Ans. S → x X y  yXx
X → xX yX 

4) Set of all strings that start and end with same symbol over  = {0, 1, 2}
Ans. S → 0A01A12A2  0  1  2
A → 0A  1A  2A 

5) Over  = {a, b}
1) At least 2a’s
S → AaAaA
A → aA bA 

2) Exactly 2a’s
S → AaAaA
A → b

70
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

3) At most 2a’s
S → AaAaA AaA  A
A → bA 

6) L = {an bn  n  1}
Ans. n = 1 ab S → aSb  ab First answer
n = 2 aabb
n = 3 aaabbb S → 0X11
X → 0X11 Second answer

7) L = {0m 12m m  0}
Ans. m=0 
n=1 011
m=2 001111

S → 0S11 First answer

S → 0X11 
X → 0X11  Second answer

8) L = {an bn+1  n  1}
Ans. n=1 abb
n=2 aabbb
n=3 aaabbbb

S → aAbb
A → aAb   First Answer

S → aSb  abb → Second Answer

S→aXb
X → aXb  b
 Third Answer

9) L = {xm ym-3  m  3}
Ans. m=3 xxx
m=4 xxxxy
m=5 xxxxxyy

S → xxx xxxA
A → xAy  First Answer

S → xXyxxx Second Answer

71
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

10) L = {ai bj  I, j  1}
Ans. S → XY
X → aXa
Y → bYb
(Because a and b are independent)

11) L = {0i 1j, 2k  i, j, k  1}


Ans. S → ABC
A → 0A0
C → 2C2
B → 1B1

12) L = {ai bi ck i, k  1}


Ans. S → AB
A → aAbab
B → cBc

13) L = {0i 1j 2j i, j  1]}


Ans. S → AB
A → 0A0
B → 1B212

14) L = {ai bj ck  j = k + i, i, k  1}
Ans.
= ai bj ck
= ai bk+i ck
= ai bi+k ck
= ai bi bk ck
A B
S → AB
A → aAb ab
B → bBc bc

 i= j 
 i j k 
15) L = a b c or , i, j , k  1
 j=k 
 
i j k
Ans. a b c

i=j j=k
a bi c k
i
aibi c j
x y A B
 
S1 S2
S

72
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

S → S1  S2
S1 → XY
x → aXb  ab
y → cYc
S2 → AB
A → aA  a
B → bBc  bc

16) Over  = {a, b}


(1) even palindrome
S → aSa  bSb 
(2) Odd palindrome
S → aSa  bSb ab
(3) Palindrome
S → aSa  bSb a  b  

* Simplification of CFG : Dec 2010 /10 M


1) Elimination of useless productions :
* Definition of useless variables :
 
A variable ‘x’ is useful if S  αXβ  w else ‘x’ is useless.

* Definition of useless production:-


A production where useless variables are present is called useless production.

Elimination procedure
Given CFG G = (V, T, P, S)
Define G’ = (V’, T’, P’, S) be the CFG that does not contain any useless productions such that L(G ’) =
L(G).
Step 1 = Initialize P’ to P

Step 2 = Find useless variables


* Variables ‘x’ is useless if it is not reachable from start variable i.e. S.
* Variable ‘x’ is useless if it cannot derive any sentence (any combination of Terminals)

Step 3 = Delete all useless productions.

Q) Eliminate use less production


S → aSb abAB
A → aA  c
B → bB  dB
Ans. Given G = ({S, A, B}, {a, b, c, d}, P, S)
P:
S → asb a bAB
A → aA C
B → bB dB

73
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Define G’ = (V’, T’, P’, S) be the CFG that does not contain any useless production such that L(G’)
= L(G).

P’ :
S → aSb abAB
A → aA c
B → bB  dB
 B cannot drive any sentence B is useless
S → aSb a
A → aA c
 A is not reachable from start variable S, therefore A is usless.
P’ : S → aSb  a
G’ = ({S}, {a,b}, P’, S)

2) Elimination of unit productions :


* Definition of unit production
A production of this form ‘X → Y’ where X and Y are variables is called unit production.

* Definition of Non-unit production :


A production not of the form ‘X → Y’ where X and Y are variables is called non-unit
production

Elimination procedure :
Give CFG G = (V, T, P, S)
Define G’ = (V’, T’, P’, S) be the CFG that does not contain any unit productions such that L(G1) = L(G)
Step 1 : Initialize P’ to P

Step 2 : Find unit productions


Say ‘X → Y’ is a unit production of X then add to X, the non-unit productions of Y that are not present in
X.

Step 3 : Delete all unit production

Q) Eliminate unit productions


S → aS  b A
A → Ab  b  a
Ans. ….. (dots represent that we have to write given CFG G……)
Productions New-Productions
S → aS –
S→b –
S→A S → Ab, S → a
A → Ab –
A→b –
A→a –

74
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

P + np (Production + New productions)


S → aS bAAba
A → Ab ba

After deleting unit productions :-


S → aSbAba
A → Abba

3) Elimination of Null Productions :


* Definition of Null Production
A production of the form:
X →  OR X →  OR X → 
Where X is a variable is called a Null Production.

* Definition of Nullable variable :-


*
A variable ‘X’ is nullable if X → or X  

Elimination Procedure :
Given CFG G = (V, T, P, S)
Define G’ = (V’, T’, P’, S) be the CFG that does not contain any null productions such that L(G’) =
L (G) – {}

Step 1 : Initialize P’ to P

Step 2 : Find nullable variables :


Say ‘X → ’ is a production of X where  contains some nullable variables, then add to X, the
productions obtained by deleting all possible subsets of nullable variables.
From  (if not present in X)

Step 3 = Delete all null productions.

Q) Eliminate null productions :


S → aS bA bAB
A → aA c 
B → bB da
Ans. ……
Productions New-Productions
B → bB B→b
B → da –
B → –
A →aA A→a
A→c –
A→ –
S → aS –
S →bA S→b
S → bAB S → bB

75
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

P + np
S → aS bA b  bAB  bB
A → aA ac
B → bB bda

After deleting Null productions


S → as babbABbB
A → aA ac
B → bB b da

Q) Simplify / Reduce the following :


S → As AC
A → aB d
B → cB  ad
C → aB db
Ans. Note : While solving this type of problem first we should eliminate null production if present then
we should eliminate unit productions of present then we should eliminate useless productions if
present.

Step 1 : Elimination of Null Production :


Productions New-Productions
S → aS S→a
S→A S→
S→C –
A → aB A→a
A→d –
A→ –
B → cB B→c
B → ad –
C → aB C→a
C → db –
P + np
S → aS  a  A C
A → aB ad
B → cB cad
C → aB adb

After deleting Null productions


S → aS aAC
A → aB ad
B → cBcad
C → aBadb

76
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Step 2 : Elimination of unit productions


Productions New-Productions
S → aS –
S→a –
S→A S → aB, S → d
S→C S → db
A → aB –
A→a –
A→d –
B → cB –
B→c –
B → ad –
C → aB –
C→a –
C → db –

P + np
S → aS aAaBdCdb
A → aBad
B → cBcad
C → aB adb

After deleting unit productions :


S → asaaBddb
A → aBad
B → cBcad
C → aBadb

Step 3 : Elimination of useless productions


S → asaaBddb
A → aBad
B → CBcad
C → aBadb

 A is not reachable from start variable.


A is useless.

 C is not reachable from start variable


C is useless
So we eliminate them.
S → asaaBddb
B → CBCad

77
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

* Normal forms :
The Normal form implies the rewriting of CFG as per the specified conditions.

* Chomsky normal form (CNF):

: Any -free CFL can be generated by CFG in which all the production of the form :
‘A → BC’ OR ‘A → a’
Where A, B and C are variables and a is a terminal
Such a CFG is said to be in CNF

* Conversion procedure from CFG to CNF :


Step 1 : Perform elimination of Null, unit and useless productions.( i.e. simplification of CFG)

Step 2 : Add to the solution the productions that are already in CNF.

Step 3 : For the remaining Non-CNF productions :-


* Replace the terminals by some variable.
* Limit the number of variables on R.H.S. to 2.

Q) Express CFG in CNF :


S → aB bA
A → a | aS  bAA
B → bbSAaBBA
Ans. Step 1 : The given grammar is already simplified

Step 2 :
Productions Solution
A→a A→a
B→b B→b
S → aB C1 → a
S → C1B S → C1B
S → bA C2 → b
S → C2A S → C2A
A → aS –
A → C1S A → C1S
A → bAA –
A → C2AA A → C2C3
C3 → AA C3 → AA
B → bSA –
B → C2SA B → C2 C4
C4 → SA C4 → SA
B → aBBA –
B → C1BBA B1 → C1 C5
C5 → BBA C5 → BC6
C6 → BA C6 → BA

78
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

CFG in CNF productions :


S1 → C1B  C2A
A → aC1SC2C3
B → bC2C4C1C5
C1 → a
C2 → b
C3 → AA
C4 → SA
C5 → BC6
C6 → BA

June 2011 / 10 M
Q) Convert the G in to CNF :
A → aBa bBa
B → aB bB
Ans. Step 1 : Elimination of Null Production
Productions New-Productions
A → aBa A → aa
A → bBa A → ba
B → aB B→a
B → bB B→b
B→ –
P + np
A → aBa aabBaba
B → aBabBb

After deleting Null productions


A → aBa aabBaba
B → aB abBb

Step 2 and Step 3 :

Productions Solution
B→a B→a
B→b B→b
A → aBa C1 → a
A → C1BC1 A → C1C2
C2 → BC1 C2 → BC1
A → aa –
A → C1C1 A → C1C1
A → bBa C3 → b
A → C3BC1 A → C3C4
C4 → BC1 C4 → BC1
A → ba –
A → C3C1 A → C3C1

79
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

B → aB –
B → C1B B → C1B
B → bB –
B → C3B B → C3B

Solution :
A → C1C2C1C1C3C4C3C1 C1 → a
B → abC1BC3B C2 → BC1
C3 → b
C4 → BC1

Q) Express CFG in CNF :


S → ~S
S → [S ) S]
S→P
S→q
Ans. Step 1 : The given grammar G is already simplified.

Step 2 and Step 3


Productions Solution
S→p S→p
S→q S→q
S → ~S C1 → ~
S →C1S S →C1S
S → [S ) S] C2 → [,C3→), C4→]
S → C2SC3SC4 S → C2C5
C5 → SC3SC4 C5 → SC6
C6 → C3SC4 C6 → C3C7
C7 → SC4 C7 → SC4
CFG in CNF:-

S→p
S→q
C1 → ~
S →C1S
C2 → [
C3→)
C4→]
S → C2C5
C5 → SC6
C6 → C3C7
C7 → SC4

80
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

* Greibach Normal Form (GNF) :


Definition : Any - Free CFL can be generated by CFG in which all the productions are of the
form:-
A → ar
Where A → variable
a → terminal
r → string of variables (can be empty also)
Such a CFG is said to be in GNF

* Conversion procedure from CFG to CNF :


Step 1 : Perform elimination of Null, unit and useless production. (i.e. Simplification of CFG)

Step 2: Use any combinations of Rule 1, Rule 2 and Rule 3 to obtain CFG in GNF.

Rule 1 : Let A → B be some A – Production and Let B → 12…..n be B – Productions


then we can write A – production as : A → 12…..n

Rule 2 : Let A → A1A2…Ar and Let A → B1B2….Bs


Then introduce a new variable B,
B → Productions: B → ii 1 i  r
A → Productions: A → ii 1is

Rule 3: Let A → aBCda, then we can rewrite


A as : A → aBCx1x2, x1 and x2 are auxiliary variable,
X1 → d
X2 → a

Q) Express CFG in GNF :


1) S → ABA BBBA
A → aAb
B → bB|a
Ans. Step 1 : already done

Step 2: B → bBa
A → aAb
S → aABA bBA | bBBaB | aA R1

2) A1 → A2A2 a
A1 → A1A1b
Ans. A2 → A1A1b
A2 → A2 A2 A1aA1b
A → A 1 B1 B2 R1

B→ i iB i.e. B→ A2A1A2A1B


A → βiβiB i.e. A2 → aA1aA1BbbB
 R2

81
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

A1 → aA1A2aA1BA2bA2bBA2a R1

B → aA1A1aA1BA1bA1bBA1aA1A1BaA1BA1BbA1BbBA1B R1

June 2011/5M
Dec 2011/8M

3) S → AA0
A → SS1
Ans. A → SS1
A → AAS0S 1 R1
A → A  1  1 2 2

 → iiB

A → iiB
i.e. B → ASASB

A → 0S 0SB11B
 R2
2

S → 0SA 0SBA 1A 1BA 0 R1


B → 0SS 0SBS1S1BS0SSB0SBSB1SB1BSB R1

S → aSabSbc


4)
S → aSC1 | bSC2 | c
C1 → a R3
2
C2 → b

5) S → SSaSbab
Ans. S → SS  asbab
A → A1 1 2
B → iiB i.e.
A → iiB i.e.
B → SSB
S → aSbaSbBababB
R2
2

S → aSC1aSC1BaC1aC1B
C1 → b R3
B→ aSC1 aSC1BaC1aC1BaSC1BaSC1BBaC1BaC1BB R1

June 2011 / 5M
Q) S → AB
A → BSBBBb
B → aAba
Ans. B → aAC1a
Where C1 → b R3

A → aAC1SBaSBaAC1BaBb R1

S → aAC1SBBaSBBaAC1BBaBBbB R1

82
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

7) Express the following in CNF and GNF :


1) L = {an bn |n > 1}
Ans. CFG : S → aSbab
Simplified CFG : S → aSbab
A → BC A → ar
A→a
CNF GNF
C1 → a S → aSC1 aC1
C2 → b C1 → b
S → C1C2
S → C1C3
C3 → SC2

2) L = {02n 1n n > 1}
Ans. 001
000011
CFG : S → 00S1 001
Simplified CFG : S →00S1  001

CNF GNF
C1 → 0 S → 0C1SC2 0C1C2
C2 → 1 C1 → 0
S → C1C3 C2 → 1
C3 → C1C2
S → C1C4
C4 → C1C5
C5 → SC2

Ambiguous and Unambiguous Grammar


* Ambiguous Grammar
Grammar is ambiguous if it can derive at least one sentence using more than one LMD / RMD.
* Unambiguous Grammar
Grammar is unambiguous if it can derive all sentences using exactly one LMD / RMD.
* Ambiguity Resolution : Dec-2011 / 5M
To resolve ambiguity no method exits we need to take a heuristic approach.

We can take help of the following


Rule 1 : Let A → A1A2….Ar and
Let A → 1 2….S
Then, A → B1A’ B2A’……BsA’
A’ → 1A’2A’….r A’/

Rule 2 : Try rewriting the grammar

83
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q) Show that the full grammar is ambiguous.


Try to eliminate ambiguity.
1) S → SSab
Ans. Let’s try to derive the sentence “aaa”
LMD : S  SS LMD : S  SS
lm lm

 aS  SSS
lm lm

 aSS  aSS
lm lm

 aaS  aaS
lm lm

 aaa  aaa
lm lm

 G can derive the above sentence using more than one LMD, G is ambiguous.
S → SS a  b
 1 1 2

R1 : S → aS’bS’
S’ → SS’

LMD : S  aS’
 aSS’
 aaS’S’
 aaSS’S’
 aaaS’S’S’
 aaaS’S’
 aaaS’
 aaa
 G cannot derive the above sentence using more than one LMD, G is ambiguous.

2) E → E + E E*Eid
Ans. "id + id * id"
LMD : 1 LMD : 2
E  E+E E E*E
lm

 id + E E+E*E
lm

 id + E * E  id + E * E
lm

 id + id * E  id + id * E
lm

 id + id * id  id + id * id
lm

Since G can derive the above sentence using more than one LMD, G is ambiguous.
Hence, we try to eliminate ambiguity.
E → E + E  E * E  id
A → A  1 A  2 1

R1 : E → idE’
E’ → + EE’  * E E’

84
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

LMD : id & id * id
E  idE’
E  id + EE’
E  id + E’E’
E  id + id * EE’E’
E  id + id * idE’E’E’
E  id + id * idE’E’
E  id + id * idE1
E  id + id * id
Since G can derive the above sentence using only one LMD, G is unambiguous

June 2010 / 5M
Q) Show that the following is ambiguous
S → aSbS  baSS 
Ans. abab
LMD  aSbS LMD  aSbS
 abSaSSbS  abS
 abaSSbS  abaSbS
 abaSbS  ababS
 ababS  abab
 abab
Since more than one LMD are possible to derive the above sentence.
The given Grammar G is ambiguous.

• Applications of grammars:-

I. Syntax Analyser or parser:-


Grammars are used to describe programming languages. There is a mechanical way of
turning the language description as a CFG in to a parser is a component of compiler that
discovers the structure of the source program and represents that structure by a parse tree.
II. XML (Extensible Mark-up language):-
An essential part of XML is the document Type definition (DTD) which is essentially a
CFG that describes the allowable tags and the ways in which these tags may be nested.

85
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

CHAPTER NO 6
Push Down Automata (PDA)
Pushdown Automata : (PDA) :
PDA is used for recognizing CFL which is generated CFG.
i/p tape
a a b b c c
readhead

Finite Stack / pushdown state


State
Control 20

Fig : Model of PDA

* Components of PDA :
PDA consists of finite set of states, i/p tape, readhead and a stack.

* Working of PDA :
Depending on the state, i/p symbol and stack top symbol.
- PDA can change the state / remain in same state.
- PDA can perform some stack operations.

* PDA and over machine are presentation by 7 tupple representation.


M = (Q, , , , q0, z0, F)
Where,
Q = Finite set of states
 = i/p alphabet
 = Stack alphabet
 = Transition function
 : Q    {}   → subsets of Q X*
q0 = Start state q0 Q
z0 = Initial stack top symbol z0  
F = Finite set of final states F  Q

86
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q) Design PDA for recognizing :


L = {an, bn  n  1}
Ans. Step 1 : Theory
……………. PDA …………

Step 2 : Logic
For each ‘a’ push 1X
For each ‘b’ pop 1X

Step 3 : Implementation
M = {Q, , , , q0, z0, F)
Q = {q0, q1, qf}
 = {a, b}
 = {X, R}
qo = q0
z0 = R
F = {qf}

:
 (q0, a, R) = {(q0, XR)}
 (q0, a, X) = {(q0, XX)}
  (q0, b, X) = {(q1, )}
 (q1, b, X) = {(q1, )}
→ (q1, , R) = {(qf, R)}

Step 4 : Example
1) (q0, aaabbb, R)
+ (q0, aabbb, XR)
+ (q0, abbb, XXR)
+ (q0, bbb, XXXR)
+ (q1, bb, XXR)
+ (q1, b, XR)
+ (q1, , R)
Accept

2) (q0, abbb, R)
+ (q0, bbb, XR)
+ (q0, bb, R)
Reject

87
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

3) (q0, bbaa, R)
Reject

4) (q0, abab, R)
+ (q0, bab, XR)
+ (q1, ab, R)
Reject

Q) L = {0n 12n  n  1}
Ans. Step 1 = Theory

Step 2 : Logic
For each ‘0’ push 2X
For each ‘1’ pop 1X

Step 3 : Implementation
:
 (q0, 0, R) = {(q0, XXR)}
 (q0, 0, X) = {(q0, XXXX)}
  (q0, 1, X) = {(q1, )}
 (q1, 1, X) = {(q1, )}
→ (q1, , R) = {(qf, R)}

Step 4 : Example
(q0, 000111111, R)
+ (q0, 00111111, XXR)
+ (q0, 0111111, XXXXR)
+ (q0, 111111, XXXXXXR)
+ (q1, 11111, XXXXXR)
+ (q1, 1111, XXXXR)
+ (q1, 111, XXXR)
+ (q1, 11, XXR)
+ (q1, 1, XR)
+ (q1, , R)
+ (qf, , R)
Accept

88
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Q) L = {Cn d3n  n  1}
Ans. Step 1 : Theory

Step 2 : Logic
For each ‘C’ push ‘3X’
For each ‘d’ pop ‘1X’

Step 3 : Implementation
:
 (q0, C, R) = {(q0, XXXR)}
 (q0, C, X) = {(q0, XXXX)}
  (q0, d, X) = {(q1, )}
 (q1, d, X) = {(q1, )}
→ (q1, , R) = {(qf, R)}

* PDA Design methods


1) PDA by Final State Method :
(q0, w, z0) (q, , r)
* qF
r  T*

2) PDA by Null / Empty store method :


(q0, w1, z0) (q, , )
* qQ
F={}

Q) L = {an bn+1  n  1}
Ans. Step 1 : Theory

Step 2 : Logic
For each ‘a’ push 1X
Bypass 1 b
For each ‘b’ pop 1X

Step 3 : Implementation
:
 (q0, a, R) = {(q0, XR)}
 (q0, a, X) = {(q0, XX)}
 (q0, b, X) = {(q1, X)}
 (q1, b, X) = {(q1, )}
(q1, , R) = {(qf, R)} = FSM
= {(q1, )} = NSM / ESM

Dec 2010 – 10 M
Q) L = {an, bm an m, n  1}
Ans. Step 1 : Theory

Step 2 : Logic
For each ‘a’ push ‘1X’
By pass all b’s

89
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

For each ‘a’ pop ‘1X’

Step 3 : Implementation
:
 (q0, a, R) = {(q0, XR)}
 (q0, a, X) = {(q0, XX)}
  (q0, b, X) = {(q1, X)}
 (q1, b, X) = {(q1, X)}
  (q1, a, X) = {(q2, )}
 (q2, a, X) = {(q2, )}
→ (q1, , R) = {(qf, R)}

June 2012 / 5 M
Q) L = {ambn cndm  m, n  1)
Ans. Step 1 : Theory

Step 2 : Logic
For each ‘a’ push ‘1X’
For each ‘b’ push ‘1Y’
For each ‘c’ pop ‘1Y’
For each ‘d’ pop ‘1X’

Step 3 : Implementation
:
 (q0, a, R) = {(q0, XR)}
 (q0, a, X) = {(q0, XX)}
  (q0, b, X) = {(q1, YX)}
 (q1, b, Y) = {(q1, YY)}
  (q1, c, Y) = {(q2, )}
 (q2, c, Y) = {(q2, )}
  (q2, d, X) = {(q3, )}
 (q3, d, X) = {(q3, )}
→ (q3, , R) = {(q3, )}

Dec 2011 / 8 M
Q) L = {0m 1n 0m+n  m, n  1}
Ans. Step 1 : Theory

Step 2 : Logic
For each ‘0’ push ‘1X’
For each ‘1’ push ‘1Y’
For each ‘0’ pop ‘1Y’
For each ‘0’ pop ‘1X’

90
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Step 3 : Implementation
:
 (q0, 0, R) = {(q0, XR)}
 (q0, 0, X) = {(q0, XX)}
  (q0, 1, X) = {(q1, YX)}
 (q1, 1, Y) = {(q1, YY)}
  (q1, 0, Y) = {(q2, )}
 (q2, 0, Y) = {(q2, )}
  (q2, 0, X) = {(q2, )}
 (q2, 0, X) = {(q2, )}
→ (q3, , R) = {(q2, )}

Q) L = {0m 1p 2n  p = m + n ; m, n  1}
Ans. Simplification :
0m 1p 2n
0m 1m+n 2n
0m 1m 1n2n

Step 2 : Logic
For each ‘0’ push ‘1X’
For each ‘1’ pop ‘1X’
For each ‘1’ push ‘1Y’
For each ‘2’ pop ‘1Y’

Step 3 : Implementation
:
 (q0, 0, R) = {(q0, XR)}
 (q0, 0, X) = {(q0, XX)}
  (q0, 1, X) = {(q1, )}
 (q1, 1, X) = {(q1, )}
  (q1, 1, R) = {(q2, YR)}
 (q2, 1, Y) = {(q2, YY)}
  (q2, 2, Y) = {(q3, )}
 (q3, 2, Y) = {(q3, )}
→ (q3, , R) = {(q3, )}

Q) Design PDA for recognizing


L = {x  na(x) = n(b(x)}
Ans. Logic

ab abab aabb abba


X
X XX X XY
R R R R
baaabb ba baba bbaa baab

91
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Push X/R
a Pop Pop
b Push
Y/R
 = (q0, a, R) = {(q0, XR)}
 (q0, b, R) = {(q0, YR)}
 (q0, a, X) = {(q0, XX)}
 (q0, b, Y) = {(q0, YY)}
 (q0, a, Y) = {(q0, )}
 (q0, b, X) = {(q0, )}
 (q0, , R) = {(q0, )}

e.g. : (q0, bbaaba, R)


+ (q0, baaba, YR)
+ (q0, aaba, YYR)
+ (q0, aba, YR)
+ (q0, ba, R)
+ (q0, a, YR)
+ (q0, , R)
+ (q0, , )
Accept

Q) Design PDA to check for well formed of parenthesis :


Ans. Logic : (()(()))

X
YX
X
R
For every ‘(’ push 1X
For every ‘)’ pop 1Y

 (q0, (, R) =  (q0, XR)


 (q0, (, X) =  (q0, XX)
 (q0, ), X) =  (q0, )
 (q0,  , R) =  (q0, )

June 2010 / 2011 – 10 M


Q) Design PDA for recognizing
 w − {a, b} * 
 
L = wcw R
C − Costate 
 w − Reverse of w 
R
 
Ans. aa bba cabbaa

92
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

 (q0, a, R) = {(q0, XR)}


 (q0, b, X) = {(q0, YR)}
 (q0, a, X) = {(q0, XX)}
 (q0, b, X) = {(q0, YX)}
 (q0, a, Y) = {(q0, XY)}
 (q0, b, Y) = {(q0, YY)}
 (q0, c, R) = {(q1, R)}
 (q0, c, X) = {(q1, X)}
 (q0, c, Y) = {(q1, Y)}
 (q1, a, X) = {(q1, )}
 (q1, b, Y) = {(q1, )}
 (q1, , R) = {(q1, )}

 w {a, b} * 
Q) L = ww R 
 w − reverse of w 
R

Ans. Note :
This is a case of NPDA design where NPDA stands for non-deterministic.
Whenever there is a double letter following cases are possible :
Case I – The middle of the strings is not reached hence we continue to push the respective
symbols on the stack.
Case II – The middle of the string is reached hence we start to pop the respective symbol on the
stack.
Logic :

abba

Y
X
R

abbbba
Y
Y
X
R

 (q0, , R) = {(q0, )}


 (q0, a, R) = {(q0, XR)}
 (q0, b, R) = {(q0, YR)}
 (q0, a, X) = {(q0, XX), (q1, )}
 (q0, b, X) = {(q0, YX)}
 (q0, a, Y) = {(q1, XY)}
 (q0, b, Y) = {(q0, YY), (q1, ) }
 (q1, a, X) = {(q1, )}

93
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

 (q1, b, Y) = {(q1, )}


 (q1, , R) = {(q2, )}

(q0, abbbba, R)
+ (q0, bbbba, XR)
+ (q0, bbba, YXR)

+ (q0, bba, YYXR) + (q1, bba, XR)

+ (q0, ba YYYXR) + (q1, ba, YXR)


+ (q0, a, YYYYXR) + (q1, a, XR)
+ (q0, , XYYYYXR) + (q1, , R)
+ (q1, , )
Accept

CFG to PDA :
Given CFG G = (V, T, P, S)
Define PDA M = (Q, , , , q0, z0, f)
For recognizing CFL generated by CFG G

Step 1 : Express CFG in GNF (A → ar)

Step 2 : PDA M = C Q, , [, q0, z0, F]

(1) Q = {q0}
(2)  = T1
(3)  = V1
(4) q0 = q0
(5) z0 = S
(6) F={}
(7) =
A → aBC
 (q0, a, A) = {(q0, BC)}

A→b
 (q0, b, A) = {(q0, )}

June – 2010 / 10 M
Q) Q1 for the grammars :
S → a ABC
A → aB  a
B → bA  a
C→a
Design PDA
Ans. Step 1 : Already done

94
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Step 2 : PDA M = {Q, , , , q0, z0, F)


(1) Q = {a, b}
(2)  = {a, b}
(3)  = {S, A, B, C}
(4) q0 = q 0
(5) z0 = S
(6) F={}
(7) :
 (q0, a, S) = {(q0, ABC)}
 (q0, a, A) = {(q0, B)}
 (q0, a, A) = {(q0, )}
 (q0, b, B) = {(q0, A)}
 (q0, b, B) = {(q0, )}
 (q0, a, C) = {(q0, )}

Step 3 : (q0, aaba, S)


+ (q0, aba, ABC)

+ (q0, ba, BBC) + (q0, ba, BC)

+ (q0, a, ABC) + (q0, a, BC) + (q0, a, AC) + (q0, a, C)


Accpet

+ (q0, , BBC) + (q0, , BC) + (q0, , BC) + (q0, , C)

June – 2011 / 10 M
q) Design PDA for
S → 0BB
B → 0S 1S  0
and test whether 010 belongs to the language.
Ans. Step 1 : Already done

Step 2 : PDA M = {Q, , , , q0, z0, F)


(1) Q = {q0}
(2)  = {0, 1}
(3)  = {S, B}
(4) q0 = q 0
(5) z0 = S
(6) F={}
(7) :

 (q0, 0, S) = (q0, BB)


 (q0, 0, B) = (q0, S)
 (q0, 1, B) = (q0, )
 (q0, 0, B) = (q0, )

95
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

Step 3 : (q0, 010000, S)


+ (q0, 10000,BB)
+ (q0, 0000, SB)
+ (q0, 000, BBB)

+ (q0, 00, SBB) +(q0, 0, BB)


+ (q0, 0, BBBB)
+ (q0, 0, BBBB) + (q0, 0, SB) + (q0, 0, ****)
+ (q0, , BBB)

+ (q0, , S) (******)
+(q0, , SBBB) +(q0, , BBB)

PDA to CFG :
Given PDA M = (Q, , , , q0, z0, F)
Define G = (V, T, P, S) for generating CFL recognized by PDAM
1) V = S + [q, A, P]; P, q, , Q, A , 
2) T=
3) S=S
4) P= * S → [q0, z0, P]; PEQ
*  (q, aA) = {(P, BC)}

[q, A, r] → a [P, B, S] [S, C, r]; r, S  Q

*  (q, a, A) = {(P, B)}

[q, A, r] → a [P, B, r] ; r  Q

* [q, a, A] = {(P, )}

[q, A, P] → a

Q) For the following PDA :-


A = ({q0, q1}, {a, b}, {z0, z}, , q0, z0, )
(1)  (q0, a, Z0) = {(q0, ZZ0)}
(2)  (q0, a, Z) = {(q0, ZZ1)}
(3)  (q0, b, Z) = {(q1, Z)}
(4)  (q1, b, Z) = {(q1, Z)}
(5)  (q1, a, Z) = {(q1, )}
(6)  (q1, , Z0) = {(q1, )}
Find equivalent CFG
Ans. G = (V, T, P, S)
A B

96
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

(1) V= {S, [q0, z0, q0], [q0, z0, q1],


C D
[q1, z0, q0], [q1, z0, q1],
E F
[q1, z, q0], [q0, z0, q1],
G H
[q1, z, q0], [q1, z, q1]}
(2) T = {a, b}
(3) S=S
(4) P = S → [q0, z0, q0] [q0, z0, q1]
(1)  (q0, a, Z0) = {(q0, ZZ0)}
[q0, Z0, q0] → a[q0, Z, q0] [q0, Z0, q0]
[q0, Z0, q0] → a[q0, Z, q1] [q1, Z0, q0]
[q0, Z0, q1] → a[q0, Z, q0] [q0, Z0, q1]
[q0, Z0, q1] → a[q0, Z, q1] [q1, Z0, q1]

(2)  (q0, a, Z1) = {(q0, ZZ)}


[q0, Z0, q0] → [q0, Z, q0] [q0, Z0, q0]
[q0, Z0, q0] → [q0, Z, q1] [q1, Z0, q0]
[q0, Z0, q1] → a[q0, Z, q0] [q0, Z, q1]
[q0, Z, q1] → a[q0, Z, q1] [q1, Z0, q1]

(3)  (q0, b, Z1) = {(q1, Z)}


[q0, Z0, q0] → b[q1, Z, q0]
[q0, Z, q0] → b[q1, Z, q0]
[q0, Z, q1] → b[q1, Z, q1]

(4)  (q1, b, Z) = {(q1, Z)}


[q1, Z1, q0] → b[q1, Z, q0]
[q1, Z, q1] → b[q1, Z, q1]

(5)  (q1, a, Z) = {(q1, )}


[q1, Z, q1] → a

(6)  (q1, , Z0) = {(q1, )}


[q1, Z0, q1] → 

* After this rewrite the production with new names A, B, C


* The perform elimination of unit and useless problems

97
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

FSM to PDA :
Note : While converting FSM to PDM no stack operations are required

Q) Design PDA to recognize strings that contain odd number of b’s over  = {a, b}
Ans. Step 1 : Theory
……… PDA ……………

Step 2 : Logic
S\I A b
nb(x) →qs q0 q1
even q0 q0 q1
odd q1 q1 q2

Step 3 : Implementation
M = {Q, , , , q0, z0, F)
(1) Q = {q0, q1, qf}
(2)  = {a, b}
(3)  = {R}
(4) q0 = q 0
(5) z0 = R
(6) F = {qf }
(7) =
 (q0, a, R) = {(q0, R)}
 (q0, b, R) = {(q1, R)}
 (q1, a, R) = {(q1, R)}
 (q1, b, R) = {(q0, R)}
 (q1, , R) = {(qf, R)}

Step 4 : Example
(q0, abbab, R)
+ (q0, bbab, R)
+ (q1, bab, R)
+ (q0, ab, R)
+ (q0, b, R)
+ (q1, , R)
+ (qf, , R)
Accept

98
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Difference between DPDA and NPDA:-

DPDA NPDA
1) In DPDA, from each state on each 1) In NPDA, from each state on
input symbol there is exactly one each input symbol there can be
transition. 0, 1 or more transitions.
2) In DPDA, the transition function is 2) In NPDA, the transition
defined as:- function is defined as :-
δ :- Q X Σ U {ε} X Г -> subsets of Q δ :- Q X Σ U {ε} X Г -> Q X Г*
X Г*
3) The implementation of DPDA is simple 3) The implementation of NPDA
because of its deterministic nature. is difficult because of its non-
deterministic nature.
4) eg:- give one example of DPDA. 4) eg:- give one example of
NPDA.

• APPLICATIONS OF PDA:-
PDA is used for recognizing CFL which is generated by CFG.
➢ Syntax Analyser:-
Syntax Analyser is a component of compiler which performs syntax analysis. In this phase, the
syntax of the statements in the source program are checked and if the syntax is correct a parse tree
is formed. PDA is used in syntax analysis for recognizing or parsing languages.

99
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

CHAPTER NO 7
Turing Machine (TM)

Turing Machine (TM) :


TM is a simple model of a computer and it is considered to be more power full machine.

TM can perform the followings :


1) Language recognition
2) Computation of some functions.

i/p / o/p tape


B B a a b b C C B B
read/write head
Finite
State
Control

Fig : Model of TM

* Components of TM
TM consist of finite set of states, i/p / o/p tape and read / write head.

* Working of TM :
Depending on the state and tape symbols.

* TM can change the state / remain in same state.


* TM can change the tape symbol / keep it same
* TM can move head either to {L, R, S]
M = {Q, , , , q0, z0, F)
Where,
Q = Finite set of states
 = i/p Alphabet
 = tape alphabet
 = transition function
 :- Q   → Q    {L, R, S}
q0 = Start State q0  Q
B = Blank symbol B  
F = Finite set of final states F  Q

100
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Universal turing machine:- (UTM)


Universal turing machine (UTM) is considered to be one of the, most powerful TM. It can be
compared to a general purpose computer, like general purpose computer UTM can also solve any
solvable problems.
Thus, it is possible to create a single machine that can be used to compute any computable
sequence.
UTM has the capability or in other words it should have the capability to imitate any other TM
‘M’ if given the following information on its tape.
➢ The initial or starting state or the current symbol scanned.
➢ The transition table of TM ‘M’.
UTM should have an algorithm to interpret correctly the rules of operation given about TM ‘M’.
There should be a marker to indicate the point at which the description of ‘M’ begins, and it keeps
a complete account of how the tape of ‘M’ looks like at every instant and guides it. It also
remembers the state ‘M’ is in and the symbol ‘M’ is reading, with the help of above information,
it simply looks up to the transition function of TM ‘M’ to carry out what ‘M’ is supposed to do.
To exhibit this behaviour, UTM should have a look up facility and should perform following
steps:-
Step1 :- scan the tape area and read the symbol that ‘M’ reads and initial state of ‘M’.
Step2:- On the above information find the new state, new symbol to be replaced and the direction
of the move.
Step3:- Move the tape to reach the symbol, move the tape in required direction read the next
symbol and finally reach the state and replace the state and scanned symbols. Go to step1.

101
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Variants of TM:-

Variants of TM are discussed as follows:-

1. TM with semi-infinite tape:-


in the standard original model of TM, the tape is semi-infinite I.e. the tape is infinite only
on the right end side (after the non-blank portion on right end side there is a series of
Blank symbols) whereas this is not the case on left end side. In this type of TM. The head
move to either left or right from its initial position. The head is allowed to move within
positions or in the right direction of initial head position. In this type there are no cells to
the left of its initial position.

2. TM with two way infinite tape:-


In this type of TM, the head is allowed to move in any direction on either side of the tape
(i.e. left end or right end). Infinite blank cells are present on both ends.

3. Mulitape turing machine:-


In this type of TM, there are multiple infinite tapes in both directions with multiple heads
but in this tape one head is associated with one tape.

4. Multihead turing machine:-


In this type of TM, there are fixed multiple heads associated with a tape, they are
numbered from 1 to K. Depending on the state and the symbol scanned, each head may
independently move in right, left direction or remain stationery.

5. Composite TM:-
In this type of TM, two or more TM can be combined to break a complex problem in to
number of simpler problems.

6. Non-deterministic TM:-
A TM is termed to be non-deterministic, when atleast in one move (i.e. depending on state
and tape symbol) it has multiple moves and hence it is difficult to determine which move
that will lead to a solution.

102
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Halting problem:-
The problem of determining whether a TM will ever halt or not is defined as halting problem.
Halting problem is an unsolvable problem because there exists no TM that can determine whether
a given problem including itself will ever halt or not.
Proof:-
Let us prove the halting problem by contradiction. Suppose there is a TM ‘M’ which decides
whether or not any computation by a TM ‘T’ will ever halt, given the description ‘dt’ of TM ‘T’
and the tape ‘t’ of TM ‘T’. then for every input (t, dt) to M. if TM’T’ halts for the input ‘t’, then
TM’M’ reaches an accept halt.
If TM’T’ does not halt for the input ‘t’, then TM’M’ reaches a reject halt.
Now let us construct another TM ‘N’ which takes ‘dt’ as input and proceeds as
follows:-
First the TM’N’ copies the input ‘dt’ and duplicates ‘dt’ on its input tape and then takes
duplicated information tape as the input to TM’M’ with one modification namely, whenever ‘M’
is supposed to reach an accept halt TM ‘N’ will loop forever.
Considering the original behaviour of TM ‘M’ we find that TM ‘N’ acts as follows:-
It loops if TM ‘T’ halts for input t=dt and halts if TM’T’ does not halt for the input t=dt.
Now, since TM’N’ itself is a TM, let us set T=N. thus replacing ‘ T’ by ‘N’ we get that TM‘N’
halts for the input ‘ dn’ if and only if TM‘N’ does not halt for the input ‘ dn’. This is a
contradiction. Hence we conclude that TM’N’ which can decide whether any other TM will ever
halt does not exist. Therefore halting problem is unsolvable.

103
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

CHAPTER NO 8
UNDECIDABILITY
• Recursive Language:-
if the language is turing decidable i.e. A language L ∈ Σ* is sai9d to be turing decidable, when
there is a turing machine M that halts for every string w that belongs to the given input (i.e. w ∈
Σ*), if the string w belongs to the language L the turing machine halts with an answer yes and if
the string w does not belongs to language L, then the turing machine halts with an answer no. if
the language is turing decidable then it is termed to be Recursive language.

• Recursive Enumerable Language:-


If the language is turing acceptable i.e. A language L ∈ Σ* is said to be turing acceptable, when
the turing machine M halts with an answer yes if the string w belongs to the language L and the
turing machine may or may not halt for the strings w that does not belong to language L. if the
Language is turing acceptable then it is termed to be Recursive Enumerable Language.

• Undecidability and Unsolvability:-


A problem whose language is recursive is said to be decidable otherwise the problem is termed to
be undecidable. There is no algorithm that could determine that the string w belongs to the
language.
Tm will halt or not for the given input w is an undecidable problem.
If there is a TM that terminates or Halts with a correct answer yes or no for every input of a
problem, then the problem Is termed to be solvable.
If there is a TM that terminates or halts with a correct answer yes for the input string w that
belongs to the language and may or may not halt with an answer no for the strings that does not
belongs to the language, then that problem is termed to be partially solvable.
If there is no TM that terminates or halts with a correct answer yes for the strings that belongs to
the language L, then that problem is termed to be unslovable.

104
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Post Correspondence Problem (PCP):-


An instance of post’s correspondence problem (PCP) consists of two lists of strings over some
alphabet Σ, the two lists must be of equal length. We generally refer to the A and B lists and write
A=w1,w2…wk and B=x1,x2…xk for some integer k. For each i, the pair (wi,xi) is said to be a
corresponding pair.
We say this instance of PCP has a solution if there is a sequence of one or more integers i1,i2…im
such that when interpreted as indexes of strings in the A and B lists, yield the same string, i.e.
wi1,wi2,…wim=xi1,xi2,…,xim , we say the sequence i1,i2,…im is a solution to this instance of PCP.
If there exists a solution to PCP, there exists infinitely many solutions.
LIST A LIST B
i Wi Xi
1 110 110110
2 0011 00
3 0110 110

A and B are defined above and let Σ={0,1}.


For instance let m=3, i1=2 ,i2=3 and i3=1.
If solution exists to the above PCP, it should verify the following condition i.e. w2w3w1 = x2x3x1.
i.e. 00110110110=00110110110. Since the above condition is verified, therefore the solution is
the list 2,3,1.
It is not necessary this solution is unique, i.e. there can exists more than one solution.
Eg:-
LIST A LIST B
i Wi Xi
1 011 0111
2 1 10
3 1 11

In above example there exists no solution to PCP because for all the substrings Wi ∈ A and Xi ∈
B, we have |Wi| < |Xi| for all I, hence the length of string generated by a sequence of substrings of
A will be less than the length of string generated by the sequence of corresponding substrings of
B.
Hence PCP has no solution for the above example.
NOTE:-
1) PCP is undecidable.
2) If the first substring used in PCP is always W1 and X1, then PCP is known as modified
Post Correspondence Problem(MPCP).

105
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021

• Rice’s Theorem:-
Every non-trivial property of the RE languages is undecidable.
PROOF:-
Let P be a non-trivial property of RE languages. Assume to begin that φ, the empty language, is
not in P. we shall return later to opposite case.
Since P is non-trivial, there must be some non-empty language L that is in P.
Let ML be a TM accepting L.
We shall reduce Lu to Lp, thus proving that Lp is undecidable, since Lu is undecidable.
The algorithm to perform the reduction takes as input a pair (M,W) and produces a TM M’.
The design of M’ is suggested by Fig 1.0, L(M’) is φ if M does not accept W, and L(M’)=L if M
accepts W.
M’ is a two-tape TM. One tape is used to stimulate M on W. Remember that the algorithm
performing the reduction is given M and W as input, and can use this input in designing the
transitions of M’. Thus, the stimulation of M on W is built in to M, the latter TM does not have to
read the transitions of M on a tape of its own.
The other tape of M’ is used to stimulate ML on the input X to M’, if necessary.
Again, the transitions of ML are known to reduction algorithm and may be built in to the
transitions of M’. The TM M’ is constructed to do the following:-
1) Stimulate M on input W. Note that W is not the input to M’, rather M’ writes M and W on to
one of its tape and stimulates the Universal Turing Machine(UTM) on that pair.
2) If M does not accept W, then M’ does nothing else. M’ never accepts its own input X, so
L(M’)=φ. Since we assume φ is not in property P, that means the code for M’ is not in Lp.
3) If M accepts W, then M’ begins stimulating ML on its own input X. Thus M’ will accept
exactly the language L. since L is in P, the code for M’ is in Lp.
One should o0bserve that constructing M’ from M and W can be carried out by an algorithm.
Since this algorithm turns (M,W) in to an M’ that is in Lp if and only if(M, W) is in Lu, this
algorithm is a reduction of Lu to Lp, and proves the property P is undecidable.

106
Downloaded by hemant sawant ([email protected])

You might also like