* Time Complexity classes * The class P RUNNING TIME Quick review of O() and Omega() notation. TIME(t(n)) = {L | L is a language decided by an O(t(n)) time Turing machine} A = {w#w: w in {0,1}^*} On input string x: 1. Scan across the tape and reject if no # found. 2. Repeat until any character other than * and # remains. 3. Compare character-by-character the ith character in the first string and the ith character in the second string. 4. Use *s to note the characters already compared. This takes O(n^2) time since we need O(n) scans of O(n) characters. A is in Time(n^2). On a two-tape Turing machine, this can be done in O(n) time. Can we do with less time on a single-tape Turing machine? No. Theorem: The above language takes Omega(n^2) time to recognize using a single-tape Turing Machine. Proof: The proof is using an argument referred to as crossing sequences. The intuition is that in order to recognize the string w#w, the TM head will need to go back and forth Omega(n) times across the tape. When the tape head moves from one cell to the other, it carries only a constant amount of information with it. Consider checking whether a string w0^{n/4}#w0^{n/4} is in the language. The TM has to carry Omega(n) amount of information across Omega(n) cells, yielding the Omega(n^2) bound. How do we formalize this? Consider the first n/4 characters of the first half of the string. We need to compare these with the first n/4 characters of the second half of the string. Every time the tape head crosses between the two sides, only a constant number of characters can be conveyed. Each crossing costs Omega(n) steps. There being Omega(n) characters, we have Omega(n^2) time. Consider the language L_n = {w0^{n/4}#w0^{n/4} : w in {0,1}^*, |w| = n/4}. We will argue that a TM that recognizes this language needs Omega(n^2) time. Let c_i denote the sequence of states the TM goes through as it crosses cell number i. Thus c_i = q_1q_2q_3...q_k; q_i is the state when the head arrives at cell i, q_2 is the state when the head arrives next at cell i, and q_j is the state when the head arrives the jth time at cell i. For a given string x of length n+1, define C(x) as follows. C(x) = {c_i: n/4 < i <= n/2} Lemma: For any x and y in L_n, C(x) and C(y) are disjoint. Proof of Lemma: Suppose not. We have two strings x and y of length n+1, for which c_i(x) = c_j(y). Let x' be the prefix consisting of the first i symbols of x, and let y' be the suffix of y consisting of the last n-j symbols of y. Then, the outcome of the the TM on the string x'y' is identical to the outcome of the TM on x and y. But x'y' is not in the language since x is not equal to y. End Proof of Lemma The number of strings in L_n = 2^{n/4}. Let m_x be the length of the shortest crossing sequence in C(x). Let m be the max of all the m_x's. Then, if Q is the set of states of the TM, then we have: Sum_{i = 0}^m |Q|^i >= 2^{n/4} The above is true if |Q|^{m+1} >= (|Q| - 1)^{2^{n/4}}. Since |Q| is constant, m = Omega(n). So there is some string x such that m_x = Omega(n). So all crossing sequences for this string is Omega(n), which means that the tape head crosses Omega(n) cells Omega(n) times, yielding an Omega(n^2) bound. End Proof of Theorem Any language that can be decided in o(n log(n)) time on a single-tape Turing machine is regular. SIMULATING MULTI-TAPE TM BY SINGLE-TAPE TM Theorem: Let t(n) be a function, t(n) >= n. Then every t(n) time multitape Turing machine has an equivalent O(t(n)^2) single-tape TM. Proof: This proof follows the simulation we had done for proving the equivalence of single-tape and multi-tape TMs. Here we repeat that simulation. Given multi-tape TM M, we will simulate using a single-tape TM S. Suppose M has k tapes. We will have the tape of S contain the first tape of M followed by #, then the second tape of M followed by #, then the third tape, and so on. To indicate the current location of the head, we will replace the tape alphabet Gamma with the tape alphabet Gamma union Gamma', which contains a "primed" version of each symbol of Gamma. Initial tape contents are #w_1w_2...w_n#u#u#...# . To simulate a single move, S scans the tape from the first # to the last # and reads the current symbols that the k heads of M are pointing to -- memorizing the symbols using a state, then apply the transition function of M, and finally make a second sweep implementing the transition of M. So for one step of the multi-tape TM, we need two sweeps of the tape. If the total active portion of the k tapes at step i is L_i, this will take time O(L_i). If, at any point, S moves one of the virtual heads to the right onto a #, then we need to extend this tape by shifting the contents right by 1 and placing a blank symbol. This also takes time O(L_i) at step i of the multi-tape machine. The initial configuration requires O(n) time. So the total time is the sum, over all i from 1 to t(n), O(L_i). The multi-tape machine takes t(n) steps, so each tape length could never exceed t(n), implying that L_i is at most kt(n). So the total time taken over all is: O(kt(n)^2 + n) = O(t(n)^2). End Proof SIMULATING NONDETERMINISTIC TM BY DETERMINISTIC TM Remember that a NDTM is a decider if all branches of its computation halt. The running time of a nondeterministic Turing Machine that is a decider is the maximum number of steps that N uses on any branch of its computation on any input of length n. Theorem: Let t(n) be a function, t(n) >= n. Then every t(n) time nondeterministic Turing machine has an equivalent 2^O(t(n)) time deterministic single-tape TM. Proof: Again we follow the simulation we have done earlier closely. First we simulate using a deterministic 3-tape machine, then we simulate the 3-tape machine using a single-tape TM. The first tape just contains the input string. The second tape is the simulation tape which will simulate one branch of the NDTM computation tree. The third tape lists the current simulation branch. Consider the computation tree of the NDTM. The root of the tree is the starting configuration. The children of a configuration C are all the configurations that C can possibily yield in one step of NDTM. This is finite, say b. Thus, the max-degree of the computation tree is b. We number the child configurations of a configuration arbitrarily from 1 to at most b. Now any (partial) computation can be specified by a string over the alphabet {1,2,...,b}. Some strings over this alphabet may not correspond to any node in the computation tree. D proceeds as follows. 1. Initially tape 1 contains the input w, and tapes 2 and 3 are empty. 2. Copy tape 1 to tape 2. Time = O(n). 3. Use tape 2 to simulate N with input w on one branch of its computation tree. Before each step of N, consult the next symbol on tape 3 to determine which of the nondeterministic choices to make. If no more symbols remain on tape 3 or this choice is invalid or N goes to reject state then go to step 4. If N goes to accept state, then move to accept and halt. Time taken is O(length of the branch). 4. Replace the string on tape 3 by its lexciographic successor. Go to step 2. Time taken is the length of the branch. The number of times the loop 3-4 is done is O(b^{t(n)}). So the total simulation time by 3-tape machine is O(t(n)b^{t(n)}) = 2^(O(t(n))). To simulate with a single-tape, we need to square this to obtain 2^(O(t(n))). End Proof THE CLASS P P = U_k TIME(n^k)