Are they any helpful guidelines to describing what a Turing machine does if you already have the pseudo code for the algorithm?
I’m taking a course on complexity theory and it takes me a while to describe a Turing machine that decides or accepts some language (states, transitions, etc.) even though I know how I could code it in something like C or even assembly. I guess I just haven’t had enough practice with Turing machines (working on it), but I appreciate any suggestions.
edit
I don’t want to make a Turing Machine simulator, I want to describe a Turing Machine on paper (alphabet, states, transitions) for deciding some language.
Here’s a trivial example of what I mean, say I need to write a Turing Machine that goes over a string of 0s and 1s and changes all the 0s in it to 1s. For example, if you start with 11010 on the tape (input) it halts with 11111 on the tape (output). Now in a high level language you know it’s something like:
Go over every character on tape
If character is 0 change it to 1
The Turing machine description is informally something like:
You have two states, q and halt. When
you are on state q and you see a 1, go
to the right without changing it. If
you see a 0, change it to 1 and go to
the right. If you see the blank symbol
(end of tape) then go to the halt
state.
Formally you will have something like {q, halt} for states. {((q, 1) -> (q, 1, R)), ((q, 0) -> (q, 1, R)), ((q, #) -> (halt, 0, L))} for transitions.
Now this problem is trivial, but there are others which are more involving (add unary numbers or recognize a language with equal number of a’s, b’s, and c’s). I could easily write the pseudocode for them, but writing the Turing Machine is far more challenging (takes me a long time) and I was wondering if there were some tips, resources, or guidelines that help me become better at solving problems like that.
Disclaimer: Your question is very general, hence so is this answer. Note that I’m anything but an expert on TMs, and
this approach will usually not be very efficient (I can’t even promise it will always be effective).
I’m just jotting down some thoughts here.
I would suggest trying an approach like this: Take your pseudo-code and reduce it so that
it only consists of a) boolean variables and b)
if-statements.For example:
When you have nested
ifs, replace them withands; removeelseblocks by usingnot:becomes
Each
ifblock should then only contain oneMOVE_TO_PREVIOUSorMOVE_TO_NEXT, possibly aWRITEcommand and any numberof variable assignments.
Complete all
ifclauses such that every single one of your booleans AND the current letter is always checked, duplicatingthe blocks where neccessary. Example:
becomes
Now, if you have n booleans and m letters, you should have m * 2n
ifs.Just imagine you have stored the booleans in a bitfield, so each possible combination of booleans represents an
integer. Hence the above becomes
which then becomes
This integer value for the bitfield is the Turing machine state. The
do_apart is just an assignment to the booleans (i.e. the bitfield, so it’s your new state), a write command (if there’s none, just write the letter that was alreadythere) and a movement command, hence explicitly a Turing Machine transition.
I hope any of the above makes sense.