Theory of Computation
Theory of Computation
Computation (TOC).
Formal Language – It is a collection of string where the meaning of the string is not important BUT the
format of the string is important. For example, strings like abba, aabb, aabbcc etc are part of formal
languages since there is no meaning here but there is a specific format of the strings. Normally, we
represent the combination of a’s and b’s as W.
NOTE – If we want to represent palindrome language, we can write L={W W R } and for string copy
language, we can write L={WW }, where W is a combination of a’s and b’s and W R is the reverse of W .
● Type 3
● Type 2
● Type 1
● Type 0
Type 0 is the superset of all other types and Type 3 is the subset of all other types.
NAME OF MACHINE
TYPE OF LANGUAGE ANOTHER NAME
ACCEPTING THE LANGUAGE
NOTE – Any languages part of T3 are also part of T2 and all T2 are part of T1 and all T1 are part of T0. All
languages are T0 and all machines are Turing Machine.
Expressive Power – The number of languages an automata can accept is called the Expressive Power of
that automata. Basically,
E ( FSM )=1
E ( PDA )=2
E ( LBA ) =3
E ( TM )=4
Example
Here, state S1 has an initialization arrow and thus is the first state. Here, there is no memory or storage.
Now, once the machine stores a single ‘A’, then the state is changed to S2. Now, at S2, if an A comes,
then the state remains unchanged as the pattern is the same. So for all patterns, a, aa, aaa … the final
state is S2 which is indicated by the double circle. So this is the 2 – state automata for language L. Since
this automata has a finite number of states (2), then this is a FSM. Since this FSM is accepting language
L1, then L1 is a Regular Language.
However, we can’t use FSM for all languages. Let us take the example of L={an bn ∨n ≥1 }. In this case,
we can draw the FSM as follows –
This FSM will accept all valid strings like ab, aabb, aaabbb and so on. However, if we have a string abbb,
then also the FSM will accept and reach the final stage S3. String abbb is an invalid string in this language
as the number of a’s and b’s need to be equal. Hence, this language can’t be represented using FSM.
Since FSM can’t represent this language, we need to try PDA.
Push Down Automata (PDA) is basically just FSM/FA in addition to 1 stack. In this case, the current
position of the string is noted. After that, whenever an a is encountered, the a gets pushed to the stack.
If a b is encountered, then an a is popped from the stack. This means that if there are equal number of a
and b’s, then the end state and starting state of the stack will be the same, then the number if a’s and
b’s is the same and therefore, the pattern is part of the language.
NOTE – FSM fails when a comparison is required as it needs some memory element. In those cases, a
stack acts as a memory element and PDA helps.
Now, let us take the case of language L={an bn c n∨n≥ 1 }. To check for this, the PDA can be
implemented using 2 stacks –
● For first stack, whenever a’s occur, push the same to the stack. If a b comes, then pop a out of
Stack 1. This stack checks the number of a’s = number of b’s.
● For second stack, whenever a’s occur, push the same to the stack. If a c comes, then pop a out
of Stack 1. This stack checks the number of a’s = number of c’s
So basically, if S1 and S2 return true, then we can conclude a = b = c and the pattern matches.
NOTE – PDA is effective only if we have 1 comparison. For 2 or more comparisons, we need an
additional stack. This is called the Turing Machine.
Turing Machine is PDA with an additional stack or FSM with 2 additional stacks. All comparisons can be
done using Turing Machine.
Let us take the case of language L={an bn c n d n∨n ≥ 1} . For this, let us consider a Turing Machine which
has 2 stacks – S1 and S2.
At the end, if both the stacks are empty, then the pattern matches as A = B = C = D. This can be further
extended to infinite variables.
Linear Bounded Automate (LBA) is basically a Turing Machine but the size of the stacks are bounded aka
fixed unlike Turing Machine which has infinitely large stacks.
Alphabet – The non – zero and finite set of symbols that are part of a formal language. Represented by
Σ.
String – The collection of symbols over the given alphabet. String lengths can vary from 1 to infinity.
However, there is a special string called Epsilon (ϵ ) also called as Null/Empty string which is a string
with zero length. ϵ is a common string across all formal languages. For a string S, we can write the
following –
ϵ concat S=S
Power of an Alphabet – The nth power of an alphabet is the number of n length strings possible with the
alphabet symbols. For example, if we have Σ={a , b }, then we have –
1
Σ ={a , b }
2
Σ ={aa , ab , ba , bb }
3
Σ ={aaa , aab , aba , abb , baa , bab , bba , bbb }
Let us take another example Σ={ab , cd }. Then here, we have
2
Σ ={abab , abcd , cdab , cdcd }
Complete Language
This is the union of all the power of an alphabet and thus has all the possible strings in the language.
¿ 0 1 2
Σ =Σ ∪ Σ ∪ Σ …
¿ +¿¿
Σ =ϵ + Σ
¿ ¿ ¿
NOTE – A language is a sub – set of Σ . This means that a language can be the same as Σ . Also, Σ and
+¿¿
Σ are called Kleen Closure and Positive closure respectively.
Empty Language – This is a language without any string. That means that if I have a language with just ϵ
as a string, then that language is NOT empty since it still has one string.
● Without Output – The FA will only tell if the language is accepted or not. Also called Language
Recognizer.
o DFA
o NFA
● With Output – It will return some form of output.
o Mealy Machine
o Moore Machine
Regular Language is the language for which a FA can be formed.
The default will be NFA and DFA is considered only if explicitly mentioned. A FA can be denoted as
follows –
FA=(Q , Σ, F , δ , q0 )
Where,
Q – Different states
Σ – The input alphabet
F – Set of final states
δ – Transition Function
q 0 – Starting state
Basically, since this type of FA doesn’t provide any output, it acts as a language recognizer and
therefore, the set of final states is a sub set of the total different states. Therefore,
F ⊆Q
Transition Function – The function that is applied to move from one state to another state. For example,
if we apply function δ with input a to state S 1, then the FA transitions to S 2. Then transition function is
δ.
δ ( S 1 , a ) =S 2
NOTE – Since the FA has five values needed to work, it is also called a 5 – value/5 – tuple machine.
Here, we have
Q= { S 1 , S 2 }
Σ= { a ,b }
F={ S 2 }
q 0=S 1
δ ( S 1 , b )=S 1
δ ( S 1 , a ) =δ ( S 2 , a )=δ ( S 2 , b )=S 2
Let us take another example,
Here, we have
Q= { S 1 , S 2 }
Σ= { a ,b }
F={ S 2 }
q 0=S 1
δ ( S 1 , b )=S 1
δ ( S 2 , b ) =S 2
δ ( S 2 , a ) =S 1 , S 2
In example 1, every transition function was Deterministic and the function could transition to one fixed
state. However, in example 2, we have a Non-Deterministic transition function when S2 is fed a as input.
The final state can be S1 or S2. The FA in examples 1 and 2 are called Deterministic FA (DFA) and Non-
Deterministic FA (NFA) respectively.
In NFA, we can expect more that 1 state from a given state and input symbol however DFA has one and
only one state given the state and input symbol. Thus, DFA is a more specific type of NFA. NFA is the
default FA type (without output). Every DFA is an NFA but NOT every NFA is a DFA. In short –
DFA ⊂ NFA
Question
Construct a minimal DFA that accepts all strings of a’s and b’s where in every string begins with the
character a .
Answer
In this case, the language is basically character a followed by any amounts of combinations of a and b .
So, we can write it as follows –
L= { a ( a+b ) } ={ a Σ }
¿ ¿
Since a+ b means either a or b , then logically, it is the same as Σ . Now, the question demands to design
¿
a minimal DFA. For that, we first need to get the minimal string in the language. For Σ , the minimal
string is ϵ . Therefore, the minimal string for the language will be –
Lmin =a . ϵ=a
Now we have the minimal string. Next, we can construct the DFA. One thumb rule to follow is –
For this automata, if the string starts with a , then the state will change to S1 and after that no matter
which string comes, the state remains at S1. This diagram achieves the required functionality, but it
doesn’t cover the actions to be taken in case a b appears at the beginning. Thus, this is currently a NFA.
Now, the FA is covering all possible cases. This is now a DFA. Now, we can write the transition function
as follows –
δ a b
S0 S1 S2
S1 S1 S1
S2 S2 S2
NOTE – If not explicitly mentioned, assume that the question is referring to a NFA. The difference
between NFA and DFA is simple –
“NFA will accept all correct strings and will not accept invalid strings. DFA will accept all valid strings
and it will reject invalid strings”.
This means that DFA will send invalid strings to a dead state in turn rejecting the string.
Question
strings
Construct minimal DFA for language L={set of all ∧b } .
a
Answer
Here, we have –
{
L= set of all
strings
a }
∧b ={ ( a+b )¿ }= { Σ ¿ }
Therefore, the minimal string will be ϵ . Therefore, we don’t need multiple states and we can design the
FA as –
strings
Construct minimal DFA for language L={set of all ∧b excluding ϵ }.
a
Answer
Here, we have
L=¿
Since we are excluding ϵ , we can say that the minimal string is either a or b . Either way, the length of
the minimal string is 1 and therefore, we need 2 states to implement the FA. The NFA can be
implemented as –
Question
Construct a minimal DFA for language L= { all strings which have lengths divisible by 5 }
Answer
NOTE – In the above case, we can write –
'
Complement of L=L ={all strings which have lengths NOT divisible by 5 }
If L is regular language (can be implemented by DFA), then L’ is also regular. Also, we have
' ¿
L ∪ L =Σ
Also remember, complement of NFA is not possible. To get the complement, we first need to convert
the NFA to DFA and then perform the complement.
Question
Answer
One important thing to note here is that m and n can be zero. So that means the minimal string for this
language is ϵ . This also means that consecutive a ’s or consecutive b ’s are possible. So, the DFA can be
drawn as –
Question
Answer
Again, here the minimal string is ϵ . But one thing to remember here is that for this language, the
number of a’s should be equal to the number of b’s. No matter how much you try, we can never ensure
that the number of a ’s and number of b ’s should be equal. This language can’t be implemented using
NFA or DFA.
This brings us back to the point we mentioned before in Page 3 of this document –
So, we can’t create a FA for this language. Now suppose we have a language as follows –
Question
Construct a minimal DFA for language L= { all strings which have the 2nd symbol ¿ RHS as A }
Answer
Question
Construct a minimal DFA for language L= { all strings which have the 3rd symbol ¿ RHS as A }
Answer
So basically, we have L= { Σ ¿ a Σ 2 } . The minimal DFA is a bit too large and I am too lazy to draw it.
Hence, enjoy a table. Note, GREEN states are final states and Red state is the initial state.
STATE Next symbol is A Next symbol is B
S0 S1 S0
S1 S2 S5
S2 S3 S4
S3 S3 S4
S4 S6 S7
S5 S6 S7
S6 S2 S5
S7 S1 S0
NOTE – If we have a language L to match the nth element from RHS, then the DFA will require 2n states
while NFA will require only n+1 . So, while checking for 3rd element from RHS, the DFA needs 8 states
but the NFA will require only 4 states.
Let us draw the NFA for the language L= { all strings with the2 nd last element as A } –
Now, we need to convert this NFA to a DFA. For that, we need to map the state transitions under all
circumstances –
S0 S0, S1 S0
S1 S2 S2
S2 - -
S0 S0, S1 S0
S0, S2 S0, S1 S0
Now all states and combinations have been taken care of. This is a DFA.
NOTE – If the NFA has n states, then the equivalent DFA will have states ranging from [ 1 , 2n ]. This is
because the number of sub-sets of an n element set vary from [ 1 , 2n ]. Therefore, this method of
converting NFA to DFA is also called subset construction.
Question
Construct a minimal DFA for language L= { all strings where the number of A' s isdivisible by 4 }
Answer
Here, we also need the DFA to match the condition where there are 0 A’s. Therefore, we will need to
keep the starting state as the final state.
Question
L= { all strings where the number of A' s isdivisible by 2∧number of B' sis divisible by 3 }
Answer
'
● L 2={all strings where the number of B s is divisible by 3 }
For now, we have 2 sub-problems and we need a solution that will satisfy both the sub-problems. To do
so, we first form hybrid states – S13, S14, S15, S23, S24, S25.
● For L1, S1 was the initial state. For L2, S3 was the initial state. Hence, S13 is the initial state for
the final solution. Also, L1 and L2 had S1 and S3 as the final states. Hence, S13 will also be the
final state for the final solution.
● For S1, if we apply a the output becomes S2 and if we apply b , the output is S1. For S3, a and b
results in S3 and S4 respectively. Hence,
o S13 on a will result in S23
o S13 on b will result in S14
● Perform the above step for all the hybrid states. This will give us the final DFA as follows –
One thing to note here is that we require both the problems to be satisfied. In short, the final state is a
combination of the final states of both the DFA. In case the question wanted a OR between the two
DFAs (like A’s divisible by 2 OR B’s divisible by 3), then we would have had 4 final states -
{ S13 , S 14 , S15 , S 23}
So, in GATE they will most likely ask the number of states that are required to implement the DFA. In
such cases, here is a step-by-step procedure to find the number of states –
First, check the symbols involved. If the symbols involved are different, then the number of states is the
product of the conditions itself. For example, in our previous problem. We had two symbols – a and b .
Since the symbols involved are different,
Step 2
If the symbols used are same, then things become interesting. Here, we write the number of symbols for
which this DFA should return true. Let us take the example of two languages as shown –
' '
L 1={all strings where the number of A s is divisible by 2∨number of A s is divisible by 4 }
' '
L 2={all strings where the number of A s is divisible by 3∨number of A s is divisible by 7 }
First, lets take the case of L 1. The number of A’s for which L1 will get detected are –
0 , 2 , 4 , 6 , 8 , 10 …
Here, we can see that there is a common difference of 2. Thus, if we have the same symbol and a
consistent common difference, then –
Now, lets move on to L2. The number of A’s for which L2 will get detected are –
0 , 3 , 6 ,7 , 9 ,12 , 14 , 15 ,18 , 21 …
Here, we don’t have a consistent common difference. In such cases, we revert back to Step 1 –
Question
L= { all strings of 0' s∧1' s which have at most 1 pair of 11∧at most 1 pair of 00 }
Answer
Here, we have L={ϵ ,0 , 1 , 00 ,11 ,0011 ,1010101010 … }. Basically, we just don’t have to consider a
pattern where more than 2 0’s or 1’s appear on the same string consecutively. We can create the DFA as
–
S1 S2 S6
S2 S3 S6
S3 S10 S4
S4 S3 S5
S5 S9 S10
S6 S2 S7
S7 S8 S10
S8 S9 S7
S9 S10 S5
This problem is quite complex and it has no method or process to get the DFA. We just need to wing it.
Trial and error (UGH). See beginning of Lecture 12 to get more clarity on how it was done.
Question
How many DFAs can be constructed with two states X and Y and language with alphabet Σ= {0 ,1 } and X
as the initial state?
Answer
We have a language with 2 symbols and a 2 state DFA. One thing we know for sure is that X is the initial
state. Now, we can have 4 main possibilities –
SL. NO X on 0 X on 1 Y on 0 Y on 1
1 X X Y Y
2 X X Y X
3 X X X Y
4 X X X X
5 X Y Y Y
6 X Y Y X
7 X Y X Y
8 X Y X X
9 Y X Y Y
10 Y X Y X
11 Y X X Y
12 Y X X X
13 Y Y Y Y
14 Y Y Y X
15 Y Y X Y
16 Y Y X X
So now, we have 16 possible transitions for each of those 4 possibilities. In short, we get a total of 64
DFAs. In case they hadn’t specified the initial state, we would have had 128 DFAs.
Question
How many DFAs can be constructed with three states X, Y and Z and language with alphabet Σ= {0 ,1 }
and X as the initial state?
Answer
Now we have three states, so writing all the combinations is gonna be super time consuming. So let’s
use MATH 😊
First, the states X, Y and Z can either be final or non – final states. So, we can have a total of 23=8
combinations.
Y 3 3
Z 3 3
Each state under any input can go to either itself or the other two states, thus the possible transitions =
3. In short,
6
Number of DFAs=8∗3 =5832
Question
How many DFAs can be constructed with two states X and Y and language with alphabet Σ= {0 ,1 } and X
as the initial state and which accepts empty language?
Answer
An empty language is accepted by a DFA which doesn’t end in a final state. This happens in two
conditions –
Now, we know from previous problems that there are 16 cases where there are no final states. Next, we
can have 4 possible cases where Y is a final state but X never reaches Y. Therefore, we have a total of 20
DFAs which accept empty language in this case.
Question
How many DFAs can be constructed with two states X and Y and language with alphabet Σ= {0 ,1 } and X
¿
as the initial state and which accept all languages aka Σ ?
Answer
Now, if Both X and Y are final, then no matter what transitions we have we will end up on a final state.
¿
Hence, all those 16 DFAs will accept all languages. Now, coming to option 2. Since we want to accept Σ ,
we need any combination that starts at X to end at X since X is the only final state. Therefore, we need a
self – loop on X. This gives us 4 combinations to work with.
NOTE – In the problem earlier, we checked for empty language and got 20 DFAs. Since we now needed
to check for all languages, we could have just complemented the previous problem. To perform
complement, we just had to swap the final and non – final states. Thus, the number remains the same
aka 20 DFAs.
REGULAR EXPRESSION
Question
L= { all strings of A' s∧B' s which have the same starting∧ending symbols }
Answer
Here,
¿ ¿
ℜ=a+ b+a . ( a+ b ) . a+b . ( a+ b ) . b+ ϵ
Let us explain this further –
● In the expression, we can see that the strings start and end with the same symbol
¿ ¿
● In between, we can have anything aka (a+ b) =Σ
● We also need to include A, B and ϵ as well since these are also part of the language.
NOTE – Minimal RE is not always unique but minimal DFA is ALWAYS unique.
Question
Give the Regular Expression (RE) for language L= { all strings which have no consecutive B ' s }
Answer
● If an A comes in the string, then the string can have any number of A’s after that.
● However, if a B comes in a string, then the next character needs to be an A. So we can consider
BA and element that can come any number of times.
● Now, in the above two expressions, we have a case where the strings always end with an A. So
we need to add another B in the end to account for the cases that end in B
Finally, we have
¿
ℜ=(a+ba) .(ϵ+b)
NOTE – Here are some of the important identities that may be helpful to remember –
Question
Answer
Question
¿ ¿ ¿
Which of the four options are equal to a ( b a ) –
¿¿ ¿
● (a b ) b
¿
● a+ ( a+ba )
¿ ¿
● (a+ b) ba(a+b)
● None
Answer
In Option 2, only b is not allowed and in Option 3, only ϵ is not allowed. Also, we can see that Option 1
is the correct answer.
Question
L= {all strings with alphabet ( a , b , c ) ∈which ever C is followed by exactly 2 A ' s∧every B is followed by A∨CA }
Answer
Here,
¿ ¿ ¿
ℜ=a (caa+ ba a + bcaa)
NFA CONSTRUCTION
Sometimes, getting RE and constructing DFA is quite complex. In those cases, we can create a NFA
instead.
Question
Construct a NFA for language L= { set of all stringswhich contain ABB as a−string }
Answer
Question
Answer
Epsilon – NFA – This is the special kind of NFA that accepts ϵ as well.
NOTE – Let us take the number of possible states as Q . Then, any transition in a DFA will result in one of
the states in Q . However, for a NFA, the transitions can result in any of the states in the Power set of Q
aka P(Q). This means that in DFA, every transition results in a state. However, in NFA we can have
cases where the transition doesn’t go to any state. So, in short, we have –
● DFA - δ :Q × Σ → Q
Q
● NFA - δ :Q × Σ → P ( Q ) ∨2
Q
● Epsilon NFA - δ :Q × ( Σ+ ϵ ) → P ( Q )∨2
Question
Construct a NFA for language L= { all strings with different starting∧ending symbols }
Answer
L= { 0a 1b 2c , where a , b , c ≥ 0 }
Now, let’s convert this ϵ – NFA to a regular NFA –
● Step 1 – Draw the same state arrangement. So, in our case, we start by drawing the regular NFA
with 3 states and q 2 being the final state.
● Step 2 – Get the final states. Any state which can go to final state in the ϵ – NFA by reading just
ϵ is a final state in the regular NFA. Thus, in our case all the three states are final states.
● Step 3 – Now that we have the states and final states, we can proceed with the transitions. This
is the tricky part. Let’s deep this.
Let us take the example of Q0. In the ϵ – NFA, we can see that when 0 enters Q0, the control stays at
Q0. However, when the input is something like 0, ϵ , then the control shifts to Q1. And if the input is
something like 0,ϵ ,ϵ , then the control shifts to Q2. So basically, when 0 enters Q0 in a regular NFA, the
transition can happen to either Q0, Q1 or Q2. This is the trick behind the transitions.
Q0
Q1
Q0 Q1 Q2
Q2
Q2
Q1
Q1 --- Q2
Q2
Q2 --- --- Q2
Question
Answer
ϵ – NFA TO DFA CONVERSION
This is exactly like NFA to DFA conversion. Except in this case, we have –
Initial State=ϵ−NFA initial state + All states that can be reached by only ϵ
Let us take the example for a ϵ – NFA below –
For this case, initial state will be q 0 q 1q 2. Using this, we can then convert this to a DFA using the
regular method to convert NFA to DFA –
Question
DECIDABLE PROBLEMS OF FA
● Equality Problem – If the given two FAs are accepting the same language or not.
Emptiness Problem
For a given FA, find the list of states that can be reached starting from the initial state. Out of those
states, if any of them are final states, then the FA accepts non – empty languages. Else, the FA accepts
an empty language. For example –
In the above FA, we have states q 0 ,q 1 , q 2∧q 3 which can be reached from the initial state q 0. Among
these states, q 2 is a final state. Hence, this FA accepts non – empty language.
Finiteness Problem
First, eliminate those states that can’t be reached from the initial state. Then, eliminate the dead states
(aka the states from which control can’t go to final state). Now, out of all the remaining states, if there
are no loops and at least one final state, then the FA is a Finite Language FA. For example,
First, we eliminate the states that can’t be reached from the initial state. Since we can reach all the
states from the initial state, no state is eliminated here. Next, we eliminate the dead states. In this case,
there are no dead states as well. So we can’t perform any minimization. In this diagram, we can find the
following loops –
● Q1 self – loop
Since we have loops and also a final state, the FA is an infinite automata.
Equality Problem
In this case, we compare 2 DFAs. If any of the inputs is an NFA, then we need to convert it to DFA. Given
2 DFAs, we can call them equal if they are accepting the same languages. To check if the DFA’s are equal,
we need to start off with the initial state and check the transitions. If at any point, one DFA accepts the
transition but another doesn’t, then we can conclude that the DFAs are not equal. For example,
Let us write the transitions. In case any of the transitions ends in either Q1 or Q4, then it means that the
pattern is accepted, else the pattern is rejected.
STATE IP = C IP = D
Now, we have covered all the states and transitions. We can see that Q1 and Q4 come together always
meaning that both the DFAs accept and reject the same strings. Therefore, these 2 DFAs are equal.
NOTE – As seen from the above problem, it is possible for 2 different DFAs to have different number of
states and different number of final states and still be equal. Let us take another example –
Let us write the transitions here –
STATE IP = C IP = D
In this case, we can see that for pattern DD, the first DFA ends up at final state Q1 but the second FA
ends up at non – final state Q6. So these 2 DFAs are not equal.
OTHER PROBLEMS
Apart from the above three decisive problems, there are three other problems –
¿
● Completeness Problem – If the DFA accepts Σ or not. To check this, find the DFA complement
(by inter-changing final and non – final states) and then check if the DFA complement accepts
empty language or not.
● Membership Problem – If a given string is accepted by the given DFA. To check this, just send
the string to the DFA and check if the string ends up on a final state or not.
● Halting Problem – If a FA halt (aka hang) after reading a string. This is a problem usually faced in
Turing Machine and is Undecided Problem.
NOTE – Any problem that can be solved using a set algorithm is a Decidable problem. Any other problem
is an Undecidable Problem.
This is used to minimize an existing DFA to get the minimal DFA. Let us understand this algorithm using
an example as shown below –
STATE IP = 0 IP = 1
Q0 (initial state) Q1 Q5
Q1 Q6 Q2
Q2 (Final State) Q0 Q2
Q3 Q2 Q6
Q4 Q7 Q5
Q5 Q2 Q6
Q6 Q6 Q4
Q7 Q6 Q2
Step 1
We need to remove the states that can’t be reached from the initial state. In our case, there is only 1
such state – Q3. So, we remove that state from the DFA –
STATE IP = 0 IP = 1
Q0 (initial state) Q1 Q5
Q1 Q6 Q2
Q2 (Final State) Q0 Q2
Q4 Q7 Q5
Q5 Q2 Q6
Q6 Q6 Q4
Q7 Q6 Q2
Step 2
Now, we will create two groups – one of final states and one of non – final states. The groups are
represented as –
π 0 → {q 0 , q 1, q 4 , q 5 , q 6 ,q 7 } ; { q 2 }
This division is done by just checking final and non – final states which means that we haven’t even read
any strings yet. Hence, this is called Zero Equivalent condition.
Step 3
Now we read 1 length strings to divide the groups. In this case now, two states in one group will have
the same behavior under 1 length string inputs. In lay man terms, if two states from the π 0 group
transition and end up in the same group, then they can be clubber together here.
For example, Q0 and Q1 under zero result in Q1 and Q6 respectively. So, under zero, the two states Q0
and Q1 return to the same group. However, under one, Q0 and Q1 result in Q5 and Q2 respectively
which are in two different groups. Hence, Q0 and Q1 can’t be in the same group in this step. Likewise,
we perform for the other states as well –
π 1 → { q 0 , q 4 , q 6 } ; { q 1, q 7 } ; { q 5 } ; { q 2 }
Since we are applying 1 length strings to check here, this is called One Equivalent condition.
Step 4
π 2 → { q 0 ,q 4 } ; {q 6 } ; { q 1 ,q 7 } ; { q 5 } ; { q 2 }
π 3 → { q 0 ,q 4 } ; {q 6 } ; { q 1 , q 7 } ; { q 5 } ; { q 2 }
Now we see that we have reached a final state. In this case, we can see that { q 0 ,q 4 } are equivalent and
{ q 1 , q 7 } are equivalent. Therefore, we can achieve the same DFA using 5 states and we don’t need q 4
and q 7 . Now, the final table DFA has become –
STATE IP = 0 IP = 1
Q0 (initial state) Q1 Q5
Q1 Q6 Q2
Q2 (Final State) Q0 Q2
Q5 Q2 Q6
Q6 Q6 Q0
Question
STATE IP = 0 IP = 1
Q0 (initial state) Q1 Q0
Q1 Q0 Q2
Q2 Q3 Q1
Q3 (Final State) Q3 Q0
Q4 Q3 Q5
Q5 Q6 Q4
Q6 Q5 Q6
Q7 Q6 Q3
Answer
We can see that q 4 , q 5 , q 6 and q 7 are not reachable from initial state q 0. Therefore, the remaining
states are –
STATE IP = 0 IP = 1
Q0 (initial state) Q1 Q0
Q1 Q0 Q2
Q2 Q3 Q1
Q3 (Final State) Q3 Q0
Now, we have –
π 0 → {q 0 , q 1, q 2 } ; {q 3 }
π1→ {q 0 , q 1 }; {q 2 }; {q 3 }
π 2 → { q 0 } ; { q 1 } ; {q 2 } ; {q 3 }
Therefore, we can see that we need all the four states. Hence, this four state DFA is the minimal DFA.
Question
STATE IP = 0 IP = 1
Q0 (initial state) Q1 Q5
Q1 Q6 Q2
Q2 (Final State) Q0 Q2
Q3 Q2 Q6
Q4 Q7 Q5
Q5 Q2 Q6
Q6 Q6 Q4
Q7 Q6 Q2
Answer
STATE IP = 0 IP = 1
Q0 (initial state) Q1 Q5
Q1 Q6 Q2
Q2 (Final State) Q0 Q2
Q4 Q7 Q5
Q5 Q2 Q6
Q6 Q6 Q4
Q7 Q6 Q2
Now, we have –
π 0 → {q 0 , q 1, q 4 , q 5 , q 6 ,q 7 } ; { q 2 }
π 1 → { q 0 , q 4 , q 6 } ; { q 1, q 7 } ; { q 5 } ; { q 2 }
π 2 → { q 0 ,q 4 } ; {q 6 } ; { q 1 ,q 7 } ; { q 5 } ; { q 2 }
π 3 → { q 0 ,q 4 } ; {q 6 } ; { q 1 , q 7 } ; { q 5 } ; { q 2 }
Q0 (initial state) Q1 Q5
Q1 Q6 Q2
Q2 (Final State) Q0 Q2
Q5 Q2 Q6
Q6 Q6 Q0
1. Arden’s method
2. State Elimination Method
Arden’s Method
● If P and Q are 2 REs and P does not contain ϵ , then the equation R=Q+ RP can be uniquely
¿
written as R=Q P .
● If P contains ϵ , then we have infinite number of solutions
These points may seem random and incomprehensible at first, but with problems we can understand
this properly –
Question
First things first, the given FA is a DFA and so we can use the Arden’s Method. Prior to starting this
method, we can perform minimization by –
● Deleting the states that are un-reachable from the initial state
Now, we start writing the equations for each remaining states. The equations basically describe how the
state can be reached –
q 1=( q 2. b ) + ( q 3.a ) +ϵ
q 2=q 1. a
q 3=q 1. b
To reach the initial state, we can use ϵ as well. So, no matter the FA, the state equation for the initial
state must include ϵ .
In our case, q1 is the final state. So, we need the equation of q1 to get the final RE. However, we don’t
need other states like q2 and q3. So, we perform substitution –
q 1=( q 1. a . b ) + ( q 1. b . a )+ ϵ
q 1=ϵ +q 1(ab+ba)
The above equation is in the form of –
R=Q+ RP
Thus, using the points in the Arden’s method, we can write –
¿
R=Q P
Finally, we have
¿ ¿
q 1=ϵ .(ab+ ba) =(ab+ba)
Thus, we now have our regular expression 😊. One thing to note here is that –
¿ ¿
( ab+ ba )¿ =[ ( ab )¿ . ( ba )¿ ] =[ ( ab )¿ + ( ba )¿ ]
In GATE, they can give any option. We must be well – versed with the properties to handle this situation.
Question
Get RE from FA –
Answer
Here, we don’t have any states to be eliminated. Hence, we start with the equation writing –
● Substitute (3) in (2). This will give the value of q2 in terms of q1, a and b
● Substitute the expression for q1 in the expression for q2 to get q2 in terms of a and b
Final answer
¿
q 3=[ a+ a ( b+aa )¿ b ] a ( b +aa )¿ a
Question
Here also we can’t eliminate any of the states by minimization. So, we can directly write equations –
q 0=ϵ
q 1=q 0.1+q 1.1+ q 3.1
q 2=q 1.0+ q 2.0+q 3.0
q 3=q 2.1
Solving these four equations, we get –
¿
q 3=1. [ 1+1.0 . ( 0+1.0 )¿ .1 ] .0. (0+1.0)¿ .1
¿ ¿
NOTE – Please note that (ar ) a=a (ra)
This is a much faster method when compared to Arden’s method. One example of the method is shown
here –
As the name suggests, we have eliminated state S2 to get the string for which the control goes from
initial to final state and thus we get the regular expression.
This method has no set algorithm and is just based on intuition and knowledge. So use it wisely for with
great power comes great responsibility.
Question
Answer
Here, we just need to reach S4. After that, no matter if we take e, f, g, h, we will end up in a final state.
Thus,
Question
Answer
The thought process –
● Then, we can keep looping over B by giving either ba or c (any number of times)
Thus,
¿
ℜ=a(ba+c )¿ d=( a c¿ b ) a c ¿ d
Before proceeding further, one should note that if we have two regular languages R1 and R2, then
While constructing FA, we need to keep the following priority order in mind –
¿> .>+ ¿
So first accommodate the ¿ expressions, then the . expressions and finally the +¿ expressions. Here are
some basic structures that can help –
a+ b
r 1 .r 2
¿
r
Question
¿
Convert RE to FA - ℜ=0 1 +1
Answer
ALTERNATE METHOD
The above standard method is easy to understand but gives a large number of states and extremely
complex FA models. Instead, we can go for an easier more intuitive way to implement this by splitting
the problem step by step.
¿
For example, let us take the RE - 0 1 +1 and check the FA step by step process –
So, we can see that we get only 4 states but we have implemented the logic 😊
Question
¿ ¿
Convert RE to FA - ℜ=( 11+0 ) ( 00+1 )
Answer
CONSTRUCT ϵ FREE NFA
For this case, we can just use the above method and just remove the ϵ transitions. For example, let us
¿ ¿
take the case of RE – ( a+ b ) ( aa+ bb ) ( a+b )
● Mealy Machine
● Moore Machine
These two machines perform some operations and then produce an output. Let’s understand these
machines using questions –
Question
Construct a Moore and Mealy Machine to count the occurrence of the substring ab in a given string.
Answer
The above diagram is a Moore Machine where each state has an output value. The transitions and
states are self – explanatory really. One thing to remember is that there is no final state in the Moore
and Mealy Machine.
The above diagram is the corresponding Mealy Machine. The main difference between Moore and
Mealy machines is that the output is written on the transition lines for Mealy machines.
General Points
● For Moore machines, the output is a function of the current state itself.
o λ ( output function ) :Q → Δ(output alphabet )
● For Mealy machines, the output depends on the state and the previous input (since the output
is on the transition line)
o λ :Q × Σ → Δ
● Moore and Mealy Machine - M =( Q , Σ ,δ , S , λ , Δ ). This is why these machines are called 6 –
tuple machines.
● For a n – length input strings, the Moore machine output string will be of length n+1 while the
Mealy machine output string will be of length n itself. This is because Moore Machine will have
an output for the initial state before the input string is fed.
Question
Construct a Moore and Mealy Machine to which takes a string of A’s and B’s an input and gives output –
Answer
The above diagram is a Moore Machine. Let us take the case of a string abaaabb as input. Then the
output becomes –
S1 2
A S2 2
B S4 0
A S2 2
A S3 1
A S3 1
B S4 0
B S1 2
So, here we can see the output to be 22021102 which is of length 8 even though the input string is of
length 7. This is mentioned in the general points.
In the above diagram, we get the Mealy Machine for the same problem. For this machine, the output
can be written as –
S1
A S2 2
B S1 0
A S2 2
A S2 1
A S2 1
B S1 0
B S1 2
In the case of a Mealy Machine, the input string and the output strings have the same length of 7.
To convert Moore to Mealy machine, write the same states and transitions as the Moore Machines.
However, write the output on the transition instead. So, for the above mentioned problem, the Mealy
machine using the conversion method will be –
This method doesn’t give the minimal Mealy Machine though. For that, either use minimization
algorithm or just use your damn brain.
Step 1
● S1,0
● S1,1
● S1,2
● S2,0
● S2,1
● S2,2
Step 2
Step 3
Delete the states that have no incoming transitions. This means that those states can’t be reached from
any of the other states. Hence, we can discard them. Additionally, take any of the S1 states as initial
(doesn’t matter really).
Question
Answer
Question
Convert the Mealy Machine to Moore Machine –
Answer
Union
In this case, the union is similar to the Set Theory union. Here are some points to remember –
L1 L2 L1 U L2 REASON
¿ ¿
Σ Anything L1 Since Σ is a super set
of everything, the
union will be L1
NOTE
● If L1 is regular language and L2 is a non – regular language, then the union may or may not be
regular. For example –
o L 1=Σ ¿ (Regular) union L 2=a n b n (Non-regular) → Σ ¿ (Regular)
o L 1=ϕ (Regular) union L 2=a n b n (Non-regular) → an bn (Non – Regular)
● If L1 U L2 is a regular language, then it is not necessary that the L1 and L2 are regular languages
–
¿ ¿
o L 1=Σ (Regular) union L 2=a n b n (Non-regular) → Σ (Regular)
¿ ¿ ¿ ¿
L 1=a b (Non – Regular) union L 2=a b (Regular) → a b (Regular)
n n
o
¿
L 1=a b (Non – Regular) union L 2=a b (Non – Regular) → Σ (Regular)
n n n n
o
L1 L2 Is L1 U L2 Regular
If L1 and L2 are Regular Languages, the L1 U L2 is always regular. Therefore, Regular Languages are
CLOSED under UNION.
Intersection
L1 L2 L1 ∩ L2 REASON
¿ ¿
Σ Anything L2 Since Σ is a super set
of everything, the
intersection will be L2
+ ¿¿ + ¿¿
a
+¿b ¿
b
+ ¿a ¿
ϕ The minimal strings for
L1 and L2 are ab and
ba and thus, the
intersection will be
empty.
+¿¿ +¿¿
ab ba ϕ The minimal strings for
L1 and L2 are ab and
intersection will be
empty.
L1 L2 Is L1 ∩ L2 Regular
Complementation
Complementation is basically –
C ¿
L =Σ −L
L L
c
REASON
¿
ϕ Σ Self – explanatory
¿
Σ ϕ Self – explanatory
¿ ¿
a ( a+b ) ϵ +b ( a+b ) L only accepts strings with a as a starting character. So its
complement will be strings which accept ϵ and b as a starting
character.
( a+ b )¿ b ( a+b )¿ a
¿
L only accepts strings with b as a sub string. The complement
will accept strings which have ϵ or a as a sub string.
NOTE – If L is regular, then its complementation will also be regular. However, if L is non – regular, then
its complement will also be non – regular. For better understanding, draw the DFA of L and change final
states to non – final states and vice – versa.
Difference
Basically –
c
L 1−L2 ⇔ L1 ∩ L 2
This makes it slightly easier to comprehend –
NOTE – If L1 and L2 are regular languages, then L1 – L2 will also be a regular language.
Subset
This is the only operation that is not closed for regular languages. That is because a subset of a regular
¿
language can be a non – regular language. For example, subset of Σ can be a n b n which is not regular.
Concatenation
Reversal Operator
To get the reversal of a language L, draw the DFA for that language. Then perform the following steps –
● If there are multiple initial states at this point, then combine them into 1 state.
NOTE – Since the language L is a regular language, its reversal LR will also be regular.
Kleen Closure
¿ ¿
This is basically transforming L into L . If L is a regular language, then L is also regular.
¿
NOTE – If L=ϕ, then L =ϵ
NOTE – Let us take the example of language L={a P∨where P is a prime number } . This language is
Non – regular. However, the Kleen closure is L¿ ={a P ∨where P is a prime number }¿ is a regular
¿ ¿
language since it accepts all strings of a except a single occurrence aka L =a −a
Quotient Operation
Definition –
L1
L= = { x ∨∀ xy ∈ L 1∧ y ∈ L 2 }
L2
Basically, we can perform normal cancellation type division for two strings. However, we need to match
exact patterns from the right to the left. For example,
abc
=ab
c
But,
abc
=ϕ
b
This is because we need to start from the RHS. We can’t cancel from in – between the string. Also, abc
means concatenation and not multiplication. Therefore,
L1
=L 1
ϵ
If L2 is not completely cancelled, then the answer is ϕ
abc ab
= =ϕ
bac ba
Till now we have looked at languages with a single string. In case the languages contain a set of strings,
then we need to try all the combinations. For example,
{ a , ab }
L=
{ ϵ , b , aa }
Thus, we need to try all combinations –
a a a ϵ
=a =ϕ = =ϕ
ϵ b aa a
ab ab ab
=ab =a =ϕ
ϵ b aa
Therefore, we have
L= { a , ϕ , ϕ , ab , a , ϕ }= {a , ab }
L=
a
a
¿= {
a a
}
, , ϕ ={ a , ϵ }
ϵ a
L=
b
¿= {
a¿ ϵ a aa
, , , … =a ¿
ϵ ϵ ϵ }
L=
b a¿ b . ϵ
b
=
b { }
=ϵ
L1 ϕ
L= = =ϕ
ϕ L2
¿ ¿
a b a ¿
L= = =a
ab a
ϵ
L= =ϕ
a
¿
Σ ¿
L= =Σ
L2
L2
L= ¿ =all prefixes of L 2
Σ
NOTE – If L1 and L2 are regular languages, then their quotient is also regular. Let us assume a language
L= { abcd }. Then, for this language we have –
In the FA of L, we can –
● Make every state as final and we get a FA that accepts all prefixes of L
● Make every state as initial and we get a FA that accepts all suffixes of L
● Make ever state as initial and final and we get a FA that accepts all substrings of L
If L is a regular language, then the prefixes, suffixes and substrings are also regular.
Substitution
It is a mapping from each symbol of Σ to one of the Regular Language over Δ . This can be represented
as –
S : Σ→ P ( Δ¿ )
This may seem like a load of fresh, methane – filled BS, but let’s see some examples which will help us
understand. Let us assume we have Σ= { a ,b } and Δ= {0 , 1 }. Then, we have
¿
S ( a ) =0
¿
S ( b ) =0 1
Basically, we are mapping each symbol of Σ to one of the RL over Δ . It can be any regular language. I
have just taken 2 random examples. We have a few formulae that we need to keep in mind –
S ( ab )=S ( a ) . S ( b )
¿
S ( a¿ ) = [ S ( a ) ]
S ( a+ b )=S ( a )+ S (b)
S ( ϵ )=ϵ
S ( ϕ )=ϕ
Question
¿
We have Σ={0 ,1 } and Δ={a , b }. The substitution is defined as S ( 0 )=a and S ( 1 )=b . Then find –
● S(010)
¿ ¿
● S ( 0 ( 0+ 1 ) 1 )
Answer
¿
● S ( 010 ) =S ( 0 ) . S ( 1 ) . S ( 0 ) =a b a
¿ ¿
● S ( 0 ( 0+ 1 ) 1 )=[ S( 0) ] . [ S ( 0 ) + S(1) ] . [ S( 1) ] =a . ( a +b ) . b ≡ a b
¿ ¿ ¿ ¿ ¿ ¿ ¿
NOTE – Since substitution replaces one symbol with a regular language, the substitution is CLOSED
under Regular Language.
Question
We have Σ={0 } and Δ={a , b }. The substitution is defined as S ( 0 )= ( a+b )( a+b ) . Then find –
● S(00)
¿
● S (0 )
Answer
4
● S ( 00 )= ( a+b )( a+b ) ( a+ b ) ( a+b )=( a+ b )
2 ¿
● S ( 0 )=[ ( a+ b ) ( a+b ) ] =[ ( a+b ) ] =Set of all even length strings
¿ ¿
Homomorphism
¿
In Substitution, every symbol in Σ was replaced by a regular language over Δ aka S : Σ→ P ( Δ ).
¿
However, in homomorphism, every symbol in Σ is replaced by a string over Δ aka S : Σ→ Δ . The
formulae that apply to Substitution also apply to Homomorphism.
We can also perform the Inverse Homomorphism by just reversing the values –
−1
h ( 0 )=ab , thenh ( ab )=0
Question
Answer
The minimal string of language L is a . However, we don’t have any value for h−1 (a). Therefore, we can’t
provide the inverse for the entire language. However, can provide the inverse for one string in this
language –
h−1 ( aba )=1
Also, we have –
¿ ¿
a ( ba ) =( ab ) a
The proof is shown below –
PDA is basically a basic DFA/NFA with an additional memory stack. To realize a NFA/DFA, we needed a
Language. Here, to realize PDA, we need Grammar as well. Grammar is defined as –
G ( V ,T , P , S )
Where,
● G – Grammar function
● V – Set of variables
● T – Set of Terminals
● P – Set of Productions
● S – Start Symbol
Grammar G is defined as –
The above given transition type info are called Productions. Using this, we take the caps letters as
symbols and small letters as terminals (default). Therefore, we have –
● V = {S, A, B}
● T = {a, b}
● S=S
This is the default interpretation. This can change in the question. Now in the above grammar, we have S
as the start symbol which changes to AB. Now A can take the value of a and B can take the value of b. So
we have –
S=¿ AB=¿ ab ( ¿ ) aϵ
This process of taking string from grammar is called Derivation. There can be two types of Derivations
depending on whether we substitute A or B first –
Recursion
S=¿ aS
This case, S is calling itself. Grammars with Recursion result in infinite strings.
NOTE – If for a Grammar we have more than one Derivation Tree, then it is called an Ambiguous
Grammar. For example, let us take the grammar below and try to get the string abab from this –
S → aSbS|bSaS|ϵ
There are multiple ways to get this –
Since the same string has more than one tree, then we can conclude that the grammar is Ambiguous
Grammar.
S → ABC
AB→ CD
aD → e
Answer
First off, this language has more than one variable in the LHS. Hence, it is neither Type 3 nor Type 2.
Now in Type 1, we have Size of LHS should be lesser than or equal to size of RHS. Here, the last
production shows that size of LHS is greater than size of RHS. Thus, this is not a Type 1 grammar.
Question
S → ABC
B→≝¿
E → cd
Answer
Since all the LHS has only one variable, this is either a Type 2 or a Type 3 language. Now, for Type 3, we
need RHS to have a single variable and any number of Terminals. However, in the first production, we
have 3 variables and 0 terminals. Hence, this is not a Type 3.
Therefore, this is a Type 2 grammar. In the third production, we have only terminals in the RHS. In such
cases, the production satisfies both LLG and RLG for Type 3.
Question
Answer
Take –
A={ a m∨m≥ 1 }
B= { bn∨n≥ 1 }
Therefore, we get
S → AB
A → aA∨a
B→ bB∨b
Question
Answer
¿
Since we have ( a+ b ) , we need to write a variable which gives us infinite strings of a and b . We can
write this by -
A → aA|bA|ϵ
Now, we get
S → AabbA
NOTE – CFL are closed under Union aka If we have two CFLs L 1 and L 2, then their union will also be a
CFL. On the other hand, CFL are not closed on Intersection since L 1∩ L 2 might not be a CFL.
Question
Answer
For a palindrome, we want the first and last strings to be the same and then the subsequent strings till
we hit the middle. At the same time, 1 length strings are also considered palindromes. Thus, we have –
S → aSa|bSb|a|b|ϵ
Question
Answer
This is an interesting and quite complex problem….SO ALLL EARS HERE PLEASEEEE!!! First off, let us see
what ww means here.
ww is basically a string where we have two equal parts. For example - aaaa , bbbb , abab ,aabaab etc.
Now, we need to write a CFG for the language where ww doesn’t match. There can be two main
conditions where this doesn’t occur –
● The string length is odd. If the string length is odd, there can’t be any 2 equal parts (DUH)
● The string length is even but the two parts are different. For example - aaabbb ,ababba ,ab
etc.
L →a∨b
A → LAL∨a
B→ LBL∨b
C → LLC ∨L
Here, we have
In short,
C= A ∪ B
Now, C is enough to gives all odd length strings which is one of the cases for which our language should
match. However, for the second case where we have even length strings but different parts, we can go
with either AB or BA . This is because A and B gives strings with middle element as a and b
respectively making the two parts unequal all the time.
S →C |AB| BA
NOTE – One thing to note here is that ϵ will not be a part of our language because if w=ϵ , then
ww =ϵϵ =ϵ . The Language L= { ww∨w ∈ Σ ¿ } is a CFL since we can write a CFG. However, its
complement L= { ww∨w ∈ Σ } needs two stacks for comparison and is a CSL not a CFL. Therefore, CFG
¿
PDA CONSTRUCTION
PDA is basically a FA with an infinite stack. In case the stack is of finite length, the system is still a FA. A
PDA can be represented as –
M =( Q , Σ ,δ , S , F , k , Z )
Where,
● Σ – Set of alphabets
● δ – Transition Function
● S – Start State
● F – Final state
● k – Stack alphabets
● PUSH
● POP
● SKIP
For example, let us take the following operation – “Suppose the on State S0 , if the input is a and the top
of the stack is Z 0, then change the state to S1 and push X on top of the stack”.
δ ( S 0 , a , Z 0 )=( S 1 , Z 0 X )
One thing to note is that the stack alphabet is X while the input alphabet is a . This means that the stack
and input alphabets need not be the same.
PUSH operation
In the above diagram, X was pushed to the top of the stack that already had Z 0.
POP operation
In the above diagram, X has been popped off and the placeholder for X at the top of the stack is now ϵ .
However, this doesn’t mean that the stack is empty.
SKIP operation
In the above diagram, the stack remains unchanged. This is the skip operation.
In short, every transition in a PDA requires the state, input alphabet and the stack alphabet. Post the
transition, the state and the stack alphabet will change. This is represented below –
¿
δ :Q × Σ × k → Q ×k
Question
Answer
For this case, we will have the input string as something like – aaaaabbbbbϵ . The ϵ marks the end of
the string. Let the current top element in the stack be Z 0. Then the PDA approach is –
● Whenever the string character is a , then push that to the top of the stack
In this case, we are accepting the string when the stack has Z 0 which is the final state. However, we can
also design the PDA such that the stack becomes empty. This is called acceptance by empty stack.
Question
Question
Answer
Before answering, let us assume a string – aabbaabbaa . This is a palindrome which means it should be
accepted by the language. If we divide the string into two parts, we get –
● aabba
● abbaa
Now, if we are to follow the previous question’s approach, we should push the first half of the string and
then start popping till we get an empty stack. However, how do we find the middle element?
In the above two halves, the first half will have a followed by a and will have to be pushed to the stack.
However, the next time we encounter a followed by a , we need to pop the stack.
Basically, this problem is non – deterministic PDA (NPDA). In this case, just take a gander really.
NOTE – As noted before, Expressive Power is the number of languages that can be realized using the
machine. For example, for every NFA a corresponding DFA exists. Thus, they have the same expressive
power. However, in the above example, we created an NPDA but the same language can’t be realized
using DPDA. Thus, we have –
E ( ϵ−NFA ) =E ( NFA )=E ( DFA ) < E ( DPDA ) < E ( NPDA ) < E(TM )
The language accepted by NPDA is called CFL but the language accepted by DPDA is called DCFL. All DCFL
are CFL but not all CFL are DCFL.
NOTE – DCFL are closed over complementation and inverse homomorphism only.
Question
Answer
Options 1 and 3 can be accomplished using a single stack without taking a guess anywhere. Thus, 1 and
3 are DCFL.
Question
Answer
● If the next character is a or b , then skip. There is atleast one skip operation in this PDA.
Question
Given the following PDA, find the type of language –
Answer
{❑n ❑m ❑n+ m }
Question
Answer
{}
CLOSURE PROPERTY
Union Example
{❑n ❑n }
{❑n ❑2 n }
{❑i❑ j ()}
Both L1 and L2 are DFCL but L is CFL since we have 2 paths for the operation to go to and it is not clear which one to tak
Intersection Example
{❑n ❑m ❑m }
{❑n ❑n ❑m }
{❑m ❑m ❑m }
Here both L1 and L2 are DCFL but L is CSL since it can’t be checked using a single stack. Thus, DCFL and CFL are not c
Complementation Example
{❑n ❑n }
❑ {❑ ❑ }
m n
Here, both and ❑
are DCFL. Therefore, DCFL are CLOSED over complementation. This can also be realized by intuit
{❑i❑ j ❑k ()}
{❑❑¿ }
❑ {❑ ❑ ❑ }
n n n
❑ {❑¿ }
In this case, both and
are CFLs but their complements are CSLs. Therefore, CFL are NOT CLOSED over complementation.
Difference Example
Since the difference involves intersection and we already know that DCFL and CFL are not closed over intersection, we ca
Concatenation Example
{❑n ❑n }
{❑m ❑2 m }
{❑n ❑n ❑m ❑2 m }
In the above example, both and are DCFLs, but is a CFL since we can’t say when either or
will be 0 and hence whether the symbols coming belong to e
or
. Hence, DCFLs are NOT CLOSED over concatenation but CFLs
Reversal
R 2n n n n
❑ ❑ ❑❑❑
In this case, the string will always begin with . But how do we know if we need to check for ❑2 n ❑n or
n n
❑ ❑ ? Thus, there is a confusion making this a CFL and not DCFL. Hence, DCFL are NOT CLOSED over reversal. On t
❑¿||
+¿¿
❑
Therefore, if is a CFL, then❑¿ and ❑+¿¿
will also be CFLs and thus the CFLs are CLOSED over Kleen and Positive Clos
{❑n ❑n ❑k ❑2 k }
Then in this case, we have –
¿
❑¿ (❑n ❑n ❑k ❑2 k ) ❑n❑n ❑k ❑2 k
In this case, the string always starts with , but then we don’t know if we need to check for ❑n ❑n or
k 2k
❑ ❑ . This makes the language a CFL. Hence, DCFLs are NOT CLOSED over Kleen or Positive Closure.
Prefix
Suffix
Subset
In this case, the PDA will keep recursing and will never end. Hence, it will never accept any string. Therefore, the PDA ac
which is the emptiness problem.
Emptiness Algorithm
Finiteness Algorithm
Membership Algorithm
Question
S → AB∨a
A → BS∨b
B→ AS∨d
E → FG
F→f
G→g
Answer
First, we eliminate the productions that are unreachable from the starting state S. Once done, the
remaining productions are –
S → AB∨a
A → BS∨b
B→ AS∨d
For emptiness problem, we will now eliminate the productions that don’t produce any strings. Since
there are no such productions, we can see that the CFG is non – empty.
, we have –
This is basically a FA with 2 stacks instead of 1. So
()
Where,
– Set of states
– Set of alphabets
– Transition functions
– Start State
– Final State
The tape in the TM is a 2 – way infinite tape. It can be also referred to as the main memory. All modern computers are Tur
In a FA, the input is usually stored in the input buffer and then fed to the FA for process
NOTE – In PDA and FA, the input is read from the input buffer but the FA/PDA can’t write back to the input buff
In this case as well we can have 2 types – Deterministic and Non-deterministic Turing Machines.
{}
( {})
Both DTM and NTM are equal. If a DTM exists, then the corresponding NTM also exists.
Question
Answer
● Replace that with and move to the right. By this time, we have confirmed that at least a single
● Keep repeating the above step for the other ’s in the tape
●
When the tape pointer reaches a Blank symbol, then replace that will a Blank symbol and stay there. No need to go
If we remove S2 and keep the self – loop at S1 itself, then the TM will accept
as well. Therefore, we need S2.
Question
Answer
We can use the TM in three ways –
● TM as a language recognizer
● Recursive Languages– When the string matches the language, then the TM goes to Halt and
Final state. However, if the string doesn’t belong to the language, then the TM goes to Halt and
Non-final state. In the end, the TM always ends up in a Halt State. That is why they are also
called Halting Turing Machines.
- When the string matches the language, then the TM goes to
● Recursive Enumerable Languages
Halt and Final state. However, if the string doesn’t belong to the language, then the TM has 2
options – It can either go to a Halt Non-Final state or get stuck in an infinite loop which will
cause the program to hang.
Since the Recursive Languages will always end up in a Halt state, their logics are called algorithms. On the other hand, Re
NOTE – Recursive Languages are Decidable languages while the Recursive Enumerable Languages are called Sem
CLOSURE PROPERTIES
NOTE – All REC are REL but not all REL are REC. If we have a language
which is a REL and the complement of language
C is also
is❑
a REL, then we can conclude that
a REC since RECs are closed over complementation.
UNDECIDABILITY
Undecidable problems are the problems for whom we can’t solve using a particular algorithm. We have a few undecidable
Halting Problem
In the case of non-halting TM, we can’t guarantee if the string input will cause the TM to go into a halt state or remain in a
However, if we use a Halting TM, then the machine always halts and therefore this problem becomes decida
Membership Problem
If a string is given, will the TM go to Halt final state or not? That is the problem here. Again, this is similar to the previous
Will the TM go to a Halt state when the input is a Blank Tape? Again, this is similar to halting problem and thus we can sa
Will the TM under the given string will enter a particular state or not? Again, this is similar to halting problem as if the TM
,
we can say that for non – H
However, if the state to be checked is the initial state, then the problem will become decidabl
CANTOR’S THEOREM
● Countable Infinites where we can type out the elements. For eg a set of all Natural Numbers.
● Uncountable Infinites where we can’t type out all the elements. For eg set of all Real Numbers
NOTE – Set of all Regular Languages is countable. So is the set of all CFLs, CSLs, Recursive Languages and RELs
● Set of CFLs
● Set of CSLs
¿
● (❑ )
● Set of non-CFLs
● Set of non-CSLs
SIMPLIFICATION OF CFG
In this case, we need to remove . So, we will replace all occurrences of❑1 with . This gives us –
❑1
❑1 ❑1
In this case, we will replace all occurrences of❑1 and with . This gives us –
❑1
❑1 ❑1
Let us take another example –
This is the elimination of unnecessary unit productions. Let us take another example –
||
The process we follow is that –
We get –
||
||
||
This gives us finally –
||
||
||
Elimination of Useless Symbol
First step, we eliminate the productions which can’t be reached from the initial state. Thus, we get –
Next, we eliminate the productions that produce a valid string and don’t go into a recursive infinite loop. Thus, we get –
Now that we have eliminated , we need to eliminate that from the other productions as well –
Now that is no longer accessible from the initial state, we can eliminate that as well. Thus, we get –
NORMAL FORMS
CNF
GNF
Question
First step is to get the simplified form. Here however, no more simplification is possible. Next, we just try to take addition
Question
||
Answer
First step is to get the simplified form. Here however, no more simplification is possible. Next, we just try to take addition
||
||
Union Y N Y Y Y Y
Intersection Y N N Y Y Y
Complementation Y Y N Y Y N
Difference Y N N Y Y N
Subset N N N N N N
Concatenation Y N Y Y Y Y
Reversal Y N Y Y Y Y
Kleen Closure Y N Y Y Y Y
Quotient Y N Y Y Y Y
Prefix Y Y Y Y Y Y
Suffix Y Y Y Y Y Y
Substrings Y Y Y Y Y Y
Substitution Y N Y N N Y
Homomorphism Y N Y N N Y
Inv. Homomorphism Y Y Y Y Y Y
NOTE
DCFL ∩ REG=DCFL . Basically, intersection of DCFL with REG will always be DCFL
This algorithm is used to check if a given string is a part of the given CFG or not. To do this, we need to
ensure that the CFG is in the Chonsky Normal Form . Let us take an example to understand this method.
Let us assume a CFG as follows –
S → AB
A → BB∨a
B→ AB∨b
All productions in the CFG have LHS with a single variable and RHS either has 2 variables or 1 terminal.
Hence, CFG is in CNF form. Let us assume that we are checking the membership of the string abbb .
a−1
b−2
b−3
b−4
The length of the string is 4. Hence, we create a 4 X 4 structure as shown below –
First, we take the block ( 1 ,1 ) – which is row 1 column 1. This block refers to the string 1 – 1 aka a .
Similarly,
Now we take the block ( 1 , 2 ) which is row 1 and column 2. This refers to string from character 1 – 2 aka
ab . To solve this, we expand the following –
String ( 1 ,2 ) =( 1, 1 ) . (2 , 2 )= A . B
We can see that S and B are the two variables that derive the string AB. Hence block ( 1 , 2 ) will have
S , B . Similarly,
Block ( 2 ,3 )=Block ( 3 , 4 ) =A
Now, we take the case of string ( 1 , 3 ) which is abb . We can represent the string in 2 ways –
String ( 1 ,3 ) =( 1 ,1 ) . ( 2, 3 ) =AA
String ( 1 ,3 ) =( 1 ,2 ) . ( 3 ,3 )=( S , B ) . B=SB , BB
Now, no production can derive AA and SB. However, we can see that BB can be derived by production
A . Therefore, Block (1,3) will be occupied by A . Similarly, Block ( 2 , 4 ) will be occupied by S , B .
Finally, we have string (1,4) which is abbb . This can be handled in 3 ways –
String ( 1 , 4 )= ( 1, 1 ) . (2 , 4 )= AS , AB
String ( 1 , 4 )= ( 1, 2 ) . (3 , 4 )=SA , BA
String ( 1 , 4 )= ( 1, 3 ) . ( 4 , 4 )= AB
Out of the above strings, only string AB can be derived by S , B . Therefore, we get –
Now that the grid is complete, all strings with S in them will be accepted. Hence, list of strings accepted
are –
● abbb – (1,4)
● ab – (1,2)
● bbb – (2,4)
n ( n+ 1 )
No of substrings= +1
2
DFA CONSTRUCTION
● To check for strings beginning with S, we need M +2 states and a dead state is required.
● To check for strings ending with S, we need M +1 states and no need of a dead state.
● To check for strings with sub-string S, we need M +1 states and no need of a dead state.
NOTE
Suppose we need to construct a DFA where the nth symbol from the right of the string is fixed. In such a
case, we can conclude –
n
● The total number of states will be 2
n−1
● The number of final and non – final states will be the same and will be 2
One extremely important thing to note here is that the above trick only works for TWO SYMBOL CASES.
Question
Construct a minimal DFA where Σ= { a ,b } and the third symbol from the right is a .
Answer
Non−final states={ q 0 , q 1 , q 2 , q 3 }
Final states= { q 4 ,q 5 ,q 6 , q 7 }
Therefore, we can write the transition table as follows –
STATE b a
q 0 (Initial) q0 q1
q1 q2 q3
q2 q4 q5
q3 q6 q7
q4 q0 q1
q5 q2 q3
q6 q4 q5
q7 q6 q7
Question
Construct a minimal DFA for Σ= { a ,b } such that the number of a is divisible by 2 and number of b is
divisible by 3
Answer
NOTE
In general, if we have a 2 – symbol alphabet Σ= { a ,b } and we need to construct a minimal DFA where
the number of a is divisible by N and the number of b is divisible by M , then –
No of states=N∗M
Hence, in the above example,
No of states=2∗3=6
NOTE
Suppose we have m symbols in the alphabet and there are a total of n states with a fixed initial state but
no fixed final state, then –
n n∗m
Total no of DFA=2 ∗n
Additionally, in case the initial state is NOT FIXED, then we can modify the above formula as follows –
n n∗m
Total no of DFA=n∗2 ∗n
Question
Answer
Let us assume the states are q 0 and q 1 respectively and that q 0 is the initial state. Thus, we have
m=n=2. Hence, we will be having 2n=4 combinations of final states as shown below –
Both Scenario 3 and Scenario 4 can’t be used since they will accept any language and will not accept the
empty language. On the other hand, all n n∗m=22∗2=16 combinations under Scenario 1 will accept
empty languages since there are no final states.
For Scenario 2, we can have the 4 combinations that will accept an empty language as follows –
Hence, we have a total of 20 DFAs that will accept empty language.