Tcs Notes For Academy Tcs Notes For Academy
Tcs Notes For Academy Tcs Notes For Academy
INDEX
Chapter No Chapter Name Page Nos
1 FSM 2-11
2 RE 12-23
5 Grammars 66-85
6 PDA 86-99
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)
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
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
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)
STF : S I → S
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)
SFT : S I → S
Q.5. Design FSM in which the input is valid if it ends in “100” = {0, 1}
Solution :-
Step II : Logic
I= (0, 1)
O = (y, n)
S= (qs, q0, q1, q2, q3)
5
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
Solution :
Step I : Theory of FSM
Step II : Logic
I= (a, b)
O = (y, n)
S= (qs, q0, q1, q2, q3, q4)
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
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}
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.
Solution :
I= {(0, 0), (0, 1), (1, 0) (1, 1)}
O = {0, 1}
S= {qs, q0, q1)
STF = S I → S
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.
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 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
12
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
examples:
L = {ba} = L1 L3 = L.L.L. = {bababa}
L2 = L.L. = {baba} L0 = { }
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*
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
15
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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.
16
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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
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
21
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
p qj = qk r qm
q0
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):-
* 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.
24
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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
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 2 : NFA for r = E {}
Start
N (R)
Start
N (S)
Start
N (R)
N (S)
OR
Start
N (R) N (S)
27
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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
Solution :
Start a
1
2
a 3
4
NFA for r = a+
(7) r = (a + b)*
Solution :
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
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
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
31
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
δ:
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
32
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
1 1
2 3 4
Start
0 1 8 9
1 0
5 6 7
δ:
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
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
δ:
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
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.
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
0,1
q 0,1
0
Start
P r
0
0,1
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
δ:
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
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
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
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
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
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
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 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)
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
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
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
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
Q5 Q5
δ (Q3, B) = δ (Q5, B) =
r r
Step 5 :
52
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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
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
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
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
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}
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
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
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/
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
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
0/c
q1 q2
0/b
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
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
Rd by → Small Letters
→ Operators
66
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
* 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.
67
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
Q) S → aASa
A → sbA ssba
Derive “aabbaa” using
LMD & RMD
Ans. LMD : S aAS Using S → aAS
lm
aabAS Using S → a
lm
aabbaS Using A → ba
lm
aabbaa Using S → a
lm
aAa Using S → a
rm
aSbbaa Using A → ba
rm
aabbaa Using S → a
rm
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 → aasaBB
Derive using LMD & RMD
(1) aaabbb
(2) bbaaba
(3) aaba
Ans. (1) LMD : S aB Using S → aB
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 → b
rm
aaaBbb Using B → b
rm
aaabbb Using B → b
rm
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 → 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
* e.g. of CFG :
G L(G)
S→a {a}
S→b {b}
S → ab {a} {b} = {a,b}
S → ab {ab}
S → aS {, a, aa, aaa, ….}
S → abS {, ab, abab, …}
S → aSbS {, a, b, aa, ab, ba, …}
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 → 0A01A12A2 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 → 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→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
71
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
10) L = {ai bj I, j 1}
Ans. S → XY
X → aXa
Y → bYb
(Because a and b are independent)
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 → cYc
S2 → AB
A → aA a
B → bBc bc
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
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 abAB
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)
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
74
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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
75
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
P + np
S → aS bA b bAB bB
A → aA ac
B → bB bda
76
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
P + np
S → aS aAaBdCdb
A → aBad
B → cBcad
C → aB adb
77
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
* Normal forms :
The Normal form implies the rewriting of CFG as per the specified conditions.
: 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
Step 2 : Add to the solution the productions that are already in CNF.
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
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 aabBaba
B → aBabBb
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 → C1C2C1C1C3C4C3C1 C1 → a
B → abC1BC3B C2 → BC1
C3 → b
C4 → BC1
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
Step 2: Use any combinations of Rule 1, Rule 2 and Rule 3 to obtain CFG in GNF.
Step 2: B → bBa
A → aAb
S → aABA bBA | bBBaB | aA R1
2) A1 → A2A2 a
A1 → A1A1b
Ans. A2 → A1A1b
A2 → A2 A2 A1aA1b
A → A 1 B1 B2 R1
81
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
A1 → aA1A2aA1BA2bA2bBA2a R1
B → aA1A1aA1BA1bA1bBA1aA1A1BaA1BA1BbA1BbBA1B R1
June 2011/5M
Dec 2011/8M
3) S → AA0
A → SS1
Ans. A → SS1
A → AAS0S 1 R1
A → A 1 1 2 2
→ iiB
A → iiB
i.e. B → ASASB
A → 0S 0SB11B
R2
2
S → aSabSbc
4)
S → aSC1 | bSC2 | c
C1 → a R3
2
C2 → b
5) S → SSaSbab
Ans. S → SS asbab
A → A1 1 2
B → iiB i.e.
A → iiB i.e.
B → SSB
S → aSbaSbBababB
R2
2
S → aSC1aSC1BaC1aC1B
C1 → b R3
B→ aSC1 aSC1BaC1aC1BaSC1BaSC1BBaC1BaC1BB R1
June 2011 / 5M
Q) S → AB
A → BSBBBb
B → aAba
Ans. B → aAC1a
Where C1 → b R3
A → aAC1SBaSBaAC1BaBb R1
S → aAC1SBBaSBBaAC1BBaBBbB R1
82
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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
83
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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*Eid
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:-
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
* 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.
86
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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)}
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
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, )}
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, )}
X
YX
X
R
For every ‘(’ push 1X
For every ‘)’ pop 1Y
92
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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
93
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
(q0, abbbba, R)
+ (q0, bbbba, XR)
+ (q0, bbba, YXR)
CFG to PDA :
Given CFG G = (V, T, P, S)
Define PDA M = (Q, , , , q0, z0, f)
For recognizing CFL generated by CFG G
(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
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
95
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
+ (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, r] ; r Q
[q, A, P] → a
96
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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
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)
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.
100
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
101
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
• Variants of TM:-
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.
104
Downloaded by hemant sawant ([email protected])
lOMoARcPSD|43069021
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])