8B TM Solutions
8B TM Solutions
8B TM Solutions
Solution:
q0 100 ⇒ 1q0 00 ⇒ 10q0 0 ⇒ 100q0 ⇒ 10q1 0 ⇒ 101qhalt
The output of M on 100 is 101.
(b) What is the output of M on 10011? on input 11?
Solution:
The output of M on 10011 is 10100.
The output of M on 11 is 10.
(c) What function is computed by M ?
Solution:
The function computed by M is the following:
On input 1n : output 10n−1
On input ε: output ε
On input x ̸= 1n , x ̸= ε: treat x as a binary number and output x+1.
1
2. Let Σ = {#, 0, 1}. Provide an implementation level description of a input-
output TM that computes the function
#⟨x/2⟩ if x is even
f (#⟨x⟩) =
#⟨3x + 1⟩ otherwise
2
A. If all of the three digits are 0, write 0 to that place on the
first tape and replace the rightmost # on the third tape with
a 0.
B. If only one of them is 1, write 1 to that place on the first
tape and replace the rightmost # on the third tape with a 0.
C. If only two of them are 1, write 0 to that place on the first
tape and replace the rightmost # on the third tape with a 1.
D. If all three of them are 1, write 1 to that place on the first
tape and replace the rightmost # on the third tape with a 1.
ii. Cross off the rightmost digit on the second tape that has not
been crossed off.
(g) Add 1 to the number on the first tape by starting with the rightmost
digit and checking if it is 0. If it is 0, change it to 1 and go to the
final step. If it is 1, change it to 0, proceed to the next rightmost
digit and repeat this step.
(h) If the first symbol after the # on the first tape is a 0, delete it and
shift the remainder of the tape one to the left. Halt.
3
3. Show that ECF G = {⟨G⟩|G is a CFG and L(G) = ϕ} is decidable.
Solution:
Observe that L(G) is the empty set (generates no strings) if it’s impossible
to generate a terminal from the start state S, no matter how you follow
the rules. For example, the set of rules
• S→A
• A→A
• B→b
S → A → A → A → ...
Even though there’s a rule B → b that can get you a terminal, you can
never reach B from S.
To show that this problem is decidable, we note that there must be a
finite number of rules. Thus we can look through each of these rules and
see if we can reach a string of terminals by following them. We start by
marking variables that can generate a string of terminals. We then repeat
this process again and again, checking all the variables that are within
i steps of being able to generate a string of terminals. When no new
variable is added, we check if the start variable has been marked. If so,
the language has at least one string in it, so we reject. In more detail,
consider the following algorithm.
4
Observe that a variable is in the set Vi is i steps away from being able to
generate a string of terminals.
x ̸∈ ECF G =⇒ either (a) x ̸= ⟨G⟩ (not a valid encoding), or (b) x = ⟨G⟩
for a CFG G = (V, Σ, R, S) and there exists some derivation S →∗ w
where w ∈ Σ∗ . In case (a), x is rejected immediately. In case (b), since
the derivation is of finite numbre of steps m, it means that if S was not
marked before, it will be marked when the loop is executed for the nth
time. Hence, the algorithm rejects in step (d). In any case, x is rejected.
x ∈ ECF G =⇒ x = ⟨G⟩ for a CFG G = (V, Σ, R, S), but there is no
derivation starting from S that yields a string of terminals. Hence, when
the loops finishes executing, S will not be marked, and the algorithm
accepts in step (d).
5
4. Let L = {⟨M, k⟩|M is a TM, k is a positive integer, and there exists an
input to M that makes M run for at least k steps}
Prove that that L is decidable.
First attempt
We can construct a TM that decides L. It will do the following:
(a) On input ⟨M, k⟩ where M is a TM:
(b) For all strings wi , i ∈ N
- run M on wi for k steps. If M doesn’t terminate on wi within k
steps, accept.
(c) If we’re finished enumerating and M terminated within k steps every
time, reject
Problem: When do we reject? We need to enumerate through an infinite
number of strings! Therefore, we will never arrive at step (c). This only
recognizes the language, not decides it
Observation: Each ”step” of a TM’s computation means it executes a
single transition function. Each time a transition function is executed, the
head of the TM can only move at most a single space in either direction.
Even if the TM head moves right on every single transition, then in k
steps, the head could only ever look at the first k + 1 characters on the
tape. Therefore, to determine whether the machine ever runs for more
than k steps or whether it halts within k steps on every input, anything
on the tape after the first k + 1 symbols is not relevant. Thus, we don’t
need to enumerate over any strings that are longer than k + 1 characters
long.
Second attempt
We can construct a TM that decides L. It will do the following:
(a) On input ⟨M, k⟩ where M is a TM:
(b) For all strings wi where len(wi ) ≤ k + 1:
- run M on wi for k steps. If M doesn’t terminate on wi within k
steps, accept.
(c) If we’re finished enumerating and M terminated within k steps every
time, reject