Sub topic 13
The Post Correspondence Problem
This chapter assumes you know the halting problem, many-one reductions, and context-free grammars. The Post Correspondence Problem (PCP) is a decision problem about strings, and it has no Turing machine in its statement. That makes it a convenient source problem for undecidability proofs about grammars and other string-based objects.
The problem
An instance of PCP is a finite list of dominoes. Each domino has a top string and a bottom string over some alphabet. You may use each domino as many times as you like, in any order.
A match is a non-empty sequence of dominoes whose top strings, concatenated, equal the concatenation of their bottom strings. PCP asks whether a match exists.
Formally, given pairs (x₁, y₁), …, (xₙ, yₙ), is there a sequence i₁, …, i_k with k ≥ 1 such that xᵢ₁xᵢ₂…xᵢₖ = yᵢ₁yᵢ₂…yᵢₖ?
Here is an instance with three dominoes:
| Domino | Top | Bottom |
|---|---|---|
| 1 | a | baa |
| 2 | ab | aa |
| 3 | bba | bb |
The sequence 3, 2, 3, 1 is a match. Both sides spell bbaabbbaa:
- Top: bba + ab + bba + a
- Bottom: bb + aa + bb + baa
What is known about its difficulty
- PCP is undecidable. No algorithm takes every instance and always answers yes or no correctly.
- PCP is recursively enumerable. If a match exists, trying all sequences in order of length will eventually find it.
- The complement of PCP (no match exists) is therefore not recursively enumerable. If it were, PCP would be recursive, which contradicts undecidability.
- It stays undecidable when the alphabet has two symbols.
- It is decidable when the alphabet has one symbol. Each domino then reduces to a pair of lengths, and a short counting argument settles the question.
A semi-decision procedure
The code below searches for a match in breadth-first order. It prunes any partial sequence where neither concatenated string is a prefix of the other, because such a sequence can never be extended to a match. The bound max_len is needed because the search may run forever on instances with no match.
from collections import deque
def pcp(tiles, max_len=10):
"""Return a list of 1-based tile indices forming a match, or None.
None means no match of length <= max_len was found. It does NOT
prove that no match exists.
"""
queue = deque([((), "", "")])
while queue:
seq, top, bot = queue.popleft()
if seq and top == bot:
return [i + 1 for i in seq]
if len(seq) == max_len:
continue
for i, (t, b) in enumerate(tiles):
nt, nb = top + t, bot + b
if nt.startswith(nb) or nb.startswith(nt):
queue.append((seq + (i,), nt, nb))
return None
print(pcp([("a", "baa"), ("ab", "aa"), ("bba", "bb")])) # [3, 2, 3, 1]
print(pcp([("a", "ab")])) # NoneThe second instance has a single domino whose bottom is longer than its top. Every use makes the bottom longer, so no match exists. For this instance the pruning happens to prove that. In general, an answer of None only means the search ran out of budget.
Why PCP is undecidable
The proof reduces A_TM (does machine M accept input w?) to PCP, in two steps.
Step 1: the modified problem (MPCP)
MPCP is PCP with one extra rule: the match must begin with the first domino in the list. First you show A_TM ≤ MPCP. Then you show MPCP ≤ PCP.
Step 2: encoding a computation as a match
Given M and w, build dominoes so that a match is forced to spell out an accepting computation history. A history is a string of configurations separated by #:
C₀ # C₁ # C₂ # … # C_accept
The construction works like this:
- The first domino is
#on top and#q₀w#on the bottom. The bottom starts with the initial configuration and is one configuration ahead of the top. - For each transition of M, add dominoes that copy the tape and apply one step of M. The top lags behind, and the bottom writes the next configuration.
- Add copy dominoes (a/a for each tape symbol, and #/#) so that unchanged tape cells carry over.
- Once the bottom reaches the accepting state, add cleanup dominoes that let the top catch up. After they are used, the two strings are equal.
The top can only catch up if the bottom contains an accepting configuration. So a match exists exactly when M accepts w.
Converting MPCP to PCP uses a marker symbol *. Insert * after every symbol of each top string and before every symbol of each bottom string. Add a final domino to close the match. This forces any PCP match to start with the original first domino.
Using PCP to prove other problems undecidable
PCP is a source problem for reductions into formal-language questions. The main example is context-free grammars.
Take a PCP instance with dominoes (x₁, y₁), …, (xₙ, yₙ). Pick new symbols a₁, …, aₙ, one per domino, that do not appear in the instance. Define two grammars:
- Gtop: SA → xᵢ S_A aᵢ | xᵢ aᵢ, for every i
- Gbot: SB → yᵢ S_B aᵢ | yᵢ aᵢ, for every i
A string generated by Gtop has the form xᵢ₁…xᵢₖ aᵢₖ…aᵢ₁. A string generated by Gbot has the same form with y in place of x. Both grammars are context-free, so both languages are context-free (CFLs).
A string is in both languages exactly when the same index sequence gives equal top and bottom strings. That is a PCP match. This gives two results.
- Intersection non-emptiness. L(Gtop) ∩ L(Gbot) ≠ ∅ if and only if the PCP instance has a match. So deciding whether two CFLs have an empty intersection is undecidable.
- Ambiguity. Form G with start rule S → SA | SB and all the rules above. A string generated by both sides has two distinct leftmost derivations. The only ambiguity possible in G arises this way, because each side is unambiguous: the trailing index symbols determine the rule choices. So G is ambiguous if and only if a match exists. Deciding whether a CFG is ambiguous is undecidable.
Related results follow from the same idea:
- The complement of L(G_top) ∪ ... style constructions (using the complement of the set of valid computation histories) show that CFG universality (does G generate Σ*?) is undecidable.
- Whether L(G₁) = L(G₂) for CFGs is undecidable.
- Whether the intersection of two CFLs is itself context-free is undecidable.
Exam traps
- Direction of reduction. To show a problem P is undecidable, reduce PCP to P (PCP ≤ P). Reducing P to PCP proves nothing about P.
- "PCP is decidable for small cases." Specific instances can be solved, and bounded versions (match using at most k dominoes) are decidable. Only the unrestricted problem over all instances is undecidable.
- Semi-decidable, not decidable. If a statement says PCP is "recursively enumerable," it is true. If it says "recursive," it is false. If it says the complement is recursively enumerable, it is false.
- Regular and deterministic CFL problems are different. Emptiness, finiteness, and membership for CFGs are decidable. Equivalence of regular languages is decidable. Equivalence of DPDAs is also decidable. It is general CFG equivalence, universality, ambiguity, and intersection emptiness that are undecidable.
- Alphabet size. Undecidability holds already for two symbols, so shrinking the alphabet does not help unless it is a single symbol.
Exercise
A PCP instance has dominoes (ab, a) and (b, abb). Determine whether it has a match, using the prefix test the search code applies.
Show answer
Start with domino 1: top ab, bottom a. The bottom is a prefix of the top, so this is a valid partial match with the top ahead by b. Adding domino 1 again gives top abab and bottom aa. That fails, since aa is not a prefix of abab. Adding domino 2 gives top abb and bottom aabb. That fails, since the second symbols differ (b versus a). Starting with domino 2: top b, bottom abb. Neither is a prefix of the other, so it fails immediately. So no sequence survives the prefix test beyond domino 1 alone, and domino 1 alone has top ab, bottom a, which are unequal. There is no match.
Exercise
Which statements are true? (a) PCP is recursively enumerable. (b) The complement of PCP is recursively enumerable. (c) Deciding whether the intersection of two CFLs is empty is undecidable. (d) Deciding whether a regular language is empty is undecidable.
Show answer
(a) is true: a search over sequences finds a match whenever one exists. (b) is false: if both PCP and its complement were recursively enumerable, PCP would be recursive, which contradicts its undecidability. (c) is true, by the reduction from PCP using Gtop and Gbot. (d) is false: emptiness of a regular language is decidable by checking whether any accepting state is reachable.
Exercise
In the CFG construction for ambiguity, why is it necessary to append the index symbols a₁, …, aₙ after each x or y string?
Show answer
Without the index symbols, a string could be produced by Gtop and by Gbot through different index sequences that happen to spell the same string. Then a shared string would not mean the same sequence of dominoes was used on both sides. The index symbols record the sequence of dominoes (in reverse), so a shared string forces the top and bottom to use the same sequence. That is exactly what a PCP match requires.