3

I am looking to define a Turing machine which takes in a tape of $2n$ $1$'s surrounded by infinite $0$'s e.g., $...0000111111110000...$ and has an end configuration of half of those $1$'s e.g., $...0001111000...$.

Here are my thoughts: I am thinking that my Turing machine could "count" the number of $1$s by replacing every second $1$ with a sentinel value and then keeping a tally of the parity to the right of all of the $1$'s. Then we could go back to the beginning of the (now cleared) tape and write that many $1$'s. Is this a correct approach? Any tips for implementation?

Raton
  • 729

3 Answers3

4

Your idea sounds good, have you tried implementing it? I'd recommend you give that a try before you read on, it's like a fun little programming exercise that helps improve your understanding of Turing machines, no matter if you get it right or wrong the first try. This site allows you to write a TM and check whether it does what you expect it to do.

Now, here's my solution.

Think of the task divided into three steps:

  1. The first two states $q_0$ and $q_1$ are responsible for writing over the $\texttt{1}$'s from left to right using the letters $\texttt{a}$ and $\texttt{b}$ in alternating order until you find a $\texttt{0}$. If the last letter you wrote was a $\texttt{b}$, you can move on to state $q_2$. The tape now looks like this: $\texttt{...00}\ \underbrace{\texttt{abab...abab}}_{2n}\ \texttt{00...}$.
  2. States $q_2$ and $q_3$ are responsible for printing a new $\texttt{1}$ at the end of the string for each $\texttt{b}$ you find. While in $q_2$, travel left past $\texttt{a}$'s and $\texttt{1}$'s until you find a $\texttt{b}$, replace it with an $\texttt{a}$ and switch to $q_3$. While in $q_3$, travel right past all $\texttt{a}$'s and $\texttt{1}$'s until you find a $\texttt{0}$, then put a new $\texttt{1}$ there instead. Repeat. Once you run out of $\texttt{b}$'s, you will hit a $\texttt{0}$ as you're travelling left in $q_2$. At that point, switch to $q_4$. The tape now looks like this: $\texttt{...00}\ \underbrace{\texttt{aaaa...aaaa}}_{2n}\ \ \underbrace{\texttt{11...11}}_{n}\ \texttt{00...}$.
  3. Travel back to the right, replacing each $\texttt{a}$ with a $\texttt{0}$ as a go along. Once you hit the first $\texttt{1}$, switch to the accepting state $q_5$, and you're done.

Note: This solution doesn't work if $n=0$, but that can be fixed quite easily. Do you see how?

2

Your idea and the previously given answer are similar in that they convert the string of $\texttt{1}$s into an alternating sequence.

Having just learned (as a result of this question) about that site that simulates Turing machines, I couldn't resist having a go at it myself. Here's my solution.

The basic idea in this case is that we alternately change the first $\texttt{1}$ to an $\texttt{a}$ and the last $\texttt{1}$ to a $\texttt{b}.$ When no $\texttt{1}$ remains to be changed, we have $\ldots\texttt{00aaa}\ldots\texttt{aabbb}\ldots\texttt{bb00}\ldots,$ with $n$ copies of $\texttt{a}$ and $n$ copies of $\texttt{b}.$ We then convert every $\texttt{b}$ to $\texttt{0}$ and every $\texttt{a}$ to $\texttt{1}$ and transition to the "end" state, which I consider to be the only accepting state.

This machine handles the case $n=0.$ When the input string contains an odd number of $\texttt{1}$s the machine stops in a non-accepting state.


And here's another machine. This one uses only the symbols $\texttt{0}$ and $\texttt{1}.$ Unlike my first machine, this one does not attempt to write the output on the exact same cells as the first half of the input. Instead, it deletes the input two symbols at a time, always on the left end, and writes the output one symbol at a time on the section of the tape to the right of the input. This repeats until there are no more input symbols to delete, then the machine goes to the "end" state. The case $n=0$ goes immediately to the "end" state, but if the length of the input is odd the attempt to delete the second $\texttt{1}$ eventually fails and the machine halts without reaching "end".

David K
  • 98,388
2

Like @DavidK, I couldn't resist either.

Here is my solution

This one works like David's machine, but I replace the $1$'s at the right end immediately with a $0$

Also, here is a machine that does it without any special symbols. It basically skips the first two $1$s and, as long as there are more $1$s, keeps deleting one $1$ for every two $1$s ... and deletes one more $1$ when done.

Another machine that sticks to $0$1's and $1$'s ... one state less than the previous machine.

Bram28
  • 100,612
  • 6
  • 70
  • 118