# Construct Turing Machine which accepts the language $ww$

I try to construct a TM that accepts the language $\{ ww \mid w \in \{a,b\}^* \}$.

Between the words $w$ is no delimeter, so I don't know, how my TM can know where the first $w$ ends and the second $w$ begins.

Is there a trick for this?

• Hint: non-determinism. Or, you know, Turing machines can count.
– Raphael
Jun 16 '15 at 11:31
• How would you try to find the spot where one halve ends and the other one begins in a very long string? Jun 16 '15 at 12:52

Here is a sketch for how a deterministic machine might work:

1. place a marker in front of the input.
2. go through the input, checking if the length is even. If not, reject.
3. place a marker behind the input.
4. going back and forth, move the markers towards each other until they meet in the middle.
5. as long as the halves are nonempty, compare and erase their first letters and reject if not equal.
6. accept.
• How can you check if the length is even? The Turing machine only knows what it is looking at right then. It can't count how many spaces it passes Jun 19 '15 at 22:49
• It only needs to keep track of the parity of the length - this can be done using two states between it keeps alternating. Jun 20 '15 at 1:05

An important thing to recognize is that you are only concerned with deciding if your input $x$ can be written as $ww$. That is, you want if decide $\exists w (x=ww)$.

There are many ways you can go about this, and honestly, there is even a large variety of easy ways to go about it. If you just have to describe the Turing machine in words (like, you don't have to construct a detailed diagram), then pretty much any of them would be sufficient. If you have to draw a diagram, here are some things you might want to consider:

If you are allowed to use nondeterminism, as was commented, you should take advantage of this. You can nondeterministically guess where there half-way point of your input is, and then check to see if the first segment matches the second segment. Likewise, you can always just guess what $w$ is and then check to see that your input tape looks like $ww$.

If you can't use nondeterminism, you can always emulate it by trying all possible middle points or writing down all possible $w$'s, stopping at a provably reasonable time, of course. You can, as a previous answer mentions, count the length of your input $x$, then use that to compute the middle point.

Some other considerations that are typical when one is a assigned problems like these deals with how many tapes your machine has. If you Turing machine only has one tape, guessing $w$ might be foolish, and you would be much better off guessing or finding the middle.

I hope this is sufficient. The tricky thing about this kind of question is, either you don't have to fully describe the Turing machine, in which case, there are lots of nice and short answers, or you do have to fully describe the Turing machine, in which case, you will have to do a nice little bit of work to convert one of these short descriptions into a diagram.

• I have to cunstruct a full TM. It isn't important if I use nendetermism or determinism, but i preffer the determinate TMs. So I construct a TM with 15 states all together in that way Klaus Draeger descripted it in the accepted answer. But thanks for your hints! Jun 16 '15 at 15:55

One way I have read of is to first check the length of the string, and if it is odd, reject the string.

Then We could put some special character (e.g. c) at the end of the entire string and copy the entire string to the other side of the special character and then apply the algorithm for checking $$a^nb^n$$.

I also found another method explained as a pseudo code in this pdf, which changes the first half of the string to some other substitute variables and then compares them.

• I don't understand, how you suggestion should work for my problem, but your link to the PDF was a great help! Jun 16 '15 at 15:50
• Care to explain why copying the string and running the $0^n1^n$ algorithm would help? why does it reject inputs of the form $xy$ with $|x|=|y|$ but $x\ne y$? Jun 18 '15 at 0:06