time and space
play

Time and Space Crawling, on the planets face. Space Complexity - PowerPoint PPT Presentation

Time and Space Crawling, on the planets face. Space Complexity Some insects, called the human race. Lost in time, and lost in space Nabil Mustafa ... and in meaning. Computability and Complexity Rocky Picture Horror Show (1973) Nabil


  1. Time and Space Crawling, on the planet’s face. Space Complexity Some insects, called the human race. Lost in time, and lost in space Nabil Mustafa ... and in meaning. Computability and Complexity – Rocky Picture Horror Show (1973) Nabil Mustafa Space Complexity Nabil Mustafa Space Complexity Defining Space Complexity Defining the Class SPACE The Class SPACE So far, concerned with the resource of time SPACE (S(n)): set of functions computable in space at most P : Class of algorithms running in at most polynomial time c · S ( n ) for some constant c > 0. NP : Class of algorithms running in at most exponential time Space is the other resource For example, SPACE ( n 2 ) is the set of all functions computable in at most O ( n 2 ) space. Space-Bounded Complexity S 1 ( n ) = O ( S 2 ( n )) = ⇒ SPACE ( S 1 ( n )) ⊆ SPACE ( S 2 ( n )) Given 1 A function S : N → N The Class PSPACE 2 A TM M that decides a language L � SPACE ( n c ) PSPACE = c ≥ 1 we say M has space complexity S ( n ) if M decides L using O ( S ( | x | )) distinct cells on the work-tape on input x . The class PSPACE contains all classes SPACE ( S ( n )) where Note: Don’t count the space used in the read-only input tape. S ( n ) is a polynomial of finite degree. Nabil Mustafa Space Complexity Nabil Mustafa Space Complexity

  2. Relation to DTIME Relation to DTIME Claim Claim SPACE ( F ( n )) ⊆ DTIME (2 F ( n ) ) DTIME ( F ( n )) ⊆ SPACE ( F ( n )) In English: if a language L can be decided in O ( F ( n )) steps, So assume TM M can decide L in space SPACE ( F ( n )) then it can be decided using space O ( F ( n )) as well. Given x , it will always halt with an accept or reject. Trivial: A TM running in t steps can only touch t cells. Basic idea: Two positions on the tape cannot occur twice! So the TM deciding L in time t can also only touch t cells. Why?: Otherwise M would not halt! Note: SPACE ( F ( n )) ⊆ DTIME ( F ( n )) not true. Quicksort takes time O ( n log n ), but space O ( n ). cs211a06 students: Did we see this idea in Discrete Math? Nabil Mustafa Space Complexity Nabil Mustafa Space Complexity Defining Configurations Representing Configurations A configuration at time t is: The symbols in each cell of the tapes Claim: Each configuration can be described using cS ( n ) bits. Head positions of all the tape-heads The TM state Symbols in each cell of the tapes: O ( S ( n )) Define the directed graph G M , x given TM M and input x Position of the tape heads: O (log S ( n )) Each vertex is a configuration The TM state: O (log | Q | ) = O (1) ( v i , v j ) ∈ E iff v j gotten by applying a transition rule to v i For a deterministic TM , each vertex has out-degree 1 Claim: There is an O ( S ( n ))-size CNF formula φ such that φ M , x ( C , C ′ ) = 1 iff C and C ′ are neighbors in G M , x . Claim: If TM M decides L , then G M , x a DAG If not a DAG, a cycle = ⇒ infinite loop. Have a formula such that encodes a valid transition from C Denote by C x start the starting configuration given x . Then check if C ′ is a valid transition from C By modifying M so that it erases all tapes before halting, we Exactly like the proof of Cook-Levin theorem. have a single configuration C accept that, if reached, always means ‘accept’. Nabil Mustafa Space Complexity Nabil Mustafa Space Complexity

  3. Relation between PSPACE and DTIME Relation between NP and PSPACE Claim Claim NP ⊆ PSPACE SPACE ( S ( n )) ⊆ DTIME (2 S ( n ) ) Only need to show for one NP complete function L Then how to compute any other language L ′ ∈ NP : Each configuration can be described using O ( S ( n )) bits. Since L NP hard, can reduce L ′ to L Configurations cannot repeat. Important: we use only polynomial space in the reduction since In the worst case, the TM could go over all possible the reduction takes only polynomial time. configurations Then solving L in PSPACE solves L ′ in PSPACE Total time: O (2 S ( n ) ). Claim: 3-CNF SAT is in PSPACE Given a formula φ in n variables Start with the assignment v 1 = 0 , . . . , v n = 0 What We Know So Far Check if φ satisfied If not, erase previous assignment, and try a new one DTIME ( F ( n )) ⊆ SPACE ( F ( n )) ⊆ DTIME (2 F ( n ) ) Halt after at most 2 n steps Total space used: n . Nabil Mustafa Space Complexity Nabil Mustafa Space Complexity Defining QBF An Example So PSPACE includes all the functions in NP ( x 1 ∨ x 2 ) ∧ ( x 1 ∨ x 2 ) Now we describe a function in PSPACE , but not known to be in NP Who wins this game? The function is a game played on a SAT formula φ : DH sets x 1 = 1 , BM sets x 2 = 1 . BM wins. Lets order the variables of φ as x 1 , . . . , x n DH sets x 1 = 0 , BM sets x 2 = 0 . BM wins. It is played by DemonHunter (DH) and BladeMaster (BM) DH moves first, and sets x 1 to a fixed boolean value. ( x 1 ∨ x 2 ∨ x 3 ) ∧ ( x 1 ∨ x 2 ∨ x 3 ) ∧ ( x 1 ∨ x 2 ∨ x 3 ) ∧ ( x 1 ∨ x 2 ∨ x 3 ) BM moves second, and sets x 2 to a fixed boolean value. DH then sets x 3 , BM sets x 4 and so on. Who wins this game? DH (BM) wins the game if the final formula is satisfied (not DH sets x 1 = 1 , BM sets x 2 = 1 . DH sets x 3 =? . BM wins. satisfied). DH sets x 1 = 0 , BM sets x 2 = 1 . DH sets x 3 = 1 . DH wins. DH sets x 1 = 0 , BM sets x 2 = 0 . DH sets x 3 = 0 . DH wins. The function is: given a SAT formula, can DH win the game? Nabil Mustafa Space Complexity Nabil Mustafa Space Complexity

  4. QBF QSAT QBF A Quantified Boolean Formula ( QBF ) is of the form: ( x 1 ∨ x 2 ∨ x 3 ) ∧ ( x 1 ∨ x 2 ∨ x 3 ) ∧ ( x 1 ∨ x 2 ∨ x 3 ) ∧ ( x 1 ∨ x 2 ∨ x 3 ) Φ = ( Q 1 x 1 )( Q 2 x 2 )( Q 3 x 3 ) . . . ( Q n x n ) φ ( x 1 , . . . , x n ) Basically, DH won because where x i are variables, each Q i ∈ {∀ , ∃} , φ is CNF formula. There existed some value of x 1 that DH could set so that no matter how BM set x 2 , there existed some value of x 3 so that formula was satisfied. Φ is valid if φ is satisfied for all quantifications Q i . DH first picked the wrong value for x 1 , but doesn’t matter. For example, ∃ x 1 ∀ x 2 ∀ x 3 φ ( x 1 , x 2 , x 3 ) is valid iff there exists a value of x 1 such that for all values of x 2 and all values of x 3 , φ In general, DH wins if there exists a value of x 1 so that for every value BM sets for x 2 , there exists a value of x 3 so that is satisfied. for every value BM sets for x 4 and so on. QSAT ∃ x 1 ∀ x 2 ∃ x 3 ∀ x 4 . . . φ ( x 1 , . . . , x n ) is satisfied Given a QBF formula Φ, is it valid? Nabil Mustafa Space Complexity Nabil Mustafa Space Complexity QSAT is in PSPACE Computing a QBF Given Φ = ( Q 1 x 1 )( Q 2 x 2 )( Q 3 x 3 ) . . . ( Q n x n ) φ ( x 1 , . . . , x n ) Claim QSAT is in PSPACE Set i = 1. Compute Φ recursively as follows: If Q i = ∃ Given a QBF Φ, can check if its valid in polynomial space Set x i = 1, run ( Q i +1 x i +1 ) . . . ( Q n x n ) φ ( . . . , 1 , x i +1 , . . . , x n ) Is it any different from SAT ? Set x i = 0, run ( Q i +1 x i +1 ) . . . ( Q n x n ) φ ( . . . , 0 , x i +1 , . . . , x n ) Just cycle through all 2 n variable assignments If at least one of the answers is 1, return 1. Otherwise return 0. After checking each assignment, re-use the space for n vars If Q i = ∀ Problem: Have to ‘remember’ lots of bits Set x i = 1, run ( Q i +1 x i +1 ) . . . ( Q n x n ) φ ( . . . , 1 , x i +1 , . . . , x n ) Set x i = 0, run ( Q i +1 x i +1 ) . . . ( Q n x n ) φ ( . . . , 0 , x i +1 , . . . , x n ) Assume QBF is ∀ x 1 ∃ x 2 ∀ x 3 ∀ x 4 . . . If both the answers is 1, return 1. Otherwise return 0. Then have to remember the result for 2 values of x 1 , 2 values for x 3 , 2 values for x 4 , etc. Re-use space for each recursive call. In the worst case, have Ω(2 n ) bits to remember Exponential space! Space: S ( n ) = 2 bits for the two answers + S ( n − 1) Solution: Compute the formula recursively so reuse space S ( n ) = O ( n + m ). Nabil Mustafa Space Complexity Nabil Mustafa Space Complexity

  5. A Way to Think About QBF QBF is PSPACE complete QBF is in PSPACE ∃ x 1 ∀ x 2 ∃ x 3 φ ( x 1 , x 2 , x 3 ) It can also be shown that QBF is PSPACE complete The Cook-Levin reduction does not work ∃ The number of variables should be polynomial (otherwise 1 0 space used is super-polynomial) In Cook-Levin, number of variables is a function of the running x 1 = 0 x 1 = 1 time ∀ ∀ For problems in P running time is polynomial For problems in PSPACE running time could be exponential x 1 = 1 , x 2 = 1 x 1 = 1 , x 2 = 0 x 1 = 0 , x 2 = 0 x 1 = 0 , x 2 = 1 Intuitively: have to ‘reuse variables’ (exactly like we re-used ∃ ∃ ∃ ∃ space to solve QBF ) Read from Arora-Barak. We skip the reduction, since its very similar to what we do x 1 = 1 x 1 = 1 x 1 = 1 x 1 = 1 x 1 = 0 x 1 = 0 x 1 = 0 x 1 = 0 x 2 = 1 x 2 = 0 x 2 = 0 x 2 = 1 x 2 = 1 x 2 = 0 x 2 = 0 x 2 = 1 next, Savitch’s Theorem. x 3 = 1 x 3 = 0 x 3 = 1 x 3 = 0 x 3 = 1 x 3 = 0 x 3 = 1 x 3 = 0 Nabil Mustafa Space Complexity Nabil Mustafa Space Complexity

Recommend


More recommend