Sub topic 2
Running a machine: configurations, halting and looping
This chapter assumes you know the 7-tuple definition of a deterministic Turing machine (TM) from the previous chapter: states Q, input alphabet Σ, tape alphabet Γ, blank symbol, transition function δ, start state q0, accept state qaccept and reject state qreject. It also assumes you are comfortable with proof by contradiction. Here you learn to describe a computation precisely, to classify its outcome, and to avoid the standard GATE mistakes about "halts", "rejects" and "loops".
Configurations: a snapshot of the machine
A TM has three pieces of changing information: the current state, the tape contents and the head position. A configuration (also called an instantaneous description) records all three in one string:
u q v- u is the tape content to the left of the head.
- q is the current state.
- v is the tape content from the head position to the right, and the head scans the first symbol of v.
Everything beyond the written part of the tape is blank. If v is empty, the head is on a blank.
The start configuration on input w is q0 w. The head is on the first symbol of w.
The yields relation
One step of the machine turns a configuration C into a configuration C'. We write C ⊢ C' ("C yields C'"). Suppose the head scans a, and δ(q, a) = (p, b, D).
- If D = R, then
u q a v ⊢ u b p v. The machine writes b, moves right, and enters p. - If D = L, then
u c q a v ⊢ u p c b v. The machine writes b, moves left, and enters p. The symbol c is the one the head moves onto.
Write ⊢* for "yields in zero or more steps". A computation is a sequence C0 ⊢ C1 ⊢ C2 ⊢ ... starting from q0 w.
Because the machine is deterministic, each configuration has at most one successor. The whole computation is therefore a single path, not a tree.
A configuration with no successor is a halting configuration. This happens in two cases:
- The state is qaccept or qreject. The machine stops by definition, and no transition is defined out of these states.
- The state is another state and δ is undefined for the scanned symbol. Many textbooks and the simulator below treat this as an implicit reject.
Three outcomes
On every input w, a deterministic TM does exactly one of three things.
| Outcome | What happens | Is w in L(M)? |
|---|---|---|
| Accepts | Reaches a configuration with q_accept | Yes |
| Rejects | Reaches q_reject, or gets stuck with no move | No |
| Loops | Never reaches a halting configuration | No |
L(M) is the set of strings on which M accepts. Looping and rejecting both keep w out of L(M). They differ in whether the machine ever gives an answer.
The vocabulary follows from this table:
- M recognizes L if L(M) = L. On strings outside L it may reject or loop.
- M decides L if L(M) = L and M halts on every input. Such a machine is called a decider or total TM.
The next chapter develops this distinction into recursive and recursively enumerable languages. For now, remember that "M does not accept w" covers two different cases, and only one of them gives an answer.
Tracing a run
The simulator below is a variant of the machine that decides { aⁿbⁿ : n ≥ 0 }. It records each configuration as u [q] v, where the bracketed state sits just before the scanned symbol. It treats a missing transition as reject and stops after max_steps with no verdict.
def trace(delta, w, start='q0', accept='qa', blank='_', max_steps=100):
tape = dict(enumerate(w))
head = 0
state = start
history = []
for _ in range(max_steps + 1):
lo = min(min(tape, default=0), head)
hi = max(max(tape, default=0), head)
cells = [tape.get(i, blank) for i in range(lo, hi + 1)]
k = head - lo
history.append(''.join(cells[:k]) + ' [' + state + '] ' + ''.join(cells[k:]))
if state == accept:
return 'accept', history
key = (state, tape.get(head, blank))
if key not in delta:
return 'reject', history
state, sym, move = delta[key]
tape[head] = sym
head += 1 if move == 'R' else -1
return 'no verdict', history
# Decides { a^n b^n : n >= 0 }
delta = {
('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'),
}
verdict, history = trace(delta, 'ab')
print(verdict)
for config in history:
print(config)Running it prints accept followed by these six configurations:
[q0] ab
X [q1] b
[q2] XY
X [q0] Y
XY [q3] _
XY_ [qa] _Each line yields the next:
- q0 reads
a, writesXand moves right. - q1 reads
b, writesYand moves left. - q2 reads
Xand returns to q0 on the right. - q0 now sees
Y, which means everyahas been matched. It moves to q3. - q3 sees a blank and accepts.
Now trace aab by hand. The machine marks the first a, finds the only b, then marks the second a and runs right past the Y. It reaches a blank in q1, where δ is undefined, so it rejects. The string is rejected, not looped, because every step moves toward a stuck configuration.
Counting steps
Each pass over the tape matches one a with one b and costs O(n) steps. There are n passes, so the machine uses O(n²) steps. This matters for GATE questions about time complexity of a single-tape machine. A single tape forces the head to travel back and forth, which a two-tape machine avoids.
Looping
A machine loops when its computation is infinite. The smallest example never stops moving:
loop_delta = {('q0', '_'): ('q0', '_', 'R')}
verdict, history = trace(loop_delta, '', max_steps=10)
print(verdict, len(history)) # no verdict 11This machine reads a blank, writes a blank, moves right and repeats. Its head wanders off forever. A machine can also loop by bouncing between two cells, in which case the same configuration repeats exactly.
The max_steps cutoff is the key point of this chapter. When the simulator returns no verdict, it has learned nothing definite. The machine might halt at step 10,001. A simulator can confirm acceptance and rejection, because both are reached in finitely many steps. It can never confirm looping by running alone.
Two partial remedies exist, and both have limits:
- Repeated configuration. If a deterministic machine ever repeats a configuration, it loops forever. Checking for repeats catches bouncing loops. It cannot catch a machine whose head moves right forever, because every configuration is new.
- Bounded tape. If the machine is restricted to n cells, there are only finitely many configurations, so every infinite run must repeat one. This is why linear bounded automata have decidable acceptance. A full TM has no such bound.
Part of the later halting-problem argument is that no algorithm can do this check for all machines and inputs.
Common errors in GATE questions
- "Rejects" versus "does not accept". A statement such as "M rejects every string not in L" is true only if M is a decider. If M merely recognizes L, strings outside L may loop.
- Halting is not accepting. A machine can halt in q_reject. "M halts on w" does not imply w ∈ L(M).
- Looping does not put w in L(M). Only reaching q_accept does.
- Implicit versus explicit reject. Questions that give a transition table often leave out q_reject. Read a missing transition as a reject. Never read it as a loop.
- Left end of the tape. In the usual textbook model the tape is infinite only to the right, and a left move at the leftmost cell leaves the head in place. The simulator above uses an infinite two-way tape instead. This changes no language that is decided or recognized, but it can change the exact configuration sequence in a trace question. Use the convention the question states.
- Counting steps. A "step" is one application of δ. Configurations in a trace include the start configuration, so a computation of k steps has k + 1 configurations. The
abrun above has 5 steps and 6 configurations.
Exercise
A deterministic TM M recognizes a language L, but it is not a decider. Which of these statements about an input w ∉ L must hold: (a) M rejects w, (b) M does not accept w, (c) M loops on w? Also, trace the machine above on the input ba and say what the final configuration is.
Show answer
Only (b) must hold. For w ∉ L, M may either reject or loop, so (a) and (c) can each be false. On ba, the start configuration is [q0] ba. State q0 has no transition on b, so the machine is stuck at once and rejects. The final configuration is [q0] ba, reached in zero steps.
Exercise
Why can a repeated-configuration check detect some loops of a deterministic TM but not all of them?
Show answer
In a deterministic machine, a configuration fully determines every later configuration. If a configuration appears twice, the computation cycles forever. But a machine can loop with a head that moves right forever or a tape that keeps growing. Each configuration is then new, so no repeat ever appears. The check therefore finds only loops with finitely many distinct configurations.