7.2 The Class P Into what groups does Decidability Theory divide the set of all problems? Into what groups does Complexity Theory divide the set of solvable problems? What's the class P? P is the class of languages decidable in Polynomial time on a deterministic single-tape TM How do Polynomial growth rates compare to Exponential growth rates? n = 1000 n^2 = 1000000 n^3 = 1000000000 n^4 = 1000000000000 2^n = decimal number with over 300 digits Why divide into Polynomial-time and Exponential-time problems? Why not divide into Linear-time and Quadratic-time problems? Polynomially Equivalent If a certain problem has a Polynomial-time solution on one kind of Deterministic computing model, what do you know about other types of reasonable Deterministic computing models? If you used some other Deterministic computing model could you expect to solve an Exponential problem in Polynomial time? Why ignore Polynomial differences in running time? Doesn't a O(n^3) solution use significantly more time than a O(n) solution? a cubic solution does require more time than a linear solution but both can be completed in reasonable time for reasonable sizes for now the focus is on tractable vs intractable Encoding Does the Encoding of a problem affect whether the problem is solvable in Polynomial-time vs Exponential-time? How would you encode a graph? a list of nodes and edges or an adjacency matrix Does the Encoding of a graph affect whether a problem is tractable? Some Encodings are Not Polynomially Equivalent What's a Unary encoding of a number? a number x is represented by x copies of a single symbol How does the size of a Unary encoding of a number compare to the size of a Binary (or decimal or base-k) encoding of the same number? Unary encoding of n requires O(n) bits Binary encoding of n requires O(log n) bits size of Unary encoding is exponentially larger The Class P What's the formal definition of the class P? P = union over k of TIME(n^k) How do you show a problem is in the class P? 1. describe a TM algorithm that solves the problem describe the algorithm as a series of lines 2. show the algorithm runs in polynomial time a. show a polynomial bound on the number of lines used b. show a polynomial bound on the steps used in each line The PATH Problem What's the PATH problem? PATH = { | G is directed graph with a path from s to t } What's a Brute-force Algorithm for the PATH problem? suppose n is the number of nodes in the graph generate all sequences of nodes of length n or less check each sequence to see if it's a valid path from s to t What kind of running-time does a Brute-force Algorithm typically have? What's the running-time of the Brute-force Algorithm for PATH? the number of sequences is n^n the algorithm uses exponential time PATH is in P What's a Polynomial-time algorithm for PATH? M = "On input , G is directed graph, s and t are nodes: 1. Mark node s. 2. Repeat line 3 until no additional nodes are marked. 3. Scan all the edges of G. If an edge (a,b) is found where a is marked and b is not marked, mark node b. 4. If t is marked, accept. Otherwise, reject." Show that M is a Polynomial-time algorithm. The RELPRIME Problem What does it mean for two numbers x and y to be Relatively Prime? 1 is the largest integer that evenly divides both x and y What's the RELPRIME problem? RELPRIME = { | x and y are relatively prime } What's a Brute-force algorithm for RELPRIME? try all possible divisors for both numbers accept if no divisor is greater than 1 What's the running time of the Brute-force algorithm? the magnitude of a number is exponential compared to the encoding RELPRIME is in P How do you show RELPRIME is in P? must be log-time in terms of magnitude of x and y What's the Greatest-Common-Divisor (GCD) of two numbers x and y? the largest integer that evenly divides both x and y Can you reduce the RELPRIME problem to the GCD problem? Given that you have a TM E that solves the GCD problem. R = "On input , x and y are natural numbers: 1. Run E on . 2. If the result is 1, accept. Otherwise, reject." Show that R is a polynomial-time algorithm. Euclid's Algorithm What's the Euclidean algorithm for the GCD problem? E = "On input , x and y are natural numbers: 1. Repeat until y = 0. 2. x = x mod y. 3. Swap x and y. 4. Output x." Show the steps of Euclid's algorithm on 48 and 21. Show that E is a polynomial-time algorithm. Lines 1 to 3 repeat at most the lesser of log2(x) or log2(y) x is reduced by at least half each time. x and y are swapped each time. Lines 1 to 4 are each linear in the number of bits in x and y. The CFL Problem What's a Brute-force algorithm for deciding a Context-Free Language L? L is generated by some CFG G in CNF derivation of a string w has 2n-1 steps (n is the length of w) generate all possible derivations with 2n-1 steps accept if any of the derivations is a derivation of w What's the running time of the brute-force algorithm? CFL is in P What's a Polynomial-time algorithm for deciding a Context-Free Language? D = "On input w = w1 ... wn: 1. If w = epsilon and S -> epsilon is a rule, accept. 2. For i = 1 to n (for each symbol in w) 3. For each variable A 4. If A -> w[i] is a rule 5. table[i,i] = A 6. For l = 2 to n (for each length) 7. For i = 1 to n-l+1 (for each start index) 8. j = i+l-1 (compute the end index) 9. For k = i to j-1 (for each split index) 10. For each rule A -> BC 11. If table[i,k] contains B and table[k+1,j] contains C put A in table[i,j] 12. If S is in table[1,n], accept. Otherwise, reject." Show the steps of D on the string 'aab' and the grammar below. S -> AB A -> AA | a B -> b Show that D is a polynomial-time algorithm.