BCS402 ST1 QPsol
BCS402 ST1 QPsol
BCS402 ST1 QPsol
NOTE: Solutions are deliberately expanded to enhance the understanding of underlying concepts, and pre-
pare you well for other similar questions. Also, there are some important additional details. Read everything
very carefully!
0 1 2
q0 ε q1 ε q2
(c) Use a suitable example to explain finite automaton without output. (K2 )
Solution. A finite automaton M over an alphabet Σ is the simplest model of computation that helps to
recognise patterns of strings w ∈ Σ∗ consisting of a finite number of alphabet symbols. It consists of a
finite set of states Q = q0 , q1 , . . . , qn−1 , where q0 is called the initial state, and a set F ⊆ Q has accept
states. At each step of computation, M reads leftmost symbol of an input string w (say a ∈ Σ), and
switches from a state q ∈ Q to a state p ∈ Q (with possibly p = q). That is, we have p := δ q, a , where
δ : Q × Σ → Q is called the transition function of the automaton M. So, the function δ specifies how
Table 1: Differences between a DFA and an NFA
DFA NFA
δ (r, a) is defined for all pairs (r, a) ∈ Q × Σ δ (r, a) may not be defined for some pairs (r, a) ∈ Q × Σ
δ (r, a) ∈ Q δ (r, a) ∈ P(Q)
ε-moves are not involved Use of ε-moves is common
is a single machine acts like multiple DFAs computing at the same time
difficult to construct easy to construct
w is accepted if δb(q0 , w) ∈ F w is accepted if δb(q0 , w) ∩ F ̸= ∅
execution time is less; needs more space less space is needed; execution takes longer time
each DFA is an NFA no NFA is a DFA
computation of regex is difficult computation of regex is easier
M switches states in Q after reading w left-to-right (one symbol at a time). Suppose δb(q0 , w) = q ∈ Q.
We say w is recognised by M if q is an accept state, i.e., q ∈ F. Otherwise, we say w is not recognised
by the automaton M. Therefore, the main role that a finite automaton without output plays is that it
acts like an acceptor or a rejector of strings. For example, consider a DFA with the state diagram as
in Fig. 2(a). We have Q = q0 , q1 , Σ = {0, 1}, F = q1 , and the transition function δ is given by
δ q0 , 0 = q0 , δ q0 , 1 = q1 , δ q1 , 0 = q0 , and δ q1 , 1 = q1 . Notice that it accepts exactly the bit
strings that end in 1. †
0 1 0,1 0,1
0
0 0,1 0
q0 q1 p q r s
1
(a) DFA for Q. 1(d) (b) NFA for Q. 1( f )
Figure 2
Equivalently, x and y are distinguishable with respect to L if xz, yz ∈ L, for some string z ∈ Σ∗ . According
to the theorem, a set L ⊆ Σ∗ is a regular language if and only if the equivalence classes of the relation
1 It
is a fundamental fact related to formal language, which was first proven by the British mathematician John Myhill in 1957,
and also independently by the American mathematician Anil Nerode in 1958.
2
(1.1) gives a finite partition of the set Σ∗ . Consider the set L = x ∈ {0, 1}∗ | x ends with 1 realised
by a DFA with the state diagram as in Fig. 2(a). Notice that a bit string w terminates at q1 if it ends with
1; otherwise, it terminates at the initial state q0 . We may thus take C1 and C2 to be the set of strings that
do and respectively don’t end in 1. In the partition given by C1 and C2 , we have C2 = L so that the states
associated with it are precisely the accept states. †
(f) Write the transition table for FA with the state diagram as in Fig. 2(b). Is it an NFA or a DFA? (K2 )
Solution. The transitions are as given below:
δ p, 0 = p, q , δ p, 1 = p , δ q, 0 = δ q, 1 = r ,
δ r, 0 = s , δ r, 1 = ∅, δ s, 0 = δ s, 1 = s
Therefore, it is an NFA. †
function(...) {
Input ... Output
}
3
(a) Construct a DFA for each one of the following: (K3 )
Solution. The state diagram of a DFA for (i) is shown in Fig. 4(a); the state diagram of a DFA for (ii) is
shown in Fig. 4(b); the state diagram of a DFA for (iii) is shown in Fig. 4(c); and, the state diagram of
a DFA for (iv) is shown in Fig. 4(d). †
0 1 0 1
1 p q r
1 0 1 0
p q r s 1 1
0 0
0 D 0,1 s 1
0
(a) DFA for Q. 2a(i) (b) DFA for Q. 2a(ii)
0
q1 1 q1 2 0,1,2
0,1 1 2
2 0,1
q0 0,1 q0 qf D
0,1 0 2 2
0
q2 0 q2 q3 1
1
(c) DFA for Q. 2a(iii) (d) DFA for Q. 2a(iv)
Figure 4
(b) Convert NFA with the state diagram as in Fig. 2(b) into an equivalent DFA. (K3 )
Solution. Applying subsets construction method to the transition table of NFA with the state diagram as
in Fig. 2(b), we obtain the transition table of an equivalent DFA, as shown in Fig. 5(a). So, the state
diagram the DFA is as in Fig. 5 (b). †
4
δ 0 1
1 1
p pq p 1
pq pqr pr 0 0 1
p pq pqr pr
pqr pqrs pr
pr pqs p 0 0
0
pqs pqrs prs
pqrs pqrs prs pqs pqrs prs ps
0 1 1
prs pqs ps
ps pqs ps 01
0 1
(a) Transition-Table for Q. 2b (b) DFA for Q. 2b
Figure 5
PS ε∗ 0 ε∗ PS ε∗ 1 ε∗ PS ε∗ 2 ε∗
q0 q0 q0 q0 , q1 , q2 q0 q0 ∅ ∅ q0 q0 ∅ ∅
q1 ∅ ∅ q1 q1 q1 , q2 q1 ∅ ∅
q2 ∅ ∅ q2 ∅ ∅ q2 q2 q2
q1 q1 ∅ ∅ q1 q1 q1 q1 , q2 q1 q1 ∅ ∅
q2 ∅ ∅ q2 ∅ ∅ q2 q2 q2
q2 q2 ∅ ∅ q2 q2 ∅ ∅ q2 q2 q2 q2
q0 0 q012 1 q12
0 1 2 1
0
0, 1 1, 2 2 2 2 0
q0 q1 q2
2 q2 DS
0, 1, 2 0,1
(a) (a)
(d) Consider the DFA M = Q, Σ, δ , q0 , F , where Q = q0 , q1 , . . . , q6 , q7 , Σ = a, b , F = q3 , and
5
the transition function δ of M is given by
δ q0 , a = q1 , δ q0 , b = q0 , δ q1 , a = q0 , δ q1 , b = q2 ,
δ q2 , a = q3 , δ q2 , b = q1 , δ q3 , a = q3 , δ q3 , b = q0 ,
δ q4 , a = q3 , δ q4 , b = q5 , δ q5 , a = q6 , δ q5 , b = q4 ,
δ q6 , a = q5 , δ q6 , b = q6 , δ q7 , a = q6 , δ q7 , b = q3 ,
It thus follows that automaton with the state diagram as in Fig. 8(b) is the desired equivalent DFA. †
b b a
q0 a q1 b q2 a q3 a
b q0 q1
a b b
a b a
b b b
q4 b q5 a q6 a q7
q3 a q2
a
b a
(a) State diagram for Q. 2d (d) Minimal DFA for Q. 2d
Figure 8
(e) Convert the Mealy machine shown in Fig. 9(a) into an equivalent Moore machine. (K2 )
6
0/z1 0 0
q1 q2 0/z2 q1 / q2 /z1 q′2 /z2 0
1
1/z1 0/z1 1/z1 1 0 1
0
1
q3 1/z2 q3 /z1 q′3 /z2 1
(a) Mealy Machine for Q. 2(e) (b) Moore Machine for Q. 2(e)
Figure 9
(b) Write a regex over the alphabet Σ = {a, b, c} containing at least one a and two b’s. (K2 )
Solution. It is given by (a + b + c)∗ a(a + b + c)∗ b(a + b + c)∗ b(a + b + c)∗ . †
= QP∗
Arden’s theorem provides an elegant method to write a regular expression for a finite automaton. More
precisely, suppose M is finite automaton with n states q1 , q2 , . . . , qn such that q1 is the initial state. We
repeatedly apply Arden’s theorem to solve the following linear systems of state equations:
where ai j ∈ Σ are labels of the edges from state qi to state q j . Further, we take ai j = ∅ when no such
edge exists. The goal is to obtain the regular expressions for all available accept states. The final answer
is obtained by taling sum of these expressions. The theorem finds applications in compiler design: During
lexical analysis, regular expressions are used to define tokens of a program. By simplifying and converting
them into equivalent finite automata, a compiler efficiently recognises and tokenises input streams.
7
or more occurrences of the triplet “aaa”, followed by zero or more occurrences of the triplet “bbb”, and
ending in the symbol b. †
(e) Find regex for the language consisting of strings in {0, 1}∗ such that the 10th symbol from the right
ends in 0. (K2 )
∗ 9
Solution. It is given by (0 + 1) 0(0 + 1) . †
(f) Compare the terms regular expression, regular set, and a finite automaton. (K2 )
Solution. A regular expression over an alphabet Σ is a “pattern form” constructed using symbols in Σ and
usual operations such as sum, concatenation, and the ∗-closure. A regular expression R for a regular set
(or regular language) L provides an algebraic description of the finite automaton M realising L. In some
cases, it provides the only way to represent a finite automaton, especially when the latter has very large
number of minimum states. Kleene proposed the concept of regular expressions to capture the “structure
of strings" in a regular language L by matching their patterns. The concept of regular expression is an
efficient tool to search and process strings of a regular set. †
Solution. Suppose the language described by regular expression R has finite automaton with the state dia-
gram as in Fig. 10(a). Then a finite automaton for R∗ has the state diagram as in Fig. 10(b). In this, ε-move
from q f to q0 creates the loop (ba)∗ , and the ε-move from q′0 to q′f ensures that the associated regular set
contains ε. †
ε
ε
b a ε b a ε
q0 q1 qf q′0 q0 q1 qf q′f
1
q0 0 q1 0 0, 1 0 1
0
0 1 1 0 1
1 q2 −1
Figure 11
8
Q 4 Attempt any THREE questions. Each question is of six marks. (3 × 6 = 18 Marks)
(a) Use Arden’s theorem to construct a regex for FA with the state diagram as in Fig. 11(a). (K3 )
Solution. In this case, the “state equations" are given by
q0 = ε + q2 · 0; (1.2a)
q1 = q0 · 0 + q1 · 0; (1.2b)
q2 = q0 · 1 + q1 · 1 + q2 · 1; (1.2c)
q1 = q0 · 00∗ = q0 · 0+ .
q2 = q0 · 1 + q1 · 1 + q2 · 1
= q0 · 1 + q0 · 0+ 1 + q2 · 1
= q0 1 + 0+ 1 + q2 · 1
Once again, applying Arden’s theorem to the last equation, it follows that
q2 = q0 1 + 0+ 1 1∗ .
q0 = ε + q2 · 0 = ε + q0 1 + 0+ 1 1∗ 0
Since q0 and q1 are accept states, the associated regular expression is given by
∗
1 + 0+ 1 1∗ 0 ε + 0+
9
q1
10 1 0
q0 0 qf
q0 qf
00 + 11 10
q3
(00 + 11)0∗ 10
(a) (b)
q1 q1
0
1 0 1 qf
q0 1 q2 qf q0 1 q2
0 0
0
0 1 10 0 1
q4 0 q3 q4 0 q3 1 q5
(c) (d)
(c) Find regexes of the two regular sets given below: (K3 )
∗
Solution. A regex for (i) and (ii) are respectively given by b∗ ab∗ ab∗ ab∗ ab∗ ab∗ and b∗ ab∗ ab∗ ab∗ . †
(d) State Kleene’s theorem and illustrate its proof using suitable examples. (K2 )
∗
Solution. The Kleene’s theorem states that a set L ⊆ Σ is a regular language if and only if some regular
expression R over the alphabet Σ describes L. Since we know that L is a regular language if and only
if there is a finite automaton M such that L = L(M). The theorem boils down to equivalence between
a regular expression and a finite automaton. Using the recursive definition of regular expression, a
simple structural induction establishes the fact that there is a finite automaton for every type of a regular
expression. In the solution of Q. 4(b), however, we have used a more direct approach. The proof of the
converse is bit involved. However, given a finite automaton, one may use state elimination method to
obtain its regular expression. We first list the general rules (to be followed in that order only):
(1) If there is an incoming edge at the initial state q0 , introduce a new initial state q′0 , and add an ε-edge
from q′0 to q0 .
(2) If there is an outgoing edge from a final state q f , introduce a new final state q′f , and add an ε-edge
from q f to q′f .
(3) Replace all final states by a single final state, using ε-edges.
10
(4) Eliminate all states one-by-one (in any order), excepting the initial and the final state.
We now consider a DFA realising the strings w ∈ {0, 1}∗ such that n1 (w) mod 3 = 0. The related state
diagram is shown in Fig. 13(a). Since there is an incoming edge at q0 , by Rule-(1), we introduce q′0 as
the new initial state, and add an ε-move from q′0 to q0 . The modified state diagram is as in Fig. 13(b).
Again, since there is an outgoing edge from the final state q0 , by Rule-(2), we introduce a final state q f
and add an ε-move from q0 to q f . The resulting state diagram is as in Fig. 13(c). The Rule-(3) is not
applicable in this case. To apply the Rule-(4), we start with elimination of the state q2 . In doing so, all
edges through q2 are to be accounted for. We thus obtain a DFA with the state diagram as in Fig. 13(d).
We next eliminate the state q1 so that the resulting DFA has the state diagram as in Fig. 13(e). Finally, it
follows from Fig. 13( f ) that associated regular expression is 0 + 10∗ 10∗ 1. †
0 0 0 0 0 0
1 1 ε 1 1
q0 q1 q2 q′0 q0 q1 q2
1 1
(a) (b)
0 0 0 0 0
ε 1 1 ε 1
q′0 q0 q1 q2 q′0 q0 q1
10∗ 1
ε 1 ε
qf qf
(c) (d)
ε ε
q′0 q0 10∗ 10∗ 1 q′0 q0 0 + 10∗ 10∗ 1
ε ε
qf qf
(e) (f)
(e) Write regular expression for the following two regular sets: (K3 )
(i) consisting of strings w ∈ {0, 1}∗ with the same number of 0;s and 1’s such that, in every prefix of w,
11
the number of 0’s and 1’s differ by at most 1;
(ii) consisting of strings w ∈ {0, 1}∗ that don’t contain 101 as a substring.
Solution. For (i), the regular set contains strings w ∈ {0, 1}∗ that alternate between 0’s and 1’s. Said differ-
ently, it contains precisely the string w ∈ {0, 1}∗ such that the absolute difference in the count of 0’s and 1’s
in every prefix of w is less than 2. This latter version help us to think in terms of a NFA for the given regular
set. For, the minimal NFA must have 3 states respectively representing the differences of −1, 0, and 1. Start
in state 0 and go to state 1 when reading a 1; and, to the state −1, when reading a 0. In state 1, go back
to state 0 when reading a 0, or else fail when reading another 1. We can use symmetry to process similarly
while in state −1. We now read a regular expression for this NFA with the state diagram as in Fig. 11(b).
Based on the preceding discussion, if you read 01 or 10, the net difference is zero. If not stopped, then read
one more 0 or 1. Therefore, the regular expression is obtained as (01 + 10)∗ (ε + 0 + 1). For (ii), notice that
we are looking for a regular expression of a DFA that is complement of a DFA with the state diagram shown
in Fig. 14(a). Therefore, for (ii), a regular expression is given by
∗
(0 + 1)∗ (0 + 00)∗ (1 + 11)(0 + 00)∗ 0∗ 11∗ 000∗ 11∗ 01(0 + 1)∗ ,
0 1 0,1 0 1 0,1
q1 1 q2 0 q3 1 q4 q1 1 q2 0 q3 1 q4
0 0
(a) (b)
Figure 14
12