6
$\begingroup$

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 ?

$\endgroup$
5
  • $\begingroup$ What if you keep a count or two? How many bits are in a count? How efficiently can increment/decrement a count? $\endgroup$
    – Jake
    Commented May 8, 2018 at 8:01
  • 1
    $\begingroup$ Keeping a count means writing the bits that represents the counter at the end of the tape and changing it for each a or b (running to the end of tape (=n steps) for each a or b (=n steps) to change the count). This would be O(n^2) steps. If i could keep a counter which operates in O(1) then the whole algorithm would run in O(n) since only 1 pass will be needed. The problem is the implementation of the counter $\endgroup$
    – Caffeine
    Commented May 8, 2018 at 8:25
  • $\begingroup$ Not if you represent the count in binary. $\endgroup$
    – Jake
    Commented May 8, 2018 at 8:25
  • $\begingroup$ But for each character you run to the end of the tape. So the runs will be 1 step, 2 steps,...., n steps. Sum(1 to n) is O(n^2). And that's even without the changes made to the bits of the counter $\endgroup$
    – Caffeine
    Commented May 8, 2018 at 8:27
  • $\begingroup$ Also, i have only 1 tape = counter will be at the end of the tape. And i have only 1 head which reads\writes $\endgroup$
    – Caffeine
    Commented May 8, 2018 at 8:28

1 Answer 1

5
$\begingroup$

The idea is to repeatedly apply the following algorithm:

  1. Determine the parity of the number of $a$'s, deleting every second $a$ on the way.
  2. Determine the parity of the number of $b$'s, deleting every second $b$ on the way.
  3. Accept if there were no $a$'s or $b$'s.
  4. Reject if the parities are different.
  5. Jump back to Step 1.

Each phase of the algorithm takes $O(n)$ steps, and there are $O(\log n)$ phases, for a total of $O(n\log n)$.

As an aside, using crossing sequences you can prove a matching lower bound of $\Omega(n\log n)$. Consider all inputs of the form $a^ib^{n-i} a^nb^n b^ja^{n-j}$. Such an input should be accepted iff $i = j$. For every input $x$, trace the execution of the Turing machine. When it crosses from the $n$th letter to the $(n+1)$th letter, record the state $\sigma$ of the Turing machine, and wait until the Turing machine crosses from the $2n$th letter to the $(2n+1)$th letter, at which time add $\sigma$ to the crossing sequence, and go back to waiting for it to cross from the $n$th letter to the $(n+1)$th letter. A cut-and-paste argument shows that the crossing sequence must depend on $i$, and so must be of length $\Omega(\log n)$ in the worst case, where the hidden constant depends on the number of states in the machine. Since it takes the machine $n$ steps to add a state to the crossing sequence, we obtain a lower bound of $\Omega(n \log n)$.

$\endgroup$
1
  • $\begingroup$ Wow ! So simple but very clever :) Thanks a lot :) $\endgroup$
    – Caffeine
    Commented May 8, 2018 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.