CS355 – Theory of Computation Dr. Aidan Mooney 2006 Lab 6

advertisement
CS355 – Theory of Computation
Dr. Aidan Mooney
2006
Lab 6
Hand up your work at the end of the lab or the start of the next.
With this lab sheet you will develop some basic Turing machine (TM) programming
skills. The lab sheet consists of solved example TM problems that you should try to fully
understand, and unsolved problems that you should use to test your knowledge.
You can use Paul Ming's Perl Virtual Turing Machine (VTM) to test your machines; it
gives a trace of every step in the computation. Open it up in a separate browser to give
yourself some comfort. I have written the following TMs to conform to the VTM's
convention. If you require a definition of any symbols (for example, '#' means the
beginning of a comment) see the VTM introduction page. Note: the VTM only works
with deterministic TMs.
Example Machine 2.A This TM takes a single word on its input tape, and writes a `T' on
the tape if the input word is in the language {a,b}. Assume that our tape head is
automatically positioned at the beginning of the input initially.
Solution. One valid table of behaviour corresponding to such a machine would be as
follows. The start state is 00.
#Si,R, Sf, W, M
#-------------00, a, 01, a, R # state 01 means we have found at least one `a'
00, b, 01, b, R # state 01 also means we have found at least one `b'
01, _, 99, T, R # there was only one `a' or `b', so accept
Copy and paste this machine into the VTM with the following sample inputs (without
quotes):
Tape: "___a___"
Blank: "_"
Initial: "00"
Run it with the following options "one line per step", "show state before tape", and "show
form after results".
Use the trace to follow the evolution of the computation (the angled brackets indicate the
position of the tape head at each step). Try a few inputs of your own, including
"___aa___", "___b___", "___ab___", "___bab___", and "_____".
NOTE: Make sure to put two or three blank symbols "_" on either side of input to simulate an infinite tape.
The VTM automatically positions the TM head at the first non-blank symbol in the input, or at the centre of
the input if it contains only blank symbols.
Example Machine 2.B Construct a TM that changes all of the symbols in its input word
to `a's. Formally, the TM performs the transformation f:{a,b}* {a}* where f is defined
as f(x)=a|x|. For the initial state of the computation, assume that the tape head is positioned
at the beginning of the input (for example, "abb", where the underscore represents the
position of the TM tape head). When the TM halts, the tape head should be positioned at
the beginning of the input word (in this case, "aaa"). One valid table of behaviour
corresponding to such a machine would be as follows. (The start state is 00.)
Note: if the comments in the TM below do not fit on a single line when you paste them into the VTM, then
you should shorten or delete them.
#Si,R, Sf, W, M
#-------------00, _, 01, _, L # when we reach a blank, we're at the end of the word
so step left
00, a, 00, a, R # if we encounter an `a', just step right
00, b, 00, a, R # if we encounter a `b', replace it and step right
01, a, 01, a, L # if we're in state 01 we're in the process of
returning to the beginning of the word
01, _, 99, _, R # when we reach the blank to the left of the word, just
step right
Try a few inputs of your own, including "____abab____", "____b____", and "______".
Next, construct your own tables of behaviour for the following Turing machines.
Use the VTM to test your tables. In all cases, assume that the tape head is at the
beginning of the input initially. Unless otherwise stated, it does not matter where the
TM tape head is when the computation finishes.
Machine 2.1 Construct a TM that changes all of the `a' symbols in its input word to `b',
and all of its 'b' symbols to `a'. Assume that the input word is an element of {a,b}*. For
example, an input "abaab" should be transformed into "babba".
Machine 2.2 Construct a TM that writes a `T' if the input word contains the substring
"aa". Assume that the input word is an element of {a,b}*. For example, inputs "abaaba"
and "aa" should cause the TM to write a `T', and input "ababa" should not cause it to
write a `T'.
Machine 2.3 Construct a TM to add 3 to a unary number. Assume the tape head is
positioned at the beginning of the input initially (for example, "11"). At the end of the
computation, the tape head should have positioned itself at the beginning of the output (in
this case, "11111").
Machine 2.4 Construct a TM that accepts the language L = {w : w {a,b}*, w ends with
the substring aab}. Your TM should halt in state 99 iff the input word is in L. If the input
word is not in L, it does not matter what your TM does, as long as it does not halt in state
99.
Machine 2.5 Modify the previous machine so that it decides the language. Your TM
must accept words in L and reject words not in L. Let 99 be your accepting state and let
50 be your rejecting state. Your Turing machine should always halt in one of these states
if it is given a word in {a,b}*. Furthermore, the tape head must be back at the start of the
word when your TM halts.
Download