Lecture%2038%20 %20 the%20class%20p

430 views
329 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
430
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture%2038%20 %20 the%20class%20p

  1. 1. The Class P Robb T. KoetherHomeworkReview The Class PIntroduction Lecture 37 (38)Comparison Section 7.2of Run TimesPolynomial TimeExponential TimeFactorial TimeThe Class P Robb T. KoetherThe PATH ProblemThe CFLMembership Problem Hampden-Sydney CollegeAssignment Fri, Nov 21, 2008 (Mon, Nov 24, 2008)
  2. 2. Outline The Class P Robb T. Koether 1 Homework ReviewHomeworkReview 2 IntroductionIntroductionComparison 3 Comparison of Run Timesof Run Times Polynomial TimePolynomial TimeExponential Time Exponential TimeFactorial TimeThe Class P Factorial TimeThe PATH ProblemThe CFLMembership Problem 4 The Class PAssignment The PATH Problem The CFL Membership Problem 5 Assignment
  3. 3. Homework Review The Class P Robb T. KoetherHomework Exercise 7.1, page 294.ReviewIntroduction Answer each part TRUE or FALSE.Comparison (a) 2n = O(n).of Run TimesPolynomial TimeExponential Time (b) n2 = O(n). (c) n2 = O(n log2 n).Factorial TimeThe Class PThe PATH ProblemThe CFL (d) n log n = O(n2 ).Membership ProblemAssignment (e) 3n = 2O(n) . n n (f) 22 = O(22 ).
  4. 4. Homework Review The Class P Robb T. KoetherHomeworkReview SolutionIntroduction (a) TRUE.Comparisonof Run Times (b) FALSE.Polynomial TimeExponential TimeFactorial Time (c) FALSE.The Class P (d) TRUE.The PATH ProblemThe CFLMembership Problem (e) TRUE.Assignment (f) TRUE.
  5. 5. Homework Review The Class P Robb T. KoetherHomeworkReview SolutionIntroductionComparison The only part that is not pretty clear is part (e).of Run TimesPolynomial Time 2O(n) means 2 raised to a function that is itself O(n).Exponential TimeFactorial Time For example, 25n+3 is 2O(n) .The Class PThe PATH Problem In part (e), use the fact that 3 = 2log2 3 .The CFLMembership Problem It follows that 3n = 2(log2 3)n , so it is 2O(n) .Assignment
  6. 6. Polynomial-Time Algorithms The Class P Robb T. KoetherHomeworkReviewIntroduction Programs that run in polynomial time are consideredComparison feasible.of Run TimesPolynomial Time For example, sorting long lists is feasible.Exponential TimeFactorial Time Programs that run in exponential time are consideredThe Class PThe PATH Problem infeasible.The CFLMembership Problem For example, factoring large integers is infeasible.Assignment
  7. 7. Polynomial-Time Algorithms The Class P Robb T. KoetherHomeworkReviewIntroduction Programs that run in polynomial time are consideredComparison feasible.of Run TimesPolynomial Time For example, sorting long lists is feasible.Exponential TimeFactorial Time Programs that run in exponential time are consideredThe Class PThe PATH Problem infeasible.The CFLMembership Problem For example, factoring large integers is infeasible.Assignment
  8. 8. Polynomial-Time Algorithms The Class P Robb T. KoetherHomeworkReviewIntroduction Programs that run in polynomial time are consideredComparison feasible.of Run TimesPolynomial Time For example, sorting long lists is feasible.Exponential TimeFactorial Time Programs that run in exponential time are consideredThe Class PThe PATH Problem infeasible.The CFLMembership Problem For example, factoring large integers is infeasible.Assignment
  9. 9. Polynomial-Time Algorithms The Class P Robb T. KoetherHomeworkReviewIntroduction Programs that run in polynomial time are consideredComparison feasible.of Run TimesPolynomial Time For example, sorting long lists is feasible.Exponential TimeFactorial Time Programs that run in exponential time are consideredThe Class PThe PATH Problem infeasible.The CFLMembership Problem For example, factoring large integers is infeasible.Assignment
  10. 10. Comparison of Run Times The Class P Robb T. KoetherHomeworkReviewIntroduction Example (Polynomial Time)Comparisonof Run Times Suppose we have Turing machines M1 , M2 , and M3Polynomial TimeExponential Time whose run times are O(n), O(n2 ), and O(n3 ),Factorial Time respectively.The Class PThe PATH Problem Suppose that each one takes 1 µs when the input sizeThe CFLMembership Problem is n = 1000.Assignment
  11. 11. Comparison of Run Times The Class P Robb T. KoetherHomework Example (Polynomial Time)ReviewIntroduction Input size M1 M2 M3Comparison 1000 1 µs 1 µs 1 µsof Run TimesPolynomial Time 104 10 µs 100 µs 1 msExponential TimeFactorial Time 105 100 µs 10 ms 1sThe Class PThe PATH Problem 106 1 ms 1s 1000 sThe CFLMembership Problem 107 10 ms 100 s 12 dAssignment 108 100 ms 2.8 h 32 y 109 1s 12 d 32000 y
  12. 12. Comparison of Run Times The Class P Robb T. KoetherHomeworkReviewIntroduction Example (Exponential Time)Comparisonof Run Times Suppose we have Turing machines M4 and M5 whosePolynomial TimeExponential Time run times are O(2n ) and O(4n ), respectively.Factorial TimeThe Class P Suppose that each one takes 1 µs when the input sizeThe PATH ProblemThe CFL is n = 1000.Membership ProblemAssignment
  13. 13. Comparison of Run Times The Class P Robb T. KoetherHomework Example (Exponential Time)ReviewIntroduction Input size M3 M4 M5Comparison 1000 1.03 µs 1 µs 1 µsof Run TimesPolynomial Time 1010 1.06 µs 1.02 ms 1.05 sExponential TimeFactorial Time 1020 1.09 µs 1.05 s 13 dThe Class PThe PATH Problem 1030 1.12 µs 1073 s 37, 000 yThe CFLMembership Problem 1040 1.15 µs 13 d 38 × 109 yAssignment 1050 1.18 µs 36 y 40 × 1015 y 1060 1.21 µs 37, 000 y 42 × 1021 y
  14. 14. Comparison of Run Times The Class P Robb T. KoetherHomeworkReviewIntroduction Example (Factorial Time)Comparisonof Run Times Finally, suppose we have a Turing machine M6 whosePolynomial TimeExponential Time run time is O(n!) .Factorial TimeThe Class P Suppose that it takes 1 µs when the input size isThe PATH ProblemThe CFL n = 1000.Membership ProblemAssignment
  15. 15. Comparison of Run Times The Class P Robb T. KoetherHomework Example (Factorial Time)ReviewIntroduction Input size M5 M6Comparison 1000 1 µs 1 µsof Run TimesPolynomial Time 1001 4 µs 1.001 msExponential TimeFactorial Time 1002 16 µs 1.003 sThe Class PThe PATH Problem 1003 64 µs 16.8 mThe CFLMembership Problem 1004 256 µs 11.7 dAssignment 1005 1.024 ms 32.2 y 1006 4.096 ms 32, 381 y
  16. 16. The Class P The Class P Robb T. KoetherHomeworkReview Definition (Class P)IntroductionComparison The class P is the class of all languages (i.e., problems) thatof Run TimesPolynomial Time are decidable in polynomial time by a deterministicExponential TimeFactorial Time single-tape Turing machine.The Class P ∞The PATH ProblemThe CFLMembership Problem P= TIME(nk ).Assignment k=0
  17. 17. The Class P The Class P Robb T. KoetherHomework We have seen (will see) that if a problem can beReview decided by a multitape Turing machine in polynomialIntroduction time O(t(n)), then it can be decided by a single-tapeComparisonof Run Times Turing machine in time O(t2 (n)), which is stillPolynomial TimeExponential Time polynomial.Factorial TimeThe Class P However, we have also seen (will also see) that aThe PATH Problem problem that can be solved nondeterministically inThe CFLMembership Problem polynomial time can be solved deterministically inAssignment exponential time, but not necessarily in polynomial time. Factoring an integer is an example (as far as we know).
  18. 18. Polynomial Time The Class P Robb T. KoetherHomeworkReviewIntroduction LemmaComparisonof Run Times If p(x) and q(x) are polynomials, then p(q(x)) is a polynomial.Polynomial TimeExponential TimeFactorial Time Thus, if a polynomial-number of steps can each beThe Class PThe PATH Problem performed in polynomial time, then the entire processThe CFLMembership Problem can be done in polynomial time.Assignment
  19. 19. The PATH Problem The Class P Robb T. KoetherHomeworkReviewIntroductionComparisonof Run Times The PATH ProblemPolynomial TimeExponential Time Given a directed graph G and two vertices s and t, doesFactorial Time there exist a directed path in G from s to t?The Class PThe PATH ProblemThe CFLMembership ProblemAssignment
  20. 20. The PATH Problem The Class P Robb T. KoetherHomeworkReviewIntroduction Example (The PATH Problem)Comparison Let G be the graph.of Run TimesPolynomial Time Let m be the number of vertices in G.Exponential TimeFactorial Time Let e(i, j) be true if there is an edge from vertex i toThe Class PThe PATH Problem vertex j and false otherwise.The CFLMembership Problem Let s and t be the starting and ending vertices.Assignment
  21. 21. The PATH Problem The Class P Robb T. KoetherHomework Example (The PATH Problem)ReviewIntroduction Begin at s and let M = {s}.Comparison Add to M all vertices that are adjacent to s (one edgeof Run TimesPolynomial Time away).Exponential TimeFactorial Time Next, add to M all vertices that are adjacent to thoseThe Class PThe PATH Problem vertices that are already in M.The CFLMembership Problem Repeat the previous step until no new vertices areAssignment added to M. See whether t ∈ M.
  22. 22. The PATH Problem The Class P Robb T. KoetherHomeworkReview Example (The PATH Problem)Introduction Is t ∈ M? No.Comparisonof Run TimesPolynomial TimeExponential TimeFactorial TimeThe Class PThe PATH Problem s tThe CFLMembership ProblemAssignment
  23. 23. The PATH Problem The Class P Robb T. KoetherHomeworkReview Example (The PATH Problem)Introduction Is t ∈ M? No.Comparisonof Run TimesPolynomial TimeExponential TimeFactorial TimeThe Class PThe PATH Problem s tThe CFLMembership ProblemAssignment
  24. 24. The PATH Problem The Class P Robb T. KoetherHomeworkReview Example (The PATH Problem)Introduction Is t ∈ M? No.Comparisonof Run TimesPolynomial TimeExponential TimeFactorial TimeThe Class PThe PATH Problem s tThe CFLMembership ProblemAssignment
  25. 25. The PATH Problem The Class P Robb T. KoetherHomeworkReview Example (The PATH Problem)Introduction Is t ∈ M? No.Comparisonof Run TimesPolynomial TimeExponential TimeFactorial TimeThe Class PThe PATH Problem s tThe CFLMembership ProblemAssignment
  26. 26. The PATH Problem The Class P Robb T. KoetherHomeworkReview Example (The PATH Problem)Introduction Is t ∈ M? Yes.Comparisonof Run TimesPolynomial TimeExponential TimeFactorial TimeThe Class PThe PATH Problem s tThe CFLMembership ProblemAssignment
  27. 27. The PATH Problem The Class P Robb T. KoetherHomeworkReview Example (The PATH Problem)Introduction Is t ∈ M? No.Comparisonof Run TimesPolynomial TimeExponential TimeFactorial TimeThe Class PThe PATH Problem s tThe CFLMembership ProblemAssignment
  28. 28. The PATH Problem The Class P Robb T. KoetherHomeworkReview Example (The PATH Problem)Introduction Is t ∈ M? No.Comparisonof Run TimesPolynomial TimeExponential TimeFactorial TimeThe Class PThe PATH Problem s tThe CFLMembership ProblemAssignment
  29. 29. The PATH Problem The Class P Robb T. KoetherHomeworkReview Example (The PATH Problem)Introduction Is t ∈ M? No.Comparisonof Run TimesPolynomial TimeExponential TimeFactorial TimeThe Class PThe PATH Problem s tThe CFLMembership ProblemAssignment
  30. 30. The PATH Problem The Class P Robb T. KoetherHomeworkReview Example (The PATH Problem)Introduction Is t ∈ M? No.Comparisonof Run TimesPolynomial TimeExponential TimeFactorial TimeThe Class PThe PATH Problem s tThe CFLMembership ProblemAssignment
  31. 31. The PATH Problem The Class P Robb T. KoetherHomeworkReview Example (The PATH Problem)Introduction Is t ∈ M? No.Comparisonof Run TimesPolynomial TimeExponential TimeFactorial TimeThe Class PThe PATH Problem s tThe CFLMembership ProblemAssignment
  32. 32. An Algorithm for PATH The Class P Robb T. Koether Example (The PATH Problem) PATH(graph G, vertex s, vertex t)HomeworkReview { M = {s};Introduction M_old = {};Comparison while (M != M_old)of Run Times {Polynomial TimeExponential 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 CFLMembership Problem M = M ∪ {j}; }Assignment if (t ∈ M) accept(); else reject(); }
  33. 33. An Algorithm for PATH The Class P Robb T. KoetherHomeworkReviewIntroductionComparison Example (The PATH Problem)of Run TimesPolynomial Time We can analyze each step in the function PATH().Exponential TimeFactorial Time Each step is done in polynomial time.The Class PThe PATH Problem Thus, PATH ∈ P.The CFLMembership ProblemAssignment
  34. 34. The CFL Membership Problem The Class P Robb T. KoetherHomeworkReviewIntroduction The CFL Membership ProblemComparison Given a context-free language L and a word w, is w in L?of Run TimesPolynomial TimeExponential TimeFactorial Time We may assume that we have a grammar for L inThe Class PThe PATH Problem Chomsky Normal Form.The CFLMembership Problem Let m be the length of the word w.Assignment
  35. 35. The CFL Membership Problem The Class P Robb T. KoetherHomeworkReview Example (The CFL Membership Problem)Introduction Earlier we saw an algorithm for testing whether a wordComparisonof Run Times w is derivable from a CNF grammar G.Polynomial TimeExponential Time The algorithm pursued every possible derivation ofFactorial Time length 2m − 1.The Class PThe PATH Problem There problem is, the number of such derivations mayThe CFLMembership Problem be exponential in m. (Why?)Assignment We need a different algorithm.
  36. 36. The CFL Membership Problem The Class P Robb T. KoetherHomeworkReviewIntroduction Example (The CFL Membership Problem)Comparisonof Run Times Our strategy will be to begin with all one-symbolPolynomial TimeExponential Time substrings of w that are derivable from G.Factorial TimeThe Class P These come directly from the rules of the form A → a.The PATH ProblemThe CFL Then proceed inductively.Membership ProblemAssignment
  37. 37. The CFL Membership Problem The Class P Robb T. KoetherHomework Example (The CFL Membership Problem)ReviewIntroduction Let w = x1 x2 x3 . . . xm .Comparison Set up a table of size m × m.of Run TimesPolynomial TimeExponential Time For all i ≤ j, table entry (i, j) is the set of all variables AFactorial Time in G from which the substring xi . . . xj is derivable.The Class PThe 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. 38. The CFL Membership Problem The Class P Robb T. KoetherHomeworkReviewIntroductionComparison Example (The CFL Membership Problem)of Run TimesPolynomial Time Basic step:Exponential TimeFactorial Time Initialize table entries (i, i) to the set of all variables A forThe Class P which there is a rule A → xi .The PATH ProblemThe CFLMembership ProblemAssignment
  39. 39. Example The Class P Robb T. Example (The CFL Membership Problem) Koether Let the grammar beHomeworkReview S → SS | aSb | bSa | εIntroductionComparisonof Run Times In CNF it isPolynomial TimeExponential TimeFactorial Time S0 → SS | AX | YA | AB | BA | εThe Class PThe PATH Problem S → SS | AX | YA | AB | BAThe CFLMembership Problem X → SBAssignment Y → BS A → a B → b
  40. 40. Example The Class P Robb T. Koether Example (The CFL Membership Problem)Homework For the word w = abbaba,ReviewIntroductionComparison Aof Run TimesPolynomial TimeExponential Time BFactorial TimeThe Class P BThe PATH ProblemThe CFLMembership Problem AAssignment B A
  41. 41. The CFL Membership Problem The Class P Robb T. KoetherHomeworkReview Example (The CFL Membership Problem)Introduction Inductive step:Comparison For each j ≥ 1, for each i < m, for each rule A → BC,of Run TimesPolynomial 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 tableFactorial Time entry (k + 1, i + j).The Class PThe PATH Problem If so, then add A to table entry (i, i + j).The CFLMembership Problem ∗ ∗ ⇒ ⇒ The idea is that if B = xi . . . xk and C = xk+1 . . . xi+j ,Assignment ∗ then A ⇒ BC = xi . . . xi+j . ⇒
  42. 42. Example The Class P Robb T. KoetherHomework Example (The CFL Membership Problem)ReviewIntroduction For example, when j = 1, consider the case whereComparison i = 1.of Run TimesPolynomial Time In this case, i + j = 2, so k must equal 1.Exponential TimeFactorial Time Table entry (1, 1) = {A} and table entry (2, 2) = {B}.The Class PThe PATH ProblemThe 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. 43. Example The Class P Robb T. Koether Example (The CFL Membership Problem)Homework For j = 1,ReviewIntroductionComparison A S0, Sof Run TimesPolynomial TimeExponential Time B ∅Factorial TimeThe Class PThe PATH Problem B S0, SThe CFLMembership Problem A S0, SAssignment B S0, S A
  44. 44. Example The Class P Robb T. Koether Example (The CFL Membership Problem)HomeworkReview When j = 2, consider the case where i = 1.Introduction In this case, i + j = 3, so k may equal 1 or 2.Comparisonof Run Times Case k = 1:Polynomial TimeExponential Time Table entry (1, 1) = {A} and table entry (2, 3) = ∅.Factorial Time This produces nothing.The Class PThe PATH Problem Case k = 2:The CFLMembership 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. 45. Example The Class P Robb T. Koether Example (The CFL Membership Problem)Homework For j = 2,ReviewIntroductionComparison A S0, S Xof Run TimesPolynomial TimeExponential Time B ∅ YFactorial TimeThe Class PThe PATH Problem B S0, S X, YThe CFLMembership Problem A S0, S ∅Assignment B S0, S A
  46. 46. Example The Class P Robb T. Koether Example (The CFL Membership Problem)Homework For j = 3,ReviewIntroductionComparison A S0, S X S0, Sof Run TimesPolynomial TimeExponential Time B ∅ Y ∅Factorial TimeThe Class PThe PATH Problem B S0, S X, Y S0, SThe CFLMembership Problem A S0, S ∅Assignment B S0, S A
  47. 47. Example The Class P Robb T. Koether Example (The CFL Membership Problem)Homework For j = 4,ReviewIntroductionComparison A S0, S X S0, S Xof Run TimesPolynomial TimeExponential Time B ∅ Y ∅ YFactorial TimeThe Class PThe PATH Problem B S0, S X, Y S0, SThe CFLMembership Problem A S0, S ∅Assignment B S0, S A
  48. 48. Example The Class P Robb T. Koether Example (The CFL Membership Problem)Homework For j = 5,ReviewIntroductionComparison A S0, S X S0, S X S0, Sof Run TimesPolynomial TimeExponential Time B ∅ Y ∅ YFactorial TimeThe Class PThe PATH Problem B S0, S X, Y S0, SThe CFLMembership Problem A S0, S ∅Assignment B S0, S A
  49. 49. The CFL Membership Problem The Class P Robb T. KoetherHomeworkReview Example (The CFL Membership Problem)Introduction Finally, see whether the start symbol is in table entryComparisonof Run Times (1, m).Polynomial TimeExponential TimeFactorial Time If it is, then accept w.The Class P If it is not, the reject w.The PATH ProblemThe CFLMembership Problem In the example, the start symbol S0 is in table entryAssignment (1, 6).
  50. 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 TimesPolynomial 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 CFLMembership 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. 51. The CFL Membership Problem The Class P Robb T. KoetherHomeworkReviewIntroduction Example (The CFL Membership Problem)Comparisonof Run Times We can analyze each step in the functionPolynomial TimeExponential Time CFLMember().Factorial TimeThe Class P Each step is done in polynomial time.The PATH ProblemThe CFL Thus, CFL ∈ P.Membership ProblemAssignment
  52. 52. Assignment The Class P Robb T. KoetherHomeworkReviewIntroduction HomeworkComparisonof Run TimesPolynomial Time Read Section 7.2, page256 - 263.Exponential TimeFactorial Time Exercises 1, 2, 3, 4, 9, pages 294 - 295.The Class PThe PATH Problem Problem 12, page 295.The CFLMembership ProblemAssignment

×