Final
Final
Exam
Solutions
n2 n2
For the term A, since 3 > e, 3 is always greater than e for all n. For the term B, we can pick
n0 = 528 such that
2 −1
n2 · 3n > 528n
holds for all n > n0 = 528. Hence, we can derive (2) to
2 2
4n > en + 528n
for all n > n0 = 528. Therefore,
2
f (n) = 2O(n ) .
Common mistake: Some directly wrote, for example,
2
22n ≥ 528n, ∀n ≥ 3,
but this is exactly what we want you to prove.
1
(b) Given a real number c > 0, we can use (2) to help us to show that
2 2 2 −1 2
en + 528n < c · (3n + n2 · 3n ) ≤ c · 4n , ∃n0 ∈ R, ∀n > n0 . (3)
ii) Copy all b into the second tape. After this step, the first and second tape’s head pointers are located
at the first c and the end, respectively.
2
iii) Compare the numbers of c and b, where the first and second tape’s head pointers move right and
left, respectively. If j = k, accept the string. Note that you may mark a compared b as x in the
second tape.
iv) In this step, we have known that j 6= k, so move both head pointers to the leftest location. Note
that we need to mark a traversed a as x to make sure we are in the leftest location.
v) Compare the numbers of a and b. Remember we have marked some b to x in the step (iii), and
have further marked all a to x in the step (iv). If i = j, accept the string.
vi) In the final, we focus on the special case j = 0.
Here is a (possibly incomplete) diagram of the TM:
t → t, S c → c, S
,
t → t, S t → t, S
b → b, R c → c, R
t → b, R b → x, L
b → b, R c → c, S t → t, S
a → a, R t → b, R t → t, L x → x, S
q1 q2 q3 qa
t → t, S
t → t, L c → c, L b → b, S
,
b → b, L x → x, L x → x, R t → t, S
,
b → b, R
a → x, L
b → b, L
c → c, L x → x, R
, , , x → x, R
b → b, L b → b, L b → b, L
q4 q5
a → x, L b → b, L c → c, L
, ,
x → x, L x → x, L x → x, L
x → x, R x → x, R
,
b → x, R x → x, R
In Problem 2, we assume the input order has been checked. For example,
ccbba
(a) (5 pts) Please use the above diagram to simulate (with details) the string
aabbccc.
For easy grading, please also tell us the total number of needed operations.
(b) (10 pts) Does the TM really handle the case j = 0? If yes, please prove it. If not, please fix the
diagram. You also need to explain the idea of your changes. For the diagram, you only need to
show the changes. Further, you are allowed introduce at most one additional state.
3
(c) (10 pts) What is the time complexity of this two-tape TM? You need to show the details.
(d) (5 pts) Suppose that we can only use one-tape TM to decide L. Can you roughly give an idea for a
new algorithm such that the time complexity is O(n log n)? Is this O(n log n) the best complexity
we can get? If yes, please show that your algorithm reaches the best big-O. If not, please prove
it. You can directly use the knowledge that you learned in this course. Please give the description
within roughly 100 words. Hint: L is not regular.
Solution.
q1 a a b b c c c a q1 a b b c c c a a q1 b b c c c a a b q2 b c c c
⇒ ⇒ ⇒
q1 t t t t t t t q1 t t t t t t t q1 t t t t t t t b q2 t t t t t t
a a b b q2 c c c a a b b q3 c c c a a b b c q3 c c a a b b c c q3 c
⇒ ⇒ ⇒ ⇒
b b q2 t t t t t b q3 b t t t t t q3 b x t t t t t q3 x x t t t t t
a a b b c q4 c c a a b b q4 c c c a a b q4 b c c c a a q4 b b c c c
⇒ ⇒ ⇒ ⇒
q4 x x t t t t t q4 x x t t t t t q4 x x t t t t t q4 x x t t t t t
a q4 a b b c c c q4 a x b b c c c q4 x x b b c c c x q5 x b b c c c
⇒ ⇒ ⇒ ⇒
q4 x x t t t t t q4 x x t t t t t q4 x x t t t t t x q5 x t t t t t
x x q5 b b c c c x x qa b b c c c
⇒ ⇒
x x q5 t t t t t x x qa t t t t t
• j = 0.
q1 → · · · → q1 → qa
• j > 0.
q1 → · · · → q1 → q2
For ac, the first setting goes wrong. Recall the main task at q1 is to handle ai . Thus we introduce
q0 to share the task at q1 . At q0 , we consider two cases.
• i > 0. We have q0 → q1 and run the original TM. A link to qa remains for handling the
situation of j = k = 0. Now ac is no longer accepted.
• i = 0. If j > 1, we have q0 → q2 and run the original TM. Otherwise, i = j = 0, so we have
links to qa .
4
q0 c → c, S t → t, S
,
t → t, S t → t, S
b → b, R
a → a, R t → b, R
t → t, S
b → b, R
a → a, R t → b, R
q1 q2 qa
t → t, S
b → b, R
t → b, R
t → t, S
t → t, S
to
b → b̃, R b → b̃, R
,
b → b, R x → x, R
in the self-loop of q4 , and change
b → b, S
t → t, S
to
b̃ → b̃, S
t → t, R
in the path q5 to qa . However, we have no time to correct the problem (this issue was reported at
12:50pm.) Also, although this issue is part of fixing the TM, we do not discuss it in the problem
statement.
Some students instead do
5
q0 c → c, S t → t, S
,
t → t, S t → t, S
a → a, R b → b, R
,
t → t, S t → b, R
b → b, R
a → a, R t → b, R
q1 q2 qa
t → t, S
b → b, R
t → b, R
t → t, S
t → t, S
ai b j c k .
Common mistake: You cannot just say that the diagram cannot handle the case of j = 0 without
giving reasons.
• j 6= 0. For the nodes q0 , q1 , q2 and q3 , they scan the input string one time. If j 6= k, this TM
will go back to the head of string by node q4 , which also traverses the string one time. In node
q5 , it checks whether i = j, so the steps are less than the string length. Overall, the steps we
need are less than three times of the input string length for deciding it, which is O(n).
• j = 0. The nodes q0 and q1 scan the input string at most one time to do the decision, which is
O(n).
(d) In the pages 7-9 of our slides “chap7 bigO2.pdf”, we learned how to design a one-tape TM that
decides the language {ai bj | i = j} with O(n log n). Thus, given a string ai bj ck , we can check
whether i = j with this O(n log n) algorithm first. If not, we restore all b to b, which requires
O(n). Then, check whether j = k with this O(n log n) algorithm again, and the total complexity is
still O(n log n). Moreover, because L is not regular, it implies “L cannot decide in o(n log n) on a
single-tape TM.” Therefore, O(n log n) is the best complexity that an algorithm for deciding L with
a single-tape TM can reach.
Problem 3 (25 pts). Let Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ·}, i.e., Σ contains all digits and the dot char-
acter. Consider the language
L = {0 · w | w ∈ {0, 1, . . . , 9}∗ }.
6
At first glance, L looks like to be the set of real numbers between 0 and 1 if we regard each element
in L as a floating point number. One student further proves that the set L is uncountable and gives a
proof using the diagonalization method:
Proof. Assume for contradiction that L is countable, i.e., there exists a correspondence f : N → L, say
where wi ∈ {0, 1, . . . , 9}∗ for all i ∈ N and wij ∈ {0, 1, . . . , 9} for all i ∈ N, 1 ≤ j ≤ |wi |. We construct
an element
0 · ŵ = 0 · ŵ1 ŵ2 . . . ŵn . . . ,
which is not paired with anything in N. This is done by choosing ŵi to be different from wii . If |wi | < i,
which means there’s no wii , we choose ŵi as 1. Besides, we avoid selecting the digits 0 or 9 when
constructing 0 · ŵ to make sure there’s no issue like 0 · 1999 . . . and 0 · 2000 . . .. With this construction,
it’s obvious that 0· ŵ 6= f (n) for all n ∈ N. Therefore, f is not a correspondence so that L is uncountable.
(a) (10 pts) We’ve learned that Σ∗ is countable, so it seems impossible that L, being a proper subset
of Σ∗ , is uncountable. Please point out the problem in the above proof with explanation. (Hint:
Think about the definition of the star operation.)
(b) (5 pts) Can you represent each element in L as a fraction number? Denote Lfrac to be the set of
real numbers whose decimal representations (in the string format) are in L. Please write Lfrac by a
general mathematical form. That is,
q
Lfrac = {s | s = , where p and q are certain values}.
p
(c) (10 pts) We’d like to show L is indeed countable by giving an explicit correspondence from N to L.
(Here N starts from 1.) This can be done by listing strings in L according to the length of strings
in ascending order. If two strings have the same length, list the one that represents smaller decimal
number first (e.g., “0 · 0” should be counted before “0 · 1”). Moreover, in our counting, “0 · 0” is
considered different from “0 · 00”, “0 · 000”, . . ., etc. Thus, each “0 · 0 . . . 0” should be counted. For
such a correspondence, which positive integer maps to the string “0 · 2022”? That is, which n such
that your f (n) is “0 · 2022”.
Solution.
(a) The problem is that the constructed ŵ has an infinite length. It doesn’t belong to
[
{0, 1, . . . , 9}∗ = {0, 1, . . . , 9}k .
k≥0
Common Mistake: You must point out where the proof goes wrong.
q
(b) L = {s | s = , where p = 10s for some s ∈ N and 0 ≤ q ≤ p − 1. }
p
7
(c) We have
f (1) = 0 · ,
f (2) = 0 · 0, f (3) = 0 · 1, . . . , f (11) = 0 · 9,
f (12) = 0 · 00, f (13) = 0 · 01, . . . , f (111) = 0 · 99,
f (112) = 0 · 000, f (113) = 0 · 001, . . . , f (1111) = 0 · 999,
f (1112) = 0 · 0000, . . . , f (3134) = 0 · 2022, . . .
Problem 4 (30 pts). Consider the following ID masking problem that is related to the student list
on our page of HW and exam scores.1
Let D be the set {0, 1, . . . , 9}. Suppose we have a set of distinct student IDs, all with length l:
We want to mask away some digits of every student ID, and the masked positions can be different
for each student. However, we only deal with masking I (1) in this problem. Once we know how to mask
I (1) , we can apply the same algorithm to every other student independently. Therefore, considering only
the masking of I (1) is adequate here.
Our goal is to keep only a few digits of I (1) , say m digits, and mask away the remaining digits.
When performing this masking, we need to make sure all other students’ IDs are different from I (1) in
at least one of these m digits. Otherwise, some students might mistakenly think that the masked I (1) is
themselves.
We can mask away the first and the last digit of I (1) so that it is displayed as
on the webpage. The student with ID I (1) can recognize it as himself since the second digit matches his
ID. On the other hand, students with ID I (2) or I (3) know that it’s not them since their second digit is
not 1.
Let us restate our requirements more formally with the following definition. We will use Ij to denote
the jth digit of a student ID I.
Definition 1. Given a set of IDs S and an ID I˜ ∈ S. We say that student I˜ can be uniquely identified
with m digits if there exists a set of indices U ⊆ {1, 2, . . . , l} satisfying
1. |U | = m
˜ we have I˜j =
2. For all I ∈ S \ {I}, 6 Ij for some j ∈ U .
1
https://www.csie.ntu.edu.tw/˜cjlin/courses/comptheory2022/scores.html
8
Let us repeat Example 1 but with rigorous definitions.
Example 2. With the same student IDs from Example 1, we choose U = {2} with |U | = 1, so we have
(2) (1) (3) (1)
I2 6= I2 and I2 6= I2 . Therefore, I (1) can be uniquely identified with 1 digit.
(a) (10 pts) Let us examine another example to familiarize ourselves with the problem. Assume we have
these student IDs:
Show that we can pick an I (1) ∈ D2 such that the selected I (1) cannot be uniquely identified with
1 digit but can be uniquely identified with 2 digits. Specifically, to prove your result you need to
select an I (1) and apply Definition 1 in your arguments like what we did in Example 2.
(b) We can further formally formulate the problem with the following language:
Notice that in our definition, the ID length l and the number of students C are not constants, but
a variable that varies between strings in L.
We are going to design a polynomial time verifier V in subproblems (b1) and (b2) to show that
L ∈ NP. To discuss the algorithm design, let us make the following assumptions:
• Each ID I (i) is a string in {0, 1}l . The same result should hold for general values as in sub-
problem (a), but we make this assumption for simplicity.
• Each ID I (i) is represented on the tape of TM by placing each digit sequentially in the tape
cells.
• To represent m on the tape, we fill m tape cells with the tape symbol 1.
• To make the algorithm design easy, we further assume to have some marker symbols between,
before and after the representations of I (i) and m.
(1) (5 pts) Design the certificate c to be used by your verifier. You are required to represent c as a
string that is not longer than l cells (recall that l is the length of IDs).
(2) (10 pts) Write down your verifier algorithm in clear steps of high-level descriptions. Then,
argue that your algorithm runs in polynomial time by giving the time complexity in big-O for
each step of your algorithm (giving some arbitrarily large order is deemed incorrect). The time
complexity should be calculated by running the algorithm on a single-tape TM and expressed
in terms of the input length n = |w|, where w is the input string of the TM:
You can ignore checking the input format of w. Note that the verifier runs on hw, ci
9
(c) (5 pts) Show that L ∈ NP again, but by designing an NTM that uses the verifier V in subproblem
(b2) as a subroutine. Show that it decides L in polynomial time by giving the time complexity for
each step. Note that you need to also consider the generation of the certificate.
Solution.
(a) We can choose I (1) so that each of its digits is the same as some other IDs. For example, let
I (1) = 56
I (2) = 55
I (3) = 66.
U = {1} or U = {2}.
(1) (2) (1) (3)
If U = {1}, we have I1 = 5 = I1 . If U = {2}, we have I2 = 6 = I2 . In both cases, I (1) is not
uniquely identified so other students may confuse the masked I (1) with their own ID. It is clear that
(1) (2) (1) (3)
I (1) can be uniquely identified with 2 digits. Let U = {1, 2}. We have I2 6= I2 and I1 6= I1 .
Common mistake: You need to show that it can be uniquely identified with 2 digits.
(b) (1) According to Definition 1, there exists a set U iff I (1) can be uniquely identified. Therefore, we
can use the set U as the certificate. To represent it as a string, an intuitive way is to use a
string of 0s and 1s: (
1 i∈U
c = c1 c2 . . . cl where ci =
0 i∈ /U
This certificate is of the same length as I (1) . Therefore, it has a length of O(n) where n is the
length of the input w.
Note: Some try to directly use U as the certificate. Then, U ⊆ {1, 2, . . . , l} and l is a variable.
However, our Σ is finite.
(2) Our verification works according to Definition 1. We loop over each position in U and make
sure that every other student is different from I (1) in one of these positions.
On input hw, ci:
i. Count the number of 1s in c to make sure that it is equal to m. Recall that we represented
m as m cells of 1s on the tape. Therefore, we can apply the algorithm from page 7 of lecture
slide “chap7 bigO2.pdf” to make sure the number of 1s in c is equal to m. We only have
to slightly modify it to ignore the marker symbols and 0s in c. Therefore, this step can be
done in O(n log n).
ii. Go through the certificate c and perform the following step for each cj (repeats |c| = O(n)
times):
• Go through each I (i) and mark it if cj = 1 and it is different from I (1) in the jth position.
This can be done in O(n).
This step takes O(n2 ) in total.
iii. Check whether I (2) , I (3) , . . . , I (C) are all marked. If yes, accept. Otherwise, reject. This can
be done in O(n).
Overall, the verification can be done in O(n2 ).
10
(c) On input hwi:
i. Nondeterministically generate a certificate c ∈ {0, 1}l . Because l is not a constant, we can use
I (C) as a reference. For each cj being generated, we mark the jth digit of I (C) . After all digits
of I (C) are marked we know that c has been generated. Going back and forth between I (C) and
c takes O(n) and we have to perform it for l = O(n) times. Therefore, this step takes O(n2 ).
ii. Run the verifier V on hw, ci. From subproblem (b2), this step takes O(n2 ).
11