Mock Exam 1
Difficulty: ★☆☆☆☆ Warmup · 45 pts · 10 questions

Time: 3 hours. Aids: One double-sided A4 handwritten cheatsheet. No calculator.

Assume P ≠ NP and NP ≠ EXP. Karp reductions denoted ≤P. Mapping reductions denoted ≤m. 0 ∈ ℕ.

Irrelevant information may cost points. Use scrap paper first.

Points
Question12345678910Total
Points532354737645
Questions
Q1 (5 points) — DFA Construction

Consider DFA D = (Q, {a, b}, δ, q0, F) with states q0 (start), q1, q2 and transition function:

δab
q0 (start)q1q0
q1q2q0
q2q2q2

The accept states are F = {q0}.

(a) (1 pt) Describe L(D) in plain English.

(b) (4 pts) Consider DFA D′ = ({p0, p1}, {a, b}, δ′, p0, {p0}) with:

δ′ab
p0 (start, accept)p1p0
p1p0p1

Construct DFA D′′ such that L(D′′) = L(D) ∩ L(D′) using the product construction. Leave out unreachable states.

Solution

(a) L(D) = the set of all strings over {a, b} that do not contain "aa" as a substring.

Reasoning: q0 is the only accept state. Reading one ‘a’ goes to q1 (not accepting — need to “return” via b). Reading two consecutive a’s goes to q2, which is a trap (all transitions go to q2, never accepting). So D accepts iff the input never has two a’s in a row.

(b) L(D′) = strings with an even number of a’s (p0 counts even, p1 counts odd; b’s don’t change state).

Product construction D′′ = (Q′′, {a, b}, δ′′, (q0, p0), F′′) where F′′ = F × F′ = {(q0, p0)}.

Starting from (q0, p0), explore reachable states:

StateabAccept?
(q0, p0)(q1, p1)(q0, p0)Yes
(q1, p1)(q2, p0)(q0, p1)No
(q2, p0)(q2, p1)(q2, p0)No
(q0, p1)(q1, p0)(q0, p1)No
(q2, p1)(q2, p0)(q2, p1)No
(q1, p0)(q2, p1)(q0, p0)No

6 reachable states. L(D′′) = strings with no “aa” AND an even number of a’s.

Accept state: {(q0, p0)}. 6 reachable states (product has 6 total, all reachable from start).
Q2 (3 points) — Subset Construction

Consider NFA N = ({q0, q1, q2}, {0, 1}, δ, q0, {q2}) with transition function:

δ01
q0 (start){q0, q1}{q0}
q1{q2}
q2 (accept)

Apply the subset construction to obtain a DFA D with L(D) = L(N). Leave out unreachable states.

Solution

Start state: {q0}. Build all reachable subsets:

DFA State01Accept?
{q0}{q0, q1}{q0}No
{q0, q1}{q0, q1}{q0, q2}No
{q0, q2}{q0, q1}{q0}Yes

3 reachable states. A DFA state is accepting if it contains q2.

L(N) = strings over {0, 1} that contain the substring “01” — reading a 0 “loads” q1, and then a 1 reaches q2.

DFA has 3 states: {q0}, {q0, q1}, {q0, q2}. Accept: {q0, q2}.
Q3 (2 points) — Regular Expression

Give a regular expression R such that:

L(R) = { w ∈ {a, b}* | w contains at least one ‘a’ and ends with ‘b’ }

Solution

We need: (1) at least one ‘a’ somewhere, and (2) the last character is ‘b’.

Strategy: get to the first ‘a’ (skip b’s), then anything, then end with ‘b’.

R = (a | b)*a(a | b)*b

Equivalent shorter forms: b*a(a | b)*b. The first (a | b)* also works — it already subsumes b*.

Q4 (3 points) — Pumping Lemma

Let L = { anbn | n ≥ 0 } over Σ = {a, b}.

A student begins the following proof:

“Suppose L is regular. Let p be the pumping length guaranteed by the pumping lemma. Choose w = apbp. Then |w| = 2p ≥ p. By the pumping lemma, w = xyz with |xy| ≤ p and |y| ≥ 1.”

Is it possible to complete this proof to show L is not regular? If so, finish it.

Solution

Yes, the proof can be completed.

Since |xy| ≤ p and w starts with ap, the prefix xy lies entirely within the a-block. So y = ak for some k ≥ 1.

Pump with i = 2:

xy2z = ap+kbp

Since k ≥ 1, we have p + k > p, so the number of a’s exceeds the number of b’s.

Therefore xy2z ∉ L, contradicting the pumping lemma.

L is not regular. □
Q5 (5 points) — CFG + PDA

Consider the context-free grammar G = ({S, X}, {a, b}, R, S) with rules:

S → XX
X → aXb | ε

(a) (2 pts) Prove that G is ambiguous.

(b) (3 pts) Give a PDA P with at most 5 states such that L(P) = { anbm | n ≥ 0, m ≥ 1 }. Give your answer as a transition diagram with transitions in the format “input, pop → push”.

Solution

(a) We show that w = “ab” has two distinct leftmost derivations.

Derivation 1:

S ⇒ XX ⇒ aXbX ⇒ abX ⇒ ab

(Expand left X → aXb, then X → ε, then right X → ε.)

Derivation 2:

S ⇒ XX ⇒ X ⇒ aXb ⇒ ab

(Expand left X → ε, then remaining X → aXb, then X → ε.)

Two different leftmost derivations for “ab”, so G is ambiguous. □

(b) PDA for L = { anbm | n ≥ 0, m ≥ 1 }:

3 states suffice: q0 (start), q1 (reading b’s), qacc (accept).

Transitions:

  • q0 ⟶ ε, ε → $ ⟶ q0  (push start marker — or just initialize with $ on stack)
  • q0: a, ε → ε  (read a’s, no stack needed)
  • q0: b, ε → ε  → q1  (read first b, move to q1)
  • q1: b, ε → ε  (read more b’s)
  • q1: ε, ε → ε  → qacc  (accept when done)

Since we don’t need to match counts, the stack is not used for counting. We only need to ensure at least one b is read, which the state structure handles.

3 states: q0 reads a’s, first b moves to q1, ε-transition to qacc.
Q6 (4 points) — Turing Machine

Consider TM M = (Q, {a, b}, {a, b, ⊔}, δ, q0, qacc, qrej) with transition function:

StateReadWriteMoveNext
q0aaRq1
q0bbRqrej
q0Rqrej
q1aaRq1
q1bbRqacc
q1Rqrej

(a) (2 pts) Give a word w of length 3 such that w ∈ L(M). Show the full sequence of configurations.

(b) (2 pts) Is L(M) decidable? Justify your answer.

Solution

(a) w = “aab”.

Configurations (underlined = head position):

q0 aab
⊢ a q1 ab
⊢ aa q1 b
⊢ aab qacc

M accepts “aab”. ✓

(b) Yes, L(M) is decidable.

L(M) = { anb | n ≥ 1 } — strings of one or more a’s followed by exactly one b.

Two arguments:

  1. M always halts: every transition moves right, and the tape is finite, so M must eventually read ⊔ and halt (accept or reject). Since M is a decider, L(M) is decidable.
  2. L(M) is regular (recognized by a simple DFA), and every regular language is decidable.
L(M) = { anb | n ≥ 1 }. Decidable — M is a decider (always halts).
Q7 (7 points) — Undecidability

Consider the language:

Lε = { ⟨M⟩ | M is a TM and ε ∈ L(M) }

(a) (2 pts) Prove that Lε is Turing-recognizable.

(b) (4 pts) Prove that Lε is undecidable. Hint: reduce from ATM.

(c) (1 pt) Prove that Lε is not co-Turing-recognizable.

Solution

(a) Lε is Turing-recognizable.

Build TM R = “On input ⟨M⟩:

  1. Simulate M on input ε.
  2. If M accepts, accept.”

If ⟨M⟩ ∈ Lε, then M accepts ε, so R accepts. If M does not accept ε (rejects or loops), R either rejects or loops — which is fine for a recognizer. So R recognizes Lε. □

(b) Lε is undecidable.

Reduction: ATMm Lε.

Assume for contradiction that some TM D decides Lε. Build TM S that decides ATM:

S = “On input ⟨M, w⟩:

  1. Construct TM M′ = “On input x:
     (a) Ignore x.
     (b) Run M on w.
     (c) If M accepts w, accept.”
  2. Run D on ⟨M′⟩.
  3. If D accepts, accept. If D rejects, reject.”

Correctness:

  • If M accepts w: then M′ accepts every input (including ε), so ε ∈ L(M′), so ⟨M′⟩ ∈ Lε, so D accepts, so S accepts.
  • If M does not accept w: then M′ accepts nothing, so ε ∉ L(M′), so ⟨M′⟩ ∉ Lε, so D rejects, so S rejects.

S decides ATM. But ATM is undecidable. Contradiction. □

(c) Lε is not co-Turing-recognizable.

From (a), Lε is TR. From (b), Lε is undecidable.

If Lε were also co-TR, then Lε would be both TR and co-TR, which implies Lε is decidable (a language is decidable iff both it and its complement are recognizable). But (b) shows Lε is undecidable. Contradiction.

Lε is TR (part a), undecidable (part b), and not co-TR (part c). □
Q8 (3 points) — Fill-in Reduction

Consider the language:

LNE = { ⟨M⟩ | M is a TM and L(M) ≠ ∅ }

Consider the following mapping reduction from ATM to LNE, computed by TM F:

F = “On input ⟨M, w⟩:

  1. Construct TM M′ = “On input x:
     (a) [FILL IN]
    2. Output ⟨M′⟩.”

(a) (1 pt) Fill in the body of M′ so that the reduction is correct. Specifically: what should M′ do, and what is L(M′) in each case?

(b) (2 pts) What does this reduction prove about LNE? State what it shows about decidability, Turing-recognizability, and co-Turing-recognizability.

Solution

(a) M′ = “On input x:

  1. Ignore x.
  2. Run M on w.
  3. If M accepts w, accept.”

Two cases:

  • M accepts w ⇒ M′ accepts every input ⇒ L(M′) = Σ* ≠ ∅ ⇒ ⟨M′⟩ ∈ LNE. ✓
  • M does not accept w ⇒ M′ accepts nothing ⇒ L(M′) = ∅ ⇒ ⟨M′⟩ ∉ LNE. ✓

So ⟨M, w⟩ ∈ ATM ⇔ F(⟨M, w⟩) ∈ LNE. The reduction is correct.

(b) The reduction ATMm LNE proves:

  • Undecidable: ATM is undecidable, so LNE is undecidable.
  • Not co-TR: The same computable function f witnesses ATMm LNE. Since ATM is not TR, LNE is not TR, which means LNE is not co-TR.
  • TR: LNE is Turing-recognizable (dovetail: enumerate all strings x1, x2, … and simulate M on each; if M accepts any xi, then L(M) ≠ ∅, so accept). This is not shown by the reduction itself, but is a separate argument.
LNE is TR, undecidable, and not co-TR.
Q9 (7 points) — NP-Completeness

Consider the following problem:

TEAM FORMATION

Instance: A set of players P, a set of required skills K, a function skill : P → 2K mapping each player to their set of skills, and an integer t.

Question: Is there a subset T ⊆ P with |T| ≤ t such that ∪p ∈ T skill(p) = K?

(a) (2 pts) Prove that TEAM FORMATION ∈ NP.

(b) (1 pt) To prove NP-hardness, we reduce from VERTEX COVER. Give a yes-instance of VERTEX COVER with |V| = 4, |E| = 3, and k = 2.

(c) (2 pts) Consider the following reduction from VERTEX COVER to TEAM FORMATION. Given instance (G = (V, E), k):

Apply this reduction to your VERTEX COVER instance from (b).

(d) (1 pt) Is the resulting instance a yes-instance of TEAM FORMATION? Explain.

(e) (1 pt) The reduction has a flaw. Identify it and explain how to fix it.

Solution

(a) TEAM FORMATION ∈ NP.

Certificate: a subset T ⊆ P.

Verifier:

  1. Check |T| ≤ t.  (O(|P|))
  2. Compute S = ∪p ∈ T skill(p).  (O(|P| · |K|))
  3. Check S = K.  (O(|K|))

All steps polynomial in input size. □

(b) VERTEX COVER yes-instance.

V = {v1, v2, v3, v4}, E = {e1={v1,v2}, e2={v2,v3}, e3={v3,v4}}, k = 2.

Cover: {v2, v3} — v2 covers e1, e2 and v3 covers e2, e3. All edges covered. ✓

(c) Apply the reduction.

  • P = {v1, v2, v3, v4}
  • skill(v1) = {e1}
  • skill(v2) = {e1, e2}
  • skill(v3) = {e2, e3}
  • skill(v4) = {e3}
  • K = {e1, e2, e3}
  • t = |V| − k = 4 − 2 = 2

(d) Yes-instance?

T = {v2, v3}, |T| = 2 ≤ 2 = t. skill(v2) ∪ skill(v3) = {e1,e2} ∪ {e2,e3} = {e1,e2,e3} = K. ✓

Yes, it is a yes-instance of TEAM FORMATION.

(e) The flaw.

The reduction sets t = |V| − k, but it should be t = k. VERTEX COVER asks for a cover of size ≤ k, and the reduction maps vertices to players and edges to skills. A vertex cover of size k corresponds to a team of size k that covers all edges/skills. Using |V| − k inverts the bound.

Counterexample: Take the same graph with k = 1. The VC instance is a no-instance (can’t cover 3 edges with 1 vertex). But the reduction gives t = 4 − 1 = 3, and T = {v1, v2, v3} covers all edges — so TEAM FORMATION says yes. The reduction maps No → Yes, which is incorrect.

Fix: change t = |V| − k to t = k.
Q10 (6 points) — True / False

For each claim, state whether it is True or False and give a proof or counterexample.

(a) (1 pt) Every NFA can be converted to a DFA with at most the same number of states.

(b) (2 pts) If L is decidable, then L is Turing-recognizable.

(c) (1 pt) The language { anbn | n ≥ 0 } is regular.

(d) (2 pts) If A ≤m B and A is undecidable, then B is not Turing-recognizable.

Solution

(a) FALSE.

Counterexample: The language “the n-th symbol from the end is ‘a’” over {a, b} can be recognized by an NFA with n + 1 states (guess the position), but any equivalent DFA requires at least 2n states (subset construction can produce exponentially many reachable states).

More concretely: an NFA for “second-to-last symbol is ‘a’” needs 3 states; the minimal DFA needs 4 states.

(b) TRUE.

Proof: Every decider is also a recognizer. A decider for L halts on all inputs, accepting if w ∈ L and rejecting if w ∉ L. The accept behavior already satisfies the definition of a recognizer (accepts exactly the strings in L). Since decidable means “there exists a decider,” and every decider is a recognizer, decidable ⊆ Turing-recognizable. □

(c) FALSE.

Proof by pumping lemma: Suppose L = { anbn | n ≥ 0 } is regular. Let p be the pumping length. Take w = apbp ∈ L. Then w = xyz with |xy| ≤ p, |y| ≥ 1, so y = ak (k ≥ 1). Pumping: xy2z = ap+kbp ∉ L since p + k ≠ p. Contradiction. □

(d) FALSE.

Counterexample: ATMm ATM via the identity function (f(x) = x). ATM is undecidable, but ATM is Turing-recognizable.

The reduction A ≤m B with A undecidable proves that B is undecidable, not that B is unrecognizable. Undecidable and not-TR are different properties.

(a) False — (b) True — (c) False — (d) False
Score Yourself
QuestionTopicMaxYour Score
Q1aDFA — identify language1
Q1bDFA — product construction4
Q2Subset construction3
Q3Regular expression2
Q4Pumping lemma3
Q5aCFG ambiguity2
Q5bPDA construction3
Q6aTM configurations2
Q6bDecidability of L(M)2
Q7aProve TR2
Q7bProve undecidable4
Q7cProve not co-TR1
Q8aFill in M′1
Q8bWhat does it prove2
Q9aProve NP2
Q9bVC instance1
Q9cApply reduction2
Q9dCheck yes/no1
Q9eFind & fix flaw1
Q10aNFA → DFA states1
Q10bDecidable ⇒ TR2
Q10canbn regular?1
Q10dReduction ⇒ not TR?2
Total45