Theory of Automata
Theory of Automata
BCSC0011: THEORY OF
AUTOMATA & FORMAL
LANGUAGES
3
Module II (Part 1)
Context Free Grammar and Context Free Languages:
Introduction, Derivation Trees
Ambiguity in Grammar, Ambiguous to Unambiguous CFG
Simplification of CFGs
Normal Forms for CFGs - CNF and GNF
Pumping lemma for CFLs
Equivalence of PDA and CFG
4
Formal Definition
Context Free
Grammar All productions in P are
of the form
G = (V, Σ, P, S)
Finite Set
A
of
Variables /
Non Variable
Terminals
Start
Finite Set of variable String of
terminal variables and
symbols terminals
Set of
Productions
rules
Language
generated by
Example of Context-Free Grammar this
grammar?
S aSb |
P {S aSb, S }
productions
G V , , S, P
7
What languages are generated by the
CFG which have the following
productions?
S aSa | bSb | λ
S aS | bS | a
S aSb | aabbb
{ai bj ck | i = j + k}
{ai bj ck | j = i + k}
S XY X aXb | λ Y bYc | λ
11
Derivation Trees
a A b b B
This is the parse tree of
a A b b B many different, but
equivalent, derivations
λ of aabbbb.
λ
14
Consider G whose productions
are
S aabbaa
S aAS | a, A SbA | SS |ba.
Show that
17
Let us consider this grammar : E E+E | id
We can create 2 parse tree from this grammar to obtain the
string id+id+id
+
+
+ +
18
Consider the grammar G with production rules −
19
If G is the grammar S SbS | a, show that G is
ambiguous.
20
▪ Ambiguity can be There are some inherently
removed by rewriting ambiguous languages like
the grammar such that L = {anbncm} ∪ {anbmcm}
there is only one
derivation or parse
tree possible for a
string of the language
which the grammar
E.g.
represents.
Ambiguous Grammar
S SbS | a
Can be rewritten as
S abS | a
21
Simplification
of CFGs
22
CFG simplification & restriction useful
theoretically & pragmatically.
□ Simpler grammars are easier to understand.
□ Simpler grammars can lead to faster parsing.
□ Restricted forms useful for some parsing
algorithms.
□ Restricted forms can give you more knowledge
about derivations.
23
Context Free Grammar can be
simplified / reduced by:
production elimination
Unit production elimination
Useless production elimination
24
Elimination of Null Productions
If λ L, we can remove all productions of the
type
Aλ
If λ L,
we obtain a new G by adding to V the new
variable S0, making S0 the start variable, and
adding to P the productions : S0 S | λ
let G′ = (V, T, S, P ) be a context-free grammar
for 25
Step 1: Construction of S aS | AB
the set W of all Aλ|a
nullable variables
Bλ|b
W1={A ∈ V | A → λ is a
D b | aAbB
production in P}
Wi+1= Wi ∪{B ∈ V | a
production B → α with W1={A, B}
α ∈ W i* } W2={A, B, S}
At some point Wi+1= Wi W3={A, B, S}
= W2 26
S aS | AB
Step 2: Construction of
P′ Aλ|a Bλ
|b
All prodns whose RHS
doesn’t have any D b | aAbB
Aa
nullable variables are Bb
included in P′
Db
If A X1 X2 … Xk is in P
S aS | AB
A 1include
then 2 k where
Xi if X i W S aS | a | AB | A | B
i
or X i if X i W D aAbB
Pr ovided A 1 2 k D aAbB | abB | aAb | 27
S aS | AB
Are we done?
Aλ|a Bλ
Does λ L? |b
S W, so λ L D b | aAbB
Aa
So, 1 more variable &
Bb
2 more prodns
Db
S0 S |
S aS | AB
S aS | a | AB | A | B
Total 14
D aAbB
prodns
D aAbB | abB | aAb | 28
Eliminate
The CFG will have
Null total 40 prodns!!
Productions Ca
S ABCBCDA A CD | C | D
A CD B Cb | b
B Cb D bD | b
Phase 1 −
Derivation of an
equivalent
grammar, G′, from
the CFG, G, such
that each variable
derives some 35
Construction of V′:
We define Vi′ ⊆ V by recursion
V1′={A ∈ V | there exists a production A → α
where α ∈ Σ*}
Vi+1′=Vi′ ∪ {B ∈ V| there exists some production B
→β
with β ∈ (Σ ∪ Vi′)*}
At some point Vk′=Vk+1′. Then we get V′=Vk′
Construction of P′ 36
Let G contain following productions
S →AB A →a B →b B →C E→c
Construction of V″:
We define Vi″ ⊆ V by recursion
V1″={S}
Vi+1″=Vi″ ∪ {X ∈ V′| there exists some production A →α
with A ∈ Vi″ & α contains the symbol X}
At some point Vk″=Vk+1″. Then we get V″=Vk″
38
Construction of P″
We had G as S →AB, A →a, B →b, B →C, and E
→c
Then we got G′ as S →AB, A →a, B →b and E
→ c 2: Find G′′ such that every variable in G′
Phase
appears in a sentential form.
V1″ = {S}
V2″ = {S, A, B} S →AB
V3″ = {S, A, B} A →a, B →b
= V 2″
Here we get V″={S,A,B}.
So P″= S →AB, A →a, B →b
39
Find reduced grammars equivalent to the following
1) S →aAa, A →Sb | bCC | DaA, C →abb | DD, E →ac, D
→aDa
2) S →AB | CA, B →BC| AB, A →a, C →aB | b
What would
Answers: happen if you
reverse the order:
1) S →aAa, A →Sb|bCC, C →abb i.e., test
2) S →CA, A →a, C → b reachability
before
generating?
40
1. Give context-free grammars that generate the following
languages
• { ai bj ck | i, j, k ≥ 0 and i = j or i = k }
G = (V, Σ, P, S) with set of variables V = {S, B, X, Y, Z},
where S is the start variable; set of terminals Σ = {a, b,
c}; and rules in P are
S XY | Z
X aXb |
Y cY |
Z aZc | B
B bB |
41
S XY | Z
2. Now find the Reduced Grammar
X aXb |
Y cY |
Z aZc | B
B bB |
42
The language A of strings of properly balanced
left and right brackets: every left bracket can
be paired with a unique subsequent right
bracket, and every right bracket can be paired
with a unique preceding left bracket. Moreover,
the string between any such pair has the same
property.
For example,
G = (V, [ ] [set
Σ, R, S) with [ [ of
] [variables
]][]]∈ A.{S}, where S is
V=
the start variable; set of terminals Σ = {[, ]}; and rules
S → ε | SS | [S]
43
Normal Forms
₪ Chomsky Normal Form (CNF)
44
Advantages of CNF
Ambiguity is removed
Y N a Y | N bY Y a | b | c | N a Y | N bY
Na a
Nb b Total 22
S1 XS2 production
S2 NbX s
52
S3 NbX
Greibach Normal
Form
▪ A context-free grammar is S → aB | bA
in Greibach Normal Form
if the right-hand side of A → a | aS | bAA
each rule has one terminal B → b | bS | aBB
followed by zero or more
non-terminals:
AaX
where
a
X V* 53
Advantages:
● Every derivation of a string s contains |s| rule
applications. So strings can be quickly parsed.
● Greibach normal form grammars can easily be
converted to pushdown automata with no - transitions.
This is useful because such PDAs are guaranteed to
Shalt.
→ aB | bA S aB # of derivation
A → a | aS | bAA aaBB steps = |aababb|
B → b | bS | aBB aabSB =6
aabaBB
aababB
Derive the string
aababb
aababb
54
Conversion to Greibach Normal Form using
substitution
S → aBc S → aBC S → Bc S → bC
B→b B→b B→b Bb
C→c C→c
S AB S aAB | bBB |
A aA | bB | b bB
Bb A aA | bB | b S AB
Bb A BS | b
S → aABSB | aA B SA | a
S → aabSb | aa
B→b
A→a
55
To convert a CFG in to a GNF:
56
But first we need to understand the concept of
Left – Recursive Rules
This is a left-recursive rule: Ak → Akα | β
The alternative (β) allows a derivation to “break out of”
the recursion.
Every left-recursive rule must have an alternative (β)
that allows breaking out of the recursion.
The rule Ak → Akα | β generates the language: βαn, n≥0
To see this, look at a few derivations:
Language generated by Ak → Akα | β ?
57
Eliminate left-recursion
• We want to eliminate the left-recursion in
this rule: Ak → Akα | β
• And we know the rule produces βαn
• But the language can also be generated
using these rules:
Ak →β|βZ
Z → α Z| α
• With those two types of rules we have
eliminated the left recursion.
58
Multiple left-recursive alternatives
Ak may have multiple alternatives that are left-
recursive:
Ak → Akα1 | Akα2 | … | Akαr
And Ak may have multiple other alternatives:
Ak → β1 | β2 | … | βs
So Ak may generate following strings where
β1(α1,…,αr)*
β2(α1,…,αr)*
…
59
*
₪ The prodns with no left
recursion are same as
in P The set of Ak prodns in P1 are
₪ Let Zk be a new variable Ak → β1 | β2 | … | βs
₪ Let G1 = (V {Zk}), Σ, Ak → β1 Zk | β2 Zk | … | βs
P1, S) where P1 is Zk
defined as
The set of Z prodns in P1 are
A k → A kα 1 | A kα 2 | … | A kα r Z k → α1 Z k | α2 Z k | … | αr
A k → β1 | β2 | … | βs Zk
60
Z k → α1 | α2 | … | αr
Loop
Convert to GNF
Convert to GNF A1 A 2 A 3
A1 A 2 A 3 A2 A 3 A 3
A2 A 3 A 3 A3 A 2 A 3 | a
A3 a A 3 | a There is a LOOP
Using substitution, we A2 A3 A2
can easily convert to
GNF Which gives rise to
Left recursion when we
A1 a A 3 A3 A 3 | a A3 A3 do substitution
A2 a A 3 A 3 | a A 3 A2 A 2 A3 A 3 | a A3
61
Conversion to GNF
64
Convert to GNF
Step 4: Check for Loop
S AB A3 A1 A2 // i > j
A BS | b
A3 A2 A3 A2 // i > j
B SA | a
A3 A3 A1 A3 A2 | b A 3 A2
Step 1 & 2: Convert to
CNF Step 5: Eliminate Left
Already in CNF Recursion
A1 A2 A3
Step 3: Rename the A2 A3 A1 | b
variables & Rewrite the
prodns A3 A3 A1 A3 A2 | b A 3 A2 | a
S – A1 A – A2 B–
65
A3
A3 A3 A1 A3 A2 | b A3 A2 |
a A prodns are:
A3 b A 3 A2 | a
A 3 b A 3 A 2 Z3 | a Z 3
’s & 1 β1
Z prodns are:
ββ’s
2
?? Z3 A 1 A 3 A 2 Z3
Z3 A 1 A 3 A 2
66
Substitute in A2 A3 A1 | b
5. Step 6: Do
substitution in A 2 b A 3 A 2 A 1 | b A 3 A 2 Z3 A 1 | a
prodns not in GNF Z3 A 1 | a A 1 | b
A1 A2 A3 Substitute in A1 A2 A3
A2 A3 A1 | b A 1 b A 3 A 2 A 1 A 3 | b A 3 A 2 Z3 A 1 A 3
| a Z3 A1 A3 | a A 1 A3 | b A 3
A3 b A 3 A2 | a
Substitute in Z3 A1 A3 A2 Z3
A 3 b A 3 A 2 Z3 | a Z 3
Z3 b A 3 A 2 A 1 A 3 A 3 A 2 Z3 | b A 3 A 2
Z3 A 1 A 3 A 2 Z3
Z3 A 1 A 3 A 3 A 2 Z3 | a Z 3 A 1 A 3 A 3 A 2
Z3
All A3Aprod
1 A3 A
n
s 2are in GNF Z3 | a A 1 A 3 A 3 A 2 Z3 | b A 3 A 3 A 2 Z3
A 3 b A 3 A 2 | b A 3 A 2 Z3 | Substitute in Z3 A1 A3 A2
a Z3 | a
Z3 b A 3 A 2 A 1 A 3 A 3 A 2 | b A 3 A 2 Z3
67
A A A A |aZ A A A A |aA
5. Step 6: Do
All A3 prodns are in GNF
substitution in
prodns not in GNF A 3 b A 3 A 2 | b A 3 A 2 Z3 | a Z 3 | a
68
A1 A2 A3
A2 A3 A1 | b Substitute in A2 A3 A1 | b
A3 b A 3 A2 | a So we get 5 A2 prodns
A 3 b A 3 A 2 Z3 | a Z 3 Substitute in A1 A2 A3
Z3 A 1 A 3 A 2 Z3 So we get 5 A1 prodns
Z3 A 1 A 3 A 2 Substitute in Z3 A1 A3 A2 Z3 | A1
Total 24 A3 A2
prodns So we get 5 + 5 Z3 prodns
Equivalence of
Push Down Automata &
Context Free Languages
69
CFG to PDA
The PDA simulates the left-sentential forms that G
uses to generate any string w
Let G = (V, , P, S)
Construct PDA N that accepts L(G) by empty stack
N = ({q}, , V , , q, S)
Transitions are defined as
For each variable A
(q, ε, A) = {(q, ) | A is a prodn in G}
For each terminal a
(q, a, a) = {(q, ε)} 70
Find a PDA equivalent to the grammar S aSbb | a
Generate the string aabb and also simulate the PDA
for
N it
= ({q}, , V , , q, S)
(q, aabb, S)
Where = {a, b}, V = {S} ├ (q, aabb, aSbb)
& is as following ├ (q, abb, Sbb)
├ (q, abb, abb)
1. (q, ε, S) = {(q, aSbb), ├ (q, bb, bb)
(q, a)} ├ (q, b, b)
2. (q, a, a) = {(q, ε)} ├ (q, ε, ε)
E I | E * E | E + E | (E)
I a | b | Ia| Ib | I0 | I1
Let N be a PDA that accepts the language
generated by the given CFG
N = ({q}, , V , , q, E)
= {a, b, 0, 1, (, ), *, +}
V = {E, I) 72
1. (q, ε, E) = {(q, I), (q, E*E), (q, E+E), (q, (E))}
2. (q, ε, I) = {(q, a), (q, b), (q, Ia), (q, Ib), (q, I0),
(q, I1)}
3. (q, a, a) = {(q, ε)} E I | E * E | E + E |
4. (q, b, b) = {(q, ε)} (E)
I a | b | Ia| Ib | I0 |
5. (q, 0, 0) = {(q, ε)} I1
6. (q, 1, 1) = {(q, ε)}
7. (q, (, () = {(q, ε)}
8. (q, ), )) = {(q, ε)}
9. (q, *, *) = {(q, ε)} 73
True or False?
₪ L = ∅ is a language.
₪ All finite languages are regular.
₪ An infinite language can have an infinite complement.
₪ All infinite languages have infinite complements.
₪ The union of infinitely many regular languages is regular.
₪ You can always convert a PDA into an NFA for the same
language.
₪ If a CFG G is in CNF, then G is not ambiguous.
₪ Some PDAs need infinite-length descriptions, as the rules
(productions) can be an arbitrary subset of V × (V ∪ Σ)* .
74
₪ The context-free languages are closed under union.
₪
₪ Show a context-free grammar that generates L = {w ∈ {a,
b}* : the first, middle & last characters of w are
identical}.
S aAa | bBb
A CAC | a 1, 010, 111,
B CBC | b
00100
Ca|b
00000000, or, for
other examples,
₪ Consider the following grammar G:
any string of even
S→1S1|T
length.
T→1X1|X
X→0X0|1
What are the first 4 strings in the lexicographic
enumeration of L(G)? 75
Give an example of a string w ∈ {0, 1}+ such that |w| > 7
PDA to CFG
We construct grammar G as follows.
G = (V, , P, S)
V = {S} {[q, z, q] | q, q Q, z }
The productions in P are induced by the
moves of PDA as follows
Rule 1:
The S productions are given by
S [q0, Z0, q] q Q
76
Rule 2:
Each move erasing a pushdown symbol given
by
(q , ε) (q, a, z) induces the prodn
[q, z, q] a
Rule 3:
Each move not erasing a pushdown symbol
given by (q1, z1z2 … zm) (q, a, z) induces
many prodns of the following form where q, q2,
…, qm can be any state in Q
77
[q, z, q] a [q , z , q ] [q , z , q ] … [q ,
Construct a context-free grammar G which accepts
L(N), where
N = ({qo, q1}, {a, b}, {Zo, Z}, , qo, Zo, φ)
is given by
1. (qo, b, Zo) = {(qo, ZZo)}
2. (qo, ε, Zo) = {(qo, ε)}
3. (qo, b, Z) = {(qo, ZZ)}
4. (qo, a, Z) = {(q1, Z)}
5. (q1, b, Z) = {(q1, ε)}
6. (q1, a, Z0) = {(qo, Zo)}78
Let G = {V, {a, b}, P, S} Total no. of
variables?
V = {S} {q, z, q] | q, q Q, z }
V = {S} {[qo, Zo, qo], [qo, Zo, q1],
[qo, Z , q0], [qo, Z , q1],
[q1, Zo, qo], [q1 , Zo, q1],
[q1, Z , q0], [q1, Z , q1],
80
Rule 2: (q, a, z) = (q , ε) induces [q, z, q] a
[q, z, q] a [q1, z1, q2] [q2, z2, q3] … [qm, zm, q]
83
(qo, a, Z) = {(q1, Z)}
[q0, Z, ] a [q1, Z, ]
13.[q0, Z, q0 ] a [q1, Z, q0 ]
14.[q0, Z, q1 ] a [q1, Z, q1 ]
n g u age
La ted?
c e p
ac
[qo Z0 q0] → a [q0 X qo] [q0 Z0 q0] | a [q0 X q1] [q1 Z0 q0] | a [q0 X q2]
[q2 Z0 q0]
[q0 Z0 q1] → a [q0 X q0] [q0 Z0 q1] | a [q0 X q1] [q1 Z0 q1] | a [q0 X q2] [q2
Z0 q1]
[q0 Z0 q2] → a [q0 X q0] [q0 Z0 q2] | a [q0 X q1] [q1 Z0 q2] | a [q0 X q2] [q2
Z0 q2]
85
[q0 X q0] → a [q0 X q0] [q0 X q0] | a [q0 X q1] [q1 X q0] | a [q0 X q2] [q2 X
q0]
[q0 Z0 q0] → [q1 Z0 q0]
[q0 Z0 q1] → [q1 Z0 q1]
[q0 Z0 q2] → [q1 Z0 q2]
[q1 X q1] → b
[q1 Z0 q2] → ε
86
Find the equivalent CFG
We have already
designed PDA for L =
{anbmcn | n ≥ 1} • S [q0, Z, q0] | [q0, Z, q1] | [q0, Z,
δ(q0, a, Z) = (q0, q2]
aZ) • [q0, Z, q0] a [q0, a, q0][q0, Z, q0]
• [q0, Z, q0] a [q0, a, q1][q1, Z, q0]
δ(q0, a, a) = (q0, • [q0, Z, q0] a [q0, a, q2][q2, Z, q0]
aa) • [q0, Z, q1] a [q0, a, q0][q0, Z, q1]
δ(q0, b, a) = (q1, • [q0, Z, q1] a [q0, a, q1][q1, Z, q1]
a) • [q0, Z, q1] a [q0, a, q2][q2, Z, q1]
• [q0, Z, q2] a [q0, a, q0][q0, Z, q2]
δ(q1, b, a) = (q1, • [q0, Z, q2] a [q0, a, q1][q1, Z, q2]
a) • [q0, Z, q2] a [q0, a, q2][q2, Z, q2]
δ(q1, c, a) = (q2, • …
87
ε)
88
||Shri Hari ||
Module II (Part 2)
Turing machines (TM):
₪ Basic Model, Definition and Representation,
₪ Variants of Turing Machine and their equivalence,
₪ TM for Computing Integer Functions,
₪ Universal TM,
₪ Church’s Thesis,
₪ Recursive and Recursively Enumerable
Languages,
₪ Halting Problem,
₪ Introduction to Computational Complexity 89
Foundation
Oct 6, 2023 90
Turing Machine
M = (Q, Σ, Γ, δ, q0, #, H)
Q - The finite set of states of the finite control
Σ - The finite set of input symbols
Τ – The set of tape symbols; Σ Τ
- Q x Τ Q x Τ x {L, R}
q0
a a b a # # …
Oct 6, 2023 94
Building a TM for a Simple Language
Is L
CF a
L?
L = { anbncn | n 0 }
Examples:
Member of L: aaabbbccc
aaabbbccc
Non-Member of L: aaabbcccc
aaabbcccc
Oct 6, 2023 95
Oct 6, 2023
YY, R
ZZ, R
96
Oct 6, 2023
aa, R
bb, R
Demonstration YY, R
q1 bY,R
ZZ, R
aX,R
q2
q0
XX, R cZ,L
__, R
YY, R q3 ZZ, L
bb, L
qac
__, R YY, L
q4 aa, L
YY, R
ZZ, R
Xa b
Y Zc _ _ ...
97
Construct a TM machine for accepting the palindromes of even
length.
a b a b b 98 a b a # …
Oct 6, 2023
Let’s now simulate the working of the TM
a b a b b a b a # …
X b a b b a b a # …
X b a b b a b # # …
X X a b b a b # # …
X X a b b a # # # …
99
Oct 6, 2023
Let’s now simulate the working of the TM
X X X b b a # # # …
X X X b b # # # # …
X X X X b # # # # …
X X X X # # # # # …
100
Oct 6, 2023
TRANSITIO
N GRAPH
101
Oct 6, 2023
Transition Table
a b X #
q0 q1,X,R q4,X,R H,#,L
q1 q1,a,R q1,b,R q2,#,L
q2 q3,#,L
q3 q3,a,L q3,b,L q0,X,R
q4 q4,a,R q4,b,R q5,#,L
q5 q6,#,L
q6 q6,a,L q6,b,L q0,X,R
102
Oct 6, 2023
Construct a TM machine for accepting the palindromes of odd
length.
103
Oct 6, 2023
What change
should we do
in the TM
that accepts
all
palindromes
of even
length?
104
Oct 6, 2023
M = ({q0,q1,q2,qf}, {0,1}, {0,1,#}, δ, q0,
#, {qf})
106
Oct 6, 2023
Describe a TM that decides the language
L = {w ∈ {0, 1}* | |w|0 = |w|1}
107
Oct 6, 2023
CONFIGURATION OF A TM
As a Turing machine computes, what things
can change?
δ(q7, 0) = (q9, 1, R)
δ(q9, 1) = (q8, 0, L)
109
Oct 6, 2023
Describe the operation of T when started in the
Let T = (K, Σ, , S, h) be a
TM where
configuration (q0, #aaaaa)
₪ K = {q0, q1, q2}
₪ Σ = {a, #} (q0, #aaaaa)
₪ S = q0
(q1, #aaaaa)
₪ is as follows
(q2, #aaa#a)
q (q, )
(q0, #aaa#a)
q0 a (q1, L)
q0 # (q0, #) (q1, #aaa#a)
q1 a (q2, #) (q2, #a#a#a)
q1 # (h, #)
(q0, #a#a#a)
q2 a (q2, a)
q2 # (q0, L) (q1, #a#a#a)
( h, #a#a#a)
110
Oct 6, 2023
A language is recursively enumerable if some Turing machine
accepts it.
If w L, then M halts in a final state
If w L, then M halts in some non final state or loops forever
112
Oct 6, 2023
Variants of TM
Oct 6, 2023
What is the behaviour when started on the
Given the TM tape
… # 1 0 1 0 # …
1. (0, 1, 1, 0, R)
2. (0, 0, 0, 1, R)
3. (1, 1, 1, 1, R)
… # 1 0 1 # …
4. (1, #, 1, 2, L)
5. (2, 1, 1, 2, L)
6. (2, 0, 0, 2, L)
7. (2, #, 1, 0, R)
115
Oct 6, 2023
3. Multi Tape Turing Machine
TM with k tapes and k
heads.
Each tape has its own head
for reading and writing.
Initially the input appears
on tape 1, and the others
start out blank.
δ: Q × k → Q × k × {L,R}k
δ(qi, a1, …, ak) = (qj, b1, …,
bk, L, R, …, L)
Oct 6, 2023 116
4. Nondeterministic Turing Machine
At any point in a computation the machine may proceed
according to several possibilities.
δ: Q × → P(Q × × {L,R})
δ(qi, a) = {(q1, b, R), (q2, c, L), …, (qm, d, R)}
We can simulate any nondeterministic TM N with a
deterministic TM D.
The idea behind the simulation is to have D try all possible
branches of N's nondeterministic computation.
If D ever finds the accept state on one of these branches,
D accepts. Otherwise, D's simulation will not terminate.
119
Oct 6, 2023
Church - Turing
Thesis
Alan Turing
Alonzo Church (1912-1954)
(1903-1995)
Some common interpretations of the thesis
are
₪ Every algorithmically computable function
is TM-computable.
₪ A function is computable iff it can be
solved by a Turing Machine
Thesis not Theorem: because we cannot prove this.. with a
counter example we could disprove it (but this has not been
125
Octdone
6, 2023yet).
M is an algorithm for achieving some desired result if
1. M is set out in terms of a finite number of exact instructions
(each instruction being expressed by means of a finite number
of symbols);
Let n =
3
0 0 0 0 0 0 0 0 # # #
127
Oct 6, 2023
What is the
significance of this 2n
L {0 | n 0}
transition?
128
Oct 6, 2023
TM for Computable
Functions
What is computable is Turing-computable;
TMs are algorithms (Kleene 1953).
TMs can be used to manipulate numerical
data
Each no. is represented in Unary. E.g.
<4> = 1111
A function f: N N is computable if there
exists a TM, M, st for every integer n, M halts
129
on <n> & the number of 1s is equal to f(n)
Oct 6, 2023
Design a TM to compute f(n) = n + 1
Consider f(3).
Write its Computation History
(S, 1, 1, S, R)
S111
(S, #, 1, H,R)
⊢ 1S11
⊢ 11S1
⊢ 111S#
⊢ 1111H#
130
Oct 6, 2023
Design a TM to compute One’s
Complement when i/p is given in
binary
M = (Q, Σ, , , S, #, Consider f(100).
H) Write its Computation History
Q = {S, H}
S100
Σ = {0, 1}
⊢ 0S00
Τ = {0, 1, #}
⊢ 01S0
= {(S, 0, 1, S, R),
⊢ 011S#
(S, 1, 0, S, R),
⊢ 011H#
131
(S, #, #, H, R)}
Oct 6, 2023
Design a TM to compute n 2 if n 2
f (n)
1 if n 2
132
Oct 6, 2023
Given two positive integers x & y, design a TM to compute x +
y
E.g. f(2, 3) = 5
(q0, 1) = (q0, 1,
… # 1 1 # 1 1 1 # … R)
(q0, #) = (q1,
x y 1,R)
(q1, 1) = (q1, 1,
… # 1 1 1 1 1 # # …
R)
(q1, #) = (q2, #,
x+ L)
y 133 (q2, 1) = (H, #,
Oct 6, 2023 L)
Design a TM that copies strings of 1 . i.e.
f(n) = 2n *
… # 1 1 1 # … q 0 w Hww
… # 1 1 1 1 1 1 # …
Test for
n=ε
n = 11
1→1,R 1 → 1, R
1→□,R □→□,R
q0 q1 q2
□
□
,R
→
→
1
□
,R
→
,R
□
□→□,L □→1,L
qf q5 q4 q3
1 → 1, L 1 → 1, L
136
Oct 6, 2023
Theorem: If L is recursive language then its
complement, Lc , is also recursive
language.
Proof:
Suppose that M decides L.
Design a new machine M′ that behaves just like M,
but:
• If M accepts, M′ rejects.
• If M rejects, M′ accepts.
Formally, can do this by
Software or Hardware
y ? algorithms
h
W (that we have studied so far)
Is an “unprogrammable”
140 piece of hardware
Oct 6, 2023
A limitation of Turing Machines:
141
Oct 6, 2023
We will see TMs are also software
Oct 6, 2023
Universal TM
Input : < Description of some TM M, w >
144
Trial solution
Just run the program with the given input. If the program
stops, we know the program stops.
But if the program doesn't stop in a reasonable amount of
time, we cannot conclude that it won't stop.
Maybe we didn't wait long enough
145
Oct 6, 2023
Computation
al Complexity
150
Oct 6, 2023
The class P is important
because
P roughly corresponds to the class of problems that are
realistically solvable on a computer.
When a problem is in P,
1 5
3 PATH?
s t
HAMPATH?
4
2 6
157
Oct 6, 2023
Does this graph have a Hamiltonian
path?
s t
158
Oct 6, 2023
No fast
algorithm for
HAMPATH
Bu
t If I somehow
discovered such a path
(perhaps using the
exponential time
Polynomial algorithm), I could
Verifiability easily convince you of
its existence, simply by
presenting it.
160
Oct 6, 2023
NP is the class of languages
that have polynomial time
verifiers.
The class NP is important because it contains
many problems of practical interest.
161
Oct 6, 2023
Some problems may not be
polynomially verifiable!!!
Most researchers
P Researchers also have
believe that the tried proving that the
two classes are not NP classes are unequal,
equal because people but that would entail
have invested showing that no fast
enormous effort to OR algorithm exists to
find polynomial time replace brute-force
algorithms for certain search. Doing so is
problems in NP, P = NP presently beyond
without success. scientific reach.
1. B is in NP, and
For example,
o "is there a Hamiltonian cycle with length less than k" is
NP-complete: it is easy to determine if a proposed certificate has
length less than k.
173
What we have seen so far …
The following are all equivalent:
L is a regular language.
There is a DFA D such that ℒ (D)
= L.
There is a NFA N such that ℒ (N)
= L.
There is a RE, R such that ℒ (R)
= L.
Unique states
176
We can write w = xyz
First Second
occurrence occurrence
s1 s2 .... si s i 1 .... sj s j1 .... sk
q q
x s1 s i y s i 1 s j z s j1 s k
177
w s1s 2 s k
s1 s2 sk
......
q ......
178
In DFA: w x y z y contains only first occurrence of
q
...
s j s i 1
s1 ...s i s j... sk
q 1 ...
x z
179
What is y here?
S3 S4 S5 When we read the
5th symbol we are in
state 5 – so all new
states visited till
now.
Therefore,
|xy| ≤ n
What is x here?
S1 S2
180
Observation 1: length |xy| n, the no. of states in DFA
Observation 2: length |y| ≥ 1, since there is atleast one
transition in the loop
We do not care about
y
...
the form of string z
Unique States
sj s i 1
s1 ... s i
q Since, in xy no
state is repeated
x 181
In general: the The string xz is
accepted if we do not
string y follow the loop
xyiz for i ≥ 0 The string xyyz is
is accepted ...
accepted if we follow
i.e the loop twice
xyiz L The string xyyyz is
for i ≥ 0 s j s i 1 accepted if we follow
the loop thrice
s1 ... s i s j1... sk
q ...
x z 182
Let L be a regular language. Then there exists a con-
stant n such that, for every string w L with |w| n,
there exist strings
x, y, z * such that
• w = xyz with |xy| n and
•|y| 1, (but x and z may be ε)
• Then, for all i 0, xyiz L.
184
Prove that L = {akbk | k ≥ 0} is not regular.
Step 1:
Suppose L is regular & is Step 3:
accepted by a FA having n Let i = 2
states. w’ = xyi z where
Step 2: o w’ = ap a2q arbk
Let w = akbk where k > n o w’ = ap +2q + r bk
w L o w’ = ak+qbk
We can write w = xyz where o Since q 0, w’ has
o |xy| n more a’s than b’s
o |y| > 0 Hence w’ L
Since k > n, we have This is a contradiction, so L
o x = ap is not Regular
o y = aq
o z = a rb k
185
o where p + q + r = k & q
Prove that L = {w | na(w) < nb(w)} is not regular.
186
Prove that L = {ak | k is prime} is not regular.
Step 1:
Suppose L is regular & is Step 3:
accepted by a FA having n Let i = p + 1
states. w’ = xyi z where
Step 2: o |w’| = |xyz| + p|y|
Let w = ap where p is prime o |w’| = p + pm = p (1 +
& m)
p>n o w’ = ap(1+m)
w L Hence w’ L since p (1+m)
We can write w = xyz is not prime
where This is a contradiction, so L
o |xy| n is not Regular
o |y| > 0
o Let y = am where m 0
187
Prove that L is not regular Step 3:
Let i = 2
2
L a k | k 1 & k is an integer w’ = xyi z where
Which o |w’| = |xyz| + |y|
language is o |w’| = n2 + |y|
it? o |w’| > n2 {since |y| 0} --- (1)
Step 1:
Suppose L is regular & is
accepted by a FA having n o Also since |w’| = n2 + |y|
states. o |w’| n2 + n {since |y| n}
Step 2: o |w’| < n2 + n + n + 1 = (n + 1)2
Let w a n2
, |w| = n2 > n ---(2)
w L n2 < |w’| < (n + 1)2
We can write w = xyz |xy2z| strictly lies b/w n2 & (n+1)2 &
where so is not a perfect square.
o |xy| n This is a contradiction, so L is not
Regular 188
o |y| > 0
Prove that L is not regular Step 3:
L = { w | w {a, b}*, w Let i = 0
= wR 1:} w’ = x z where
Step
o w’ = ak-qbak
Suppose L is regular & is
o w’ w’ R
accepted by a FA having n
So, xy0z L
states.
This is a contradiction, so L is not
Step 2:
Let w = akbak where k > Regular
n
w L
We can write w = xyz
where
o |xy| n
o |y| > 0
Since k > n, we have
189
o q
Hope you enjoyed this
course
Plz send your feedback
(both positive ones & ones
suggesting improvement) at
charul@gla.ac.in
All the Best
Always follow the right path
Oct 6, 2023 190
Pumping Lemma
for
Context Free Languages
191
₪ Let L be an infinite
CFL. There exists
some integer n such
that for all w in L with
|w| ≥ n,
₪ w = uvxyz with |vxy| ≤
n and |vy| > 0 such
that
₪ uvixyiz ε L for all i = 0,
1, 2, 3 .... 192
Consider the first non-terminal that is
repeated, when traversing the path from the
leaf to the
S root.
Second occurrence of non-terminal A
A
First occurrence of non-terminal A
A
195
Derivation tree for
w = bbcaa *
for some
grammar
S uAz
*
A vAy
z *
A x
u
So, we can y
write
Oct 6, w
2023= uvxyz
v x 196
Pump Zero Times
A A
A
x
u v x y z u y
198
Oct 6, 2023
Pump Twice, Thrice …
A A
A A
u v A y z
u v x y z
v x y
199
Oct 6, 2023
Observe
We have the production rules which lead us
to the following derivations:
k1 k2 k1 k 2 1
v a y a
m m m m
a ...... a b ...... b a ...... a b ...... b
u vx y 202
2
Leti
= m k1 k 2 m m m
a .......... ...... a b ...... b a ...... a b ...... b
u v2 x y2 z
a m k1 k2 b m a mb m uv 2 xy 2 z L k1 k 2 1
m m m m m m m m
a b a b or a b a b
or
m m m m
a b a b 204
Case 2: v is within the first
am
y is within the first
b m
k2
v ak 1 y b k1 k 2 1
m m m m
a ...... a b ...... b a ...... a b ...... b
u v x y z
205
2
Leti
=
m k1 m k2 m m
a .......... .. a b .......... .. b a ...... a b ...... b
u v 2 x
y 2 z
m k1 m k 2 m m 2 2
a b a b uv xy z L
2 2
However, from Pumping Lemma: uv xy z L
206
Contradiction!!!
Similar analysis if vxy overlaps
m m m m
a b a b
or
m m m m
a b a b
207
Case 3: v overlaps the first am & bm
m k2 k1 m k3 m m
a ...... a b ... b a ... a b ......... b a ...... a b ...... b
u v 2 x y 2
z
m k 2 k1 m k3 m m 2 2
a b a b a b uv xy z L
2 2
uv
However, from Pumping Lemma: xy z L
209
Contradiction!!!
Case 4: v is within the first am
y overlaps the first am & bm
m m m m
a ...... a b ...... b a ...... a b ...... b
uv x y z
nor
m m m m
a b a b
nor
a mb m a mb m
211
Prove that L = {ai bi ci | i ≥ 0} is not a CFL.
Proof:
Let L be a CFL. Since L is infinite, the pumping
lemma can be applied. Let n be the constant of the
lemma.
Let w L & w = ak bk ck for some k > n.
o Then we can write w = uvxyz
o Such that |vy| > 0
o And |vxy| ≤ n
Since |vxy| ≤ n, therefore it cannot contain all 3
212
symbols – a, b & c
Case I: Suppose vxy contains one of the symbol in Σ.
o Suppose vxy contains at least one a.
o Then uv2xy2z will have more a’s than b’s or c’s.
o Therefore uv2xy2z does not belong to L.
o This is a contradiction of the pumping lemma.
Case II: Suppose vxy contains two of the symbol in Σ.
o Suppose vxy contains at least one a & at least one b.
o Then uv2xy2z will have more a’s & b’s than c’s.
o Therefore uv2xy2z does not belong to L.
o This is a contradiction of the pumping lemma.
In all the cases we get a contradiction.
213
Therefore our assumption that L is context free is wrong.
Oct 6, 2023
Prove that the following languages are not CFLs.
1) L = {0i1i0i1i | i >0}
2)
214
Prove that is not a CFL.
Proof:
Let L be a CFL. Since L is infinite, the pumping
lemma can be applied. Let n be the constant of the
lemma.
Let w L and w = 02^k for some k > n.
o Then we can write w = uvxyz
o Such that |vy| > 0
o And |vxy| ≤ n
Let v = 0p and y = 0q st p+q>0
215
|w| = 2k
Consider the string uvixyiz
|uvixyiz| = |uvxyz| + |vi-1| + |yi-1|
| uvixyiz |= 2k + (i-1) (p + q)
Let i = 2k+1 +1
| uvixyiz |= 2k + (2k+1 +1 -1) (p + q)
= 2k (1 + 2 (p + q))
So the length of uvixyiz is not a power of 2
Therefore, uvixyiz L
This is a contradiction of Pumping Lemma & so L is not
216
Context free