Assignment 7 Math 335: Computability and Complexity (1) Show using Theorem 28.15 (lecture notes 13) that if A ≤pm B and B ∈ N P , then A ∈ N P . (Yes, we showed this already using the definition of N P . Now do it using the theorem.) Solution. Since A ≤pm B, there is a function f with x ∈ A ⇐⇒ f (x) ∈ B, and f is computable in polynomial time. Since B ∈ N P , by Theorem 28.15, there is a polynomial time relation R(·, ·), and a polynomial p such that x ∈ B if and only if there is a y with |y| ≤ p(|x|) and R(x, y) holds. Let p1 be a polynomial which bounds the number of steps it takes to compute f , and let p2 be a polynomial which bounds the number of steps it takes to compute R. Define a new relation Q(·, ·) as follows: Q(x, y) ⇐⇒ R(f (x), y). First, we claim that Q is computable in polynomial time by the following algorithm: (a) Compute f (x) using the polynomial time algorithm for f . (b) Decide R(f (x), y) using the polynomial time algorithm for R. (c) If R(f (x), y) holds, then Q(x, y) holds. If R(f (x), y) does not hold, then Q(x, y) does not hold. The first step runs in at most p1 (|x|) steps, by assumption. The second step runs in at most p2 (|f (x)|, |y|). But |f (x)| is itself bounded by p1 (|x|), because we can only write down one symbol of output per step. So the second step runs in at most p2 (p1 (|x|), |y|) steps. So the entire algorithm runs in p1 (|x|) + p2 (p1 (|x|), |y|) steps, and a polynomial of polynomials is a polynomial. Let r(n) = p(p1 (n)). Then if x ∈ A, we know that f (x) ∈ B, so there is a y with |y| ≤ p(|f (x)|) ≤ p(p1 (|x|)) = r(|x|) and R(f (x), y) holds, meaning Q(x, y) holds. Conversely, if there is a y with |y| < r(|x|) and Q(x, y) holds, then R(f (x), y) holds, and |y| < p(p1 (|x|)), so f (x) ∈ B, so x ∈ A. Thus Q and r show that A ∈ N P via Theorem 28.15. (2) Make a deterministic single-tape Turing machine that takes as input a string of 0s and 1s, and sorts the string so that all the 1s are at the front and 0s at the back. Show that your machine runs in O(|x|2 ) time. (If it doesn’t, find a machine that does.) Solution. Our machine will work by running through the input, and every time it sees a 0 followed by a 1, it swaps them. Then it returns to the beginning and repeats. When it’s returning to the beginning, if it doesn’t see a 1 to the right of a 0, the string is now in sorted order, so it halts. (This is bubble sort, for those who are familiar.) 0/1 0/R 1/R 0/R s B/L 1/0 B/L 0/L B/R B/R 1/L 0/L 1/L 0/L 0/L h B/R 1/L Now, to argue that this runs in O(|x|2 ) time. Each swap requires 4 steps: turn the 1 into a 0; take a step to the left; turn the 0 into a 1; take a step back to the right. Each time we pass from left to right through the string, we perform at most |x| swaps. So a single pass from left to right takes at most |x| + 4|x| = 5|x| steps (walk through the entire input, and swap each bit with the previous bit). Of course, a pass back from right to left takes |x| steps. So we need to argue that we make no more than |x| passes through the string. Consider what happens when the machine finds a 0 with a 1 immediately after it. First, it swaps those two bits. Then it moves on, so if the next bit is another 1, it performs another swap. So it will keep moving that 0 to the right until it encounters another 0. So in the first pass, the last 0 in the string is moved to the very last position (if it wasn’t already there). Then in the second pass, the second to last 0 in the string is moved to the second to last position (if it wasn’t already there). So after the kth pass, k of the 0s have been moved all the way to the right. Since there are at most |x| 0s, we know that after |x| passes, all the 0s will be moved to the far right, so the string is now sorted. So the machine makes at most |x| passes, and each pass takes at most 6|x| steps (5|x| to move from left to right, and then another |x| to move back from right to left), for a total of 6|x|2 steps, 6|x|2 ∈ O(|x|2 ). (3) Make a deterministic Turing machine with separate input and output tapes that takes as input a string of 0s and 1s, and outputs the sorted version of the string so that the 1s are at the front and 0s at the back. Show that your machine runs in O(|x|) time. (If it doesn’t, find a machine that does.) Solution. This machine will scan through the input from left to right, and every time it sees a 1, it writes another 1 on the output. Then it scans back from right to left, and every time it sees a 0, it writes another 0 on the output. Then it halts. 1,B/L,B 0,B/0,0 0,0/L,L 0,B/R,B 1,B/1,1 1,1/R,R s B, B/L, B B, B/R, L h Every time it sees a 1 while moving from left to right, it takes an additional step to write a 1 on the output tape. So it takes at most 2|x| steps to pass from left to right, if it sees a 1 at every bit. Similarly, it takes at most 2|x| steps to pass from right to left. So the total number of steps is bounded by 4|x|, which is in O(|x|). (4) What is the missing part of the formula from the proof that SAT is N P -complete? Solution. We need to say that if M follows instruction I = hq, a, R, ri at step s, then our state was q, we changed to state r, we were looking at symbol a, and the head moved one to the left. ^ ^ As,I → Qs,q ∧ Qs+1,r ∧ [Hs,j → (Ss,j,a ∧ Hs,j+1 )] s j We also need to say that if M follows instruction I = hq, a, L, ri at step s, then our state was q, we changed to state r, we were looking at symbol a and the head moved one to the right. ^ ^ As,I → Qs,q ∧ Qs+1,r ∧ [Hs,j → (Ss,j,a ∧ Hs,j−1 )] s j 2
© Copyright 2026 Paperzz