Scsb1303 Toc Notes
Scsb1303 Toc Notes
Introduction
1.1. What is Theory of Computation or Automata Theory?
1
Table 1.1 Modelling Languages and Language Acceptor
2
w ={ 1,01,10,100,010, 111,1011… ......}
w ={ 1,01,10,100,010, 110 ,111,1011……..} --invalid
L = {w/w consists of odd no. of 1’s}
2. Find L with 0’s and 1’s with even no. of 1’s
∑ ={ 0,1}
w ={Λ ,11,011,101,110,0110, 1010, ........... }
w ={Λ ,11,011,101,100, 110,0110, 1010, …..} --invalid
L = {w/w consists of even no. of 1’s}
3. Find L with a’s and b’s with even no. of a’s
∑ ={ a,b}
w ={Λ ,aa,baa,aba,aab,baab, abba,abab ........... }
w ={Λ ,aa,baa,aaa,aab,baab, abba,abab ……..} --invalid
L = {w/w consists of even no. of a’s}
4. Find L with a’s and b’s with even no of a’s and b’s.
∑ ={ a,b}
w ={Λ ,aa,bb,aabb,abab,baba,bbaa ........... }
L = {w/w consists of even no. of a’s and b’s}
5. Find L with a’s and b’s having a substring aa
∑ ={ a,b}
w ={aa,baa,aab,baab, abaa,aabaa............ }
L = {w/w consists of a substring aa}
1.4. Regular Language:
A language is regular if there exits a DFA for that language.
The language accepted by DFA is RL.
1.5. Regular Expression:
A Mathematical notation used to describe the regular language.
This is formed by using 3 Symbols:
(i). [dot operator] – for concatenation
(ii) + [Union operator] –at least 1 occurrence
eg) 1+ = {1,11,111,-------}
(iii) {*} [Closure Operator ] – Zero or more occurrences
eg) 1* = {Λ ,1,11,111,… }
3
1.6 Basic Regular Expressions:
• Ø is a RE and denotes the empty set.
• ε is a RE and denotes the set { ε }
• For each a in ∑, a is a RE and denotes the set {a}
• If r and s are RE that denoting the languages R and S respectively, then,
– (r+s) is a RE that denotes the set (RUS)
– (r.s) is a RE that denotes the set R.S
– (r)* is a RE that denotes the set R*
1.7 Problems on RE:
1. Write the RE for the language of even no. of 1’s.
∑={0,1}
W={ᴧ,11,011,101,110,1111,1100,....}
RE=(11)*
2. Write the RE for the language of odd no. of 1’s.
∑={0,1}
W={1,10,01,100,111,1110,.....}
RE=(11)*.1
3. Write the RE for the language of any length including Ʌ.(a,b)
∑={a,b}
W={ᴧ,a,b,aa,ab,aab,baa,aaaa,. .... }
RE=(a+b)*
4. Write the RE with a string starting with a.
∑={a,b}
W={a,aa,ab,aaa,abb,. ....}
RE=(a).(a+b)*
5. Write the RE for the language having a substring aa.
∑={a,b}
W={aa,baa,aab,baaa,aaaa ... }
RE=(a+b)*.aa.(a+b)*
6. Write the RE with a string starting with either a or ab.
∑={a,b}
4
W={a,ab,aa,aba,abb,abbb,. ... }
RE=(a+ab).(a+b)*
7. Write RE with a string consists of a’s and b’s ending with abb.
∑={a,b}
W={abb,aabb,babb,aaabb,. ..... }
RE=(a+b)*.abb
8. Write RE with a string consists of a’s and b’s starting with abb.
∑={a,b}
W={abb,abbb,abba,abbaa,. .... }
RE=abb.(a+b)*
9. Write the RE for identifiers in ‘C’ Programming.
Letter=(a-z)
Digit=(0-9)
RE=(letter+_).(letter+_+digit)*
10. Write the RE with a string that should not start with two 0’s.
∑={0,1}
W={0,1,01,011,110,....}
RE=(01+1).(1+0)*
11. Write RE with a string that begins and ends with double consecutive letters.
∑={a,b}
W={aa,bb,aabb,bbaa,aabaa,bbbaa,. ..... }
RE=(aa+bb).(a+b)*.(aa+bb)
12. Strings of a’s and b’s in which 3rd symbol from right end is ‘a’.
∑={a,b}
W={aaa,abb,aabb,bbabb,…}
RE=(a+b)*a.(a+b)(a+b)
13. Write RE for the strings consisting of atleast 1 ‘a’ followed by strings consisting of
atleast 1 ‘b’ followed by strings consisting of atleast 1 ‘c’.
∑={a,b,c}
W={abc,aabc,abbc,aabbcc,aaabbc,…}
RE=a+.b+.c+
5
14. Write the RE for the strings over {0,1} of length 6 or less.
∑={0,1}
Length 6-{001100,110011,010101,000000,..}
(0+1).(0+1).(0+1).(0+1).(0+1).(0+1)
(0+1+Ʌ)6
(0+1)6
(0+1+Ʌ)*
6
Figure 1.2 Types of Automata
• Only one path for specific input from the current state to the next state.
• DFA does not accept the null move.
• It is used in Lexical Analysis phase in Compilers.
Example: RE=(a+b)+
7
2.2 Construction of DFA:
1. Construct DFA to accept strings of a’s and b’s having a substring aa.
W={aa,aaa,baa,aab,aabb,abaa…..}
W={a,ab,ba,bba,abb,bbba,….}
8
Figure 1.6 State Diagram
DFA Definition
M=(Q, ∑,q0, δ,A)
Q={q0,q1}
∑={a,b}
q0 =q0
A = q1
δ –Transition Function
δ(q0,a)=q1
δ(q0,b)=q0
δ(q1,a)=null
δ(q1,b)=q1
3. Construct DFA to accept strings of a’s and b’s with atleast 1 ‘a’.
W={a,aa,ab,ba,bba,baa,….}
9
q0 =q0
A = q1
δ –Transition Function
δ(q0,a)=q1
δ(q0,b)=q0
δ(q1,a)=q1
δ(q1,b)=q1
4. Construct DFA to accept strings of 0’s and 1’s with substring 01.
W={01,001,010,011,1001,…..}
W={Ʌ,a,aa,aaa,aba,abb,bbb,aabb,…..}
10
Figure 1.9 State Diagram
DFA Definition
M=(Q, ∑,q0, δ, A)
Q={q0,q1,q2,q3}
∑={a,b}
q0 =q0
A = {q0,q1,q2,q3}
δ –Transition Function
δ(q0,a)=q1
δ(q0,b)=q0
δ(q1,a)=q2
δ(q1,b)=q1
δ(q2,a)=q3
δ(q2,b)=q2
δ(q3,a)=null
δ(q3,b)=q3
6. Construct DFA to accept strings that end with 011.
W={011,1011,0011,11011,00011,100011…..}001000011,01100011,
11
∑={0,1}
q0 =q0
A = q3
δ –Transition Function
δ(q0,0)=q1
δ(q0,1)=q0
δ(q1,0)=q1
δ(q1,1)=q2
δ(q2,0)=q1
δ(q2,1)=q3
δ(q3,0)=q1
δ(q3,1)=q0
7. Construct DFA to accept strings of a’s and b’s with even number of a’s and b’s.
W={Ʌ,aa,bb,aabb,abab,baba,aaaabb…}
8. Construct DFA with even number of a’s and odd no. of b’s.
W={b,aab,aba,baa,ababb,aaaab,aababab,….}
M=(Q, ∑,q0, δ, A)
9. Construct DFA with odd number of a’s and even no. of b’s.
W={a,abb,bba,bab,abbaa,ababa,…..}
12
M=(Q, ∑,q0, δ, A)
10. Construct DFA with odd number of a’s and b’s.
W={ab,ba,babb,ababab,…….}
M=(Q, ∑,q0, δ, A)
11. Construct DFA to accept odd and even no’s represented using binary notation.
0->0
1->1
2->10
3->11
4->100
5->101
6->110
7->111
8->1000
010
13
Figure 1.11.State Diagram
M=(Q, ∑,q0, δ, A)
2.2 Extension of δ: δ*
δ-> for one input symbol
δ=Q x ∑ -> Q
δ*->the state in which FA ends up if it begins in state q and receives a string x of input
symbols
δ*(q,x) i.e. δ*: Q x ∑*->Q
14
δ*(q0,abc) = δ(δ*(q0,ab),c)
= δ(δ(δ*(q0,a),b),c)
= δ(δ(δ(δ*(q0,Ʌ),a),b),c)
= δ(δ δ(q0,a),b),c)
= δ(δ(q1,b),c)
= δ(q2,c)
=q3
2.4 STRING ACCEPTANCE BY FINITE AUTOMATA:
Let M=(Q,∑,q0,A, δ) be an FA. A string xε∑* is accepted by M, if
δ*( q0,x)ϵA
15
∑: finite set of the input symbol
q0: initial state
A: final state
δ: Transition function
δ: Q x ∑ →2Q
3.1 Example:
Obtain an NFA to accept the language L={ w| w ε ababn or aban}
16
3.2 Problem: convert NFA to DFA [Subset Construction Method]
Solution:
MN=( QN,∑N,q0,AN,δN) be an NFA.
δN(q0,a)={q0,q1)
δN(q0,b)={q1)
δN(q1,a)= ∅
δN(q1,b)={q2)
δN(q2,a)={q2)
δN(q2,b)={q2)
MD=( QD,∑D,q0,AD,δD) be an DFA.
Step 1:
Start state of NFA is the start state of DFA.
Obtain the transitions from this state. State a b
q0 is the start state. q0 A B
δD (q0,a)=δN(q0,a) A
=[q0,q1] --- A B
δD (q0,b)=δN(q0,b)
=[q1] --- B
δD (A,b)= δN(A,b)
17
= δN([q0,q1],b)
= δN(q0,b) U δN(q1,b)
={q1}U{q2}
=[q1,q2] ----- C
State a b
q0 A B
A A C
B
Transition from B:
C
δD (B,a)= δN(B,a)
= δN([q1],a)
= δN(q1,a)
=∅
δD (B,b)= δN(B,b)
= δN([q1],b)
= δN(q1,b)
={q2} -- D
State a b
q0 A B
A A C
B ∅ D
C
Transition from C:
D
δD (C,a)= δN(C,a)
= δN([q1,q2],a)
= δN(q1,a)U δN(q2,a)
=∅Uq2
=[q2]------D
δD (C,b)= δN(C,b)
= δN([q1,q2],b)
= δN(q1,b) U δN(q2,b)
={q2}Uq2
18
=[q2]------D
State a b
q0 A B
A A C
B ∅ D
C D D
D
Transition from D:
δD (D,a)= δN(D,a)
= δN([q2],a)
= δN(q2,a)
=[q2]------D
δD (D,b)= δN(D,b)
= δN([q2],b)
= δN(q2,b)
=q2 ---D
State a b
q0 A B
A A C
B ∅ D
C D D
D D D
19
The final state q2 of NFA is there in C and D states in DFA. So make C and D as final state.
Determine:
20
𝜹(𝒓,𝟏)
i) δ*(q0,11)= (⋃ )
𝒓𝜺𝜹∗(𝒒,𝟏)
= ⋃𝜹(𝒓,𝟏)
𝒓𝜺{𝒒𝟎,𝒒𝟏} )
= δ(q0,1) U δ(q1,1)
={q0,q1} U {q2}
={q0,q1,q2}
∴11 is not accepted.
ii) δ*(q0,01)= (⋃𝜹(𝒓,𝟏) )
𝒓𝜺𝜹∗(𝒒,𝟎)
𝜹(𝒓,𝟏)
= ⋃𝒓𝜺{𝒒𝟎} )
= δ(q0,1)
={q0,q1}
∴01 is not accepted.
𝜹(𝒓,𝟏)
iii) δ*(q0,111)= (⋃ )
𝒓𝜺𝜹∗(𝒒,𝟏𝟏)
= ⋃𝜹(𝒓,𝟏)
𝒓𝜺{𝒒𝟎,𝒒𝟏,𝒒𝟐} )
= ⋃𝜹(𝒓,𝟏)
𝒓𝜺{𝒒𝟎,𝒒𝟏}
)
= δ(q0,1) U δ(q1,1)
={q0,q1} U{q2}
={q0,q1,q2}
∴11 is not accepted.
3.6 THEOREM 1:
Statement:
For any NFA machine M=(Q, ∑, q0, δ , A) accepting a language L ⊆ ∑*, there is a DFA
machine M1= (Q, ∑, q0, δ , A) that accepts L.
21
Proof by Structural Induction:
Proof:
DFA- M1is defined as follows:
– Q1=2Q
– q1=q0
– A1={qϵQ1 |A⊆Q}
– For,qϵQ1 and aϵ∑,
3. Induction Step:
To prove, for any aϵ∑, xϵ∑*
δ1*(q1,xa)= δ*(q0,xa) to be proved
22
This allows transitions not only input symbols but also on null inputs.
Ʌ or ε
aɅ=a | oɅ=0 |1Ʌ = 1
o*
4.1 Definition:NFA-Ʌ
NFA-Ʌ is a 5 tuple machine M=(Q,∑,q0,A,δ), where
Q - is a set of finite states,
∑ - finite set of input symbols
q0εQ,
A ⊆ Q and
δ:Qx(∑U{Ʌ})->2Q
• It is a set of all states that can be reached from any state on Λ symbol
• Let M=(Q, ∑, q0, δ , A) be a NFA- Λ machine and let S be any subset of Q.
• The Λ(S) is the set defined as follows:
1. Every element of S is in element of Λ(S)
2. For any q 𝞮 Λ(S), every element of δ(q, Λ) is in Λ(S)
4.3 Problems on Ʌ Closure:
1.
Ʌ(q2)={q2}
23
2.
24
(i) Find δ*(q0, Ʌ)
δ*(q0, Ʌ) = Ʌ({q0})
={q0,p,t}
(ii) Find δ*(q0, 0)
δ*(q0, Ʌ0) = 𝚲(⋃𝛅(𝐫,𝟎) )
𝒓𝜺𝛅∗ (𝐪𝟎,𝚲)
𝛅(𝐫,𝟎)
= 𝚲(⋃𝒓𝜺{𝒒𝟎,𝒑,𝒕} )
= Ʌ(δ(q0,0) U δ(p,0)U δ(t,0))
= Ʌ(∅ U{P}U{u})
= Ʌ({p,u})
={p,u}
String not accepted.
(iii) δ*(q0, 01)
δ*(q0, 01) = 𝚲(⋃𝛅(𝐫,𝟏) )
𝒓𝜺𝛅∗ (𝐪𝟎,𝟎)
𝛅(𝐫,𝟏)
= 𝚲(⋃𝒓𝜺{𝒑,𝒖} )
= Ʌ(δ(p,1) U δ(u,1))
= Ʌ({r}U∅)
= Ʌ({r})
={r}
String not accepted.
(iv) δ*(q0, 010)
δ*(q0, 010) = 𝚲(⋃𝛅(𝐫,𝟎) )
𝒓𝜺𝛅∗ (𝐪𝟎,𝟎𝟏)
𝛅(𝐫,𝟎)
= 𝚲(⋃𝒓𝜺{𝒓} )
= Ʌ(δ(r,0))
= Ʌ({s})
={s,u,q0,p,t}
String is accepted.
4.5 Conversion of NFA-Ʌ to an NFA:
25
Figure 1.21 State Diagram
Ʌ-closure(A)={A,B,D}
Ʌ-closure(B)={B,D}
Ʌ-closure(C)={C}
Ʌ-closure(D)={D}
(i) δ*(A,0)= δ*(A,Ʌ0)
𝛅(𝐫,𝟎)
=𝚲(⋃𝒓𝜺𝛅∗ )
(𝐀,𝚲)
= 𝚲(⋃𝒓𝛅(𝐫,𝟎)
𝜺 𝚲(𝐀))
𝛅(𝐫,𝟎)
= 𝚲(⋃𝒓𝜺 {𝑨,𝑩,𝑫}
)
=Ʌ(δ(A,0) U δ(B,0) U δ(D,0))
= Ʌ(A,C,D)
={A,B,C,D}
(ii) δ*(A,1)= δ*(A,Ʌ1)
𝛅(𝐫,𝟏)
=𝚲(⋃𝒓𝜺𝛅∗ (𝐀,𝚲))
= 𝚲(⋃𝒓𝛅(𝐫,𝟏)
𝜺 𝚲(𝐀))
𝛅(𝐫,𝟏)
= 𝚲(⋃𝒓𝜺 {𝑨,𝑩,𝑫} )
= 𝚲(⋃𝒓𝛅(𝐫,𝟎)
𝜺 𝚲(𝐁))
𝛅(𝐫,𝟎)
= 𝚲(⋃𝒓𝜺 {𝑩,𝑫})
=Ʌ(δ(B,0) U δ(D,0))
= Ʌ(C,D)
26
={C,D}
(iv) δ*(B,1)= δ*(B,Ʌ1)
𝛅(𝐫,𝟏)
=𝚲(⋃𝒓𝜺𝛅∗ (𝐁,𝚲))
= 𝚲(⋃𝒓𝛅(𝐫,𝟏)
𝜺 𝚲(𝐁))
𝛅(𝐫,𝟏)
= 𝚲(⋃𝒓𝜺 {𝑩,𝑫})
=Ʌ(δ(B,1) U δ(D,1))
= Ʌ(∅)
=∅
(v)δ*(C,0)= δ*(C,Ʌ0)
𝛅(𝐫,𝟎)
=𝚲(⋃𝒓𝜺𝛅∗ (𝐂,𝚲))
= 𝚲(⋃𝒓𝛅(𝐫,𝟎)
𝜺 𝚲(𝐂))
𝛅(𝐫,𝟎)
= 𝚲(⋃𝒓𝜺 {𝑪} )
=Ʌ(δ(C,0))
= Ʌ(∅)
=∅
(vi) δ*(C,1)= δ*(C,Ʌ1)
𝛅(𝐫,𝟏)
=𝚲(⋃𝒓𝜺𝛅∗ (𝐂,𝚲))
= 𝚲(⋃𝒓𝛅(𝐫,𝟏)
𝜺 𝚲(𝐂))
𝛅(𝐫,𝟏)
= 𝚲(⋃𝒓𝜺 {𝑪} )
=Ʌ(δ(C,1))
= Ʌ(B)
={B,D}
= 𝚲(⋃𝒓𝛅(𝐫,𝟎)
𝜺 𝚲(𝐃))
𝛅(𝐫,𝟎)
= 𝚲(⋃𝒓𝜺 {𝑫} )
=Ʌ(δ(D,0))
= Ʌ(D)
={D}
27
(viii) δ*(D,1)= δ*(D,Ʌ1)
𝛅(𝐫,𝟏)
=𝚲(⋃𝒓𝜺𝛅∗ (𝐃,𝚲))
= 𝚲(⋃𝒓𝛅(𝐫,𝟏)
𝜺 𝚲(𝐃))
𝛅(𝐫,𝟏)
= 𝚲(⋃𝒓𝜺 {𝑫} )
=Ʌ(δ(D,1))
= Ʌ(∅)
=∅
Transition Table:
NFA- Ʌ NFA
∑
STATES δ*(q,0) δ*(q,1)
Ʌ 0 1
A {B} {A} ∅ {A,B,C,D} ∅
B {D} {C} ∅ {C,D} ∅
C ∅ ∅ {B} ∅ {B,D}
D ∅ {D} ∅ {D} ∅
NFA:
Note: Final state of NFA and NFA- Ʌ are same, moreover if initial state Ʌ(A)={A,B,D} has
NFA-Ʌ final state then initial state A is also final state.
4.6.Theorem 2
• Equivalence of NFA-ꓥ and NFA
28
• If L is accepted by an NFA with ꓥ transitions then L is accepted by NFA without ꓥ
transitions
• Statement:
For any NFA NULL machine M=(Q, ∑, q0, δ , A) accepting a language L ⊆ ∑*, there
is a NFA machine M1= (Q, ∑, q0, δ , A) that accepts L.
Given:
NFA-ꓥ, M=(Q,∑,δ,q0,A)
NFA, M1=(Q1,∑,δ1,q1,A1)
Proof:
• By Definitions:
• For, aϵ∑ :
δ1(q1,a) = 𝚲(⋃𝛅(𝐫,𝟏) ) (By definition it is true if we pass a single length string)
𝒓𝜺𝛅∗ (𝐂,𝚲)
Basis Step:
29
δ1*(q1,xa)= δ*(q0,xa)
LHS: δ1*(q1,xa)
=⋃𝛅(𝐫,𝐚) ) ------(By Induction Hypothesis)
𝒓𝜺 δ1∗{𝒒𝟏,𝒙}
=⋃𝛅(𝐫,𝐚)
𝒓𝜺 δ∗{𝒒𝟎,𝒙}
)
=⋃𝛅(𝐫,𝐚) 𝛅(𝐬,𝚲)
)
𝒓𝜺 𝚲(⋃𝒔𝜺 δ∗{𝒒𝟎,𝒙} )
𝛅(𝐬,𝐚)
= 𝚲(⋃𝒔𝜺 δ∗{𝒒𝟎,𝒙}
)
= δ*(q, xa)
=RHS Hence the theorem is proved
30
Case 1: r=r1+r2
31
Figure 5.2 State Diagram
32
Any path from q0 to f0 consists either of a path from q0 to f0 on Ʌ or a path from q0 to q1
on Ʌ, followed by some number of paths from q1 to f1, then back to q1 on Ʌ, each labelled
by a string in L(M1) followed by a path from q1 to f1 on a string in L(M1).
Hence L(M)= L(M1)*
OR
The NFA-Ʌ recognizes {ab}
Step 5: RE=a*
33
The NFA-Ʌ recognizes {ε,a,aa,aaa,. ..... }
Step 6: RE= a+
= a.a*
Follow step 4 for construction.
Problems:
Construct NFA-Ʌ for the following regular expression using Thompson’s Construction
method.
a. (a|b)*abb
a
a/b
34
35
Figure 5.4 State Diagram of NFA-NULL
6.Kleene’s Theorem Part -II
• Any language accepted by a finite automaton is regular.
• Where
• i= Start state
• j = Final state
• k = No.of states
36
Problem 1: Obtain the regular expression for the finite automata shown below:
37
i=1 (start state)
j=2 (final state)
k=2 (no. of states)
Substitute I,j,k in formula:
Find:
In:
When K=0:
38
Substitute in equation 2 and 3:
Substitute in equation 3:
When K=0:
Substituting in 1:
39
40
SCHOOL OF COMPUTING
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Terminals:
– Defines the basic symbols from which a string in a language are composed.
– Represented in lower case letters.
Non Terminals :
– They are special symbols that are described recursively in terms of each other and
terminals. Represented in upper case letters.
Production rules:
– It Defines the way in which NTs may be built from one another and from terminal.
Start Symbol:
– It is a special NT from which all the other strings are derived. It is always present in the first
rule on the LHS.
Example:
Consider the set of productions
<exp> →<exp>+<exp>
<exp>→<exp>*<exp>
<exp>→(<exp>)
<exp>→id
We apply productions
repeatedly (id+id)*id
<exp> ⇒ <exp>*<exp>
⇒ (<exp>)*<exp>
⇒ (<exp>+<exp>)*<exp>
⇒ (id+id) * id
Where (id+id) is the word in the language of <exp>
- Use E instead of
<exp> Productions:
E→ E+E
E→
E*E
E→
(E)
E→id
G=({E},{+,*,id,(,)},P,E)
1.1 Problems:
1. Given a CFG, find the language generated by G
(i) G=(N,T,P,S) where N={S}, T={a,b}
P={S→aSb, S→ab}
S=>ab S=>aSb S=>aSb S=>aSb
=>aaSbb =>aabb =>aaSbb
=>aaabbb =>aaaSbbb
=>aaaabbbb
W= { ab, aabb,aaabbb,aaaabbbb,.......... }
L(G)={anbn |n≥1}
G=({S},{1,Ꜫ },P,S)
ii. Design a CFG for a language consisting of arithmetic expression.
T={id,+,-,*,/,(,)}
W={id,id+id,id-id,id*id,id/id, id+id*id,(id-id)/id,……}
P: S -> id
S -> S+S
S -> S-S
S ->S*S
S ->S/S
(or)
S->id |S+S |S-S|S*S |S/S |(S)
G=({S},{+,-,*,/,id},P,S)
iii. Design a CFG for a language accepting balanced parenthesis
T={{,},[,],(,)}
W=(Ꜫ ,{},[],(), {()},([]),{{}},(()),[[()]],….,{}[]{},()(),{}(),….}
P: S->{ S} |[S ] |(S ) |SS | Ꜫ
G=({S},{ Ꜫ ,[,],{,},(,)},P,S)
iv. L={𝒂𝒏𝒃𝒎| 𝒎 > 𝒏 𝒂𝒏𝒅 𝒏 ≥ 𝟎}
T={a,b}
W={b,bb,bbb,…,abb,abbb,…,aabbb,aaabbbb,…}
n=0, m=1=> b
n=0, m=2=>b, bb, bbb,bbbb,….
n=1, m=2 =>abb,abbb,abbbb,…
n=2, m=3=>aabbb,aaabbbb,….
P:
B->b |bB
S->aSb |B
G=({S,B}, {a,b} , P,S)
vii. Construct the CFG for the language having any number of a's followed by any number
of b’s over the set ∑= {a}
W={ Ꜫ ,aaaa,bbbb,aabb,abbb,…..}
a*.b*
x=aabb
P:
S->A.B
A->aA|Ꜫ
B->bB| Ꜫ
G=({S,A,B},{a,b, Ꜫ },P,S)
1.3 Regular Expression to CFG
i. Find the CFG equivalent to a Regular Expression
RE=ab. (a+bb)*
Generate the production for the language L1={ab}
A->ab
Generate the production for the language L2=a+bb
B->a | bb
Generate the production for the language L2*=(a+bb)*
C->Ꜫ |BC
RE=ab. (a+bb)*
P: S->A.C
C->Ꜫ |BC
A->ab
B->a | bb
G=({S,A,B,C}, {a,b, Ꜫ },P, }
ii. Obtain the CFG for RE=(011+1)*(01)*
L1=(011+1)
L2=>(011+1)*
Generate the production for the language L1=(011+1)
A->011|1
Generate the production for the language L2=(011+1)*
B->AB|Ꜫ
A->011|1
Similarly derive for (01)*
C->DC|Ꜫ
D->01
Finally generate the concatenation of the 2 languages by adding the production
S->BC
B->AB|Ꜫ
A->011|1
C->DC|Ꜫ
D->01
Definition 4:
Regular Grammar
A Grammar G=(N,T,P,S) is regular if every production takes one of the following forms:
B→aC
B→a
Where B & C are NT and ‘a’ is a T
2.5 Problems:
1. For the Grammar G defined by
S->AB
B->a|Sb
A->Aa|bB
Give the derivation trees for the following sentential forms
(i) baSb
S=>AB | S->AB
=>bBB | A->bB
=>baB | B->a
=>baSb | B->Sb
(ii) bAaBbB
S=>AB S->AB
=>bBB A->bB
=>bSbB B->Sb
=>bABbB S->AB
=>bAaBbB A->Aa
3. Ambiguity
Definition: An Ambiguous CFG
A CFG G is ambiguous if there is atleat one string in L(G) having two or more distinct derivation
trees(or equivalently two or more distinct LMD).
i. Is the following grammar ambiguous:
𝑙𝑚𝑑
E ⇒=⇒ E*E E-> E*E
𝑙𝑚𝑑
⇒=⇒ E+E*E E->E+E
𝑙𝑚𝑑
⇒=⇒ id+E*E E->id
𝑙𝑚𝑑
⇒=⇒ id+id*E E->id
𝑙𝑚𝑑
⇒=⇒ id+id*id E->id
12
There are 2 parse trees or 2 leftmost derivations for the string ‘id+id*id’. So the given grammar is
ambiguous.
ii. Is the following grammar ambiguous:
S->iCtS |iCtSeS |a
C->b
Consider the String: ibtibtaea
𝒍𝒎𝒅
S ⇒=⇒ iCtS S->iCtS
𝒍𝒎𝒅
⇒=⇒ ibtS C->b
𝒍𝒎𝒅
⇒=⇒ ibt iCtSeS S-> iCtSeS
𝒍𝒎𝒅
⇒=⇒ ibtibtSeS C->b
𝒍𝒎𝒅
⇒=⇒ ibtibtaeS S->a
𝒍𝒎𝒅
⇒=⇒ ibtibtaea S->a
13
𝒍𝒎𝒅
S⇒=⇒ iCtSeS
𝒍𝒎𝒅
⇒=⇒ibtSeS
𝒍𝒎𝒅
⇒=⇒ibtiCtSeS
𝒍𝒎𝒅
⇒=⇒ibtibtSeS
𝒍𝒎𝒅
⇒=⇒ibtibtaeS
𝒍𝒎𝒅
⇒=⇒ibtibtaea
14
There are 2 parse trees or 2 leftmost derivations for the string ‘ibtibtaea’. So the given
grammar is ambiguous.
iii. Is the following grammar ambiguous:
S->AB | aaB
A->a | Aa
B->b
String: aab
𝒍𝒎𝒅
S ⇒=⇒AB S->AB
𝒍𝒎𝒅
⇒=⇒ AaB A->Aa
𝒍𝒎𝒅
⇒=⇒ aaB A->a
𝒍𝒎𝒅
⇒=⇒ aab B->b
𝒍𝒎𝒅
S⇒=⇒ aaB S->aaB
𝒍𝒎𝒅
⇒=⇒ aab B->b
15
There are 2 parse trees or 2 leftmost derivations for the string ‘aab’. So the given grammar is
ambiguous.
An unambiguous CFG for Algebraic expression
If a CFG is ambiguous , it is often possible and usually desirable to find an equivalent unambiguous
CFG.
4. Normal Forms:
Chomsky Normal Form(CNF)
Greibach Normal Form(GNF)
4.1 Chomsky Normal Form(CNF):
Eliminating ꓥ productions
16
A->aAb | ab
C->aC |a
D ->aDa | bDb |aa |bb
Step 2:
Eliminating unit productions S->C
S->AACD | ACD |CD | AAC |AC | aC |a
A->aAb | ab
C->aC |a
D ->aDa | bDb |aa |bb
Step 3:Restricting the right sides of the productions to single terminals or strings of two or
more variables(NON TERMINALS).
S->AACD | ACD |CD | AAC |AC | Xa C |a
A-> Xa A Xb | Xa Xb
C-> Xa C |a
D -> XaD Xa | Xb D Xb | Xa Xa | Xb Xb
Xa->a
Xb->b
Step 4:
S->AT1 |AT2 |CD |AT3 |AC | Xa C |a
T1->AT2
T2->CD
T3->AC
A-> XaT4 | Xa Xb
T4-> A Xb
C-> Xa C |a
D->XaT5 | XbT6| Xa Xa | Xb Xb
T5->D Xa
T6-> D Xb
Xa->a
17
Xb->b
4.3 Greibach Normal Form (GNF)
Let G=(N, T, P, S) be a CFG. The CFG ‘G’ is said to be in GNF, if all the production are of the
form:
A->aα
Where a ↋ T and α ↋ N*
A non-terminal generating a terminal which is followed by any number of non-terminals.
For example, A → a.
S → aASB.
Step 1: Convert the grammar into CNF.
Step 2: Rename the non-terminals to A1, A2, A3,…
Step 3: In the grammar, convert the given production rule into GNF form.
Problem 1:
S-> AB1 |0S | ε
A->00A | B
B-> 1A1
Step 1: Eliminate null productions.
S-> ε
S->AB1 | 0S | 0
A->00A |B
B->1A1
Step 2: Eliminate unit productions
A->B is the unit production.
S->AB1 |0S |0
A->00A |1A1
B->1A1
Step 3: Restricting right hand side production with single terminal symbol or two or more
non terminals.
X- >0
18
Y->1
S->ABY | XS |0
A->XXA |YAY
B->YAY
Step 4: Final CNF
X->0 Y->1
S->AT1 T1->BY
S->XS | 0
A->XT2 T2->XA A->YT3 T3->AY
B->YT3
Step 5: Rename Non terminal as A1, A2, A3,……..
S=A1, A= A2, B=A3, X=A4, Y=A5, T1=A6, T2=A7, T3=A8
X->0 Y->1
S->AT1 T1->BY
S->XS | 0
A->XT2 T2->XA A->YT3 T3->AY
B->YT3
A4->0 A5->1
A1->A2A6 | A4A1 | 0
A2->A4A7 | A5A8
A3->A5A8
A6->A3A5
A7->A4A2
A8->A2A5
Step 6: Obtain productions to the form A->aα
Final CFG is
A4->0 A5->1
A2->0A7 |1A8
A3->1A8
A7->0A2
A8->0A7A5 | 1A8A5
A6->1A8A5
A1->0A7A6 | 1A8A6 | 0A1 |0
19
SCHOOL OF COMPUTING
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
1
UNIT 3 PUSH DOWN AUTOMATA
3. Definition
The PDA can be defined as a collection of 7 tuples:
M=( Q, ∑, Γ, q0, Z0, F, δ )
Q: the finite set of states
∑: the input set
Γ: a stack symbol which can be pushed and popped from the stack q0:
the initial state
Z0: a start symbol which is in Γ.
F: a set of final states.
δ: Transition function which is used for moving from current state to next state.
δ: Q x {Σ ∪ ε} x Γ -> Q x Γ*
(i.e) δ(q,a,x)=(p,α)
2
from state ‘q’ for an input symbol ‘a’, and stack symbol ‘x’, goto state ‘p’ and x is replaced by
string ‘α’.
3.1. Instantaneous Description (ID)
• An instantaneous description is a triple ID
(q, w, α)
Where:
Q describes the current state.
w describes the remaining input.
α describes the stack contents, top at the left.
Example Derivation: (p, b, T) ⊢ (q, w, α)
3.2. Definition: Acceptance by a PDA
1. Acceptance by Final State:
If M =(Q, ∑, Γ, δ, q0, Z, F) is a PDA and the language L(M) accepted by the final state is
given by: x∈∑* and x is accepted by M if,
L(M) = {x | (q0, x, Z0) ⊢* (q,Ʌ ,α)}
Where q∈Aα∈Γ*
Language Acceptance:
A language L ⊆∑* is said to be accepted by M, if L is precisely the set of string accepted by
M.
L=L(M)
4. Construction of PDA
1. Design a PDA for accepting a language {anbn | n>=1}.
Solution:
1. Decide the nature of the language b’s followed by ‘a’.
2. Execution procedure using stack:
Push all a’s on to stack.
For every ‘b’ pop out an ‘a’.
3. Define the states
q0 – push all a’s on to the stack.
q1 – when a ‘b’ encounters, pop ‘a’ from stack.
q2 – accepting state.
PDA Diagram
3
Transition Table:
4
|-(q1, b, az0)
|-(q1,Ʌ, z0)
|-(q2, Ʌ, z0)
String Accepted
Transition Table
Move No. State Input Symbol Top of stack Moves
1 q0 a Z0 (q0,az0)
2 q0 b Z0 (q0,bz0)
3 q0 a a (q0,aa)
4 q0 a b (q0,ab)
5 q0 b b (q0,bb)
6 q0 b a (q0,ba)
7 q0 C a (q1,a)
8 q0 C b (q1,b)
9 q0 C Z0 (q1,z0)
5
10 q1 a a (q1,Ʌ)
11 q1 b b (q1, Ʌ)
12 q1 Ʌ Z0 (q2,z0)
All other combinations - No moves
Rejected
6
Transition Table:
Move No. State Input Symbol Top of stack Moves
1 q0 [ Z0 (q0,[z0)
2 q0 { Z0 (q0,{z0)
3 q0 [ [ (q0,[[)
4 q0 [ { (q0,[{)
5 q0 { { (q0,{{)
6 q0 { [ (q0,{[)
7 q0 } { (q1, Ʌ)
8 q0 ] [ (q1, Ʌ)
9 q1 } { (q1, Ʌ)
10 q1 ] [ (q1, Ʌ)
11 q1 Ʌ Z0 (q2,z0)
12 q0 Ʌ Z0 (q2,z0)
All other combinations No moves
7
9 q1 Ʌ Z0
11 q2 Ʌ Z0
Accept
4.
5. Parsing:
To derive a string using the production rules for a grammar.
It is used to check whether or not a string is syntactically correct.
Parser takes the inputs and builds a parse tree.
5.1. Types of parser
Top down Parser- Parsing starts from the top with the start symbol and derives a string using
a parse tree.
Bottom up parser- Starts from the bottom with the string and comes to the start symbol using
a parse tree.
5.2. Design of Top down parser
1. Push the start symbol onto the stack.
2. If the top of the stack contains a NT, pop it out of the stack and push its right hand
side of the production.
3. If the top of the stack matches with input symbol being read, pop it.
4. If the input string is fully read and the stack is empty go to final state.
Statement:
8
Let G=(N,T,P,S) be a Context Free G, then there is a push down automata M, so that
L(M)=L(G)
Define ‘M’ as:
M=(Q,∑ , Γ, δ, q0, Z0, F)
Where, Q={q0,q1,q2}
Γ = N U ∑ U {z0}
A = {q2}
a) δ(q0,Ʌ,z0)={(q1,Sz0)}
d) δ (q1,Ʌ,z0)={(q2,z0)}
Q={q0,q1,q2}
A={q2}
Solution:
Let M=(Q, ∑, Γ, δ, q0, Z, F)
Q={q0,q1,q2}
∑={a,b}
Γ={S,a,b,z0}
F={q2}
9
Transition Table:
State Input Stack Moves
Symbol
q0 Ʌ Z0 (q1,Sz0)
Q1 Ʌ S (q1,a),(q1,aS),(q1,bSS),(q1,SSb),(q1,SbS)
Q1 a a (q1, Ʌ)
Q1 b b (q1, Ʌ)
Q1 Ʌ Z0 (q2,z0)
All other combination none
Example: abbaaa
S->a | aS | bSS | SSb| SbS
S=>SbS S->SbS
=>abS S->a
=>abbSS S->bSS
=>abbaS S->a
=>abbaaS S->aS
=>abbaaa S->a
10
(q1, aaa, aSz0)
(q1,aa, Sz0)
(q1,aa, aSz0)
(q1.a, Sz0)
(q1,a , az0)
(q1, Ʌ, z0)
(q2)
2. Construct PDA
S->S+X | X
X->X*Y | Y
Y->(S) | id
String: id+id*id
Solution:
Let M=(Q, ∑, Γ, δ, q0, Z0, F)
Q={q0,q1,q2}
∑={ +,*,id,(,)}
Γ={ z0,S,X,Y, +,*,id,(,)}
A={q2}
String: id*id+id
S=>S+X S->S+X
=>X+X S->X
=>X*X+X X->X*X
=>Y*X+X X->Y
-> id*X+X Y->id
=>id*Y+X X-> Y
=>id*id+X Y->id
11
=>id*id+Y X->Y
=>id*id+id Y->id
(q0,id*id+id,z0) -|(q0,id*id+id,Sz0)
(q1,id*id+id, S+X)
(q1, id*id+id, X+X)
(q1, id*id+id, X*X+X)
(q1, id*id+id, Y*X+X)
(q1, id*id+id, id*X+X)
(q1, *id+id, *X+X) (q1,
id+id, X+X) (q1,id+id,
Y+X)
(q1, id+id, id+X)
(q1, +id, +X)
(q1, id, X)
(q1, id, Y)
(q1, id, id)
12
Reduce T->a T+SZ0 *a
Shift - *T+SZ0 A
Shift - a*T+SZ0 -
Reduce T->T*a T+SZ0 -
Reduce S->S+T S Z0 -
Accept
Problem :
Construct DPDA for the language
L={x∈{a,b}*|na(x)>nb(x)}
solution
DPDA with Null transition :
13
W= {a, aa, aaa, aab, aba,....., baa, bbaaa, aabba, aaab,...}
DPDA without Ʌ
W= {a, aa, aaa, aab, aba,....., baa, bbaaa, aabba, aaab,...}
Transition Table:
Move No. State Input Symbol Top of stack Moves
1 q0 b Z0 (q0,b Z0)
2 q0 a b (q0,Ʌ)
3 q0 b b (q0,bb)
4 q0 a Z0 (q1, Z0)
5 q1 b a (q1, Ʌ)
14
6 q1 a Z0 (q1,aZ0)
7 q1 a a (q1,aa)
8 q1 b Z0 (q0, Z0)
7. Pumping Lemma
CFL- We can always find two pieces of any sufficiently long string to pump in tandem .i.e. if
we repeat each of the two pieces the same number of times, we get another string of the
language.
For any language L, we break its strings into five parts and pump second and fourth substring.
Let ‘L’ be any CFL. Then there is a constant ‘n’ depending on L, such that if ‘Z’ is in L and
|z|>=n, then we may write,
Z=uvwxy
uviwxiy ↋ L, For all i>0,
|vx|>=1
|vwx|<=n
7.1. Procedure
Assume that L is context free.
15
It has to have a pumping length(say n)
Find a string ‘z’ in L such that |z]>=n.
Divide z into uvwxy.
Show that uviwxiy ∉ L
Problem 1
L={anbncn, n>=0} is not a CFL.
Solution:
i. Assume ‘L’ is a CFL.
i. Let ‘n’ be a natural number obtained by using pumping lemma.
ii. Let z= anbncn |z|=n+n+n=3n
iv. Split z into uvwxy such that |vx|>=1, |vwx|<=n
Assume z=an-iaibn-j-kbjbkcn
u=an-i v=ai w=bn-j-k x=bj y=bkcn
for i=2
=> uv2wx2y =>an-iaiaibn-j-kbjbjbkcn
=>an+ibn+jcn ∉ L
Eg) n=4
4 4 4
Z=a b c =>aaaabbbbcccc
u=a v=aa w=abbbbc x=c y=cc
Let i=2
uviwxiy =>uv2wx2y=>aaaaaabbbbccccc=>a6b4c5∉L
Therefore the given language is not CFL.
Problem 2:
L={0p|p is prime is not CFL.
Solution:
i. Assume ‘L’ is a CFL.
i. Let ‘n’ be a natural number obtained by using pumping lemma.
i. Let P be a prime no. such that p>=n
Z=0pϵ L |z|=p>=n
iv. Split z into uvwxy such that |vx|>=1, |vwx|<=n
Let v=0k x=0l such that k+l>=1 and <=n
Hence |uwy|=p-k-l
If we pump v and x p+1 times
|uvwxy|=|uwy|+|v(p+1).x(p+1)|
=p-k-l+k(p+1)+l(p+1)
=p-k-l+pk+k+pl+l
=p+pk+pl
=p(k+l+1)
Which is not prime.
16
SCHOOL OF COMPUTING
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
1
UNIT 4 TURING MACHINES
Turing machines - Models of computation and the Turing thesis - Definition of TM and TM as
language acceptor - Non-deterministic TM and Deterministic TM – Universal TM.
1. Introduction
Model of Computation
But Turing machine can handle this type of computation using queue (Tape)
-Compare the first half of the string to the 2nd half then queue is more appropriate than stack.
“Any algorithmic procedure that can be carried out by a human, a team of humans or a
computer- can be carried out by some Turing machine”
2
Tape head is centered on one of the squares of the tape.
2. Definition of TM
(Q, Γ, ∑, δ, q0, B, F)
where –
q0 – Start state
F – Final state
δ : Q X Γ Q X Γ X {L, R, S}
q1 X |Y,D q2
From state q1 with x, replace X |Y , go to state q1, and move the tape head either D = {
L,R, S}(Fig 4.2)
(i) Crash: If in this situation D=L but the tape head is scanning square 0, the leftmost
square, the tape head is not allowed to move.
(ii) Halt: r=h, the move causes the turing machine to halt.
3
Turing Machine can be represented by
1. Transition Table
2. Instantaneous Description
3. Transition Diagram
α1, α2 ϵ Γ* - the contents of the tape upto the rightmost non blank symbol.
(iii) Turing machine will never halt and enters into an infinite loop.
The language accepted by the turing machine is REL( Recursively Enumerable Language)
L = { 0n 1n |n >= 1}
Solution:
W = {01,0011,000111,……}
4
∆ 0 0 1 1 ∆ ∆ ∆ ∆ ∆ ∆
Execution Procedure:
∆0 0 1 1 ∆
∆0011∆
∆X011∆
BX011B
BX0Y1B
BX0Y1B
BX0Y1B
BXXY1B
BXXY1B
BXXYYB
BXXYYB
BXXYYB
BXXYYB
BXXYYB
5
Define Tuples
qs-start state
Q={qs,qo,q1,q2,q3,h}
F={h}
∑={0,1}
Γ={0,1,X,Y,B}
δ : Transition table :
state 0 1 X Y B
Qs - - - (q0,B,R)
q0 (q1,X,R) (q3,y,R)
q3 (q3,y,R) (h,B,S)
|- (q2, B X 0 1 1 B)
6
|- ( q2, B X 0 1 1 B)
|- (q3, B X 0 Y 1 B)
|- (q3, B X 0 Y 1 B )
|- ( q1, B X 0 Y 1 B)
|- ( q2, B X X Y 1 B)
|- (q2, B X X Y 1 B)
|- (q3, B X X Y Y B)
|- (q3, B X X Y Y B )
|- ( q1,B X X Y Y B)
|- (q4, B X X Y Y B)
|- ( q4, B X x Y Y B )
|- ( q5, B X X Y Y B )
Δ a b b a Δ Δ Δ ……..
∆ a b a ∆ ∆ ∆ ∆ ∆ ………..
Δ a b b Δ Δ Δ ……
7
Define Tuples
qs-start state
Q={qs,q0,q1,q2,q3,q4,q5,h }
F={h }
∑={a,b}
Γ={ a,b,∆}
δ : Transition table :
state a b Δ
qs - - (q0, Δ,R)
q3 (q3,a,L) (q3,b,L) -
8
Instantaneous Description:
String: aba
Ⱶ (q1, ∆ ∆ b a ∆)
Ⱶ(q1, ∆ ∆ b a ∆)
Ⱶ(q1, ∆ ∆ b a ∆)
Ⱶ(q2, ∆ ∆ b a∆)
Ⱶ(q3, ∆ ∆ b∆∆)
Ⱶ(q3, ∆ ∆ b∆∆)
Ⱶ(q0, ∆ ∆ b∆∆)
Ⱶ(q4, ∆ ∆ ∆∆∆)
Ⱶ(q5, ∆ ∆ ∆∆∆)
Ⱶ(h, ∆ ∆ ∆∆∆)
Accepted
9
4. L={x={a,b}* | x ends with an abb}
R.E=(a+b)*abb
Input Tape:
∆ a b a ∆ ∆ ∆ ∆ ∆ …….
Output Tape:
∆ a b a ∆ a b a ∆ …….
∆aba∆∆∆∆
∆aba∆∆∆∆
∆Aba∆∆∆∆
∆Aba∆∆∆∆
∆Aba∆∆∆∆
∆Aba∆∆∆∆
∆Aba∆a∆∆
∆Aba∆a∆∆
∆Aba∆a∆∆
∆Aba∆a∆∆
∆Aba∆a∆∆
∆ABa∆a∆∆
∆ABa∆a∆∆
∆ABa∆∆a∆∆
∆ABa∆a∆∆∆
10
∆ABa∆ab∆∆
∆ABa∆ab∆∆
∆ABa∆ab∆∆
∆ABa∆ab∆∆
∆ABa∆ab∆∆
∆ABA∆ab∆∆
∆ABA∆ab∆∆
∆ABA∆ab∆∆
∆ABA∆ab∆∆
∆ABA∆aba∆
∆ABA∆aba∆
∆ABA∆aba∆
∆ABA∆aba∆
∆ABA∆aba∆
∆ABA∆aba∆
∆ABa∆aba∆
∆Aba∆aba∆
∆aba∆aba∆
∆Aba∆a∆∆
∆ABa∆ab∆
∆ABA∆aba
∆aba∆aba
11
Define Tuples
qs-start state
Q={qs,q0,q1,q2,q3,q4,q5,q6,q7,q8,h}}
F={h }
∑={a,b}
Γ={ a,b,A.B,∆}
δ : Transition table :
States a b A B Δ
q0 - - - - (q1, Δ,R)
12
q1 (q2,A,R) (q3,B,R) - - (q8, Δ,L)
q8 - - (q8,a,L) (q8,b,L) -
13
|-(q8, ΔAB a Δ a b a ) |-(q8, ΔAb a Δ a b a )
|-(q8, Δ ab a Δ a b a ) |-(h, Δ ab a Δ a b a )
Even:
∆ 1 1 1 1 ∆ ∆ ∆ ∆ …….
Odd:
∆ 1 1 1 ∆ ∆ ∆ ∆ ∆ …….
Define Tuple:
(Q, Γ, ∑, δ, q0, B, F)
where –
Q- { q0,q1,q2,q3,q4,h}
Γ – {∆,1}
∑ - {1}
∆ – blank symbol
q0 – Start state
h – Final state
14
δ:
STATES 1 ∆
q0 - (q1,∆,R)
q1 (q1,1,R) (q2,∆,L)
q2 (q3,∆,L) (h,∆,S)
q3 - (q4,∆,R)
q4 - (h,1,S)
Input: ababba
∆ a b a b B a ∆ ∆ ∆ ∆ ∆ …..
∆ a b ∆ b b a ∆ ∆ ∆ ∆ ∆ ∆ …..
∆ a b b b a ∆ ∆ ∆ ∆ ∆ ∆ ……
Execution Logic:
Δ bab Δ ΔΔaΔΔ
Δ bab Δ ΔΔbΔΔ
ΔΔabΔ a b Δ Δ - HALT
ΔΔabΔ
ΔΔabΔ
ΔΔabΔ
15
Define Tuple:
(Q, Γ, ∑, δ, q0, B, F)
where –
Q- { q0,q1,q2,q3,q4,h}
Γ – {∆,1}
∑ - {1}
∆ – blank symbol
q0 – Start state
h – Final state
16
Transition Table: δ
STATES a b ∆
|- (q2, Δ Δ b a Δ )
|- (q2, Δ Δ b a Δ )
|- (q2, Δ Δ b a Δ )
|- (q3, Δ Δ b a Δ )
|- (q4, Δ Δ b Δ Δ )
|- (q5, Δ Δ a Δ Δ )
|- (q6, Δ b a Δ Δ ) - HALT
∆ a b b a b b ∆ ∆ ∆ ∆ ∆ …..
17
∆ a b b a b b ∆ ∆ ∆ ∆ ∆ …..
∆ a b b A B B ∆ ∆ ∆ ∆ ∆ …..
18
Define Tuple:
(Q, Γ, ∑, δ, q0, B, F)
where –
Q- { q0,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10}
Γ – {∆,a,b,A,B}
∑ - {1}
∆ – blank symbol
q0 – Start state
Transition Table: δ
STATES a b A B ∆
q0 - - - - (q1,∆,R)
q3 (q4,A,L) (q4,B,L) - - -
q6 (q7,A,R) (q8,B,R) - - -
19
9. Construct a turing machine for subtraction
0, m≤n
Case 1: m>n
Input tape:
Δ 0 0 0 1 0 0 Δ …..
Output Tape:
Δ Δ Δ Δ 0 Δ Δ Δ …..
Case 2: m<=n
Input tape:
Δ 0 0 1 0 0 0 Δ Δ …..
Output Tape:
Δ Δ Δ Δ Δ Δ Δ Δ …..
20
3. Variations of Turing Machine
FC
……… ……
FC
21
4. Multi-tape Multi-head Turing Machine:
The multi-tape Turing machine has multiple tapes and multiple heads
Each tape controlled by separate head
Multi-Tape Multi-head Turing machine can be simulated by standard Turing
22
An offline Turing machine can simulate any turing machine A by adding one more tape
than Turing machine A. The reason for using an extra tape is that the offline Turing machine
makes a copy of its own input into the extra tape and it then simulates Turing machine A as if
the extra tape were A’s input.
A UTM is a specified Turing machine that can simulate the behavior of any TM.
3 directions
e - encoding function
Tu=e(T1).e(Z)
23
Encoding:
∆-0
a -00
b -000
h-0
q0 - 00
q1 - 000
q2– 0000
For directions
S-0
L-00
R -000
Transition Function
δ(q0,∆)=(q1,∆,R)
00101000101000
δ(q1,b)=(q1,b,R)
0001000100010001000
δ(q1,a)=(q2,b,L)
24
000100100001000100
δ(q1,∆)=(q2, ∆,L)
000101000010100
δ(q2,b)=(q2,b,L)
00001000100001000100
δ(q2,∆)=(h,∆,S)
00001001010
e(T1)=
001010001010001100010001000100010001100010010000100010011000101000010100
11000010001000010001001100001001010
Z=bba
e(Z)=0001000100
Tu=e(T1).e(Z)
=1100101000101000110001000100010001000110001001000010001001100010100001
010011000010001000010001001100001001010.11000100010011
For any input string Tu will halt if and only if T halts on input Z.
25
SCHOOL OF COMPUTING
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Recursive Language:
Theorem1:
Every recursive language is Recursively enumerable.
If T is TM recognizing L(RL) then we can get a TM thataccepts the language Lby modifying T so that
when the output is 0 it does not enter the reject state but enters into an infinite loop.
Theorem 2:
Every REL is not recursive.
Theorem 3:
The complement of a RL is recursive (or) If L is recursive, so is L ′.
Let L be a Recursive Language and M be the TM that halts on all inputs and accepts L.
Construct M1 for L′.
M accepts and halts for Yes, then M′ rejects and halts.
M rejects and halts for N, then M′ accepts and halts.
Theorem 4:
i. Union of two recursive languages is recursive.
ii. Union of two recursively enumerable languages is REL.
iii. Intersection of two recursively enumerable languages is REL.
Construct M:
It first simulates M1,If Yes than M accepts.
If M1 rejects, then M simulates M2 and accepts if and only if M2 accepts.
i.e) M accepts L1 U L2.
Let L1 and L2 be the Recursive Enumerable languages accepted by TMs M1 and M2.
M simultaneously simulates M1 and M2.
If either accepts, then M accepts. i.e. M accepts L1 U L2.
Let L1 and L2 be the Recursive Enumerable languages accepted by TMs M1 and M2.
M halts if both M1 and M2 halts.
M will never halt if either M1 or M2 enter into infinite loop.
( i.e.) M accepts L1 L2.
Theorem 5:
If a Language L and its complement L′ are both Recursively Enumerable, then L and hence L′ are
recursive.
Instance of a problem
- It is a particular string x , so that when the string is provided as input to the TM, the
answer is “YES “ or “NO”
- For more complicated problems to be solved by a TM, instances may need to be encoded
over the input alphabet of the machine.
TM Yes-instance of P
i/p string x
No-instance of P
Theorem 1 :
The language NSA is not recursively enumerable.
Proof by contradiction:
Assume NSA is REL.
Then, let Lbea NSAand Tbea TM accepting L.
Then, L(T) = NSA and w ε L(T)
But w ε L(T), since is not of the form e(T).
Then w ε NSA, is notpossible, which implies our assumption is false. Hence,
NSA is not REL.
Theorem 2:
The language SA is Recursively Enumerable but not Recursive
Proof:
W.K.T:
1. If L isrecursive,thenL’is also recursive.
2. Every RL is also REL.
3. NSA is not REL.
-If SA is recursive, then NSA is also recursive, then NSA is also REL.
-But by 3, NSA is not REL.
-Then, NSA is not recursive and not REL.
-Let T simulate processing w=e(T).
-T halts if w=e(T) & T loops forever if w ε e(T).
-To conclude, SA is REL but not Recursive.
Definition 5: Reducing One Decision Problem to Another
Suppose P1 and P2 are decision problems. We say P1 is reducible to P2 (P1 ≤ P2) if there is an
algorithm that finds, for an arbitrary instance I of P1, an instance F (I ) of P2, such that for every I , the
answers for the two instances are the same, or I is a yes-instance of P1 if and only if F (I ) is a yes-
instance of P2.
If L1 and L2 are languages over alphabets ∑1 and ∑2, respectively, we say L1 is reducible to L2 (L1 ≤L2)
if there isa Turing-computablefunctionf:∑1→∑2*,suchthat for every x∈∗1, x∈ L1if and only if f (x)
∈ L2 .
Theorem 3:
Show that the Accepts problem is unsolvable.
T1 accepts y. Letting T1 = T and y = e(T ) gives us such a pair, and F (T ) = (T , e(T )) can be obtained
algorithmicallyfromT;therefore, Fisa reductionfromSelf-Acceptingto Accepts.
Theorem 4:
Show that Halting Problem is unsolvable.
Let y to be x. TM T1 is defined such that for every x, T accepts x if and only if T1 halts on x. A
reformulation of this statement is: (i) if T accepts x, then T1 halts on x, and (ii) if T doesn’t accept
x, then T1 doesn’t halt on x.
Definition 7 :
An instance of Post’s correspondence problem (PCP) is a set {(α1, β1), (α2, β2), . . . , (αn, βn)} of pairs,
where n ≥ 1 and the αi’s and βi’s are all non-null strings over an alphabet .
Given an instanceof this type, does there exist a positive integer k and a sequence of integers i1, i2, ...,ik
with each ij satisfying 1 ≤ ij ≤ n, satisfying
αi1αi2 ...αik = βi1βi2 ...βik
The PCP decisionproblem is a combinatorialproblem involving pairs ofstrings not related to Turing
machines. The figure below shows a sample instance. Each of the five rectangles in the Figure is called
adomino, andassumethatthere is an unlimitedsupplyofeachofthefive.
10 01 100
The question is whether it is possible to make a horizontal line of one or more dominoes with
duplicates allowed, so that the string obtained by reading across the top halves matches the one
obtained by reading across the bottom.
Solution:
Definition 8:
An instance of the modified Post correspondence problem (MPCP) looks exactly like an instance of PCP,
but now the sequence of integers is required to start with 1. The question can be formulated this
way:
Does there exist a positive integer k and a sequence i2, i3, ... , ik such that Instances of PCP and MPCPare
calledcorrespondencesystemsandmodifiedcorrespondencesystems,respectively.
For an instance of either type, if it is a yes-instance we will say that there is a match for the instance,
or that the sequence of subscripts is a match, or that the string formed bythe αij ’s represents a match.
3. Accepts two or more : Given a TM T , does L(T ) have at least two elements?
5. MPCP ≤ PCP.
6. Accepts ≤ MPCP.
Halting Problem:
In the theory of computability, the problem of halting is the question of deciding, from an arbitrary
computer program description and an input, whether the program will finish running or continue to
run indefinitely. In 1936, Alan Turing proved that there could not be a general algorithm for all
possible program-input pairs to solve the halting problem.
Given a program and an input to the program, determine if the program will eventually halt when it
is given that input.
Loop
Program
Input
Halt
Proof by Contradiction
1. Assume the statement is true.
2. Solve the problem.
3. Check if there is a contradiction.
Proof:
1. Assume it is possible to solve the halting problem.
Assume it is possible to construct a machine H that solves the halting problem.
H receives 2 inputs:
P- program
I – input to the program P
H machine gives an answer ‘yes’ if program P halts on input I.
If it goes into an infinite loop, H gives answer no.
P Yes
H
I No
If we give the program of machine X to itself along with the given set of inputs which has already
been generated.
If the output of the machine H inside X is ‘Yes’, it means X will loop which contradicts the result
of machine H.
“Yes means it will halt but it didn’t”
In the same way, if the result of machine H is ‘No’, it means X will halt which again contradicts the
result of H.
“No means it will not halt but it halted”
This machine H does exist and the halting problem is unsolved.
Rice Theorem
Rice theorem states that any non-trivial semantic property of a language which is recognized by a
Turing machine is undecidable. A property, P, is the language of all Turing machines that satisfy
that property.
Definition 9
If P is a non-trivial property, and the language holding the property, Lp , is recognized by Turing
machine M, then Lp = {<M> | L(M) ∈ P} is undecidable.
Properties
Property of languages, P, is simply a set of languages. If any language belongs to P (L ∈ P),
it is said that L satisfies the property P.
A property is called to be trivial if either it is not satisfied by any recursively enumerable
languages, or if it is satisfied by all recursively enumerable languages.
A non-trivial property is satisfied by some recursively enumerable languages and are not
satisfied by others. Formally speaking, in a non-trivial property, where L ∈ P, both the
following properties hold:
o Property 1 − There exists Turing Machines, M1 and M2 that recognize the same
language, i.e. either ( <M1>, <M2> ∈ L ) or ( <M1>,<M2> ∉ L )
o Property 2 − There exists Turing Machines M1 and M2, where M1 recognizes the
language while M2 does not, i.e. <M1> ∈ L and <M2> ∉ L
Proof:
Suppose, a property P is non-trivial and φ ∈ P.
Since, P is non-trivial, at least one language satisfies P, i.e., L(M0) ∈ P , ∋ Turing Machine M0.
Let, w be an input in a particular instant and N is a Turing Machine which follows −
On input x
Run M on w
If M does not accept (or doesn't halt), then do not accept x (or do not halt)
If M accepts w then run M0 on x. If M0 accepts x, then accept x.
A function that maps an instance ATM = {<M,w>| M accepts input w} to a N such that
If M accepts w and N accepts the same language as M0, Then L(M) = L(M0) ∈ p
If M does not accept w and N accepts φ, Then L(N) = φ ∉ p
Since ATM is undecidable and it can be reduced to Lp, Lp is also undecidable.