SlideShare a Scribd company logo
1 of 52
Download to read offline
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)
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
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 ).
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.
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
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
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
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
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
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
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
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
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
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
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
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
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).
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
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
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
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.
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
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
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
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
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
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
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
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
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
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
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();
                     }
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
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
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.
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
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}.
                                                             ⇒
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
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
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
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 .
                                      ⇒
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.
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
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.
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
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
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
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
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).
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();
                     }
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
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

More Related Content

Viewers also liked

Outline Res Meth 2010 Spring Feb01
Outline Res Meth 2010 Spring Feb01Outline Res Meth 2010 Spring Feb01
Outline Res Meth 2010 Spring Feb01Yender McLee
 
Application of toolbox so far… how to benefit from tool box in education
Application of toolbox so far… how to benefit from tool box in educationApplication of toolbox so far… how to benefit from tool box in education
Application of toolbox so far… how to benefit from tool box in educationGlobal Water Partnership
 
Scott findlay critical habitat and recovery
Scott findlay critical habitat and recoveryScott findlay critical habitat and recovery
Scott findlay critical habitat and recoverySustainable Prosperity
 
Chris morgan presenting - 2011
Chris morgan   presenting - 2011Chris morgan   presenting - 2011
Chris morgan presenting - 2011Ray Poynter
 
Modern Jago Session One UX
Modern Jago Session One UXModern Jago Session One UX
Modern Jago Session One UXModernJago
 
Humber trends presentation
Humber trends presentationHumber trends presentation
Humber trends presentationRon Nurwisah
 
JavaOne 2007 BOF Session
JavaOne 2007 BOF SessionJavaOne 2007 BOF Session
JavaOne 2007 BOF SessionJeff Hoffman
 
Analgesia And Anesthesia For The Breastfeeding Mother
Analgesia And Anesthesia For The Breastfeeding MotherAnalgesia And Anesthesia For The Breastfeeding Mother
Analgesia And Anesthesia For The Breastfeeding MotherBiblioteca Virtual
 
Txt2calm
Txt2calmTxt2calm
Txt2calmtimpj
 

Viewers also liked (16)

Outline Res Meth 2010 Spring Feb01
Outline Res Meth 2010 Spring Feb01Outline Res Meth 2010 Spring Feb01
Outline Res Meth 2010 Spring Feb01
 
Ijpar 13 506 sujatha
Ijpar 13 506 sujathaIjpar 13 506 sujatha
Ijpar 13 506 sujatha
 
Application of toolbox so far… how to benefit from tool box in education
Application of toolbox so far… how to benefit from tool box in educationApplication of toolbox so far… how to benefit from tool box in education
Application of toolbox so far… how to benefit from tool box in education
 
Polynomial
PolynomialPolynomial
Polynomial
 
Scott findlay critical habitat and recovery
Scott findlay critical habitat and recoveryScott findlay critical habitat and recovery
Scott findlay critical habitat and recovery
 
EEX 504 TRANSITION CHAP. 1
EEX 504 TRANSITION CHAP. 1EEX 504 TRANSITION CHAP. 1
EEX 504 TRANSITION CHAP. 1
 
Risk Management Summit
Risk Management SummitRisk Management Summit
Risk Management Summit
 
Chris morgan presenting - 2011
Chris morgan   presenting - 2011Chris morgan   presenting - 2011
Chris morgan presenting - 2011
 
Modern Jago Session One UX
Modern Jago Session One UXModern Jago Session One UX
Modern Jago Session One UX
 
Humber trends presentation
Humber trends presentationHumber trends presentation
Humber trends presentation
 
JavaOne 2007 BOF Session
JavaOne 2007 BOF SessionJavaOne 2007 BOF Session
JavaOne 2007 BOF Session
 
김수지
김수지김수지
김수지
 
Physical fitnes
Physical fitnesPhysical fitnes
Physical fitnes
 
Life sutra
Life sutraLife sutra
Life sutra
 
Analgesia And Anesthesia For The Breastfeeding Mother
Analgesia And Anesthesia For The Breastfeeding MotherAnalgesia And Anesthesia For The Breastfeeding Mother
Analgesia And Anesthesia For The Breastfeeding Mother
 
Txt2calm
Txt2calmTxt2calm
Txt2calm
 

Similar to Lecture%2038%20 %20 the%20class%20p

Time_Complexity.pptx
Time_Complexity.pptxTime_Complexity.pptx
Time_Complexity.pptxvivekcommon
 
Np completeness
Np completenessNp completeness
Np completenessRajendran
 
7._Representing_Real_Life_Situations_Using_Rational_Functions.pptx.pdf
7._Representing_Real_Life_Situations_Using_Rational_Functions.pptx.pdf7._Representing_Real_Life_Situations_Using_Rational_Functions.pptx.pdf
7._Representing_Real_Life_Situations_Using_Rational_Functions.pptx.pdfCharlesBuale1
 
Computational Complexity: Oracles and the Polynomial Hierarchy
Computational Complexity: Oracles and the Polynomial HierarchyComputational Complexity: Oracles and the Polynomial Hierarchy
Computational Complexity: Oracles and the Polynomial HierarchyAntonis Antonopoulos
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O NotationJawad Khan
 

Similar to Lecture%2038%20 %20 the%20class%20p (10)

AA ppt9107
AA ppt9107AA ppt9107
AA ppt9107
 
Time_Complexity.pptx
Time_Complexity.pptxTime_Complexity.pptx
Time_Complexity.pptx
 
Intro to modelling_wur
Intro to modelling_wurIntro to modelling_wur
Intro to modelling_wur
 
Np completeness
Np completenessNp completeness
Np completeness
 
7._Representing_Real_Life_Situations_Using_Rational_Functions.pptx.pdf
7._Representing_Real_Life_Situations_Using_Rational_Functions.pptx.pdf7._Representing_Real_Life_Situations_Using_Rational_Functions.pptx.pdf
7._Representing_Real_Life_Situations_Using_Rational_Functions.pptx.pdf
 
Computational Complexity: Oracles and the Polynomial Hierarchy
Computational Complexity: Oracles and the Polynomial HierarchyComputational Complexity: Oracles and the Polynomial Hierarchy
Computational Complexity: Oracles and the Polynomial Hierarchy
 
class23.ppt
class23.pptclass23.ppt
class23.ppt
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Me330 lecture3
Me330 lecture3Me330 lecture3
Me330 lecture3
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O Notation
 

Recently uploaded

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 

Recently uploaded (20)

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
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