## Turing machine for palindrome - turing-machines

### Turing Machine with non trivial states and transitions

```Please give me some idea as to how to go about this
Draw a Turing machine (using Sipser notation) having at least 4 nontrivial (i.e., nonrejecting) states and at least six nontrivial (i.e., not to the rejecting state) transitions.
```
```A Turing machine has:
A finite number of states, of which one is accepting and one is rejecting. The task apparently requires five states (four nonrejecting (of which one must be accepting) and one rejecting). The states are usually drawn as circles, with a label (the state name) inside each. One of the states is the starting state; it is marked with an arrow pointing at it.
A finite input alphabet. {0, 1} or {a, b} are typical choices.
A finite tape alphabet, which includes a special blank symbol, all symbols from the input alphabet, and possibly more symbols (but this is not required).
A transition function which assigns a state, a tape symbol and a direction to each combination of a state and a tape symbol. The direction can be L (left) or R (right). A transition is drawn as an arrow from one state to another (or possibly a circular arrow from a state back to itself), and the arrow is labelled with two tape symbols and either L or R. Apparently, you need six such arrows.
The machine also has an infinite tape that is divided into cells. In each cell, there can be a symbol from the tape alphabet. The symbols that initially are on the tape are called the input to the machine. The machine has a read head that is always located over one of the cells. Let's say you have a transition arrow from state A to state B, with the symbols a, b, and R on it. That means: "If the machine is in state A and the symbol under the tape head is a, then we should replace that symbol with b, go to state B, and move the read head one cell to the right."```

### How to design a Turing machine which checks whether a number is prime or not?

```I could only make out that the logic must have included the logic of multiplication and division in Turing machines . But actually I cannot make out the exact solution.
```
```Well, if just any old TM will do, this one is easy to understand.
input tape is #(n)#
use a TM to write this to the tape: #(n):(2):(n)#
use a TM to subtract whatever is in between the : from the thing after both : over and over until either (1) you have too few symbols to remove (not divisible) or (2) there are exactly zero symbols left (evenly divisible). In case of (1) continue, otherwise - in case of (2) - halt-reject as not prime.
use a TM to write this to the tape: #(n):(m+1):(n)#
use a TM to check wither the thing before the first : is greater than the thing in between the two :. If so, continue with step 3. Otherwise, halt-accept (since the original input cannot be divisible by a number greater than itself)
Take all the TMs described and construct a single TM that incorporates all that behavior in various states. This is a TM for the language of prime numbers.
Assuming a unary encoding (that is, the natural number n is represented by the string 11...1 where 1 is repeated n times), here's some more pointers on making the individual TMs:
original input
go to the first blank after the input and write a :. Then, go to the right and write 1. Then, go to the right and write another 1. Then go to the right and write another :. Then, bounce back and forth from the beginning to the first blank at the end, copying the input natural number's unary digits. To do this, update the input digits to some other symbol, like A, so you remember ones you already copied. Stop this process when there is an A immediately to the left of the first :. Then, set all the A back to 1.
bouncing back and forth, remove a 1 from the rightmost section for each 1 in the innermost section, updating the innermost section to use A to mark them. Once everything in the innermost section is marked, set them back to 1, and repeat the process until you run out of 1s in the rightmost section .
erase the second : and everything after it; then write a 1 at the end, then a :, and do what the TM described in 2 does.
bounce back and forth between the leftmost and innermost sections, marking in each one as you go. If you run out of symbols in the innermost section first, the number is less than the input number; if you run out at the same time, the numbers are the same, meaning that the input number is first divisible by itself, so it's prime.```

### Develop Turing Machine to count number of As and Bs in a String

```The given Language is
L = {a^n b^n where n>= 0}
I have to develop a turing machine for it but i dont get the logic. will it go like this if an a is not followed by a b you go to halt reject state, but how we check if the count of a and b are equal?
will be very grateful for any response
```
```A Turing Machine decides a language if it enters the halt-accept state when the input tape initially contains a string in the language, and enters the halt-reject state otherwise.
So, to decide your language L, you need to define states and transitions so that when the tape contains a string a^n b^n the TM ends up in halt-accept, and such that it ends up in halt-reject otherwise.
We can assume a single-tape TM and that you don't need the input tape restored to its original state after processing has finished (that is, we can do whatever we want to the tape and don't need to clean up or move the tape head anywhere in particular). What we need is a strategy for processing the tape so that we can reliably know when the string is or isn't in the language. In your case, we need a's followed by b's, with the same number of a's and b's. If you had to do this task in real life, how might you approach it?
You could first count the a's and then count the b's, and then see if the numbers are equal; however, this doesn't really simplify matters for us, since telling whether two numbers are equal is actually a somewhat harder problem than telling if two strings have the same length (the language ww | w in {a, b}* is context-sensitive, whereas a^n b^n is context-free).
Another approach is the "one for me, one for you" approach used by pirates when dividing treasure and mathematicians when establishing bijections among sets: to show that two sets have the same number of elements, you simply pair them off such that all elements are in exactly one pair. This doesn't yield the same problem that counting did, in that we don't have the same subproblem to solve: whereas the other problem yielded a harder subproblem, this one yields a subproblem of exactly the same difficulty as the original. Indeed, it's a smaller instance of the exact same problem! By erasing one a and then erasing a corresponding b, we can reduce a problem of size n to one of size n - 2. By repeating this process, we will eventually erase the whole tape and accept, or run out of symbols to pair with, and reject.
This is our design. For implementation, we will require an initial state. From the initial state, we can assume we are looking at the first non-blank tape cell, if any. If the tape cell is blank, the input is empty and we must accept since a^0 b^0 is in the language with n = 0. If the tape cell has b, we may halt-reject since we were expecting to see an a, if anything, first. If the tape cell has a, then we need to look for a corresponding b to erase.
Now, which b should we erase? Currently, we know where the input ends since we have a blank immediately after the last input symbol. If we use blanks to erase b's before that, we will have no good way of knowing whether we might have more input to the right of any currently blank cell. You could overcome this by introducing a new tape symbol E for erased cells; then, when erasing, write E instead of blank to indicate erasure. Then, it really doesn't matter which b you erase. If you prefer not to use a new tape symbol, however, you can instead erase the b immediately before the blank at the end of the input (rejecting if an a is found there instead) and achieve the same thing.
In any event, after erasing a corresponding b, we can return to the beginning of the tape (the blank cell first reached after moving left) and repeat the process over and over again until we accept or reject.
Here is a sample implementation:
Q T Q' T' D
-- - -- -- -
// accept on empty tape
q0 B hA B S
// erase an a or reject if no a
q0 a q1 B R
q0 b hR b S
// scan to end of input
q1 B q2 B L
q1 a q1 a R
q1 b q1 b R
// erase last b or reject if none
q2 B qR B S
q2 a qR a S
q2 b q3 B S
// scan to beginning of input, then repeat
q3 B q0 B R
q3 a q3 a L
q3 b q3 b L```

### Letter substitutions termination

```Given:
A char string S length l containing only characters from 'a' to 'z'
A set of ordered substitution rules R (in the form X->Y) where x, y are single letters from 'a' to 'z' (eg, 'a' -> ' e' could be a valid rule but 'ce'->'abc' would never be a valid rule)
When a rule r in R is applied on S, all letters of S which are equal to the left side of the rule r would be replaced by the letter in the right side of r, if the rule r cause any replacement in S, r is called triggered rule.
Flowchart (Algorithm) :
(1) Alternately apply all rules in R (following the order of rules in R) on S.
(2) While (there exists any 'triggered rule' DURING (1) ) : repeat (1)
(3) Terminate
The question is: Is there any way to determine if with a given string S and set R, the algorithm would terminate or not (running forever)
Example1 : (manually executed)
S = 'abcdef' R = { 'a'->'b' , 'b' -> 'c' }
(the order is implied the order of appearance from left to right of each rule)
Ater running algorithm on S and R:
(1.1): 'abcdef' --> 'bbcdef' --> 'cccdef'
(2.1): repeat (1) because there are 2 replacements during the (1.1)
(1.2): 'cccdef'
(2.2): continue to (3) because there is no replacement during the (1.2)
(3) : terminate the algorithm
=> The algorithm terminate with the given S and R
Example2:
S = 'abcdef' R = { 'a'->'b' , 'b' -> 'a' }
(the order is implied the appearance order from left to right of each rule)
Ater running algorithm on S and R:
(1.1): 'abcdef' --> 'bbcdef' --> 'abcdef'
(2.1): repeat (1) because there are 2 replacements during the (1.1)
(1.2): 'abcdef --> 'bbcdef' --> 'abcdef'
(2.2): repeat (1) because there are 2 replacements during the (1.2)
(1.3): ...... that would be alike (1.1) forever....
The step (3) (terminate) is never reached.
=> The algorithm won't terminate with the given S and R.
I worked on this and found no efficient algorithm for the question
"if the algorithm halts".
First idea came to my mind was to "find cycle" of letters which
are in triggered rules but the number of rules may be too large
for this idea to be ideal.
The second one is to propose a "threshold" for the time of the
repeat, if the threshold is exceeded, we conclude the algorithm
would not terninate.
The "threshold" could be choosen randomly, (as long as it big
enough) - this approach is not really compelling.
I am thinking that if there is any upper bound for the
"threshold" which ensures that we always get the right answer.
And I came up with threshold = 26 where 26 is the number of
letter from 'a' to 'z' - but I can't prove that it true (or not).
(I hope that It would be something like Bellman-Ford algorithm which determines negative cycle in a fixed number of step,..)
homework)
```
```One simple way to think about solving this is to consider a string of length 1 and see if the problem can loop for any given starting letter. Since the string's length is never changing, and applying a rule applies to each character in S independently, it suffices to consider just a string of length 1.
Now, start with a state diagram with 26 states - 1 for each letter of the alphabet. Now, for your state transitions, consider this process:
Apply the transitions from R 1 at a time in order, until you reach the end of R. If from a particular state (letter), you do not ever reach a new letter, you know that if you reach the starting letter, you terminate. Otherwise, after applying the entire sequence of R, you will end up with a new letter. This will be your new state.
Note that all state transitions are deterministic because we apply the entire sequence of R, not just the individual transitions. If we applied the individual transitions, we might get confused, because we might have a -> b, b->a, a->c. When looking at the individual operations, we might think there are two possible transitions from a (either to b or to c), but really, considering the entire sequence, we see definitively that a transitions to c.
You will be done creating your state diagram after considering the next states of each starting letter. Creating the entire state diagram in this manner requires 26 * |R| operations. If the state diagram contains a loop, then if the string S contains any of the letters in the loop, then it fails to halt, otherwise it will halt.
Alternatively, if you just consider halting after 26 iterations through the entire sequence from R, you can use that as well.```

### Construct a Turing-Machine to decide ww^Rw

```w^R is the reverse of w and w is {0, 1}* . So the TM needs to decide a word followed by the reverse of this word followed by the word.
I don't want the answer, I just want a lead to start and to get on the right track.
```
```Since some time has passed and the answer probably isn't needed anymore, I guess I'll propose a solution for the benefit of future students looking for an example of how one language can be recognized by a Turing machine.
Here's the idea. We'll take as the tape alphabet {0, 1, a, b, c, d} and make a single-tape singly-infinite tape Turing machine that recognizes w w^R w. The machine will work in five phases:
Replace 0s and 1s in the prefix w w^R with a's and b's.
See whether w w^R is a palindrome.
Restore the tape to its pristine state.
Replace 0s and 1s in the suffix w^R w with c's and d's.
See whether w^R is a palindrome.
Note that this is simply one easy (for me to understand, that is) way to show that there exists a Turing machine to recognize this language. Naturally, showing that there exists an algorithm to solve this in any Turing-equivalent system of computation is just as good (it proves there exists a TM)... still, this outlines the construction of one such TM. Also note that there may be a simpler, more efficient or more intuitive TM to solve this problem... again, this is only one approach.
Step 1 will work as follows:
Precondition: The tape begins with a blank, contains any string in (0+1)*, and is followed by an infinite string of blank squares.
Postcondition: Halts if tape is empty or if length is not a multiple of 3; else, the tape begins with a blank, is followed by (a+b)^2n (c+d)^n, followed by an infinite string of blanks.
Move to the right.
If empty, halt accept. Otherwise, Scan to the right until you find an empty tape square, then move left.
Change the tape to c if 0 or d if 1.
Scan left until you find an empty tape square. Move right.
If the tape is 0 or 1, change to a or b then move right. If the tape is c or d, halt reject.
If the tape is 0 or 1, change to a or b then move right. If the tape is c or d, halt reject.
If tape is c or d, scan to the beginning of the tape and go to Step 2. Otherwise, scan right until c or d, then move left.
Change the tape to c if 0 or d if 1.
Scan left until you find either a or b. Move right.
Repeat starting at 4.
Step 2 will work as follows:
Precondition: The tape begins with a blank, is followed by (a+b)^2n (c+d)^n, followed by an infinite string of blanks.
Postcondition: Halts if the prefix (a+b)^2n isn't a palindrome; otherwise, leaves the tape in a state like D (c+d)^3n D*
Move right.
If tape is a (or b), move right. If tape is c or d, go to the beginning of the tape, then go to Step 3.
If the tape is c, d or blank, halt reject. Otherwise, scan right until you find a c, d or blank. Move left.
If the tape is a b (or a), halt reject. Otherwise, change this to a c (or d) and scan back to the left until you see a blank, a c or a d. Move right. Change a (or b) to c (or d). Move right.
Repeat starting at step 2.
Step 3 will work as follows
Precondition: Tape is D (c+d)^3n D*
Postcondition: Tape is D (0+1)^3n D*
Move right.
If tape is c, write 0 and move right. If tape is d, write 1 and move right. If tape is blank, move to first blank space at the end of tape and go to step 4.
Repeat step 2.
Step 4 and 5 work just like steps 1 and 2, except you work backwards (the tape now looks like D (c+d)^n (a+b)^2n D*, and you must check to see whether the (a+b)^2n part is a palindrome.
Any string passing both these tests must be of the form w w^R w where w is in (0+1)*.
```
```As a hint, note that wwRw must have length 3n for some n (since each character appears exactly three times). You might therefore build a Turing machine that works by somehow counting the length of the string, using this to determine where the boundaries of the three strings are, and then checking that the three pieces all have the appropriate composition. If you can't count up a multiple of 3 characters, you could immediately reject.
Depending on what sort of TM is allowed, this might be easiest with a multitrack or multitape Turing machine so that you can mark up the letters with some extra information.
Hope this helps!```