Time & Space Complexity
1503 words
8 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Time & Space Complexity ## 🎯 Learning Objectives - Define time and space complexity classes (P, NP, PSPACE, EXP) - Explain the time hierarchy theorem - Analyze relationships between complexity classes - Distinguish between deterministic and nondeterministic complexity - Understand the P vs. NP problem * * * ## 1.

Time & Space Complexity
🎯 Learning Objectives
- Define time and space complexity classes (P, NP, PSPACE, EXP)
- Explain the time hierarchy theorem
- Analyze relationships between complexity classes
- Distinguish between deterministic and nondeterministic complexity
- Understand the P vs. NP problem
1. Introduction to Complexity
1.1 Intuition
Decidability asks: "Can a computer solve this problem?" Complexity asks: "How efficiently can it solve this problem?" Some problems are solvable but require so much time (millions of years) that they're effectively impossible. Complexity theory classifies problems by the resources they need.
1.2 Asymptotic Notation
| Notation | Meaning | Example |
|---|---|---|
| O(g(n)) | At most c⋅g(n) for large n | 3n2+n∈O(n2) |
| Ω(g(n)) | At least c⋅g(n) for large n | n3∈Ω(n2) |
| Θ(g(n)) | Exactly c⋅g(n) for large n | n2+n∈Θ(n2) |
| o(g(n)) | Strictly less than g(n) | n2 is o(n3) but NOT o(n2) |
2. Time Complexity Classes
2.1 Definition
TIME(t(n))={L∣L is decided by an O(t(n)) time TM}
| Class | Definition | Example Problems |
|---|---|---|
| P | ⋃kTIME(nk) | Path in graph, sorting, CFG parsing |
| NP | ⋃kNTIME(nk) | SAT, TSP, coloring, subset sum |
| EXP | ⋃kTIME(2nk) | Generalized chess, Presburger arithmetic |
2.2 The Time Hierarchy Theorem
Theorem: For time-constructible functions f(n), TIME(f(n))⊊TIME(f(n)2).
This means: MORE TIME allows solving STRICTLY MORE problems. There's always a problem that takes just a bit more time.
Consequence: P⊊EXP (strict containment — there are problems in EXP not in P).
2.3 P vs. NP
(Diagram)
Key question: Does P=NP? If yes, all efficiently verifiable problems are efficiently solvable. If no, some problems (like SAT, TSP) have no efficient algorithm.
3. Space Complexity Classes
3.1 Definition
SPACE(s(n))={L∣L is decided by an O(s(n)) space TM}
| Class | Definition | Example |
|---|---|---|
| L | SPACE(logn) | Determining if a graph is connected |
| NL | NSPACE(logn) | Path finding in directed graph |
| PSPACE | ⋃kSPACE(nk) | Quantified Boolean formulas (QBF) |
| EXPSPACE | ⋃kSPACE(2nk) | Presburger arithmetic |
3.2 Space vs. Time
(Diagram)
Key relationships:
- P⊆NP⊆PSPACE=NPSPACE (Savitch's theorem)
- P⊊EXP (time hierarchy theorem)
- PSPACE⊆EXP (space ≤ time)
- P=EXPSPACE (if P=PSPACE, then P=EXP too, which contradicts the hierarchy theorem)
3.3 Savitch's Theorem
Theorem: NSPACE(f(n))⊆SPACE(f(n)2)
Intuition: Nondeterministic space can be simulated deterministically with only a quadratic space overhead. This is much better than time (where exponential slowdown occurs). So PSPACE=NPSPACE, but we don't know if P=NP.
4. The Landscape
| Class | Characterization | Complete Problems |
|---|---|---|
| P | Efficiently solvable | Linear programming, matching |
| NP | Efficiently verifiable | SAT, 3SAT, clique, vertex cover |
| PSPACE | Polynomial space | QBF, geography |
| EXP | Exponential time | Generalized chess |
| L | Log space | Graph connectivity |
| NL | Nondeterministic log space | Directed graph path |
5. Common Pitfalls
Pitfall 1: Confusing NP with "Not Polynomial"
The mistake: Thinking NP stands for "not polynomial."
Why students make it: The name is misleading.
How to catch it: NP = Nondeterministic Polynomial time. Problems in NP can be SOLVED by an NTM in polynomial time, or VERIFIED by a DTM in polynomial time.
Correct approach: NP = polynomial-time verification, not "exponential."
Pitfall 2: Assuming P ≠ NP is the Default
The mistake: Treating P ≠ NP as proven fact.
Why students make it: Everyone talks about "hard" NP problems as if they're impossible.
How to catch it: P ≠ NP is a conjecture, not a theorem. It's overwhelmingly believed but unproven. Both possibilities (P = NP and P ≠ NP) are consistent with current knowledge.
Correct approach: "If P ≠ NP" when discussing NP-completeness. Be careful what follows from the assumption vs. what's proven.
Pitfall 3: Equating "Hard" with "Intractable"
The mistake: Concluding that NP-complete problems are always intractable.
Why students make it: NP-completeness proofs suggest worst-case hardness.
How to catch it: NP-completeness is about WORST-CASE instances. Many NP-complete problems have practical algorithms that work well on real-world instances (SAT solvers, integer programming).
Correct approach: NP-completeness says "hard in the worst case." Real instances may be easy. Use specialized algorithms, heuristics, and approximation.
6. Key Concepts Reference
| Concept | Definition | Key Insight |
|---|---|---|
| P | O(nk) time on DTM | Efficient computation |
| NP | O(nk) time on NTM | Verifiable in poly time |
| PSPACE | O(nk) space | Includes NP, QBF-complete |
| EXP | O(2nk) time | Strictly larger than P |
| Time hierarchy | More time → more problems | P=EXP |
| Savitch's theorem | NSPACE(f)⊆SPACE(f2) | PSPACE=NPSPACE |
| Polynomial reduction | A≤pB | Used for NP-completeness |
7. 📝 Practice Questions
Q1: Why do we use polynomial time as the definition of "efficient" rather than linear or quadratic?Answer: Polynomial time (nk) is robust: (1) It's closed under composition (polynomial of polynomial is polynomial). (2) It's independent of machine model (polynomial time on any reasonable model is polynomial on a TM). (3) Most practical algorithms run in polynomial time. (4) Problems with super-polynomial lower bounds are truly intractable. Linear time is too restrictive (many algorithms are O(n log n) or O(n²)), and exponential time includes truly infeasible problems. Q2: Show that any problem in NP can be solved in exponential time.Answer: If L∈NP, there's an NTM N that decides L in O(nk) time. We can simulate N using a DTM via breadth-first search of the computation tree. The branching factor of N is at most b (finite), and the tree depth is O(nk). The total number of configurations is at most bO(nk) which is O(2nk) for some k. So L can be decided in O(2nk) time → NP⊆EXP. This is proven, not conjectured. Q3: State and explain Savitch's theorem.Answer: Savitch's theorem: NSPACE(f(n))⊆SPACE(f(n)2) for f(n)≥logn. Intuition: Nondeterministic space can be simulated deterministically with only quadratic space overhead. The proof uses divide-and-conquer: to check if a configuration can reach another in t steps, check if there's a midpoint that can be reached in t/2 steps from the start and can reach the end in t/2 steps. This recursive approach reuses space (depth O(f(n)), each level O(f(n)) space → total O(f(n)2)).Key implication: PSPACE=NPSPACE (polynomial space is unaffected by nondeterminism), unlike time where P=NP is believed. Q4: What is the difference between a reduction A≤pB and A≤mB?Answer: A≤mB (mapping reduction) requires only that the reduction function f is computable (in finite time). A≤pB (polynomial-time reduction) requires that f is computable in polynomial time. ≤p is more restrictive — it preserves polynomial-time computability. ≤m is used in undecidability proofs; ≤p is used in NP-completeness proofs. All ≤p reductions are also ≤m reductions, but not vice versa. Q5: Why don't we know if P = PSPACE?Answer: We know P⊆NP⊆PSPACE, but we can't prove proper inclusion for either containment. Proving P⊊PSPACE would imply P=NP (since NP⊆PSPACE). Proving P=PSPACE would imply NP=PSPACE (since NP⊆PSPACE). Either result would be revolutionary, and both seem far beyond current techniques. The time hierarchy theorem gives P=EXP but nothing separates P from PSPACE or NP from PSPACE with current methods. Q6: Place these problems in the most likely complexity class: (a) finding a path in a graph, (b) 3SAT, (c) QBF, (d) generalized chess.Answer: (a) Path in graph → P (BFS/DFS runs in O(n+m) time) (b) 3SAT → NP (verifiable in polynomial time, believed not in P) (c) QBF → PSPACE-complete (quantified boolean formulas are the canonical PSPACE-complete problem) (d) Generalized chess → EXP (on an n×n board, generalized chess is EXP-complete) Q7: Explain the Time Hierarchy Theorem's significance.Answer: The Time Hierarchy Theorem proves that TIME(f(n))⊊TIME(f(n)2) for time-constructible f. This means: (1) More time allows solving strictly more problems — there's always a decidable problem that needs more than O(f(n)) time. (2) P⊊EXP (since P=⋃kTIME(nk) and EXP=⋃kTIME(2nk), and the hierarchy theorem shows TIME(nk)⊊TIME(2n) for any k). (3) The proof uses diagonalization — constructing a TM on the fly that disagrees with every O(f(n)) time TM. Q8: If someone proves P = PSPACE, what other problems would be efficiently solvable?Answer: If P = PSPACE, then every problem solvable in polynomial space is also solvable in polynomial time. This would include: (1) All problems in NP (SAT, TSP, graph coloring) — P = NP follows from P = PSPACE since NP ⊆ PSPACE. (2) All PSPACE-complete problems (QBF, generalized geography, many games). (3) Problems in PH (polynomial hierarchy) — the entire hierarchy collapses to P. This would be revolutionary — the biggest discovery in computer science — but is widely believed to be false.
8. 🔗 Cross-References
- Week 11 - NP-Hardness: Polynomial reductions
- Week 12 - NP-Completeness: Complete problems
- BSCS4021 (Advanced Algorithms): Algorithm analysis Join Discord PreviousReductionsNextNP-Hardness