Tutorial 3, Design and Analysis of Algorithms, 2024
Tutorial 3, Design and Analysis of Algorithms, 2024
1. The following DTM computes a function f : N → N. Find the function f and the average
time-complexity of the DTM. For computing f (x), the DTM is started in the initial state q0 ,
scanning the start symbol ▷, which precedes x written in binary. qh is the halting state.
δ (q0 , ▷) = (q1 , ▷, R)
δ (q1 , 0) = (q2 , 0, R)
δ (q1 , 1) = (q1 , 1, R)
δ (q1 , B) = (q3 , 0, L)
δ (q2 , 0) = (q2 , 0, R)
δ (q2 , 1) = (q2 , 1, R)
δ (q2 , B) = (q4 , B, L)
δ (q3 , 1) = (q3 , 0, L)
δ (q3 , ▷) = (q5 , ▷, R)
δ (q4 , 0) = (qh , 1, L)
δ (q4 , 1) = (q4 , 0, L)
δ (q5 , 0) = (qh , 1, L)
2. Let LOOKUP denote the following function: on input a pair < x, i > (where x is a binary
string and i is a natural number), LOOKUP outputs the ith bit of x or 0 if |x| < i. Prove that
LOOKUP ∈ P.
3. Define a two dimensional TM to be a TM where its tape is an infinite grid (and the machine
can move not only Left and Right but also Up and Down). Show that for every (time con-
structible) T : N → N and every Boolean function f , if f can be computed in time T (n) using
a two-dimensional TM then f ∈ DTIME(T (n)2 ).
4. Define a RAM TM to be a TM that has random access memory. We formalize this as follows:
the machine has an infinite array A that is initialized to all blanks. It accesses this array as
follows. One of the machine’s work tapes is designated as the address tape. Also the machine
has two special alphabet symbols denoted by R and W and an additional state we denote by
qa . Whenever the machine enters qa , if its address tape contains [i]2 R (where [i]2 denotes the
binary representation of i) then the value A[i] is written in the cell next to the R symbol. If
its tape contains [i]2W σ (where σ is some symbol in the machine’s alphabet) then A[i] is set
to the value σ . Show that if a Boolean function f is computable within time T (n) (for some
time-constructible T ) by a RAM TM, then it is in DTIME (T (n)2 ).
5. Consider the following simple programming language. It has a single infinite array A of
elements in { 0, 1, B } (initialized to B) and a single integer variable i. A program in this
1
language contains a sequence of lines of the following form:
label : If A[i] equals σ then cmds.
Where σ ∈ { 0, 1, B } and cmds is a list of one or more of the following commands: (1) Set
A[i] to τ where τ ∈ { 0, 1, B }, (2) Goto label, (3) Increment i by one, (4) Decrement
i by one, and (5) Output b and halt, where b ∈ { 0, 1 }. A program is executed on an
input x ∈ { 0, 1 }n by placing the i’th bit of x in A[i] and then running the program following
the obvious semantics. Prove that for every functions f : { 0, 1 }∗ → { 0, 1 } and (time con-
structible) T : N → N, if f is computable in time T (n) by a program in this language, then
f ∈ DTIME(T (n)).
6. Prove that the following languages/decision problems on graphs are in P: (You may pick
either the adjacency matrix or adjacency list representation for graphs; it will not make a
difference. Can you see why?)
(a) CONNECTED - the set of all connected graphs. That is, G ∈ CONNECTED if every
pair of vertices u, v in G are connected by a path.
(b) TRIANGLEFREE - the set of all graphs that do not contain a triangle (i.e., a triplet u, v, w
of connected distinct vertices.
(c) BIPARTITE - the set of all bipartite graphs. That is, G ∈ BIPARTITE if the vertices of
G can be partitioned to two sets A, B such that all edges in G are from a vertex in A to a
vertex in B (there is no edge between two members of A or two members of B).
(d) TREE - the set of all trees. A graph is a tree if it is connected and contains no cycles.
Equivalently, a graph G is a tree if every two distinct vertices u, v in G are connected by
exactly one simple path (a path is simple if it has no repeated vertices).
7. Recall that normally we assume that numbers are represented as string using the binary basis.
log n
That is, a number n is represented by the sequence x0 , x1 , . . . , xlog n such that n = ∑i=0 xi 2i ,
where for each i ∈ [0. . log n] xi ∈ { 0, 1 }. However, we could have used other encoding
schemes. If n ∈ N and b ≥ 2, then the representation of n in base b, denoted by [x]b is
obtained as follows: first represent n as a sequence of digits in { 0, . . . , b − 1 }, and then
replace each digit d ∈ [0. . b − 1] by its binary representation. The unary representation of n,
denoted by [n]1 is the string 1n (i.e., a sequence of n ones).
(a) Show that choosing a different base of representation will make no difference to the
class P. That is, show that for every subset S of the natural numbers, if we define
LSb = { [n]b : n ∈ S } then for every b ≥ 2, LSb ∈ P ⇔ LS2 ∈ P.
(b) Show that choosing the unary representation may make a difference by showing that the
following language is in P:
UNARYFACTORING = { < [n]1 , [l]1 , [k]1 >: there is a prime j ∈ (l, k) dividing n }.
8. For any language L, we define the language reverse(L) as the set of all strings of L in reverse
order:
reverse(L) = { w | reverse(w) ∈ L }.
2
Prove or disprove:
9. Prove that allowing the certificate to be of size at most p(|x|) (rather than equal to p(|x|)) in
the certificate definition of NP, makes no difference. That is, show that for every polynomial-
time DTM M and polynomial p : N → N, the language
is in NP.
10. Let LINEQ denote the set of satisfiable rational linear equations. That is, LINEQ consists of
the set of all pairs (A, b) where A is an m × n rational matrix and b is an m dimensional
rational vector, such that Ax = b for some n-dimensional vector x. Prove that LINEQ is in NP.