0% found this document useful (0 votes)
22 views95 pages

Theory of Computation

The document discusses the Theory of Computation (TOC) and formal languages, explaining their types and the machines that accept them, such as Finite Automata (FA), Push Down Automata (PDA), and Turing Machines (TM). It details the structure of these machines, their expressive power, and provides examples of constructing Deterministic Finite Automata (DFA) for various languages. Additionally, it covers concepts like alphabets, strings, and the closure properties of languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views95 pages

Theory of Computation

The document discusses the Theory of Computation (TOC) and formal languages, explaining their types and the machines that accept them, such as Finite Automata (FA), Push Down Automata (PDA), and Turing Machines (TM). It details the structure of these machines, their expressive power, and provides examples of constructing Deterministic Finite Automata (DFA) for various languages. Additionally, it covers concepts like alphabets, strings, and the closure properties of languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 95

The mathematical study of the computation machines and their capabilities is called the Theory of

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.

For example, let us take a language defined below –


m n
L={a b ∨m ,n ≥ 1 }
In this language, we can have strings like aaabb, ab, abbbb, aaabbbbb. But strings like bbbaaaa or
abbba are not part of the language since they don’t follow the format. This formal language has an
infinite number of strings under it and it is called an infinite formal language.

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 .

TYPES OF FORMAL LANGUAGES

According to Chomsky, there are four types of formal languages –

● 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

Finite automate (or) Finite State


T3 Regular Language (RL)
Machine (FSM)

T2 Context Free Language (CFL) Push Down Automate (PDA)

Context Sensitive Language


T1 Linear Bounded Automate (LBA)
(CSL)

Recursive Enumerable Language Turing Machine (TM) aka


T0
(REL) Computer

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

Let us assume there is a language as defined below –


n
L={a ∨n ≥1 }
This means that an infinite number of strings with just ‘a’ as the character are possible here. The
automata for this language looks like this –

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.

● If we encounter an A, then push it to S1.

● If we encounter a B, then push it to S2 and pop A from S1.

● If we encounter a C, then push it to S1 and pop B from S2.

● If we encounter a D, then pop C from S1.

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 }

NOTE – We can see that Σ 0=ϵ .

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.

FINITE AUTOMATA & REGULAR LANGUAGE

There are four major types of FA –

● 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.

FINITE AUTOMATA WIHTOUT OUTPUT

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.

Let us take an example of FA with the following state transition diagram –

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 –

No of states=Length of minimal string+1


Thus, we need a 2 states to construct the required DFA. The DFA looks like this –

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.

Let us add another state S2 as shown below –

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 –

This is a NFA still. We can add a loop to make it a DFA –


Question

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 –

Once we add a loop to S1, we get the DFA as shown below –

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

Construct a minimal DFA for language L= { am bn ∨m, n ≥ 0 }

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

Construct a minimal DFA for language L= { an bn∨n ≥0 }

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 –

L= { am bn ∨m, n ≥ 0∧m+ n=10 }


In this case, a FA is possible. This is because the sum of number of a and b should be 10. Hence, the
language is finite and using 11 states, we can create the DFA despite the fact that we need to perform
comparison.

Question

Construct a minimal DFA for language L= { all strings which have the 2nd symbol ¿ RHS as A }

Answer

So basically, we have L= { Σ ¿ a Σ 1 } . The minimal DFA is as shown below –

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

The same thing can be represented as a NFA as shown below –

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.

NFA TO DFA CONVERSION

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 –

STATE Next symbol is A Next symbol is B

S0 S0, S1 S0

S1 S2 S2

S2 - -

Now, we can write the transition table for DFA as follows –

STATE Next symbol is A Next symbol is B

S0 S0, S1 S0

S0, S1 S0, S1, S2 S0, S2

S0, S1, S2 S0, S1, S2 S0, S2

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

Construct a minimal DFA for language

L= { all strings where the number of A' s isdivisible by 2∧number of B' sis divisible by 3 }
Answer

For this problem, we can divide the requirement into two FA –

● L 1={ all strings where thenumber of A s is divisible by 2 }


'

'
● L 2={all strings where the number of B s is divisible by 3 }

Now, for L1 and L2, we can draw separate DFA –

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}

GATE QUICK TIP

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 –

Step 1 (Product Automata)

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,

Total number of states=2 ( courtesy of A )∗3 ( courtesy of B )=6

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 –

Total number of states=Common Difference=2


In short, L1 can be implemented with a DFA with only 2 states.

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 –

Total number of states=3∗7=21


Also, the answer in Step 1 is the WORST-CASE number. So even if the options given are more than the
product automata, DON’T CHOOSE THAT OPTION.

Question

Construct a minimal DFA for language

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

STATES Next State when I/P = 0 Next State when I/P = 1

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

S10 S10 S10

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 –

● X is a non – final state and Y is a non – final state.

● X is a non – final sate and Y is a final state.

● X is a final state and Y is a non – final state.

● X is a final state and Y is a final state

For each of these possibilities, we have the following transitions –

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.

For each of those combinations, we can map the possible transitions as –

STATE I/P = 0 I/P = 1


X 3 3

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 –

● If there is no final state in the DFA

● If the final state is never achieved

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

First off, we have four possibilities –

1. Both X and Y are non – final


2. X is final and Y is non – final
3. X is non – final and Y is final
4. Both X and Y are final
In this case, we have X as the initial state. Hence, to accept ϵ , X has to be a final state. So, we can rule
out options 1 and 3.

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.

Thus, total number of such DFAs are 20.

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

Give the Regular Expression (RE) for language

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) =Σ

● Since we either have strings bounded by A or B, we added an OR operator in between (+)

● 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

Here, there are three things to consider –

● 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

Which of the four options are equal –


¿
● a
¿
● (aa)
¿
● (aa) a
¿
● (aa) ( a+ϵ )

Answer

In the above case, we have –

● First option gives all possible strings with A’s


● Second option gives all possible even strings with A’s

● Third option gives all possible odd strings with A’s

● Fourth option gives all possible strings with A’s

Hence, we have options 1 and 4 as equal.

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

Give the Regular Expression (RE) for language

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

The language can be written as –


¿ ¿
L=Σ abb Σ

Question

Construct a NFA for language L= { ABB , BAA }

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

ϵ – NFA TO NFA CONVERSION


Let us take the example for a ϵ – NFA below –

The language represented in the ϵ – NFA is: -

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.

CURRENT STATE WHEN I/P=0 WHEN I/P=1 WHEN I/P=2

Q0
Q1
Q0 Q1 Q2
Q2
Q2

Q1
Q1 --- Q2
Q2

Q2 --- --- Q2

Thus, we have the final NFA as –

Question

Convert ϵ -NFA to NFA –

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

Convert ϵ -NFA to DFA –


Answer

DECIDABLE PROBLEMS OF FA

There are three main decidable problems of FA –

● Emptiness Problem – If the give FA is accepting Empty Language or not.

● Finiteness Problem – If the given FA is accepting Finite or Infinite Language.

● 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

● Q0 and Q1 loop (abababab…)

● Q0, Q2 and Q1 loop (abbabbabb…)

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

(Q1, Q4) (Q1, Q4) (Q2, Q5)

(Q2, Q5) (Q3, Q6) (Q1, Q4)

(Q3, Q6) (Q2, Q7) (Q3, Q6)

(Q2, Q7) (Q3, Q6) (Q1, Q4)

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

(Q1, Q4) (Q1, Q4) (Q2, Q5)

(Q2, Q5) (Q3, Q7) (Q1, Q6)

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.

PARTITION ALGORITHM (STATE EQUIVALENCE)

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

Keep dividing the groups based on the principle in Step 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 }

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

Minimize the DFA using Partition Algorithm –

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

Minimize the DFA using Partition Algorithm –

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

In the above diagram, we can’t reach q 3 and thus we eliminate that –

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 }

In this case, we can see that a 5 state DFA is a minimal DFA.


STATE IP = 0 IP = 1

Q0 (initial state) Q1 Q5

Q1 Q6 Q2

Q2 (Final State) Q0 Q2

Q5 Q2 Q6

Q6 Q6 Q0

FA TO REGULAR EXPRESSION CONVERSION

There are two methods here –

1. Arden’s method
2. State Elimination Method

Arden’s Method

● This method can’t be applied to ϵ – NFA

● 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

Give the RE for the following FA –


Answer

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

● Deleting the dead states

So after minimization, we can remove q 4 which is a dead 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 –

q 1=ϵ +q 1. a+q 2.b (1)


q 2=q 1. a+q 2.b +q 3. a ( 2 )
q 3=q 2. a (3)
Now, it is just plain arithmetic –

● Substitute (3) in (2). This will give the value of q2 in terms of q1, a and b

● Substitute the expression for q2 in (1) and get q1 in terms of a and b

● Substitute the expression for q1 in the expression for q2 to get q2 in terms of a and b

● Substitute the final expression for q2 in (3) to get the RE

Final answer
¿
q 3=[ a+ a ( b+aa )¿ b ] a ( b +aa )¿ a

Question

Get the RE from the FA –


Answer

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)

STATE ELIMINATION PROBLEM

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,

ℜ=( ac +bd ) . ( e+ f + g+h )

Question

Answer
The thought process –

● First, we need to go from A to B and then to C

● To go from A to B, we apply string a

● Then, we can keep looping over B by giving either ba or c (any number of times)

● Then after that, we apply d to get to C

Thus,
¿
ℜ=a(ba+c )¿ d=( a c¿ b ) a c ¿ d

REGULAR EXPRESSION TO FA CONVERSION

Before proceeding further, one should note that if we have two regular languages R1 and R2, then

● R1+R2 will also be a regular language.

● R1.R2 will also be a regular language.


¿ ¿
● R 1 and R 2 are also regular languages.

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

In the above cases, we have gotten ϵ – NFA for the expressions.

Question
¿
Convert RE to FA - ℜ=0 1 +1

Answer

The order of construction goes something like –


¿
● First accommodate 1 in the FA
¿
● Then accommodate 0. 1 in the FA
¿
● Finally accommodate 0. 1 +1 in the FA
¿
We can get 1 as follows –

Next, we can concatenate 0 as follows –


Finally, we can OR the 1 as follows to get the final RE –

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 )

FINITE AUTOMATA WIHT OUTPUT

These are of 2 types –

● 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

● Mealy and Moore machines don’t have a final state.

● Mealy and Moore are deterministic machines (like DFA)

● 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 –

● O/P = 0 if the string ends with ab

● O/P = 1 if the string ends with aa

● O/P = 2 for all other cases

Answer

The above diagram is a Moore Machine. Let us take the case of a string abaaabb as input. Then the
output becomes –

INPUT CURRENT STATE OUTPUT

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 –

INPUT CURRENT STATE OUTPUT

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.

MOORE TO MEALY CONVERSION

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.

MEALY TO MOORE CONVERSION

Let us take the example above –

Step 1

Write all the state and output combinations –

● S1,0

● S1,1

● S1,2

● S2,0

● S2,1

● S2,2

Step 2

Write the transitions –

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

Construct Moore Machine from the following Mealy Machine –

Answer

Question
Convert the Mealy Machine to Moore Machine –

Answer

CLOSURE PROPERTIES OF REGULAR LANGUAGE

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

ϕ Anything L2 Since ϕ is a sub set of


everything, the union
will be L2
¿ ¿ ¿ ¿
a b n
a b
n
L1 Since a b is a super
set of a n b n, the union
will be L1 here.

( a+ b )¿ a ( a+b )¿ ( a+ b )¿ b ( a+b )¿ ( a+ b )+¿¿ In this case, L1 matches


strings with at least a
single a . On the other
hand, L2 matches
strings with at least a
single b . So apart from
ϵ , any combination of
a and b are accepted.
¿ ¿
a b ( a+ b )¿ L2 In this case, L2 is
¿
basically Σ . Thus,
answer is L2

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

● If L1 and L2 are regular, then the union will always be regular.

L1 L2 Is L1 U L2 Regular

Regular Regular Yes

Regular Non – Regular Maybe

Non – Regular Regular Maybe

Non – Regular Non – Regular Maybe

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

ϕ Anything L1 Since ϕ is a sub set of


everything, the
intersection will be L1
¿ ¿ ¿ ¿
a b a b
n n
L2 Since a b is a super
set of a n b n, the
intersection will be L2
here.

( a+ b )¿ a ( a+b )¿ ( a+ b )¿ b ( a+b )¿ ( a+ b )¿ ab ( a+b )¿ In this case, L1 matches


strings with at least a
single a . On the other
hand, L2 matches
strings with at least a
single b . So the
intersection will have
strings which accept ab
.
¿ ¿
a b ( a+ b )¿ L1 In this case, L2 is
¿
basically Σ . Thus,
answer is L1
¿
a ( aa )¿ L2 L2 is a sub-set of L1
¿ ¿
a b ab ab L1 needs to end with b
and L2 needs to start
with a. So, the
intersection will be ab
¿ ¿ ¿ ¿ ¿ ¿
a b b a a +b Here, the intersection
will be { ϵ , a , b }
¿ ¿

+ ¿¿ + ¿¿

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.

NOTE – If L1 and L2 are regular, then L 1∩ L 2 is also a regular language.

L1 L2 Is L1 ∩ L2 Regular

Regular Regular Yes

Regular Non – Regular Maybe

Non – Regular Regular Maybe

Non – Regular Non – Regular Maybe

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

NOTE – If L1 and L2 are regular languages, then L1 . L2 will also be regular.

Reversal Operator

To get the reversal of a language L, draw the DFA for that language. Then perform the following steps –

● Reverse the edge directions


● Interchange initial and final states

● 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,

abc abc abc abc abc


= = = = =ϕ
bca acb bac cab cba
Also, we have

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 }

Let’s take few more examples –


aaaaa
L= = { aa , aaa , aaaa }
{ a , aa , aaa }
¿
a ¿
L= ={ ϵ , a , aa , aaa … }=a
a

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 –

● Prefix of L - { ϵ , a , ab , abc , abcd }

● Suffix of L - { ϵ , d , cd , bcd , abcd }

● Substrings of L - { ϕ , ϵ , a ,b ,c , d , ab , bc , cd , abc ,bcd , abcd }

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 –

Thus, we have another answer –


¿
[ h−1 ( ab ) ] . h−1 ( a )=1 ¿ 0
Therefore, final answer –
h
−1
[ a ( ba )¿ ]=02¿ +1¿ 0

PUSH DOWN AUTOMATA (PDA) and CONTEXT FREE LANGUAGE (CFL)

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

Let us understand this using an example –

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 –

● S=¿ AB=¿ aB=¿ ab (Left Most Derivation)

● S=¿ AB=¿ Ab=¿ ab (Right Most Derivation)

Recursion

When the same state calls itself in a production. For example –

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.

CHAUNCY’S CLASSIFICATION OF GRAMMAR

There are four types of grammars –

LANGUAGE REPRESENTATION EXPLANATION


TYPE OTHER NAME
GENERATED

Recursive LHS can’t be ϵ but RHS can


Unrestricted +¿ →(V +T ) ¿
¿

Type 0 Enumerable (V +T ) be anything


Grammar
Language

LHS and RHS can be


Context anything except ϵ . Since
Context Sensitive +¿ →(V +T )
+ ¿¿
¿
Type 1 Sensitive (V +T ) LHS derives RHS, size of
Grammar
Language LHS should be less than or
equal to RHS.

The LHS should be a single


Context Free Context Free ¿
Type 2 V →(V +T ) variable and RHS can be
Grammar Language
anything.

¿ ¿ The first and second cases


V →V T ∨T
are called Left Linear
Regular
Type 3 Regular Grammar Or Grammar (LLG) and Right
Language
¿ ¿ Linear Grammar (RRG)
V →T V ∨T respectively

NOTE – For the types of ϵ – free grammar, we have –

T 3⊂T 2⊂T 1⊂T 0


Question

Find the type of 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.

Therefore, this is a Type 0 grammar.

Question

Find the type of grammar –

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.

CONTEXT FREE GRAMMAR (CFG) - Type 2 Grammar

Question

Write a CFG for language L={am b n∨m ,n ≥ 1 }.

Answer

Take –

A={ a m∨m≥ 1 }

B= { bn∨n≥ 1 }
Therefore, we get

S → AB
A → aA∨a
B→ bB∨b
Question

Write a CFG for language L= { ( a+b ) abb ( a+ b ) }


¿ ¿

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.

Let us consider a language L which has grammar S → AB . Then, we have


+¿¿
● For L , the grammar becomes S → AB∨SS

● For L , the grammar becomes S → AB|SS|ϵ


¿

In short, CFL are closed over Kleen Closure.

Question

Write a CFG for language L= { Set of all pallindrom strings of alphabets ( a , b ) }

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

Write CFG for language L= { ww∨w ∈ ( a+ b ) }


¿

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.

Now, let us write three CFGs below –

L →a∨b
A → LAL∨a
B→ LBL∨b
C → LLC ∨L
Here, we have

● L which gives either a or b

● A which gives all odd length strings with a in the middle

● B which gives all odd length strings with b in the middle

● C which gives all odd length strings

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.

Hence, the final answer is

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
¿

is not closed over complementation.

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,

● Q – Set of all states

● Σ – Set of alphabets

● δ – Transition Function

● S – Start State

● F – Final state

● k – Stack alphabets

● Z – Initial top of stack

The stack used in PDA can perform 3 operations –

● 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”.

For this example, we can write the equivalent transition as –

δ ( 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

Construct a PDA for language L= { an bn∨n ≥1 } .

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

● Whenever the string character is b , then pop from the stack

● At the end, we should have Z 0 in the stack and ϵ in the string

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

Construct a PDA for language L= { am bn ∨m, n ≥ 1∧m>n }.


Answer

Question

Construct a PDA for language L= {w w R ∨w ∈ ( a+b ) }


¿

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

Find the DCFL from the following languages –

Answer

Options 1 and 3 can be accomplished using a single stack without taking a guess anywhere. Thus, 1 and
3 are DCFL.

LANGUAGE TYPE OF CFL


m
a b c d
n n m
DCFL
m
a b c d
n m n
CFL
m
a b c d
m n n
DFCL
n
a b c d
n n n
CSL
¿
R
w w ∨w ∈ Σ CFL
¿
R
w cw ∨w ∈ Σ DCFL
¿
ww ∨w ∈ Σ CSL
R
w xw ∨w , x ∈ Σ
¿
DCFL. Basically, we can take w=ϵ and the string
becomes ϵxϵ =x . Thus the language is basically
¿
all strings under Σ which makes this a regular
language as well
+¿¿
R
w xw ∨w , x ∈ Σ DCFL. Here, the language boils down to -
+ ¿b¿

L=a ( a+b )+ ¿a+ b (a+ b) ¿

This is also a regular language which implies it is a


DCFL as well.

Question

Given the following PDA, find the type of language –

1. Regular Infinite Language


2. DCFL but not Regular
3. CFL but not DCFL
4. Finite Language

Answer

Let’s go step by step –

● At first, the stack has only Z 0.

● When the first character encountered is a , then push to the stack

● If the next character is a or b , then skip. There is atleast one skip operation in this PDA.

● If the last character is b , then pop the a from the stack.

In short, the language we get –


+ ¿b ¿
L=a ( a+b )
Thus, we can see that the language is Regular and Infinite.

Question
Given the following PDA, find the type of language –

Answer

{❑n ❑m ❑n+ m }
Question

Given the following PDA, find the type of language –

Answer
{}

NOTE – One of the above transitions is


which implies that without reading input and without looking at the top of the stac
Additionally, when we write
1 , ϵ∨ϵ , we are stating that the operation should push 1 on the stack irrespective of the top of the stack

CLOSURE PROPERTY

OPERATION DCFL CFL

Union Not Closed Closed

Intersection Not Closed Not Closed

Complementation Closed Not Closed


Difference Not Closed Not Closed

Concatenation Not Closed Closed

Reversal Not Closed Closed

Kleen Closure Not Closed Closed

Positive Closure Not Closed Closed

Prefix Closed Closed

Suffix Not Closed Closed

Subset Not Closed Not Closed

Union Example

Let us take 3 languages –

{❑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

Let us take the languages –

{❑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

Let us take the languages –

{❑n ❑n }
❑ {❑ ❑ }
m n
Here, both and ❑
are DCFL. Therefore, DCFL are CLOSED over complementation. This can also be realized by intuit

On the other hand, let us see the following examples –

{❑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

For any two languages and , the difference can be given by –


Since the difference involves intersection and we already know that DCFL and CFL are not closed over intersection, we ca

Concatenation Example

Let us take the examples below –

{❑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

Let us take a DCFL example –


n n n 2n
❑❑❑❑
This is a DCFL since if the string begins with , we need to check for ❑n ❑n
. On the other hand, if the languagen begins
2n with
, then we need to check for ❑ ❑
. Therefore, there is no confusion and discrepancy. However, let us reverse it –

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

Kleen Closure and Positive Closure

For Kleenand Positive Closure, we have –

❑¿||
+¿¿

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

Let us take the language –


n n
❑❑
Then the CFG of L will be –

The prefixes will be given by –


||
Here, we can see that both DCFLs and CFLs are CLOSED over Prefixes.

Suffix

Let us take the language –


n n k 2k
❑❑❑❑
- n n and ❑k ❑2 k . So in suffix of
The above language is a DCFL. Now we can have 2 suffixes❑
, we have some❑confusion since we don’t know how many
’s we need to match. Therefore, DCFLs are NOT CLOSED over suffix. H

Subset

Let us take the language –


¿
()
n n n
which is❑ ❑ ❑Therefore,
This is a DCFL. However, the language is a subset of
a CSL. DCFLs and CFLs are NOT CLOSED over Subset.

DECIDABLE PROBLEMS OF CFL

There are three main problems with CFL –

● Emptiness– If the given PDA is accepting Empty Language or not.

● Finiteness– If the given PDA is accepting Finite or Infinite Language

● Membership– If the input string is a part of the CFL or not.

Let us take the example of the production –


||

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

1. Take a grammar as input.


2. Eliminate the non – reachable states from the start symbol.
3. Eliminate the productions that don’t generate any strings
4.
If at this point there are still some productions in the grammar, then the grammar is non – empty.

Finiteness Algorithm

1. Take grammar as input.


2. Eliminate the non – reachable states from the start symbol.
3. Eliminate the productions that don’t generate any strings
4. Write the variable – variable relations (relationship graph) .
5. If there exists a cycle, the grammar is infinite.

Membership Algorithm

CYK algorithm explained in the end.

Question

Find the Emptiness and Finiteness of the following CFG –

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.

For finiteness problem, we draw the relationship graph as follows –

Since a cycle exists, the CFG is infinite.


TURING MACHINE

, we have –
This is basically a FA with 2 stacks instead of 1. So

A Turing Machine can be represented as –

()
Where,

– Set of states

– Set of alphabets

– Transition functions

– Start State

– Final State

– Tape (this is corollary to stack)

– Initial tape symbol(Blank symbol)

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

Let us take the following transitions –

In the above transitions –


1. First transition is going from State S1 to S2 if the tape pointer points to
. The transition is replacing the
with a and moving to the right.
2. Second transition is going from State S1 to S2 if the tape pointer points to
. The transition is replacing the
with a blank symbol and moving to the left.

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

Construct a TM for language{❑n } .

Answer

Here is the step – by – step process for this machine –

● The tape pointer starts at

● 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

Construct a TM for language{❑n } .

Answer
We can use the TM in three ways –

● TM as a language recognizer

● TM as a transducer (to perform calculations)

● TM as an enumerator (to print patterns)

TURING MACHINE LANGUAGES

There are two languages that TM accepts –

● 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

PROPERTY REC REL

Union Closed Closed

Intersection Closed Closed

Concatenation Closed Closed

Complementation Closed Not Closed

Subset Not Closed Not Closed

Inverse Homomorphism Closed Closed

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

Blank Tape Halting Problem

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

State Entry Problem

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

There are two types of infinites –

● 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

According to Cantor’s theorem, if is a set of countable infinite values, then the ()


will be uncountably infinite.

NOTE – Set of all Regular Languages is countable. So is the set of all CFLs, CSLs, Recursive Languages and RELs

List of Countable sets in TOC


¿
● ❑

● Set of Regular Languages

● Set of CFLs

● Set of CSLs

● Set of Recursive Languages


● Set of RELs

List of Uncountable sets in TOC

¿
● (❑ )

● Set of non – regular languages

● Set of non-CFLs

● Set of non-CSLs

● Set of non-recursive languages

● Set of non-regular languages

SIMPLIFICATION OF CFG

1. Elimination of null productions


2. Elimination of unit productions
3. Elimination of useless symbols

Elimination of Null Productions

Let us take the example as follows –


❑1
❑1 ❑1

In this case, we need to remove . So, we will replace all occurrences of❑1 with . This gives us –
❑1
❑1 ❑1

Let us take another example –


❑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 –

In this case, if and are both , then will also be . Thus –

Now, we follow the procedure and start replacing –


||||||
||

Elimination of Unit Productions

In this case, we use the basic transitive properties. For example –

In this case, we can directly write –

This is the elimination of unnecessary unit productions. Let us take another example –
||
The process we follow is that –

● In Production 3, we substitute for B

● In Production 2, we substitute for A

● In Production 1, we substitute for B

We get –
||
||
||
This gives us finally –
||
||
||
Elimination of Useless Symbol

Let us take the example as follows –

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

There are of two types –

● Chonsky Normal Form (CNF)

● Greibach Normal Form (GNF)

CNF

2 variables or 1 terminal. For example –

GNF

All the productions generate the productions as follow –


¿

Since❑¿ includes , a single terminal production is also allowed.

Question

Convert the following grammar to CNF –


Answer

First step is to get the simplified form. Here however, no more simplification is possible. Next, we just try to take addition

Question

Convert the following grammar to GNF –

||

Answer

First step is to get the simplified form. Here however, no more simplification is possible. Next, we just try to take addition

||
||

CLOSURE PROPERTIES SUMMARY

REG DCFL CFL CSL REC REL

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

CYK ALGORITHM – CFG MEMBERSHIP

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 .

First, we will label the characters in the string as follows –

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,

Strings ( 2 , 2 )=( 3 , 3 )=( 4 , 4 )=b


In the blocks, we put the variables that can derive the said string. So, string a can be derived from
variable A . Similarly, string b can be derived from variable B. Thus, we get –

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)

Hence, string abbb is accepted by the given CFG.

SOME SIMPLE POINTS


SUBSTRINGS

We know that for a string say GATE, then –

Trivial substrings={ ϵ ,GATE }


Non−trivial substrings= {G , A , T , E , GA , AT ,TE , GAT , ATE }
Therefore, when all the symbols are distinct, then –

n ( n+ 1 )
No of substrings= +1
2

DFA CONSTRUCTION

Suppose we have strings with sub-string S that is of length M . Then,

● 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

In addition to this, we can write the transition table as follows –

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

Here, n=3 and hence, we can write –


3
No of states=2 =8
3−1
No of final states=No of non−final states=2 =4
Let us assume –

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.

You might also like