1. Gyanastra
  2. Theory of Computation
  3. Turing Machines
  4. Designing a Turing Machine for a^n b^n
Free lesson, no account needed 7 min read 1 practice questions after it
Asked in past papers1 time
  • UGC NET2016

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 L={anbn∣n≥1}L = \{ a^n b^n \mid n \ge 1 \}. The language is not regular but is context-free; the TM accepts it by marking and matching symbols, comparing the number of aa's with the number of bb's without counting.

Key concepts

  • Language: L={anbn∣n≥1}L = \{ a^n b^n \mid n \ge 1 \}.
  • Classification: LL 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 n≥1n \ge 1 rather than n≥0n \ge 0 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 aa).
  • TM capabilities: read a symbol, change (write) that symbol, move left, move right.
  • Marking scheme: every aa is changed to XX, every bb is changed to YY.
  • "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

  1. See an aa, mark it as XX (change aa to XX), then move right.
  2. When a bb is seen, mark it as YY (change bb to YY), then move left.
  3. Move left until the first XX is reached; this guarantees the head is standing just before the first unmarked aa.
  4. After reaching the first XX, turn right and change that aa to XX; go right crossing all aa's and all YY's, reach the next bb and change it to YY; go left crossing all YY's and all aa's until the first XX is hit; move right and mark the next aa as XX.
  5. Repeat: keep moving right until the first YY (on the way any number of aa's and any number of YY's are skipped), change the bb to YY, keep moving left until the first XX.
  6. Termination check: after the first XX, if there is no aa (only YY's), all aa's are over. Then go right and check whether all bb's are over. If all bb's are over, accept. This confirms all aa's are followed by all bb's.
Figure. Each a is changed to X and each b to Y, returning left to the first X after every match. When no unmarked a remains, scan right to confirm no unmarked b remains, then accept.

Formulas

  • L={anbn∣n≥1}L = \{ a^n b^n \mid n \ge 1 \} — the language accepted by the TM; nn is the number of aa's, equal to the number of bb's.

  • Marking rule: a→Xa \to X, b→Yb \to Y — each matched pair of one aa and one bb is marked.

  • TM marking cycle for ana^{n} bnb^{n}

  • The marking-and-matching loop that pairs each a with a b, ending in the acceptance check.

Worked examples

Example: a a b ba\,a\,b\,b (n=2n = 2), with blanks to the left and right of the input on the tape.

  • Mark first aa as XX, move right; mark first bb as YY, move left; go left to the first XX; move right and mark the next aa as XX; go right past YY's to the next bb and mark it as YY; go left to the first XX; no aa remains after the first XX, so all aa's are over; go right and check all bb's are over — they are, so the string is accepted.

Example: a a a b b ba\,a\,a\,b\,b\,b (n=3n = 3), blanks on both sides.

  1. Head on first aa: change a→Xa \to X, move right.
  2. Head on first bb: change b→Yb \to Y, move left.
  3. Move left until the first XX is reached (head now just before the first unmarked aa).
  4. Turn right, change that a→Xa \to X, move right.
  5. Cross all aa's and all YY's, reach the next bb, change b→Yb \to Y, move left.
  6. Cross all YY's and all aa's until the first XX, move right, change the next a→Xa \to X.
  7. Keep moving right until the first YY (skipping any number of aa's and YY's), change that b→Yb \to Y, move left until the first XX.
  8. After the first XX, if no aa remains (only YY's), all aa's are over; go right and verify all bb's are over; if yes, accept.

Comparison

Machine / propertyRelation to L={anbn∣n≥1}L = \{ a^n b^n \mid n \ge 1 \}
RegularCannot accept LL: LL is not regular
Context-freeAccepts LL: a context-free grammar exists and a pushdown automaton can be designed
Turing machineMore powerful; accepts LL by marking and matching symbols

Revision summary

  • L={anbn∣n≥1}L = \{ a^n b^n \mid n \ge 1 \} is not regular but is context-free; the TM accepts it by marking.
  • A TM can accept epsilon; n≥1n \ge 1 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 aa becomes XX, every bb becomes YY.
  • Movement pattern: after marking an aa as XX move right; after marking a bb as YY move left; always return left until the first XX, then move right to the next unmarked aa.
  • Acceptance check: after the first XX, if no aa remains, all aa's are over; then scan right to confirm all bb'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 {ana^{n} bnb^{n}} 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.
  • {ana^{n} bnb^{n} | n≥0} is context-free but not regular (pumping lemma for regular languages); {ana^{n} bnb^{n} cnc^{n}} 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 {ana^{n} bnb^{n}}.
  • 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.
Pages 1–2 of 2