Question 1. [10 marks] Show whether or not the set of all even integers {…,-4,-2,0,2,4,…} is countable.. Answer 1. This is countable. To prove this, we show that there is a bijection between this set and the set of natural numbers. The bijection is as follows: f(n) = n, if n is even = -n-1, otherwise Question 2. [10 marks] Write a regular expression which describes the language of binary numbers from the alphabet {0,1} which are either odd or a power of 2 (or both). Answer 2. (0|1)*1 | 10* Question 3. [10 marks] Construct a deterministic finite-state automaton to recognise the language described in Question 2. Answer 3. 0 1 1 1 0 1 0 1 0 2 3 0 Question 4. [10 marks] Consider the following grammar with start symbol S: S if id then S else S S if id then S S id Show that this grammar is ambiguous by giving two distinct parse trees for the following sentence: if id then if id then id else id Answer 4. S if S id then S if id then if S else S id id id then if S else id then S S id id Question 5. [10 marks] Consider the following grammar with start symbol E: E E+T | E-T | T T T*F | T/F | F F (E) | id Construct a parse tree for the following string: id*(id-id)/id Answer 5. E T T*F F*F id*F id*T/F id*F/F id*(E)/F id*(E-T)/F id*(T-T)/F id*(F-T)/F id*(id-T)/F id*(id-F)/F id*(id-id)/F id*(id-id)/id Question 6. [10 marks] Construct a pushdown automaton that recognises the language {ajbk: j > k} Answer 6. states input symbols stack symbols initial state final states transitions {q, s, f} {a, b} {a} s {f} { ((s, , ), (s, a)), ((s, b, ), (q,)), ((q, b, ), (q,)), ((q, , a), (f, )), ((f, , ), (f, )) } Question 7. [10 marks] Consider a Turing machine with start state 0 and the following transitions: State Symbol (State, Symbol) 0 a (1, #, R) 0 b (4, #, R) 0 # (0, #, Y) 1 a (1, a, R) 1 b (1, b, R) 1 # (2, #, L) 2 a (3, #, L) 2 # (2, #, Y) 3 a (3, a, L) 3 b (3, b, L) 3 # (0, #, R) 4 a (4, a, R) 4 b (4, b, R) 4 # (5, #, L) 5 b (3, #, L) 5 # (5, #, Y) Trace the execution of this Turing machine with the string baaab# as input. Answer 7. 0 4 4 4 4 4 5 3 3 3 3 0 1 1 baaab# #aaab# #aaab# #aaab# #aaab# #aaab# #aaab# #aaa## #aaa## #aaa## #aaa## #aaa## ##aa## ##aa## 1 2 3 3 0 1 2 ##aa## ##aa## ##a### ##a### ##a### ###### ###### accepts Question 8. [10 marks] Describe the language which is accepted by the Turing machine in Question 7. What is meant by the statement f(n) = O(g(n))? What is the time complexity of the Turing machine in Question 7 (in terms of O(f(n)) notation) for an input of length n? Answer 8. The Turing machine accepts the language {w {a,b}* | w = wR} We say that f(n) = O(g(n)) if there are positive constants c and N such that f(n) cg(n) for all integers n N. The time complexity of the machine is O(n2) Question 9. [10 marks] Is the following problem decidable? Give a proof of your answer. Given a Turing machine T and a string w, does T loop forever on input w? Answer 9. This problem is undecidable. To show this is the case, we reduce the problem of whether or not a given Turing machine halts over a given string (which is known to be undecidable) to it. Given a Turing machine T, we construct a Turing machine T' that accepts the same language but never crashes. Then for any input string w, T fails to accept w if and only if T' loops forever on w. This means that the problem of whether or not a given Turing machine halts over a given string is reducible to this one. Question 10. [10 marks] Describe what is meant by each of the following: (a) Recursive languages (b) Recursively enumerable languages (c) Primitive recursive functions Show that the function to add together two natural numbers is primitive recursive. Answer 10. (a) A language is recursive if there is a decision procedure for every string to indicate whether or not it belongs to the language. (b) A language is recursively enumerable if there is a Turing machine which accepts it. (c) A function f is primitive recursive if it can be constructed from functions g and h as follows: f(x,0) = h(x) f(x,succ(y)) = g(x,y,f(x,y)) The add function can be defined as a primitive recursive function as follows: add(m,0) = m add(m,succ(n)) = succ(add(m,n))