13

In Sipser's Introduction to the Theory of Computation, the author explains that two strings can be compared by “zigzagging” back and forth between them and “crossing off” one symbol at a time (i.e., replacing them with a symbol such as $x$). This process is displayed in the following figure (from Sipser):

Diagram that shows how a Turing machine can compare two strings

However, this process modifies the strings being compared, which would be problematic if the Turing machine needs to access these strings in the future. What are ways of performing a string comparison without modifying the strings?

Frank
  • 335
  • 2
  • 11

5 Answers5

29

Create two new types of marks: $\dot{0}, \dot 1$. Those two will act "like" $x$, but can still keep the information about the string.

So when you cross-off a letter, add a "dot" to it at the top instead of fully replacing it with $x$.

Then, if you want the original strings back, after you are done comparing, go through the entire strings and remove the "dots": replace $\dot 0$ with $0$ and $\dot 1$ with $1$.

nir shahar
  • 11,753
  • 3
  • 17
  • 35
24

One simple way is to create a copy of the entire input right after the original input, if your TM only has a single tape. To distinguish the original from the copy you can use ## as separator. So the first step is to scan the entire input to reach the end, add ##, go back to copy the entire input to the right side of ## and finally apply the comparison used in Sipser to the copy.

Russel
  • 2,788
  • 1
  • 9
  • 16
9

Here is an alternative solution using the original binary alphabet of $\{0,1\}$ (without adding extra letters, apart from $x$ which can also be replaced with $\sqcup$), that also manages to work without allocating extra memory from its tape:

We only keep one "$x$" per string, moving it one right for each comparison we did. That being said, to know what letter was there before we replaced it with $x$, we can utilize the fact that its possible to encode it within the turing machine's internal state.

nir shahar
  • 11,753
  • 3
  • 17
  • 35
3

In general, it can't be done.

E.g., if the strings are length-prefixed, and writing to the cells containing the strings is not allowed, then there is no way that a machine of $n$ internal states can traverse a string longer than $n$ from end to end, and so there is no single program that can compare strings of arbitrary length (or do much of anything with them).

You have to be able to "bring information with you" on the tape, since there isn't enough memory in the internal state space to store anything of interest. Unless the input is encoded in a way that gives you some working space – leaving alternate cells blank, for example – you have no alternative but to rewrite the tape to create space for yourself.

You can always modify the strings in a reversible way, and restore them to their original positions after you've worked out and recorded the answer. But Turing machines aren't normally expected to do that.

benrg
  • 2,511
  • 6
  • 13
2

You can get along without a copy and without more symbols than used in the strike-out solution - at the cost of increasing the number of states. That is, you only ever have one $x$ per string and (per additional states) keep in mind what was under them, thus allowing you to restore the info upon the next round.