Mock Exam 3
Difficulty: ★★★☆☆ Medium · 45 pts · 10 questions
Cover

CSE2315 Automata, Languages and Computability. 10 open questions, 45 points total. Closed-book — one double-sided A4 handwritten cheatsheet allowed. No calculator. Assume P ≠ NP and NP ≠ EXP. Karp reductions denoted ≤P. Irrelevant information costs points.

Comparable to the actual Endterm 24-25. Requires solid understanding; some questions have non-obvious angles.

Q1 — 5 pts — DFA Product Construction

Consider the following two DFAs over Σ = {a, b}.

DFA D1: States {q0, q1}, start state q0, accept states F1 (to be determined).

L(D1) = {w ∈ {a, b}* | w contains an even number of a’s}.

DFA D2: States {p0, p1}, start state p0, accept states F2 = {p1}.

L(D2) = {w ∈ {a, b}* | w contains an odd number of b’s}.

(a) (1 pt) Give the set F1 for D1.

(b) (4 pts) Construct a DFA D such that L(D) = L(D1) ⊕ L(D2) (symmetric difference). Leave out unreachable states.

Solution

Part (a)

q0 tracks "even number of a’s seen." The start state q0 has seen zero a’s (even), so:

F1 = {q0}

Part (b) — Product construction

Symmetric difference: accept when exactly one of D1, D2 accepts (XOR).

Product states: (qi, pj). Start: (q0, p0).

Accept states F = {(qi, pj) | exactly one of qi ∈ F1, pj ∈ F2}.

  • (q0, p0): q0 ∈ F1 ✓, p0 ∉ F2 → exactly one → accept
  • (q0, p1): q0 ∈ F1 ✓, p1 ∈ F2 ✓ → both → reject
  • (q1, p0): q1 ∉ F1, p0 ∉ F2 → neither → reject
  • (q1, p1): q1 ∉ F1, p1 ∈ F2 ✓ → exactly one → accept
F = {(q0, p0), (q1, p1)}

Transition table:

Stateab
(q0, p0) ☆(q1, p0)(q0, p1)
(q1, p0)(q0, p0)(q1, p1)
(q0, p1)(q1, p1)(q0, p0)
(q1, p1) ☆(q0, p1)(q1, p0)

All 4 states are reachable from the start state. L(D) = "even #a’s XOR odd #b’s."

Q2 — 3 pts — GNFA State Elimination

Consider the NFA N over Σ = {0, 1} with states {q0, q1, q2}, start state q0, accept state q2.

Convert N to a GNFA G with L(G) = L(N) using Sipser’s procedure, then remove state q1. Show the resulting GNFA (all transitions with their regex labels).

Solution

Step 1 — Build GNFA

Add new start state qs with ε-transition to q0, and new accept state qa with ε-transition from q2.

GNFA transitions (before elimination):

  • qs → q0: ε
  • q0 → q0: 0
  • q0 → q1: 1
  • q1 → q1: 1
  • q1 → q2: 0
  • q2 → q2: 0
  • q2 → q0: 1
  • q2 → qa: ε

Step 2 — Eliminate q1

For each pair (qi, qj) where qi has an edge to q1 and q1 has an edge to qj:

Formula: R(qi, qj) = Rold(qi, qj) ∪ R(qi, q1) · R(q1, q1)* · R(q1, qj)

  • q1’s self-loop: 1
  • Into q1: q0 → q1 via "1"
  • Out of q1: q1 → q2 via "0"

Only affected pair: (q0, q2)

Old q0 → q2: ∅ (no direct edge)

New q0 → q2: ∅ ∪ 1 · 1* · 0 = 1·1*·0 = 1+0

Resulting GNFA

States: {qs, q0, q2, qa}. Transitions:

FromToLabel
qsq0ε
q0q00
q0q21+0
q2q20
q2q01
q2qaε
q0 → q2 gets label 1+0 (= 1·1*·0). All other transitions unchanged.

The path through q1 means: read one or more 1’s, then a 0 to reach q2.

Q3 — 2 pts — Regular Expression

Give a regular expression R over Σ = {0, 1} such that:

$$L(R) = \{w \in \{0,1\}^* \mid w \text{ does not contain } 110 \text{ as a substring}\}$$

Solution

Key insight

The substring "110" is forbidden. After seeing "11", the next character cannot be 0 — it must be another 1 (or the string ends). So once a run of two or more 1’s starts, only more 1’s can follow until the string ends.

Building the regex

The string consists of two phases:

  • Phase 1: Any sequence of "0" and "10" blocks — these can never create "110"
  • Phase 2: A trailing run of 1’s (possibly empty) — once we start a run of ≥2 ones, we can’t go back to 0
$$R = (0 \mid 10)^* \cdot 1^*$$

Verification

StringContains "110"?Matches R?✓?
εNoYes — empty (0|10)*, empty 1*
110YesNo — cannot produce "110"
111NoYes — empty first part, 1*=111
0101NoYes — (0)(10)(1)*=0·10·1
1100YesNo — "10" block can’t be followed by "0" then back to phase 1 with "11"
10100NoYes — (10)(10)(0)

The (0|10)* part ensures every 1 in phase 1 is immediately followed by 0 — so no two consecutive 1’s can occur before the final trailing block.

Q4 — 3 pts — Pumping Lemma

Let \(L = \{w \in \{a,b\}^* \mid w = w^R\}\) (the language of palindromes).

Use the pumping lemma to prove that L is not regular. Use the word \(w = a^p b a^p\).

Solution

Setup

Assume for contradiction that L is regular with pumping length p.

Choose \(w = a^p b a^p\). Then \(w \in L\) since \(w^R = a^p b a^p = w\), and \(|w| = 2p + 1 \ge p\).

Pumping

By the pumping lemma, \(w = xyz\) where:

  1. \(|xy| \le p\)
  2. \(|y| \ge 1\)
  3. \(xy^i z \in L\) for all \(i \ge 0\)

Since \(|xy| \le p\), both x and y lie entirely within the first block of a’s. So \(y = a^k\) for some \(k \ge 1\).

Pump down: i = 0

$$xy^0z = xz = a^{p-k} b a^p$$

Left side has \(p - k\) a’s before b. Right side has \(p\) a’s after b.

Since \(k \ge 1\), we have \(p - k < p\), so the string is not a palindrome.

\(a^{p-k}ba^p \notin L\) since \(p-k \ne p\). Contradiction with condition 3. ∴ L is not regular.
Q5 — 5 pts — CFG + PDA

Consider the CFG G = ({S}, {a, b, c}, R, S) with rules:

$$S \to aSa \mid bSb \mid c$$

(a) (2 pts) Is G ambiguous? Prove your answer.

(b) (3 pts) Give a PDA P with at most 6 states that recognizes L = \(\{wcw^R \mid w \in \{a, b\}^*\}\).

Solution

Part (a) — Ambiguity

G is not ambiguous.

Every word in L(G) has the form \(wcw^R\) where \(w \in \{a,b\}^*\). The derivation is uniquely forced:

  • The outermost symbols of the sentential form determine which rule applies
  • If the string starts with a (and ends with a), we must use S → aSa
  • If the string starts with b (and ends with b), we must use S → bSb
  • If the remaining string is just "c", we must use S → c
G is unambiguous. Each \(wcw^R\) has exactly one leftmost derivation — the first/last character at each step uniquely determines the production.

Part (b) — PDA

Strategy: push symbols until c is read, then pop matching symbols.

States: qstart, qpush, qpop, qacc.

StateInputStack topPushNext state
qstartεε$qpush
qpushaεaqpush
qpushbεbqpush
qpushcεεqpop
qpopaaεqpop
qpopbbεqpop
qpopε$εqacc
4 states: qstart, qpush, qpop, qacc. Push phase reads w and pushes each symbol. On reading c, switch to pop phase. Pop phase matches wR against the stack. Accept when stack marker $ is reached.
Q6 — 4 pts — Turing Machine

Consider TM M with input alphabet Σ = {a}, tape alphabet Γ = {a, ␣, X}, and transition function:

StateReadWriteMoveNext
q0aXRq1
q0Rqacc
q1aaRq1
q1Lq2
q2aLq3
q2XXRqrej
q3aaLq3
q3XXRq0

(a) (2 pts) Find a word w with |w| = 4 on which M accepts. Show the full sequence of TM configurations.

(b) (2 pts) Is L(M) decidable? What language does M recognize?

Solution

Understanding M

M operates in rounds. Each round:

  1. q0: Mark the leftmost a with X, go right
  2. q1: Scan right past all a’s to find the right end
  3. q2: Erase the rightmost a (replace with ␣), go left
  4. q3: Scan left past all a’s back to the X marker, restart

Each round removes a pair: one from the left (marked X) and one from the right (erased). Accepts when all a’s are consumed. Rejects if in q2 no a is left (only X), meaning odd number of a’s.

Part (a) — w = aaaa

#ConfigurationState
1aaaaq0
2Xaaaq1
3Xaaaq1
4Xaaaq1
5Xaaaq1
6Xaaaq2
7Xaa␣␣q3
8Xaa␣␣q3 (a→L)
9Xaa␣␣q3
10Xaa␣␣q0
11XXa␣␣q1
12XXaq1
13XXa␣␣q2
14XX␣␣␣q3
15XX␣␣q0
16acceptqacc
w = aaaa. M accepts after removing pairs (a1,a4) and (a2,a3).

Part (b)

L(M) = \(\{a^{2n} \mid n \ge 0\}\) = strings of a’s with even length (including ε).
Yes, L(M) is decidable. In fact, L(M) is regular — recognized by a 2-state DFA that counts a’s mod 2.
Q7 — 7 pts — Undecidability

Let \(L = \{\langle M \rangle \mid M \text{ is a TM and } L(M) \text{ is infinite}\}\).

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

(b) (4 pts) Prove that L is undecidable.

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

Solution

Part (a) — L is Turing-recognizable (2 pts)

Build a TM T that recognizes L using dovetailing:

T = “On input ⟨M⟩:

  1. For n = 1, 2, 3, …
  2.  Run M on strings s1, s2, …, sn for n steps each
  3.  Count the number of distinct strings accepted so far
  4.  If the count exceeds n, accept.”

More precisely: for each threshold k = 1, 2, …, dovetail until k distinct strings are found to be accepted. If L(M) is infinite, every threshold k is eventually met, so T accepts. If L(M) is finite, T never reaches a high enough threshold and loops (never accepts).

T accepts ⟨M⟩ iff L(M) is infinite. T may loop if L(M) is finite. ∴ L is TR.

Part (b) — L is undecidable (4 pts)

Reduce from ATM = {⟨M, w⟩ | M accepts w}.

Assume for contradiction that decider R decides L. Build decider S for ATM:

S = “On input ⟨M, w⟩:

  1. Construct TM M′ = “On input x: ignore x. Run M on w. If M accepts w, accept.”
  2. Run R on ⟨M′⟩.
  3. If R accepts, accept. If R rejects, reject.”

Correctness:

  • If M accepts w: M′ accepts every input → L(M′) = Σ* (infinite) → ⟨M′⟩ ∈ L → R accepts → S accepts.
  • If M does not accept w: M′ accepts nothing → L(M′) = ∅ (finite) → ⟨M′⟩ ∉ L → R rejects → S rejects.

So S decides ATM. Contradiction since ATM is undecidable.

ATMm L, so L is undecidable.

Part (c) — L is not co-TR (1 pt)

From (a), L is Turing-recognizable. If L were also co-Turing-recognizable, then L would be both TR and co-TR, which means L would be decidable (by running both recognizers in parallel). But (b) shows L is undecidable. Contradiction.

TR + undecidable → not co-TR.
Q8 — 3 pts — Fill-in Reduction

Consider the language:

$$L = \{\langle M \rangle \mid M \text{ accepts all strings of length } \le 5\}$$

We want a mapping reduction from ATM to L. The computable function F is:

F = “On input ⟨M, w⟩:

  1. Construct M′ = “On input x:   (a) …”
  2. Output ⟨M′⟩.”

(a) (1 pt) What should M′ do on input x? Complete the description.

(b) (2 pts) What does this reduction prove about L? What complexity class does L belong to (TR, co-TR, neither)?

Solution

Part (a)

M′ = “On input x:
 1. If |x| > 5, reject.
 2. Run M on w.
 3. If M accepts, accept.”

Correctness of the reduction

  • M accepts w → M′ accepts all x with |x| ≤ 5 (step 2 always reaches step 3, which accepts) → ⟨M′⟩ ∈ L
  • M does not accept w (rejects or loops) → M′ rejects or loops on all x with |x| ≤ 5 → M′ does not accept any string of length ≤ 5 → ⟨M′⟩ ∉ L

Part (b)

ATMm L, so L is undecidable.

For the recognizability class: L is co-Turing-recognizable.

To see this, we build a recognizer for the complement \(\overline{L}\) = {⟨M⟩ | ∃ string x with |x| ≤ 5 that M does not accept}:

“On input ⟨M⟩: Run M on all strings of length 0, 1, …, 5 in parallel (dovetail). There are finitely many such strings. If any run halts and rejects, accept.”

This recognizes \(\overline{L}\), so L is co-TR.

L is undecidable and co-TR. Since co-TR + undecidable → L is not Turing-recognizable.
Q9 — 7 pts — NP Scaffold

COLORFUL MATCHING

Instance: A bipartite graph G = (A ∪ B, E), a coloring c: E → {1, …, k}, and an integer m.

Question: Is there a matching M ⊆ E with |M| ≥ m such that no two edges in M have the same color?

(a) (2 pts) Prove that COLORFUL MATCHING ∈ NP.

(b) (1 pt) Give a yes-instance of Vertex Cover with |V| = 4, |E| = 4, K = 2.

(c) (2 pts) A student proposes the following reduction from Vertex Cover to COLORFUL MATCHING:

Given VC instance (G = (V, E), K): set A = V, B = E. Add edge (v, e) iff v is an endpoint of e. Color all edges color 1. Set m = K.

Apply this reduction to your VC instance from (b).

(d) (1 pt) Is the resulting COLORFUL MATCHING instance a yes-instance?

(e) (1 pt) Explain what is wrong with the reduction and suggest a fix.

Solution

Part (a) — COLORFUL MATCHING ∈ NP

Certificate: a set of edges M.

Verifier checks in polynomial time:

  1. |M| ≥ m
  2. M is a valid matching: no two edges in M share a vertex
  3. All edges in M have distinct colors: no two edges in M have the same color
Certificate = matching M. Verification is O(|M|²) for pairwise checks. ∴ COLORFUL MATCHING ∈ NP.

Part (b) — VC yes-instance

V = {v1, v2, v3, v4}, E = {(v1,v2), (v2,v3), (v3,v4), (v1,v3)}, K = 2.

Vertex cover: {v2, v3}. Every edge has at least one endpoint in {v2, v3}. ✓

Part (c) — Apply the reduction

Bipartite graph: Left side A = {v1, v2, v3, v4}, right side B = {e1, e2, e3, e4}.

Edges in the bipartite graph (vertex v connected to edge e iff v is an endpoint):

  • v1 — e1 (v1v2), v1 — e4 (v1v3)
  • v2 — e1 (v1v2), v2 — e2 (v2v3)
  • v3 — e2 (v2v3), v3 — e3 (v3v4), v3 — e4 (v1v3)
  • v4 — e3 (v3v4)

All bipartite edges are colored 1. m = K = 2.

Part (d)

No. Since all edges have color 1, any matching of size ≥ 2 contains two edges of the same color. A colorful matching can have at most 1 edge, but m = 2 requires 2.

Part (e) — The flaw and fix

Flaw: All edges are assigned the same color (color 1). This means the "colorful" constraint forbids any matching of size ≥ 2, regardless of the VC instance. The reduction always produces a no-instance (for K ≥ 2), so it cannot be correct.

Fix: Assign distinct colors to the bipartite edges. For instance, give each original edge ei its own color i. Then the edges (v, ei) and (v′, ei) both get color i. A colorful matching picks at most one edge per color (= per original edge), and at most one edge per left-vertex. A matching of size K then corresponds to K original edges, each covered by a distinct vertex — i.e., a vertex cover.

Alternatively: drop the color constraint entirely, reducing to standard bipartite matching. But this changes the target problem, so it’s a weaker fix.

Q10 — 6 pts — True / False

For each statement, determine whether it is true or false and provide a brief justification.

(a) (2 pts) If L1 and L2 are both Turing-recognizable, then L1 ∩ L2 is Turing-recognizable.

(b) (1 pt) Every regular language is in P.

(c) (2 pts) If A ≤P B and B ∈ NP, then A ∈ NP.

(d) (1 pt) ATMm HALTTM.

Solution

(a) True

Given recognizers T1 for L1 and T2 for L2, build recognizer T for L1 ∩ L2:

T = “On input w: Run T1 and T2 on w in parallel (dovetail). If both accept, accept.”

If w ∈ L1 ∩ L2: both eventually accept → T accepts. If w ∉ L1 ∩ L2: at least one never accepts → T never accepts.

True. TR is closed under intersection.

(b) True

A DFA processes input in a single left-to-right pass, reading each symbol once. Running time is O(n) where n = |w|. Since O(n) ⊂ O(nk) for k = 1, this is polynomial.

True. REG ⊂ P (DFA runs in O(n) time).

(c) True

A ≤P B means there exists a poly-time computable function f such that x ∈ A iff f(x) ∈ B.

Since B ∈ NP, there is a poly-time verifier VB for B.

Build verifier VA for A: “On input (x, c): compute f(x) in poly time. Run VB(f(x), c). Accept iff VB accepts.”

Total time: poly(|x|) for f + poly(|f(x)|) for VB = polynomial.

True. NP is closed under polynomial-time reductions.

(d) True

Construct mapping f: on input ⟨M, w⟩, output ⟨M′, w⟩ where:

M′ = “On input x: simulate M on x. If M accepts, accept. If M rejects, loop forever.”

  • M accepts w → M′ halts on w (accepts) → ⟨M′, w⟩ ∈ HALTTM
  • M rejects w → M′ loops on w → ⟨M′, w⟩ ∉ HALTTM
  • M loops on w → M′ loops on w → ⟨M′, w⟩ ∉ HALTTM
True. ATMm HALTTM via the reduction that converts rejections into loops.
Score Yourself
QuestionTopicPointsCorrect?
Q1aDFA Product — accept states1
Q1bDFA Product — symmetric difference4
Q2GNFA State Elimination3
Q3Regular Expression2
Q4Pumping Lemma3
Q5aCFG Ambiguity2
Q5bPDA Construction3
Q6aTM Trace2
Q6bTM Language2
Q7aTR proof2
Q7bUndecidability reduction4
Q7cNot co-TR1
Q8aFill-in — M′ description1
Q8bFill-in — conclusion2
Q9aNP membership2
Q9bVC instance1
Q9cApply reduction2
Q9dYes/No instance1
Q9eFix the flaw1
Q10aTR ∩ closure2
Q10bREG ⊂ P1
Q10cNP closed under ≤P2
Q10dATMm HALTTM1
Total/45