Sie sind auf Seite 1von 52

The Class P Robb T.

Koether Homework Review Introduction Comparison of Run Times


Polynomial Time Exponential Time Factorial Time

The Class P
Lecture 37 (38) Section 7.2 Robb T. Koether
Hampden-Sydney College

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Fri, Nov 21, 2008 (Mon, Nov 24, 2008)

Outline
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

1 2 3

Homework Review Introduction Comparison of Run Times Polynomial Time Exponential Time Factorial Time The Class P The PATH Problem The CFL Membership Problem Assignment

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Homework Review
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Exercise 7.1, page 294. Answer each part TRUE or FALSE. (a) 2n = O(n). (b) n2 = O(n). (c) n2 = O(n log2 n). (d) n log n = O(n2 ). (e) 3n = 2O(n) . (f) 22 = O(22 ).
n n

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Homework Review
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Solution (a) TRUE. (b) FALSE. (c) FALSE. (d) TRUE. (e) TRUE. (f) TRUE.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Homework Review
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Solution The only part that is not pretty clear is part (e). 2O(n) means 2 raised to a function that is itself O(n). For example, 25n+3 is 2O(n) . In part (e), use the fact that 3 = 2log2 3 . It follows that 3n = 2(log2 3)n , so it is 2O(n) .

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Polynomial-Time Algorithms
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Programs that run in polynomial time are considered feasible. For example, sorting long lists is feasible. Programs that run in exponential time are considered infeasible. For example, factoring large integers is infeasible.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Polynomial-Time Algorithms
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Programs that run in polynomial time are considered feasible. For example, sorting long lists is feasible. Programs that run in exponential time are considered infeasible. For example, factoring large integers is infeasible.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Polynomial-Time Algorithms
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Programs that run in polynomial time are considered feasible. For example, sorting long lists is feasible. Programs that run in exponential time are considered infeasible. For example, factoring large integers is infeasible.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Polynomial-Time Algorithms
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Programs that run in polynomial time are considered feasible. For example, sorting long lists is feasible. Programs that run in exponential time are considered infeasible. For example, factoring large integers is infeasible.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Comparison of Run Times


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (Polynomial Time) Suppose we have Turing machines M1 , M2 , and M3 whose run times are O(n), O(n2 ), and O(n3 ), respectively. Suppose that each one takes 1 s when the input size is n = 1000.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Comparison of Run Times


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (Polynomial Time) Input size 1000 104 105 106 107 108 109 M1 1 s 10 s 100 s 1 ms 10 ms 100 ms 1s M2 1 s 100 s 10 ms 1s 100 s 2.8 h 12 d M3 1 s 1 ms 1s 1000 s 12 d 32 y 32000 y

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Comparison of Run Times


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (Exponential Time) Suppose we have Turing machines M4 and M5 whose run times are O(2n ) and O(4n ), respectively. Suppose that each one takes 1 s when the input size is n = 1000.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Comparison of Run Times


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (Exponential Time) Input size 1000 1010 1020 1030 1040 1050 1060 M3 1.03 s 1.06 s 1.09 s 1.12 s 1.15 s 1.18 s 1.21 s M4 1 s 1.02 ms 1.05 s 1073 s 13 d 36 y 37, 000 y M5 1 s 1.05 s 13 d 37, 000 y 38 109 y 40 1015 y 42 1021 y

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Comparison of Run Times


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (Factorial Time) Finally, suppose we have a Turing machine M6 whose run time is O(n!) . Suppose that it takes 1 s when the input size is n = 1000.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Comparison of Run Times


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (Factorial Time) Input size 1000 1001 1002 1003 1004 1005 1006 M5 1 s 4 s 16 s 64 s 256 s 1.024 ms 4.096 ms M6 1 s 1.001 ms 1.003 s 16.8 m 11.7 d 32.2 y 32, 381 y

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The Class P
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Denition (Class P) The class P is the class of all languages (i.e., problems) that are decidable in polynomial time by a deterministic single-tape Turing machine.

The Class P
The PATH Problem The CFL Membership Problem

P=
k =0

TIME(nk ).

Assignment

The Class P
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

We have seen (will see) that if a problem can be decided by a multitape Turing machine in polynomial time O(t(n)), then it can be decided by a single-tape Turing machine in time O(t2 (n)), which is still polynomial. However, we have also seen (will also see) that a problem that can be solved nondeterministically in polynomial time can be solved deterministically in exponential time, but not necessarily in polynomial time. Factoring an integer is an example (as far as we know).

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Polynomial Time
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Lemma If p(x) and q(x) are polynomials, then p(q(x)) is a polynomial. Thus, if a polynomial-number of steps can each be performed in polynomial time, then the entire process can be done in polynomial time.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

The PATH Problem Given a directed graph G and two vertices s and t, does there exist a directed path in G from s to t?

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Let G be the graph. Let m be the number of vertices in G. Let e(i, j) be true if there is an edge from vertex i to vertex j and false otherwise. Let s and t be the starting and ending vertices.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Begin at s and let M = {s}. Add to M all vertices that are adjacent to s (one edge away). Next, add to M all vertices that are adjacent to those vertices that are already in M . Repeat the previous step until no new vertices are added to M . See whether t M .

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Is t M ? No.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Is t M ? No.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Is t M ? No.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Is t M ? No.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Is t M ? Yes.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Is t M ? No.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Is t M ? No.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Is t M ? No.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Is t M ? No.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The PATH Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) Is t M ? No.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

An Algorithm for PATH


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem)


PATH(graph G, vertex s, vertex t) { M = {s}; M_old = {}; while (M != M_old) { M_old = M; for (i = 0; i < m; i++) for (j = 0; j < m; j++) if (e(i, j) E && i M && j / M) M = M {j}; } if (t M) accept(); else reject(); }

The Class P
The PATH Problem The CFL Membership Problem

Assignment

An Algorithm for PATH


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The PATH Problem) We can analyze each step in the function PATH(). Each step is done in polynomial time. Thus, PATH P.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The CFL Membership Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

The CFL Membership Problem Given a context-free language L and a word w, is w in L? We may assume that we have a grammar for L in Chomsky Normal Form. Let m be the length of the word w.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The CFL Membership Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) Earlier we saw an algorithm for testing whether a word w is derivable from a CNF grammar G. The algorithm pursued every possible derivation of length 2m 1. There problem is, the number of such derivations may be exponential in m. (Why?) We need a different algorithm.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The CFL Membership Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) Our strategy will be to begin with all one-symbol substrings of w that are derivable from G. These come directly from the rules of the form A a. Then proceed inductively.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The CFL Membership Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) Let w = x1 x2 x3 . . . xm . Set up a table of size m m. For all i j, table entry (i, j) is the set of all variables A in G from which the substring xi . . . xj is derivable. For example,
Table entry (1, 1) = {A V | A = x1 }. Table entry (3, 5) = {A V | A = x3 x4 x5 }. Table entry (1, m) = {A V | A = w}.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The CFL Membership Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) Basic step:


Initialize table entries (i, i) to the set of all variables A for which there is a rule A xi .

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Example
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) Let the grammar be S SS | aSb | bSa | In CNF it is S0 SS | AX | YA | AB | BA | S SS | AX | YA | AB | BA X SB Y BS A a B b

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Example
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) For the word w = abbaba,

A B B A B A

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The CFL Membership Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) Inductive step:


For each j 1, for each i < m, for each rule A BC, and for each k from i to i + j 1,
See whether B is in table entry (i, k) and C is in table entry (k + 1, i + j).

The Class P
The PATH Problem The CFL Membership Problem

If so, then add A to table entry (i, i + j).

Assignment

The idea is that if B = xi . . . xk and C = xk+1 . . . xi+j , then A BC = xi . . . xi+j .

Example
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) For example, when j = 1, consider the case where i = 1. In this case, i + j = 2, so k must equal 1. Table entry (1, 1) = {A} and table entry (2, 2) = {B}. There are rules S0 AB and S AB. So table entry (1, 2) = {S0 , S}. That is, S0 = ab and S = ab.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Example
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) For j = 1,

S0, S B B S0, S A S0, S B S0, S A

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Example
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) When j = 2, consider the case where i = 1. In this case, i + j = 3, so k may equal 1 or 2. Case k = 1:
Table entry (1, 1) = {A} and table entry (2, 3) = . This produces nothing.

The Class P
The PATH Problem The CFL Membership Problem

Case k = 2:
Table entry (1, 2) = {S0 , S} and table entry (3, 3) = {B}. There is the rule X SB.

Assignment

So table entry (1, 3) = {X }. That is, X = abb.


2

Example
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) For j = 2,

S0, S B

X B Y S0, S A X, Y S0, S B S0, S A

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Example
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) For j = 3,

S0, S B

X B

S0, S Y S0, S A X, Y S0, S B S0, S S0, S A

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Example
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) For j = 4,

S0, S B

X B

S0, S Y S0, S A

X X, Y S0, S B Y S0, S S0, S A

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Example
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) For j = 5,

S0, S B

X B

S0, S Y S0, S A

X X, Y S0, S B

S0, S Y S0, S S0, S A

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The CFL Membership Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) Finally, see whether the start symbol is in table entry (1, m). If it is, then accept w. If it is not, the reject w. In the example, the start symbol S0 is in table entry (1, 6).

The Class P
The PATH Problem The CFL Membership Problem

Assignment

An Algorithm for CFL


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem)


CFLMember(graph G, string w) { for (int i = 1; i <= m; i++) if ((A w[i]) R) Add A to table[i, i]; for (int len = 2; len <= m; len++) for (int i = 1; i <= m - len + 1; i++) { int j = i + len - 1; for (int k = i; k <= j - 1; k++) for each ((A BC) R) if (B table[i, k] && C table[k+1, j]) Add A to table[i, j]; } if (S table[1, m]) accept(); else reject(); }

The Class P
The PATH Problem The CFL Membership Problem

Assignment

The CFL Membership Problem


The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Example (The CFL Membership Problem) We can analyze each step in the function CFLMember(). Each step is done in polynomial time. Thus, CFL P.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Assignment
The Class P Robb T. Koether Homework Review Introduction Comparison of Run Times
Polynomial Time Exponential Time Factorial Time

Homework Read Section 7.2, page256 - 263. Exercises 1, 2, 3, 4, 9, pages 294 - 295. Problem 12, page 295.

The Class P
The PATH Problem The CFL Membership Problem

Assignment

Das könnte Ihnen auch gefallen