Tafl Notes 2
Tafl Notes 2
FINITE AUTOMATA
Finite automata are basic modules of a computer system, which addresses simple real time problems. Figure
1.1 shows conceptual view of finite automata. It has an input tape, read head and finite controller. Input
tape is divided into finite cells and each cell stores one input symbols. Read head is used to read the input
symbol from the input tape. The finite controller used to record the next state of finite automat.
a. union (𝖴),
b. intersection (∩), and
c. difference (−)
d. complementation defined as
S1 U S2 = {x | x ϵ S1 or x ϵ S2}
S1 Ո S2 = {x | x ϵ S1 and x ϵ S2}
S1 - S2 = {x | x ϵ S1 and x ∉ S2}
The complement of a set S, denoted by Ṡ consists of all elements not in S. To make this meaningful,
we need to know what the universal set U of all possible elements is. If U is specified, then
Ṡ={x | x ϵ U, x ∉ S}
The set with no elements, called the empty set or the null set, is denoted by ɛ. From the definition of a set,
it is obvious that
Graphs:
A graph is a construct consisting of two finite sets, the set V = {υ1, υ2,…, υn} of vertices and the set E =
{e1, e2,…, em} of edges. Each edge is a pair of vertices from V, for instance,
ei=(v, vj).
Languages:
A finite, nonempty set of symbols are called the alphabet and is represented with symbol ∑. From the
individual symbols we construct strings, which are finite sequences of symbols from the alphabet. For
example, if the alphabet ∑ = {0}, then ∑* to denote the set of strings obtained by concatenating zero or
more symbols from ∑.
Kleen closure(*closure):
*closure = {∑0U∑1U∑2U∑3 ........ }
∑0={ ɛ} ∑1={0, 1} ∑2={00, 01, 10, 11}
∑3={000, 001, 010, 011, 100, 101, 110, 111}
Therefore ∑*={ ɛ , 0, 1, 00, 01, 10, 11, 000, 001, 010, 011, 100, 101,……}
+
Closure on ∑ = {0, 1}
∑+= {∑1U∑2U∑3…}
∑*=∑+ + ɛ
Therefore ∑+=∑*- ɛ
Formal Languages and Automata Theory: IT is a mathematical model used to design computer programs
and sequential logic circuits. It is state machines which captures and makes transition to accept or reject.
Finite automata has input tape, read head and finite controller Input tape: It is divided into finite cell and
each cell is used to store one input symbol. Read head: Read head is used to read input symbol from input
tape. Finite Controller: Finite controller records, the transition to next state. The default value of finite
controller is initial state q0. States are represented with circles; they are of three types,
States are connected by lines called transition. It has two ends, one end is called initiation point and the
other end is called termination point (>).
Initiation Termination
Figure 1.4 shows a transition diagram to map cities and their roads.
Q={q0,q1,q2} ∑ ={a, b, c}
δ:
δ (q0, a)=q1 δ (q0,b)=q0 δ (q0,c)=q2
δ (q1,a)=q1 δ (q1,b)=q2 δ (q1,c)=q0
δ (q2,a)=q2 δ (q2,b)=q1 δ (q2,c)=q2
Note: Initial State is q0 and final state is q2
Q\∑ a b c
q0 q1 q0 q2
q1 q1 q2 q0
*q2 q2 q1 q2
If outgoing transitions are not dependent on number of input symbols, then such automata are called as
Nondeterministic Finite Automata.
There is no outgoing transition from initial state, for a symbol ‘1 ‘and similarly there is no outgoing
transition from final state for ‘0’.
{}=ɛ
{a}=a
(q0, ɛ)= q0 //q0 is reading empty string, string is empty therefore, transition to same state q0.
(q0, wa)
Where ‘w’ is the multi-symbol string and ‘a’ is a symbol, hence we separate the transition function for
strings and symbols as given below:
δ( (q0, w), a)
δ:
δ (A0, 0)=A1 δ (A0, 1)=A2 δ(A0, 2)=A0
δ (A1, 0)=A2 δ (A1, 1)=A0 δ(A1, 2)=A1
Example 1.5: Find out whether the given string is accepted by the DFA. Assume the string as abab.
From the given string ∑ = {a,b} . Note that always string is traversed by left side to right side,
= δ (q2, b) =q3 2
(q0, aba)= δ( (q0, ab), a) from equation 2 (q0, ab)= q3
= δ(q3, a)=q1
(q0, abab)= δ( (q0, aba), b)
= δ( (q1, b)= q0
Given string is accepted by a DFA.
(q0, ɛ)=q0;
In general we can write L={w |w is a string consist of even number of a’s and even number of b’s}
Design of DFA :
Example 1.8: Design a DFA which accept all the strings ends with symbol a over the alphabet ∑={a, b}.
Termination state for symbol ‘a’ is q1 and for ‘b’ it is q0. Outgoing transition from q1 on
symbol ‘a’ is loop, and for ‘b’ it is to q0 .
Figyre 1.16 :DFA which accept all the strings ends with a.
∑={0, 1}.
ii. Convert all states of NFA to DFA, that is from each state two outgoing transitions, one for
symbol ‘0’ another for ‘1’. From initial state only one outgoing transition on symbol ‘0’, one
Termination state for symbol ‘0’ is A1 and for ‘1’ it is A0. Outgoing transition from A1 on
symbol ‘0’ is loop. From A2 on symbol ‘0’ to A1 and on symboll ‘1’ to A0.
Figyre 1.20 :DFA which accept all the strings ends with 01.
∑={0, 1, 2}.
i. Draw the NFA for the smallest string 012 of the language.
The NFA for the string 012 is
ii. Convert all states of NFA to DFA, that is from each state three outgoing transitions, one for
symbol ‘0’ second for ‘1’ third from ‘2’. From initial state only one outgoing transition on
symbol ‘0’, two more outgoing transitions are required that is on ‘1’ and ‘2’. If the condition
Termination state for symbol ‘0’ is A1 and for ‘1’ and ‘2’ is A0. Outgoing transition from A1 on
symbol ‘0’ is loop and for’2’ is A 0. From A2 on symbol ‘0’ to A1 and on symbol ‘1’ to A0. From
A3 on symbol ‘0’ to A1 and on symbols ‘1’ and ‘2’ to A0.
Figure 1.24 :DFA which accept all the strings ends with 012.
∑={a, b}.
Figure 1.26 : DFA which accept all the string begin with a.
ii. Condition is begin, outgoing transitions from final state is loop.
∑={0, 1}.
ii. Convert all states of NFA to DFA, that is from each state two outgoing transitions, one for
symbol ‘0’ another for ‘1’. From initial and intermediate states only one outgoing transition,
one more outgoing transition is required. If the condition in the given problem is, begin with,
the outgoing transition from both the states to new state called dead state.
iii. Condition is begin, outgoing transitions from final state A2 on symbol ‘0’ and ‘1’ is loop.
δ:
δ (A0, 0)=A1 δ (A0, 1)=A0
δ (A1, 0)=A1 δ (A1, 1)=A2
δ (A2, 0)=A1 δ (A2, 1)=A0 δ
(A3, 0)=A3 δ (A3, 1)=A3
IS=A0; FS=A2
Example 1.12: Design a DFA which accept all the strings begins with 01 or ends with 01 or both over the
alphabet ∑={0, 1}.
We have already designed both the DFAs, joining of these DFAs, is the solution for the given problem.
Figure 1.29: DFA which accept all the string begin with 01.
Figure 1.30: DFA which accept all the string ends with 01.
Example 1.13: Design a DFA which accept all the binary strings divisible by 2.
δ:
δ (q0, 0)=q0 δ (q0, 1)=q1 ; δ (q1, 0)=q2 δ (q1, 1)=q3
δ (q2, 0)=q4 δ (q2, 1)=q0 ; δ (q3, 0)=q1 δ (q3, 1)=q2
δ (q4, 0)=q3 δ (q4, 1)=q4 ;
IS=q0; FS=q0
Example 1.15: Design a DFA which accept string over x, y every block of length 3 contains at least one x.
Figure 1.35: NFA which is accepting three symbol string in which one symbol is x.
3. The position of A1 and A5 is same as A0 and A4. In between A0 and A4 the symbol is ‘x’,
inbetwwen A1 and A5 is also x and so on.
Figure 1.36: DFA which accept string over x, y if every block of length 3 contains at least one x.
δ:
δ (A0, x)= A4 δ (A0, y)= A1 ; δ (A1, x)=A5 δ (A1, y)=A2
δ (A2, x)=A6 δ (A2, y)=A3; δ (A3, x)=A3 δ (A3, y)=A3
δ (A4, x)=A5 δ (A3, y)=A5; δ (A5, x)=A6 δ (A5, y)=A6 ;
Example 1.16: Design a DFA which accept even number of r and even number of s over the symbol ∑={r,
s}.
i. Draw separate DFA, one accept even number of r’s and another one accept even number of
s’s.
ii. Join two DFA, we get four new states AB, AC, BC, BD. Designed DFA accept even number of r
and even number of s, out of four states AB satisfied this condition, hence this state is a final
state.
Example 1.17: Design a DFA which accept all the strings described by the language L={N0(w)≥ 1 and
N1(w)=2}
i. Exchange I symbol of first string with the last symbol of the first string, the string obtained is
the second string of the language.
ii. Draw NFA for two smallest strings, for both NFA initial and final states are same.
Figure 1.40: DFA which accept all the strings described by the language L={N 0(w)≥ 1 and N1(w)=2}.
IS=A0; FS={A3}
In DFA the outgoing transitions are defined in advance, where as in NFA the outgoing transitions are not
defined in advance. In DFA, the string is either accepted or not accepted, where as in NFA both are
possible. The finite automata shown in the figure 1.41 is a NFA. There is no outgoing transition for the
symbol ‘a’ from the state q1.
Example 1.18: Design NFA which accept all strings, if second symbol from right side is 1 over the
alphabet ∑={0, 1}.
ii. Draw the intermediate state and transition for the symbols ‘0’ and ‘1’.
iii. Draw the initial state and transition for the symbol ‘1’.
Figure 1.45: Non-deterministic finite automata accepting two strings 10, 11.
iv. Designed NFA has to accept all the strings, if second symbol from right side is symbol ‘1’, to
do this, mark loop to initial state on symbols ‘0’ and ‘1’.
Figure 1.46: NFA which accepts all strings, if second symbol from right side is 1 over the alphabet ∑={0, 1}.
Figure 1.47: NFA which accept all strings, if second symbol from right side is 1 over the alphabet ∑={0, 1}.
We have already seen both NFA and DFA, both are same in the definition but they differ in transition
function. If number of states in NFA has two states q0 and q1. Here, q0 is initial state and q1 is final state.
The equivalent states in DFA’s are *q0], [q1] and [q0, q1]. Where q0 is initial state and (q1), (q0, q1) are the
final states. The pair (q0, q1) is final state because; one of its states is final state. They are represented as
shown in figure 1.48
Example 1.19: Convert the NFA shown in figure 1.49 into its equivalent DFA.
The conversion begins with the initial state of the NFA which is also an initial state of DFA. Conversion
generates number of new states, for every new state we are finding the outgoing transitions fordifferent
symbols.
Finite automat is used to search a text. There are two methods used to search text, they are:
To search a text “shine” and “hire” the NFA shown in figure 1.51 is used
To search a text “shine” and “hire” the DFA shown in figure 1.52 is used
Example 1.20: Design a finite automata, that reads string made up of letters HONDA, and recognize
those string that contain the word HOD as a substring.
i. Find out the alphabet from the string HONDA. The input symbols are ∑={H, O, N, D, A}
ii. Draw the NFA for the substring HOD.
Figure 1.54: DFA accept all the strings ends with HOD over the symbols {H, O, N, D, A}
Till now whatever automata we studied were characterized by a transition on the symbols. But there are
some finite automata having transition for empty string also, such NFAs are called NFA- ɛ transition.
ɛ-closure (q0): states which are closed / related to state q0 on empty transition(ɛ) is called ɛ-closure (q0).
Example 1.21: Find out ɛ-closure of all the states of ɛ-NFA shown in figure 1.55.
Example 1.22: Convert the ɛ-NFA shown in figure 1.57 into NFA
Note: state ɛ-closure consist of final state of ɛ-NFA, the particular state is a final state in NFA.
= ɛ-closure(δ({q0q1}, 0).
=ɛ-closure(q0 U q2).
=ɛ-closure(q0) U ɛ-closure(q2).
={q0q1} U {q0q1q2}
={q0q1q2}
= ɛ-closure(δ({q0q1}, 1).
=ɛ-closure(q0) U ɛ-closure(φ).
={q0q1}
= ɛ-closure(δ(q1, 0)).
= ɛ-closure(q2).
={q0q1q2}
= ɛ-closure(δ(q1, 1)).
= ɛ-closure(φ).
=φ
= ɛ-closure(δ({q0q1q2}, 0).
=ɛ-closure(q0 U q2 U φ).
={q0q1q2}
= ɛ-closure(δ({q0q1q2}, 1).
=ɛ-closure(q0 U φ U φ).
= {q0q1}
= ɛ-closure(δ({q0q1}, 0).
=ɛ-closure(φ U q1).
=ɛ-closure(φ) U ɛ-closure(q1).
= φ U q1
=q1
= ɛ-closure(δ({q0q1}, 1).
=ɛ-closure(q0U φ).
=ɛ-closure(q0) U ɛ-closure(φ).
={q0q1}U φ
=[q0q1]
= ɛ-closure(δ({q0q1}, 2).
=ɛ-closure(φ U q2).
=ɛ-closure(q2).
=[q1q2 ]
= ɛ-closure(δ(q1, 0).
= ɛ-closure(q1).
= q1
= ɛ-closure(δ(q1, 1).
= ɛ-closure(φ).
= ɛ-closure(δ(q1, 2)
= ɛ-closure(q2)
= [q1 q2]
= ɛ-closure(φ U q1)
= ɛ-closure(q1)
= q1
= ɛ-closure(q0 U φ)
= ɛ-closure(q0)
= [q0, q1]
= ɛ-closure(q2)
= [q1 q2]
= ɛ-closure(δ(q1, 0 ) U δ (q2, 0 ))
= ɛ-closure(q1 U φ).
= ɛ-closure(q1)
= q1
= ɛ-closure(δ(q1, 1 )U δ (q2, 1 ))
= ɛ-closure(φ U φ).
= ɛ-closure(φ)
=φ
= ɛ-closure(δ(q1, 2 )U δ (q2, 2 ))
= ɛ-closure(q2 U φ).
= ɛ-closure(q2)
( φ, (0,1,2))= φ
Mark the final states, any pair which has a final state of ɛ -NFA the particular pair is afinal state in DFA.
REGULAR EXPRESSIONS
Language represented in the form of expression (symbols separated by operator) is called regularexpression.
∑= {a, b, 0, 1}
Before studying regular expression in detail, we will discuss some fundamental rules of the regular
expression.
SHREEDEVIPRAMOD,CSE,BRCE 30
RE=(00+11) (0+1)* +(0+1)*(00+11).
xxvii. Find the regular expression of the language L={anbm :n≥0, m≥0}
L={Ɛ+a+aa..}{Ɛ+b+bb…}
RE=a*b*
xxviii. Find the regular expression of the language L={anb2m+2 : n≥0, m≥0}
RE=(aa)*(bb)*bb.
xxix. Find the regular expression of the language L={a2nbm : n≥4, m≤3}
RE=(aaaa)*(Ɛ+b+bb+bbb)
xxx. Find the regular expression of the language L={anbm/n+m is even}
RE=(aa)*(bb)*+a(aa)*b(bb)*
Three basic regular expressions are used in conversion (0+1), 01 and 0*.
But in regular expression each symbol is having its own finite automata.
Definition for the finite automat which accept the symbol ‘1’ is
Definition for the finite automat which accept the symbol ‘0’ or ‘1’ is
Q={Q0 U Q1 U E U F} ={ A, B, C, D, E. F);
∑= {0, 1}
δ(A, 0)= B; δ (C, 1)= D; δ(E, Ɛ)={A, C}; δ(D, Ɛ)=F: δ(D, Ɛ)=F
IS=E; FS=F
For the symbol ‘0’ the finite automata is shown in figure 2.2
SHREEDEVIPRAMOD,CSE,BRCE 31
Definition for the finite automat which accept the symbol ‘0’ is
Definition for the finite automat which accept the symbol ‘1’ is
We are joining these two finite automata with disturbing the regular expression 0+1 as shown in figure
2.4. To join these two finite automata we need more extra states, one for the initial state and second is for
the final state, states A and B no more initial states and B and D are no more final states. All these states
will now become intermediate states as shown in figure 2.4.
Definition for the finite automat which accept the symbol ‘0’ or ‘1’ is
Q={Q0 U Q1 U E U F} ={ A, B, C, D, E. F);
∑={0, 1}
δ(A, 0)= B; δ (C, 1)= D; δ(E, Ɛ)={A, C}; δ(D, Ɛ)=F: δ(D, Ɛ)=F
IS=E; FS=F
For the regular expression 01 the finite automata is shown in figure 2.6.
SHREEDEVIPRAMOD,CSE,BRCE 32
Rewrite the expression 01 as 0Ɛ1.
We can join the two finite automata shown in figure2.5 and figure 2.6 without disturbing the regular
expression 01 is shown in figure 2.6.
∑={0, 1}
IS=A; FS=D
The designed finite automata accept all the strings of 0* ie., {Ɛ, 0, 00, 000…}
Q={Q0 U E U F} ={ A, B, E, F);
∑=0
SHREEDEVIPRAMOD,CSE,BRCE 33
δ
IS=E; FS=F
Example 2.1: Design a finite automat for the regular expression 0*1
Join finite automata of 0* and 1, which will be the solution for the regular expression 0*1
Example 2.2: Design a finite automat for the regular expression 0(0+1)*1
i. Draw the finite automata for the symbol ‘0’ and ‘1’
SHREEDEVIPRAMOD,CSE,BRCE 34
Figure 2.13: Finite automata for regular expression 1.
i. Recursive Method.
ii. State Elimination Method.
i. Recursive Method :
In this method it uses recursive method, to construct new regular expression.
For example the regular expression of DFA shown in figure 2.17 is
SHREEDEVIPRAMOD,CSE,BRCE 35
Figure 2.17: A typical Finite automata.
In order to obtain the value of regular expression , we use all the (transition) possible regular
expression of the DFA. The possible regular expression of the figure 2.17 is shown in the table2.1.
Equations to obtain the regular expression of all the transitions are as follows:
If state 2 and 3 are final states then the regular expression is written as:
Example 2.3: Find the regular expression of DFA shown in figure 2.17 by using recursive method.
Example 2.4: Find the regular expression of DFA shown in figure 2.18 by using recursive method.
SHREEDEVIPRAMOD,CSE,BRCE 36
State Elimination Method:
In state elimination method, we obtain regular expression, by eliminating all the intermediate states. After
eliminating the intermediate states, it is easy to find the regular expression for the remaining states (initial
and final).
SHREEDEVIPRAMOD,CSE,BRCE 37
Figure2.20: Finite automata
RE=01
The regular expression of the finite automata shown in figure 2.2 is:
RE=0a*1
From the above examples we come to know that eliminating state has one predecessor state A 0 and one
successor state A2. In complex finite automata the eliminating state may have more number of predecessors
and successors. One Such example is shown in the figure 2.23.
Before eliminating any state from the finite automata, we need to find out the path passing through the
eliminating state. The paths passing through the eliminating state are P1ESn, P1ES1, PnES1 and PnESn.
After eliminating state E, the automata is shown in the figure 2.24.
After eliminating all predecessors and successors, leaving initial state and final state, the remaining
machine looks shown in the figure 2.25.
SHREEDEVIPRAMOD,CSE,BRCE 38
Figure 2.25: Finite automata after eliminating all predecessors and successors.
The regular expression is, language accepted by the initial state of finite automata and Su*.
The language accepted by the initial state is {R*+Su*t+R*Su*t + su*tR*…}, this is like {x*+y*+xy + yx
…}
We can simplify it as (x+y)* similarly, we can simplify the language for the initial state as (R+Su*t)*.
Therefore, regular expression of finite automata is (R+Su*t)*Su*. Note that in some cases the same state is
initial as well as final state.
SHREEDEVIPRAMOD,CSE,BRCE 39
Figure 2.29: Four paths are passing through eliminating state A1
Example 2.5: Construct regular expression for the finite automat shown in figure 2.30.
No intermediate in the given finite automata, hence we can’t eliminate any of the state.
Therefore RE =(R+Su*t)*Su*
= (b+aa*b)*aa*
=b(Ɛ+aa*)*aa*
=b(aa*)*aa*
Example 2.6: Construct regular expression for the finite automat shown in figure 2.31 by using state
elimination method.
We can eliminate one intermediate state A1, the paths passing through the eliminating state are A0A1A0,
A0A1A2, A2A1A0 and A2A1A2.
SHREEDEVIPRAMOD,CSE,BRCE 40
Figure 2.32: Final finite Automata
RE=(R+Su*t)*Su*
= ((2+02*1)+(1+02*0)(0+12*0)*(2+02*0))*((1+02*0)(0+12*0)*.
Pumping lemma:
If more 0s, there will be more number of pumping. In such a case it is very difficult to count the number
of pumping. To count the number of pump easily we draw circles around the intermediate state so that we
can easily count the number of pumping to prove the language as non-regular.
SHREEDEVIPRAMOD,CSE,BRCE 41
Figure 2.35: Finite Automata pumped at intermediate state finite time.
To prove the language as non-regular, smallest string of language is divided into uvw by considering two
conditions.
i. V Ɛ.
ii. |UV|≤n where n is the smallest string of the language. To prove the language as a non-regular,
throughout the proof the value of n is constant.
Example 2.7: Prove that the language L={ :i≥1} is not regular.
0(00)i000000 ЄL
I=1
000000000 ЄL
I=2
00000000000 L
When i=2 the string obtained is not belongs to language, hence proved language is not regular.
Example 2.8: Prove that the language L={anbn+1 :n≥2} is not regular.
i=1
aaabbbb Є L
I=2
Aaabaabbbb L
When i=2 the string obtained is not belongs to language, hence proved the language is non-regular.
SHREEDEVIPRAMOD,CSE,BRCE 42
Closure Properties of Regular Expression:
Till now we have seen many regular expressions and their applications. These regular expressions are
bounded/ related by operators called closure properties of regular expression. The different operators used
to relate regular expressions are:
i. Boolean.
ii. Reversal.
iii. String Homomorphism and Inverse Homomorphism.
i. Boolean Operator:
They are of different types
a. Union b. Intersection c. Difference d. Compliment.
a. Union Operation.
To perform union operation it uses following steps.
This operation is applied on two machines defined as
M1=(Q1, ∑1, δ1, IS1, FS1)
M2=(Q2, ∑2, δ2, IS2, FS2)
Find the language L1 and L2 of the two machines.
Perform union operation on the two languages L1 and L2 to obtain language L3
Combine two machines to perform union operation.
Apply language L3 on combined machines to obtain machine for union operation.
In order to obtain the finite automata for the union operation, we need to join two finite automata. The
transition table for union operation is shown in table…
SHREEDEVIPRAMOD,CSE,BRCE 43
Equivalent diagram is shown in the figure 2.38
Figure 2.38: Finite Automata after combining figure 2.36 and 2.37.
The final automata for the union operation is shown in figure 2.42
SHREEDEVIPRAMOD,CSE,BRCE 44
Figure 2.42: Union of two finite automata of figure 2.36 and 2.37.
Figure 2.43: Intersection of two finite automata of figure 2.36 and 2.37.
Figure 2.44: Difference of two finite automata of figure 2.36 and 2.37.
Compliment:
SHREEDEVIPRAMOD,CSE,BRCE 45
Figure 2.46 : Equivalent DFA for the figure 2.45.
iii. Compliment DFA. Reverse all transitions and convert all final to non final and vice-versa.
State B and φ are not reachable from the initial State. Hence the two states are removed from the finite
automata.
RE=Ɛ
String Homomorphism:
SHREEDEVIPRAMOD,CSE,BRCE 46
Figure 2.51: Homomorphism.
Inverse Homomorphism:
If the homomorphism from an alphabet {0, 1, 2} to {a, b} is defined by h(0)=ab; h(1)=b; &
h(2)=aa, then:
i. What is h(2201).
ii. If L is language 1*02* what is h(L).
iii. If L is language(ab+baa)*bab then what is h-1(L)
i. h(2201)=aaaaabb
ii. h(L)=h(1*02*)=b*ab(aa)*
iii. (ab+baa)*bab=(0+12)*10
In Equivalence, two different DFA’s are given, we need to find out the two DFA’s are similar or not.
By filling table and Preshapat tree we can decide the two DFAs are similar | not similar.
Preshapat Tree: To find pair of nodes similar | not similar Preshapat tree is used. It consists of root
node, intermediate nodes and leaves nodes. The root node of the tree is pair of initial states of two
DFAs. Next we construct branches of tree depending on number of input symbols. If leaves node do
not have a cross in the difference table and same pairs are repeating, we stop constructing tree and
decide the root pair is similar or else dissimilar, any of leaves node has a cross in the difference table.
SHREEDEVIPRAMOD,CSE,BRCE 47
For example
Note that root of the tree is pair of initial states. Leaves nodes of the tree do not have cross in the
difference table and nodes are repeating, hence the two DFA’s are similar.
Example 2.11: Find out the two DFAs are similar or not.
SHREEDEVIPRAMOD,CSE,BRCE 48
Figure 2.58: Transition tree with table.
The leaves node AG has X in the table; hence two DFAs are dissimilar.
In case of minimization, the two states are minimized to one state if they are similar. For example state X
and Y are equal, they are replaced by one state as shown in figure 2.59
To decide the states are similar | not similar we use Shanprepat tree.
Shanprepat Tree: To find equivalence between pair of states Shanprepat tree is used. It consists of root
node, intermediate nodes and leaves nodes. The root node of the tree is pair of states used to find the similar
| not similar states. Next we construct branches of tree depending on number of input symbols. If leaves
node do not have a cross in the difference table and same pairs are repeating, we stop constructing tree and
decide the root pair is similar or else decide two states are dissimilar, any of leaves node has a cross in the
difference table.
Example 2.12: Using table filling method, minimize the DFA shown in figure 2.60 and draw the transition
diagram of the resulting DFA.
SHREEDEVIPRAMOD,CSE,BRCE 49
Example 2.13: Using table filling method, minimize the DFA shown in figure 2.63 and draw the
transition diagram of the resulting DFA.
From the given DFA we draw the dissimilar table as shown in figure. As compared to final state C all
states are different (non-final states), hence X is marked.
The leaves node decides the root node. Any leaves node has X in the table, the root node is marked as X
or else pair node repeats. Stop when the pair nodes start repeating, and decide the root node pair is similar.
SHREEDEVIPRAMOD,CSE,BRCE 50
Figure 2.65: Differnce table with Shanprepat Tree.
From the table, we come to know that A=E, B=H and D=F. the resulting DFA is shown in figure..
SHREEDEVIPRAMOD,CSE,BRCE 51
Example 2.14: Using table filling method, minimize the DFA shown in table and draw the transition
diagram of the resulting DFA.
The table shows the dissimilarity in the states of the given problem.
SHREEDEVIPRAMOD,CSE,BRCE 52
Figure 2.69: Differnce table with minimized DFA.
53
Module -III
Context-Free Grammars and Pushdown Automata (PDA)
1. Introduction to Rewrite Systems and Grammars
SHREEDEVIPRAMOD,CSE,BRCE 53
Simple-rewrite (G, S) will generate the strings in L(G). The symbol ⇒ to indicate steps in a
derivation.
Given: S aS ------ rule 1
S ------ rule 2
A derivation could begin with: S ⇒ aSb ⇒ aaSbb ⇒ aabb
When to Stop
Case 1: The working string no longer contains any nonterminal symbols (including, when it is
). In this case, we say that the working string is generated by the grammar.
Example: S ⇒ aSb ⇒ aaSbb ⇒ aabb
Case 2: There are nonterminal symbols in the working string but none of them appears on the
left-hand side of any rule in the grammar. In this case, we have a blocked or non-terminated
derivation but no generated string.
Given: S aSb ----- rule 1
S bTa ----- rule 2
S ----- rule 3
SHREEDEVIPRAMOD,CSE,BRCE 54
Recall Regular Grammar which has a left-hand side that is a single nonterminal and have a
right-hand side that is or a single terminal or a single terminal followed by a single
nonterminal.
X →Y
( NT) ( or T or T NT)
SHREEDEVIPRAMOD,CSE,BRCE 55
No restrictions on the form of the right hand sides. But require single non-terminal on left hand
side.
Example: S , S a, S T, S aSb, S aSbbT are allowed.
ST aSb, a aSb, a are not allowed.
The name for these grammars “Context Free” makes sense because using these rules the
decision to replace a nonterminal by some other sequence is made without looking at the
context in which the nonterminals occurs.
Definition Context-Free Grammar
A context-free grammar G is a quadruple, (V, , R, S), where:
• V is the rule alphabet, which contains nonterminals and terminals.
• (the set of terminals) is a subset of V,
• R (the set of rules) is a finite subset of (V - ) V*,
• S (the start symbol) is an element of V - .
Given a grammar G, define x ⇒G y to be the binary relation derives-in-one-step, defined so
that ∀ x,y V* (x ⇒G y iff x = A, y = and there exists a rule A is in RG )
Any sequence of the form w0 ⇒G w1 ⇒G w2 ⇒G . . . ⇒G wn is called a derivation in G. Let
⇒G* be the reflexive, transitive closure of ⇒G. We’ll call ⇒G* the derive relation.
A derivation will halt whenever no rules on the left hand side matches against working-string.
At every step, any rule that matches may be chosen.
S NP VP
NP the Nominal | a Nominal | Nominal | ProperNoun | NP PP
Nominal N | Adjs N
N cat | dogs | bear | girl | chocolate | rifle
ProperNoun Chris | Fluffy
Adjs Adj Adjs | Adj
Adj young | older | smart
VP V | V NP | VP PP
V like | likes | thinks | shots | smells
PP Prep NP
Prep with
If L has a property that every string in it has two regions & those regions must bear some
relationship to each other, then the two regions must be generated in tandem. Otherwise,
there is no way to enforce the necessary constraint.
SHREEDEVIPRAMOD,CSE,BRCE 58
Example 1: L = {anbncm : n, m ≥ 0} = L = {, ab, c, abc, abcc, aabbc, …….}
The cm portion of any string in L is completely independent of the anbn portion, so we should
generate the two portions separately and concatenate them together.
G = ({S, A, C, a, b, c}, {a, b, c}, R, S} where:
R = { S AC /* generate the two independent portions
A aAb | /* generate the anbn portion, from the outside in
C cC | } /* generate the cm portion
Example derivation in G for string abcc:
S ⇒ AC ⇒ aAbC ⇒ abC ⇒ abcC ⇒ abccC ⇒ abcc
Example 4: L = {anwwR bn: w {a, b}*} = {, ab, aaab, abbb, aabbab, aabbbbab, ........... }
The anbn is the inner portion and wwR is the outer portion of any string in L.
G = {{S, A, a, b}, {a, b}, R, S}, where:
R = {S aSb------------- rule 1
S A---------------- rule 2
A aAa ------------- rule 3
A bAb ------------ rule 4
A ---------------- rule 5 }.
Example derivation in G for string aabbab:
S ⇒ aSb ⇒ aAb ⇒ aaAab ⇒ aabAbab ⇒ aabbab
SHREEDEVIPRAMOD,CSE,BRCE 59
Example 5: Equal Numbers of a’s and b’s. = {w {a, b}*: #a(w) = #b(w)}.
L = {, ab, ba, abba, aabb, baba, bbaa, …….}
G = {{S, a, b}, {a, b}, R, S}, where:
R = { S aSb ---------- rule 1
S bSa ----------- rule 2
S SS ------------ rule 3
S -------------- rule 4 }.
Example derivation in G for string abba:
S ⇒ aSa ⇒ abSba ⇒ abba
Example 6
L = {aibj : 2i = 3j + 1} = {a2b1 , a5b3 , a8b5 ......... }
G = {{S, a, b}, {a, b}, R, S}, where:
aibj 2i = 3j + 1
2 1
ab 2*2= 3*1 + 1 = 4
5 3
ab 2*5= 3*3 + 1 = 10
8 5
ab 2*8= 3*5 + 1 = 16
R={ S aaaSbb | aab }
Example derivation in G for string aaaaabbb:
S ⇒ aaaSbb ⇒ aaaaabbb
SHREEDEVIPRAMOD,CSE,BRCE 60
Example: G = ({S, A, B, C, D, a, b}, {a, b}, R, S), where
R={ S AB | AC
A aAb |
B bA
C bCa
D AB }
1) a and b terminal symbols are productive
2) A is productive( because A aAb )
3) B is productive( because B bA )
4) S & D are productive(because S AB & D AB )
5) C is unproductive
On eliminating C from both LHS and RHS the rule set R obtained is
R = { S AB A aAb | B bA D AB }
Example
G = ({S, A, B, C, D, a, b}, {a, b}, R, S), where
R = {S AB
A aAb |
B bA
D AB }
S, A, B are reachable but D is not reachable, on eliminating D from both LHS and RHS the
rule set R is
R = { S AB
A aAb |
B bA }
SHREEDEVIPRAMOD,CSE,BRCE 61
5. Proving the Correctness of a Grammar
Given some language L and a grammar G, can we prove that G is correct (ie it generates
exactly the strings in L)
To do so, we need to prove two things:
1. Prove that G generates only strings in L.
2. Prove that G generates all the strings in L.
Algorithms used for generation and recognition must be systematic. The expansion order is
important for algorithms that work with CFG. To make it easier to describe such algorithms,
we define two useful families of derivations.
a. A leftmost derivation is one in which, at each step, the leftmost nonterminal in the
working string is chosen for expansion.
b. A rightmost derivation is one in which, at each step, the rightmost nonterminal in the
working string is chosen for expansion.
Example 1 : S → AB | aaB A → a | Aa B → b
Left-most derivation for string aab is S ⇒ AB ⇒ AaB ⇒ aaB ⇒ aab
Right-most derivation for string aab is S ⇒ AB ⇒ Ab ⇒ Aab ⇒ aab
SHREEDEVIPRAMOD,CSE,BRCE 62
Left-most Derivation for the string “the smart cat smells chocolate”
S ⇒ NP VP
⇒ the Nominal VP
⇒ the Adjs N VP
⇒ the Adj N VP
⇒ the smart N VP
⇒ the smart cat VP
⇒ the smart cat V NP
⇒ the smart cat smells NP
⇒ the smart cat smells Nominal
⇒ the smart cat smells N
⇒ the smart cat smells chocolate
Right-most Derivation for the string “the smart cat smells chocolate”
S ⇒ NP VP
⇒ NP V NP
⇒ NP V Nominal
⇒ NP V N
⇒ NP V chocolate
⇒ NP smells chocolate
⇒ the Nominal smells chocolate
⇒ the Adjs N smells chocolate
⇒ the Adjs cat smells chocolate
⇒ the Adj cat smells chocolate
⇒ the smart cat smells chocolate
Parse Trees
Regular grammar: in most applications, we just want to describe the set of strings in a language.
Context-free grammar: we also want to assign meanings to the strings in a language, for which
we care about internal structure of the strings. Parse trees capture the essential grammatical
structure of a string. A program that produces such trees is called a parser. A parse tree is an
(ordered, rooted) tree that represents the syntactic structure of a string according to some formal
grammar. In a parse tree, the interior nodes are labeled by non terminals of the grammar, while
the leaf nodes are labeled by terminals of the grammar or .
A parse tree, derived by a grammar G = (V, S, R, S), is a rooted, ordered tree in which:
1. Every leaf node is labeled with an element of ∑ 𝖴{ },
2. The root node is labeled S,
3. Every other node is labeled with some element of: V –∑, and
4. If m is a nonleaf node labeled X and the children of m are labeled x1, x2, …,
xn, then R contains the rule X → x1, x2, …, xn.
SHREEDEVIPRAMOD,CSE,BRCE 63
Example 1: S AB | aaB A a | Aa Bb
Left-most derivation for the string aab is S ⇒ AB ⇒ AaB ⇒ aaB ⇒ aab
Parse tree obtained is
Example 3: Parse Tree -Structure in English for the string “the smart cat smells
chocolate”. It is clear from the tree that the sentence is not about cat smells or smart cat
smells.
A parse tree may correspond to multiple derivations. From the parse tree, we cannot tell which
of the following is used in derivation:
S ⇒ NP VP ⇒ the Nominal VP ⇒
S ⇒ NP VP ⇒ NP V NP ⇒
Parse trees capture the important structural facts about a derivation but throw away the details
of the nonterminal expansion order. The order has no bearing on the structure we wish to assign
to a string.
Generative Capacity
Because parse trees matter, it makes sense, given a grammar G, to distinguish between:
1. G’s weak generative capacity, defined to be the set of strings, L(G), that G generates,
and
2. G’s strong generative capacity, defined to be the set of parse trees that G generates.
When we design grammar, it will be important that we consider both their weak and their
strong generative capacities.
7. Ambiguity
Sometimes a grammar may produce more than one parse tree for some (or all ) of the strings
it generates. When this happens we say that the grammar is ambiguous. A grammar is
ambiguous iff there is at least one string in L(G) for which G produces more than one parse
tree.
SHREEDEVIPRAMOD,CSE,BRCE 64
Example 1: Bal={w { ),(}*: the parenthesis are balanced}.
G={{S,),( }, {),(},R,S} where R={ S S SS S (S) }
Left-most Derivation1 for the string (())() is S ⇒ S⇒(S)S ⇒ ((S))S ⇒ (())S ⇒ (())(S) ⇒ (())()
Left-most Derivation2 for the string (())() is S ⇒ SS ⇒SSS ⇒SS ⇒ (S)S ⇒ ((S))S ⇒ (())S
⇒ (())(S) ⇒ (())()
Since both the parse trees obtained for the same string (())() are different, the grammar is ambiguous.
Since both the parse trees obtained for the same string iytiytxex are different, the grammar is
ambiguous.
Since both the parse trees obtained for the same string aab are different, the grammar is
ambiguous.
SHREEDEVIPRAMOD,CSE,BRCE 65
Why Is Ambiguity a Problem?
With regular languages, for most applications, we do not care about assigning internal
structure to strings.
With context-free languages, we usually do care about internal structure because, given a
string w, we want to assign meaning to w. It is generally difficult, if not impossible, to assign
a unique meaning without a unique parse tree. So an ambiguous G, which fails to produce a
unique parse tree is a problem.
Consider string 2+3*5 written as id +id*id, left-most derivation for string id +id*id is
E ⇒ E*E ⇒ E+E*E ⇒ id+E*E ⇒ id+id*E ⇒ id+id*id.
Similarly the right-most derivation for string id +id*id is
E ⇒ E+E ⇒ E+E*E ⇒ E+E*id ⇒ E+id*id ⇒ id+id*id.
The parse trees obtained for both the derivations are:-
Should the evaluation of this expression return 17 or 25? Designers of practical languages
must be careful that they create languages for which they can write unambiguous grammars.
Techniques for Reducing Ambiguity
No general purpose algorithm exists to test for ambiguity in a grammar or to remove it when
it is found. But we can reduce ambiguity by eliminating
a. rules like S →
b. Rules with symmetric right-hand sides
• A grammar is ambiguous if it is both left and right recursive.
• Fix: remove right recursion
• S → SS or E→E+E
c. Rule sets that lead to ambiguous attachment of optional postfixes.
SHREEDEVIPRAMOD,CSE,BRCE 66
a. Eliminating ε-Rules
Let G =(V, , R, S) be a CFG. The following algorithm constructs a G such that L(G ) =
L(G)-{} and G contains no rules:
removeEps(G: CFG) =
1. Let G = G.
2. Find the set N of nullable variables in G.
3. Repeat until G contains no modifiable rules that haven’t been processed:
Given the rule PQ, where Q N, add the rule P if it is not already present
and if and if P .
4. Delete from G all rules of the form X .
5. Return G.
SHREEDEVIPRAMOD,CSE,BRCE 67
What If ε ∈ L?
Sometimes L(G) contains and it is important to retain it. To handle this case the algorithm
used is
atmostoneEps(G: CFG) =
1. G = removeEps(G).
2. If SG is nullable then /* i. e., L(G)
2.1 Create in G a new start symbol S*.
2.2 Add to RG the two rules:S* and S* SG.
3. Return G.
The new grammar built is better than the original one. The string (())() has only one parse
tree.
But it is still ambiguous as the string ()()() has two parse trees?
So we get: S* | S
S SS1 /* force branching only to the left
S S1 /* add rule
S1 (S) | ()
SHREEDEVIPRAMOD,CSE,BRCE 68
Unambiguous Grammar for Bal={w { ),(}*: the parenthesis are balanced}.
G={{S,),( }, {),(},R,S} where
S* n | S
S SS1 | S1
S1 (S) | ()
The parse tree obtained for the string ()()() is
SHREEDEVIPRAMOD,CSE,BRCE 69
Proving that the grammar is Unambiguous
A grammar is unambiguous iff for all strings w, at every point in a leftmost derivation or
rightmost derivation of w, only one rule in G can be applied.
S* ---(1)
S* S ---(2)
S SS1 ---(3)
S S1 ---(4)
S1 (S) ---(5)
S1 () ---(6)
S* ⇒ S ⇒SS1 ⇒SS1S1 ⇒S1S1S1 ⇒ () S1S1 ⇒ () () S1 ⇒ () () ()
Inherent Ambiguity
In many cases, for an ambiguous grammar G, it is possible to construct a new grammar G
that generate L(G) with less or no ambiguity. However, not always. Some languages have the
property that every grammar for them is ambiguous.We call such languages inherently
ambiguous.
Example: L = {aibjck: i, j , k 0, i=j or j=k}.
Every string in L has either (or both) the same number of a’s and b’s or the same number of
b’s and c’s. L = {anbncm: n, m 0} {anbmcm: n, m 0}.
One grammar for L has the rules:
S S1 | S2
S1 S1c | A /* Generate all strings in {anbncm}.
A aAb |
S2 aS2 | B /* Generate all strings in {anbmcm}.
B bBc |
Consider the string a2b2c2 .
It has two distinct derivations, one through S1 and the other through S2
S ⇒ S1 ⇒ S1c⇒ S1cc ⇒ Acc ⇒ aAbcc ⇒ aaAbbcc ⇒ aabbcc
S ⇒ S2 ⇒ aS2 ⇒ aaS2 ⇒ aaB ⇒ aabBc ⇒ aabbBcc⇒ aabbcc
Given any grammar G that generates L, there is at least one string with two derivations in G.
SHREEDEVIPRAMOD,CSE,BRCE 70
8. Normal Forms
We have seen in some grammar where RHS is , it makes grammar harder to use. Lets see
what happens if we carry the idea of getting rid of -productions a few steps farther. To make
our tasks easier we define normal forms.
Normal Forms - When the grammar rules in G satisfy certain restrictions, then G is said to be
in Normal Form.
• Normal Forms for queries & data can simplify database processing.
• Normal Forms for logical formulas can simplify automated reasoning in AI systems
and in program verification system.
• It might be easier to build a parser if we could make some assumptions about the form
of the grammar rules that the parser will use.
Normal Forms for Grammar
Among several normal forms, two of them are:-
• Chomsky Normal Form(CNF)
• Greibach Normal Form(GNF)
SHREEDEVIPRAMOD,CSE,BRCE 71
Converting to Chomsky Normal Form
Apply some transformation to G such that the language generated by G is unchanged.
1. Rule Substitution.
Example: X aYc Y b Y ZZ equivalent grammar constructed is X abc | aZZc
There exists 4-steps algorithm to convert a CFG G into a new grammar Gc such that: L(G) =
L(Gc) – {}
convertChomsky(G:CFG) =
1. G' = removeEps(G:CFG) S
2. G'' = removeUnits(G':CFG) AB
3. G''' = removeMixed(G'':CFG) A aB
4. G'v = removeLong(G''' :CFG) S ABCD
return Gc
SHREEDEVIPRAMOD,CSE,BRCE 72
removeUnits returns G'' :
S aACa | aAa | aCa | aa
A cC | a | c
B cC | c
C cC | c
SHREEDEVIPRAMOD,CSE,BRCE 73
Now, by apply removeLong returns G'v :
S TaS1 | TaS3 | TaS2 | TaTa
S1 AS2
S2 CTa
S3 ATa
A TcC | a | c
B TcC | c
C TcC | c
Ta a
Tc c
SHREEDEVIPRAMOD,CSE,BRCE 74
Example 3: Apply the normalization algorithm to convert the grammar to CNF
G: S → ABC
A → aC | D
B → bB | ε | A
C → Ac | ε | Cc
D → aa
removeEps(G:CFG) returns
G: S → ABC | AC | AB | A
A → aC | D | a
B → bB | A | b
C → Ac | Cc | c
D → aa
removeUnits(G':CFG) returns
G : S → ABC | AC | AB | aC | aa | a
A → aC | aa | a
B → bB | aC | aa | a | b
C → Ac | Cc | c
D → aa
removeMixed(G'':CFG) returns
G : S → ABC | AC | AB | Ta C | Ta Ta | a
A → Ta C | Ta Ta | a
B → Tb B | Ta C | Ta Ta | a | b
C → A Tc | C Tc | c
D → Ta Ta
Ta a
Tb b
Tc c
SHREEDEVIPRAMOD,CSE,BRCE 75
9. Pushdown Automata
An acceptor for every context-free language. A pushdown automata , or PDA, is a finite state
machine that has been augmented by a single stack.
Definition of a (NPDA) Pushdown Automaton
M = (K, S, G, Δ , s, A), where:
K is a finite set of states,
S is the input alphabet,
G is the stack alphabet,
s ∈ K is the initial state,
A ⊆ K is the set of accepting states, and
Δ is the transition relation.
Configuration
A configuration of PDA M is an element of K X S* X G*. Current state, Input that is still left
to read and, Contents of its stack.
The initial configuration of a PDA M, on input w, is (s, w, ).
If s1s2…sn is pushed onto the stack: the value after the push is s1s2…sncba
SHREEDEVIPRAMOD,CSE,BRCE 76
Yields-in-one-step written |-M relates configuration1 to
A computation by M is a finite sequence of configurations C0, C1, C2,,,,,,,,,,,,,Cn for some n ≥0
such that:
• C0 is an initial configuration,
• Cn is of the form (q, , ), for some q ∈ K and some string in G*, and
• C0 |-M C1 |-M C2 |-M ,,,,,,,,,,,, |-M Cn.
Nondeterminism
If M is in some configuration (q1, s, ) it is possible that:
● Δ contains exactly one transition that matches. In that case, M makes the specified
move.
● Δ contains more than one transition that matches. In that case, M chooses one of
them.
● Δ contains no transition that matches. In that case, the computation that led to that
configuration halts.
Accepting
Let C be a computation of M on input w then C is an accepting configuration
iif C= (s, w, ) |-M* (q, , ), for some q ∈ A.
A computation accepts only if it runs out of input when it is in an accepting state and the stack
is empty.
C is a rejecting configuration iif C= (s, w, ) |-M* (q, w, ),
where C is not an accepting computation and where M has no moves that it can makes from (q,
w, ). A computation can reject only if the criteria for accepting have not been met and there
are no further moves that can be taken.
Let w be a string that is an element of S* . Then:
• M accepts w iif atleast one of its computations accepts.
• M rejects w iif all of its computations reject.
The language accepted by M, denoted L(M), is the set of all strings accepted by M. M rejectsa
string w iff all paths reject it.
It is possible that, on input w, M neither accepts nor rejects. In that case, no path accepts and
some path does not reject.
Transition
Transition ((q1, c, 1), (q2, 2)) says that “If c matches the input and g1 matches the current top
of the stack, the transition from q1 to q2 can be taken. Then, c will be removed from the input,
1 will be popped from the stack, and 2 will be pushed onto it. M cannot peek at the top of the
stack without popping
• If c = , the transition can be taken without consuming any input.
• If 1 = , the transition can be taken without checking the stack or popping anything.
Note: it’s not saying “the stack is empty”.
• If 2 = , nothing is pushed onto the stack when the transition is taken.
SHREEDEVIPRAMOD,CSE,BRCE 77
Example1: A PDA for Balanced Parentheses. Bal={w { ),(}*: the parenthesis are
balanced}
M = (K, S, G, Δ, s, A),
where:
K = {s} the states
S = {(, )} the input alphabet
= {(} the stack alphabet
A = {s} the accepting state
Δ = {(s, ( )) -------------------- (1)
((s, ), ( ) , (s, )) --------- (2) }
Unread
Transition State Stack
input
S (()))
1 S ())) (
1 S ))) ((
2 S )) (
2 S )
The computation has reached the final state S and stack is empty, but still the string is
rejected because the input is not empty.
SHREEDEVIPRAMOD,CSE,BRCE 78
Example2 of Rejecting -- Input string = ((())
Transition State Unread input Stack
S ((())
1 S (()) (
1 S ()) ((
1 S )) (((
2 S ) ((
2 S (
(S, ((()),) |- (S, (()),( |- (S,( )),(() |- (S, )),((() |- (S, ),(() |- (S, ,()
The computation has reached the final state S and runs out of input, but still the string is
rejected because the stack is not empty.
The computation has reached the final state f, the input string is consumed and the stack is
empty. Hence the string aabb is accepted.
SHREEDEVIPRAMOD,CSE,BRCE 79
-----(1)
Δ = {((s, a, ), (s, a)
-----(2)
((s, b, ), (s, b))
-----(3)
((s, c, ), (f, ))
-----(4)
((f, a, a), (f, ))
-----(5)
((f, b, b), (f, ))}
SHREEDEVIPRAMOD,CSE,BRCE 80
10. Deterministic and Nondeterministic PDAs
Exploiting Nondeterministic
Previous examples are DPDA, where each machine followed only a single computational
path. But many useful PDAs are not deterministic, where from a single configuration there
exist multiple competing moves. As in FSMs, easiest way to envision the operation of a
NDPDA M is as a tree.
Each node in the tree corresponds to a configuration of M and each path from the root to a
leaf node may correspond to one computation that M might perform. The state, the stack and
the remaining input can be different along different paths. As a result, it will not be possible
to simulate all paths in parallel, the way we did for NDFSMs.
SHREEDEVIPRAMOD,CSE,BRCE 81
SHREEDEVIPRAMOD,CSE,BRCE 82
Example 2: PDA for {w {a, b}* : #a(w) = #b(w)}= Equal Numbers of a’s and b’s.
L = {, ab, ba, abba, aabb, baba, bbaa, …….}
M = (K, S, G, Δ, s, A),
where:
K = {s} the states
S = {a, b} the input alphabet
Γ = {a, b} the stack alphabet
A = {s} the accepting state
Δ ={((s, a, ), (s, a)) -----(1)
((s, b, ), (s, b)) -----(2)
((s, a, b), (s, )) -----(3)
((s, b, a), (s, ))} -----(4)
Example 3: The a Region and the b Region are Different. L = {ambn : m ≠ n; m, n > 0}
It is hard to build a machine that looks for something negative, like ≠. But we can break L
into two sublanguages: {ambn : 0 < n < m} and {ambn : 0 < m < n}
• If stack and input are empty, halt and reject
• If input is empty but stack is not (m > n) (accept)
• If stack is empty but input is not (m < n) (accept)
SHREEDEVIPRAMOD,CSE,BRCE 83
Δ = { ((1, a, ), (1, a )) -----(1)
((1, b, a ), (2, )) -----(2)
((2, b, a ), (2, )) -----(3)
((2, , a ), (3, )) -----(4)
SHREEDEVIPRAMOD,CSE,BRCE 84
((4, b, ), (4, )) } -----(7)
SHREEDEVIPRAMOD,CSE,BRCE 85
Two problems with this M:
1. We have no way to specify that a move can be taken only if the stack is empty.
2. We have no way to specify that the input stream is empty.
3. As a result, in most of its moves in state 2, M will have a choice of three paths to take.
Case1: A transition that should be taken only if the stack is empty competes against one or
more moves that require a match of some string on the stack.
Problem: Nondeterminism could be eliminated if it were possible to check for an empty
stack.
Solution: Using a special bottom-of-stack marker ( # )
Before doing anything, push a special character onto the stack. The stack is then logically empty
iff that special character (#) is at the top of the stack. Before M accepts a string, its stack must
be completely empty, so the special character must be popped whenever M reaches an
accepting state.
Now the transition back to state 2 no longer competes with the transition to state 4, which can
only be taken when the # is the only symbol on the stack. The machine is still
nondeterministic because the transition back to state 2 competes with the transition to state 3.
SHREEDEVIPRAMOD,CSE,BRCE 86
Case2: A transition that should be taken only if the input stream is empty competes against
one or more moves that require a match against a specific input character.
Problem: Nondeterminism could be eliminated if it were possible to check for an empty input
stream.
Solution: using a special end-of-string marker ($ )
Adding an end-of-string marker to the language to be accepted is a powerful tool for reducing
nondeterminism. Instead of building a machine to accept a language L, build one to accept L$,
where $ is a special end-of-string marker.
Now the transition back to state 2 no longer competes with the transition to state 3, since the
can be taken when the $ is read. The $ must be read on all the paths, not just the one where we
need it.
SHREEDEVIPRAMOD,CSE,BRCE 87
But the facts about CFGs and PDAs are different from the facts about RLs and FSMs.
1. There are context-free languages for which no deterministic PDA exists.
2. It is possible that a PDA may
● not halt,
● not ever finish reading its input.
However, for an arbitrary PDA M, there exists M that halts and L(M) = L(M).
There exists no algorithm to minimize a PDA. It is undecidable whether a PDA is minimal.
Problem 2 : Let M be a PDA that accepts some language L. Then, on input w, if w L then
M will halt and accept. But if w L then, while M will not accept w, it is possible that it will
not reject it either.
Example1: Let S = {a} and consider M =
For L(M) = {a}. The computation (1, a, e) |- (2, a, a) |- (3, e, e) causes M to accept a.
Example2: Consider M =
Formal ones that do not restrict the class of the language that are being considered-
converting grammar into normal forms like Chomsky or Greibach normal form.
Practical ones that work only on a subclass of the CFLs- use grammars in natural
forms.
SHREEDEVIPRAMOD,CSE,BRCE 88
There are two alternatives to this:
1. PDA by Final state: Accept if, when the input has been consumed, M lands in an
accepting state, regardless of the contents of the stack.
2. PDA by Empty stack: Accept if, when the input has been consumed, the stack is empty,
regardless of the state M is in.
All of these definitions are equivalent in the sense that, if some language L is accepted by a
PDA using one definition, it can be accepted by some PDA using each of the other definition.
For example:- If some language L is accepted by a PDA by Final state then it can be accepted
by PDA by Final state and empty stack. If some language L is accepted by a PDA by Final
state and empty stack then can be accepted by PDA by Final state.
We can prove by showing algorithms that transform a PDA of one sort into and equivalent
PDA of the other sort.
Equivalence
1. Given a PDA M that accepts by accepting state and empty stack, construct a new
PDA M that accepts by accepting state alone, where L(M) = L(M).
2. Given a PDA M that accepts by accepting state alone, construct a new PDA M that
accepts by accepting state and empty stack, where L(M) = L(M).
Hence we can prove that M and M accept the same strings.
1. Accepting by Final state Alone
Define a PDA M = (K, S, G, Δ , s, A). Accepts when the input has been consumed, M lands
in an accepting state, regardless of the contents of the stack. M accepts if C= (s, w, ) |-M* (q,
, g), for some q A.
M will have a single accepting state qa. The only way for M to get to qa will be to land in an
accepting state of M when the stack is logically empty. Since there is no way to check that
the stack is empty, M will begin by pushing a bottom-of-stack marker #, onto the stack.
Whenever # is the top symbol of the stack, then stack is logically empty.
The construction proceeds as follows:
1. Initially, let M = M.
2. Create a new start state s.
Add the transition ((s, , ),(s, #)),
3. For each accepting state a in M do:
Add the transition ((a, ,#),(qa, )),
4. Make qa the only accepting state in M
Example:
SHREEDEVIPRAMOD,CSE,BRCE 89
It is easy to see that M lands in its accepting state(qa) iff M lands in some accepting state
with an empty stack. Thus M and M accept the same strings.
2. Accepting by Final state and Empty stack
The construction proceeds as follows:
1. Initially, let M = M.
2. Create a new accepting state F
3. From each accepting state a in M do:
Add the transition ((a, , ),(F, )),
4. Make F the only accepting state in M
5. For every element g of Γ,
Add the transition to M ((F, , g), (F, )).
In other words, iff M accepts, go to the only accepting state of M′ and clear the stack. Thus
M′ will accept by accepting state and empty stack iff M accepts by accepting state.
Example:-
SHREEDEVIPRAMOD,CSE,BRCE 90
Sl.No Sample Questions
1. Define context free grammars and languages.
11. Define Chomsky normal form. Apply the normalization algorithm to convert the grammar to
Chomsky normal form.
a. S → aSa S → B B → bbC
B → bb C → ε C → cC
b. S → ABC A → aC | D B → bB | ε | A
C → Ac | ε | Cc D → aa
12. Define Push down automata (NPDA). Design a NPDA for the CFG given in Question (2).
13. Design a PDA for the given language.L$, where L = {w ∈ {a, b}* : #a(w) = #b(w)}.
14. Design a PDA for the language: L={ aibjck : i+j=k ,i>=0,j>=0}
16. Design a PDA for the language: L={ aibjck : i+k=j ,i>=0,k>=0}
17. Design a PDA for the language: L={ aibjck : k+j=i ,k>=0,j>=0}
SHREEDEVIPRAMOD,CSE,BRCE 91
Module-4
Context-Free and Non-Context-Free Languages
● The language AnBn = {anbn | n≥ 0} is context-free.
● The language AnBnCn ={ anbncn |n≥0} is not context free because a PDA's stack cannot count
all three of the letter regions and compare them.
Theorem: The regular languages are a proper subset of the context-free languages.
Proof: We first show that every regular language is context-free. We then show that there
exists at least one context-free language that is not regular.
There exists at least one context-free language that is not regular : The regular languages
are a proper subset the context-free languages because there exists at least one language anbn
that is context –free but not regular.
There cannot be more CFLs than CFGs. So there are at most a countably infinite number of context-
free languages. There is not a one-to-one relationship between CFLs and CFGs, since there are an
infinite number of grammars that generate any given language. But we know that, every regular
language is context free and there is a countably infinite number of regular languages.
SHREEDEVIPRAMOD,CSE,BRCE 90
• Exhibit a context-free grammar for it.
• Exhibit a (possibly nondeterministic) PDA for it.
Theorem: The length of the yield of any tree T with height h and
Let k be bn+1.
Any string that can be generated by G and whose parse tree contains no paths with repeated
nonterminals must have length less than or equal to bn. Assuming that b≥2, it must be the case that bn+1
> bn. So let w be any string in L( G) where |w|≥k.
Let T be any smallest parse tree for w. T must have height at least n+1. Choose some path in T of
length at least n + 1. Let X be the bottom-most repeated non terminal along that path. Then w can be
rewritten as uvxyz as shown in below tree,
The tree rooted at [1] has height at most n+1.Thus its yield, vxy, has length less than or equal
to bn+1,which is k. Further, vy≠Ɛ .Since if vy were Ɛ then there would be a sma er parse tree for wand
we choose T so that h at wasn't so.
Finally, v and y can be pumped: uxz must be in L because rule2 could have been used immediately
SHREEDEVIPRAMOD,CSE,BRCE 91
at[1]. And, for any q≥1, uvqxyqz must be in L because rule1 could have been used q times before finally
using rule2.
SHREEDEVIPRAMOD,CSE,BRCE 92
The resulting string will have letters out of order and thus not in L.
So L is not context-free.
Call the part before the c the leftside and the part after the c the right side. We consider all the
cases for where v and y could fall and show that in none of them are all the condition so f the theorem met:
• If either v or y overlaps region 3, set q to 0. The resulting string will no longer contain a c and so is
not in WcW.
• If both v and y occur before region 3 or they both occur after region 3, then set q to 2. One side will
be longer than the other and so the resulting string is not in WcW.
• If either v or y overlaps region1 ,then set q to 2. In order to make the right side match. Something
would have to be pumped into region 4. But any v,y pair that did that would violate the requirement
that lvxyl ≤ k.
• If either v or y overlaps region2, then set q to 2. In order to make the right side match, something
would have to be pumped into region 5. But any v,y pair that did that would violate the requirement
that lvxyl ≤ k.
• There is no way to divide w into uvxyz such that all the conditions of the Pumping Theorem are
met . So WcW is not context-free.
• If L1 and L2 are context free languages then there exists a context-free grammar G1= (V1,Σ1, R1,S1)
and G2=(V2,Σ2,R2,S2) such that L1=L(G1) and L2=L(G2).
SHREEDEVIPRAMOD,CSE,BRCE 93
• We will build a new grammar G such that L(G)=L(G1)UL(G2). G will contain all the rules of both
G1 and G2.
• We add to G a new start symbol S and two new rules. S→S1 and S→S2. The two new rules allow G
to generate a string iff at least one of G1or G2 generates it.
So, G = ( V1 U V2 U {S}, Σ1 U Σ2, R1 U R2 U {S→ S1,S→S2}, S )
• Every rule in G is of the form X→BC or X→a, where X, B, and C are elements of (V-Σ) and a ϵΣ
• So construct, from G, a new grammar G1 , Such that L(G1)= LR .
• G1= (VG, ΣG, R', SG) , Where R' is constructed as follows:
For every rule in G of the form X→BC, add to R' the rule X→CB
For every rule in G of the form X→ a then add to R' the rule X→ a
SHREEDEVIPRAMOD,CSE,BRCE 94
• Then letsub is a letter substitution function from L1 to L2 iff letsub(L1) ={ w∈ Σ2*: ∃y ∈ L1 (w=y
except that every character c of y has replaced by sub(c))}.
Example : Let y= VTU ∈ L1 And sub(c) is given as : sub(V) = Visvesvaraya
sub(T) = Technological
sub(U)= University
Then , sub(VTU) = Visvesvaraya Technological University
SHREEDEVIPRAMOD,CSE,BRCE 95
• If L1 is context-free, then there exists some PDA M1= (K1,Σ,𝛤1,𝚫1,S1,A1) that accepts it.
• If L2 is regular then there exists a DFSM M2= (K2,Σ,𝛿,S2,A2) that accepts it.
• We construct a new PDA, M3 that accepts L1 ∩ L2. M3 will work by simulating the parallel
execution of M1 and M2.
• For each transition ((q1, a, 𝛽) ,(p1, 𝗒)) in 𝚫1 and each transition ((q2, a ) ,p2) in 𝛿, add 𝚫3 the
transition: (((q1,q2),a,𝛽) ,((p1,p2), 𝗒)).
• For each transition ((q1,ℇ,𝛽) ,(p1, 𝗒)) in 𝚫1and each state q2 in k2, add to 𝚫3 the transition:
(((q1,q2),ℇ,𝛽) ,((p1,p2), 𝗒)).
SHREEDEVIPRAMOD,CSE,BRCE 96
The regular languages are closed under complement, intersection and difference. Why are the context-
free languages different? Because the machines that accept them may necessarily be nondeterministic.
1. From M1, construct an equivalent DFSM M', using the algorithm ndfsmtodfsm, presented in the
proof of Theorem5.3. (If M1 is already deterministic. M'=M1.)
2. M' must be stated completely. so if it is described with an implied dead state, add the dead state and
all required transitions to it.
3. Begin building M2 by setting it equal to M'. Then swap the accepting and the non-accepting states.
So M2 M'= (KM',.Σ,𝜹M',sM',KM'-AM').
We have no PDA equivalent of ndfstodfsm because there provably isn't one. We defined a PDA M
to be deterministic iff:
•ΔM contains opairs of transitions that compete with each other, and
• if q is an accepting state of M, then there is no transition ((q,ε,ε),(p,a)) for any p or a.
For example, the PDA accepts L, but it is nondeterministic because the transition to state3 (where the
a's will be popped) can compete with both of the other transitions from state1.
SHREEDEVIPRAMOD,CSE,BRCE 97
With an end-of-string marker, we can build the deterministic PDA, which can only take the transition
to state3, the a-popping state. When it sees the $:
NOTE: Adding the end-of-string marker cannot convert a language that was not
context-free into one that is.
SHREEDEVIPRAMOD,CSE,BRCE 98
Closure Properties of the Deterministic Context-Free
Languages
1) Closure Under Complement
Theorem: The deterministic context-free languages are closed under
complement.
Proof: The proof is by construction. If L is a deterministic context-free language over the alphabet Σ,
then L$ is accepted by some deterministic PDA M = (K, Σ U{$}, 𝐹, , s, A).
We need to describe an algorithm that constructs a new deterministic PDA that accepts (¬L)$.
We defined a construction that proceeded in two steps:
• Given an arbitrary FSM, convert it to an equivalent DFSM, and then
• Swap accepting and non accepting states.
A deterministic PDA may fail to accept an input string w for any one of several reasons:
1. Its computation ends before it finishes reading w.
2. Its computation ends in an accepting state but the stack is not empty.
3.Its computation loops forever, following ε-transitions, without ever halting in either an
accepting or a non accepting state.
4. Its computation ends in a non accepting state.
If we simply swap accepting and non accepting states we will correctly fail to accept every string that
M would have accepted (i.e., every string in L$). But we will not necessarily accept every string in (
¬L)$. To do that, we must also address issues 1through 3 above.
An additional problem is that we don't want to accept ¬L(M). That includes strings that do not end
in $. We must accept only strings that do end in $ and that are in (¬L)$.
SHREEDEVIPRAMOD,CSE,BRCE 99
={ aibjck |i,j,k≥0 and (i=j=k)} U {w∈{a,b,c}*: the letters are out of order}.
Let, L"'=L"∩a*b*c* = {anbncn | n ≥ 0}
But L'" is AnBnCn={ anbncn |n≥0},which we have shown is not context-free.
SHREEDEVIPRAMOD,CSE,BRCE 100
Since L is context-free, it is accepted by some (non deterministic) PDA M. M is an example
of an on deterministic PDA for which no equivalent deterministic PDA L exists. If such a deterministic
PDA did exist and accept L, it could be converted into a deterministic PDA that accepted L$. But, if
that machine existed. L would be deterministic context-free and we just showed that it is not.
(a) PalEven={wwR:w∈{a,b}*}.
(b) {anbncmd|n,m≥0}U{anbmcme|n,m≥0}.
• There exist languages that are in the outer donut because they are inherently ambiguous. Two
examples are:
{aibjck|i,j,k ≥ 0 and ((i≠j)or(j≠k))}
{aibjck|i,j,k ≥ 0 and ((i=j) or (j=k))}
SHREEDEVIPRAMOD,CSE,BRCE 101
Regular Language is Deterministic Context-Free
Theorem: Every regular language is deterministic context-free.
Proof: The proof is by construction. {$} is regular. So, if L is regular then so is L$ (since the regular
languages are closed under concatenation).So there is a DFSM M that accepts it. Using the construction
to show that every regular language is context-free Construct, from M a PDA P that accepts L$. P will
be deterministic.
buildunambiggrammar(M:deterministicPDA)=
1. Let G=buildgrammar(convertPDAtodetnormalform(M)).
2. Let G' be the result of substituting ε for $ in each rule in which $ occurs.
3. Return G'.
NOTE: The algorithm convertPDAtodetnormalform, is described in the theorem that proves the
deterministic context-free languages are closed under complement.
This question can be answered for every context-free language and for every context-free
language L there exists a PDA M such that M accepts L. But existence of a PDA that accepts L does
not guarantee the existence of a procedure that decides it.
SHREEDEVIPRAMOD,CSE,BRCE 102
It turns out that there are two alternative approaches to solving this problem, both of which
work:
● Use a grammar: Using facts about every derivation that is produced by a grammar in Chomsky
normal form, we can construct an algorithm that explores a finite number of derivation paths
and finds one that derives a particular string w iff such a path exists.
● Use a PDA : While not all PDAs halt, it is possible, for any context-free language L, to craft
a PDA M that is guaranteed to halt on all inputs and that accepts all strings in L and rejects all
strings that are not in L.
1. If L is specified as a PDA, use PDA to CFG, to construct a grammar G such that L(G) =L (M).
4. If w ≠ ε then:
4.1. From G, construct G' such that L (G') = L(G)-{ε} and G' is in Chomsky normal form.
4.2. If G derives to, it does so in (2 • |w| - 1) steps. Try all derivations in G of that number
Elimination of ε-Transitions
Theorem: Given any context-free grammar G=(V,Σ,R,S), there exists a PDA M
such that L(M)=L(G)-{ε} and M contains no transitions of the form
SHREEDEVIPRAMOD,CSE,BRCE 103
((q1,ε,α),(q2,𝜷)). In other words, every transition reads exactly one input
character.
Proof: The proof is by a construction that begins by converting G to Greibach normal form. Now
consider again the algorithm cfgtoPDAtopdown, which builds, from any context-free grammar G, a
PDA M that, on input w, simulates G deriving w, starting from S.
M= ({p,q},Σ,V,Δ, p,{q}), where Δ contains:
1. The start-up transition ((p,ε,ε),(q,S)), which pushes the start symbol on to the stack and
goes to state q.
2. For each rule X→s1s2...sn, in R, the transition ((q,ε,X),(q,s1s2...sn)), which replaces X by
s1s2...sn. If n=0 (i.e., the right-hand side of the rule is ε), then the transition ( (q, ε, X), (q, ε)).
3. For each character c ∈ Σ. the transition ((q, c, c), (q,ε)), which compares an expected
character from the stack against the next input character.
If G contains the rule X→cs2...sn, (where c ∈Σ and s2 through sn, are elements of V-Σ), it is not
necessary to push c onto the stack, only to pop it with a rule from step 3.
Instead, we collapse the push and the pop into a single transition. So we create a transition that can
be taken only if the next input character is c. In that case, the string s2 through sn is pushed onto the
stack.
Since terminal symbols are no longer pushed onto the stack. We no longer need the transitions created
in step3 of the original algorithm.
So, M=({p,q},Σ,V,Δ,p,{q}), where Δ contains:
1. The start-up transitions: For each rule S→cs2...sn the transition ((p,c,ε),(q,s2...sn)).
2. For each rule X→cs2...sn (where c∈Σ and s2 through sn, are elements of V-Σ), the
transition ((q,c,X),(q,s2...sn)).
cfgtoPDAnoeps(G:context-freegrammar)=
1. Convert G to Greibach normal form, producing G'.
2. From G' build the PDA M described above.
SHREEDEVIPRAMOD,CSE,BRCE 104
b) The total number of computations pursued by M must be less than or equal to bn, where b
is the maximum number of competing transitions from any state in M.
c) The total number of steps that will be executed by all computations of M is bounded by nbn
Proof:
a) Since each computation of M must consume one character of w at each step and M will halt when
it runs out of input, each computation must halt within n steps.
b) M may split into at most b branches at each step in a computation. The number of steps in a
computation is less than or equal to n. So the total number of computations must be less than or equal
to bn.
c) Since the maximum number of computations is bn and the maximum length of each is n, the
maximum number of steps that can be executed before all computations of M halt is nbn.
So a second way to answer the question, "Given a context-free language L and a string w, is w
in L?" is to execute the following algorithm:
decideCFLusingPDA(L:CFL,w:string)=
1. If L is specified as a PDA, use PDAtoCFG, to construct a grammar G such that L(G)=L(M).
2. If L is specified as a grammar G, simply use G.
3. If w=ε then if SG is nullable then accept, otherwise reject.
4.If w≠ε then:
4.1. From G, construct G' such that L(G')=L(G)-{ε} and G' is in Greibach normal form.
4.2. From G' construct, using cfgtoPDAnoeps, a PDA M' such that L(M')=L(G') and M' has
no ε-transitions.
4.3.We have proved previously that, all paths of M' are guaranteed to halt within a finite
number of steps. So run M' on w, Accept if M' accepts and reject otherwise.
SHREEDEVIPRAMOD,CSE,BRCE 105
Proof :
decideCFLempty( G: context-free grammar) =
1. Let G' =removeunproductive(G).
2. If S is not present in G' then return True else return False.
decideCFLinfinite(G:context-freegrammar)=
1. Lexicographically enumerate all strings in Σ* of length greater than bn and less than or
equal to bn+1+bn.
2. If, for any such string w, decideCFL(L,w) returns True then return True. L is infinite.
3. If, for all such strings w, decideCFL(L,w) returns False then return False. L is not infinite.
TURING MACHINE
The Turing machine provides an ideal theoretical model of a computer. Turing machines are useful
in several ways:
• Turing machines are also used for determining the undecidability of certain languages and
• As an automaton, the Turing machine is the most general model, It accepts type-0
languages.
• It can also be used for computing functions. It turns out to be a mathematical model of
partial recursive functions.
• Measuring the space and time complexity of problems.
Turing assumed that while computing, a person writes symbols on a one-dimensional paper (instead
of a two dimensional paper as is usually done) which can be viewed as a tape divided into cells. In
SHREEDEVIPRAMOD,CSE,BRCE 106
Turing machine one scans the cells one at a time and usually performs one of the three simple
operations, namely:
(i) Writing a new symbol in the cell being currently scanned,
(ii) Moving to the cell left of the present cell, and
(iii) Moving to the cell right of the present cell.
SHREEDEVIPRAMOD,CSE,BRCE 107
5. is the transition function mapping (q,x) onto (q',y,D) where D denotes the direction of
movement of R/W head; D=L orR according as the movement is to the left or right.
6. q0∈ Q is the initial state, and
7. F⊆Q is the set of final states.
Notes:
(1) The acceptability of a string is decided by the reachability from the initial state to some final state.
(2) may not be defined for some elements of QX 𝚪.
REPRESENTATION OF TURINGMACHINES
We can describe a Turing machine employing
(i) Instantaneous descriptions using move-relations.
(ii) Transition table, and
(iii) Transition diagram (Transition graph).
REPRESENTATION BY INSTANTANEOUS DESCRIPTIONS
Definition: An ID of a Turing machine M is a string 𝛼𝛽𝛾, where 𝛽 is the present state of M, the
entire input string is split as 𝛼𝛾, the first symbol of 𝛾 is the current symbol a under the R/W head and
𝛾 has all the subsequent symbols of the input string, and the string 𝛼 is the substring of the input
string formed by all the symbols to the left of a.
EXAMPLE: A snapshot of Turing machine is shown in below Fig. Obtain the instantaneous
description.
SHREEDEVIPRAMOD,CSE,BRCE 108
Notes: (1) For constructing the ID, we simply insert the current state in the input string to the left of
the symbol under the R/W head.
(2) We observe that the blank symbol may occur as part of the left or right substring.
We give the definition of in the form of a table called the transition table If (q, a)=(𝛾,𝛼,𝛽). We write
𝛼𝛽𝛾 under the 𝛼-column and in the q-row. So if we get 𝛼𝛽𝛾 in the table, it means that 𝛼 is written in
the current cell, 𝛽 gives the movement of the head (L or R) and 𝛾 denotes the new state into which
the Turing machine enters.
EXAMPLE:
Consider, for example, a Turing machine with five states q1,...,q5 where q1 is the initial state and q5 is the
(only) final state. The tape symbols are 0,1and b. The transition table given below describes :
SHREEDEVIPRAMOD,CSE,BRCE 109
LANGUAGE ACCEPTABILITY BY TURING MACHINES
Let us consider the Turing machine M=(Q,Σ,𝚪,,q0,b,F). A string w in Σ* is said to be
accepted by M, if q0w 𝛼1p𝛼2 for some P∈F and 𝛼1,𝛼2∈𝚪*.
EXAMPLE: Consider the Turing machine M described by the table below
As (q5,1) is not defined, M halts; so the input string 011 is not accepted
SHREEDEVIPRAMOD,CSE,BRCE 110
M halts. As q2 is not an accepting state,001 is not accepted by M.
SHREEDEVIPRAMOD,CSE,BRCE 111
EXAMPLE 2: Design a TM that accepts {0n1n| n≥ 0}
Solution: We require the following moves:
(a) If the leftmost symbol in the given input string w is 0, replace it by x and move right till we
encounter a leftmost 1in w. Change it to y and move backwards.
(b) Repeat (a) with the leftmost 0. If we move back and forth and no 0 or 1 remains. Move to a final
state.
(c) For strings not in the form 0n1n, the resulting state has to be non-final.
we construct a TM M as follows:M = (Q, Σ,𝚪, , q0,b, F)
Q = {q0,q1,q2,q3,qf}
F = {qf}
Σ = { 0,1}
𝚪= { 0,1,x,y,b}
SHREEDEVIPRAMOD,CSE,BRCE 112
2. STORAGE IN THE STATE
We can use a state to store a symbol as well. So the state becomes a pair(q,a) where q is the
state and a is the tape symbol stored in (q, a). So the new set of states becomes Qx𝚪.
EXAMPLE: Construct a TM that accepts the language 0 1* + 1 0*.
We have to construct a TM that remembers the first symbol and checks that it does not
appear afterwards in the input string.
So we require two states, q0, q1. The tape symbols are 0,1 and b. So the TM, having the 'storage
facility in state‘, is M=({q0,q1}X{0,1,b},{0,1},{0,1,b},,[q0,b],[q1,b]})
We describe by its implementation description.
1. In the initial state, M is in q0 and has b in its data portion. On seeing the first symbol of the input
sting w, M moves right, enters the state q1 and the first symbol, say a, it has seen.
2. M is now in [q1,a].
(i) If its next symbol is b, M enters [q1,b], an accepting state.
(ii) If the next symbol is a, M halts without reaching the final state (i.e. is not defined).
(iii) If the next symbol is ā, (ā=0 if a=1 and ā=1 if a=0), M moves right without changing
state.
3. Step2 is repeated until M reaches [q1,b] or halts ( is not defined for an input symbol in w).
3. MULTIPLE TRACK TURING MACHINE
In a multiple track TM, a single tape is assumed to be divided into several tracks. Now the tape
alphabet is required to consist of k-tuples of tape symbols, k being the number of tracks. In the case of
the standard Turing machine, tape symbols are elements of r; in the case of TM with multiple tracks,
it is 𝚪k.
4. SUBROUTINES
First a TM program for the subroutine is written. This will have an initial state and a 'return'
state. After reaching the return state, there is a temporary halt for using a subroutine, new states are
introduced. When there is a need for calling the subroutine, moves are effected to enter the initial state
for the subroutine. When the return state of the subroutine is reached, return to the main program of
TM.
EXAMPLE: Design a TM which can multiply two positive integers.
Solution: The input (m,n), m,n being given ,the positive integers represented by 0 m10n. M starts with
0m10n in its tape. At the end of the computation, 0mn (mn in unary representation) surrounded byb's is
obtained as the output.
SHREEDEVIPRAMOD,CSE,BRCE 113
The major steps in the construction are as follows:
1. 0m10n1 is placed on the tape (the output will be written after the rightmost 1).
2. The leftmost 0 is erased.
3. A block of n 0's is copied onto the right end.
4. Steps 2 and 3 are repeated m times and 10m10mn is obtained on the tape.
5. The prefix 10m1of 10m10mn is erased, leaving the product 0mn as the output.
For every 0 in 0m, 0n is added onto the right end. This requires repetition of step3. We define a
subroutine called COPY for step3. For the subroutine COPY the initial state is q1 and the final state
is q5 is given by the transition table as below:
The Turing machine M has the initial state q0. The initial ID for M is q00m10n. On seeing 0,the following
moves take place
q1 is the initial state of COPY. The following moves take place for M1:
After exhausting 0s, q1 encounters 1. M1 moves to state q4. All 2's are converted back to 0's and
M1 halts in q5. The TM M picks up the computation by starting from q 5 The q0 and q6 are the states of
M. Additional states are created to check whether reach 0 in 0 m gives rise to 0m at the end of the
rightmost 1 in the input string. Once this is over, M erases 10n1 and finds 0mn in the input tape.
M can be defined by M=({q0,q1,....q12}{0,1},{0,,2,b},,q0,b,{q12}) where is defined by table givenbelow:
SHREEDEVIPRAMOD,CSE,BRCE 114
ADDITIONAL PROBLEMS
1. Design a Turing machine to obtain complement of a binary number.
IDEA OF CONSTRUCTION:
1) If symbol is 0 change it to 1, move read write head to RIGHT
2) If symbol is 1 change it to 0, move read write head to RIGHT
3) Symbol is b (blank) don’t change, move read write head to RIGHT, and HALT.
The construction is made by defining moves in the following manner:
(a) ql is the initial state. On scanning 1, no change in state and write 0 and move head to RIGHT.
(c) If M is in state q1and scans blank, it enters q2 and writes b move to right.
(d) q2 is the only accepting state.
Symbolically, M=({ql,q2},{1,0,b},{1,0,b},,ql,b,{q2}) Where is defined by:
2. Design a TM that converts binary number into its 2’s complement representation.
IDEA OF CONSTRUCTION:
Read input from left to right until right end blank is scanned.
Begin scan from right to left keep symbols as it is until 1 found on input file.
SHREEDEVIPRAMOD,CSE,BRCE 115
If 1 found on input file, move head to left one cell without changing input.
Now until left end blank is scanned, change al 1’s to 0 and 0’s to 1.
We require the following moves:
(a) Let q1 be initial state, until blank is scanned, move head to RIGHT without changing
anything. On scanning blank, move head to RIGHT change state to q 2 without changing the
content of input.
(b) If q2 is the state, until 1 is scanned, move head to LEFT without changing anything. On
reading 1, change state to q3, move head to LEFT without changing input.
(c) If q3 is the state, until blank is scanned, move head to LEFT, if symbol is 0 change to 1,
otherwise if symbol is 1 change to 0.On finding blank change state to q4, move head to LEFT
without Changing input.
(d) q4 is the only accepting state.
We construct a TM M as follows:
M = (Q, Σ,, , q0,b, F)
Q = {q1,q2,q3,q4}
F = {q4 }
Σ = {0,1}
𝚪= {0,1,b}
SHREEDEVIPRAMOD,CSE,BRCE 116
(d) q4 is the final state, TM halts.
we construct a TM M as follows: M = (Q, Σ, 𝚪, , q0,b, F)
Q = {q1,q2,q3,q4}
F = {q4}
Σ = { b,1}
𝚪= {1,b}
SHREEDEVIPRAMOD,CSE,BRCE 117
Now at q0 there are two cases:
1. If 0’s and 1’s are found on input , it repeats the matching cycle just described.
2. If X’s and Y’s are found on input, then it changes all the 0’s to X and all the 1’s to Y’s.
The input was a palindrome of even length, Thus, state changed to q6.
4. If state is q3 or q4
If X’s and Y’s are found on input, it concludes that: The input was a palindrome of odd
length, thus, state changed to q6.
We construct a TM M as follows:
M = (Q, Σ, 𝚪, , q0,b,F)
Q = {q0,q1,q2,q3,q4,q5,q6}
F = {q6}
Σ = { b,1,0}
𝚪 = {X,Y,b}
PRACTICE PROBLEMS
1. Design a Turing machine to replace al a’s with X and al b’s with Y.
2. Design a Turing machine to accept anbm n>m.
3. Design a Turing machine to accept anbn n<m.
4. Design a Turing machine to accept (0+1)*00(0+1)* .
5. Design a Turing machine to increment a given input.
6. Design a Turing machine to decrement a given input.
7. Design a Turing machine to subtract two unary numbers.
8. Design a Turing machine to multiply two unary numbers.
9. Design a Turing machine to accept a string 0’s followed by a 1.
SHREEDEVIPRAMOD,CSE,BRCE 118
10. Design a Turing machine to verify if the given binary number is an even number or not.
11. Design a Turing machine to shift the given input by one cell to left.
12. Design a Turing machine to shift the given input to the right by one cell .
13. Design a Turing machine to rotate a given input by one cell.
14. Design a Turing machine to erase the tape.
15. Design a Turing machine to accept anbncn .
16. Design a Turing machine to accept any string of a’s & b’s with equal number of a’s & b’s.
17. Design a Turing machine to accept anb2n.
18. Design a Turing machine to accept anbkcm: where n=m+k.
19. Design a Turing machine to accept anbkcm: where m=n+k.
20. Design a Turing machine to accept anbkcm: where k=m+n.
SHREEDEVIPRAMOD,CSE,BRCE 119
Module 5
Church-Turing thesis-1936
• Any algorithmic procedure that can be carried out by a human or a computer, can also be
carried out by a Turing machine.
• Now it is universally accepted by computer scientists that TM is a Mathematical model of an
algorithm.
• TM has an algorithm and an algorithm has a TM. If there is an algorithm problem is
decidable, TMsolves that problem
• The statement of the thesis –
“ Every function which would naturally be regarded as computable can be
computed by a Turing machine”
Implies
• Any mechanical computation can be performed by a TM
• For every computable problem there is a TM
• If there is no TM that decides P there is no algorithm that can solve problem P.
• In our general life, we have several problems and some of these have solutions, but some have
not, we simply say a problem is decidable if there is a solution otherwise undecidable.
example:
• Does Sun rises in the East? YES
• Will tomorrow be a rainy day ? ( YES/NO ? )
SHREEDEVIPRAMOD,CSE,BRCE 120
Decidable and Undecidable Languages
• A problem is said to be decidable if its language is recursive OR it has solution.
Example:
Decidable :
-Does FSM accept regular language?
- is the power of NFA and DFA same
Undecidable:
- For a given CFG is L(G) ambiguous?
L is Turing decidable (or just decidable) if there exists a Turing machine Mthat accepts all strings in L and
rejects all strings not in L. Note that by rejection means that the machine halts after a finite number of
steps and announces that the input string is not acceptable.
1. (Recursive)
TMs that always halt, no matter accepting or
non-accepting DECIDABLE PROBLEMS
2. (Recursively enumerable)
TMs that are guaranteed to halt only on acceptance.
If non-accepting, it may or may not halt (i.e., could loop forever).
• Undecidable problems are those that are not recursive
Recursive languages
A Language L over the alphabet∑ is called recursive if there is a TM M that accepts every word in L and
rejects every word in L’
Accept (M)=L
Reject(M)=L’ loop(M)=
ø Example: b(a+b)*
SHREEDEVIPRAMOD,CSE,BRCE 121
Recursively Enumerable Languages closed under complementation? (NO)
SHREEDEVIPRAMOD,CSE,BRCE 122
1. Prove that Recursive Languagess are closed under Union
SHREEDEVIPRAMOD,CSE,BRCE 123
2. Prove that
3. Prove that
SHREEDEVIPRAMOD,CSE,BRCE 124
SHREEDEVIPRAMOD,CSE,BRCE 125
PCP is a combinatorial problem formulated by Emil Post in 1946. This problem has many applications
in the field theory of formal languages. A correspondence system P is a finite set of ordered pairs of
non empty strings over some alphabet. Let A w1, w2 ,…, wn B v1, v2 ,…, vn
wi wj wk v iv j vk
Index wj Vi
1 100 001
2 11 111
3 111 11
Let W = w2w1w3=v2v1v3 = 11100111 we have got a solution. But we may not get solution always
for various other combinations and strings of different length. Hence PCP is undecidable.
w1wi wj wk v1vi v j vk
If the index start with 1 and then any other sequence then it is called MPCP
SHREEDEVIPRAMOD,CSE,BRCE 126
• Generality: the algorithm should be applicable to all problems of a similar
form
Comparing Algorithms (While comparing two algorithm we use time and space complexities)
⚫ Time complexity
⚫ Space complexity
◦ Best case
◦ Worst case
Average case
Asymptotic analysis
⚫ The big-Oh notation is used widely to characterize running times and space bounds
⚫ The big-Oh notation allows us to ignore constant factors and lower order terms and focus on
the main components of a function which affect its growth
⚫ Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants
c and n0 such that
f(n) cg(n) for n n0
⚫ Example: 2n + 10 is O(n)
◦ 2n + 10 cn
◦ (c 2) n 10
◦ n 10/(c 2)
It is true for c = 3 and n0 = 10
7n-2 is O(n)
need c > 0 and n0 1 such that 7n-2 c•n for n n0
this is true for c = 7 and n0 = 1
f(n)= O(g(n)) iff there exist positive constants c and n0 such that f(n) ≤ cg(n) for all n ≥ n0
O-notation to give an upper bound on a function
SHREEDEVIPRAMOD,CSE,BRCE 127
Omega provides an asymptotic lower bound on a function.
⚫ The big-Oh notation gives an upper bound on the growth rate of a function
⚫ The statement “f(n) is O(g(n))” means that the growth rate of f(n) is no more than the
growth rate of g(n)
⚫ We can use the big-Oh notation to rank functions according to their growth rate
f n a a n a n2 ... a nd
0 1 2 d
Class P Problems:
P stands for deterministic polynomial time. A deterministic machine at each time executes an
instruction. Depending on instruction, it then goes to next state which is unique. Hence time
complexity of DTM is the maximum number of moves made by M is processing any input string of
length n, taken over all input of length n.
⚫ The class P consists of those problems that are solvable in polynomial time.
⚫ More specifically, they are problems that can be solved in time O(nk) for some constant k,
where n is the size of the input to the problem
⚫ The key is that n is the size of input
Def: A language L is said to be in class P if there exists a DTM M such that M is of time complexity
P(n) for some polynomial P and M accepts L.
SHREEDEVIPRAMOD,CSE,BRCE 128
Class NP Problems
Def: A language L is in class NP if there is a nondeterministic TM such that M is of time complexity
P(n) for some polynomial P and M accepts L.
⚫ NP is not the same as non-polynomial complexity/running time. NP does not stand for not
polynomial.
⚫ NP = Non-Deterministic polynomial time
⚫ NP means verifiable in polynomial time
⚫ Verifiable?
◦ If we are somehow given a ‘certificate’ of a solution we can verify the legitimacy in
polynomial time
Quantum Computers
⚫ Computers are physical objects, and computations are physical processes. What computers
can or cannot compute is determined by the law of physics alone, and not by pure
mathematics. Computation with coherent atomic-scale dynamics.
The behavior of a quantum computer is governed by the laws of quantum mechanics.
⚫ In 1982 Richard Feynmann, a Nobel laurite in physics suggested to build computer based on
uantum mechanics.
⚫ Quantum mechanics arose in the early 1920s, when classical physics could not explain
everything.
⚫ QM will provide tools to fill up the gulf between the small and the relatively complex
systems in physics.
⚫ Bit ( 0 or 1 ) is the fundamental concept of classical computation and information. Classical
computer built from electronic circuits containing wires and gates.
⚫ Quantum bit and quantum circuits which are analogous to bits and circuits. Two possible
states of a qubit (Dirac)are 0 1
⚫ Quantum bit is qubit described mathematically (where (alpha) is complex number)
0 0 1 1
⚫ Qubit can be in infinite number of state other than dirac |0> or |1>
SHREEDEVIPRAMOD,CSE,BRCE 129
SHREEDEVIPRAMOD,CSE,BRCE 130
1. With One tape but Multiple heads: a K head TM has fixed k number of heads and
move of TM depends on the state and the symbol scanned by each head. ( head
can move left, right or stationary).
2. Multidimensional TM: It has finite control but the tape consists of a K-
dimensional array of cells infinite in all 2 k directions. Depending on the state and
symbol scanned , the device changes the state, prints a new symbol, and moves its
tape head in one of the 2 k directions, either positively of negatively along one of
the k axes.
3. Non deterministic TM: In the TM for a given state and tape symbol scanned by
the tape head, the machine has a finite number of choices for the next move. Each
choice consists of new state, a tape symbol to print and direction of head motion.
< a b a b a >
Left EM Right EM
Two extra symbols < and > are used left end marker and right
end marker.
Input lies between these markers
SHREEDEVIPRAMOD,CSE,BRCE 131