<<

Chapter 8: Memory, Paths, and Games slides © 2019, David Doty ECS 220: Theory of Computation based on “The Nature of Computation” by Moore and Mertens

- Space versus time

PSPACE = problems decidable with polynomial memory

You can reuse space, but you can’t reuse time.

Leads to unintuitive results: • nondeterminism doesn’t help space-bounded computation: PSPACE = NPSPACE • proving something doesn’t exist is as easy as proving it exists: NPSPACE = coNPSPACE

But some intuitions hold: • with more space, you can compute more: SPACE(o(t)) SPACE(t) • if time is bounded, space is also bounded: TIME(t) SPACE(t) • if space is bounded, time is also bounded: SPACE(t) ⊊TIME(2O(t)) ⊆ Biggest open question: does space help more than⊆ time? P ≠ PSPACE?

Chapter 8 2 Read-only and write-only memory • Sublinear-time computation largely uninteresting for Turing machines. • Somewhat interesting for RAM machines, e.g., binary search. • Sublinear-space computation makes more sense, e.g., for searching the graph G=(V,E), where V = { web pages} • Cannot load input into memory. • Formalized by giving /RAM machine read-only input, but read-write working memory… only latter is counted as space usage. • To talk about writing more output than allowed space usage, use a write-only output. (irrelevant for Boolean output, but not for space-bounded reductions) • Textbook assumes RAM (random access memory) in this chapter. • given input/working memory location i, read/write to location i takes one step (note this only matters if we also care about the time) • note: if size of input/working memory is k, takes log(k) bits to write i

8.1: Welcome to the State Space 3 Space-bounded complexity classes

• SPACE(s(n)) = class of problems solvable with O(s(n)) working memory on inputs of size n

= SPACE(log n)

• PSPACE = SPACE() � 𝑐𝑐∈ ℕ

8.1: Welcome to the State Space 4 Logarithmic space example deciding palindromes

def palindrome(x): i = 1 : j = |x| log n for i while i < j: if x[i] != x[j]: log n for j return False i += 1 j -= 1 return True

8.1: Welcome to the State Space 5 Polynomial space example def periodic_orbit(ca,init): n = |init| checking if a configuration is in a periodic orbit of a x = init cellular automaton for j = 1..2n: x = update(ca, x) if x = init: return True return False def update(ca, x): y = "" for i = 0..|x|-1: l = x[(i-1) mod |x|] m = x[i] = x[(i+1) mod |x|] y.append(ca(l,m,r)) memory: init, x, y, j (n bits each) and i (log n bits) return y 8.1: Welcome to the State Space 6 Time bounds versus space bounds

Assumption: a program allocates O(1) bits per time step… then O(t(n)) bits allocated total, space bound s(n) = O(t(n)). TIME(t) SPACE(t) P PSPACE

⊆ ⊆ If a program uses O(s(n)) bits, it has at most 2O(s(n)) configurations. If it repeats one it runs forever… so if it halts on all inputs, t(n) = 2O(s(n)) SPACE(s) TIME(2O(s)) L P PSPACE EXP

⊆ ⊆ ⊆

8.1: Welcome to the State Space 7 Nondeterministic time versus deterministic space

Recall NTIME(t) TIME(2O(t)) (e.g., NP EXP)

⊆ ⊆ memory used: def exhaustive_search_A(x): n = |x| witness-length(n) ≤ t(n) for each w in {0,1}≤witness-length(n): if V (x,w) = True: space(VA) ≤ t(n) A return True return False

NTIME(t) SPACE(t) NP PSPACE

⊆ 8.1: Welcome to the State Space⊆ 8 Putting all relationships together

L P NP PSPACE EXP NEXP EXPSPACE

(Time Hierarchy Theorem) ⊆ P ⊆ ⊆ ⊆ EXP ⊆ ⊆ NP NEXP (Nondeterministic ⊊ Time Hierarchy Theorem) L PSPACE⊊ EXPSPACE Space⊊ Hierarchy Theorem⊊: If s1 = o(s2), then SPACE(s1) SPACE(s2) 8.1: Welcome to the State Space 9 ⊊ Nondeterministic space-bounded computation

• Textbook goes through some “prover/verifier” formulations. Key difference with NP: witnesses can be exponential length, for example, sequence of moves in a sliding block puzzle or chess game. • I prefer the “nondeterministic program” formulation: • NSPACE(s) = problems solvable by a nondeterministic program using space O(s(n)) on inputs of size n. • [correct answer = yes] [some computation path accepts] • [correct answer = no] [no computation path accepts] ⇒ n • NL = NSPACE(log ) ⇒ • NPSPACE = NSPACE(nc) HuaRongDao, Wikipedia � 8.2: Show Me the Way 10 𝑐𝑐∈ ℕ Prover/Verifier characterization of NSPACE(s)

• verifier V • input x • witness/proof w • |w| is arbitrary • V has read-only access to both x and w • RAM access to x • sequential access to bits of w from-left-to-right (like a DFA) (*) • x is a yes-instance ( w) V(x,w) accepts • Why (*)? Otherwise we could encode NP-complete problems using only ⇔ ∃ logarithmic space, e.g., HAMPATH would be in NL, so we would have NL = NP.

8.2: Show Me the Way 11 Reachability

• REACHABILITY: • Given: directed graph G=(V,E) and two nodes s,t in V • Question: is there a path from s to t in G? def reachable(G=(V,E),s,t): u = s • Claim: REACHABILITY NL. num_searched = 1 Why? while u != t: memory needed: ∈ v = guess neighbor of u u = v u,v,num_searched (log |V|) if num_searched = |V|: return False num_searched += 1 return True

8.2: Show Me the Way 12 The long computational reach of REACHABILITY

• If our nondeterministic program has space s, we can search graphs of size to 2s (i.e., internet-sized graphs) • Flip this around: every nondeterministic program is defined completely by its configuration reachability graph: • V = set of configurations of the program (state of memory) • (u,v) E iff there is a nondeterministic transition from u to v • can assume single accepting configuration a (TM erases all tapes before halting) ∈ • deterministic programs have a line graph; nondeterministic is more general • So every problem in NSPACE(s) is equivalent to a REACHABILITY problem on a graph O(s) of size 2 : given input x with starting configuration cx, can we reach from cx to a? • REACHABILITY on G=(V,E) is solvable using DFS in time O(|V|+|E|) = O(2O(s) + 2O(s)) O(s) NSPACE(s) TIME(2 ) NL P NPSPACE EXP O(1) degree

8.2: Show Me the Way 13 ⊆ ⊆ ⊆ NL-completeness

• Previous slide: • REACHABILITY NL • every problem in NL is equivalent to a REACHABILITY problem on a polynomial- size graph ∈ • We’ll define NL-completeness, and show REACHABILITY is NL-complete. • L = NL REACHABILITY L.

⇔ ∈

8.3: L and NL-completeness 14 Logspace reductions

• A f:{0,1}*  {0,1}* from A to B is logspace if computable by a O(log n)-space bounded program. Write A ≤L B. • input is read-only • output is write-only • worktape is read/write; only worktape counts against space usage • Most reductions used in NP-completeness proofs are logspace • e.g., to reduce CLIQUE to INDEPENDENT-SET, to determine whether to add edge {u,v} to output, one need only ask whether {u,v} is edge in input graph • B is NL-complete if B NL and B is NL-hard: for all A NL, A ≤L B. • Claim: logspace reductions are transitive: A ≤L B and B ≤L C A ≤L C. ∈ ∈ Why? can be very slow! lots of recomputation of already-computed bits just to save space 8.3: L and NL-completeness ⇒ 15 First NL-complete problem

• NL-WITNESS-EXISTENCE: • Given: nondeterministic program P, input x, integer k in unary (string 1k) • Question: Is there a sequence of guesses P(x) can make so it accepts while using at most log k bits of memory? • NL-WITNESS-EXISTENCE is NL-hard: For any A NL, decided by c·log(n)-space-bounded program P, to reduce A to c NL-WITNESS-EXISTENCE, on input x, output (P, x, k), where k = ??n need to count how many 1’s ∈ reduction has written; takes • NL-WITNESS-EXISTENCE NL: log(nc) = c·log n bits to store nondeterministic program Q deciding if (P, x, k) NL-WITNESS-EXISTENCE: run P(x), checking to ensure∈ space usage never exceeds log k. Since k is given in unary, Q uses space log k ≤ log n, where n = |(P,x∈,k)|.

8.3: L and NL-completeness 16 REACHABILITY is NL-complete

• reduction showing NL-WITNESS-EXISTENCE ≤L REACHABILITY • input (P,x,k), output (G,s,t) What are G, s, and t? • want P(x) accepts using ≤ log k space there’s a path from s to t in G • G = k-space-bounded configuration reachability graph of P • V = { configurations of P using ≤ log k space⇔ } • E = { (u,v) | P goes from u to v in one step }

• s = starting configuration cx on input x • t = accepting configuration

17 Simulating nondeterminism deterministically

• Seems to incur exponential time overhead: • NTIME(t) TIME(2O(t)), and we don’t know how to do better with time

• We can do much⊆ better with space, incurring only a quadratic overhead. • Savitch’s Theorem: For any s(n) ≥ log n, NSPACE(s) SPACE(s2). • Corollary: NPSPACE = PSPACE ⊆ • Corollary: NL SPACE(log2 n) (polylogarithmic, but not logarithmic) But says L = SPACE(log n) ≠ SPACE(log2 n), so we still don’t know whether⊆ L = NL.

8.4: Middle-first search and nondeterministic space 18 REACHABILITY SPACE(log2 n) • BFS and DFS use linear memory • to avoid visiting the∈ same node twice, must store all the visited nodes • Savitch’s algorithm (“middle-first search”) visits each node repeatedly s s s k/4 Let s,t V and k > 0. Write to denote k/2 “there is a path of length ≤ k from𝑘𝑘 s to t.” u1 k/4 ∈ 𝑠𝑠 → 𝑡𝑡 / / k Observation: and u u 𝑘𝑘 𝑘𝑘 2 𝑘𝑘 2 k/4 def reachable(G=(V,E),𝑠𝑠 → 𝑡𝑡 ⟺s, t,∃ k):𝑢𝑢 ∈ 𝑉𝑉 𝑠𝑠 𝑢𝑢 𝑢𝑢 𝑡𝑡 u if k = 1: return [(s,t) E] or [s = t] k/2 2 k/4 for u in V: if reachable(G, s, u, ∈k/2) and reachable(G, u, t, k/2): t t t return True return False

8.4: Middle-first search and nondeterministic space 19 Space usage of Savitch’s algorithm

def reachable(G=(V,E), s, t, k): if k = 1: return [(s,t) E] or [s = t] G = (V,E) for u in V: if reachable(G, s, u, ∈k/2) and reachable(G, u, t, k/2): n = |V| return True return False • Each level of recursion has variables s, t, u, k, each taking log n bits. • First call is reachable(G, s, t, n), with log n levels of recursion. • So O(log2 n) total bits required at deepest level of recursion, i.e., REACHABILITY SPACE(log2 n). • Recall that predicting whether nondeterministic program will accept using at most s(n) space is ∈ equivalent to REACHABILITY on graph of size 2s(n), i.e., for any s(n) ≥ log n, NSPACE(s) SPACE(s2). • Open question: Is Savitch’s algorithm optimal? Is there a ⊆ • log1.99(n)-space algorithm? • log(n)-space algorithm? This would imply L = NL… not as unbelievable as P = NP

8.4: Middle-first search and nondeterministic space 20 Logical definitions of complexity classes

|w| = poly(|x|) VA is poly-time

A NP means x A ( w) VA(x,w) accepts

Turns out…∈ ∈ ⇔ ∃ PSPACE poly(|x|) k = O(1) B ΣkP means x B ( w1)(Ɐw2)( w3) … (Ɐwk) VB(x,w1,w2,w3, … ,wk) accepts ∈ ∈ ⇔ ∃ ∃ V |wi| = poly(|x|) B is poly-time

8.6: PSPACE, Games, and Quantified SAT 21 Expressing REACHABILITY with logical quantifiers

• Desired predicate to define: R(s,t,k) = “there is a path from s to t of length at most k” • Basic predicate: E(u,v) = “(u,v) is an edge or u=v” R(u,v,1) • Use only “first-order” quantifiers: ( u) refers to a node u (not a set of nodes, which would be “second-order”) ⇔ ∃ • R(s,t,k) ( u1)( u2)…( uk-1): E(s,u1) E(u1,u2) … E(uk-1,t) • Caveat: the quantified logical formula must be size poly(input), yet we will use ⇔graphs∃ exponentially∃ ∃ larger than∧ the input:∧ ∧k could be huge

8.6: PSPACE, Games, and Quantified SAT 22 Expressing REACHABILITY with logical quantifiers

Idea: middle-first search (recall Savitch’s Theorem)

R(s,t,k) ( uk/2): R(s,uk/2,k/2) R(uk/2,t,k/2) ( uk/4)( uk/2)( u3k/4): R(s,uk/4,k/4) R(uk/4,uk/2,k/4) R(uk/2,u3k/4,k/4) R(u3k/4,t,k/4) …⇔ ∃ ∧ ⇔ ∃ ∃ ∃ ∧ ∧ ∧ ( u1)( u2)…( uk-1): R(s,u1,1) R(u1,u2,1) … R(uk-1,t,1) ( u1)( u2)…( uk-1): E(s,u1) E(u1,u2) … E(uk-1,t) [same as first attempt ] ⇔ ∃ ∃ ∃ ∧ ∧ ∧ is⇔ shorthand∃ ∃ for∃ a long string of ORs:∧ ∧ ∧

( u V) φ(u) φ(u1) φ(u2) … φ(un) if V = {u1, u2, … ,un} ∃ Ɐ∃is ∈shorthand⇔ for a long∨ string∨ of ANDs:∨ (Ɐu V) φ(u) φ(u1) φ(u2) … φ(un) if V = {u1, u2, … ,un} 8.6: PSPACE, Games, and Quantified SAT 23 ∈ ⇔ ∧ ∧ ∧ Expressing REACHABILITY with logical quantifiers

R(s,t,k) ( u1): R(s,u1,k/2) R(u1,t,k/2) replace with Ɐ ( u ) (Ɐ(a ,b ) { (s,u ), (u ,t) } ): R(a ,b ,k/2) ⇔ ∃ 1 1 1 ∧ 1 1 1 1 ∧ ⇔ (∃u1) (Ɐ(a1,b1) ∈ { (s,u1), (u1,t) } ) ??

⇔ (∃u2) (Ɐ(a2,b2) ∈ { (a1,u2),(u2,b1) } ): R(a2,b2,k/4) … ∃ ∈ ( u1) (Ɐ(a1,b1) { (s,u1), (u1,t) } ) size of formula = O(log k) (i.e., can handle ( u2) (Ɐ(a2,b2) { (a1,u2), (u2,b1) } ) m = log k exponential-size graph ⇔ …∃ ∈ ∃ ∈ with polynomial-size ( um) (Ɐ(am,bm) { (am-1,um), (um,bm-1) } ): E(am,bm) formula)

∃ ∈ 8.6: PSPACE, Games, and Quantified SAT 24 REACHABILITY as a game between prover and skeptic

Prover: I claim we can get from s to t in k steps, through midpoint u1.

Skeptic: I don’t believe you. How can you get from s to u1 in k/2 steps?

Prover: By going through u2.

Skeptic: Oh yeah? How can you get from u2 to u1 in k/4 steps?

Prover: By going through u3. NP = one round between prover and verifier … PSPACE = poly(n) rounds

Skeptic: Really? How can you get from u6 to u8 in 1 step?

Prover: [sigh]… (u6,u8) is an edge. Just follow the edge.

Skeptic: Oh, you’ right… anyway, back to u6 to u13 in 2 steps…

8.6: PSPACE, Games, and Quantified SAT 25 PSPACE-completeness

• SPACE-PREDICT (“obviously” PSPACE-complete… why?) • Given: program P, input x, integer m in unary • Question: does P(x) accept using ≤ m bits of memory? • TWO-PLAYER-SAT We’ll show SPACE-PREDICT ≤ QSAT • Given: Boolean formula φ(x1,…,xn) • Question: Two players take turns setting the input variables xi {0,1}. Player 1 sets x1, then Player 2 sets x2, then Player 1 sets x3, …. Player 1 wins if φ is true, and Player 2 wins if φ is false. Does Player 1 have a winning∈ strategy? • QSAT (“Quantified SAT”: equivalent to TWO-PLAYER-SAT)

• Given: Boolean formula φ(x1,…,xn) QSAT PSPACE… why? • Question: Let ψ = [( x )( x )( x )( x ) ··· : φ(x ,...,x )]. Is ψ true? 1 2 3 4 ∈ 1 n 8.6: PSPACE, Games, and Quantified SAT 26 ∃ ∀ ∃ ∀ SPACE-PREDICT ≤ QSAT

• Input: (P program, input x, space bound m). Define Gx = (Vx,Ex), where Vx = { configurations of P(x) with space m } (note m = log |Vx|) Ex = { (u,v) | u goes to v in one step }

• P(x) accepts R(s,t,|Vx|) ( u1)(Ɐa1,b1)… ( um)(Ɐam,bm) [(am,bm) Ex] s = initial t = accepting There’s our quantified formula φ (almost) configuration of P(x)⇔ configuration of P⇔ ∃ ∃ ∈

• QSAT alternates and Ɐ on every bit, but not this φ … how to fix? • One more issue: Ɐ ranges over all bits, but we need to constrain (a ,b ) … ∃ t t

8.6: PSPACE, Games, and Quantified SAT 27 Ensuring Skeptic cannot cheat

th • t move is of the form (at, bt) {(at−1, ut), (ut, bt−1)}, but need to ensure that the bits Skeptic picks one at a time, which encode (at, bt), ∀ ∈ actually encode a pair in the set {(at−1, ut), (ut, bt−1)}

• Fix: final predicate to evaluate changes from (am,bm) Ex to

(am,bm) Ex ∈ [(a ≠ b b ≠ u ) (a ≠ u b ≠ b ) ] 2 1∈ 2 2 2 2 2 1 Prover automatically “wins” ∨ if Skeptic tries to “cheat” by [ (a3 ≠ b2 b3 ≠ u3) (a3 ≠ u3 b3 ≠ b2) ] ∨ ∧ ∨ picking … ∨ a b a u u b ∨ ∧ ∨ ( t, t) {( t−1, t), ( t, t−1)} [(am ≠ bm−1 bm ≠ um) (am ≠ um bm ≠ b∨m−1) ] ∉ 8.6: PSPACE, Games, and Quantified SAT 28 ∨ ∧ ∨ IP = PSPACE (proven in Chapter 11)

• If a QSAT formula ψ = [( x1)( x2)( x3)( x4) ··· : φ(x1,...,xn)] is false, then (by deMorgan) for any bit Prover chooses for x1, ∃ ∀ ∃ ∀ there is a bit Verifier can choose for x2, so that

for any bit Prover chooses for x3,

there is a bit Verifier can choose for x4, so that …

φ(x1,...,xn) is false, i.e., Verifier has a winning strategy. But Verifier needs a polynomial-space algorithm to compute which bits it must choose to win. • IP = interactive proof systems where Verifier is a polynomial-time, randomized algorithm, and can win with high probability. • In other words, the system remains equally powerful even if we downgrade the computational power of Verifier from polynomial-space to polynomial-time, but give it a random-number generator. (Prover remains PSPACE-powerful) • Without interaction, polynomial-time randomized computation is the class BPP (probably = P)

8.6: PSPACE, Games, and Quantified SAT 29 Geography • Road trip game: • Player 1 names a city (PariS) • Player 2 names a city whose first letter is S (SacramentO) • Player 1 names a city whose first letter is O (OmahA) • Player 2 names a city whose first letter is A (Albuquerque) … • Graph G=(V,E) V = {city names} E = { (u,v) | u[-1] = v[0] } • Players alternate moving along an edge to an unvisited node • Loser is first who cannot make a move

• GEOGRAPHY: GEOGRAPHY PSPACE… why? • Input: directed graph G and starting node v • Question: Does player 1 have a winning strategy starting at v? ∈

8.7: Games People Play 30 GEOGRAPHY is PSPACE-complete (QSAT ≤ GEOGRAPHY)

• Assume QSAT uses 3CNF formulas (via conversion in Cook-Levin Theorem) • How to evaluate a given 3CNF formula on a given input using Geography? φ = ( x y z ) ( x y z ) x=1, y=1, z=0 How to evaluate a quantified 3CNF formula using Geography?∨ ∨ ∧ ∨ ∨ key idea: visited nodes lock in ( x y z ) ( x y z ) choice for variable assignment Alternate red and blue players… ∨ ∨ ∨ ∨ Blue player is stuck! x marks already red can choose a node, so that x x y y z z visited notes no matter what node blue chooses, red can choose a node, so that … so that, no matter what clause blue chooses, start node red can satisfy it and trap blue 31 Proving no path exists

• A problem is in NP if there is a time-efficiently-checkable proof when the answer is yes (e.g., does a graph have a Hamiltonian path?) • The conjecture “NP ≠ coNP” expresses our intuition that there is not a time-efficiently-checkable proof for the lack of a witness. • Immerman–Szelepcsényi Theorem: NSPACE(s) = coNSPACE(s) for s(n) ≥ log n • Corollary: NL = coNL (also NPSPACE = coNPSPACE, but both obviously equal PSPACE by Savitch’s Theorem) • There’s an obvious space-efficiently-checkable proof when there is a path in a graph G from s to t… but there’s also a space-efficiently-checkable proof when there is no path from s to t!

8.5: You can't get there from here 34 Strategy: Show NONREACHABILITY NL

• Since REACHABILITY is NL-complete, NONREACHABILITY∈is coNL-complete. • So NONREACHABILITY NL NONREACHABILITY ≤L REACHABILITY REACHABILITY is coNL-complete (*) NONREACHABILITY is NL-complete • Thus NL = coNL, and∈ “collapse⇒ propagates upwards” (padding):⇒ • e.g., to show NSPACE(n) = coNSPACE⇒(n): technicality: cannot actually write 0k in space |x|, but in RAM AD k |x| • P (A) = { x0 | k = 2 and x A } model, can simply return 0 for • A NSPACE(n) PAD(A) NSPACE(log n) = NL = coNL any bit query at location i > |x| ∈ k • To determine if x A, let y = x0 and use coNL algorithm for “y PAD(A)?” • Takes∈ space O(log⇒ |y|) = O∈(|x|), so A coNSPACE(n). ∈ ∈ (*) since all A in NL obey A ≤L NONREACHABILITY∈ ≤L REACHABILITY

8.5: You can't get there from here 35 How to show NONREACHABILITY NL?

Intuition: “inductive counting”: the nondeterministic program, to decide whether t is not reachable from s: ∈ • [main trick] counts number r of nodes reachable from s in ≤ n steps, • guesses r paths of length ≤ n ending in different nodes, in order • accepts if none of the paths ends in t. (note paths can be checked storing only O(1) nodes at a time) How to ensure paths end in different nodes? • Can’t remember them all, but… • Can count number of different end-nodes we’ve seen, • Can remember previous end-node, and • Can ensure next end-node is larger lexicographically

8.5: You can't get there from here 36 Inductive Counting

// count # of nodes (k+1)-reachable from s def count(G,s,k+1):

Goal: “nondeterministically compute” rk = count(G,s,k) if k > 0 else 1 r = number of nodes reachable from s: rk+1 = 0 program makes guesses and can have for each node v: standard NL algorithm error, but if no error, definitely returns r v_reachable = False for k-reachability ck = 0 for each node u: if reachable(G,s,u,k):

ck += 1 if (u,v) E or u=v: v_reachable = True Why is this algorithm logspace? ∈ if ck ≠ rk: // check proper guessing above ERROR if v_reachable = True:

rk+1 += 1 return rk+1

8.5: You can't get there from here 38