Develop Turing Machine to count number of As and Bs in a String - turing-machines

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

Related

Explaining Computation of a Turing Machine

I am having some trouble interpreting what this Turing machine actually does (i.e., I am uncertain how to explain it in plain English).
I believe I have created the state diagram correctly using the transition table I was given (although not 100% on this either).
From what I can see this TM will halt in an accepting state (q2) whenever the input is of the form
(a || b || B)*Ba*c(a || b || c || B)*,
that is any amount of a's, b's, and blanks (but no c's), followed by at least one blank, any amount of a's, and exactly one c. Anything can come after since we go left upon finding first c.
I suppose my question is
a) Is my work up to this point correct? and
b) Is there a more meaningful explanation of this Turing machine (i.e. a richer description than I wrote of the input that halts in (q2)).
Some observations:
q0 reads left to right, doesn't change the tape, and stops when it hits c.
q1 reads left to right, swaps a and b, halts when it sees a B and turns around when it hits an a.
The only way to for the TM to halt is if
There is a c somewhere on the tape to the right of the initial tape position
In q1, the last pass from right to left sees only b and leaves only a between the first c and the rightmost B to the left of c.
q1 changes everything between the first c and the rightmost b to the left of that c to a b eventually
Given initial tape configuration >BxBycz, the machine will always halt in configuration >BxB(a^|y|)cz. It accepts any string that contains c.
Your state diagram disagrees with the table in that the table has the transition function defined so that f(q1, a) = (q0, b, L) and f(q1, b) = (q1, a, L), but your diagram shows f(q1, a) = (q1, a, L) and f(q1, b) = (q0, b, L).

Is WW where W belongs to {a,b}* a context free language?

Is WW where W belongs to {a,b}* a context free language?
If yes, please provide the PDA for it.
No, it is not
Assume, for sake of contradiction, that it is, then there is a PDA that accept it.
According to the pumping lemma (for CFGs), there is a length p such that for every word (we will pick one shortly) s there are some substring u,v,w,x,y such that s=uvwxy and:
|vwx|<=p
|vx|>=1
uv^n wx^n y is in the language for any positive n
Let's consider the word a^p b^p a^p b^p, and such u,v,w,x,y
Either vwx contains the middle of the word, or it's entirely contained in the first half, or it is entirely contained in the second half.
If it's in the first half, then in the word uv^2 wx^2 y. We have added a total length of no more than p, thus we have "moved" the mid-point by no more than p/2, so right now the mid-point continues with b, but the word starts with a a, so it's not of the form ww
Same argument goes for it being in the second half.
Now let's assume it contains the middle, and consider uwy (using n=0). Since |vwx|<=p, then we have removed from the a's and b's in the middle, but not from the a's and b's at the edges. We have also removed a positive amount of letters, so uwy is of the form a^p b^k a^m b^p were either k<p or m<p. Eitherway, it's not of the form of ww

PushDown Automaton (PDA) for L={a^(n)b^(n)c^(n)|n>=1}

I am on a fool's errand trying to construct a Pushdown automaton for the non-context-free language L={a^(n)b^(n)c^(n)|n>=1} and thought of two approaches.
First approach:-
I thought that for every 'a' in string I will push 3 'a' into the stack and for every 'b' in the string, I will pop 2 'a' from the stack now for every 'c' in the string I will still have 1 'a' in the stack.
Problem with the First approach:- the language generated becomes something like this L={a^(p)b^(m)c^(n)| p>=1 and could not determine how m and n can be defined}
Second approach:-
We know that L={ a^(n)b^(m)c^(m)d^(n) | n>=0 } is a context-free language and L={ wxw | w∈(a,b)* } is also context-free language.
So, I thought L={ a^(n)b^(m)b^(m)c^(n) | n>=1 and m=floor((n+1)/2) }
Problem with the Second approach:- don't know if we can calculate floor(n+1/2) in the PDA without disturbing the elements of the stack.
Please help in determining how m and n can be defined in the first approach and how can I find floor((n+1)/2) in the PDA.
JFLAP files available for both if needed.
As Ami Tavory points out, there is no PDA for this language because this language is not context-free. It is easy to recognize this language if you use a queue instead of a stack, use two stacks, or use a Turing machine (all equivalent).
Queue machine:
Enqueue as as long as you see as, until you see a b.
Dequeue as and enqueue bs as long as you see bs, until you see a c
Dequeue bs as long as you see cs.
Accept if you end this process with no additional input and an empty queue.
Two-stack PDA:
Use the first stack to make sure a^n b^n by pushing a when you see an a and popping a when you see a b;
Use the second stack to make sure b^n c^n by pushing b when you see a b and popping b when you see a c;
Accept if both stacks are empty at the end of this process.
Turing machine:
Ensure a^n ... c^n by replacing each a with A and erasing a matching c;
Ensure A^n b^n by erasing matching pairs of A and b;
Accept if at the end of this process you have no more A and no more b, i.e., the tape has been completely cleared.
One reason you've not managed to construct a pushdown automaton for this language, is because there isn't any. The Bar Hillel pumping lemma shows this.
To outline the proof, suppose it can be done. Then, for some p, each string larger than p can be partitioned to uvwxy, s.t.,
|vwx| < p
|vx| > 1
uvnwxny is also accepted by the automaton, for any n.
The first rule implies that vwx can't span the three regions, only at most two (for large enough strings). The second and third rules now imply that you can pump so that the un-spanned region is smaller than the at least one of the other regions.

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,..)
How about you? Please help me find the answer (this is not a
homework)
Thankyou for reading.
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!

Resources