# Solutions to Second Midterm Exam

#### 13 April 2008

Question text is in black, solutions are in blue.

```  Q1: 10 points
Q2: 10 points
Q3: 10 points
Q4: 10 points
Q5: 20 points
Q6: 30 points
Q7: 30 points
Total: 120 points
```

• Question 1 (10): (True/False with justification) Define the language SUBCFG to be {(G1, G2): G1 and G2 are CFG's and L(G1) ⊆ L(G2)}. Then SUBCFG is not Turing decidable.

TRUE. There are two good proofs. One shows ALLCFGm SUBCFG, which suffices because ALLCFG was proved to be undecidable in the text. This reduction takes a grammar G and returns (GALL, G) where GALL is any grammar that generates Σ*. This is correct because Σ* ⊆ L(G) if and only if L(G) = Σ*.

The other proof is a Turing reduction showing that EQCFGT SUBCFG, which suffices because EQCFG was shown to be undecidable in a homework problem. This reduction decides whether (G1, G2) is in EQCFG by testing whether (G1, G2) and (G2, G1) are both in SUBCFG.

• Question 2 (10): (True/False with justification) Define the language TRTM to be {M: M is a Turing machine and L(M) is TR}. Then TRTM is Turing decidable.

TRUE. One might try to prove this undecidable with the Rice-Myhill-Shapiro Theorem, but this property of TM languages is trivial so it doesn't apply. In fact for any machine M, L(M) is TR by definition. So to decide this language we need only decide whether the input string is a valid TM, and this is decidable.

• Question 3 (10): (True/False with justification) Let A be any Turing decidable language. There must exist an enumerator E that outputs all the elements of A in nonincreasing order , which means that E never outputs a longer string after a shorter one.

FALSE. Let A be any TD language with an infinite number of strings, such as 0*. The enumerator must begin by printing some string w, but then it is only allowed to print strings of length at most that of w. There are only finitely many such strings, so it cannot print all the strings in A.

• Question 4 (10): (True/False with justification) For any Turing machine M, define LD(M) to be the language of all strings w such that ww (the concatenation of two copies of w) is in L(M). There exists a Turing machine N such that LD(N) = L(N).

TRUE. Such a machine is guaranteed to exist by the fixed-point version of the Recursion Theorem (Theorem 6.8), because we could have f transform any machine M into a machine N such that L(N) = LD(M). But it's easier to give an example of a machine whose language X has the property that the set of strings w such that ww ∈ X is exactly X. The empty language has this property, as do {ε} and Σ*.

• Question 5 (30): For this question, recall that the alphabet of a one-tape Turing machine must contain a blank character, which initially occupied all the cells on the tape to the right of the input. The blank character cannot occur as part of the input string, and (following Sipser) we assume that there are no tape cells to the left of the input string

A left-blank Turing machine is a one-tape Turing machine that always moves left when it sees a blank character. We define ALBTM to be the set of pairs (M, w) where M is a left-blank Turing machine and w ∈ L(M).

ALBTM is not Turing decidable, because we can mapping-reduce ATM to it. Given any one-tape Turing machine M, we want to build a left-blank machine M' such that L(M) = L(M'). M' has the same alphabet as M but has one new character b, a "fake blank". When it sees a b, it does what M would have done on a real blank. When it sees a real blank in some state q, it overwrites the blank with a b, moves left (as it must) and enters a new state q'. In state q', whatever it sees, it move right and goes back to state q. (Note that in order to restore the state after the move left, we need a separate state q' for each state q of M.) When m would write a blank, we have M' write a b instead. The computation of M' tracks that of M exactly, except for these extra two steps the first time M' sees each blank -- these two steps convert the blank to a b and M' then continues. There is one additional technicality -- if the input string is ε we must deal with the fact that when M' sees the first blank in state q0, it will attempt to move left but actually stay in place under Sipser's rules for Turing machines. So we must have δ(q0,blank) = (q0,b,L), create no new state q0', and make sure that M' never reenters state q0 after it starts.

• (b,10) Similarly, a right-blank Turing machine is a one-tape Turing machine that always moves right when it sees a blank, and ARBTM is the set of pairs (M, w) where M is a right-blank Turing machine and w ∈ L(M). Is ARBTM Turing decidable? Prove your answer.

The language ARBTM is Turing decidable. If a right-blank Turing machine ever reaches the blank to the right of the input, it will never return to the input because it will see blanks and move right until or unless it accepts or rejects. We can check each state q to see what happens if the machine sees the first blank in state q -- whether it eventually accepts, eventually rejects, or goes right forever. (If it ever revisits a state we know it will go right forever, so this check takes only a finite number of steps.

We can then modify M to get a machine M' that accepts or rejects as soon as it sees the first blank. But M' is in effect an LBA, because it never leaves the area of the original input. We know that ALBA is decidable -- this is Theorem 5.9 in Sipser, or we can recall that we need only simulate an LBA for long enough that we know it to be in a loop if it has not yet halted.

• Question 6 (30): This question deals with functions from N to N, where N is the set {1,2,3,...}. Define S to be the set of all functions from N to N. A function in S is called periodic if there are positive integers t and q such that for all n with n ≥ t, f(n) = f(n+q). For example, the function whose values in order are (3, 16, 3, 3265, 24, 7, 24, 7, 24, 7, 24, 7,...) is periodic because we may take t = 5 and q = 2. Define SP to be the set of all periodic functions in S.

• (a,10) Prove that S is an uncountable set. (Hint: If f is any function from N to S, show that f is not onto by giving an example of a function in S that is not in the range of f.)

Following the hint, let f be any function from N to S. We will show that f is not onto, which means S is uncountable because "counting" S would mean constructing an f that was onto. For every positive integer i, let gi be the function f(i). Then define a function d from N to N by the rule d(i) = gi(i) + 1. For any i, d cannot be the same function as gi because d(i) ≠ gi(i). So f is not onto because d is not in its range.

• (b,10) Prove that SP is a countable set.

If you solve (c) first, you can simply observe that since every TM is denoted by a finite string, there are only countably many TM's and thus at most countably many functions that are computed by TM's. But a direct proof is also straightforward. If f is in SP, we can denote f by a finite sequence of positive integers: (t, q, f(1), f(2),..., f(t+q-1)) -- this is enough because every other value of f is equal to one of these and we can compute which (as we will do in part (c)). Every finite sequence of positive integers can be denoted by a finite string, and since there are only countably many strings there can be only countably many sequences and thus only countably many functions in SP.

• (c,10) Prove that every function in SP is Turing computable.

Given any function f in SP, we can construct a Turing machine that has a lookup table sequence (t, q, f(1),..., f(t+q-1)) encoded within it. On input n, this Turing machine computes f(n) by the recursive code ```if (n <= t+q-1) return lookup(n); else return f(n-q);```. Alternatively, we can avoid the recursion by returning `lookup(t + ((n-t)%q))` in the `else` case. This algorithm is clearly implementable by an always-halting Turing machine.

• Question 7 (30): Here are three easy questions dealing with mapping reducibility. Recall that if A and B are any two languages, A ≤m B means that there exists a Turing computable (always-defined) function f such that for any string x, x ∈ A if and only if f(x) ∈ B.

• (a, 10) Prove that if A ≤m B and B ≤m C for any three languages A, B, and C, then A ≤m C. (This is solved exercise 5.6 in Sipser, but of course you may not just quote it.)

Let f be the function proving A ≤m B and let g be the function proving B ≤m C. Define the function h by the rule h(x) = g(f(x)). Then (x ∈ A) ↔ (f(x) ∈ B) ∈ (g(f(x)) ∈ C) and thus h proves that A ≤m C.

• (b, 10) Recall that ATM is the set of pairs (M, W) where M is a Turing machine and w ∈ L(M). Recall that LBOS is the set of Turing machines M such that M ∉ L(M). Prove that LBOSm A TM.

Given any machine M, let f(M) be the pair (M,M). Then (M ∈ LBOS) ↔ (M ∉ L(M)) ↔ ((M,M) ∉ ATM) and f is the desired reduction.

• (c, 10) Prove that A TMm LBOS.

Given any pair (M,w), let g(M,w) be the machine NM,w that erases its input and then runs M on w. L(NM,w) is equal to Σ* if w ∈ L(M) and to ∅ otherwise, so we have that ((M,w) ∉ ATM) ↔ (g(M,w) ∈ L(NM,w)) and g is the desired reduction.