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

Post on 26-Jun-2015

257 views 0 download

Tags:

Transcript of Lecture%2038%20 %20 the%20class%20p

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The Class PLecture 37 (38)

Section 7.2

Robb T. Koether

Hampden-Sydney College

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Outline

1 Homework Review

2 Introduction

3 Comparison of Run TimesPolynomial TimeExponential TimeFactorial Time

4 The Class PThe PATH ProblemThe CFL Membership Problem

5 Assignment

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Homework Review

Exercise 7.1, page 294.

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

= O(22n).

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Homework Review

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Homework Review

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Polynomial-Time Algorithms

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Polynomial-Time Algorithms

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Polynomial-Time Algorithms

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Polynomial-Time Algorithms

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Comparison of Run Times

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Comparison of Run Times

Example (Polynomial Time)

Input size M1 M2 M3

1000 1 µs 1 µs 1 µs104 10 µs 100 µs 1 ms105 100 µs 10 ms 1 s106 1 ms 1 s 1000 s107 10 ms 100 s 12 d108 100 ms 2.8 h 32 y109 1 s 12 d 32000 y

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Comparison of Run Times

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Comparison of Run Times

Example (Exponential Time)

Input size M3 M4 M5

1000 1.03 µs 1 µs 1 µs1010 1.06 µs 1.02 ms 1.05 s1020 1.09 µs 1.05 s 13 d1030 1.12 µs 1073 s 37, 000 y1040 1.15 µs 13 d 38× 109 y1050 1.18 µs 36 y 40× 1015 y1060 1.21 µs 37, 000 y 42× 1021 y

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Comparison of Run Times

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Comparison of Run Times

Example (Factorial Time)

Input size M5 M6

1000 1 µs 1 µs1001 4 µs 1.001 ms1002 16 µs 1.003 s1003 64 µs 16.8 m1004 256 µs 11.7 d1005 1.024 ms 32.2 y1006 4.096 ms 32, 381 y

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The Class P

Definition (Class P)The class P is the class of all languages (i.e., problems) thatare decidable in polynomial time by a deterministicsingle-tape Turing machine.

P =∞⋃

k=0

TIME(nk).

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The Class P

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Polynomial Time

LemmaIf p(x) and q(x) are polynomials, then p(q(x)) is a polynomial.

Thus, if a polynomial-number of steps can each beperformed in polynomial time, then the entire processcan be done in polynomial time.

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

The PATH ProblemGiven a directed graph G and two vertices s and t, doesthere exist a directed path in G from s to t?

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

Example (The PATH Problem)

Begin at s and let M = {s}.Add to M all vertices that are adjacent to s (one edgeaway).Next, add to M all vertices that are adjacent to thosevertices that are already in M.Repeat the previous step until no new vertices areadded to M.See whether t ∈ M.

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

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

s t

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

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

s t

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

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

s t

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

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

s t

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

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

s t

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

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

s t

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

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

s t

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

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

s t

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

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

s t

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The PATH Problem

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

s t

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

An Algorithm for PATH

Example (The PATH Problem)PATH(graph G, vertex s, vertex t){

M = {s};M_old = {};while (M != M_old){

M_old = M;for (i = 0; i < m; i++)

for (j = 0; j < m; j++)if (e(i, j) ∈ E && i ∈ M && j /∈ M)

M = M ∪ {j};}if (t ∈ M)

accept();else

reject();}

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

An Algorithm for PATH

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The CFL Membership Problem

The CFL Membership ProblemGiven a context-free language L and a word w, is w in L?

We may assume that we have a grammar for L inChomsky Normal Form.Let m be the length of the word w.

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The CFL Membership Problem

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The CFL Membership Problem

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The CFL Membership Problem

Example (The CFL Membership Problem)Let w = x1x2x3 . . . xm.Set up a table of size m× m.For all i ≤ j, table entry (i, j) is the set of all variables Ain G from which the substring xi . . . xj is derivable.For example,

Table entry (1, 1) = {A ∈ V | A ∗=⇒ x1}.Table entry (3, 5) = {A ∈ V | A ∗=⇒ x3x4x5}.Table entry (1,m) = {A ∈ V | A ∗=⇒ w}.

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The CFL Membership Problem

Example (The CFL Membership Problem)Basic step:

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Example

Example (The CFL Membership Problem)Let the grammar be

S → SS | aSb | bSa | ε

In CNF it is

S0 → SS | AX | YA | AB | BA | εS → SS | AX | YA | AB | BA

X → SB

Y → BS

A → aB → b

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Example

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

A

B

A

B

B

A

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The CFL Membership Problem

Example (The CFL Membership Problem)Inductive step:

For each j ≥ 1, for each i < m, for each rule A→ BC,and for each k from i to i + j− 1,

See whether B is in table entry (i, k) and C is in tableentry (k + 1, i + j).

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

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Example

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

∗=⇒ ab and S ∗=⇒ ab.

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Example

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

A

B

A

B

B

A S0, S

S0, S

S0, S

S0, S

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Example

Example (The CFL Membership Problem)When j = 2, consider the case where i = 1.In this case, i + j = 3, so k may equal 1 or 2.Case k = 1:

Table entry (1, 1) = {A} and table entry (2, 3) = ∅.This produces nothing.

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

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

That is, X 2=⇒ abb.

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Example

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

A S0, S

X, Y

S0, S

S0, S

S0, S

X

Y∅

A

A

B

B

B

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Example

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

A S0, S

X, Y

S0, S

S0, S

S0, S S0, S

S0, S

A

A

B

X

B

B

Y

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Example

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

A

B

A

B

B

A S0, S

X, Y

S0, S

S0, S

S0, S

X

Y

S0, S

S0, S X

Y∅

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Example

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

A

B

A

B

B

A S0, S

X, Y

S0, S

S0, S

S0, S

X

Y

S0, S

S0, S X

Y

S0, S

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The CFL Membership Problem

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

An Algorithm for CFL

Example (The CFL Membership Problem)CFLMember(graph G, string w){

for (int i = 1; i <= m; i++)if ((A → w[i]) ∈ R)

Add A to table[i, i];for (int len = 2; len <= m; len++)

for (int i = 1; i <= m - len + 1; i++){

int j = i + len - 1;for (int k = i; k <= j - 1; k++)

for each ((A → BC) ∈ R)if (B ∈ table[i, k] && C ∈ table[k+1, j])

Add A to table[i, j];}

if (S ∈ table[1, m])accept();

elsereject();

}

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

The CFL Membership Problem

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

The Class P

Robb T.Koether

HomeworkReview

Introduction

Comparisonof Run TimesPolynomial Time

Exponential Time

Factorial Time

The Class PThe PATH Problem

The CFLMembership Problem

Assignment

Assignment

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