knowledge

Theory of Computation: Lecture 7

Worst-case time complexity, asymptotic notation, TIME and P, polynomial-time verification, NP, nondeterministic polynomial time, and the P versus NP question.

Worst-Case Time

Decidability asks whether a Turing machine always gives the right answer and halts. Complexity asks how long a decider takes as the input length grows. Recognizers are not enough for this purpose: if a machine may loop on no-instances, its worst-case time may be infinite.

Worst-case running time. Let MM be a Turing-machine decider over alphabet Σ\Sigma. The running time, or time complexity, of MM is the function t:NNt:\mathbb{N}\to\mathbb{N} defined by

t(n)=maxwΣw=n{number of steps M takes on w}.t(n)=\max_{\substack{w\in\Sigma^*\\ |w|=n}} \{\text{number of steps }M\text{ takes on }w\}.

The maximum exists because there are finitely many strings of length nn, and MM halts on every one of them.

For each input length, keep the slowest input of that length and forget the easier ones.

If a decider has step counts 2,9,102,9,10 on the length-11 inputs over some alphabet, then t(1)=10t(1)=10. If its length-33 inputs take at most 8585 steps, then t(3)=85t(3)=85. Different deciders for the same language may have different running-time functions.

Asymptotic Time Classes

Exact step counts are too sensitive to machine details. Complexity theory compares the eventual growth of running-time functions, ignoring constant factors and finitely many small inputs.

Big-O notation. For functions f,g:NR0f,g:\mathbb{N}\to\mathbb{R}_{\ge 0}, write f(n)=O(g(n))f(n)=O(g(n)) if there exist constants C>0C>0 and n0Nn_0\in\mathbb{N} such that for every nn0n\ge n_0,

f(n)Cg(n).f(n)\le C\cdot g(n).

Small-o notation. Write f(n)=o(g(n))f(n)=o(g(n)) if for every constant c>0c>0, there exists n0Nn_0\in\mathbb{N} such that for every nn0n\ge n_0,

f(n)<cg(n).f(n)<c\cdot g(n).

Big-O means "eventually no more than a constant multiple." Small-o means "eventually smaller than every constant multiple."

Recipe: comparing growth rates.

  1. To prove f(n)=O(g(n))f(n)=O(g(n)), choose concrete constants CC and n0n_0, then prove f(n)Cg(n)f(n)\le Cg(n) for all nn0n\ge n_0.
  2. To disprove f(n)=O(g(n))f(n)=O(g(n)), show that f(n)/g(n)f(n)/g(n) is unbounded.
  3. To prove f(n)=o(g(n))f(n)=o(g(n)), show that f(n)/g(n)0f(n)/g(n)\to 0.

Example 1. Common comparisons.

5n3+1=O(2n)5n^3+1=O(2^n) because exponentials eventually dominate polynomials. But 5n3+1O(20n+5)5n^3+1\ne O(20n+5) because the ratio grows like n2/4n^2/4. Also n=o(n)\sqrt n=o(n), while an eventually positive function never satisfies f(n)=o(f(n))f(n)=o(f(n)).

Time complexity class. For a time bound t(n)t(n),

TIME(t(n))={LΣL is decided by a TM in time O(t(n))}.\mathrm{TIME}(t(n)) = \{L\subseteq\Sigma^*\mid L\text{ is decided by a TM in time }O(t(n))\}.

A language belongs to TIME(t(n))\mathrm{TIME}(t(n)) if some decider for it has that asymptotic worst-case bound. A slower decider for the same language does not affect membership in the class.

Typical containments follow directly from growth rates:

TIME(n)TIME(n2)TIME(2n)TIME(2n).\mathrm{TIME}(n) \subseteq \mathrm{TIME}(n^2) \subseteq \cdots \subseteq \mathrm{TIME}(2^{\sqrt n}) \subseteq \mathrm{TIME}(2^n).

Every regular language is in TIME(n)\mathrm{TIME}(n): simulate its DFA and take one constant-time transition per input symbol.

Polynomial time (PP). PP is the class of languages decidable in polynomial time by a deterministic Turing machine:

P=k1TIME(nk).P=\bigcup_{k\ge 1}\mathrm{TIME}(n^k).

P is the coarse mathematical model of efficient deterministic decision.

Example 2. Languages in PP.

The language of sorted integer arrays is in PP: scan adjacent entries and reject at the first inversion. The graph path language

{G,s,ts and t are connected in G}\{\langle G,s,t\rangle\mid s\text{ and }t\text{ are connected in }G\}

is in PP by breadth-first search. The language of connected graphs is in PP by running one graph search from an arbitrary vertex and checking whether every vertex is reached.

Certificates and Verification

Many decision problems have a useful split between finding a solution and checking a proposed solution. The proposed solution is extra data, not part of the original instance.

Certificate. A string CC intended to prove that an instance xx is a yes-instance. A verifier receives x,C\langle x,C\rangle and checks whether CC really proves membership.

A decider must answer from xx alone. A verifier is allowed to be handed the missing clue.

Verification view of a languagexCverifier Vaccept?pairedx in L iff some short C makes V acceptx not in L iff every C is rejected
VerificationxCverifier Vaccepts iff C proves x is yesyes: some short C acceptsno: every C rejects
A verifier checks a proposed certificate; it does not have to find one.

Conjunctive normal form (CNF). A Boolean formula in CNF is an AND of clauses, where each clause is an OR of literals, and each literal is a variable or its negation.

Satisfying assignment. A Boolean assignment to the variables of a formula φ\varphi that makes φ\varphi true.

SAT-verify. The language

SAT-verify={φ,CC is a satisfying assignment of φ}\mathrm{SAT\text{-}verify} = \{\langle \varphi,C\rangle\mid C\text{ is a satisfying assignment of }\varphi\}

is in PP. Substitute the assignment into the formula and check that every clause contains at least one true literal.

SAT. The satisfiability problem is

SAT={φφ is satisfiable}.\mathrm{SAT} = \{\langle \varphi\rangle\mid \varphi\text{ is satisfiable}\}.

Equivalently,

φSATC such that φ,CSAT-verify.\langle\varphi\rangle\in\mathrm{SAT} \quad\Longleftrightarrow\quad \exists C\text{ such that }\langle\varphi,C\rangle\in\mathrm{SAT\text{-}verify}.

A brute-force decider for SAT tries every truth assignment and runs the verifier on each one. This always halts, but for mm variables it may check 2m2^m assignments.

Example 3. SAT certificate.

For

φ=(xyz)(¬x¬y)(x¬z),\varphi=(x\vee y\vee z)\wedge(\neg x\vee \neg y)\wedge(x\vee \neg z),

the assignment x=truex=\mathrm{true}, y=falsey=\mathrm{false}, z=truez=\mathrm{true} is a certificate: each clause has at least one true literal.

Graph problems show the same pattern with certificates that are mappings or subsets.

Graph isomorphism. A graph isomorphism from G1G_1 to G2G_2 is a bijection f:V(G1)V(G2)f:V(G_1)\to V(G_2) that preserves adjacency:

{u,v}E(G1){f(u),f(v)}E(G2).\{u,v\}\in E(G_1) \quad\Longleftrightarrow\quad \{f(u),f(v)\}\in E(G_2).

Two graphs are isomorphic if at least one such bijection exists.

GI-verify.

GI-verify={G1,G2,CC is a graph isomorphism G1G2}.\mathrm{GI\text{-}verify} = \{\langle G_1,G_2,C\rangle\mid C\text{ is a graph isomorphism }G_1\to G_2\}.

This verifier is polynomial-time: check that CC is a bijection, then check the adjacency equivalence for every pair of vertices.

Graph isomorphism language.

GI={G1,G2G1 and G2 are isomorphic}.\mathrm{GI} = \{\langle G_1,G_2\rangle\mid G_1\text{ and }G_2\text{ are isomorphic}\}.

Equivalently, G1,G2GI\langle G_1,G_2\rangle\in\mathrm{GI} iff some certificate CC makes G1,G2,CGI-verify\langle G_1,G_2,C\rangle\in\mathrm{GI\text{-}verify}.

Independent set. An independent set in a graph GG is a subset SV(G)S\subseteq V(G) such that no two vertices in SS are adjacent.

INDSET-verify.

INDSET-verify={G,k,CC is an independent set of size k in G}.\mathrm{INDSET\text{-}verify} = \{\langle G,k,C\rangle\mid C\text{ is an independent set of size }k\text{ in }G\}.

This verifier is polynomial-time: check that C=k|C|=k, then check every pair u,vCu,v\in C and reject if {u,v}E(G)\{u,v\}\in E(G).

Independent set language.

INDSET={G,kG has an independent set of size k}.\mathrm{INDSET} = \{\langle G,k\rangle\mid G\text{ has an independent set of size }k\}.

Equivalently, G,kINDSET\langle G,k\rangle\in\mathrm{INDSET} iff some certificate CC makes G,k,CINDSET-verify\langle G,k,C\rangle\in\mathrm{INDSET\text{-}verify}.

GI certificatecbdaef123456bijection Ca->1b->2c->3d->4e->5f->6INDSET certificate123456S={1,3,5}no selected edge existsGI checks all adjacencies under the bijection.INDSET checks all pairs inside the chosen subset.
GI certificatecbdaef123456C: a->1, b->2, c->3d->4, e->5, f->6INDSET certificate123456S={1,3,5}no selected edge existsverification checks pairsrather than finding the set
Certificates can be concrete structures: a vertex bijection for GI, or a vertex subset for INDSET.

For SAT, GI, and INDSET, verification is polynomial. The direct search algorithms are still expensive: enumerate all assignments, all bijections, or all vertex subsets.

Verifiers and NP

The certificate pattern becomes a machine model when quantified over all instances.

Verifier for a language. A Turing machine VV is a verifier for LL if it halts on certificate pairs and, for every input xx,

  • if xLx\in L, then there exists a certificate CC such that VV accepts x,C\langle x,C\rangle;
  • if xLx\notin L, then for every certificate CC, VV rejects x,C\langle x,C\rangle.

Polynomial-time verifier. A verifier is polynomial-time if there is a polynomial pp such that yes-instances have relevant certificates of length at most p(x)p(|x|), and VV checks those pairs in time polynomial in x|x|.

Equivalently, LL has a polynomial-time verifier if there is a polynomial-time TM VV and a polynomial pp such that

xLC, Cp(x) and V accepts x,C.x\in L \quad\Longleftrightarrow\quad \exists C,\ |C|\le p(|x|) \text{ and }V\text{ accepts }\langle x,C\rangle.

For xLx\notin L, no certificate of the allowed length can make VV accept.

NP. The class NPNP is the class of languages that have polynomial-time verifiers.

NP means yes-instances have short certificates that can be checked quickly.

ModelInputYes-instance conditionNo-instance condition
Deciderxxaccepts xxrejects xx
Verifierx,C\langle x,C\ranglesome CC is acceptedevery CC is rejected

By the verifiers above,

SAT, GI, INDSETNP.\mathrm{SAT},\ \mathrm{GI},\ \mathrm{INDSET}\in NP.

Nondeterministic Polynomial Time

The name NPNP comes from an equivalent nondeterministic machine definition.

Nondeterministic Turing machine (NTM). A Turing machine whose transition rule may offer several legal next moves:

δ:Q×ΓP(Q×Γ×{L,R}).\delta:Q\times\Gamma\to\mathcal{P}(Q\times\Gamma\times\{L,R\}).

One input may therefore generate a tree of computations rather than a single run.

A nondeterministic machine accepts if at least one branch finds a successful path.

Nondeterministic decider. An NTM NN decides LL if every computation branch halts and, for every input xx,

  • if xLx\in L, then at least one computation of NN on xx accepts;
  • if xLx\notin L, then every computation of NN on xx rejects.

Polynomial-time NTM. An NTM runs in polynomial time if the longest computation branch on any input of length nn has length bounded by a polynomial in nn.

Theorem. A language has a polynomial-time verifier if and only if it has a polynomial-time nondeterministic decider.

Proof sketch.

  1. Verifier to NTM. On input xx, nondeterministically write a certificate CC of polynomial length, then run the verifier on x,C\langle x,C\rangle. If a valid certificate exists, one branch accepts. If no valid certificate exists, every branch rejects.
  2. NTM to verifier. Let CC encode the sequence of nondeterministic choices along one computation branch. The verifier simulates the NTM on xx, following the choices in CC, and accepts exactly when that branch accepts.

Nondeterministic time class.

NTIME(t(n))={LL has a nondeterministic O(t(n))-time decider}.\mathrm{NTIME}(t(n)) = \{L\mid L\text{ has a nondeterministic }O(t(n))\text{-time decider}\}.

Using the theorem,

NP=k1NTIME(nk).NP=\bigcup_{k\ge 1}\mathrm{NTIME}(n^k).

This is the same class defined by polynomial-time verifiers.

P versus NP. Every language in PP is in NPNP: a polynomial-time decider can be used as a verifier that ignores the certificate. Thus

PNP.P\subseteq NP.

Whether P=NPP=NP is open. The question asks whether every problem whose yes-instances can be checked quickly also has yes/no answers that can be found quickly by a deterministic algorithm.

possible: P=NPP=NPSATINDSETGIpossible: P proper subset NPNPPSATINDSETGI?SAT and INDSET are NP-complete: putting either in P would force P=NP.
possible: P=NPP=NPSATINDSETGIpossible: P proper subset NPNPPSATINDSETGI?SAT or INDSET in Pwould force P=NP
P is contained in NP. Whether the containment is strict is the P versus NP problem.

Cook-Levin theorem (informal). SAT is complete for NPNP: a polynomial-time algorithm for SAT would give polynomial-time algorithms for every language in NPNP. Therefore

SATPP=NP.\mathrm{SAT}\in P \quad\Longleftrightarrow\quad P=NP.

The same is true for INDSET:

INDSETPP=NP.\mathrm{INDSET}\in P \quad\Longleftrightarrow\quad P=NP.