Complexity Theory [complexity-theory]

Definition 1. Elephant Jokes [elephant-jokes]

How do you shoot a blue elephant?
With a blue elephant gun.
How do you shoot a red elephant?
Hold its trunk shut until it turns blue, then shoot it with a blue elephant gun.
How do you shoot a purple elephant?
Paint it red, hold its trunk shut until it turns blue, then shoot it with a blue elephant gun.

Elephants are much like reductions.

Definition 2. Reductions [reductions]

Say we have two problems.

Problem 1.
Shoot a red elephant.
Problem 2.
Shoot a blue elephant.

We can say that P1P_{1} reduces to P2P_{2} if we can design an algorithm for solving P1P_{1} that uses P2P_{2}. The following are equivalent:

  • P1TP2P_{1}\underset{T}{\leq}P_{2}
  • P1P_{1} reduces to P2P_{2}
  • P1P_{1} can be solved in terms of P2P_{2}
  • P1P_{1} can be solved using P2P_{2} as a subroutine

IMPORTANT: Note the order! This is easy to flip-flop!

Given our original problems: this means that the problem of shooting a red elephant can be solved by shooting a blue elephant.

What else does this tell us?

  • If P2P_{2} is easy, then P1P_{1} is easy
  • If P1P_{1} is hard, then P2P_{2} is hard (we’ll use this idea a lot)
  • P1P_{1} is no harder than P2P_{2}
  • P2P_{2} is at least as hard as P1P_{1}

We can similarly talk about languages this way.

L1L2L_{1} \leq L_{2} means:

  • deciding L1L_{1} is no harder than deciding L2L_{2}, or
  • deciding L2L_{2} is at least as hard as deciding L1L_{1}.

Example. Showing the Halting problem to be undecidable

Last time: ATMA_{\text{TM}} is undecidable.

  • ATM={M,w:Macceptsw}A_{\text{TM}} = \left\{ {\left\langle {M,w} \right\rangle:M\ \text{accepts}\ w} \right\}
  • HALTTM={M,w:Mhalts on inputw}\text{HALT}_{\text{TM}} = \left\{ {\left\langle {M,w} \right\rangle:M\ \text{halts on input}\ w} \right\}

Recall: HALTTM\text{HALT}_{\text{TM}} is recognizable

  • You can create a Turing machine UU that takes an M,w\left\langle {M,w} \right\rangle and simulate it: if UU halts, our original Turing machine MM accepts

We want to show that ATMTHALTTMA_{\text{TM}}\underset{T}{\leq}\text{HALT}_{\text{TM}}. Proof by contradiction:

  • Suppose there is a Turing machine RR that decides HALTTM\text{HALT}_{\text{TM}} (it doesn’t exist).
  • We want to use RR to design a Turing machine SS that decides ATMA_{\text{TM}}. Design:

    • Use RR to reject if the input runs forever
    • Otherwise, simply simulate the input (it is now guaranteed to be decidable)
  • So we can, and we do, and we arrive at a contradiction: as we know there is no such decider for ATMA_{\text{TM}}.

Then as ATMA_{\text{TM}} reduces to HALTTM\text{HALT}_{\text{TM}}: HALTTM\text{HALT}_{\text{TM}} is at least as hard as ATMA_{\text{TM}}, which is undecidable. So HALTTM\text{HALT}_{\text{TM}} is undecidable.

Aside: reduction can go both ways! ATMA_{\text{TM}} reduces to HALTTM\text{HALT}_{\text{TM}}, and vice versa. There are (probably) problems that cannot be related, however.

Example. Showing that ETME_{\text{TM}} is undecidable

We will show by reduction: by reducing ATMA_{\text{TM}} to ETME_{\text{TM}}, i.e. show a Turing-machine for deciding ATMA_{\text{TM}} that uses ETME_{\text{TM}} as a subroutine.

  • ATM={M,w:Macceptsw}A_{\text{TM}} = \left\{ {\left\langle {M,w} \right\rangle:M\ \text{accepts}\ w} \right\}
  • ETM={M:Mis a Turing machine andL(M)is emptyE_{\text{TM}} = \{\left\langle M \right\rangle:M\ \text{is a Turing machine and}\ L(M)\ \text{is empty} (i.e. MM accepts nothing)

Proof: Assume we have a decider RR that decides ETME_{\text{TM}}. Idea: create a new Turing machine NN with M,wM,w hardcoded inside.

  • if y != w: reject
  • if y == w: then run MM on input ww
  • So the language is either ww or \varnothing.

Simulating a Turing machine SS on input xx:

  • if xx not of form M,w\left\langle {M,w} \right\rangle then reject
  • Construct the Turing-machine NN above
  • Run RR on input NN
  • If RR accepts, then we know MM does not accept ww
  • If RR accepts, SS rejects, and if RR rejects, SS accepts.

Computability vs. Complexity [computability-complexity]

What is the difference between this theory of computation and Turing machines, vs. complexity?

Computability-theory studies whether problems have any algorithmic solution whatsoever.

Complexity theory studies whether problems have efficient algorithmic solutions.

  • How much time is required?
  • How much space is required?
  • Does randomness help?

Definition 1. Worst case analysis [worst-case-analysis]

Usually theorists do worst case analysis, i.e. as a function of the input length nn, what is the maximum amount of resources used over all inputs of length nn? (note: nn is a commonly used variable!)

The idea here is that it’s helpful to analyze the worst case, as every input will then result in equal or better time.

Definition 1. Runtime [runtime]

  1. Complexity theory.

    1. The runtime of a Turing machine MM is f:f:{\mathbb{N}}\rightarrow{\mathbb{N}}: f(n)=max(xΣ*,|x|=n)f(n) = \max\left( {x \in \Sigma \ast ,|x| = n} \right). (the number of steps of MM on input nn until it halts) (assume MM is a decider for simplicity)
    2. The runtime of a nondeterministic Turing machine NTM\text{NTM} on input XX is max-length of any root-leaf path. For deciders – no infinite paths!

Definition 2. Class [class]

  1. Complexity theory. A class is a set of languages (or equivalently, computational problems).

Definition 3. Complexity class [complexity-class]

A complexity class is a class defined by Turing machine resource constraints.

Definition 4. TIME(t(n)) [time-tn]

TIME(t(n))\text{TIME}\left( {t(n)} \right) is the class of all languages LL such that there exists a Turing machine with runtime O(t(n))O\left( {t(n)} \right) that decides LL.

In briefer notation: TIME(t(n))={languageL:TMMthat decidesLin timeO(t(n))}\text{TIME}\left( {t(n)} \right) = \left\{ {\text{language}\ L:\exists\ \text{TM}\ M\ \text{that decides}\ L\ \text{in time}\ O\left( {t(n)} \right)} \right\}

Definition 1. P (complexity class) [p]

P=c>0TIME(nc)P = \bigcup_{c > 0}\text{TIME}\left( n^{c} \right)

Why is P a good definition for efficient computation?

  • Insensitive to model of computation: by the extended Church-Turing thesis, switching computational models “only” involves a polynomial slowdown
  • Closed under composition: can use polynomial time algorithm as a subroutine
  • In practice: algorithms usually can be improved. If we find an O(n30)O\left( n^{30} \right) time algorithm, it typically can be improved to, say, O(n5)O\left( n^{5} \right) time.

Example. 2COLORMAP

Let GG be a “map” of countries in the plane, where a coloring is valid if neighboring countries have different colors.

Question. The two color map does not work for every map. So consider it as a problem: 2COLORMAP={G:Ghas valid red/blue coloring}\text{2COLORMAP} = \left\{ {\left\langle G \right\rangle:G\ \text{has valid red/blue coloring}} \right\}. Is 2COLORMAP \in P?

Answer. Yes! Start with an arbitrary country, set it to red, set their neighbors to blue, and continue. If there is a conflict, reject, if there is no conflict, accept.

It’s unknown if the three color map is in P. It’s known to be in EXP: O(3nn2)O(4n)O\left( {3^{n}n^{2}} \right) \in O\left( 4^{n} \right)

Recall: different machine models have different efficiencies: our multi-tape Turing machines were broadly more efficient than our single-tape Turing machines.

Example.

PAL={wwR:wΣ*}\text{PAL} = \left\{ {ww^{R}:w \in \Sigma \ast} \right\}

  • Decided by a single-tape Turing machine in O(n2)O\left( n^{2} \right) time
  • Decided by a multi-tape Turing machine in O(n)O(n) time

Revising the Church-Turing thesis [extended-church-turing]

The extended Church-Turing thesis is the belief that Turing machines capture our intuitive notion of what is efficiently computable. It postulates that everything we can compute in time t(n)t(n) on any physical computer can be computed on a Turing machine in time O(t(n)c)O\left( {t(n)}^{c} \right), for some constant cc.

Is this true, does it hold? Maybe not, with newer forms of computation like quantum computers.

Definition 3. P and EXP [p-exp]

Definition 1. P (complexity class) [p]

P=c>0TIME(nc)P = \bigcup_{c > 0}\text{TIME}\left( n^{c} \right)

Why is P a good definition for efficient computation?

  • Insensitive to model of computation: by the extended Church-Turing thesis, switching computational models “only” involves a polynomial slowdown
  • Closed under composition: can use polynomial time algorithm as a subroutine
  • In practice: algorithms usually can be improved. If we find an O(n30)O\left( n^{30} \right) time algorithm, it typically can be improved to, say, O(n5)O\left( n^{5} \right) time.

Example. 2COLORMAP

Let GG be a “map” of countries in the plane, where a coloring is valid if neighboring countries have different colors.

Question. The two color map does not work for every map. So consider it as a problem: 2COLORMAP={G:Ghas valid red/blue coloring}\text{2COLORMAP} = \left\{ {\left\langle G \right\rangle:G\ \text{has valid red/blue coloring}} \right\}. Is 2COLORMAP \in P?

Answer. Yes! Start with an arbitrary country, set it to red, set their neighbors to blue, and continue. If there is a conflict, reject, if there is no conflict, accept.

It’s unknown if the three color map is in P. It’s known to be in EXP: O(3nn2)O(4n)O\left( {3^{n}n^{2}} \right) \in O\left( 4^{n} \right)

Definition 2. EXP [exp]

$`\text{EXP}` = \bigcup_{k > 0}\text{TIME}\left( 2^{n^{k}} \right)$

The four color map works for all maps, so 4COLORMAP={G:Gis a valid map}\text{4COLORMAP} = \left\{ {\left\langle G \right\rangle:G\ \text{is a valid map}} \right\}.

Theorem 1. Time Hierarchy Theorem [time-hierarchy]

Question: is Exp == P (complexity class)? Answer: No!

The time hierarchy theorem tells us that given an f:f:{\mathbb{N}}\rightarrow{\mathbb{N}} that is “reasonable” where f(n)=Ω(nlogn)f(n) = \Omega\left( {n\log n} \right), then the TIME(f(n))TIME(f(n)4)\text{TIME}\left( {f(n)} \right) \subset \text{TIME}\left( {f(n)}^{4} \right).

Notably, TIME(f(n))TIME(f(n)4)\text{TIME}\left( {f(n)} \right) \neq \text{TIME}\left( {f(n)}^{4} \right)!

Corollary.

TIME(nc)TIME(n4c)\text{TIME}\left( n^{c} \right) \neq \text{TIME}\left( {n^{4}c} \right) for any c1c \geq 1.

Corollary.

TIME(nc)TIME(2n)\text{TIME}\left( n^{c} \right) \neq \text{TIME}\left( 2^{n} \right) for any c1c \geq 1.

TIME(2nk)TIME\text{TIME}\left( 2^{n^{k}} \right) \neq \text{TIME}

Question. How do you prove the Time hierarchy theorem?

Answer. By diagonalization. Look at Turing machines that are “fast” and Turing machines that are “slow”.

Idea: Look at all Turing machines that run in “fast” time, and come up with a language that they cannot possibly decide.

But we want that language to be decidable by a “slow” Turing machine.

Language that is in EXP and not in P: ???

Time Complexity [time-complexity]

Where is 3COLORMAP? We know it is in Exp, and either in Np (complexity class) or in both Np (complexity class) and P (complexity class).

The Time Hierarchy theorem tells us that there is something in Exp \smallsetminus P (complexity class). (why? still not clear)

Figure 1: Complexity hierarchy

Theorem 2. Certificates and Verifiers [certificates-verifiers]

NP has two main, equivalent definitions:

  1. There exists a nondeterministic polytime TM MM that decides LL.
  2. There exists a deterministic polytime TM VV and a constant cc s.t.:

    • L={xΣ*:yΣ*s.t.|y||x|cVaccepts(x,y)}L = \left\{ {x \in \Sigma \ast :\exists y \in \Sigma \ast \text{s.t.}\ |y| \leq |x|^{c} \land V\ \text{accepts}\ \left( {x,y} \right)} \right\}
    • where xx is the “input”, yy is the “certificate”, VV is the “verifier”

i.e. for the three color graph, give us a colored graph and we can verify it

The certificate yy serves as the verifier VV’s substitute for nondeterminism.

Example. Verifying 3colormap

Claim: 3colormap \in Np (complexity class).

  • {G:Gis 3-colorable}\left\{ {\left\langle G \right\rangle:G\ \text{is 3-colorable}} \right\}
  • Must find a deterministic polytime TM V s.t. 3COLOR={x:y,|y||x|c,Vaccepts(x,y)}\text{3COLOR} = \left\{ {x:\exists y,|y| \leq |x|^{c},V\ \text{accepts}\ \left( {x,y} \right)} \right\}
  • Code for VV: on input (x,y)\left( {x,y} \right)

    • If xx not of form , where is a Graph, reject
    • From yy, extract values color(v){1,2,3}\text{color}(v) \in \left\{ {1,2,3} \right\} for each vVv \in V
    • For every edge {u,v}E\left\{ {u,v} \right\} \in E: if color(u) == color(v), reject
    • Otherwise accept
  • yy does exist, it’s simply a valid coloring of the graph

To show correctness of VV, we need three things:

  1. If x=Gx = \left\langle G \right\rangle and G is 3-colorable: need ys.t.|y||x|c\exists y\ \text{s.t.}\ |y| \leq |x|^{c} and Vaccepts(x,y)V\ \text{accepts}\ \left( {x,y} \right).

    • It does exist, yy can be a valid 3 coloring of GG. Note: |y|=O(n)|y| = O(n) and |x|=Ω(n)|x| = \Omega(n)
  2. If xGx \neq \left\langle G \right\rangle where G is 3-colorable: then no certificate yy should make VV erroneously accept.

    • If xx not of form G\left\langle G \right\rangle: vv definitely rejects.
    • If xx of form G\left\langle G \right\rangle by not 3-colorable, then VV must reject because it always checks that yy is a valid coloring.
  3. Must check that VV runs in time polynomial in |xy||xy|.

Example. Proofs of equivalence of our NP definitions

Proof. (2)(1)(2)\Rightarrow(1)

  • Given verifier VV, must construct a NTM MM deciding LL
  • Inputs to the verifier are xx and yy, with |y||x|c|y| \leq |x|^{c}

  • Code for MM:

    • On input xx, nondeterministically pick yΣ*y \in \Sigma \ast with |y||x|c|y| \leq |x|^{c}
    • Run VV on input (x,y)\left( {x,y} \right)
    • Accept if VV accepts. Reject if VV rejects.

    i.e. Suppose xLys.t.Vaccepts(x,y)there is a leaf whereMacceptsx \in L\Rightarrow\exists y\ \text{s.t.}\ V\ \text{accepts}\ \left( {x,y} \right)\Rightarrow\text{there is a leaf where}\ M\ \text{accepts}. If xLys.t.Vaccepts(x,y)leafx \notin L\Rightarrow\nexists y\ \text{s.t}.V\ \text{accepts}\ \left( {x,y} \right)\Rightarrow\nexists\ \text{leaf}, so MM does not accept xx.

Proof. (1)(2)(1)\Rightarrow(2)

  • Given an NTM MM, you can convert that into a verifier VV for LL.
  • Input to MM is xx, runtime is at most |x|c|x|^{c}
  • Idea: VV simulates the tree, but only on a single branch of configuration tree. yy specifies which branch. nondeterminism comes into play by letting us pick the correct branch.

    • our nondeterminism lets us pick the correct branch: then the cc term is the depth of the path, which is at most polynomial
  • Code for MM:

    • Simulate MM deterministically on input xx, using yy as the sequence of nondeterministic decisions
    • Accept if MM accepts. Reject if MM rejects.

Why NP is nice [cpsc/why-np]

We’ll spend the next couple of lectures talking about Np (complexity class). Why? Nondeterministic Turing machines are ridiculously impractical!

Many problems that we’re interested in have the form L={x:y,|y||x|c,Vaccepts(x,y)}L = \left\{ {x:\exists y,|y| \leq |x|^{c},V\ \text{accepts}\ \left( {x,y} \right)} \right\} (where xx is the problem input and yy is the object we are seeking).

Efficient test: is yy a valid solution?

We showed 3COLOR \in Np (complexity class).

Np (complexity class) has lots of problems that are interesting and nice to solve, but that we don’t know of any polynomial time solution. But: if any of these problems are in P (complexity class), then all of them are in P (complexity class). NP-completeness!