Powerpoint exploring the locations used in television show Time Clash
Lecture%2038%20 %20 the%20class%20p
1. The Class P
Robb T.
Koether
Homework
Review
The Class P
Introduction Lecture 37 (38)
Comparison Section 7.2
of Run Times
Polynomial Time
Exponential Time
Factorial Time
The Class P Robb T. Koether
The PATH Problem
The CFL
Membership Problem Hampden-Sydney College
Assignment
Fri, Nov 21, 2008
(Mon, Nov 24, 2008)
2. Outline
The Class P
Robb T.
Koether
1 Homework Review
Homework
Review
2 Introduction
Introduction
Comparison
3 Comparison of Run Times
of Run Times Polynomial Time
Polynomial Time
Exponential Time Exponential Time
Factorial Time
The Class P
Factorial Time
The PATH Problem
The CFL
Membership Problem 4 The Class P
Assignment The PATH Problem
The CFL Membership Problem
5 Assignment
3. Homework Review
The Class P
Robb T.
Koether
Homework Exercise 7.1, page 294.
Review
Introduction
Answer each part TRUE or FALSE.
Comparison (a) 2n = O(n).
of Run Times
Polynomial Time
Exponential Time
(b) n2 = O(n).
(c) n2 = O(n log2 n).
Factorial Time
The Class P
The PATH Problem
The CFL
(d) n log n = O(n2 ).
Membership Problem
Assignment
(e) 3n = 2O(n) .
n n
(f) 22 = O(22 ).
4. Homework Review
The Class P
Robb T.
Koether
Homework
Review Solution
Introduction
(a) TRUE.
Comparison
of Run Times (b) FALSE.
Polynomial Time
Exponential Time
Factorial Time
(c) FALSE.
The Class P (d) TRUE.
The PATH Problem
The CFL
Membership Problem
(e) TRUE.
Assignment (f) TRUE.
5. Homework Review
The Class P
Robb T.
Koether
Homework
Review
Solution
Introduction
Comparison The only part that is not pretty clear is part (e).
of Run Times
Polynomial Time 2O(n) means 2 raised to a function that is itself O(n).
Exponential Time
Factorial Time
For example, 25n+3 is 2O(n) .
The Class P
The PATH Problem In part (e), use the fact that 3 = 2log2 3 .
The CFL
Membership Problem
It follows that 3n = 2(log2 3)n , so it is 2O(n) .
Assignment
6. Polynomial-Time Algorithms
The Class P
Robb T.
Koether
Homework
Review
Introduction
Programs that run in polynomial time are considered
Comparison feasible.
of Run Times
Polynomial Time For example, sorting long lists is feasible.
Exponential Time
Factorial Time
Programs that run in exponential time are considered
The Class P
The PATH Problem
infeasible.
The CFL
Membership Problem
For example, factoring large integers is infeasible.
Assignment
7. Polynomial-Time Algorithms
The Class P
Robb T.
Koether
Homework
Review
Introduction
Programs that run in polynomial time are considered
Comparison feasible.
of Run Times
Polynomial Time For example, sorting long lists is feasible.
Exponential Time
Factorial Time
Programs that run in exponential time are considered
The Class P
The PATH Problem
infeasible.
The CFL
Membership Problem
For example, factoring large integers is infeasible.
Assignment
8. Polynomial-Time Algorithms
The Class P
Robb T.
Koether
Homework
Review
Introduction
Programs that run in polynomial time are considered
Comparison feasible.
of Run Times
Polynomial Time For example, sorting long lists is feasible.
Exponential Time
Factorial Time
Programs that run in exponential time are considered
The Class P
The PATH Problem
infeasible.
The CFL
Membership Problem
For example, factoring large integers is infeasible.
Assignment
9. Polynomial-Time Algorithms
The Class P
Robb T.
Koether
Homework
Review
Introduction
Programs that run in polynomial time are considered
Comparison feasible.
of Run Times
Polynomial Time For example, sorting long lists is feasible.
Exponential Time
Factorial Time
Programs that run in exponential time are considered
The Class P
The PATH Problem
infeasible.
The CFL
Membership Problem
For example, factoring large integers is infeasible.
Assignment
10. Comparison of Run Times
The Class P
Robb T.
Koether
Homework
Review
Introduction Example (Polynomial Time)
Comparison
of Run Times
Suppose we have Turing machines M1 , M2 , and M3
Polynomial Time
Exponential Time
whose run times are O(n), O(n2 ), and O(n3 ),
Factorial Time respectively.
The Class P
The PATH Problem Suppose that each one takes 1 µs when the input size
The CFL
Membership Problem is n = 1000.
Assignment
11. Comparison of Run Times
The Class P
Robb T.
Koether
Homework Example (Polynomial Time)
Review
Introduction Input size M1 M2 M3
Comparison 1000 1 µs 1 µs 1 µs
of Run Times
Polynomial Time 104 10 µs 100 µs 1 ms
Exponential Time
Factorial Time 105 100 µs 10 ms 1s
The Class P
The PATH Problem
106 1 ms 1s 1000 s
The CFL
Membership Problem
107 10 ms 100 s 12 d
Assignment 108 100 ms 2.8 h 32 y
109 1s 12 d 32000 y
12. Comparison of Run Times
The Class P
Robb T.
Koether
Homework
Review
Introduction
Example (Exponential Time)
Comparison
of Run Times Suppose we have Turing machines M4 and M5 whose
Polynomial Time
Exponential Time run times are O(2n ) and O(4n ), respectively.
Factorial Time
The Class P Suppose that each one takes 1 µs when the input size
The PATH Problem
The CFL
is n = 1000.
Membership Problem
Assignment
13. Comparison of Run Times
The Class P
Robb T.
Koether
Homework Example (Exponential Time)
Review
Introduction Input size M3 M4 M5
Comparison 1000 1.03 µs 1 µs 1 µs
of Run Times
Polynomial Time 1010 1.06 µs 1.02 ms 1.05 s
Exponential Time
Factorial Time 1020 1.09 µs 1.05 s 13 d
The Class P
The PATH Problem
1030 1.12 µs 1073 s 37, 000 y
The CFL
Membership Problem
1040 1.15 µs 13 d 38 × 109 y
Assignment 1050 1.18 µs 36 y 40 × 1015 y
1060 1.21 µs 37, 000 y 42 × 1021 y
14. Comparison of Run Times
The Class P
Robb T.
Koether
Homework
Review
Introduction
Example (Factorial Time)
Comparison
of Run Times Finally, suppose we have a Turing machine M6 whose
Polynomial Time
Exponential Time run time is O(n!) .
Factorial Time
The Class P Suppose that it takes 1 µs when the input size is
The PATH Problem
The CFL
n = 1000.
Membership Problem
Assignment
15. Comparison of Run Times
The Class P
Robb T.
Koether
Homework Example (Factorial Time)
Review
Introduction Input size M5 M6
Comparison 1000 1 µs 1 µs
of Run Times
Polynomial Time 1001 4 µs 1.001 ms
Exponential Time
Factorial Time 1002 16 µs 1.003 s
The Class P
The PATH Problem
1003 64 µs 16.8 m
The CFL
Membership Problem
1004 256 µs 11.7 d
Assignment 1005 1.024 ms 32.2 y
1006 4.096 ms 32, 381 y
16. The Class P
The Class P
Robb T.
Koether
Homework
Review
Definition (Class P)
Introduction
Comparison
The class P is the class of all languages (i.e., problems) that
of Run Times
Polynomial Time
are decidable in polynomial time by a deterministic
Exponential Time
Factorial Time
single-tape Turing machine.
The Class P ∞
The PATH Problem
The CFL
Membership Problem
P= TIME(nk ).
Assignment k=0
17. The Class P
The Class P
Robb T.
Koether
Homework
We have seen (will see) that if a problem can be
Review decided by a multitape Turing machine in polynomial
Introduction
time O(t(n)), then it can be decided by a single-tape
Comparison
of Run Times Turing machine in time O(t2 (n)), which is still
Polynomial Time
Exponential Time
polynomial.
Factorial Time
The Class P
However, we have also seen (will also see) that a
The PATH Problem problem that can be solved nondeterministically in
The CFL
Membership Problem
polynomial time can be solved deterministically in
Assignment
exponential time, but not necessarily in polynomial time.
Factoring an integer is an example (as far as we know).
18. Polynomial Time
The Class P
Robb T.
Koether
Homework
Review
Introduction Lemma
Comparison
of Run Times
If p(x) and q(x) are polynomials, then p(q(x)) is a polynomial.
Polynomial Time
Exponential Time
Factorial Time
Thus, if a polynomial-number of steps can each be
The Class P
The PATH Problem
performed in polynomial time, then the entire process
The CFL
Membership Problem can be done in polynomial time.
Assignment
19. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Introduction
Comparison
of Run Times
The PATH Problem
Polynomial Time
Exponential Time
Given a directed graph G and two vertices s and t, does
Factorial Time
there exist a directed path in G from s to t?
The Class P
The PATH Problem
The CFL
Membership Problem
Assignment
20. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Introduction
Example (The PATH Problem)
Comparison Let G be the graph.
of Run Times
Polynomial Time Let m be the number of vertices in G.
Exponential Time
Factorial Time
Let e(i, j) be true if there is an edge from vertex i to
The Class P
The PATH Problem vertex j and false otherwise.
The CFL
Membership Problem
Let s and t be the starting and ending vertices.
Assignment
21. The PATH Problem
The Class P
Robb T.
Koether
Homework Example (The PATH Problem)
Review
Introduction
Begin at s and let M = {s}.
Comparison Add to M all vertices that are adjacent to s (one edge
of Run Times
Polynomial Time away).
Exponential Time
Factorial Time Next, add to M all vertices that are adjacent to those
The Class P
The PATH Problem
vertices that are already in M.
The CFL
Membership Problem Repeat the previous step until no new vertices are
Assignment added to M.
See whether t ∈ M.
22. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The PATH Problem)
Introduction Is t ∈ M? No.
Comparison
of Run Times
Polynomial Time
Exponential Time
Factorial Time
The Class P
The PATH Problem
s t
The CFL
Membership Problem
Assignment
23. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The PATH Problem)
Introduction Is t ∈ M? No.
Comparison
of Run Times
Polynomial Time
Exponential Time
Factorial Time
The Class P
The PATH Problem
s t
The CFL
Membership Problem
Assignment
24. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The PATH Problem)
Introduction Is t ∈ M? No.
Comparison
of Run Times
Polynomial Time
Exponential Time
Factorial Time
The Class P
The PATH Problem
s t
The CFL
Membership Problem
Assignment
25. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The PATH Problem)
Introduction Is t ∈ M? No.
Comparison
of Run Times
Polynomial Time
Exponential Time
Factorial Time
The Class P
The PATH Problem
s t
The CFL
Membership Problem
Assignment
26. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The PATH Problem)
Introduction Is t ∈ M? Yes.
Comparison
of Run Times
Polynomial Time
Exponential Time
Factorial Time
The Class P
The PATH Problem
s t
The CFL
Membership Problem
Assignment
27. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The PATH Problem)
Introduction Is t ∈ M? No.
Comparison
of Run Times
Polynomial Time
Exponential Time
Factorial Time
The Class P
The PATH Problem
s t
The CFL
Membership Problem
Assignment
28. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The PATH Problem)
Introduction Is t ∈ M? No.
Comparison
of Run Times
Polynomial Time
Exponential Time
Factorial Time
The Class P
The PATH Problem
s t
The CFL
Membership Problem
Assignment
29. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The PATH Problem)
Introduction Is t ∈ M? No.
Comparison
of Run Times
Polynomial Time
Exponential Time
Factorial Time
The Class P
The PATH Problem
s t
The CFL
Membership Problem
Assignment
30. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The PATH Problem)
Introduction Is t ∈ M? No.
Comparison
of Run Times
Polynomial Time
Exponential Time
Factorial Time
The Class P
The PATH Problem
s t
The CFL
Membership Problem
Assignment
31. The PATH Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The PATH Problem)
Introduction Is t ∈ M? No.
Comparison
of Run Times
Polynomial Time
Exponential Time
Factorial Time
The Class P
The PATH Problem
s t
The CFL
Membership Problem
Assignment
32. An Algorithm for PATH
The Class P
Robb T.
Koether Example (The PATH Problem)
PATH(graph G, vertex s, vertex t)
Homework
Review
{
M = {s};
Introduction
M_old = {};
Comparison while (M != M_old)
of Run Times {
Polynomial Time
Exponential Time M_old = M;
Factorial Time for (i = 0; i < m; i++)
The Class P for (j = 0; j < m; j++)
The PATH Problem if (e(i, j) ∈ E && i ∈ M && j ∈ M)
/
The CFL
Membership Problem M = M ∪ {j};
}
Assignment
if (t ∈ M)
accept();
else
reject();
}
33. An Algorithm for PATH
The Class P
Robb T.
Koether
Homework
Review
Introduction
Comparison
Example (The PATH Problem)
of Run Times
Polynomial Time
We can analyze each step in the function PATH().
Exponential Time
Factorial Time Each step is done in polynomial time.
The Class P
The PATH Problem
Thus, PATH ∈ P.
The CFL
Membership Problem
Assignment
34. The CFL Membership Problem
The Class P
Robb T.
Koether
Homework
Review
Introduction
The CFL Membership Problem
Comparison Given a context-free language L and a word w, is w in L?
of Run Times
Polynomial Time
Exponential Time
Factorial Time
We may assume that we have a grammar for L in
The Class P
The PATH Problem
Chomsky Normal Form.
The CFL
Membership Problem Let m be the length of the word w.
Assignment
35. The CFL Membership Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The CFL Membership Problem)
Introduction Earlier we saw an algorithm for testing whether a word
Comparison
of Run Times
w is derivable from a CNF grammar G.
Polynomial Time
Exponential Time
The algorithm pursued every possible derivation of
Factorial Time
length 2m − 1.
The Class P
The PATH Problem There problem is, the number of such derivations may
The CFL
Membership Problem
be exponential in m. (Why?)
Assignment
We need a different algorithm.
36. The CFL Membership Problem
The Class P
Robb T.
Koether
Homework
Review
Introduction Example (The CFL Membership Problem)
Comparison
of Run Times Our strategy will be to begin with all one-symbol
Polynomial Time
Exponential Time substrings of w that are derivable from G.
Factorial Time
The Class P
These come directly from the rules of the form A → a.
The PATH Problem
The CFL Then proceed inductively.
Membership Problem
Assignment
37. The CFL Membership Problem
The Class P
Robb T.
Koether
Homework Example (The CFL Membership Problem)
Review
Introduction
Let w = x1 x2 x3 . . . xm .
Comparison Set up a table of size m × m.
of Run Times
Polynomial Time
Exponential Time
For all i ≤ j, table entry (i, j) is the set of all variables A
Factorial Time in G from which the substring xi . . . xj is derivable.
The Class P
The PATH Problem
For example,
The CFL ∗
Membership Problem Table entry (1, 1) = {A ∈ V | A = x1 }.
⇒
∗
Assignment Table entry (3, 5) = {A ∈ V | A = x3 x4 x5 }.
⇒
∗
Table entry (1, m) = {A ∈ V | A = w}.
⇒
38. The CFL Membership Problem
The Class P
Robb T.
Koether
Homework
Review
Introduction
Comparison Example (The CFL Membership Problem)
of Run Times
Polynomial Time Basic step:
Exponential Time
Factorial Time Initialize table entries (i, i) to the set of all variables A for
The Class P which there is a rule A → xi .
The PATH Problem
The CFL
Membership Problem
Assignment
39. Example
The Class P
Robb T.
Example (The CFL Membership Problem)
Koether
Let the grammar be
Homework
Review
S → SS | aSb | bSa | ε
Introduction
Comparison
of Run Times In CNF it is
Polynomial Time
Exponential Time
Factorial Time S0 → SS | AX | YA | AB | BA | ε
The Class P
The PATH Problem S → SS | AX | YA | AB | BA
The CFL
Membership Problem
X → SB
Assignment
Y → BS
A → a
B → b
40. Example
The Class P
Robb T.
Koether
Example (The CFL Membership Problem)
Homework
For the word w = abbaba,
Review
Introduction
Comparison A
of Run Times
Polynomial Time
Exponential Time B
Factorial Time
The Class P B
The PATH Problem
The CFL
Membership Problem
A
Assignment
B
A
41. The CFL Membership Problem
The Class P
Robb T.
Koether
Homework
Review
Example (The CFL Membership Problem)
Introduction Inductive step:
Comparison For each j ≥ 1, for each i < m, for each rule A → BC,
of Run Times
Polynomial Time
and for each k from i to i + j − 1,
Exponential Time
See whether B is in table entry (i, k) and C is in table
Factorial Time
entry (k + 1, i + j).
The Class P
The PATH Problem If so, then add A to table entry (i, i + j).
The CFL
Membership Problem ∗ ∗
⇒ ⇒
The idea is that if B = xi . . . xk and C = xk+1 . . . xi+j ,
Assignment ∗
then A ⇒ BC = xi . . . xi+j .
⇒
42. Example
The Class P
Robb T.
Koether
Homework Example (The CFL Membership Problem)
Review
Introduction For example, when j = 1, consider the case where
Comparison i = 1.
of Run Times
Polynomial Time In this case, i + j = 2, so k must equal 1.
Exponential Time
Factorial Time
Table entry (1, 1) = {A} and table entry (2, 2) = {B}.
The Class P
The PATH Problem
The CFL
There are rules S0 → AB and S → AB.
Membership Problem
So table entry (1, 2) = {S0 , S}.
Assignment
∗ ∗
⇒ ⇒
That is, S0 = ab and S = ab.
43. Example
The Class P
Robb T.
Koether
Example (The CFL Membership Problem)
Homework
For j = 1,
Review
Introduction
Comparison A S0, S
of Run Times
Polynomial Time
Exponential Time B ∅
Factorial Time
The Class P
The PATH Problem
B S0, S
The CFL
Membership Problem
A S0, S
Assignment
B S0, S
A
44. Example
The Class P
Robb T.
Koether
Example (The CFL Membership Problem)
Homework
Review
When j = 2, consider the case where i = 1.
Introduction In this case, i + j = 3, so k may equal 1 or 2.
Comparison
of Run Times
Case k = 1:
Polynomial Time
Exponential Time
Table entry (1, 1) = {A} and table entry (2, 3) = ∅.
Factorial Time This produces nothing.
The Class P
The PATH Problem
Case k = 2:
The CFL
Membership Problem Table entry (1, 2) = {S0 , S} and table entry (3, 3) = {B}.
Assignment There is the rule X → SB.
So table entry (1, 3) = {X}.
2
⇒
That is, X = abb.
45. Example
The Class P
Robb T.
Koether
Example (The CFL Membership Problem)
Homework
For j = 2,
Review
Introduction
Comparison A S0, S X
of Run Times
Polynomial Time
Exponential Time B ∅ Y
Factorial Time
The Class P
The PATH Problem
B S0, S X, Y
The CFL
Membership Problem
A S0, S ∅
Assignment
B S0, S
A
46. Example
The Class P
Robb T.
Koether
Example (The CFL Membership Problem)
Homework
For j = 3,
Review
Introduction
Comparison A S0, S X S0, S
of Run Times
Polynomial Time
Exponential Time B ∅ Y ∅
Factorial Time
The Class P
The PATH Problem
B S0, S X, Y S0, S
The CFL
Membership Problem
A S0, S ∅
Assignment
B S0, S
A
47. Example
The Class P
Robb T.
Koether
Example (The CFL Membership Problem)
Homework
For j = 4,
Review
Introduction
Comparison A S0, S X S0, S X
of Run Times
Polynomial Time
Exponential Time B ∅ Y ∅ Y
Factorial Time
The Class P
The PATH Problem
B S0, S X, Y S0, S
The CFL
Membership Problem
A S0, S ∅
Assignment
B S0, S
A
48. Example
The Class P
Robb T.
Koether
Example (The CFL Membership Problem)
Homework
For j = 5,
Review
Introduction
Comparison A S0, S X S0, S X S0, S
of Run Times
Polynomial Time
Exponential Time B ∅ Y ∅ Y
Factorial Time
The Class P
The PATH Problem
B S0, S X, Y S0, S
The CFL
Membership Problem
A S0, S ∅
Assignment
B S0, S
A
49. The CFL Membership Problem
The Class P
Robb T.
Koether
Homework
Review Example (The CFL Membership Problem)
Introduction
Finally, see whether the start symbol is in table entry
Comparison
of Run Times (1, m).
Polynomial Time
Exponential Time
Factorial Time
If it is, then accept w.
The Class P If it is not, the reject w.
The PATH Problem
The CFL
Membership Problem
In the example, the start symbol S0 is in table entry
Assignment (1, 6).
50. An Algorithm for CFL
The Class P
Robb T. Example (The CFL Membership Problem)
Koether
CFLMember(graph G, string w)
Homework
{
Review for (int i = 1; i <= m; i++)
if ((A → w[i]) ∈ R)
Introduction
Add A to table[i, i];
Comparison for (int len = 2; len <= m; len++)
of Run Times
Polynomial Time
for (int i = 1; i <= m - len + 1; i++)
Exponential Time {
Factorial Time
int j = i + len - 1;
The Class P for (int k = i; k <= j - 1; k++)
The PATH Problem
for each ((A → BC) ∈ R)
The CFL
Membership Problem if (B ∈ table[i, k] && C ∈ table[k+1, j])
Assignment Add A to table[i, j];
}
if (S ∈ table[1, m])
accept();
else
reject();
}
51. The CFL Membership Problem
The Class P
Robb T.
Koether
Homework
Review
Introduction Example (The CFL Membership Problem)
Comparison
of Run Times We can analyze each step in the function
Polynomial Time
Exponential Time CFLMember().
Factorial Time
The Class P
Each step is done in polynomial time.
The PATH Problem
The CFL Thus, CFL ∈ P.
Membership Problem
Assignment
52. Assignment
The Class P
Robb T.
Koether
Homework
Review
Introduction
Homework
Comparison
of Run Times
Polynomial Time
Read Section 7.2, page256 - 263.
Exponential Time
Factorial Time Exercises 1, 2, 3, 4, 9, pages 294 - 295.
The Class P
The PATH Problem
Problem 12, page 295.
The CFL
Membership Problem
Assignment