2

I need to build a TM with exactly 1 tape for the language L = {w| w is a word with same number of a's and b's in it, for example: abba, aababb}

The TM has to have ONLY 1 tape and it has to run in O(nlog(n)) time. I understand how to do it in O(n^2) but i have no idea how to make it nlog(n).

If i have the input w = "aaaa....abbbbb....bb" for example, where w = a ^ n/2 * b ^n/2 (which is the worst case) then i will go backwards each time (to delete each a for each b) and the steps taken will be of size 1,2,3,4.....n. Sum(1 to n) is O(n^2)...

Help ? any ideas ?

Caffeine
  • 113
  • 1
  • 1
  • 7

1 Answers1

3

At a high level, the solution I'm thinking of processes the word in order from left to right. At all times, there is a prefix of the word that has been processed, and a suffix of the word that has not been processed. Letting delta be the number of as in the prefix minus the number of bs, the tape looks like this at the beginning of each iteration:

<big-endian abs(delta)><sign(delta)><suffix>
                                    ^
                                tape head

To process the next letter (i.e., the first letter in the suffix), add/subtract one from delta depending on the whether the letter is an a or b and whether sign is + or -, moving the representation of delta one space to the right in the process. At the end, check whether delta is zero.

It might be easier to use a two's complement representation where the leftmost digit is presumed to repeat infinitely, but I'm not sure if there are any hidden snags.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120
  • This is a surprisingly simple solution! It's pretty much how you would write the code on a regular computer except for the part about moving the counter up through memory. – Paul Hankin May 08 '18 at 13:41