Anda di halaman 1dari 5

300121 Formal Languages and Automata Assignment One (10% = 10 Marks) 1. Prove that Proof: We prove this by contradiction.

n. Assume that 14 is rational. Then, there exists two natural numbers n, m such that 14 = n/m. In addition, we can assume that n and m have no common factors. Hence, we have 14 = n2 /m2 . That is, n2 = 14 m2 . Hence, n must be even. Let n = 2a . Then, 4a2 = 14m2 . Therefore, 2a2 = 7m2 . This shows that m must be even as well. However, this contradicts to our assumption that n and m have no common factors. Thus, 14 is not rational. 2. Using induction to prove that for all n 4 the inequality 2n < n! holds. (1 mark) Proof: Induction basis: If n = 4, then 2n = 24 = 16; n! = 4! = 24. In this case, 2n < n!. Induction hypothesis: Suppose that 2n < n! holds for all n, 4 n k. Induction step: Consider n = k + 1. We have 2k+1 = 2 2k (k + 1) 2k (k + 1) k! = (k + 1)!. This shows that 2n < n! holds when n = k + 1. Hence, 2n < n! holds for all n 4. 3. Give DFAs accepting the following languages over = {0, 1}. (a) The set of all strings with three consecutive 0s; (1 mark) (b) The set of strings with 011 as a substring. (1 mark) Answer: (a) DFA M = (Q, , , q0 , F ), where Q = {q0 , q1 , q2 , q3 }, = {0, 1}, F = {q3 } and: (q0 , 0) = {q1 }, (q0 , 1) = {q0 }, (q1 , 0) = {q2 }, (q1 , 1) = {q0 }, (q2 , 0) = {q3 }, (q2 , 1) = {q0 }, (q3 , 0) = {q3 }, (q3 , 1) = {q3 }. 1 0,1 1 1 start q0 0 q1 0 q2 0 q3 14 is not rational. (1 mark)

(b) DFA M = (Q, , , q0 , F ), where Q = {q0 , q1 , q2 , q3 }, = {0, 1}, (q0 , 0) = {q1 }, (q0 , 1) = {q0 }, (q1 , 0) = {q1 }, (q1 , 1) = {q2 }, (q2 , 0) = {q1 }, (q2 , 1) = {q3 }, (q3 , 0) = {q3 }, (q3 , 1) = {q3 }. 1 q0 0 0 0 start q1 1

F = {q3 } and:

0,1 1

q2

q3

4. Convert the following DFA M = (Q, = {0, 1}, , q0 , Qf ) into a regular expression, where Q = {q0 , q1 , q2 , q3 }, Qf = {q0 }, and (q0 , 0) = q3 , (q1 , 0) = q0 , (q2 , 0) = q2 , (q3 , 0) = q1 , (1 mark) Answer: To work out the regular expression form DFA/NFA, rstly we need to modify the given DFA/NFA to a standard form, then remove all the middle states until we have only the initial state and the accepting state left in the form of GTG. Given DFA: 1 q0 0 q1 Then remove q1 : 1 0 start q4 q0 00 10 Next remove q3 : q3 11 01 1 q2 0 q3 0 1 0 1 1 0 q2 q4 After Modify: 1 q0 0 q1 q3 0 1 0 1 1 0 q2 (q0 , 1) = q0 , (q1 , 1) = q3 , (q2 , 1) = q1 , (q3 , 1) = q2 .

start

start

1 + 0(01)*00

0(01)*10*

0 + 11(01)*1

start

q4

q0 10 + 11(01)*00

q2

start

q4

q0

Now we have the regular expression: [1 + 0(01)*00]*+[1 + 0(01)*00]*0(01)*10*[0 + 11(01)*1]*[10 + 11(01)*00].

5. Given the following NFA M = (Q, = {0, 1}, , q0 , Qf ), where Q = {q0 , q1 , q2 }, Qf = {q1 , q2 }, and (q0 , 0) = {q1 }, (q1 , 0) = {q0 , q2 }, (q1 , ) = {q0 }, (q2 , 1) = {q1 }, (q0 , ) = {q1 }, (q1 , 1) = {q1 , q2 }, (q2 , 0) = {q2 }, (q2 , ) = {q1 }.

Convert this M into a DFA. (1 mark) Answer: To convert NFA to DFA, we need to perform the following steps: Given NFA: 1 0 0, 1 0, start q0 0, NFA to DFA: 0, 1 q1 1, q2

start

q0

0, 1

q0 , q1 , q2

6. Find context-free grammar for the following languages (with n 0, m 0, k 0).

(a) L = {an bm ck : n + 2m = k}; (1 mark) (b) L = {an bm ck : n = m or m = k}. (1 mark) Answer: ANSWER: A grammar G = (V, T, S, P ) is called a context-free if all productions in P have the form A x, where A V and x (V T )*. In this case, n + 2m = k, thus, if there is 1a, there will be 1c, and if there is 1b, there will be 2c. Thus, G = ({S, A}, {a, b, c}, S, P ), where P is: S aSc | S1 | S1 bS1 cc | (b) L = {an bm ck : n = m or m = k}. (1 mark) ANSWER: In this question, there are 3 cases: (1). n = m, k is arbitrary; (2). m > k, n is arbitrary; and (3). m < k, n is arbitrary. Case(1). n = m, k is arbitrary: S1 AC A aAb | C Cc | Case(2). m > k, n is arbitrary: S2 BDE B aB | D bD | b E bEc | Case(3). m < k, n is arbitrary: S3 BF G B aB | F bF c | G cC | c Thus, S S1 | S2 | S3

7. The following grammar generates the language of regular expression 0 1(0 + 1) : S A1B A 0A | B 0B | 1B | Give the leftmost and rightmost derivations of the strings: (a) 00101; (b) 1001; and (c) 00011. (2 marks) Answer: (a) 00101;

ANSWER: Left Most Derivation: S A1B 0A1B 00A1B 001B 0010B 00101B 00101 Right Most Derivation: S A1B A10B A101B A101 0A101 00A101B 00101 (b) 1001; and ANSWER: Left Most Derivation: S A1B 1B 10B 100B 1001B 1001 Right Most Derivation: S A1B A10B A100B A1001B A1001 1001 (c) 00011. ANSWER: Left Most Derivation: S A1B 0A1B 00A1B 000A1B 0001B 00011B 00011 Right Most Derivation: S A1B A11B A11 0A11 00A11 000A11 00011