Theory of Computation · Turing Machines
Designing a Turing Machine for a^n b^n
Overview
A Turing machine (TM) is designed to accept the language . The language is not regular but is context-free; the TM accepts it by marking and matching symbols, comparing the number of 's with the number of 's without counting.
Key concepts
- Language: .
- Classification: is not regular, but it is context-free (a context-free grammar exists for it, and a pushdown automaton can be designed for it).
The TM is a more powerful machine that accepts it by marking and matching symbols. - Epsilon: the TM can accept epsilon (for example, by accepting immediately when the first symbol is blank), but the language is taken with rather than as a design choice.
Including or excluding epsilon makes no real difference to the design. - Tape setup: the input string is written on the tape; all cells to the left of the input are blanks and all cells to the right of the input are blanks. The TM head points at some symbol of the input (e.g. an ).
- TM capabilities: read a symbol, change (write) that symbol, move left, move right.
- Marking scheme: every is changed to , every is changed to .
- "Man who cannot count" analogy: two rooms, one full of bags of cement and one full of bags of sand; the man must check whether the number of cement bags equals the number of sand bags without counting.
- Crude method: pull out one bag of sand and one bag of cement, pair them up, repeat — this requires physically moving every bag.
- Better method: take a stone/chalk, mark one bag in the first room, go to the other room and mark one bag there, come back and mark one more here, and so on — one mark here, one mark there.
This is exactly comparing without counting.
How it works
- See an , mark it as (change to ), then move right.
- When a is seen, mark it as (change to ), then move left.
- Move left until the first is reached; this guarantees the head is standing just before the first unmarked .
- After reaching the first , turn right and change that to ; go right crossing all 's and all 's, reach the next and change it to ; go left crossing all 's and all 's until the first is hit; move right and mark the next as .
- Repeat: keep moving right until the first (on the way any number of 's and any number of 's are skipped), change the to , keep moving left until the first .
- Termination check: after the first , if there is no (only 's), all 's are over. Then go right and check whether all 's are over. If all 's are over, accept. This confirms all 's are followed by all 's.
Drawing the diagram…
Formulas
-
— the language accepted by the TM; is the number of 's, equal to the number of 's.
-
Marking rule: , — each matched pair of one and one is marked.
-
TM marking cycle for
Drawing the diagram…
- The marking-and-matching loop that pairs each a with a b, ending in the acceptance check.
Worked examples
Example: (), with blanks to the left and right of the input on the tape.
- Mark first as , move right; mark first as , move left; go left to the first ; move right and mark the next as ; go right past 's to the next and mark it as ; go left to the first ; no remains after the first , so all 's are over; go right and check all 's are over — they are, so the string is accepted.
Example: (), blanks on both sides.
- Head on first : change , move right.
- Head on first : change , move left.
- Move left until the first is reached (head now just before the first unmarked ).
- Turn right, change that , move right.
- Cross all 's and all 's, reach the next , change , move left.
- Cross all 's and all 's until the first , move right, change the next .
- Keep moving right until the first (skipping any number of 's and 's), change that , move left until the first .
- After the first , if no remains (only 's), all 's are over; go right and verify all 's are over; if yes, accept.
Comparison
| Machine / property | Relation to |
|---|---|
| Regular | Cannot accept : is not regular |
| Context-free | Accepts : a context-free grammar exists and a pushdown automaton can be designed |
| Turing machine | More powerful; accepts by marking and matching symbols |
Revision summary
- is not regular but is context-free; the TM accepts it by marking.
- A TM can accept epsilon; is used only as a design choice, and it makes little difference to the design.
- TM operations used: read a symbol, write/change a symbol, move left, move right.
- Marking scheme: every becomes , every becomes .
- Movement pattern: after marking an as move right; after marking a as move left; always return left until the first , then move right to the next unmarked .
- Acceptance check: after the first , if no remains, all 's are over; then scan right to confirm all 's are over; if so, accept.
- The "man who cannot count" analogy: compare by marking one item on each side instead of counting.
Previous-year exam insights
- A standard TM for { } must also reject strings with a b before an a, with unequal counts, and with symbols other than a,b; the acceptance check must verify that no unmarked a remains AND no unmarked b remains (scan the whole tape), otherwise strings like aab or abb would be wrongly accepted.
- { | n≥0} is context-free but not regular (pumping lemma for regular languages); { } is not context-free (pumping lemma for CFLs) but is accepted by a TM, so it is context-sensitive/recursive.
- A TM's transition is delta(state, tape symbol) = (new state, symbol written, L/R);
the machine halts and accepts on entering the accept state and halts and rejects on entering the reject state or when no transition is defined. - A nondeterministic TM and a deterministic TM accept exactly the same class of languages (recursively enumerable), so a deterministic design suffices for { }.
- A single-tape TM can simulate a multi-tape TM with only quadratic slowdown, and a TM can simulate a PDA/CFG; hence every context-free language is accepted by a TM.
- For the marking design, the tape alphabet must include the input symbols plus the markers X, Y and the blank symbol, and the blank must be distinguishable from input symbols so the ends of the tape can be detected.