TOC Full Syllabus Notes
TOC Full Syllabus Notes
Theory of Computation :-
Purpose of Toc
Language
Eg. L= {a,ab,aab}
Grammar
Set of Rules are called as Grammar. Grammar is a four tuple entity denoted by G
(N,T,P,S).
Those strings which are made by following given set of rules are said to be valid
and other are said to be invalid.
Consider a Grammar
S aA
S bB
A aA/ a
B bB /b
Non-Terminal symbols are not a part of string but they help in generating
strings.
Every grammar has starting symbol(non-terminal), usually we denote it by
‘S’.
S aA
S bB
A aA/ a
B bB /b
S aA
aa (1)
S aA
aaA
aaa (2)
S aA
aaA
aaaA
aaaa (3)
S bB
bb (4)
bB
bbB
bbb (5)
L= {aa, bb, aaa,bbb, aaaa,bbbb,……..}
This grammar generates either the String of ‘a’ or String of ‘b’ in which no. of a
should be atleast 2 and no. of b should be at least 2 in number.
# Consider a grammar: -
S aSb
S aSb
ab ab (1)
aSb
a(aSb)b
a (ab)b = aabb
aSb
a(aSb)b
aaaSbbb
aaabbb
aaabbb
Language = { ,ab,aabb,aaabbb,------}
Or
an bn , n 0
L={,a,b,aa,bb,aba,bab,aaa,bbb,aaaa,bbbb,abba,baab, aabbaa…}
L={c,aca,……….}
Unrestricted Grammar
This grammar is known by the name phrase structure grammar and most general
grammar.
Example: -
(1) SaAa B
(2) A aB
(3) a AB (not type0)
Context-sensitive grammar
Example: -
Aa bB
S Ab
Aba aA (not type 1,type 0)
Context free-grammar: -
Condition:
Example: - A
Regular grammar: -
Condition: -
Left hand side should contain only single non-terminal and right hand side should
contain a terminal or along with terminal we can use one non-
terminal or only signal terminal.
Example: -
A B
A
A aaA
A Aaa
A aAa
A aaaAb
Special points
But vice versa of above three statement may or may not be true.
Type 3 Type 2 Type 1 Type 0
Layout of different language
Question: -
Answer
Here grammar is not given, here directly language is given, and they ask whether
this language is regular or not.
And second way is to generate grammar for given language which is difficult to
generate.
Regular symbols
1) *
2)
3) ab
4)
Regular expression
a* = { ,a, aa,aaa,aaaa,aaaaa------------------------ }.
a+: - (plus)
+ can we have any value from 1 to
a+b :-
a + b = { a,b }
a + b a b a, b.
Exercise: - (a + b).(a + b) = ?
Precedence order:-
Any language consists of only two characters then how many two length string can
be generated by this regular expression: -
(a + b). (a + b)
2c1 * 2c1 22 4
n = 2
Meaning of this is in your alphabet set there exist 2 alphabet
Question
Answer
2n
(a + b) * =??
(a + b)0 =
(a + b)1 = {a, b}
(a + b) . (a + b) = {aa, bb,ba,ab}
To answer this type of question, if we can find anyone one string with is
uncommon in between two regular expressions then surely we can say both regular
expression is not equal.
But systematic procedure says first draw the DFA for both the regular expression
then check whether both DFA are equivalent or not.
If both are equivalent it implies both the regular expression are equivalent
otherwise it will not be equivalent.
Exercise
a (a+b) * = (aa+ab) * check whether this two regular expression are equivalent??
Answer: -
cannot be generated by a(a+b) * but (aa+ab) * will generate . Therefore, both are
not equivalent.
Exercise: -
(a* b*)* = (a+b)* check whether both the regular expression are equivalent or not??
Answer: -
Yes, they both are equivalent.
Answer: -
Exercise:
a(a+b) * = (aa+ab) + check whether both the regular expression are equivalent or
not??
Answer: -
Exercise
a (a*+b*) = aa*+ab* , check whether both the regular expression are equivalent or
not??
Answer:-
Property
Exercise
Answer
Regular symbol:
Some properties of this Regular Symbol
a.
a. = a
{}
a+ a
{} Empty language/set
null string Empty string
Try it: -
Answer: -
(c) option
(d) option
Question: -
an bn, n 0
Answer
Question
(ab)n, n 0
Answer: -
Yes, given language is regular because there exists regular expression for it i.e.
(ab)*. Therefore, it lies under regular language.
Now another way to check whether our language is regular or not. With the help of
finite automata.
DFA:
DFA stands for deterministic finite automata. DFA consist of set of finite state, in
this finite automata we have one initial state and having zero or more than zero
final state.
DFA is a language acceptor, it only accepts the string, it can’t able to generate
string.
DFA configuration: -
M ( q0, F)
Total Function
A state with arrow is known as Initial state and a state with double circle is
known as Final state.
Language generated by given NFA = {aa,bb,aba, bab,_ _ _ _ _ _ _ _so on}
Means b* not b+
If there exist cycle in our DFA, and there exist at least one final state,
which is reachable from initial state then language accepted by this
DFA is infinite.
Regular expression generated by given DFA:
(ab*a + ba*b) (a + b) *
If there exist no cycle in our DFA, then that DFA accept finite language , it will
never accept infinite language.
Every finite language is regular. But infinite language may or may not be
regular.
Question
Design a minimize DFA for a language over a and b contain at least one a
Solution: -
Question
Design a DFA for a language over a and b contain at least two a’s
Solution
Property of DFA
For every input, there exist exactly one transaction in all-state of finite automata,
then that finite automata are considered to be Deterministic Finite Automata.
L= a(a+b)*
Question
Answer
No, because there exists no transaction for b in initial state i.e. in DFA for every
state there should be exactly one transaction for every input.
L= a(a+b)*
If in any state there is no transaction for any symbol, then from that symbol
we will go to the invalid state / dead state / Trap state / sink state.
Question: -
Design a DFA for a language over a and b contains exactly two a’s?
Solution: -
Design a DFA for a language over a and b contains at most two a’s?
Answer :-
L={lemda,a,b,ab,ba,aa,bb,…….}
If our initial state is final state, then only our language generates
string in DFA.
There exists no transaction for in DFA, but in NFA, there can exist
transaction for also.
Special Points
• Try it:-
Question: -
Design a DFA for a language containing at least one a and at least one b?
Answer: -
L={ab,ba,abb,baa,aba,bab,abababab,abbbbbba.…..}
Question: -
Design a DFA for a language containing at least one a and two b’s?
Answer: -
Answer :-
In other words our language , accept those string in which number of a’s is
divisible by 2.
Question:
Solution:-
q0 state mod 3 = 0
q1 state mod 3 = 1
q2 state mod 3 = 2
Special point
Here in this L = na(w) mod 2 = 0, there are two states in our minimized DFA.
• In general, if our language is
L = na(w) mod n = 0
Then
In our minimized DFA, there will be n states, with one final states and (n-1) states
as non-final State.
• Suppose we have following state: -
L = na(w) mod 2 = 1
Question: -
L = na(w) mod 2 = 0
And
nb(w) mod 2 = 0
Question
Design a DFA for a language over a and b where language is given by:-
L = na(w) mod 2 = 0
And
L = nb(w) mod 3 = 0
Solution
q0 is final state
Total number of states we get ( 2*3 ) = 6.
Special point
na(w) mod n = k1
or/and
nb(w) mod m = k2
Question: -
Design a DFA for a language over (a, b) where language is given by:-
(na) mod 2 = 0
L= or
(nb) mod 3 = 0
Answer :-
Here total state is 6 with four final state and two non-final state.
DFA: -
For every input there is exactly one transaction.
NFA: -
More than one or zero transaction for every input.
?? Which one is more powerful?? Powerful in the sense, which finite automata
generate more language then other, NFA or DFA.
Non-Deterministic Deterministic
NFA and DFA both are equivalent in power means every NFA can be
converted into DFA, and every DFA can be converted into NFA.
NPDA is more powerful than DPDA, means NPDA can accept more
language then DPDA. In other words, there are some language for which
only NPDA can be design but PDA can’t be design.
Power between NLBA and DLBA is open question.
Classification of Language
Closure property
Closure means for example, addition of two natural number are closured under
natural number it means that addition of two natural number are always be under
natural number.
But subtraction of two natural number is not closured under natural number it
means that subtraction of two natural number may be natural number or may not
be natural number.
Union
Intersection
Concatenation
Kleen’s closure
Complement
Transpose
Subset operation
Prefix
Suffix
Reversal
Union
Intersection:
Concatenation
Complement
Context-free languages are closured under complement. Complement of
context-free language may or may not be context-free.
Complement of recursive enumerable language is definitely non-recursive
enumerable.
Except this two, all other languages are closured under complement.
There are some language which are not accepted by turning machine.
Finite automata is memory-less-device.
Transpose
Question:
L = an bn, n < 3
Answer
• Every finite language is regular but every regular language is not necessary
to be finite.
Union
L1 = { ab, ba }
L2 = {bab, aba }
and if
L1 = a* b*
L2 = b* a*
L1L2 = L1 + L2 = a* b* + b* a*
Case 1:
Answer:-
Regular non-Regular
For example
L1 is Regular because is finite and every finite set /language is regular and
say
L2 = an bn non-regular
an bn = an bn
non-Regular
Next example:
L1 = ( a+b)* Regular
L2 = an bn Non-Regular
L1L2 = ( a+b)*
Regular
Question:
i.e. L1 L2 L3 L4------------------
Example:
Non-Regular
L1 = a* b*
L2 = b* a*
L1 = a* b*
L2 = b* a*
There exist transaction means without reading input we can change our
state. But if your FA contain transaction then it is NFA not DFA.
Special point
NFA configuration
M ( q0, F)
Here
Q set of finite state
set of finite alphabet
Total Function
q0 initial state
F set of finite final state.
: Q () 2Q
Intersection
L1 = a* b*
L2 = b* a*
L1L2 = a*+ b*
Regular
L1 L2 L3 L4 L5 _ _ _ _ _ _ _ _ _
Answer: -
L1 =
L2 = anbn
L1L2 = = Regular
L1 = (a+b)*
L2 = anbn ,
Concatenation: -
Question: -
Answer: -
L1. L2 = regular
Regular Regular
Question
Answer:
L1 = a*b*
L2 = b*a*
At last the automata obtained will be the concatenation of L1 and L2. Here we are
excluding trap states assume NFA for both regular language.
KLEEN’S CLOSURE
L* = L0 L1 L2 L3_ _ _ _ _ _ so on
L0 = {}
L1 = {a,b}
L.L L2 = {aa,ab,ba,bb}
L* = (a+b)*
L!=L*
Kleen’s Closure of regular language is always regular.
It is possible that L* = L ?
Yes it is possible, For Example:
L = a*b*
L* = (a*b*)* = (a+b)*
Here L L*
But here L = {}
L* = {} = L = L*.
First of draw NFA for the Language, then merge all the final state & make it one,
draw transaction from final to initial state and also make transaction from
initial to final state.
Let L = {ab}
NFA for L1
Complement
Lc = * - L
All those string which are the part of * but not a language. In other words, all
those string which are not a part of our language but are the part of its complement.
If L = (a+b)*
then
L =
c
L = a*b*
Now converting the final state of DFA of L into non – final state and non – final
state, then we get desirable complement of language L.
DFA for LC
Remember this step is only and only can be applied to DFA can’t be
applicable to NFA, it may or may not give desirable complement of language.
Question
SUBSET
Regular expressions are not closured under subset. Subset means for example a* is
a subset of a*b*
a* a*b*
an bn a*b*
Question
Transpose
Example:-
A a A Right-linear grammar
B bB
A a All the non-terminal should be in right side.
Next Example: -
A Aa Left-linear grammar
B Ba
A a All the non-terminal should be in left side.
A aA
B Ba linear grammar
C c
Answer :- To find transpose of any grammar, then reverse the production rule of
grammar.
For example: -
If our production rule of grammar is as follows: -
A aA
B bB
A c
Question:
Design a minimized DFA for the language which accept the binary string whose
decimal equivalent is divisible by 5??
Answer
q0 initial state
q4 final state
Question
Answer
Question
How many states are exist in minimized DFA for the language accepted by binary
string whose decimal equivalent is divisible by n
Answer
Question
Regular expression:- a+ b+
Question:
Answer
Regular expression: - a* b*
Question
Answer
DFA for L = an bm ck.
Question
Answer
Question:
Design a minimized DFA which accept all the string over a and b.
Answer
Question:
Invalid
Remove a and b and make it non final state.
Question:
Answer
Invalid
Regular expression of above DFA = (a+b)*
Question
Design a minimized DFA whose string start and end with same symbol?? With
atleast two character or also accept empty string.
Answer
Question
Design a minimized DFA whose string start and end with same symbol??
Answer
Question
Design a minimized DFA whose string start and end with different symbol??
Answer
Question
Answer
Way, to draw minimized DFA directly, find out minimum length string which can
be accepted our language, and think how to make in general.
Question
Conclusion: -
Question
Construct a dfa which accept set of all string over { a,b } such that second symbol
from LHS is b
Solution
Question
Answer
Question
Design a minimized DFA which accept set of all string over { a,b } such that
length of string should be divisible by 2
Answer
Question
Answer
Question
Answer
Question
Answer
Question
L = | an | n 0 and n! = 3 and n! = 5
Answer
Answer
Question
Answer
In general, number of states = count (aaa) + 1
=4
Question
Answer
Every a is followed by b
Question
Answer
Question
Answer
Question
Every a is followed by bb
Answer
Question
Answer
Question
Solution
Answer
Total 10 states.
Question
Design a minimized DFA in which all string should have following property
All string containing 00 but does not contain 000.
Answer
Question
Answer
Question:
Answer
By default decimal equivalent is divisible by = n+1 .
Question
a* b* + b* a*
Answer
Question
Question
Find out how many states exist in minimized DFA for the regular expression.
( ab + b )*
Answer
Question
How many number of states are there in the minimize DFA that accepts the
following language??
L = { a3(n+1) | n 0 }
Solution
L = { a3 ,a6, a9 -------------------}
Question
Find the number of states are there in the minimize DFA for the regular expression.
a+ b+ + b+ a+
Solution
Question
Design a minimized DFA with exactly two a’s and more than two b’s.
Answer
1
Total number of states = 13
Question
Design a minimized DFA with at least one a and exactly two b’s.
Answer
Question
Construct a DFA which accept set of all string over {a,b} such that second symbol
from RHS is ‘a’.
Solution
No. of states = 2n
Prefix:
If we have drawn NFA then make all state as final state and if we drawn DFA then
make all states as final states except dead state.
Question
If all the states of DFA over {a, b} are final state then string accepted by this DFA
is
Answer
Special point:-
dfa
Suffix
• Every string, first suffix is . Write in right to left fashion but read it in left to
right fashion.
L ={ abb }
Suffix (L) = { , b, bb, abb}
Draw transaction from initial state to every remaining state of DFA, other than
trap state if we have drawn DFA.
Try this
Answer
C
Reversal
L = {w | w L}
LR = {wR | w L}
For example: -
If L = {a+ b+}
LR = {b+ a+}
Regular Language are closured under reversal.
Step 1
Suppose language L = a* b*
To draw reversal of nfa change the direction of all transaction and make initial
state as final state and final state as initial
If there are multiple initial or final states, make them one by transaction.
One more example
Right quotient: -
Let L1 and L2 be language on the same alphabet, then right quotient of L1 with L2
is defined as
To form right quotient of L1 and L2, we take all the strings in L1 that have a suffix
belonging to L2.
Example
L1 = {an bm | n 1, m 0} {ba}
L2 = { bm : m 1 }
L1 /L2 = {an bm | n 1, m 0 }
Left quotient: -
I. L = an bn , n 0
II. L = am bn , n, m 0
III. L = am bn , m! = 2n
It is non-regular language
If relationship is there and if that relationship is valid for infinite, then that
language will not be Regular Language.
It is non-regular language.
w Set of string
wr Reversal of w
aabcba L
abbacabba L
V. L = wxwr | w *
Answer
Explanation: -
VI. L = wxwr , w +
, = (a,b)
x *
Answer
Here we didn’t need to insure equality, here we only want to insure that starting
symbol & ending symbol should be same, this feature can be insuring by finite
automata
Regular expression for above grammar look like this: -
R = a(a+b) + a + b(a+b) * b
VII. L = an c bn , n 0
Answer
It is non-regular language.
VIII. L = an x bn , x *
n 1
Answer
Answer
Non-regular language
X. L = 0n 0n , n 0
Answer:
L = 02n
It means that string accepted by given language are even number of 0’s.
It’s corresponding regular expression is (00) *, because there is a pattern
which easily can be seen in dfa/nfa.
If length of string is in AP, then language will be regular.
Answer
Answer: -
Non-regular language.
Answer
Non-regular language.
Answer
In above language there is no fix pattern between strings of this language. Now
finding Kleen’s closure of above language: -
Kleene closure of L is L*
i.e. L* = L0 L1 L2 L3 L4------------------ L8
L0 = { }
L = {aa, aaa, aaaaa, a7 ,a11 ---------------- }
1
Kleene closure of this language will, will not contain single a but contain all the
strings over a except single a.
R = + aa+
XV. L = xwwr , x, w +
, (a+b)
Non-regular.
XVI. L = 02 | i 0
Above language is non-regular language, because distance between two
consecutive strings is not in A.P, it is in GP. Therefore above language is
non-regular.
XVII. L = ai bj | i + j = 5
i+j 5
i+j 5
ai bj | i+j 5 all are regular
i+j5
i+j 5
XVIII. L = ai bj | i - j = 5
Answer
Non-regular language because above language is producing infinite string and
having equality
non-regular language.
i+j 5
i+j 5
ai bj i+j 5 all are non-regular.
i+j5
i+j 5
Try this:-
Question no. 53, CS-2008
Answer (a)
Question
How many different DFA’s are possible with two states and two inputs??
Answer
a’s self-loop or change the state when a come and same situation for b.
Total = 4 4 = 16
16 + 16 + 16 + 16 = 64
Question
How many different DFA’s are possible with three inputs and two states??
Answer
Total = 64
Question
How many different NFA are possible with two states and two inputs??
Answer
(2)
(3)
(4)
a
a
a b
q0 qj
b
b
One more Example:
a b a
q1 q2 q2
a
b a
b
q1 q2
q3
a
b
a b
a
b
b b
b
a
a
b a
b
b
bb
b
b
Step II : Draw transaction table for NFA consider the following NFA:
a
b
b
a
q0
q1 a,b
q2
a b
q0 q0 q1
q1 q2 q1, q2
q2 q2 q0
Now corresponding transaction table for dfa.
a b
q0 q0 q1
q1 q2 q12
q22
q2 q0
q1222
q2 q012
q01222
q02 q012
q1, q2 q12, merge the subscript and order should be maintained in subscript
(sorted).
How many states are possible in DFA with n states to NFA, maximum
number of status in our DFA will be-
(a) 3n (b) 2n
(c) 4n (d) 5n
Ans. 0
q0
0 q1 0 q2
1
1
q3 q4
a/0 b/1
q2
q3 q4
q4
0
1
q0 q01 q03
Special Point:
(i) represent trap state.
Input: DFA
Output: Minimized DFA
Step 1 Draw a table for all pairs of states (Qi, Qj) not necessarily connected
directly [All are unmarked initially]
Step 2 Consider every state pair (Qi, Qj) in the DFA where Qi ∈ F and Qj ∉ F or
vice versa and mark them. [Here F is the set of final states].
Step 3 Repeat this step until we cannot mark anymore states −
If there is an unmarked pair (Qi, Qj), mark it if the pair {δ(Qi, A), δ (Qi,
A)} is marked for some input alphabet.
Step 4 Combine all the unmarked pair (Qi, Qj) and make them a single state in
the reduced DFA.
Example
a b c D e f
b
c ✔ ✔
d ✔ ✔
e ✔ ✔
f ✔ ✔ ✔
Step 3 − We will try to mark the state pairs, with green colored check mark,
transitively. If we input 1 to state ‘a’ and ‘f’, it will go to state ‘c’ and ‘f’
respectively. (c, f) is already marked, hence we will mark pair (a, f). Now, we
input 1 to state ‘b’ and ‘f’; it will go to state ‘d’ and ‘f’ respectively. (d, f) is
already marked, hence we will mark pair (b, f).
a b c D e f
c ✔ ✔
d ✔ ✔
e ✔ ✔
f ✔ ✔ ✔ ✔ ✔
After step 3, we have got state combinations {a, b} {c, d} {c, e} {d, e} that are
unmarked.
So the final minimized DFA will contain three states {f}, {a, b} and {c, d, e}
If X and Y are two states in a DFA, we can combine these two states into {X, Y}
if they are not distinguishable.
Two states are distinguishable, if there is at least one string S, such that one of δ
(X, S) and δ (Y, S) is accepting and another is not accepting.
Hence, a DFA is minimal if and only if all the states are distinguishable.
Algorithm
Step 1 All the states Q are divided in two partitions − final states and non-final
states and are denoted by P0. All the states in a partition are 0thequivalent.
Take a counter k and initialize it with 0.
Step 2 Increment k by 1. For each partition in Pk, divide the states in Pk into two
partitions if they are k-distinguishable. Two states within this partition X
and Y are k-distinguishable if there is an input S such that δ(X,
S) and δ(Y, S)are (k-1)-distinguishable.
Step 3 If Pk ≠ Pk-1, repeat Step 2, otherwise go to Step 4.
Combine kth equivalent sets and make them the new states of the reduced
Step 4
DFA.
Example
Q δ(q,0) δ(q,1)
a b c
b a d
c e f
d e f
e e f
f f f
Let us apply above algorithm to the above DFA −
P0 = {c,d,e}, {a,b,f}
// check for states a,f for input 1, output is c,f which are not belonging in same set in P0
b,f for input 1,output is d,f which are not belonging in same set in P0 //
P1 = {c,d,e}, {a,b},{f}
P2 = {c,d,e}, {a,b},{f}.
Hence, P1 = P2.
There are three states in the reduced DFA. The reduced DFA is as follows −
Q δ(q,0) δ(q,1)
Try This
Qus. CS-2007
74.
Ans. (c)
75.
Ans. (b)
Finite automata may have outputs corresponding to each transition. There are two
types of finite state machines that generate output −
Mealy Machine
Moore Machine
Mealy Machine
A Mealy Machine is an FSM whose output depends on the present state as well as
the present input.
q0 is the initial state from where any input is processed (q0 ∈ Q).
Moore Machine
Moore machine is an FSM whose outputs depend on only the present state.
q0 is the initial state from where any input is processed (q0 ∈ Q).
Output depends both upon Output depends only upon the present state.
present state and present
input.
Generally, it has fewer states Generally, it has more states than Mealy
than Moore Machine. Machine.
Output changes at the clock Input change can cause change in output
edges. change as soon as logic is done.
Mealy machines react faster In Moore machines, more logic is needed to
to inputs. decode the outputs since it has more circuit
delays.
Algorithm
Input: Moore Machine
Output: Mealy Machine
Step 1 Take a blank Mealy Machine transition table format.
Step 2 Copy all the Moore Machine transition states into this table format..
Step 3 Check the present states and their corresponding outputs in the Moore
Machine state table; if for a state Qi output is m, copy it into the output
columns of the Mealy Machine state table wherever Qi appears in the
next state..
Example
Let us consider the following Moore machine −
Next State
Present State Output
a=0 a=1
→a d b 1
b a d 0
c c c 0
d b a 1
Step 1 & 2
Next State
→a D b
B A d
c C c
d B a
Step 3
Next State
⇒a D 1 b 0
b A 1 d 1
c C 0 c 0
d B 0 a 1
Example
Let us consider the following Mealy Machine −
Next State
→a D 0 b 1
b A 1 d 0
c C 1 c 0
d b 0 a 1
Here, states ‘a’ and ‘d’ give only 1 and 0 outputs respectively, so we retain states
‘a’ and ‘d’. But states ‘b’ and ‘c’ produce different outputs (1 and 0). So, we
divide b into b0, b1 and c into c0, c1.
Next State
Present State Output
a=0 a=1
→a d b1 1
b0 a d 0
b1 a d 1
c0 c1 C0 0
c1 c1 C0 1
d b0 a 0
a a b
b
b
q0 q1 q2
a B
S A
First Step :
A aA |bB
BbB|aB|
There will be 3 states, why states? Because there are three non-terminal in
our grammar.
All those state which generates terminals, after visiting that terminal go to
the final state.
b
a
a a
q0 b
S A B
b
S aA
S bB
A aA
A bB
B aB
B bB/Lemda
b
a
a a
S
A B
S b B
A
B
Try This :
IT-2004
Solution :
a
a
A b B
A b B
A aA | bB
B aB
B bA |
Context-Free Language :
Ans. PDA acts like finite automata, means it will accepts only regular
language.
If we are capable to accept this language with the help of stack logic, then this
language lies under context-free.
Stack Logic :
Push w onto the stack, then do nothing when c come, then after this if input contain
b then stack should also contain 'b'. Then pop the b by doing this if string is empty
then our stack should also be empty.
Qus. 19 Qus.40
CS-2006 CS-2010
abbbba
When to push & when to pop there is a confusion and if someone till that length of
w is 3 then we can insure, therefore this language is context free 0.
L = wwr
Context-free language
DCFL NCFL
Where it is clear from where is to pop and from where to push, then that
language lies under DCFL.
Ans. L = anbmck | n = m or n = k
abbabb
Given language is not a context free language because we can't insure this by stack
logic.
abb abb
w | w
PDA with finite capacity of stack length, it is will accept only regular
language.
How to Design PDA for A Context Free Language:
When we design actual PDA, stack never be empty, it contain special symbol i.e.
z0.
a, z0|az0 b, a/
q0 a, a/ , z0|z0
, z0|z0
PDA do not have any trap state and there is no concept of minimum state
of PDA.
a a b b
a a a
z0 z0 z0 z0 z0
Stack Implementation
b, a/ q1 q
, z0|z0 2
q0
a, a | aaa , z0|z0
a, z0|az0
b, a/ q1 , a/ q2 , z0|z0
q0 q
3
a, a | aaa
, a/
, z0|z0
Language of palindrome is NCFL and its complement is also NCFL.
Ans.
a, z0|az0 b, a/
b, z0/bz0 a, b|
q0 , z0|z0
c, a|a
a, a | aa c, z0|z0
a, b | ab c, b|b
b, a | ba
Push-down Automata
8) (q0, c, a) (q1, a)
9) (q0, c, b) (q1, b)
where u reached
Qus. IT-2006
Ans. (c)
Solution :
a, a/
a, z0|az0
a, a|aa , z0|z0
a, a|
b, b|bb
b, b| b, b|
b,a|ba
a, b|ab
CONTEXT-FREE-GRAMMAR:
S aAB
non-terminal
E E+E
E E*E
E id
Here T = {+, *, id} [Terminal]
N = {E} [Non-Terminals]
S = {E}
Starting symbol
E E+E
id + E
Left-Most 5 steps are required to drive
id + E * E string from this grammar.
Derivation
id + id * E
id + id * id
Sentential form
RIGHT-MOST DERIVATION:
E E+E
E+E*E
5 steps
E + E * id
E + id * id
id + id * id
E E+E E
E+E*E
E + E
E + E * id
E + id * id E * E
id
id + id * id
id id
Parse Tree
E E+E
E E*E
E id
Special Points :
S SS
S
String
S
S
S S
S
S
| |
|
S S
| |
(1) (2)
Ambiguous Grammar :
The term 'ambiguous' is used only for grammar, not for the language,
because there exists multiple grammar for a single language.
But for some language there exist no unambiguous grammar, that language
is known as inherently ambiguous language.
No, because for every regular language, there exist a DFA & every grammar
which are designed by DFA will never be ambiguous.
Ans. S aSbS
S bSaS
S E0
S
S
a S b a S b S
S
|
E0
E0
b S a S a b S
S
| | |
|
E0 E0 E0
E0
abab abab
Ambiguous Grammar
L = anbkcm | n = k or n = m n 0, m, k 0.
Aa
Aa
It is in Chomsky Normal Form
Bb
S
II EXAMPLE:
SAB
S S A
S
S
Aa
Ab
Above grammar is not in Chomsky Normal Form because starting symbol contains
, no problem but S occurs in R.H.S. in some production rule, that’s why our
grammar is not in Chomsky Normal Form.
III EXAMPLE:
SAB
Aa |
Bb
There is some algorithm which can be applied on the grammar when your
grammar is in CNF.
Step II: Remove all unit production from the production set. {A->B}
Step III: Remove all the production rule involving the redundant non-
terminal.
Case II: When that symbol is not reachable from starting symbol.
SAXY
SAS1
S1XY
SbBb
AaA
Aa
BbB
Bb
Case 2
SbBb
AaA
Aa
BbB
Bb
Fa
Eb
Those production rule which are already in Chomsky Normal Form write as its-
Aa
Bb
This two production rule is already is in Chomsky Normal Form so don't change
anything in that production rules.
SFAF
SEBE
SFS1
S1AF
SEB1
B1BE
AFA
Aa
Bb
Aa$*
a belongs T , $ belongs V*
T Set of Terminals.
V Set of Non-Terminals.
A-aBB, A-> aBCC….., B->b…..
SAB
SBB
Aa
Bb
(aa) S S S
(ab) (bb) (bb)
(aab
)
aabb
b (S)
It uses dynamic paradigm to solve the problem. Its complexity is 0(n3) where
n is the length of string.
To draw PDA for any context-free grammar there will be exactly three states in it
(It is fixed).
SAB
ABB
Aa
BAB
Bb
Step I : Push starting symbol S on the top of the stack. When there is a non-
terminal, we will do not read any input ,we will push that non-terminal in
Stack.
(q1, a)
(q1, b)
Whenever there are two option for any non-terminal, there exist NPDA for
it.
If top of the stack contain terminal then to pop that terminal, input should
also contain that terminal.
If top of stack contain a then input should contain a, then we can pop that
terminal from the stack.
(q1, a, a) (q1, )
(q1, b, b) (q1, )
b, b|
, z0|SZ0 , z0|z0 qf
q1
q0
a, a | aaa
, A|BBA
, A/a
Non -deterministic push-down automation
We have to check whether 'ab' string can be generate or not.
ab
z0
CS-2003
51 (c)
CS-2006
32 (d)
PUMPING LEMMA FOR REGULAR LANGUAGES:
For every string having greater than or equal to K where K is some limit it is
possible to divide that string into three parts x, y, z and if.
For example:
a*b* is a regular language, we will take one string of that language aabb.
aabb €L
x y z
xyz EL
a (a)n bb
n=1 aabb
Putting different value of n, we are pumping different string.
a a b b
x y z a (ab)n b L
If string can be pump by pumping lemma then language may be regular may
not be regular.
If string is not pumped, then definitely our language is non-regular.
Ans. anbn | n 0, take one string of this language let us say we take aabb.
a a bb a (a)nbb L (G)
x y z
Here string is not pumping string so pumping string so pumping lemma says above
language is non-regular language.
Why we are saying, if strings are pumped then we can't say about regularity?
Let us take a language of equal numbers of a’sn and equal number of b's.
a ab b
x y z
x (y)nz L
a (ab)n b L
n=1 aabb L
n=2 aababb L
n=3 aababb L
No Comments on regularity.
then,
uvnwxn y L
|v x| > 0, |u v w| p
anbncn will not be pump so given language is not context free language.
Special Points :
NCFL DCFL
(Non-Deterministic (Deterministic
L1 = anbn DCFL
L2 = anb2n DCFL
L1 U L2 = anbm | n = m or m = 2n
NCFL
By this counter example we can say that DCFL are not closured under
union.
Union of two DCFL, it may be DCFL it may not be DCFL, but it never go
outside context-free language.
Regular U DCFL = DCFL
INTERSECTION:
Suppose
L1 L2 = Not a CFL
= anbncn
Context-Free Language
CONCATENATION:
Qus.31 IT-2006
Ans. (b)
Qus.33 CS-2006
Ans. (b)
Qus.1CS-2011
Ans. (c)
P-=P
Qus. 17 CS-2010
Ans. (b)
Qus. 13 CS-2008
Ans. (d)
1) UNION
2) CONCATINATION
3) INTERSECTION
4) KLEEN'S PLUS
5) KLEEN'S STAR
6) COMPLEMENT
7) REVERSAL
8) PREFIX
9) SUFFIX
10) MIN
11) MAX
12) DIFFERENCE
Operator Grammar :
Operator grammar lies under DCFL and all DCFL can be parse in 0 (n)
where n is the length of string.
There are two rules for grammar to be operator grammar.
1) No production
S AB
S AbB
S AaaB
All this production rules are allowed.
Sa
S ba
SIMPLE GRAMMAR :
Simple grammar also generates only DCFL. This grammar will never be
ambiguous grammar.
This grammar also known as LL (1) grammar.
PROPERTY :
S av*
a T, V Non-terminal
By using S-grammar, string can be parse in 0 (n) time, where n is the length
of our string.
Special Point :
for CNF : 2n - 1
for GNF : n
2) anbncn
2) L = |x # y| x, y (0 + 1)* and x! = y
4) L = |anbm| n m 3 n
If G is CFG then there exist an equivalent grammar that does not contain any
useless symbol.
If G be any context-free grammar with L (G) then there exist an
equivalent grammar G having no production.
If G be any context-free-grammar without production, then there exist a
CFG G' such that does not have any unit production and that is equivalent to
G.
Any context-free grammar with L (G) has an equivalent grammar in
Chomsky Normal Form.
For every context free grammar G with L (G) there exist an equivalent
grammar G in GNF.
Context-free language are not closured under left and right quotient but
under RE it will be closured.
2) CONCATINATION
3) KLEEN STAR
4) KLEEN PLUS
5) PREFIX
6) SUFFIX
7) REVERSAL
8) HOMOMORPHIC
9) INVERSE HOMOMORPHIC
10) SUBSTITUATION
11) CYCLE
4. We have given CSG (Context Sensitive Grammar) and they ask you whether
this grammar generates context-free language or not, this property is
undecidable.
4. L (G1) L (G2)
If G2 Regular Grammar
5. Membership is decidable.
Whether there is any
6. G1 = Regular Grammar common word in between
this two grammar is
G2 = Context Free Grammar decidable
TURNING MACHINE:
, , they are called blank spaces, after input remaining all contain blank spaces.
There exist a read/write head in turning machine, read/write head can move
in any direction but one all at a time.
Reading or writing can be performed on the all with read/write head.
For our convenience, we can assume any initial position of read/write head.
Read/write head
a b b
a b
b
Convert a into blank space and move read/write head towards right.
Now move read/write head to the right until we don't get right most b.
a a b b
Now how we know that right most 'b' we have encountered?
Until we don't get blank space in right most and when we encountered first
blank space right most then take one left at that position we go get right most 'b'.
Now convert that b into blank space and move read head to left and repeat this
procedure again till we get this situation.
Left to the read/write head & right to the read/write blank space (symbol).
a, a|R b, b|R
a, /R
q1 b, b/R q2 , /L q3
q0
, /hault
b, /L
, /R
q
5 q4 a, a/L
b, b/L
Turning machine halt means, at that point onwards, turning machine will
stop.
For input 'ba' turning machine read/write will be crash.
Crash is the form of halting (Halting on some non-final state).
If turning machine is crashed means string is rejected by turning machine.
And if turning machine is halting on some final state, it means string is
accepted by turning machine.
a, /R
Ans.
a, a|R
b, b/h
Ans.
a, a|R , /H
b, b/R
TM for ab string
q0 a, a/R q1 a, a/H
q 2
TM (Turning Machine)
Ans.
a, a/R b, b|R
b, b|R
, /h
, /h
, |h
, |h
TM for almost two a's
LBA
The computation is restricted to the constant bounded area. The input alphabet
contains two special symbols which serve as left end markers and right end
markers which mean the transitions neither move to the left of the left end marker
nor to the right of the right end marker of the tape.
A linear bounded automaton can be defined as an 8-tuple (Q, X, ∑, q0, ML, MR, δ,
F) where −
δ is a transition function which maps each pair (state, tape symbol) to (state,
tape symbol, Constant ‘c’) where c can be 0 or +1 or -1
Configuration of TM
TM is a 7-tuple machine
L (Q, ∑, ┌, δ, q0, □, F)
┌ tape alphabet
□ blank symbol
In TM and PDA, atleast two states are necessary, atleast one is accepting and other
is non-accepting
L= a2^i | i>=0 is also a context sensitive language because there exist a LBA for that
language.
Special point
This turning machine will not be half for the input 01.
Whether turning machine halt or not is undecidable problem. Halting
problem of turning machine is undecidable.
With some constraints whether turning machine will halt is decidable.
Ever halt is undecidable.
How to identity given problem of TM is decidable or not??
Those properties for which we have to run TM provide that there is no constraints
is given, all such properties are undecidable and remaining all other are decidable.
A set is said to be countably infinite set if there exist one to one correspondence
between the set and the set of natural number.
In better words there exist a bijection between the set and set of natural number. Is
set of odd number is countably infinite set?
Bijection insures that fixed positon of element when arrange elements in
specific order.
Set of natural number is countably infinite
Set of even as well as odd number is countable infinite set.
Set of prime number and rational umber is countably infinite
Set of non-decreasing function are countably infinite.
Set of real number is uncountable infinite set.
Explanation
Between in 0 and 0.1 there are infinite number therefore we cannot justify exact
position of 0.1 therefore it is uncountable infinite set.
Unrestricted grammar
Recursive
Recursively enumerable
Recursive language
It means if our language is recursive then there exist a TM that will halt on
every input.
If string belongs to the language it halts on some final state. If string doesn’t
belong to the language it will halt on some non -final state.
Recursive language is also known as turning decidable language and recursive
enumerable is also known as Turning recognizable language.
Finite set is lexicographic enumerable. Yes, all finite set are lexicographical
enumerable. Some infinite set are also enumerating lexicographical
For example
(a+b) * = {, a, b, aa, ab, ----------}. Now someone ask where four a’s will be
present, at what position. 1+2+4+8= 15 position +1 =16 position. According to this
way we can justify the position of any string by this way.
So set of all possible string over a and b is canonical enumerable or in other words
we can say set of all possible string over a and b is countable infinite set.
Countable means there exist a TM which can generate set of all possible string
over a and b.
Try this
Cs-2003 15 ans d
{, {1},{2},{3},---------------infinite}
Here we cannot say where 1st element sets will be terminal and from where second
element will be started.
Means power set of (a+b) * and as (a+b) * is countably infinite and we know that
power set of countably infinite is uncountable.
Set of TM is countable it means we can design a TM which can generate all the
TM and such TM is known as Universal Turning Machine.
Different type of TM
Multi-tape Turing Machines have multiple tapes where each tape is accessed
with a separate head. Each head can move independently of the other heads.
Initially the input is on tape 1 and others are blank. At first, the first tape is
occupied by the input and the other tapes are kept blank. Next, the machine reads
consecutive symbols under its heads and the TM prints a symbol on each tape and
moves its heads.
A Multi-tape Turing machine can be formally described as a 6-tuple (Q, X, B, δ,
q0, F) where −
δ(Qi, [a1, a2, a3,....]) = (Qj, [b1, b2, b3,....], Left_shift or Right_shift)
In a Non-Deterministic Turing Machine, for every state and symbol, there are a
group of actions the TM can have. So, here the transitions are not deterministic.
An input is accepted if there is at least one node of the tree which is an accept
configuration, otherwise it is not accepted.
If all branches of the computational tree halt on all inputs, the non-deterministic
Turing Machine is called a Decider and if for some input, all branches are
rejected, the input is also rejected.
δ is a transition function;
A Turing Machine with a semi-infinite tape has a left end but no right end. The
left end is limited with an end marker.
It is a two-track tape −
Upper track − It represents the cells to the right of the initial head position.
Lower track − It represents the cells to the left of the initial head position
in reverse order.
The infinite length input string is initially written on the tape in contiguous tape
cells.
The machine starts from the initial state q0 and the head scans from the left end
marker ‘End’. In each step, it reads the symbol on the tape under its head. It writes
a new symbol on that tape cell and then it moves the head either into left or right
one tape cell.
It has two special states called accept state and reject state. If at any point of
time it enters into the accepted state, the input is accepted and if it enters into the
reject state, the input is rejected by the TM. In some cases, it continues to run
infinitely without being accepted or rejected for some certain input symbols.
Note − Turing machines with semi-infinite tape are equivalent to standard Turing
machines.
Cs -2011
Special point
We have discussed PDA with single stack but there exist two stack PDA, 2
stack PDA can accept all the language which can be accepted by TM.
If string is in the form of a and b that language go till context sensitive.
Turing machine will involve where function type language is given.
Co-Re
Those languages which are not recursive enumerable but their complement are
recursive enumerable.
Example
L = {w | w belongs L(Ti)}
L = {w | M halts on w}
Example
We have taken string and run on one TM it halts and in other turning machine it
goes into the loop means we can say this property is partially decidable. So given
language is recursively enumerable language.
L ={<M> | L(M) = Φ}
Purely undecidable(RE)
A ∝ B and B ∝ C A ∝ C
Example
S AB
A B
Rank(S)=2
Rank(A)=1
Rank(B)= 0
Language shown below is neither RE nor Co-RE
L ≠Φ
L(M1) ≠ L(M2)
L halts on w
L contain at least eight members.
POST CORRESPONDENCE PROBLEM
We can say that there is a Post Correspondence Solution, if for some i1,
i2,………… ik, where 1 ≤ ij≤ n, the condition xi1…….xik = yi1....yik satisfies.
Example
Find whether the lists
Solution
x1 x2 x3
M Abb aa aaa
N Bba aaa aa
Here,
x2x1x3 = ‘aaabbaaa’
x2x1x3 = y2y1y3
Example 2
Find whether the lists M = (ab, bab, bbaaa) and N = (a, ba, bab) have a Post
Correspondence Solution?
Solution
x1 x2 x3
M ab bab bbaaa
N a ba bab
If at most word exist, then it will never be RE and if atleast word exist it will
be RE.