Discrete MathematicsLanguages, Automata and Computability6 of 6
Languages, Automata and Computability · 6
Decidable and Recognizable Languages, and P versus NP
Now that we know there is a problem no computer can ever solve (the Halting Problem), we need a way to sort any yes-or-no problem by whether a program can always answer it, only confirm the "yes" cases, or solve it fast.
1
Decision Problems as Languages and Deciders
A language is the set of all input strings for which a yes-or-no question has the answer YES, and a Decider is a program box guaranteed to stop on every input with YES or NO. Because a computer encodes numbers, maps, and code as strings of characters or 0s and 1s, answering any yes-or-no question is the same as testing whether an input string belongs to the language of that problem.
Decision Problems as Languages and Deciders
A language is Decidable when we can build a Decider box that never gets stuck in an infinite loop on any input:
⟶number, map, or codeInput string⟶Decider Box⎩⎨⎧stops with YES (Accept)if inside the languagestops with NO (Reject)if outside the language
Here are the key steps for testing whether 91 is prime.
Text solution
Set a hard ceiling on the searchBecause no divisor of 91 can be larger than 91 itself, the list of trial divisors is finite and the program cannot loop forever:
Trial divisors∈{2,3,4,…,90}
Test divisors in order until one divides evenly or the ceiling is reachedDividing 91 by 2,3,4,5,6,7 in turn hits an exact factor at 7, so the box stops immediately and turns on the red NO light:
91=7×13⟹stops with NO (not prime)
2
Recognizable Languages, Dovetailing, and the Halting Problem
When a search has no upper bound, a machine can stop and light up YES the moment it finds a solution, but it may keep searching forever when no solution exists; a language with a box like this is called Recognizable (or Turing-recognizable). Let's see how an unbounded search works on x3+y3+z3=42, how to run multiple Recognizer boxes without getting trapped in an infinite loop, and why the Halting Problem (`Stops?`) lives inside the Recognizable circle.
Recognizable Languages, Dovetailing, and the Halting Problem
Unlike a Decider, a Recognizer only promises to stop when the answer is YES:
⟶Input string⟶Recognizer Box⎩⎨⎧always stops with YESon a YES inputstops with NO or loops ∞on a NO input
Running one Recognizer box to completion before starting the next can trap us in an infinite loop on the first box while a halting box waits its turn. Instead, we alternate between the boxes one tick at a time (dovetailing) so a looping box never blocks a halting one.
When Box A loops forever, waiting for it to finish traps the machine before Box B ever starts, while alternating one step at a time still reaches Box B's YES.
Using dovetailing and direct simulation, we can connect Recognizers to Deciders and place the Halting Problem (`Stops?`) on our map.
Text solution
Interleave a YES-recognizer and a NO-recognizer to build a DeciderAlternating one tick of a YES-recognizer with one tick of a co-recognizableNO-recognizer inside one outer box guarantees that one of the two inner boxes eventually halts on every input:
⟹Tick 1: step 1 of YES-box,Tick 2: step 1 of NO-box,Tick 3: step 2 of YES-box,…Decider
Simulate the input program directly to recognize `Stops?`Running `Program` on `Input` inside a box stops and lights up YES the moment `Program` halts, and runs forever if `Program` loops forever:
Run Program(Input)⟶⎩⎨⎧halts at some finite step⟹stop and light up YESloops forever⟹simulation loops ∞(allowed on NO)
3
Proving Undecidability by Reduction Wiring
A reduction proves a new problem is Undecidable by wiring a hypothetical Decider box for the new problem inside an empty outer shell for a problem we already know is impossible (`Stops?`). Let's use this wiring to prove that `PrintsZero?` ("Given a program and an input, will that program ever print `'0'`?") is Undecidable.
Proving Undecidability by Reduction Wiring
If the input program prints `'0'` early on and then enters an infinite loop, leaving its printer connected would make `PrintsZero?` light up YES, a false YES for `Stops?`. Instead, we disconnect the printer during the run and reconnect it to print `"0"` only after the program halts.
The inner `PrintsZero?` box tells us whether a program halts only when the wrapper throws away everything the program prints while it runs.
Here is the three-step reduction wiring from `Stops?` to `PrintsZero?`.
Text solution
Place the known impossible problem on the outside and the new problem on the insideStart with an empty outer shell for `Stops?` and place the genie's hypothetical `PrintsZero?` Decider box inside it:
known UndecidableOuter shell: Stops?⊃testing if DecidableInner genie box: PrintsZero?
Modify the input into a wrapper program that can only print `'0'` after haltingBefore feeding `Program` and `Input` into `PrintsZero?`, pass them through a modifier that disconnects the printer during the run and prints `'0'` only after `Program` finishes:
[Wrapper=run Program(Input)with output → trash;after it halts,reconnect printer andprint("0")]
Check both outcomes to confirm the outer `Stops?` shell worksWiring the inner box's YES and NO lights straight to the outer shell builds an impossible `Stops?` Decider, proving no `PrintsZero?` Decider can exist:
⟹⟹⟹⟹Program stops on Input(YES for Stops?)Wrapper reaches print("0")PrintsZero? lights up YESProgram loops on Input(NO for Stops?)Wrapper stays silent foreverPrintsZero? lights up NO
4
Unrecognizable Languages and Loops
An Unrecognizable language lies completely outside the Recognizable circle, meaning no machine can even guarantee stopping with YES on the YES inputs. Let's test `Loops?` ("Given a `Program` and an `Input`, does `Program` run forever on `Input`?") by pairing it with the Recognizer we already built for `Stops?`.
Unrecognizable Languages and Loops
Here is how dovetailing proves that `Loops?` is Unrecognizable.
Text solution
Match a YES for `Loops?` to a NO for `Stops?`Because looping forever is the exact opposite of halting, a working YES-recognizer for `Loops?` is automatically a NO-recognizer (co-recognizer) for `Stops?`:
⟺YES for Loops?(runs forever)NO for Stops?(never stops)
Dovetail the two Recognizers inside an outer `Stops?` Decider shellAlternating our working YES-recognizer for `Stops?` (Box 1) with a hypothetical YES-recognizer for `Loops?` (Box 2) one tick at a time guarantees that one of the two boxes halts on every input:
Alternatebuilt by simulationBox 1(YES-recognizer for Stops?)andhypothetical genie boxBox 2(YES-recognizer for Loops?)⟹Decider for Stops?
Rule out Box 2 by contradictionSince a `Stops?` Decider cannot exist and Box 1 already exists, Box 2 cannot exist, placing `Loops?` outside the Recognizable circle:
⟹Stops? is Undecidableno YES-recognizerfor Loops? exists
5
Polynomial Time, Class P, and Class NP Verifiers
Inside the Decidable circle, Class P is the collection of problems a Decider can solve from scratch in polynomial time, O(nk), while Class NP is the collection of problems where a YES answer can be verified in polynomial time with the help of a short Hint (also called a Certificate or Witness). Both `Shortest Path` and `Traveling Salesperson` on an n-city map are Decidable because the number of possible routes is finite, yet their step counts grow at completely different rates.
Polynomial Time, Class P, and Class NP Verifiers
Comparing the step counts on n=100 cities shows why polynomial time separates practical algorithms from brute-force explosions:
Shortest Path (Dijkstra’s algorithm):O(n2) steps⟹1002=10,000 operations(in P)Traveling Salesperson (every ordering,or best known methods):n! steps, or about 2n steps⟹2100≈1.3×1030 operations
A problem belongs to Class NP if a Verifier Box can check a proposed YES hint in polynomial time (while never being tricked into saying YES when the answer is NO):
Because any problem we can solve from scratch in O(nk) time can be verified in O(nk) time by simply ignoring the Hint and solving the problem directly, Class P sits completely inside Class NP:
⟹⟹Class P (solvable alone in O(nk))Verifier ignores Hintand solves in O(nk)P⊆NP
Remember that NP stands for Nondeterministic Polynomial, never "Not Polynomial", and every problem in P is automatically in NP too.
6
Reducing 3-SAT to Independent Set and NP-Completeness
The NP-Complete problems sit at the top edge of the NP circle: they live inside NP, and every problem in NP can be translated (fast-wired) into them in polynomial time. Let's build a polynomial-time reduction from `3-SAT` (making every 3-item logic clause True without contradictions) to `Independent Set` (choosing k dots in a graph with no line between any two of them), and see how all of these language classes fit together.
Reducing 3-SAT to Independent Set and NP-Completeness
In the graph built from a k-clause `3-SAT` formula, triangle lines keep us from picking two dots in the same clause, and conflict lines keep us from picking both a variable (like x) and its negation (¬x). Picking one dot per triangle with no conflict lines between them corresponds directly to a True/False assignment that makes every clause True.
In the graph for a 3-SAT formula, picking one dot per triangle with no lines between them makes every clause true without any contradictions.
Here are the key steps that fast-wire an outer `3-SAT` shell into an inner `Independent Set` box.
Text solution
Turn each 3-item clause into a triangle of 3 dotsFor a `3-SAT` formula with k clauses, draw k triangles (3k dots in total) so that the triangle lines forbid picking more than 1 dot from the same clause:
⟶k clauses of 3 itemsk triangles (3k dots,at most 1 chosen dotper triangle)
Draw conflict lines between every variable and its negationConnect every dot labeled with a variable (like x) to every dot labeled with its opposite (¬x) so contradictory items can never both be picked:
Verify both directions between the outer `3-SAT` shell and the inner `Independent Set` boxChoosing 1 True dot per clause gives k dots with no shared triangle or conflict lines, and conversely any k independent dots must place 1 dot in each of the k triangles without contradictions, setting their labels to True to satisfy all k clauses:
⟺3-SAT formula issatisfiable (YES)Graph has an independentset of k dots (YES)
This map brings together every language class from the outside in.
Key concepts
Every yes-or-no question is a language (the set of all YES input strings), and where that language lives on the map comes down to how a program box behaves on its inputs and how you wire boxes together:
Decidable vs. Recognizable (look for a hard ceiling on the search)
If a brute-force search has a finite upper bound (like testing divisors up to n to check whether n is prime), the box is guaranteed to finish and say YES or NO, making the language Decidable. Only when the search space has no upper bound (like searching all integers for x3+y3+z3=42 or simulating `Program(Input)` in `Stops?`) does brute force give merely a Recognizer (stops with YES on a YES, may loop forever on a NO). Whenever you run multiple Recognizer boxes (or pair a YES-recognizer with a NO-recognizer to build a Decider), never run one to completion before starting the next; dovetail them one tick at a time (A1,B1,A2,B2,…).
Undecidable vs. Unrecognizable (wire the known impossible problem on the outside)
To prove a new problem like `PrintsZero?` is Undecidable, place `Stops?` as the outer shell and the new problem as the inner genie box, wrapping the input program so it triggers the inner YES if and only if it halts (never put `Stops?` on the inside). To prove a problem like `Loops?` is Unrecognizable (outside the Recognizable circle entirely), notice that a YES for `Loops?` is a NO for `Stops?`: if a YES-recognizer for `Loops?` existed, dovetailing it with the YES-recognizer for `Stops?` would build an impossible `Stops?` Decider.
Class P, Class NP, and NP-Complete (solving from scratch vs. checking a Hint in O(nk) time)
Inside the Decidable circle, a problem is in P if a Decider solves it from scratch in polynomial time O(nk) (like Dijkstra's O(n2) `Shortest Path`), and in NP if a Verifier Box checks a YES Hint in O(nk) time (so P⊆NP automatically). To prove a problem is NP-Complete, verify that it is in NP and fast-wire a known NP-Complete problem like `3-SAT` as the outer shell into your new problem on the inside using an O(nk) modifier, checking both directions (YES⟺YES). If any one NP-Complete problem gets a polynomial-time algorithm, all of NP collapses into P(P=NP), while Undecidable problems like `Stops?` stay Undecidable.
7
Practice
1
Explain why every yes-or-no question can be viewed as a language, and compare the following two problems:
Count how many triples (x,y,z) exist when each variable is between −n and n: does that search have a hard ceiling where the box can stop and light up NO?
Any input (a number, a map, or a program) is encoded as a string of characters or 0s and 1s, and the language of a yes-or-no problem is the set of all input strings for which the answer is YES.
Problem (A) is Decidable because the search space is finite: between −n and n there are (2n+1) choices for each variable, giving (2n+1)3 triples (x,y,z) in total. (Watch out: don't count 2n choices per variable and get 8n3 by forgetting 0, and don't claim Problem (B) can also stop at ±n, because solutions to x3+y3+z3=n can require numbers much larger in size than n.) That finite count gives a hard ceiling where the machine is guaranteed to finish checking every triple and stop with YES if one works or NO if none do.
Problem (B) has no upper bound on how large x,y, and z might be: spiraling outward through all triples of integers is guaranteed to stop and light up YES if a solution exists, which proves (B) is Recognizable, but when the answer is NO the search never reaches a point where it can rule out larger numbers and stop with NO.
2
Suppose you have a working Recognizer box R for a Recognizable language L. You want to build a new Recognizer box that takes three inputs A,B, and C and lights up YES if at least one of A,B, or C is in L. Explain why running R on A until it finishes, then on B, then on C fails, and give the exact schedule that fixes it.
What happens to B and C if A is a NO input that makes R loop forever? How can you share clock ticks across all three inputs?
Because R is only a Recognizer, it is allowed to loop forever on a NO input. If A is a NO input that makes R loop forever and C is a YES input, running A to completion first traps the machine in A's infinite loop so it never reaches B or C and fails to light up YES.
To fix this, dovetail (interleave) the three runs step by step: run step 1 of R on A, then step 1 of R on B, then step 1 of R on C, then step 2 on A, step 2 on B, step 2 on C, and so on, stopping and lighting up YES the moment any of the three runs lights up YES. (Watch out: don't dovetail A and B first and wait for them to finish before starting C, because if both A and B loop forever while C is a YES input, a two-way alternation never starts C.) Even if two of the inputs loop forever, the third still advances every third tick and reaches its YES in a finite number of steps.
3
Consider the problem `PrintsDone?`: "Given a `Program` and an `Input`, will `Program` ever print the string `'DONE'` when run on `Input`?" (a) A student attempts to prove `PrintsDone?` is undecidable by placing a `Stops?` Decider box inside an outer `PrintsDone?` shell. Why does this direction prove nothing? (b) Prove that `PrintsDone?` is Undecidable using a reduction from `Stops?`: state which box is the outer shell and which is the inner genie box, describe the wrapper program, and verify both cases (when `Program` stops on `Input` and when `Program` loops forever on `Input`).
Which problem do we already know is impossible to build (`Stops?` or `PrintsDone?`), and how do you prevent `Program` from printing `'DONE'` on its own before it halts?
(a) Putting `Stops?` on the inside only shows "If I had an impossible `Stops?` Decider, I could build a `PrintsDone?` Decider," which proves nothing about whether `PrintsDone?` can exist. The known impossible problem (`Stops?`) must always be the outer shell, and the new problem (`PrintsDone?`) must be the inner genie box.
(b) Place an empty `Stops?` Decider shell on the outside and put a hypothetical `PrintsDone?` Decider box on the inside. Given `Program` and `Input` entering the outer `Stops?` shell, pass them through a modifier that builds a wrapper program: the wrapper runs `Program` on `Input` with its printer disconnected (redirecting all output to the trash), and only after `Program` halts does the wrapper reconnect the printer and execute `print("DONE")`. (Watch out: don't try to simply delete `print("DONE")` lines from the source code of `Program`, because a looping program can compute and print the letters `'D'`, `'O'`, `'N'`, `'E'` in other ways unless its printer is disconnected during the run.) Feed this wrapper into the inner `PrintsDone?` box and wire its YES and NO lights directly to the outer YES and NO lights.
Case 1If `Program` stops on `Input` (YES for `Stops?`), the wrapper finishes running `Program`, reconnects the printer, and prints `"DONE"`, so `PrintsDone?` lights up YES.
Case 2If `Program` loops forever on `Input` (NO for `Stops?`), the wrapper stays stuck running `Program` with the printer disconnected and never prints `"DONE"`, so `PrintsDone?` lights up NO.
Because this wiring would build an impossible `Stops?` Decider, no `PrintsDone?` Decider can exist.
4
Consider the problem `NeverPrintsZero?`: "Given a `Program` and an `Input`, is it true that `Program` never prints `'0'` at any point during its run on `Input`?" Given that `PrintsZero?` is undecidable, first explain why `PrintsZero?` is Recognizable, and then prove that `NeverPrintsZero?` is Unrecognizable (lives completely outside the Recognizable circle).
A YES for `NeverPrintsZero?` is what outcome for `PrintsZero?`? Recall what happens when you dovetail a YES-recognizer and a NO-recognizer for the same undecidable problem.
`PrintsZero?` is Recognizable because we can build a box that simulates `Program` on `Input` step by step and watches its output: if `Program` ever prints `'0'`, it does so at some finite step, at which moment our box stops and lights up YES (and if `Program` never prints `'0'`, our box either halts without lighting up YES or loops forever, which is allowed for a Recognizer). (Watch out: don't claim `NeverPrintsZero?` is also Recognizable by "running `Program` until it stops and checking that no `'0'` appeared", because when `Program` loops forever without printing `'0'`, the answer to `NeverPrintsZero?` is YES, yet the simulation runs forever and never lights up YES.)
Now suppose for contradiction that a genie hands us a YES-recognizer for `NeverPrintsZero?`. Because a YES for `NeverPrintsZero?` is the exact same thing as a NO for `PrintsZero?`, the genie's box is a NO-recognizer (co-recognizer) for `PrintsZero?`. We can place both our YES-recognizer for `PrintsZero?` and the genie's YES-recognizer for `NeverPrintsZero?` inside an outer `PrintsZero?` Decider shell and alternate them one step at a time (dovetailing). On every input `(Program, Input)`, `Program` either eventually prints `'0'` or never prints `'0'`, so one of the two inner boxes is guaranteed to stop and light up YES, letting the outer shell stop and light up YES or NO accordingly. That would give a full Decider for `PrintsZero?`, contradicting that `PrintsZero?` is undecidable; therefore no Recognizer for `NeverPrintsZero?` can exist.
5
Apply the polynomial-time reduction from `3-SAT` to `Independent Set` to the 3-clause formula (x OR y OR ¬z) AND (¬x OR ¬y OR z) AND (x OR ¬y OR ¬z). (a) How many dots (vertices), how many triangle lines, and how many conflict lines does the constructed graph have, and what target number k of dots is passed to the `Independent Set` box? (b) Using the satisfying assignment x=True,y=False,z=False, pick a valid set of k dots (one from each clause triangle) and explain why no two chosen dots share a triangle line or a conflict line.
Each of the k=3 clauses forms a triangle of 3 dots. For the conflict lines, count how many dots are labeled with each variable (x,y,z) and multiply by how many dots are labeled with its negation (¬x,¬y,¬z).
(a) With k=3 clauses, the graph has 3k=9 dots arranged in 3 triangles. Each triangle has 3 lines, giving 3×3=9 triangle lines. For the conflict lines:
x (in clauses 1 and 3, so 2 dots) connects to ¬x (in clause 2, so 1 dot) for 2×1=2 lines;
y (in clause 1, so 1 dot) connects to ¬y (in clauses 2 and 3, so 2 dots) for 1×2=2 lines;
z (in clause 2, so 1 dot) connects to ¬z (in clauses 1 and 3, so 2 dots) for 1×2=2 lines.
(Watch out: don't count just 1 conflict line per variable to get 3 conflict lines and 12 total lines, because every occurrence of a variable must connect to every occurrence of its negation.) Adding these gives 2+2+2=6 conflict lines (15 lines in total), and the target passed to `Independent Set` is k=3 dots.
(b) Under x=True,y=False,z=False, the literals that evaluate to True are x,¬y, and ¬z.(Watch out: don't pick y from Triangle 1 or z from Triangle 2, which evaluate to False under this assignment and would share a conflict line with ¬y or ¬z.) We can pick x from Triangle 1 (where x and ¬z are True), ¬y from Triangle 2 (where only ¬y is True), and ¬z (or x or ¬y) from Triangle 3 (where all three are True). These 3 dots share no triangle lines because we picked 1 dot from each triangle, and they share no conflict lines because all three chosen labels (x,¬y,¬z) are simultaneously True under a single assignment, so no variable and its negation are both picked.
6
Suppose you want to prove that a new decision problem, `TeamSchedule?`, is NP-Complete using the known NP-Complete problem `3-SAT`. (a) State the two conditions you must prove, including for the reduction which problem is the outer shell, which is the inner helper box, what speed requirement the modifier must meet, and both logical directions you must verify. (b) If someone later discovers an O(n3) algorithm that solves `TeamSchedule?` from scratch, what happens to the complexity or decidability of `3-SAT`, `Traveling Salesperson`, and the Halting Problem (`Stops?`)?
Check both parts of the definition of NP-Complete (NP membership and NP-Hardness). Which box must go on the outside so that a fast box for `TeamSchedule?` solves `3-SAT`?
(a) First, prove `TeamSchedule?` is in NP by building a Verifier Box that takes a problem input and a short Hint (Certificate/Witness) and checks whether the hint is valid in polynomial time, O(nk), such that a valid hint exists if and only if the answer is YES. Second, prove `TeamSchedule?` is NP-Hard by wiring `3-SAT` as the outer shell around a hypothetical `TeamSchedule?` box as the inner helper box, using a modifier that translates any `3-SAT` formula into a `TeamSchedule?` input in polynomial time, O(nk), and verifying both directions: (1) if the `3-SAT` formula is satisfiable (YES), then the translated `TeamSchedule?` input is a YES, and (2) if the translated `TeamSchedule?` input is a YES, then the `3-SAT` formula is satisfiable (YES). (Watch out: don't put `TeamSchedule?` on the outside and `3-SAT` on the inside, because that direction only shows `TeamSchedule?` can be solved using `3-SAT`, not that `TeamSchedule?` is at least as hard as `3-SAT`.)
(b) Because `3-SAT` (and every problem in NP) fast-wires into `TeamSchedule?` in polynomial time, an O(n3) algorithm for `TeamSchedule?` immediately gives a polynomial-time algorithm for `3-SAT`, `Traveling Salesperson`, and every other problem in NP, collapsing the entire NP circle into P (so P=NP). (Watch out: don't say that P=NP makes `Stops?` solvable too, because the Halting Problem lies outside the Decidable circle altogether, so it remains Undecidable.)