Sub topic 3
Designing machines: marking symbols for a^n b^n and a^n b^n c^n
This chapter assumes you know the 7-tuple definition of a deterministic Turing machine (DTM), how configurations work, and the difference between accepting, rejecting and looping. It also assumes you have seen why a^n b^n needs a stack in a pushdown automaton (PDA). Here you learn the standard design technique for simple languages, the marking strategy, and then apply it to a^n b^n c^n, which no PDA can accept.
The marking strategy
A Turing machine (TM) has no counter or stack. It has a tape it can both read and write. The standard way to count is to overwrite symbols you have already matched with marker symbols from the tape alphabet Γ, which are not in the input alphabet Σ.
A design in this style has four parts:
- A sweep pattern. Decide where the head goes in each round: right to find something, left to return.
- Markers. Use one marker per input symbol, such as X for a, Y for b and Z for c. Markers record that a symbol is already paired.
- A turnaround rule. Choose a marker the head can recognize on the way back, so it knows where to restart.
- A final check. When the first symbol has no more unmarked copies, verify that nothing unmarked remains.
Rejection is implicit. If the machine reads a symbol for which no transition exists, it halts and rejects. Writing only the useful transitions keeps the design short, and GATE questions usually assume this convention.
Designing a machine for a^n b^n
The language is { a^n b^n : n ≥ 0 }. Each round does the following:
- Mark the leftmost unmarked a as X.
- Move right past remaining a's and already-marked b's (Y).
- Mark the first unmarked b as Y.
- Move left back to the rightmost X and step right.
When the head returns and sees Y instead of a, no a's are left. The machine then checks that only Y's remain until the blank.
| State | Role |
|---|---|
| q0 | Start of a round. Looks for the next unmarked a. |
| q1 | Moving right to find an unmarked b. |
| q2 | Moving left to find the rightmost X. |
| q3 | All a's are matched. Checking that only Y's remain. |
| qa | Accept. |
Here is the simulator and the machine in Python. A missing key means reject. Hitting the step limit returns None, because a simulator cannot decide whether a machine will ever halt.
def run(delta, w, start='q0', accept='qa', blank='_', max_steps=10000):
tape = dict(enumerate(w))
head = 0
state = start
for _ in range(max_steps):
if state == accept:
return True
key = (state, tape.get(head, blank))
if key not in delta:
return False
state, sym, move = delta[key]
tape[head] = sym
head += 1 if move == 'R' else -1
return None # step limit reached: no verdict
# Accepts { a^n b^n : n >= 0 }
delta_ab = {
('q0', 'a'): ('q1', 'X', 'R'),
('q0', 'Y'): ('q3', 'Y', 'R'),
('q0', '_'): ('qa', '_', 'R'),
('q1', 'a'): ('q1', 'a', 'R'),
('q1', 'Y'): ('q1', 'Y', 'R'),
('q1', 'b'): ('q2', 'Y', 'L'),
('q2', 'a'): ('q2', 'a', 'L'),
('q2', 'Y'): ('q2', 'Y', 'L'),
('q2', 'X'): ('q0', 'X', 'R'),
('q3', 'Y'): ('q3', 'Y', 'R'),
('q3', '_'): ('qa', '_', 'R'),
}
for w in ['', 'ab', 'aabb', 'aab', 'abb', 'ba']:
print(repr(w), run(delta_ab, w))
# '' True, 'ab' True, 'aabb' True, 'aab' False, 'abb' False, 'ba' FalseTracing a failing input
Trace aab:
- q0 marks the first a, giving
XabB-style progress: the tape isX a b. q1 skips a, then marks b as Y. The tape isX a Y. - q2 moves left over a to X, and q0 steps right onto a.
- q0 marks that a as X, giving
X X Y. q1 moves right over Y and reads the blank. - There is no transition for (q1, blank), so the machine halts and rejects.
The extra a had no b to pair with. This is the typical rejection: the head runs off the end of the marked region while still looking for a partner.
Extending the idea to a^n b^n c^n
The language { a^n b^n c^n : n ≥ 0 } is not context-free. You can show that with the pumping lemma for CFLs. A TM handles it by adding a third marker and extending each round by one more pairing step. Each round now marks one a as X, one b as Y and one c as Z.
| Marker | Replaces | Meaning |
|---|---|---|
| X | a | This a is paired. |
| Y | b | This b is paired. |
| Z | c | This c is paired. |
The round has these steps:
- q0 marks the leftmost unmarked a as X and moves to q1.
- q1 skips a's and Y's, and marks the first b as Y.
- q2 skips b's and Z's, and marks the first c as Z. Then q4 moves left over everything until X, and q0 steps right.
- When q0 sees Y, all a's are matched. q3 and q5 then check that the rest is exactly Y's followed by Z's followed by a blank.
# Accepts { a^n b^n c^n : n >= 0 }
delta_abc = {
('q0', 'a'): ('q1', 'X', 'R'),
('q0', 'Y'): ('q3', 'Y', 'R'),
('q0', '_'): ('qa', '_', 'R'),
('q1', 'a'): ('q1', 'a', 'R'),
('q1', 'Y'): ('q1', 'Y', 'R'),
('q1', 'b'): ('q2', 'Y', 'R'),
('q2', 'b'): ('q2', 'b', 'R'),
('q2', 'Z'): ('q2', 'Z', 'R'),
('q2', 'c'): ('q4', 'Z', 'L'),
('q4', 'a'): ('q4', 'a', 'L'),
('q4', 'b'): ('q4', 'b', 'L'),
('q4', 'Y'): ('q4', 'Y', 'L'),
('q4', 'Z'): ('q4', 'Z', 'L'),
('q4', 'X'): ('q0', 'X', 'R'),
('q3', 'Y'): ('q3', 'Y', 'R'),
('q3', 'Z'): ('q5', 'Z', 'R'),
('q5', 'Z'): ('q5', 'Z', 'R'),
('q5', '_'): ('qa', '_', 'R'),
}
# run() is the function defined above
tests = ['', 'abc', 'aabbcc', 'aabbc', 'abcc', 'acb', 'cba', 'aabccc']
for w in tests:
print(repr(w), run(delta_abc, w))
# '' True, 'abc' True, 'aabbcc' True, 'aabbc' False,
# 'abcc' False, 'acb' False, 'cba' False, 'aabccc' FalseNotice how each rejection happens. In aabbc, the second round reaches the blank while q2 still looks for a c. In abcc, q5 meets an unmarked c after the Z's. In acb, q1 reads c and no transition exists.
Edge cases and common mistakes
- Forgetting the empty string. For n ≥ 0 you need the (q0, blank) transition. If the question says n ≥ 1, remove it.
- Skipping the final check. Without q3 and q5, an input like
abccwould be accepted once the a's run out. The leftover symbols must be verified. - Marker confusion on the way back. Moving left, the machine must pass over a, b, Y and Z and stop only at X. If you let it stop at any marker, the next round restarts in the wrong place.
- Left-end problems. On a tape that is infinite only to the right, never move left from cell 0. Here X always sits to the left, so q4 stops before the boundary.
- Cost. Each round scans the whole marked region, so a^n b^n takes O(n²) steps on a single-tape DTM. Counting every step matters when a question asks how many moves a trace uses.
Why this works
The language is not context-free because a PDA's single stack can compare two counts but loses the first count after using it. A TM rewrites its tape, so each marker keeps its pairing information in place. The same marking technique extends to a^n b^n c^n d^n and to languages like ww, where you mark and compare matching positions.
Exercise
Modify the a^n b^n machine so that it accepts { a^n b^(2n) : n ≥ 0 }. Which part of the round changes, and what does the machine do on abb and ab?
Show answer
Each round must mark one a and then two b's before returning. Add an extra state after q1 marks the first b as Y: it moves right over b's and Y's, marks the next unmarked b as Y, then goes left to the rightmost X. On abb the round marks X, Y, Y, then q0 sees Y and the final check finds only Y's, so the machine accepts. On ab the second b-marking step reaches the blank, no transition exists, and the machine rejects.