HOMEWORK 2 SOLUTIONS Copyright: without explicit written permission from the copyright owner.

advertisement
HOMEWORK 2 SOLUTIONS
JOEL FRIEDMAN
Copyright: Copyright Joel Friedman 2015. Not to be copied, used, or revised
without explicit written permission from the copyright owner.
Exercise 7.2.6
Assume that the set of functions from the integers to the integers could be listed
in a countable sequence
f1 , f2 , . . .
For each i let
0 if fi (i) 6= 0, and
g(i) =
1 otherwise.
It follows that for all i we have g(i) 6= fi (i), and hence g 6= fi . Hence g does not
appear in the above sequence.
2.
There are many variants on this algorithm. High level: read from left to right,
each time we see a 0 we toggle between two states. When we hit a blank symbol,
we figure out whether to accept or reject.
Implementation level: Q = {q0 , q1 , qacc , qrej }. Toggle between q0 and q1 until we
reach a blank; Σ = {0, 1}, Γ = {0, 1, blank}.
Formally: At this point we have already described all of the Turing machine
except for δ. So: δ(q0 , 0) = (q1 , any, R), δ(q0 , 1) = (q0 , any, R), δ(q1 , 0) =
(q0 , any, R), δ(q1 , 1) = (q1 , any, R), δ(q1 , blank) = (qacc , any, any), δ(q0 , blank) =
(qrej , any, any).
[Here any means you can put anything (i.e., it is not important); blank is the
blank symbol; any values of δ not listed are irrelevant (i.e., never reached).]
3.
There are many variants on this algorithm. This one is a bit slow but easy to
describe
High level: scan from left to right until we see a 0 followed by a 1 or vice versa; in
this case we replace both by a 2, and then repeat (ignoring the 2’s that we create).
When no consecutive 0 and 1 or vice verse—ignoring the 2’s—then we are done.
Implementation level: mark the first symbol on the tape so we know where the
left end of the input is; i.e., replace a 0 or 1 by the symbol end. So the cell alphabet
Research supported in part by an NSERC grant.
1
2
JOEL FRIEDMAN
should be Γ = {0, 1, blank, end, 2}. The states should have a a “searching for a 0
state,” call it qs0 , and a “searching for a 1 state,” call it qs1 . This seems to be all
we need, so we will have {q0 , qs0 , qs1 , , qacc , qrej }, as states, possibly needing some
more when we get to the formal description, such as a state, qL , used to move to
the left to repeat the left to right scan, and qR to find the first 0 or 1 as we repeat
the left to right scan.
Formal description:
(1) (first step:) δ(q0 , 0) = (qs1 , end, R), δ(q0 , 1) = (qs0 , end, R), δ(q0 , blank) =
(qacc , any, any),
(2) (movement in qs1 :) δ(qs1 , 0) = (qs1 , 0, R), δ(qs1 , 1) = (qL , 2, L),
δ(qs1 , blank) = (qacc , any, any),
(3) (movement in qs0 :) δ(qs0 , 1) = (qs0 , 1, R), δ(qs0 , 0) = (qL , 2, L),
δ(qs0 , blank) = (qrej , any, any),
(4) (movement in qL :) δ(qL , x) = (qL , x, L) for x = 0, 1, 2, δ(qL , end) =
(qR , end, R)
(5) (movement in qR :) δ(qR , 2) = (qR , 2, R), δ(qR , 0) = (qs1 , 2, R), δ(qR , 1) =
(qs0 , 2, R), (if no 0 or 1 on right moving scan, then same number of 0’s and
1’s and hence:) δ(qR , blank) = (qacc , any, any).
Q = {q0 , qs0 , qs1 , , qacc , qrej , qL , qR }.
4.
There are many variants on this algorithm. (This one is very slow.)
High level: use a few tapes for simplicity. If the first tape contains the number
n, use a second tape to count a counter m from 2 to n − 1, and repeatedly see if
m divides n roughly as follows: repeatedly subtract m from n and see if we ever
reach 0 (in practice you would probably want to use a division algorithm), this
subtraction could take place on a third tape.
Implementation level (medium level): The counter on the second tape is advanced by adding one and comparing the result with the first tape. The division
algorithm can work repeatedly by seeing if the result on the third tape is less than
m (the counter on the second tape), and if not then we subtract m from the number
on the third tape, and checking if it is zero.
One could alternatively subtract m starting from one digit away from the largest
digit on the third tape; this makes the division algorithm take roughly log(n/m)
iterations of the subtraction rather than n/m iterations of the subtraction.
You can start to see what the formal description looks like: we have three tapes,
some states to advance the counter on tape 2, some states to subtract (with borrowing) the result on tape 2 from tape 3, etc.
Department of Computer Science, University of British Columbia, Vancouver, BC
V6T 1Z4, CANADA, and Department of Mathematics, University of British Columbia,
Vancouver, BC V6T 1Z2, CANADA.
E-mail address: jf@cs.ubc.ca or jf@math.ubc.ca
URL: http://www.math.ubc.ca/~jf
Download