TOC Notes
TOC Notes
1
Course Code/Title: CS3401/Theory of Computation
The set is a collection of elements or items. By giving proofs about the sets we try to prove certain properties
of the sets.
For example if there are two expressions A and B and we want to prove that both the expressions A and B are
equivalent then we need to show that the set represented by expression A is same as the set represented by
expression B. Let PUQ = QUR if we map expression A with PUQ and expression B with QUR then to prove
A = B we need to prove PUQ = QUP.
This proof is of the kind "if and only if" that means an element x is in A if and only if it is in B. We will
make use of sequence of statements along with logical justification in order to prove this equivalence.
Let us prove PUQ = QUP.
Example 1.3.2 Prove that if n is a positive integer such that n mod 4 is 2 or 3 then n is not a perfect
square. AU: Dec.-12, Marks 6
Solution: The method of contra positive is used. According to method of contra position for proving "If A
then B" we prove if not A then not B.
Here we prove - IF n is a perfect square then n mod (4) must be 0 or 1.
Consider that there exists a positive integer n such that n mod 4 is 0 or 1. Then n is a perfect square i.e. n =
m2, where m is another positive integer. Following are some cases -
1) If m mod 4 = 0 then m = 4i.
For instance - IF i = 1 then m is perfect square
Here i = 1, 4, 9, ...
2) If m mod 4 = 1, then m = 4i + 1.
Here i = 2, 6, 12, 20, ...
3) If m mod 4 = 2, then m = 4i + 2.
Here we fail to identify value of i that will make m as perfect square.
4) If m mod 4 = 3 then m = 4i + 3.
Again there does not exist any value of i which lead to m as perfect square. This proves that n is a positive
integer such that n mod 4 is 2 to 3 and n is not a perfect square.
3
Course Code/Title: CS3401/Theory of Computation
4
Course Code/Title: CS3401/Theory of Computation
Now,
2) Induction hypothesis -
Now we will assume n = k and will obtain the result for it. The equation then becomes,
1+2+3+ ... + k = (k(k+1)/2
3) Inductive step -
Now we assume that equation is true for n = k. And we will then check if it is also true for n = k + 1 or not.
Consider the equation assuming n = k + 1
L.H.S. = 1+ 2+ 3... + k + k + 1 equation (1)
k(k+1)/2 +k+1
= k(k + 1) + 2(k + 1)/2
= (k+ 1) (k + 2)/2 taking common factor
i.e. = (k + 1) (k + 1 + 1)/2
= R.H.S.
Example 1.4.3 Prove that 12 + 22 + 32 + ... + n2 = Σni=1 i2 = n(n + 1) (2n + 1)/6 using mathematical
induction. AU: May-07, 16, Marks 6, May-14, Marks 8
Solution: We will first prove it by basis of induction and then will consider induction hypothesis.
1. Basis of induction -
Let n = 1
L.H.S. = 12 = 1
R.H.S. = 1(1+1)(2.1+1) /6 = 6/6
R.H.S. = 1
As L.H.S. = R.H.S it is proved for n = 1.
2. Induction hypothesis - Let, n = k.
5
Course Code/Title: CS3401/Theory of Computation
Then
12 + 22 + …….. + k2 = Σki=1 i2 = k(k+1)(2k+1) /6 is true
Now, we will try to prove it for n = k + 1. Hence we will substitute n by k + 1.
12 + 22 + 32 +............+ (k+1)2 = Σk+1i=1 i2 = (k+1)(k+1+1)(2(k+1)+1) /6
L.H.S. = 12 + 22 + 32 +...... + k2 + (k+1)2
We will substitute equation (1) and we will get,
= (k+1) [k(2k+1)+6k+6 /6] = (k+1) [2k2+k+6k+6 /6]
= (k+1) [2k2 +7k+6 /6] = (k+1)(2k2+4k+3k+6) /6
= (k+1)(2k(k+2)+3(k+2)) /6 = (k+1)((2k+3)(k+2)) /6
= (k+1)((k+2)(2k+3)) /6 = = (k+1)(k+1+1)(2(k+1)+1) /6
= R.H.S.
As L.H.S. = R.H.S., it is true that using n = k + 1 the given expression can be proved.
6
Course Code/Title: CS3401/Theory of Computation
7
Course Code/Title: CS3401/Theory of Computation
8
Course Code/Title: CS3401/Theory of Computation
i.e.
2k+1 <= (k+1)! is true.
As it is true for n = k + 1, the given expression is proved.
Example 1.4.8 Prove that for every integer n≥0 the number 42n+1 + 3n+2 is multiple of 13.
AU: May-08, 14, Marks 10
Solution: We will prove this using method of induction.
Basis - Assume
n=1
P(1) = 42(1)+1+31+2
= 43 + 33
= 91 which is = 13×7
Hence 42n+1 + 3n+2 is multiple of 13 when n = 1.
Inductive step - Assume n = k
P(k) = 42k+1 + 3k+2
P(k) = 13 m
where m is some integer.
Thus we assume that given expression is true for P(k). Now we will prove that is also true for P(k+1).
Let, n = k +1 then
P(k+1) = 42(k+1)+1 + 3(k+1)+2
We can simplify it as -
= 4(2k+1)+2 + 3(k+2)+1
= 42. 42k+1+42. (3k+2 - 3k+2) + 31.3k+2
equal to zero
Taking common factors as 42 and 3k+2 we get
= 42 (42k+1 + 3k+2) + 3k+2 (-42+3)
= 42 (13m) + 3k+2(-13).
equation (1)
P(k + 1) = 13 (42m - 3k+2)
That is multiple of 13, for P(k + 1).
This proves that given integer is multiple of 13.
9
Course Code/Title: CS3401/Theory of Computation
10
Course Code/Title: CS3401/Theory of Computation
• For example: suppose current state is q, and suppose reader is reading the symbol 'a' then it is finite control
which decides what will be the next state at input 'a'. The transition from current state q with input w to next
state q' producing w' will be represented as,
(q, w) Ⱶ (q', w')
If w is a string and M is a finite automata, then w is accepted by the FA
iff (w, s) Ⱶ (q, ε)
with q as final state.
11
Course Code/Title: CS3401/Theory of Computation
• The set of strings accepted by a FA given by M then M is accepted by language L. The acceptance of M by
some language L is denoted by L (M).
• A machine M accepts a language L iff,
L = L (M).
Representation of Finite Automata
The strings and languages can be accepted by a finite automata, when it reaches to a final state. There are
two preferred notations for describing automata :
1. Transition diagram
A transition diagram or transition graph can be defined as collection of –
1) Finite set of states K.
2) Finite set of symbols Σ.
3) A non empty set S of K. It is called start state.
12
Course Code/Title: CS3401/Theory of Computation
Another example:
The rows of the table corresponds to states and columns of the table correspond to inputs. Here q0 is start
state, q2 is final state.
Types of Automata
13
Course Code/Title: CS3401/Theory of Computation
From state S0 for input 'a' there is only one path, going to S2. Similarly from S0 there is only one path for
input b going to S1.
The DFA can be represented by the same 5-tuples described in the definition of FSM.
Definition of DFA
A deterministic finite automation is a collection of following things -
1) The finite set of states which can be denoted by Q.
2) The finite set of input symbols Σ.
3) The start state q0 such that q0 ϵ Q.
4) A set of final states F such that F ϵ Q.
5) The mapping function or transition function denoted by δ. Two parameters are passed to this transition
function: One is current state and other is input symbol. The transition function returns a state which can be
called as next state.
For example, q1 = δ (q0, a) means from current state q0, with input a the next state transition is q1.
In short, the DFA is a five tuple notation denoted as :
A = (Q, Σ, δ, q0, F)
The name of DFA is A which is a collection of above described five elements.
Example 1.6.1 Design a FA which checks whether the given binary number is even.
Solution:
14
Course Code/Title: CS3401/Theory of Computation
• Logic: The binary number is made up of 0's and 1's when any binary number ends with 0 it is always even
and when a binary number ends with 1 it is always odd. For example,
0100 is a even number, it is equal to 4.
0011 is a odd number, it is equal to 3.
• Design: While designing FA we will assume one start state one state ending in 0 and other state for ending
with 1. Since we want to check whether given binary number is even or not, we will make the state for 0, as
the final state. (Refer Fig. 1.6.2)
Simulation: The FA indicates clearly S1 is a state which handles all the 1's and S2 is a state which handles all
the 0's. Let us take some input.
Example 1.6.2 Design FA which checks whether the given unary number is divisible by 3.
Solution:
15
Course Code/Title: CS3401/Theory of Computation
• Logic :
The unary number is made up of ones. The number 3 can be written in unary form as 111, number 5 can be
written as 11111 and so on.
The unary number which is divisible by 3 can be 111 or 111111 or 111111111 and so on.
• Design
• Simulation
Consider a number 111111 which is equal to 6 i.e. divisible by 3. So after complete scan of this number we
reach to final state q3.
Start 111111
State q0
1q1 11111
11q2 1111
111q3 111
1111q1 11
11111q2 1
111111q3 → Now we are in final state.
Example 1.6.3 Design FA to check whether given decimal number is divisible by three.
Solution:
• Logic :
To determine whether the given decimal number is divisible by three, we need to take the input number digit
by digit.
Also, while considering its divisibility by three, we have to consider that the possible remainders could be 1,
2 or 0. The remainder 0 means, it is divisible by 3. Hence from input set {0, 1, 2, …. 9} (since decimal
number is a input), we will get either remainder 0 or 1 or 2 while testing its divisibility by 3. So we need to
group these digits according to their remainders.
16
Course Code/Title: CS3401/Theory of Computation
• Simulation:
Let us test the above FA, if the number is 36 then it will proceed by reading the number digit by digit.
Hence the number is divisible 3, isn't it ? Similarly if number is 121 which is not divisible by 3, it will be
processed as
S 121
1 S1 21
17
Course Code/Title: CS3401/Theory of Computation
12 S0 1
121 S1 which is remainder 1 state.
Example 1.6.4 Design FA which checks whether a given binary number is divisible by three.
Solution:
• Logic:
We will consider the input digit by digit.
While considering the divisibility by 3, we should think of three types of states, namely-
remainder 0 i.e. S0 state
remainder 1 i.e. S1 state
remainder 2 i.e. S2 state
• Design:
• Simulation:
Let us take some number 010 which is equivalent to 2. We will scan this number from MSB to LSB.
0 1 0
↑ ↑ ↑
S0 S1 S2
Then we will reach to state S2 which is remainder 2 state. Similarly for input 1001 which is equivalent to 9
we will be in final state after scanning the complete input.
1 0 0 1
↑ ↑ ↑ ↑
S1 S2 S1 S0
Thus the number is really divisible by 3.
We can also represent the simulation in following manner -
18
Course Code/Title: CS3401/Theory of Computation
Example 1.6.5 Consider the finite automata transition table shown below with F = {q0} AU: Dec.-10,
Marks 10
• The sets accepted by this language are (0011, 00, 11, 1010, 0101, ... }
• This shows that all the strings contain even number of 0's and even number of 1's. Hence the language
accepted by this finite automata contains "all the strings having even number of O's and even number of 1's".
Example 1.6.6 Design FA to accept L, where L = {Strings in which a always appears trippled) over the set Σ
= {a, b}. AU: Dec.-10, Marks 10
Solution:
• Logic: For this particular language the valid strings are aaab, baaaaaa, bbaaab and so on. The a always
appears in a clump of 3.
19
Course Code/Title: CS3401/Theory of Computation
Note that in Fig. 1.6.8 the sequence of tripple a is maintained to reach to the final state.
• Design
Example 1.6.7 Construct a DFA that accepts all the strings on {0, 1} except those containing the substring
101.
AU: May-07, Marks 6, May-14, Marks 8
Solution :
• The simplest method to construct such DFA is to construct DFA for the language containing the substring
101.
• Now just change the non-final states to final states and make final state as non-final state. The DFA then
becomes shown in Fig. 1.6.9.
• is a DFA that does not accept the language containing substring 101.
20
Course Code/Title: CS3401/Theory of Computation
Example 1.6.8 Define DFA. Design a DFA to accept the binary numbers which are divisible by 5.
Solution: i) DFA - Refer section 1.6.
ii) DFA for divisibility by 5: For designing DFA as a divisibility by 5 tester for a binary string we will
consider following states.
q0 - remainder 0 state
q1 - remainder 1 state
q2 - remainder 2 state
q3 - remainder 3 state
q4 - remainder 4 state
Now we will design DFA by considering above states and finding next states with input 0 and 1.
The DFA will be
Example 1.6.10 Construct a DFA accepting all string w over {0, 1}, such that number of 1's in w is 3 mod
4. AU: Dec.-11, Marks 8
22
Course Code/Title: CS3401/Theory of Computation
Example 1.6.11 Give deterministic finite automata accepting the following language over the alphabet.
1) Number of 1's is a multiple of 3.
2) Number of 1's is not a multiple of 3. AU: Dec.-13, Marks 8
Solution: 1) We assume alphabet as input set Σ = {1}
2) For recognizing number of 1's not a multiple of 3, we will use the DFA in step (1). That means we will
make non final states as final and final state as non-final ones. The DFA will then be as shown in figure.
Example 1.6.12 Given Σ = (a, b), construct a DFA which recognize the language
L= {bm abn :m,n>0}. AU Dec.-16, Marks 6
Solution:
• The m and n are > 0 and there is no restriction on number of m and n. In other words any number of m and
n are allowed except null.
• Hence we can rewrite given L as
L = b+ ab+
• From this L we can construct DFA as
23
Course Code/Title: CS3401/Theory of Computation
Example 1.6.13 Construct a DFA to accept strings over (a, b), such that every block of length five contains
at least two a's. Use extended transition of function to trace a string W = aabba.
Solution: The following DFA contains block of five symbols. The DFA loops back when the block ends. At
each state both the a and b symbols are considered. The state names indicate the total number of a's so far
visited. For instance if the state name is 1 then it indicates that there is single a being read. When two a's are
read then it leads to a final state. The DFA will be shown in Fig. 1.6.12.
Example 1.6.14 Design a DFA which accept strings of O's and 1's which when interpreted as a binary
integer is multiple of 5. Also give the sequence of states that DFA is in while processing the input string:
1001011.
Solution: The required DFA can be drawn as follows.
Processing of 1001011
24
Course Code/Title: CS3401/Theory of Computation
q0 Ⱶ 1001011
1 q1 Ⱶ 001011
10 q2 Ⱶ 01011
100 q4 Ⱶ 1011
1001 q4 Ⱶ 011
10010 q3 Ⱶ 11
100101 q2 Ⱶ 1
1001011 q0 Ⱶ Accept.
25
Course Code/Title: CS3401/Theory of Computation
• Thus it is not fixed or determined that Fig. 1.7.1 Non-deterministic finite automata with a particular input
where to go next. Hence this FA is called non-deterministic finite automata.
26
Course Code/Title: CS3401/Theory of Computation
Example 1.7.1 Construct a NFA for a language L which accepts all the strings in which the third symbol
from right end is always a. Over Σ= {a, b}.
Solution:
• Logic: The strings in such a language are of the form
Thus we get third symbol from right end as 'a' always. The NFA then can be
• Design:
The above figure is a NFA because in state q0 with input 'a' we can go to either state q0 or state q1.
Example 1.7.2 Draw state transition diagram for FA over {a, b} containing substring aabb. AU:
May-09, Marks 4
Solution: The FA can be
Example 1.7.3 Construct NFA accepting binary strings with two consecutive 0's. AU: May-12, Marks 8
Solution: For constructing this NFA the input set Σ ={0,1}. The NFA will be -
27
Course Code/Title: CS3401/Theory of Computation
• In this NFA with ε, q0 is a start state with input 0 one can be either in state qo or in state q1. If we get at the
start a symbol 1 then with ε - move we can change state from q0 to q1 and then with input we can be in state
q1.
• On the other hand, from start state q0, with input 1 we can reach to state q2.
• Thus it is not definite that on input 1 whether we will be in state q1 or q2. Hence it is called
nondeterministic finite automata (NFA) and since there are some ε moves by which we can simply change
the states from one state to other. Hence it is called NFA with ε.
Definition of NFA with ε
The language L accepted by NFA with &, denoted by M = (Q, Σ, δ, q0, F) can be defined as:
Let, M = (Q, Σ, δ, q0, F) be a NFA with ε.
Where
Q is a finite set of states.
Σ is input set.
δ is a transition or a mapping function for transitions from Q×{ΣUε} to 2Q.
q0 is a start state.
F is a set of final states such that F ϵ Q.
Example 1.8.1 Construct NFA with ε which accepts a language consisting the strings of any number of a's
followed by any number of b's. Followed by any number of c's.
Solution: Here any number of a's or b's or c's means zero or more in number. That means there can be zero or
more a's followed by zero or more b's followed by zero or more c's. Hence NFA with ε can be -
Normally ε's are not shown in the input string. The transition table can be -
28
Course Code/Title: CS3401/Theory of Computation
29
Course Code/Title: CS3401/Theory of Computation
Solution:
ε - closure (q0) = {q0, q1, q2} means self state + ε - reachable states.
ε - closure (q1) = {q1, q2} means q1 is a self state and q2 is a state obtained from q1 with ε input.
ε - closure (q2) = {q2}
30
Course Code/Title: CS3401/Theory of Computation
3. Marks qi
4. for each symbol a
{
5. U = ε - closure (δ(qi, a))
6. if (U is not in D states)
{
7. Add U as unmarked state in D states.
}
8. Dtran [qi, a] = U
}
}
Example for Understanding
Example 1.9.1 Convert the given NFA with ɛ to NFA without ε. AU: Dec.-09, Marks 8
Solution: Step 1: We will first obtain ε - closure of each state i.e. we will find out ε - reachable states from
current state.
Hence
ε - closure (q0) = {q0, q1, q2}
ε - closure (q1) = {q1, q2}
ε - closure (q2) = {q2}
As ε - closure (q0) means with null input (no input symbol) we can reach to q0, q1 or q2. In a similar manner
for q1 and q2 ε - closures are obtained.
Step 2: Now we will obtain δ' transitions for each state on each input symbol.
δ' (q0, 0) = ε - closure (δ (δ' (q0, ε), 0))
= ε - closure (δ(ε - closure(q0), 0))
= ε - closure (δ(q0, q1, q2), 0).
= ε - closure (δ(q0, 0) U δ(q1, 0) U δ (q2,0))
= ε - closure (q0 U ϕ U ϕ)
= ε - closure (q0) = {q0, q1, q2}
32
Course Code/Title: CS3401/Theory of Computation
Step 4:
34
Course Code/Title: CS3401/Theory of Computation
Solution: We will first obtain ε - closure of every state. The ɛ - closure is basically an ε - transition from one
state to other. Hence
ε - closure (0) = {0}
ε - closure (1) = {1}
ε - closure (2) = {2, 3, 4, 6, 9}
ε - closure (3) = {3, 4, 6}
ε - closure (4) = {4}
ε - closure (5) = {5, 8, 3, 4, 6, 9}
= {3, 4, 5, 6, 8, 9} sorted it!
ε - closure (6) = {6}
ε - closure (7) = {7, 8, 3, 4, 6, 9}
= {3, 4, 6, 7, 8, 9}
35
Course Code/Title: CS3401/Theory of Computation
Now we will build the transition table using above calculated δ' transitions.
State {2} is a final state since ε - closure (2) contains 9 which is actually a final state in given NFA. Similarly
state 5, 7, 8 and 9 are final states.
Example 1.9.3 Convert the following NFA with & to NFA without ɛ.
40
Course Code/Title: CS3401/Theory of Computation
41
Course Code/Title: CS3401/Theory of Computation
States q1 and q2 becomes the final as closures of q1 and q2 contains the final state q2. The NFA can be shown
by transition diagram as shown in Fig. 1.9.5.
42
Course Code/Title: CS3401/Theory of Computation
Example 1.9.5 Construct an NFA without ε transitions for the NFA given in Fig. 1.9.7. AU May-12,
Marks 8
Review Question
1. If L is accepted by a NFA with & transition then show that L is accepted by an NFA without ε transition.
AU: May-04, Dec.-09, Marks 8; May-09, Marks 12, Dec.-12, Marks 8
Equivalence of NFA and DFA
AU: May-05,12,13,14,16,18, Dec.-04,06,14,15,16,19, Marks 13
NFA can be converted to equivalent DFA. Following theorem illustrates this concept.
Theorem: Let L be a set accepted by non-deterministic finite automation. Then there exists a deterministic
finite automation that accepts L.
OR
Prove that "A language L is accepted by some DFA if and only if L is accepted by some NFA". AU: Dec.-15,
Marks 10, AU: May-05, Dec.-14, Marks 10
Proof :Let
M = (Q, Σ, δ, q0, F) be an NFA for language L. Then define DFA M' such that,
M' = (Q', Σ`, δ', q0`, F')
The states of M' are all the subset of M'. The Q'=2Q.
F' be the set of all the final states in M.
The elements in Q' will be denoted by [q1, q2, q3, ... qi] and the elements in Q are denoted by {q0, q1, q2,
… }The [q1, q2, ... qi] will be assumed as one state in Q' if in the NFA q0 is a initial state it is denoted in DFA
as q0` = [q0]. We define,
δ' ([q1, q2, q3, … qi], a) = [P1, P2, P3, ... Pj]
45
Course Code/Title: CS3401/Theory of Computation
From the transition table we can compute that there are [q0], [q1], [q0, q1] states for its equivalent DFA. We
need to compute the transition from state [q0, q1].
47
Course Code/Title: CS3401/Theory of Computation
49
Course Code/Title: CS3401/Theory of Computation
50
Course Code/Title: CS3401/Theory of Computation
The final state F' = { [s], [p, q, r, s], [p, q, s], [p, r, s], [p, s] }
The transition graph shows two disconnected parts. But part I will be accepted as final DFA because it
consists of start state and final states, in part II there is no start state. Refer Fig. 1.10.3 (See Fig. 1.10.3 on
next page):
Example 1.10.4 Convert the following NFA to a DFA . AU: May-13, Marks 10
51
Course Code/Title: CS3401/Theory of Computation
52
Course Code/Title: CS3401/Theory of Computation
The states [p, r] and [p, q, r] are final states as they contain [r].
The DFA can be represented by the transition diagarm as shown -
The states [q] and [r] are eliminated as they are dead states.
Example 1.10.5 Construct a DFA accepting binary strings such that the third symbol from the right end is
1. AU May-12, Marks 10
Solution: The strings in such a language are -
53
Course Code/Title: CS3401/Theory of Computation
54
Course Code/Title: CS3401/Theory of Computation
Example 1.10.6 Construct a non-deterministic finite automaton accepting the set of strings over {a,b}
ending in aba. Use it to construct a DFA accepting the same set of strings. AU: May-14, Marks 10
Solution: NFA for accepting the strings ending with aba is,
55
Course Code/Title: CS3401/Theory of Computation
δ (q0, b) = {q0}
δ (q1, a) = ϕ
δ (q1, b) = {q2}
δ (q2, a) = {q3}
δ (q2, b) = ϕ
δ (q3, a) = ϕ
δ (q3, b) = ϕ
The δ transitions on newly generated states is
δ ([q0, q1], a) = {q0, q1} i.e. [q0, q1]
δ ([q0, q1], b) = {q0, q2} = [q0, q2] new state
δ ([q0, q2], a) = {q0, q1, q3} = [q0, q1, q3] new state
δ ([q0, q2], b) = {q0}
δ ([q0, q1, q3], a) = [q0, q1]
δ ([q0, q1, q3], b) = [q0, q2]
As no new state is getting generated. We will build transition table is as follows -
56
Course Code/Title: CS3401/Theory of Computation
Review Question
1. Prove that "A language L is accepted by some DFA if and only if L is accepted by some NFA". AU
Dec.-15, Marks 10
Equivalence of NFA with ε to DFA
AU: May-05, 11, Dec.-06,13,15,18, Marks 16
Prove that there exists a DFA for every ε - NFA.
AU: May-11, Marks 8
Step 1: Consider M = (Q, Σ, δ, q0, F) is a NFA with ε. We have to convert this NFA with ε to equivalent DFA
denoted by
57
Course Code/Title: CS3401/Theory of Computation
Solution:
Step 1: To convert this NFA we will first find ɛ-closures.
ε - closures {q0} = {q0, q1, q2}
ε - closures {q1} = {q1, q2}
ε - closures {q2} = {q2}
Step 2: Let us start from ε - closure of start state
ε - closure {q0} = {q0, q1, q2} we will call this state as A.
Now let us find transitions on A with every input symbol.
δ (A, a) = ε-closure (δ (A, a))
= ε - closure (δ (q0, q1, q2), a)
= ε - closure {(q0, a) U δ (q1, a) U δ (q2, a)}
= ε - closure {q1}
= {q1, q2). Let us call it as state B.
δ (A, b) = ε-closure (δ (A, b))
= ε - closure (δ (q0, q1, q2), b)
58
Course Code/Title: CS3401/Theory of Computation
Example 1.11.2 Convert the given NFA into its equivalent DFA -
= ε - closure {q0}
= {q0, q1, q2} i.e. state A
δ' (A, 1) = ε - closure { δ ((q0, q1, q2), 1)}
= ε - closure {(q0,1) U δ (q1,1) U δ (q2,1)}
= ε - closure {q1, q2}
= {q1, q2} Call it as state B
δ' (A, 2) = ε - closure {δ ((q0, q1, q2), 2)}
= ε - closure {δ (q0, 2) U δ (q1, 2) U δ (q2, 2)}
= ε - closure {q2}
= {q2} Call it as state C.
Thus we have obtained,
δ` (A, 0) = A
δ` (A, 1) = B
δ` (A,2) = C
i.e.
Now we will find transitions on states B and C for each input.
Hence,
δ' (B, 0) = ε - closure {δ (q1, q2), 0}
= ε - closure {δ (q1, 0) U δ (q2, 0)}
= ε - closure {ϕ}
=ϕ
δ' (B, 1) = ε - closure {δ (q1, q2), 1}
= ε - closure {δ (q1, 1) U δ (q2, 1)}
= ε - closure {q1}
60
Course Code/Title: CS3401/Theory of Computation
61
Course Code/Title: CS3401/Theory of Computation
State A
62
Course Code/Title: CS3401/Theory of Computation
65
Course Code/Title: CS3401/Theory of Computation
As state A = {p} it is a start state and states C and D contain final state r, hence these are final states. The
transition diagram for the DFA is,
Example 1.11.4 Consider the following ε - NFA for an identifier. Consider the ε -closure of each state and
find it's equivalent DFA. AU: Dec.-15, Marks 10
Solution:
ε - closure (1) = {1} Call it as state A
δ' (A, letter) = ε - closure(δ (1, letter)
= ε - closure(2)
= {2, 3, 4, 5, 8, 10} Call it as state B
δ' (A, letter) = B
δ' (A, digit) = ε - closure(δ (1, digit))
=ϕ
δ' (A digit) = ϕ
δ' (B, letter) = ε - closure(δ (2, 3, 4, 5, 8, 10), letter)
= ε – closure (6)
= {6, 7, 4, 5, 8, 10) i.e.
= {4, 5, 6, 7, 8, 10} Call it as state C
δ' (B, letter) = C
δ' (B, digit) = ε - closure(&(2, 3, 4, 5, 8, 10), digit)
= ε – closure (9)
= {9, 7, 4, 5, 8, 10} i.e.
= {4, 5, 7, 8, 9, 10} Call it as state D
δ' (B, digit) = D
δ' (C, letter) = ε - closure(δ (4, 5, 6, 7, 8, 10), letter)
66
Course Code/Title: CS3401/Theory of Computation
= ε – closure (6)
=C
δ' (C, letter) = C
δ' (C, digit) = ε - closure(δ (4, 5, 6, 7, 8, 10), digit)
= ε – closure (9)
=D
δ' (C, digit) = D
δ' (D, letter) = ε - closure(δ (4, 5, 7, 8, 9, 10), letter)
= ε - closure (6)
=C
δ' (D, letter) = C
δ' (D, digit) = ε - closure(δ (4, 5, 7, 8, 9, 10), digit)
= ε - closure (9)
=D
δ' (D, digit) = D
The two states C and D are equivalent. Similarly state B and C are equivalent
B = C = D.
The minimized DFA will then be
Example 1.11.5 Convert the following ε - NFA to NFA and then convert the resultant NFA to DFA.
AU Dec.-18, Marks 13
67
Course Code/Title: CS3401/Theory of Computation
68
Course Code/Title: CS3401/Theory of Computation
70
Course Code/Title: CS3401/Theory of Computation
δ (R, 1) = {C}
δ (C, 0) = ϕ
δ (C, 1) = C
The transition table will be
Review Questions
1. Let L be a set accepted by NFA then prove that there exists a deterministic finite automaton that accepts L.
Is the converse true? AU: May-05, Marks 10
2. Prove that a language L is accepted by some ε - NFA if and only if L is accepted by some DFA. AU: Dec.-
06, Marks 8
3. Prove the equivalence of NFA and DFA using subset construction. AU: Dec.-13, Marks 8
Minimization of DFA
AU Dec.-18, May-13, 14, 16, Marks 10
The minimization of FSM means reducing the number of states from given FA. Thus we get the FSM with
redundant states after minimizing the FSM.
While minimizing FSM we first find out which two states are equivalent we can represent those two states
by one representative state.
71
Course Code/Title: CS3401/Theory of Computation
The two states q1 and q2 are equivalent if both δ (q1, x) and δ (q2, x) are final states or both of them are non
final states for all x ϵ Σ* (Σ* indicate any string of any length) we can minimize the given FSM by finding
equivalent states.
Example 1.12.1 Minimize the DFA as given below. AU May-16, Marks 8
Solution :
We will build the transition table for given DFA, as follows:
Step 1: We will build a table in following format for finding equivalent states and mark final and non-final
states with x
72
Course Code/Title: CS3401/Theory of Computation
Step 2: The states that need to be marked are (A, B), (A, D), (A, E), (A, F), (A, G), (A, H), (B, D), (B, E),
(B, F), (B, G), (B, H), (D, E), (D, F), (D, G), (D, H), (E, F), (E, G), (E, H), (F, G), (F, H), (G, H).
If we observe transition table then
δ (B, 0) = G
δ (B, 1) = C
δ (H, 0) = G
δ (H, 1) = C
Similarly
δ (D, 0) = C
δ (D, 1) = G
δ (F, 0) = C
δ (F, 1) = G
Thus pairs (B, H) and (D, F) are equivalent
Step 3:
Step 4:
Now, consider pair (A, E)
73
Course Code/Title: CS3401/Theory of Computation
74
Course Code/Title: CS3401/Theory of Computation
Thus we get equivalent pairs as (A, E), (B, H), (D, F). Hence the minimized DFA will be,
75
Course Code/Title: CS3401/Theory of Computation
AU Dec.-18, Marks 7
Solution: We will construct table as follows
We will mark final and non-final state by x
Now we will find the equivalence of the pairs (3, 4), (3, 5), (3, 6), (4, 5), (4, 6),(5, 6),
Pair (3, 4)
δ (3, a) = 6 δ (3, b) = 7 → Final, Non Final Pair
δ (4, a) = 5 δ (4, b) = 4 → Final, Non Final Pair
Hence put x in (3, 4)
Pair (3, 5)
76
Course Code/Title: CS3401/Theory of Computation
Pair (5, 6)
δ (5, a) = 7 δ (5, b) = 5 → Final, Non Final Pair
δ (6, a) = 2 δ (6, b) = 7 → Final, Non Final Pair
Hence put x in (5, 6)
Thus we do not get any of the mentioned pairs as equivalent pair.
The only equivalent pairs are (1, 2), (1, 7), (2, 7)
We assume state 1 = 2 = 7. We eliminate state 2 and 7 by replacing them by 1.
The minimized DFA can be given by following transition table.
77
Course Code/Title: CS3401/Theory of Computation
But, since state 4 and 5 are not reachable from start state 1, we eliminate them. Also, state 6 is a dead state.
Hence, minimizes DFA is
Example 1.12.3 Define distinguishable and indistinguishable states. Using table filling method, minimize the
following DFA. Draw the transition diagram of resulting DFA.
78
Course Code/Title: CS3401/Theory of Computation
Solution: Distinguishable and indistinguishable states: If for some input string w, δ (p w) gives an accepting
state and δ (q, w) gives a non-accepting state or vice versa then states p and q are called distinguishable
states or non-equivalent states.
If for some input string w, δ (p, w) and δ (q, w) both produces either accepting states or non-accepting states,
then states p and q are called indistinguishable or equivalent states.
We will apply a table filling method for minimizing the given DFA.
We will mark X in a final and non-final pairs. Now we will consider each pair and check for its equivalence
consider pair G, H.
δ (G, 0) = H and δ (G, 1) = B
δ (H, 0) = I and δ (H, 1) = C
Since pairs (H, I) and (B, C) are already marked X, pair (G, H) is not equivalent so we will mark X in pair
(G, H).
79
Course Code/Title: CS3401/Theory of Computation
80
Course Code/Title: CS3401/Theory of Computation
Consider pairs (D, H), (D, G), (D, F), (D, E) out of which
δ (D, 0) = E and δ (D, 1) = H
δ (H, 0) = I and δ (H, 1) = C
Hence put X in (D, H),
δ (D, 0) = E δ (D, 1) = H
δ (G, 0) = H δ (G, 1) = B
Pair (E, H) is equivalent. For pair (H, B) we will find their input transitions. Both state H and state B yield
final states we say pair (D, G) is equivalent at the same time we find pair (H, B) as equivalent.
δ (D, 0) = E and δ (D, 1) = H
δ (F, 0) = G and δ (F, 1) = B
As pair (E, G) is having X we will put X in (D, F).
Consider pair (D, E),
δ (D, 0) = E δ (D, 1) = H
δ (E, 0) = F δ (E, 1) = I
Pair (D, E) is not matching, put X in (D, E).
Consider pair (B, G),
δ (B, 0) = C δ (B, 1) = F
δ (G, 0) = H δ (G, 1) = B
As pair (C, H) is not equivalent put X in (B, G).
Consider pair (B, F),
δ (B, 0) = C δ (B, 1) = F
δ (F, 0) = G δ (F, 1) = B
81
Course Code/Title: CS3401/Theory of Computation
82
Course Code/Title: CS3401/Theory of Computation
Example 1.12.4 Minimize the Finite automaton Fig. 1.12.2 below and show both the given and the reduced
one are equivalent.
83
Course Code/Title: CS3401/Theory of Computation
AU May-14, Marks 10
Solution: We will first construct the transition table for given DFA -
Now we will partition the states Q in Q10 and Q20. The state q4 is a final state. Hence
Q10= F = {q4}
Q20 = Q – Q10 = {q0, q1, q2, q3)
П0 = {{q4}, {q0, q1, q2, q3}}
Now we will compare q0 with q1. We find that under 0 column we get q1 and q2. And q1 and q2 lie in the same
set i.e. Q2. But under 1-column of q0 and q1 we get q3, q4 respectively. But q3 ϵ Q20 and q4 ϵ Q10. Hence they
are not 1- equivalent.
Similarly compare q0 and q2. We will find that under 0 column of q0 and q2 we get q1 only. But under 1-
column of q0 and q2 we get q3 and q4. The q3 ϵ Q2 and q4 ϵ Q1.
Hence q0 and q2 are not 1 - equivalent. Similarly q0 is not 1 - equivalent to q3 because of 1 column entries.
84
Course Code/Title: CS3401/Theory of Computation
Q'1 = {q4}
Q'2 = {q0}
Q'3 = {q1, q2, q3}
Hence, II1 = {{q4}, {q0}, {q1, q2, q3}}
Now in set (q1, q2, q3) we compare q1 with q2 under 0-column we get q2 and q1 which are in the same set, and
under 1 - column we get q4. And for state q1 and q3 we get same entries under 0 column and under 1 -
column. Hence we can group them {q2, q3}.
The equivalence class becomes,
П2 = {{q4}, {q0}, {q1}, {q2, q3}}
Now further we cannot partition any of the set. We get
П3 = {{q0}, {q2}, {q4}, {q1, q3}}
Review Question
1. Discuss on the relation between DFA and minimal DFA. AU: May-13, Marks 6
85
Course Code/Title: CS3401/Theory of Computation
86
Course Code/Title: CS3401/Theory of Computation
87
Course Code/Title: CS3401/Theory of Computation
q0 remainder 0 state.
q1 remainder 1 state.
q2 remainder 2 state.
The q2 is remainder 2 state hence we will make it as final state.
Q.11 What is a finite automation? Give two examples. AU: May-07, Dec.-15, 17
Ans.: A finite automation is a collection of 5-tuples (Q, Σ, δ, q0, F) where
• Q is a finite set of states, which is a non-empty one.
• Σ is input alphabet, indicates input set.
• q0 in Q is the initial state.
• F is a set of final states.
• δ is a transition function.
For example -
1. The FA which accepts only those strings which start with 1 and end with 0.
88
Course Code/Title: CS3401/Theory of Computation
Q.12 Enumerate the differences between DFA and NFA. AU: May-07,14, Dec.-17, 18
Ans. :
Q.13 Construct a DFA over Σ = (a, b) which produces not more than 3a's. AU: May-08
Ans.: In the given language at the most 3a's are allowed and there is no restriction on number of b's. Such a
DFA can be as shown in Fig. 1.13.4.
The above DFA accepts {ε, a, aa, aaa, ab, aba,...}. The states q0, q1, q2 and q3 are final states but state q4 is a
non-final state. Thus the above DFA accepts 3a's and not more than that.
Q.14 Define the languages described by NFA and DFA. AU: Dec.-08
[Refer sections 1.6 and 1.7
• The finite automata is called Deterministic Finite Automata if there is only one path for a specific input
from current state to next state. For example, the DFA can be shown in Fig. 1.6.1.
89
Course Code/Title: CS3401/Theory of Computation
• The concept of Non-deterministic Finite Automata is exactly reverse of Deterministic Finite Automata. The
Finite Automata is called NFA when there exists many paths for a specific input from current state to next
state. The NFA can be shown as in Fig. 1.7.1
Q.15 Construct deterministic finite automata to recognise odd number of 1's and even number of zero's. AU:
May-10
Ans. :
Q.16 Construct a DFA for the language over {0, 1}* such that it contains "000" as a substring. AU : May-11
Ans.:
90
Course Code/Title: CS3401/Theory of Computation
b) For required DFA, we will first create a DFA that contains the substring 110.
Now change non-final state to final state and final state to non-final state.
Q.18 Find the set of strings accepted by the finite automata. AU: Dec.-10
91
Course Code/Title: CS3401/Theory of Computation
• δ is a transition function.
b) Transition diagram: A transition diagram can be defined as collection of -
1) Finite set of states K
2) Finite set of symbols
3) A non empty set S of K which is called start state.
From state S0 for input 'a' there is only one path, going to S2. Similarly from S0 there is only one path for
input b going to S1.
The DFA can be represented by the same 5-tuples described in the definition of FSM.
Q.21 Define the term epsilon transition. AU: May-13
Ans.: In the non deterministic finite state machine, the transition that does not require input symbols for state
transition and is capable of transiting to zero or more states with & is called epsilon transition.
Q.22 What is a non deterministic finite automation? [Refer section 1.7] AU: Dec.-13
• The concept of Non-deterministic Finite Automata is exactly reverse of Deterministic Finite Automata.
The Finite Automata is called NFA when there exists many paths for a specific input from current state to
next state. The NFA can be shown as in Fig. 1.7.1
• Note that the NFA shows from q0 for input a there are two next states q1 and q2. Similarly, from q0 for
input b the next states are q0 and q1.
• Thus it is not fixed or determined that Fig. 1.7.1 Non-deterministic finite automata with a particular input
where to go next. Hence this FA is called non-deterministic finite automata.
92
Course Code/Title: CS3401/Theory of Computation
Q.23 Draw the transition diagram (automata) for an identifier. AU: Dec.-13
Ans. :
93
Course Code/Title: CS3401/Theory of Computation
Q.27 Draw a non-deterministic automata to accept a string containing substring 0101. AU: May-16
Ans.:
Q.28 Obtain the DFA equivalent to the following NFA. AU: Dec.-03
Ans.: The transition table for given NFA can be drawn as follows.
94
Course Code/Title: CS3401/Theory of Computation
Q.29 Obtain the NFA without ε transition to the following NFA with ε transition. AU: Dec.-03
95
Course Code/Title: CS3401/Theory of Computation
ε - closure (q0) = {q0, q1, q2} as we have ε transition from q0 to q0, q1, q2.
ε - closure (q1) = {q1, q2}
ε - closure {q2} = {q2}
Q.32 Find the language accepted by the DFA given below. AU: May-06
96
Course Code/Title: CS3401/Theory of Computation
ε - closure (q0) = {q0, q1, q2} as we have ε transition from q0 to q0, q1, q2.
ε - closure (q1) = {q1, q2}
ε - closure {q2} = {q2}
97
Course Code/Title: CS3401/Theory of Computation
The (a + b)* means any combination with a and b even a null string.
Example 2.1.2 Write r.e. to denote a language L which accepts all the strings which begin or end with either
00 or 11.
Solution: The r.e. can be categorized into two subparts.
R = L 1 + L2
L1 = The strings which begin with 00 or 11.
L2 = The strings which end with 00 or 11.
Let us find out L1 and L2.
L1 = (0011) (any number of O's and 1's)
L1 = (0011) (0+1)*
Similarly,
L2 = (any number of 0's and 1's) (00 + 11)
= (0+1)* (00 + 11)
Hence
R = [(00+11) (0+1)*] + [(0+1)* (00+11)]
Example 2.1.3 Write the r.e. to denote the language L over Σ = {a, b} such that all the strings do not contain
the substring "ab".
Solution: The L = {ε, a, b, bb, aa, ba, baa ...)
The r.e. = (b* a*)
In this regular expression if we substitute a* = ε we get all combination of b's and similarly if we substitute
b* = ε then we will get all combinations of a's. We have strictly maintained a condition for not to have ab as
substring by giving the regular expression as b* a*.
Example 2.1.4 What is regular expression? Write a regular expression for set of strings that consists of
alternating 0's and 1's. AU May-16, Marks 8
Solution: Regular expression - Refer section 2.1.
Regular Expression
Let Σ be an alphabet which is used to denote the input set. The regular expression over Σ can be defined as
follows.
1. ϕ is a regular expression which denotes the empty set.
2. ε is a regular expression and denotes the set {ε} and it is a null string.
3. For each 'a' in Σ 'a' is a regular expression and denotes the set {a}.
4. If r and s are regular expressions denoting the languages L1 and L2 respectively, then
r+s is equivalent to L1 U L2 i.e. union.
99
Course Code/Title: CS3401/Theory of Computation
The Fig. 2.2.1 shows that it is convenient to convert the regular expression to NFA with & moves. Let us see
the theorem based on this conversion.
Equivalence of NFA and Regular Expression
AU: May-04,06,09,10,11,12,14,16,17, Dec.-10,14,15,19, Marks 10
Theorem 1: Let r be a regular expression, then there exists a NFA with ε transitions that accepts L (r).
101
Course Code/Title: CS3401/Theory of Computation
Induction: This theorem can be true for n number of operators. The n is greater than or equal to 1. The
regular expression contains equal to or more than one operators.
In any type of regular expression there are only three cases possible.
1. Union 2. Concatenation 3. Closure
Let us see each,
Case 1: Union case
Let r = r1 + r2 where r1 and r2 be the regular expressions.
There exists two NFA's M1 = (Q1, Σ1, δ1, {f 1})
and M2 = (Q2, Σ2, δ2,{f2})
L (M1) = L(r1) means the language states by regular expression r1 is same which is represented by M1.
Similarly L (M2) = L (r2).
Q1 represents the set of all the states in machine M1.
Q2 represents the set of all the states in machine M2.
We assume that Q1 and Q2 are totally different i.e. Q1 and Q2 are disjoint.
Let q0 be new initial state and f0 be the new final state we will form
M = ((Q1 U Q2 U {q0, f0}), (Σ1 U Σ2), δ, q0 {f0})
The δ is denoted by,
i) δ (q0, ε) (q1, q2}
ii) δ (q, a) = δ1 (q, a) for q in Q1 - {f1} and a in Σ1 U {E}.
iii) δ (q, a) = δ2 (q, a) for q in Q2 -{f2} and a in Σ2 U {E}.
iv) δ (f1, ε) = δ1 (f2, ε) = {f0}
All the moves are now present in machine M which is as shown Fig. 2.3.2.
102
Course Code/Title: CS3401/Theory of Computation
The construction of machine M is shows the transition from q0 to f0 must begin by going to q1 or q2 on ε. If
the path goes to q1, then it follows the path in machine M1 and goes to the state f1 and then to f0 on ε.
Similarly, if the path goes to q2, then it follows the path in machine M2 and goes to state f2 and then to f0 on ε.
Thus the L (M) = L (M1) U L (M2). That means either the path in machine M1 or M2 will be followed.
Case 2: Concatenation case
Let, r = r1 r2 where г1 and r2 are two regular expressions. The M1 and M2 denotes the two machines such that
L (M1) = L (r1) and L (M2) = L (r2).
The construction of machine M will be
M = (Q1 U Q2, Σ1 U Σ2, δ,{q1}, {f2})
The mapping function δ will be given as
i) δ (q, a) = δ1 (q, a) for q in
Q1 - {f1} and α in Σ1 U{ε}
ii) δ (f1, ε) = {q2}.
iii) δ (q, a) = δ2 (q, a) for q in
Q2 and a in Σ1 U {ε}
The machine M is shown in the Fig. 2.3.3.
The initial state is q1 by some input a the next state will be f1. And on receiving ε the transition will be from
f1 to q2 and the final state will be f2. The transition from q2 to f2 will be on receiving some input b.
Thus L (M) = ab
That is a is in L (M1) and b is in L (M2).
Hence we can prove L (M) = L (M1) L (M2).
Case 3: Closure case
Let r = r1 where r1 be a regular expression.
103
Course Code/Title: CS3401/Theory of Computation
The machine M1 shows that from q0 to q1 there is a transition on receiving ε similarly, from q0 to f0 on &
there is a path. The path exists from f1 to q1, a back path. Similarly a transition from f1 to f0 final state, on
receiving ε. The total recursion is possible. Thus one can derive ɛ, a, aa, aaa, .... for the input a.
Thus L (M) = L (M1)* is proved.
Now based on this proof let us solve some examples. These examples illustrate how to convert given regular
expression to NFA with ɛ moves.
Example 2.3.1 Construct an NFA equivalent to the following regular expression
((10) + (0+1)*) 01. AU: May-06, Marks 10
Solution: Consider
r.e. = (r1 + r2) r3
where
r1 = 10
r2 = (0+1)*
r3 = 01
104
Course Code/Title: CS3401/Theory of Computation
r2 = (0+1)*
r3 = 01
Now we will design the final NFA for r. (See Fig. 2.3.8 on next page)
(I+00) (1+0) = 97
Example 2.3.2 Construct a NFA equivalent to (0 + 1)* (00 + 11). AU May-04, Marks 8
Solution: Consider
r.e. = (0 + 1)* (00 + 11)
r1 = (0 + 1)
r2 = (00 + 11)
The NFA for r1 can be drawn as follows.
105
Course Code/Title: CS3401/Theory of Computation
The NFA for the given regular expression can be (See Fig. 2.3.11 on next page).
Example 2.3.3 Construct NFA with epsilon for the RE = (a|b)* ab and convert into DFA and further find the
minimized DFA. AU May-17, Marks 16
Solution: The NFA with ɛ can be constructed as follows
106
Course Code/Title: CS3401/Theory of Computation
107
Course Code/Title: CS3401/Theory of Computation
The δ transition of state B and state D is same but B is a non final state while D is a final state. So we can not
merge them. We can merge state A and state C. Then the minimized DFA will be
108
Course Code/Title: CS3401/Theory of Computation
3. If we reduce the finite automaton to only one state graph by state eliminating method then -
4. After eliminating the sufficient number of states we can obtain r.e. as sum of the expressions derived from
the reduced automata. Let us understand this method of obtaining regular expression with the help of some
examples.
Example 2.3.4 Obtain method r.e. from given FA by state elimination method.
Solution: In above given FA state q3 is a dead state. Hence we will eliminate state q3. Thus the FA now
becomes
This FA resembles the generic finite automata with two state (as given in Fig. 2.3.15)
Hence required r.e. will be
a*bb* As q2 is a final state we get
i.e. r.e. = a*b+
Example 2.3.5 Represent the language given by the following DFA, by obtaining r.e. by state elimination
method.
109
Course Code/Title: CS3401/Theory of Computation
Solution: In above transition graph state q1 is a dead state. We can not reach to final state from this state,
hence we will eliminate state q1. The FA then becomes as –
The state q2 can be written as (a + b) *. Hence the required r.e. is a(a+b)*. This regular expression specifies a
language which starts with letter a and it is followed by any number of a's and b's.
Example 2.3.6 Construct Finite automaton to accept the regular expression
(0+1)*(00 + 11)(0+1)*. AU: May-14, Marks 8, May-11, Marks 6
Solution:
The FA for r.e. =(0+1) (00 + 11) (0+1)* as follows –
110
Course Code/Title: CS3401/Theory of Computation
Example 2.3.7 Construct Finite Automata equivalent to the regular expression (ab +a)*. AU: Dec.-15, Marks
6
Solution: First of all we will construct NFA for given regular expression.
Example 2.3.8 Draw a non-deterministic automata to accept string containing the substring 0101. AU May-
16, Marks 2
111
Course Code/Title: CS3401/Theory of Computation
Example 2.3.9 Construct a finite automata for the regular expression 10 + (0+11)0*1 AU Dec.-19, Marks 6
Solution :
112
Course Code/Title: CS3401/Theory of Computation
Review Question
1. Let r be a regular expression. Then prove that there exists a NFA with & transition that accept L(r).
AU: Dec.-03, Marks 16; Dec.-06, Marks 8; Dec.-10, Marks 10; May-12, Marks 6
Conversion of Finite Automata to Regular Expressions
AU Dec.-03,04,05,09,11,13,16,19, May-05,06,07,08, 12, Marks 16
113
Course Code/Title: CS3401/Theory of Computation
The Arden's theorem is useful for checking the equivalence of two regular expression as well as in
conversion of DFA to r.e. Let us see its use in conversion of DFA to r.e.
Following algorithm is used to build the r.e. from given DFA.
1. Let q1 be the initial state.
2. There are q2, q3, q4, ...qn number of states. The final state may be some qj where j ≤ n.
3. Let αji represents the transition from qj to qi
4. Calculate qj such that
qi = αji qj
If qi is a start state
to qi
qi = αji qj + ε
5. Similarly compute the final state which ultimately gives the regular expression r.
Let us solve few examples based on this algorithm.
Example 2.4.1 Construct r.e. from given DFA.
εR=R
Substituting value of q1 in q2 we get
q2 = q1 b + q2 b
q2 = a * b + q2 b
We can compare this equation with R = Q + R P assuming R = q2, Q = a*b, P = b which gets reduced to R =
QP*.
q2 = a*b.b*
As R R* = R+
q2 = a*.b+
From the given DFA, if we want to find out the regular expression, we normally calculate the equation for
final state. Since in the given DFA q2 is a final state and q2 = a*b+. We can conclude as the DFA represents a
as regular expression.
Example 2.4.2 Represent the language accepted by following DFA.
Solution: Since there is only one state in the finite automata let us solve for q0 only.
q0 = q00 + q01 + ε
q0 = q0 (0+1) + ε
= ε . (0+1)* R = Q + R P
q0 = (0+1)*
Since q0 is a final state, q0 represents the final r.e. as
r = (0 + 1)*
L (r) = {ε, 0,00,1,11,10,....}
L (r) = {any combination of 0 and 1}
Example 2.4.3 Construct r.e. for the given DFA. AU May-07, Marks 6
115
Course Code/Title: CS3401/Theory of Computation
q2 = q11 + q21
q3 = q20 + q3 (0 + 1)
Since final states are q1 and q2, we are interested in solving q1 and q2 only.
Let us see q1 first
q1 = ε + q10
Which is R = Q + R P equivalent so we can write
q1 = ε .(0)*
q1 = 0*
ε. R = R
Substituting this value into q2, we will get
q2 = 0*1 + q21
q2 = 0*1 (1)* R = Q + R P ⇒ Q P*
The regular expression is given by
r = q1 + q2 =0* + 0* 1. 1*
r = 0* + 0* 1+ 1.1* = 1+
Example 2.4.4 Convert the following NFA into a regular expression. AU: Dec.-13, Marks 8
116
Course Code/Title: CS3401/Theory of Computation
A = (0+1)* … (5)
Equation (2) becomes
B = A1 = (0+1)* 1 … (6)
Now by using equation (6) we get equation (3) as
C = B (0+1)
C = (0+1)* 1 (0+1) … (7)
Using equation (7), we get equation (4) as
D = C (0+1)
= (0+1)* 1 (0+1) (0+1) … (8)
As state C and D are final states equation (7) and equation (8) represent final regular expression.
r.e. [(0+1)* 1 (0 + 1)] + [(0+1)* 1 (0 + 1) (0 + 1)]
Example 2.4.5 Construct the regular expression corresponding to the state diagram given in the following
Fig. 2.4.5. AU: May-05, Marks 10; May-08, Marks 16, Dec.-19, Marks 2
Solution: We will obtain the regular expression for the given state diagram using Arden's theorem. Let us
write equations for each state.
q1 = q10 + q30 + ε being a start state, add ɛ.
q2 = q11 + q21 + q31
q3 = q20
Consider q2 first.
q2 = q11 + q21 + q31
q2 = q11 + q21 + q201 q3 = q20
q2 = q2 (1+01) + q11
R = Q + RP gives R = QP*
R = q2, Q = q11, P = (1 + 01)
q2 = q11 (1+01)*
Then put value of q2 in q3 and we get,
q3 = q11 (1+01)* 0
117
Course Code/Title: CS3401/Theory of Computation
118
Course Code/Title: CS3401/Theory of Computation
q3 = a* ba* b
As q3 represents the final state, the equation for state q3 represents the regular expression. Hence regular
expression = a* ba* b.
Review Questions
1. State and explain the conversion of DFA into regular expression using Arden's theorem. Illustrate with an
example. AU: Dec.-11, Marks 16
2. Discuss the basic approach to convert from NFA to regular expression. Illustrate with an example. AU
Dec.-16, Marks 16
Equivalence of Two Regular Expressions
AU May-10,11, Marks 9
The two regular expressions P and Q are equivalent (denoted as P = Q) if and only if P represents the same
set of strings as Q does. For showing this equivalence of regular expressions we need to show some
identities of regular expressions.
Let P, Q and R are regular expressions then the identity rules are as given below
1. ε R = R ε = R
2. ε* = ε ε is null string
3. (ϕ)* = ε ϕ is empty string.
4. ϕ R = R ϕ = ϕ
5. ϕ + R = R
6. R + R = R
7. RR* = R*R = R+
8. (R*)* = R*
9. ε + RR* = R*
10. (P+Q) R = PR + QR
11. (P+Q)* = (P* Q*) = (P* + Q*)*
12. R* (ε + R) = (ε + R) R* = R*
13. (R + ε)* = R*
14. ε + R* = R*
15. (PQ)* P = P (QP)*
16. R* R + R = R* R
Let us see one important theorem named Arden's Theorem which helps in checking the equivalence of two
regular expressions.
119
Course Code/Title: CS3401/Theory of Computation
Arden's Theorem: Let, P and Q be the two regular expressions over the input set 2. The regular expression R
is given as
R = Q + RP
Which has a unique solution as R = QP*.
Proof : Let, P and Q are two regular expressions over the input string Σ.
If P does not contain & then there exists R such that
R = Q + RP …. (2.5.1)
We will replace R by QP* in equation (2.5.1)
Consider R.H.S. of equation (2.5.1)
= Q + QP* P
= Q(ε + P* P)
= QP*
ε + R * R = R*
Thus R = QP*
is proved. To prove that R = QP* is a unique solution, we will now replace L.H.S. of equation (2.5.1) by Q +
RP. Then it becomes
Q + RP
But again R can be replaced by Q + RP.
Q + RP = Q + (Q + RP) P
= Q + QP + RP2
Again replace R by Q + RP.
= Q + QP + (Q + RP) P2
= Q + QP + QP2 + RP3
Thus if we go on replacing R by Q + RP then we get, Q + RP = Q + QP + QP2 + ... + QPi + Rpi+1
= Q(ε + P+ P2 + ….. Pi) + Rpi+1
From equation (2.5.1),
R = Q (ε + P+ P2 + ….. + Pi) + Rpi+1 …. (2.5.2)
Where i ≥ 0
Consider equation (2.5.2),
R = Q(ε + P+ P2 + ... + Pi + RPi+1 /p*
R = QP* + RPi+1
Let w be a string of length i.
120
Course Code/Title: CS3401/Theory of Computation
In RPi+1 has no string of less than i + 1 length. Hence w is not in set RPi+1. Hence R and QP* represent the
same set. Hence it is proved that
R = Q + RP has a unique solution.
R = QP*
Example 2.5.1 Prove (1+00*1) + (1+00*1) (0+10*1) * (0+10*1) = 0*1(0+10*1) *AU: May-11, Marks 8
Solution: Let us solve L.H.S. first,
(1+00*1) + (1+00*1) (0+10*1) * (0+10*1)
We will take (1+00*1) as a common factor
1+00*1
(ε+(0+10*1)*(0+10*1))
(ε+ R*R) where R = (0+10*1)
As we know, (ε + R*R) = (ε + RR*) = R*
(1+00*1) ((0+10*1)*) out of this consider
(1+00*1) (0+10*1)*
Taking 1 as a common factor
(ε+00*) 1 (0+10*1) *
Applying ε + 00* = 0*
0*1 (0+10*1) *
= R.H.S.
Hence the two regular expressions are equivalent.
Example 2.5.2 Show that (0*1*)* = (0+1)*
Solution: Consider L.H.S.
= (0*1*)*
{ε, 0,00,1,11,111, 01, 10,...}
= {any combination of O's, any combination of 1's, any combination of 0 and 1, ε}
Similarly,
R.H.S.
= (0+1)*
= {ε, 0, 00, 1, 11, 111, 01, 10,....}
= {ε, any combination of 0's, any combination of 1's, any combination of 0 and 1}
Hence, L.H.S. = R.H.S. is proved.
121
Course Code/Title: CS3401/Theory of Computation
If qm is in F i.e. q1, q2, q3, … qm is in L(M) then a1, a2,...aj ak+1 ak+2 ......am is also in L(M)
. Since there is path from q0 to qm that goes through qj but not around the loop labelled aj+1 .... ak. Thus
δ (q0, a1, aj ak+1 ...am) = δ (δ (q0, q1, ... qj), ak+1 .... am)
= δ (qj, qk+1 ...qm)
= δ (qk, qk+1 ...qm)
= qm
That is what we have proved that given any long string can be accepted by FA, we should be able to find a
substring near the beginning of the string that may be pumped i.e. repeated as many times as we like and
resulting string may be accepted by FA.
123
Course Code/Title: CS3401/Theory of Computation
126
Course Code/Title: CS3401/Theory of Computation
x = aaa
y=b
z=b
W = (aaa) (bbb) (b)
W = a3 b4 € {L = am bn | m > n}
iii) y has both a's and b's. Then assume W = a3 b2.
If we map W = xyiz with i = 2 then W in equation (3) becomes
W = aa ab b
x = aa
y = ab
z=b
W = (aa) (abab) (b)
= a3 bab2 € ambn
From above three cases it is clear that our assumption of L being regular is wrong. This proves that given L =
ambn is not regular.
The simple way to know whether given language is regular or not is that try to draw finite automata for it, if
you can easily draw the FA for the given L then that language is surely the regular otherwise not.
Example 2.6.7 Using pumping lemma for regular sets, prove that the language L = {0m1n 0m+n | m ≥ 1 and n ≥
1} is not regular. AU: Dec.-13, Marks 10
Solution: Let us assume that L is a regular language. The input string w ϵ L such that
|w| = | xyz | = 0m1n 0m+n
By pumping lemma we can write
w = xyz such that |xy| ≤ n and |y| ≠ 0.
There are three cases: i) y has only 0's
ii) y has only 1's
iii) y has 0's and 1's.
Case i) y has only 0's
Consider w = 00010000
If we map w = xyz then
By pumping lemma,
w = xyiz
W = 0 00 10000
128
Course Code/Title: CS3401/Theory of Computation
0 = x, 00 = y, 10000 = z
if i = 2 then
w = xyyz i.e.
= 0(00) (00) (10000)
W = 05 11 04 € 0m 1n 0m+n
Case ii) y has only 1's
Consider w = 00110000
If we map w = xyz then
By pumping lemma,
w = 00 11 0000
x = 00, y = 11, z = 0000
w = xyiz
if i = 2 then
w = xyyz i.e.
= 00(11) (11) (0000)
= 02 14 04 € 0m 1n 0m+n
Case iii) y has 0's and 1's
Consider w = 001000
w = 0 01 000
x = 0, y = 01, z = 000
If we map w = xyz then
By pumping lemma,
w = xyiz
If i = 2 then
w = xyyz i.e.
= 0 (01) (01) (000)
= 021 011103 € 0m1n 0m+n
From above three cases, it is clear that our assumption of L being regular is wrong. This proves that given L
= 0m1n 0m+n is not regular.
Example 2.6.8 Prove that following languages are not regular:
i) { w ϵ {a,b}* |w = wR} ii) Set of strings of 0's and 1's beginning with 1, whose value treated as a binary
number is prime. AU: Dec.-19, Marks 13
129
Course Code/Title: CS3401/Theory of Computation
If certain languages are regular and language L is formed from them by certain operations (such as union or
concatenation) then L is also regular. These properties are called closure properties of regular languages.
Such languages represent the class of regular languages which is closed under the certain specific operations.
The closure properties express the idea that when one or many languages are regular then certain related
languages are also regular. The closure properties of regular languages are as given below.
1. The union of two regular languages is regular.
2. The intersection of two regular languages is regular.
3. The complement of a regular languages is regular.
4. The difference of two regular languages is regular.
5. The reversal of a regular languages is regular.
6. The closure operation on a regular language is regular.
7. The concatenation of regular language is regular.
8. A homomorphism of regular languages is regular.
9. The inverse homomorphism of regular language is regular.
Theorem 1: If L1 and L2 are two languages then L1 U L2 is regular.
Proof: If L1 and L2 are regular then they have regular expression L1 = L (R1) and L2 = L (R2) Then L1 U L2 =
L (R1+ R2) thus we get L1 U L2 as regular language. (any language given by some regular expression is
regular).
Theorem 2: The complement of regular language is regular. AU May-14, Marks 6, Dec.-08, Marks 2
Proof : Consider L1 be regular language which is accepted by a DFA M = (Q, Σ, δ, q0, F) The complement of
regular language is L1 which is accepted by M' = (Q, Σ, δ, q0, Q - F) That means M is a DFA with final states
ϵ F and M' is a DFA in which all the non-final states of M become final. In other words, we can say that the
strings that are accepted by M are rejected by M' similarly, the strings rejected by M are accepted by M'.
Thus as L1 is accepted by DFA M', it is regular.
Theorem 3: If L1 and L2 are two regular languages then L1∩ L2 is regular. AU: Dec.-07, 08, Marks 4
Proof: Consider that languages L1 is regular. That means there exists some DFA M1 that accepts L1. We can
write M = (Q1, Σ, δ1, q1, F1) Similarly being L2 regular there is another DFA M2 (Q2, Σ, δ2, q2, F2)
Let, L be the language obtained from L1 ∩ L2. We can the simulate M = (Q, Σ, δ, q, F)
where
Q = Q1 ∩ Q 2
δ = δ1 ∩ δ2 a mapping function derived from both the DFAS.
q ϵ Q which is initial state of machine M.
F = F1 ∩ F2, the set of final states, which is common for M1 and M2 both.
131
Course Code/Title: CS3401/Theory of Computation
It is clear that there exists some DFA which accepts L1 ∩ L2 i.e. L. Hence L is a regular language. This
proves that if L1 and L2 are two regular languages then L1 is regular. In other words the regular language is
closed under intersection.
Theorem 4: If L1 and L2 are two regular languages then L1 - L2 is regular.
Proof: The L1 - L2 can also be denoted as L1 U Ḹ2
Consider L1 be regular language which is accepted by DFA M =(Q,Σ,δ,q0,F) The complement of regular
language L1 is Ḹ1 which is accepted by M'=(Q,Σ,δ,q0,Q-F) That means M is a DFA with final state's set F and
M' is a DFA in which all the non final states of M become final states and all the final states of M become
non-final states. Thus L1 and Ḹ2 are two regular languages. That also means: these languages are accepted by
regular expressions. If L1 = L(R1) and Ḹ2 = L(R`2). Then L1 U Ḹ2 = L (R1 + R`2). This ultimately shows that
L1 U Ḹ2 is regular. In other words L1 – L2 is regular. Thus regular languages are closed under difference.
Theorem 5: The reversal of a regular languages is regular. AU: Dec.-07, 08, Marks 4
Proof: Reversal of a string means obtaining a string which is written from backward that is w is denoted as
reversal of string w. That means L (wR) = (L(w))R. This proof can be done with basis of induction.
Basis: If w= ε or ϕ then wR is also ε or ϕ
i.e. (ε)R = ε and (ϕ)R = ϕ
Hence L(wR) is also regular.
Induction :
Case 1: If w = w1+ w2 then
w = (w1)R + (w)R
As the regular language is closed under union. Then w is also regular.
Case 2: If w = w1 + w2
Consider w1 = (ab, bb)
and w2 = (bbba, aa)
The wR1 = (ba, aa)
wR2 = (aaab, bb) then
w – wR1 wR2
(ba, aa, aaab, bb) is also regular. Thus given language is regular one.
Theorem 6: The closure operation on a regular language is regular.
Proof: If language L1 is regular then it can be expressed as L1 = L(R1) Thus for a closure operation a
language can be expressed as a language of regular expressions. Hence L1 is said to be a regular language.
Theorem 7: If L1 and L2 are two languages then L1 L2 is regular. In other words regular languages are closed
under concatenation.
Proof: If L1 and L2 are regular then they can be expressed as L1 = L(R1) and L2 = L(R2) Then L1 L2 = L(R1 R2)
thus we get a regular language. Hence it is proved that regular languages are closed under concatenation.
132
Course Code/Title: CS3401/Theory of Computation
133
Course Code/Title: CS3401/Theory of Computation
For example - Let L = (010)* be a regular language. And L = {010, 010010, ...} Let h(0) = a and h(1) = bb be
homomorphic function. Then
h(L) = (abba)*
This L can be represented by following DFA.
Thus there exists a FA which accepts h-1(1). This shows that, inverse homomorphism of regular language is
regular.
Review Questions
1. Show that regular languages are closed under intersection and reversal. AU: Dec.-07, 08, Marks 8
2. Prove that regular sets are closed under substitution. AU: Dec.-10, Marks 6
3. State and prove using an example, the properties of regular language. AU: May-10, Marks 7
4. Prove any two closure properties of regular languages. AU: Dec.-12, Marks 6
5. Discuss in detail about the closure properties of regular languages. AU: May-13, Dec.-13, Marks 8; Dec.-
17, Marks 13
Two Marks Questions with Answers
Q.1 Is it true that the language accepted by any NFA is different from the regular language? Justify your
answer. AU: May-04
Ans. No, any language accepted by DFA, NFA, ε-NFA is called a regular language. For any finite automaton
we can construct an equivalent regular expression and the language represented by regular expressions is a
regular language.
Q.2 Describe the following by regular expression
a) L1 = The set of all strings of 0's and 1's ending in 00.
b) L2 The set of all strings of 0's and 1's beginning with 0 and ending with 1.
AU: May-04, Dec.-12
Ans. a) The set of all strings of 0's and 1's is given by (0 + 1)*.
134
Course Code/Title: CS3401/Theory of Computation
We assume that Q1 and Q2 are totally disjoint. We will now assume q0 as a new initial state and f0 be the new
final state. Then we will form new machine M as
M = ((Q1 U Q2 U {q0, f0}), (Σ1 U Σ2), δ, q0{f0})
This shows that L(M) = L(M1) U L(M2)
i.e. L(R) = L(r) = L(r1) U L(r2)
The machine designed for L(r1) U L(r2) shows that language Ri is regular.
Q.7 Verify whether L = {a2n |n ≥1} is regular. AU: May-07
Ans. Refer similar example 2.6.2.
Q.8 State pumping lemma and its advantages. AU: Nov.-14, Dec.-07, 08, 13, 17
136
Course Code/Title: CS3401/Theory of Computation
Ans.: Pumping lemma: Let L be a regular set. Then there exists a constant n such that if z is any word in L
such that |z| ≥ n then we can write z = uvw such that |uv| <n and |v| ≥ 1 for all i ≥ 0 and uv1w is in L. The n
indicates number of states in FA and should not be greater than the number of states.
The advantage of pumping lemma is that this theorem is used to check whether given language is regular or
not.
Q.9 Show that the complement of a regular language is also regular. AU: Dec.-07
Ans. If L is a regular language, then its complement denoted by L' is also regular. To prove this, consider that
language L is accepted by finite automaton M. Now all the words accepted by language L will reach to final
state of machine M. If we change all the states of M to non-final states and all the non-final states of M to
final state we get a new FA which is denoted by M'. Now this M' will accept all the words not belonging to L
and reject all the words belonging to L. That means M' will accept the language L'. Thus there exists FA
which accepts L'. Hence L' which is complement of L is also regular.
Q.10 Construct a DFA for the regular expression aa*bb*. AU : May-08
Ans. The DFA for aa*bb* is -
Note that is represented by q1 to f1. We can reach to a final state f0 from q0 with input ε. Hence ϕ* = ε is
shown by above figure.
Q.13 Construct an NDFA for all strings over alphabet = {a, b} that contains a substring 'ab'. AU: May-09
137
Course Code/Title: CS3401/Theory of Computation
Q.14 Write regular expressions for the following language over the alphabet Σ = {0,1}. "The set of all strings
not containing 101 as a substring". Provide justification that your regular expression is correct. AU : May-09
Ans. We will first construct DFA for the language containing 101 as substring.
Now, just change the non-final states to final states and make final state as non-final state. The DFA then
becomes –
138
Course Code/Title: CS3401/Theory of Computation
r.e. = 0*(1*(00*)*)*
Q.15 Prove or disprove the following for regular expression (a+b) + (cd) for Σ = {a,b,c,d}. AU: May-09
Ans.: To prove whether or not given language is regular or not following theorem is used -
"For every regular language there exists a finite automata".
If we could be able to build the FA for given expression then that expression is side to be regular. Hence
Now to accept the language which does not contain 00, we will change the non final state to final state and
final state to non final state.
The regular expression will be (01+ 1) + (10+ 1) + 0.
139
Course Code/Title: CS3401/Theory of Computation
Q.18 Is the set of strings over the alphabet {0} of the form 0n where n is not a prime is regular ? Prove or
disprove. AU: Dec.-11
Ans. Let us assume that L is a regular language n is not a prime number.
Let zϵL, L = 0n
|z| = uvw, here i = 1.
Now consider L = uvi w where i = 2
= u v. vw
That means adding 1 to n, we get
n < |uvvw|
n < n+1
But (n + 1) can be a prime number. Hence our assumption becomes contradictory to it. This shows that L is
not regular language.
Q.19 Construct regular expression for the language over the set ẑ = {a,b} in which total number of a's are
divisible by 3.
Ans. r.e. = (b* ab* ab* a)*
Q.20 Find out the language generated by the regular expression (0+1)*.
Ans.: L = {ε, 0, 1, 00, 11, 01, 10,111, ....}
L = {Any number of 0's and 1's including nullstring}
Q.21 What is the closure property of regular set s?
Ans.: If certain languages are regular and language L is formed by them by certain operations such as (union,
concatenation and so on) and if we find L as regular, then this property is called closure property of regular
sets.
Q.22 Mention the applications of pumping lemma.
Ans.: 1. Pumping lemma is used for deciding whether or not the given language is regular.
2. It is also useful for determing whether L is accepted by a regular expression or not.
Example 1:
L = {0n1n} is not regular.
L = {0P|p-is a prime number}
Example 2:
All these languages are non-regular languages and this can be proved by pumping lemma.
Q.23 Is it true that the language accepted by NFA is different from regular language? Justify your answer.
Ans. The language accepted by NFA is always regular. Because any NFA is equivalent to DFA. And if we
represent certain language by NFA then we can definitely represent it using DFA. The regular language is a
140
Course Code/Title: CS3401/Theory of Computation
kind of language which can be represented by DFA. Hence the language accepted by NFA is not different
from regular language.
Q.24 Give regular expression to describe an identifier and positive integer.
Ans.: id = (a - zA - Z) (a - zA - Z 0-9)*
P = (0-9)+
Q.25 Find the string of minimum length in {0,1}* NOT in the language corresponding to the given regular
expressions.
a) 0* (100*) * 1* b) (1+01) (0 +01) *
Ans. a) {110} € 0* (100*)* 1*.
b) ϕ.
Q.26 Design FA that accepts {0,1}*.
Ans.:
Q.27 Write a regular expression which generates strings with at least one 0 and one 1.
Ans. r.e. = = (0+ 1*) (1+ 0*) (0+1)*
Q.28 Give the operations that satisfy closure properties of regular languages.
Ans. The operations are union, intersection, complement, difference, reversal, concatenation, reversal,
homomorphism, inverse homomorphism.
Q.29 Give a regular expression for the set of all strings having odd number of 1's.
Ans.: r.e. = 1(0+11)*
Q.30 State the precedence of regular expression operators.
Ans.:
1. The star operator is having highest precedence.
2. The dot operator (concatenation) has the next highest position,
3. Final precedence is given to + i.e. union operation. The union is an associative operator.
Q.31 What does regular expression (a/b) * denote ?
Ans. The strings generated by the given expression are {ε, a, b, aa, bb, ab, ba, ....} This denotes the language
L = {Any number of a's and/or b's including null string}.
Q.32 Give the regular expression for set of all strings ending in 00. AU: Dec.-10
Ans. r.e. = = (0+1)* 00
141
Course Code/Title: CS3401/Theory of Computation
142
Course Code/Title: CS3401/Theory of Computation
143
Course Code/Title: CS3401/Theory of Computation
Q.45 Show whether a language L = {0n 12n|n> 0} is regular or not using pumping lemma. AU: May-17
Ans.: We will map W = xyz for given string W ϵ L = {0n 12n}
Suppose W = 001111
If we map W = xyz then -
W = 0 011 11
x = 0, y = 011 z = 11 ...(1)
By pumping lemma if W = xyi z ϵ L then the language is regular.
For equation (1) we have W = xyiz. We assume i = 2 then string becomes
W = xyyz
= 001101111
W = 0212 0114 € L
This shows that given language is not regular.
Q.46 Give language of regular expression a ? (b|c)*. AU: May-17
Ans. The ? denotes preceding character occurs for one or zero times. The * means the string occurs for any
number of times. The language contains a string which may begin with single a or no a and followed by any
number of b's and c's.
Q.47 Write regular expression for the set of all strings of 0's and 1's not containing 101 as substring. AU:
May-18
Ans. :
r.e. = 0*(1*000*)*1*0*
In above expression all 1's are followed by 00. This pattern is repeated for any number of times.
144
Course Code/Title: CS3401/Theory of Computation
Types of Grammar - Chomsky's hierarchy of languages - Context - Free Grammar (CFG) and Languages -
Derivations and Parse trees - Ambiguity in grammars and languages - Push Down Automata (PDA):
Definition - Moves - Instantaneous descriptions - Languages of pushdown automata- Equivalence of
pushdown automata and CFG - CFG to PDA - PDA to CFG - Deterministic Pushdown Automata.
This is a hierarchy therefore every language of type 3 is also of type 2, 1 and 0. Similarly every language of
type 2 is also of type 1 and 0 etc.
145
Course Code/Title: CS3401/Theory of Computation
The context free languages are the languages which can be represented by Context Free Grammar (CFG).
The production rule is of the form
Α→α
where A is any single non-terminal and a is any combination of terminals and non-terminals.
A NFA or DFA cannot recognize strings of this language because these automata are not having "stack" to
memorize. Instead of it the Push Down Automata can be used to represent these languages.
Type 1 - Context sensitive languages
The context sensitive grammars are used to represent context sensitive languages. The context sensitive
grammar is follows the following rules -
1. The context sensitive grammar may have more than one symbol on the left hand side of their production
rules.
2. The number of symbols on the left hand side must not exceed the number of symbols on the right hand
side.
3. The rule of the form A → ε is not allowed unless A is a start symbol. It does not occur on the right hand
side of any rule.
The automaton which recognizes context sensitive languages is called linear bounded automaton. While
deriving using context sensitive grammar the sentential form must always increase in length every time a
production rule is applied. Thus the size of a sentential form is bounded by a length of the sentence we are
deriving.
Type 0 Unrestricted languages
There is no restriction on the grammar rules of these type of languages. These languages can be effectively
modeled by Turing machines.
Context-Free Grammar (CFG) and Languages
Definition :
The context free grammar can be formally defined as a set denoted by G = (V, T, P, S) where V and T are set
of non-terminals and terminals respectively. P is set of production rules, where each production rule is in the
form of
non-terminal → non-terminals
or non-terminal → terminals
S is a start symbol.
For example,
P = {S→ S + S
S→ S * S
S→ (S)
S→ 4}
146
Course Code/Title: CS3401/Theory of Computation
If the language is 4 + 4 * 4 then we can use the production rules given by P. The start symbol is S. The
number of non-terminals in the rules P is one and the only non terminal i.e. S. The terminals are +, *, (,) and
4.
We are using following conventions.
1. The capital letters are used to denote the non-terminals.
2. The lower case letters are used to denote the terminals.
Example: The formation of production rules for checking syntax of any English statement is
SENTENCE → NOUN VERB
NOUN→ Rama / Seeta / Gopal
VERB→ goes / writes / sings
Thus if want to derive a string "Rama sings" then we can follow the above rules.
Derivations
AU May-10,11,12,16, Dec.-11,14,16,17,18, Marks 16
The production rules are used to derive certain strings. We will now formally define the language generated
by grammar G = (V, T, P, S). The generation of language using specific rules is called derivation.
Definition :
Let G = (V, T, P, S) be the context free grammar. If A →ẞ is a production of P and a and y are any strings
from non-terminals or terminals i.e. in (VUT)* then a Ay → aẞy.
Suppose a1, a2, a3,... am are strings in (VUT)*, m ≥ 1.
a1 ⇒ a2
a2 ⇒ a3
.
.
am-1 ⇒ am
Then we can say that a1 ⇒ am
The language generated by G is denoted by L(G). The language L is called context free language CFL if
L(G) is for CFG.
For example
G = ({S, B} {a, b}, {S → a B b, S)
B → bbb}
is a grammar. Then we can derive a string abbbb as -
i) We will first start from start symbol S
147
Course Code/Title: CS3401/Theory of Computation
S⇒aBb
ii) Then we will replace B by bbb
S ⇒ a B b ⇒ a bbbb
Thus we can obtain the desired language L by certain rules. The language L can be described as a language
which starts with letter a and having 4 b's following. Let us solve some examples for derivation of CFG.
Solved Examples
Example 3.3.1 Try to recognize the language L for given CFG.
G = [{S}, {a,b}, P, {S}]
where P = {S → aSb, S → ab}
Solution: Since S → a S b | a b is a rule. | indicates the 'or' operator.
S →aSb
If this rule can be recursively applied then,
S
aSb
↓
aaSbb
↓
aaaSbbb
and if finally we can put S→ ab then it becomes aaaa bbbb. Thus we can have any number of a's first then
equal number of b's following it. Hence we can guess the language as {L=an bn where n≥1}. The only way to
recognize the language is to try out various strings from the given production rules. Simply by observing the
derived strings, one can find out the language getting generated from given CFG.
Example 3.3.2 Construct the CFG for the regular expression (0+1)*
Solution: The CFG can be given by,
P = {S → 0S | 1S
S → ε}
The rules are in combination of 0's and 1's with the start symbol. Since (0+1)* indicates (ε, 0, 1, 01, 10, 00,
11, ...) in this set ε is a string. So in the rules we can set the rule S → ε.
Example 3.3.3 Construct a grammar generating
L = w c wT where we w ϵ {a,b}*
Solution: The strings which can be generated for given L is {aacaa, bcb, abcba, bacab ...}
The grammar could be
S→aSa
148
Course Code/Title: CS3401/Theory of Computation
S→ b S b
S→c
Since the language L = w c wT where w ϵ (a + b)*
Hence S → a S a or S → b S b. The string abcba can be generated from given production rules as
S
aSa
abSb
abcba
Thus any of this kind of string could be derived from the given production rules.
Example 3.3.4 Construct CFG for the language L which has all the strings which are all palindrome over Σ =
{a,b} AU: Dec.-17, Marks 7
Solution: As we know the strings are palindrome if they posses same alphabets from forward as well as from
backward.
For example, the string "madam" is a palindrome because
Since the language L is over = (a, b). We want the production rules to be build a's and b's. As ε can be the
palindrome, a can be palindrome even b can be palindrome. So we can write the production rules as
G = ({S}, {a, b}, P, S)
P can be
S→aSa
S→bSb
S→a
S→b
S→ε
The string abaaba can be derived as
149
Course Code/Title: CS3401/Theory of Computation
which is a palindrome.
Example 3.3.5 Construct CFG for the language containing all the strings of different first and last symbols
over Σ = {0,1}.
Solution: Since the problem statement says as if the string starts with 0 it should end with 1 or if the string
starts with 1 it should end with 0.
S→0A1|A0
A→0A|1A|ε
Thus clearly, in the above CFG different start and end symbols are maintained. The non terminal A → 0A |
1A | ϵ indicates (0+1)*. Thus the given CFG is equivalent to the regular expression [0 (0+1)*1+1(0+1)*0]
Example 3.3.6 Construct the CFG for the language L= an b2n where n ≥ 1.
Solution: Here the number of b's are doubled than a's so we can write
S → aSbb | abb
It is as simple as this!
Example 3.3.7 Build a CFG for the language L = {0i 1j 2k | j>i+k}.
Solution: As the language L = 0i 1j 2k such that j > i + k.
Hence we can rewrite L as
150
Course Code/Title: CS3401/Theory of Computation
A→0A1|ε
B→1B|1
C→1C2|ε
Hence the CFG can be
S→ABC
A→0A1|ɛ
B→1B|1
C→1C2|ε
Example 3.3.8 Build a CFG for the language L = {0i 1j 2k | i = j}
Solution: As i = j and there is no condition on k. We can say that k is an arbitrary value. Then we rewrite L as
L = 0i 1j 2k
0i 1j = A
2k = B
S→AB
A→0A1|ε
B→2B|ε
Example 3.3.9 Obtain CFG for the language L = {0i 1j 2k | j≤ k}.
Solution: Here j≤ k. That means the language L has two variations.
L1 = 0i 1j 2j where k = j
L2 = 0i 1j 2k where k > j
Hence the required CFG can be
S→AB
A→0A|ε
B→1B2|C
C→2C|ε
Example 3.3.10 Consider the alphabet A = (a, b) and the language L= {bb, bab, baab, baaab, ...) over A. AU
May-10, Marks 16
i) Is A* finite or infinite? Give a brief reason for your answer. [2]
ii) Write down a regular expression that represents the above language L. [4]
iii) Write down a regular grammar which describes the above language L. [4]
iv) Draw the DFA corresponding to the above language L. [6]
151
Course Code/Title: CS3401/Theory of Computation
Solution: i) The A* is an infinite language, because there are any number of a's in the string and the string is
starting and ending with b. This leads to an infinite strings of the language.
ii) The regular expression will be = ba*b.
iii) The regular grammar G = (V, T, P, S) where, V is set of non terminals = {S, X, T}. T is the set of
terminals which is (a, b). P is a set of production rules as shown below -
{
S → bX
X → aX|aT|b
T→b
}
S is a start symbol.
This is a right linear grammar because the non-terminal appears at the right end of the rule.
iv) The DFA will be
Example 3.3.11 Construct a CFG for the set {ai bj ck | i ≠ j or j ≠ k} As there are two cases i ≠ j or j ≠ k. AU
May-11, Marks 6
Solution: Hence the production rules will be
S → R1 R2
R1 → aAbbBC | aaAbBC
R2 → AbBccC | AbbBcC
A → aA | ε
B → bB | ε
C → cC | ε
Example 3.3.12 If S → aSb|aAb, A → bAa, A → ba is the context free grammar. Determine the context free
language. AU Dec.-11, Marks 6
Solution: The strings generated by this grammar are (a ba b, aa babb, aaa bbaa bbb, ...) This denotes the
language L = {an bm am bn |n, m≥1}
Example 3.3.13 Find the context free langauge for the following grammars
1) S → aSbs | bSaS | ε
2) S → aSb | ab AU May-12, Marks 10
152
Course Code/Title: CS3401/Theory of Computation
From these derivations, we can observe that the derived strings contain equal number of a's and b's. Thus this
CFG denotes the language L containing equal number of a's and b's.
2) Refer example 3.3.1.
Example 3.3.14 Construct a context free grammar for {0m 1n | 1≤m≤n} AU: Dec.-14, Marks 4
Solution: G = (V, T, P, S) where
V = {S, A}
T = {0, 1}
P is a production rules set. It is given by
S → 0S1 | 0A |01
A → 1A | 1
Example 3.3.15 Construct a CFG for the regular expression (011 + 1) (01). AU May-16, Marks 6
Solution: The production rules are
S → AB
A→X|Y
B → 01
X → 011
Y→1
Example 3.3.16 Construct a context free grammar for the language.
L = {an | n is odd} AU: Dec.-16, Marks 6
Solution: The context free grammar G = (V, T, P, S)
Where
V = (S, T)
T = (a)
The S is a start symbol.
153
Course Code/Title: CS3401/Theory of Computation
Parse Trees
AU Dec.-03,04,05,10,12,14,15, May-04,05,09,13,16, Marks 16
154
Course Code/Title: CS3401/Theory of Computation
• Derivation trees is a graphical representation for the derivation of the given production rules for a given
CFG.
• It is the simple way to show how the derivation can be done to obtain some string from given set of
production rules.
• The derivation tree is also called parse tree.
155
Course Code/Title: CS3401/Theory of Computation
OR
Theorem Let G = (V, T, P, S) be a context free grammar then prove that if the recursive inference procedure
tells us that terminal string w is in the language of variable A, then there is a parse tree with root A and yield
w. AU: Dec.-15, Marks 10
Proof: For a non-terminal S there exists S ⇒ w if and only if there is a derivation tree starting from root S
and yielding w. To prove this we will use method of induction.
Induction hypothesis: We assume that for K-1 nodes the derivation tree can be drawn. We then can prove that
for K vertices also we can have a derivation tree. That means the input string a can be derived as S → S 1, S2,
S3, ….Sk. There are two cases: either Si may be a leaf variable or Si may be an interior node yielding a. The
S derives a by fewer number of K steps then a ϵ S1 S2 S3 …Sk.
If ai = Si then Si is leaf node (terminal) and if Si ⇒ ai then Si is an interior node. The tree for S1 S2 …Sk will
be shown in Fig. 3.3.4
We can also represent it as shown in Fig. 3.4.5.
156
Course Code/Title: CS3401/Theory of Computation
157
Course Code/Title: CS3401/Theory of Computation
B → b | bS | aBB
For the string aaabbabbba, Find
1) Leftmost derivation
2) Rightmost derivation
3) Parse tree AU May-13, Marks 8
Solution: For leftmost derivation and parse tree refer answer of Q.10 from Two Marks Questions with
Answers
Rightmost Derivation :
Pppppppppppppppppppppppp
Example 3.5.3 Given the grammar G = (V, Σ, R, E) where
V = {E, D, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, +, -, *, /, ()},
Σ = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, +, -, *, /, ()}, and R contains the following rules:
E→D|(E)| E+ E |E + E| E* E |E| E / E
D → 0|1|2|3| ...9
find a parse tree for the string 1+2*3. AU: Dec. 15, Marks 6
Solution: The parse tree
pppppppppppppppppppppppppppppppp
Example 3.5.4 Show the derivation steps and construct derivation tree for the string 'ababbb' by using
leftmost derivation with the grammar.
S → ABIɛ
A → aB
B → Sb. AU May-16, Marks 5
Solution :
Ppppppppppppppppppppppppppppppppp
Example 3.5.5 Consider the context free grammar (CFG) given below. Give the leftmost derivation for the
string bbaa using the grammar.
S → bS | aT | ε
T → aT | bU | ε
U → aT | ɛ
AU Dec.-18, Marks 2
Solution :
pppppppppppppppppppppppppppp
158
Course Code/Title: CS3401/Theory of Computation
159
Course Code/Title: CS3401/Theory of Computation
160
Course Code/Title: CS3401/Theory of Computation
161
Course Code/Title: CS3401/Theory of Computation
162
Course Code/Title: CS3401/Theory of Computation
163
Course Code/Title: CS3401/Theory of Computation
164
Course Code/Title: CS3401/Theory of Computation
165
Course Code/Title: CS3401/Theory of Computation
166
Course Code/Title: CS3401/Theory of Computation
167
Course Code/Title: CS3401/Theory of Computation
168
Course Code/Title: CS3401/Theory of Computation
169
Course Code/Title: CS3401/Theory of Computation
170
Course Code/Title: CS3401/Theory of Computation
171
Course Code/Title: CS3401/Theory of Computation
172
Course Code/Title: CS3401/Theory of Computation
173
Course Code/Title: CS3401/Theory of Computation
174
Course Code/Title: CS3401/Theory of Computation
175
Course Code/Title: CS3401/Theory of Computation
176
Course Code/Title: CS3401/Theory of Computation
177
Course Code/Title: CS3401/Theory of Computation
178
Course Code/Title: CS3401/Theory of Computation
179
Course Code/Title: CS3401/Theory of Computation
180
Course Code/Title: CS3401/Theory of Computation
181
Course Code/Title: CS3401/Theory of Computation
182
Course Code/Title: CS3401/Theory of Computation
183
Course Code/Title: CS3401/Theory of Computation
184
Course Code/Title: CS3401/Theory of Computation
185
Course Code/Title: CS3401/Theory of Computation
186
Course Code/Title: CS3401/Theory of Computation
187
Course Code/Title: CS3401/Theory of Computation
188
Course Code/Title: CS3401/Theory of Computation
189
Course Code/Title: CS3401/Theory of Computation
190
Course Code/Title: CS3401/Theory of Computation
191
Course Code/Title: CS3401/Theory of Computation
192
Course Code/Title: CS3401/Theory of Computation
193
Course Code/Title: CS3401/Theory of Computation
194
Course Code/Title: CS3401/Theory of Computation
195
Course Code/Title: CS3401/Theory of Computation
196
Course Code/Title: CS3401/Theory of Computation
197
Course Code/Title: CS3401/Theory of Computation
198
Course Code/Title: CS3401/Theory of Computation
199
Course Code/Title: CS3401/Theory of Computation
200
Course Code/Title: CS3401/Theory of Computation
201
Course Code/Title: CS3401/Theory of Computation
202
Course Code/Title: CS3401/Theory of Computation
203
Course Code/Title: CS3401/Theory of Computation
204
Course Code/Title: CS3401/Theory of Computation
205
Course Code/Title: CS3401/Theory of Computation
206
Course Code/Title: CS3401/Theory of Computation
207
Course Code/Title: CS3401/Theory of Computation
208
Course Code/Title: CS3401/Theory of Computation
209
Course Code/Title: CS3401/Theory of Computation
210
Course Code/Title: CS3401/Theory of Computation
5
Course Code/Title: CS3401/Theory of Computation
6
Course Code/Title: CS3401/Theory of Computation
7
Course Code/Title: CS3401/Theory of Computation
8
Course Code/Title: CS3401/Theory of Computation
9
Course Code/Title: CS3401/Theory of Computation
10
Course Code/Title: CS3401/Theory of Computation
11
Course Code/Title: CS3401/Theory of Computation
12
Course Code/Title: CS3401/Theory of Computation
13
Course Code/Title: CS3401/Theory of Computation
14
Course Code/Title: CS3401/Theory of Computation
15
Course Code/Title: CS3401/Theory of Computation
16
Course Code/Title: CS3401/Theory of Computation
17
Course Code/Title: CS3401/Theory of Computation
18
Course Code/Title: CS3401/Theory of Computation
19
Course Code/Title: CS3401/Theory of Computation
20
Course Code/Title: CS3401/Theory of Computation
21
Course Code/Title: CS3401/Theory of Computation
22
Course Code/Title: CS3401/Theory of Computation
23
Course Code/Title: CS3401/Theory of Computation
24
Course Code/Title: CS3401/Theory of Computation
25
Course Code/Title: CS3401/Theory of Computation
26
Course Code/Title: CS3401/Theory of Computation
27
Course Code/Title: CS3401/Theory of Computation
28
Course Code/Title: CS3401/Theory of Computation
29
Course Code/Title: CS3401/Theory of Computation
30
Course Code/Title: CS3401/Theory of Computation
31
Course Code/Title: CS3401/Theory of Computation
32
Course Code/Title: CS3401/Theory of Computation
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71