Skip to main content

10.4 Computational Complexity: Class P, Class NP, Reductions & NP-Complete Problems

πŸ“šModule 01Topic 1.1⏱️5 min read
🎯High-Yield For:Semester Exams β€’ GATE CSE β€’ Technical Interviews

πŸ’‘ Core Intuition:​

In the previous topics, we answered the fundamental question of computability: Can this problem be solved by an algorithm at all? We discovered that problems like the Halting Problem and Post Correspondence Problem are mathematically impossible to solve.

However, in real-world computer science, a second, equally critical barrier exists: tractability. A problem might be decidable, but if the fastest known algorithm requires 210002^{1000} operations, the universe will experience heat death before the computation finishes. Computational complexity classifies solvable problems by the physical resourcesβ€”time and memory spaceβ€”demanded by their optimal algorithms.

🍳 The Everyday Analogy:​

Consider a 10001000-piece jigsaw puzzle or a 9Γ—99 \times 9 Sudoku grid:

  • Solving from Scratch (Class NP): If you are handed an empty Sudoku grid, finding the valid numbers requires exploring an exponential tree of possibilities with frequent dead ends and backtracking. Finding the solution from scratch feels hard.
  • Verifying a Completed Solution (Class P): If a friend hands you an already-completed Sudoku grid, verifying whether their solution is correct takes less than 60 seconds: you simply scan each of the 9 rows, 9 columns, and 9 boxes to check for duplicates. Verification is blindingly fast.

This asymmetry between finding a solution and verifying a claimed solution is the heart of the Class P versus Class NP divide.

πŸ’» Bridging to Computer Science​

In system architecture, algorithm engineering, and cryptography:

  • Class P (Deterministic Polynomial Time): Problems solvable in O(nk)O(n^k) steps (e.g., Shortest Path via Dijkstra, Sorting via MergeSort, Primality Testing via AKS). These problems scale gracefully with massive datasets and run efficiently on production servers.
  • Class NP (Nondeterministic Polynomial Time): Decision problems where any candidate solution ("certificate" or "witness") can be verified by a deterministic algorithm in O(nk)O(n^k) time.
  • The P=?NPP \stackrel{?}{=} NP Question: If an efficient algorithm exists to verify every valid solution, does an efficient algorithm necessarily exist to find that solution from scratch? This remains the most famous open question in theoretical computer science, carrying a $1,000,000 Millennium Prize.

⏱️ Asymptotic Complexity Classes: DTIME, NTIME, DSPACE & NSPACE​

We formalize computational resources using multi-tape Turing Machines as the standard reference model.

Time Complexity Classes​

Let t:N→Nt: \mathbb{N} \to \mathbb{N} be a function.

  1. DTIME(t(n))\text{DTIME}(t(n)): The class of all languages decidable by a Deterministic Turing Machine running in time O(t(n))O(t(n)) for all inputs of length nn.
  2. NTIME(t(n))\text{NTIME}(t(n)): The class of all languages decidable by a Non-Deterministic Turing Machine where every computation branch terminates within O(t(n))O(t(n)) steps.
P=⋃kβ‰₯1DTIME(nk)\text{P} = \bigcup_{k \ge 1} \text{DTIME}(n^k) NP=⋃kβ‰₯1NTIME(nk)\text{NP} = \bigcup_{k \ge 1} \text{NTIME}(n^k)

Space Complexity Classes​

Let s:N→Ns: \mathbb{N} \to \mathbb{N} be a function.

  1. DSPACE(s(n))\text{DSPACE}(s(n)): The class of all languages decidable by a Deterministic Turing Machine that scans at most O(s(n))O(s(n)) distinct tape cells.
  2. NSPACE(s(n))\text{NSPACE}(s(n)): The class of all languages decidable by a Non-Deterministic Turing Machine that scans at most O(s(n))O(s(n)) distinct tape cells.
PSPACE=⋃kβ‰₯1DSPACE(nk)\text{PSPACE} = \bigcup_{k \ge 1} \text{DSPACE}(n^k) NPSPACE=⋃kβ‰₯1NSPACE(nk)\text{NPSPACE} = \bigcup_{k \ge 1} \text{NSPACE}(n^k)
Architecture Flow

The Computational Complexity Containment Spectrum

The strict and suspected containment chain linking logarithmic space to exponential time

πŸ’ΎSub-Linear

Logarithmic Space (L)

Problems solvable with O(log n) read/write workspace (pointers).

β†’
L βŠ† P
⚑Tractable

Polynomial Time (P)

Efficiently solvable problems: Sorting, Shortest Paths, 2-SAT, Matching.

β†’
P βŠ† NP
πŸ”Verifiable

Non-Deterministic Time (NP)

Solutions verifiable in polynomial time: 3-SAT, Clique, Hamiltonian Cycle.

β†’
NP βŠ† PSPACE
πŸ›οΈIntractable

Polynomial Space (PSPACE)

Reversible games and logic: Quantified Boolean Formulas (QBF), Chess, Go.

β†’
PSPACE βŠ† EXPTIME
πŸ”₯Exponential

Exponential Time (EXPTIME)

Problems requiring O(2^(n^k)) time. P ⊊ EXPTIME is mathematically proven.

Savitch's Theorem (1970):
For any space-constructible function s(n)β‰₯log⁑ns(n) \ge \log n:

NSPACE(s(n))βŠ†DSPACE(s(n)2)\text{NSPACE}(s(n)) \subseteq \text{DSPACE}(s(n)^2)

A direct corollary is that non-deterministic polynomial space collapses into deterministic polynomial space:

PSPACE=NPSPACE\text{PSPACE} = \text{NPSPACE}

🧩 Defining Class P vs. Class NP​

To analyze NP with mathematical precision, we use the Polynomial-Time Verifier Definition:

Definition: Polynomial-Time Verifier​

A language LβŠ†Ξ£βˆ—L \subseteq \Sigma^* is in NP if and only if there exists a deterministic polynomial-time algorithm VV (called the Verifier) and a polynomial p(n)p(n) such that:

w∈Lβ€…β€ŠβŸΊβ€…β€ŠβˆƒcβˆˆΞ£βˆ—Β with ∣cβˆ£β‰€p(∣w∣)Β suchΒ thatΒ V(w,c)=ACCEPTw \in L \iff \exists c \in \Sigma^* \text{ with } |c| \le p(|w|) \text{ such that } V(w, c) = \text{ACCEPT}
  • The string cc is called the certificate or witness.
  • If w∈Lw \in L, there exists at least one concise certificate that proves it to VV in polynomial time.
  • If wβˆ‰Lw \notin L, no certificate ccβ€”no matter how clever or deceptiveβ€”can trick VV into accepting.

Class P vs. Class NP: The Solvability vs. Verifiability Boundary

Contrast the computational mechanisms of deterministic solution search vs certificate verification

Class P

Efficient Solvability

⚑
Dominant Architecture / DomainDeterministic Polynomial Time (DTIME(n^k))
  • β€’A deterministic algorithm finds the correct YES/NO answer directly in O(n^k) steps.
  • β€’Zero guessing or certificate required.
  • β€’Closed under complementation: P = co-P.
  • β€’Examples: Minimum Spanning Tree (Kruskal/Prim), GCD (Euclid), Linear Programming (Interior Point).
"Problems where an algorithmic path to the solution is directly computable."
Class NP

Efficient Verifiability

πŸ”
Dominant Architecture / DomainPolynomial-Time Witness Verification
  • β€’A deterministic verifier checks a proposed certificate in O(n^k) steps.
  • β€’Finding the certificate might require exponential brute force over all candidates.
  • β€’Suspected to NOT be closed under complementation: NP β‰  co-NP.
  • β€’Examples: 3-SAT, Traveling Salesperson (Decision), Graph Coloring, Subset Sum.
"Problems where verifying a proposed answer is easy, even if finding it is hard."

The P versus NP uestion​

Because any deterministic algorithm running in time O(nk)O(n^k) can verify its own solution in polynomial time (by simply ignoring the certificate and computing the answer directly), we have:

PβŠ†NP\text{P} \subseteq \text{NP}

The central open question in modern science is whether the reverse holds:

P=?NP\text{P} \stackrel{?}{=} \text{NP}

Most computer scientists strongly believe that P≠NP\text{P} \ne \text{NP}. If P=NP\text{P} = \text{NP}, every problem whose solution can be easily checked could also be solved just as easily. This would break modern asymmetric cryptography (RSA, ECC), automate mathematical theorem proving, and revolutionize drug discovery.


πŸ”„ Polynomial-Time Reductions (A ≀_p B)​

To rank the relative difficulty of computational problems, we use Karp Reductions (Polynomial-Time Many-One Reductions):

Definition (Polynomial-Time Reduction):
A language AβŠ†Ξ£βˆ—A \subseteq \Sigma^* is polynomial-time reducible to a language BβŠ†Ξ“βˆ—B \subseteq \Gamma^* (denoted A≀pBA \le_p B) if there exists a function f:Ξ£βˆ—β†’Ξ“βˆ—f: \Sigma^* \to \Gamma^* such that:

  1. ff is computable by a Deterministic Turing Machine in polynomial time O(nk)O(n^k).
  2. For every input string wβˆˆΞ£βˆ—w \in \Sigma^*:
w∈Aβ€…β€ŠβŸΊβ€…β€Šf(w)∈Bw \in A \iff f(w) \in B
Architecture Flow

The Mechanics of a Polynomial-Time Reduction (A ≀p B)

How an instance of Problem A is transformed into an instance of Problem B

πŸ“₯Input A

Instance w of Problem A

Arbitrary input string w whose membership in language A is unknown.

β†’
f(w) in O(n^k)
βš™οΈTransformation

Polynomial Reducer Engine

Deterministic transformation converts instance w into candidate instance f(w).

β†’
Maps to B
πŸ“¦Black Box

Solver for Problem B

Executes solver on f(w). Solver outputs YES if and only if w belongs to A.

The Two Critical Directional Rules of Reductions​

Whenever A≀pBA \le_p B:

  1. Tractability Transfers Upwards:
(A≀pB∧B∈P)β€…β€ŠβŸΉβ€…β€ŠA∈P(A \le_p B \land B \in \text{P}) \implies A \in \text{P}

If Problem BB is easy, then Problem AA must also be easy. 2. Hardness Transfers Downwards:

(A≀pB∧Aβˆ‰P)β€…β€ŠβŸΉβ€…β€ŠBβˆ‰P(A \le_p B \land A \notin \text{P}) \implies B \notin \text{P}

If Problem AA is hard, then Problem BB must be at least as hard.


πŸ† NP-Hard & NP-Complete Definitions​

Using polynomial-time reductions, we can define the hardest problems in the entire class NP:

The Structural Hierarchy of Complexity Classes (Assuming P β‰  NP)

The relationship between Class P, Class NP, NP-Complete, and NP-Hard

NP-Hard

At Least as Hard as Every Problem in NP

πŸ”₯

A language H is NP-Hard if every language L in NP reduces to H in polynomial time (βˆ€L ∈ NP, L ≀p H). NP-Hard problems do NOT need to be in NP, and many are undecidable (e.g., the Halting Problem is NP-Hard!).

Halting ProblemExact TSP OptimizationGeneral QBFPost Correspondence Problem
↓Intersection with NP Defines NP-Complete
NP-Complete

The Hardest Problems Inside Class NP

πŸ†

A language X is NP-Complete if: (1) X ∈ NP, and (2) X is NP-Hard. If ANY single NP-Complete problem can be solved in polynomial time, then P = NP.

3-SATVertex CoverCliqueHamiltonian CycleSubset SumGraph 3-Coloring
↓Contains Tractable Core
Class P

Tractable Deterministic Polynomial-Time Core

⚑

Problems solvable in deterministic O(n^k) time. Contained strictly inside NP assuming P β‰  NP.

2-SATShortest PathEulerian CircuitMinimum Spanning TreeMatrix Multiplication

πŸ“œ The Cook-Levin Theorem & Karp's 21 NP-Complete Problems​

How do you prove the very first problem is NP-Hard when you have no existing NP-Hard problems to reduce from?

The Cook-Levin Theorem (1971):
The Boolean Satisfiability Problem (SAT) is NP-Complete.

Proof Sketch:
Stephen Cook and Leonid Levin independently proved that for any arbitrary language L∈NPL \in \text{NP} and input string ww, the complete execution history of the polynomial-time Non-Deterministic Turing Machine MM accepting ww can be encoded as a single giant Boolean formula ΦM,w\Phi_{M, w} in Conjunctive Normal Form (CNF):

  • Variables encode tape cell contents, head positions, and state transitions across p(n)p(n) time steps.
  • The formula Ξ¦M,w\Phi_{M, w} has size polynomial in ∣w∣|w| (O(p(n)2)O(p(n)^2)).
  • Ξ¦M,w\Phi_{M, w} is satisfiable if and only if there exists a valid sequence of non-deterministic transitions leading to an accepting state.
  • Therefore, w∈Lβ€…β€ŠβŸΊβ€…β€ŠΞ¦M,w∈SATw \in L \iff \Phi_{M, w} \in \text{SAT}, proving L≀pSATL \le_p \text{SAT} for all L∈NPL \in \text{NP}.

Once SAT was established as the root NP-Complete problem, Richard Karp (1972) established a tree of polynomial-time reductions showing that 21 foundational combinatorial problems are all NP-Complete:

Canonical NP-Complete Problems vs. Their Tractable P Counterparts

Small variations in problem constraints that cause the complexity to jump from P to NP-Complete

πŸ”€

Boolean Satisfiability

2-SAT vs 3-SAT
  • 2-SAT (clauses with 2 literals): In P via Strongly Connected Components in an implication graph (O(V + E)).
  • 3-SAT (clauses with 3 literals): NP-Complete via Cook-Levin theorem reduction.
πŸ•ΈοΈ

Graph Traversal

Eulerian vs Hamiltonian
  • Eulerian Circuit (visit every edge once): In P (check if all vertices have even degree in O(V + E)).
  • Hamiltonian Cycle (visit every vertex once): NP-Complete.
πŸ“

Path Optimization

Shortest vs Longest Path
  • Shortest Path: In P via Dijkstra's algorithm (O(E + V log V)) or Bellman-Ford.
  • Longest Simple Path (between two vertices without cycles): NP-Complete.
🎨

Graph Coloring

2-Color vs 3-Color
  • 2-Colorability (Bipartite Graph): In P via simple 2-color BFS/DFS in O(V + E).
  • 3-Colorability: NP-Complete even for planar graphs with maximum degree 4.

🏭 Real-World Production Context: Engineering with NP-Completeness​

Software engineers encounter NP-Complete problems daily in large-scale infrastructure. When a problem is proven NP-Complete, you do not waste months trying to find an exact polynomial-time algorithm. Instead, production systems adopt four proven engineering strategies:

  1. SAT/SMT Solvers in Formal Verification:
    • Modern Boolean SAT solvers (Z3, CaDiCaL, CVC5) use Conflict-Driven Clause Learning (CDCL). Despite the theoretical exponential worst-case of 3-SAT, production solvers solve instances with millions of variables in seconds.
    • Amazon Web Services (AWS) uses SMT solvers (Z3) to verify that IAM security policies and VPC routing tables contain zero unauthorized data exposure paths.
  2. Compiler Register Allocation via Graph Coloring:
    • Mapping an arbitrary number of program variables to a finite set of kk CPU registers (e.g., 16 x86-64 registers) is isomorphic to the kk-Graph Coloring problem (NP-Complete).
    • Compilers like GCC and LLVM use Chaitin's heuristic algorithm: build an interference graph, simplify vertices of degree <k< k, and spill variables to RAM only when coloring fails.
  3. Approximation Algorithms for Logistics:
    • The Traveling Salesperson Problem (TSP) and Bin Packing govern route optimization for shipping fleets (UPS, Amazon).
    • Instead of searching for the exact optimum, routing engines use polynomial-time approximation schemes (e.g., Christofides' 1.5-approximation algorithm for Metric TSP) or meta-heuristics (Simulated Annealing, Genetic Algorithms).
  4. Cryptographic Asymmetry:
    • Public-key cryptography (RSA, Diffie-Hellman, Elliptic Curves) relies fundamentally on one-way computational asymmetry.
    • Multiplying two 2048-bit primes takes microseconds (in Class P). Factoring the resulting 4096-bit composite number requires superpolynomial time with classical algorithms.

πŸ“ Numerical Applications & Worked Examples​

Example 1: Verifier Formulation for the Subset-Sum Problem​

Problem Statement:
The SUBSET-SUM decision problem is defined as follows:
Given a finite set of integers S={s1,s2,…,sn}S = \{s_1, s_2, \dots, s_n\} and an integer target TT, does there exist a subset Sβ€²βŠ†SS' \subseteq S such that:

βˆ‘x∈Sβ€²x=T\sum_{x \in S'} x = T

Formulate a formal polynomial-time Verifier algorithm V(I,c)V(I, c) to prove that SUBSET-SUM∈NP\text{SUBSET-SUM} \in \text{NP}. Specify the structure of the certificate cc, its size bound, and the exact asymptotic runtime of the verifier.

Step-by-Step Solution:

  • Step 1: Certificate Structure and Size Bound:
    • Let the instance be I=(S,T)I = (S, T), where n=∣S∣n = |S| and each integer sis_i is encoded in binary using at most bb bits. The total input size is ∣I∣=O(nβ‹…b)|I| = O(n \cdot b).
    • A candidate certificate cc is a binary bitmask of length nn:
c=(x1,x2,…,xn)∈{0,1}nc = (x_1, x_2, \dots, x_n) \in \{0, 1\}^n

where xi=1x_i = 1 indicates that si∈Sβ€²s_i \in S', and xi=0x_i = 0 indicates siβˆ‰Sβ€²s_i \notin S'.

  • The size of the certificate is ∣c∣=nβ‰€βˆ£I∣|c| = n \le |I|, which is strictly bounded by a linear polynomial p(∣I∣)p(|I|).

  • Step 2: Deterministic Verifier Algorithm V(I,c)V(I, c):

    def verify_subset_sum(S, T, c):
    # Step 2a: Check certificate length
    if len(c) != len(S):
    return "REJECT"

    # Step 2b: Accumulate the sum of selected elements
    running_sum = 0
    for i in range(len(S)):
    if c[i] == 1:
    running_sum += S[i]

    # Step 2c: Compare against target
    if running_sum == T:
    return "ACCEPT"
    else:
    return "REJECT"
  • Step 3: Verification Correctness:

    • If (S,T)∈SUBSET-SUM(S, T) \in \text{SUBSET-SUM}, there exists a valid subset summing to TT. Setting xi=1x_i = 1 for elements in that subset produces a certificate where V(I,c)V(I, c) accepts.
    • If (S,T)βˆ‰SUBSET-SUM(S, T) \notin \text{SUBSET-SUM}, no subset sums to TT. Every possible bitmask c∈{0,1}nc \in \{0, 1\}^n results in βˆ‘xisiβ‰ T\sum x_i s_i \ne T, causing VV to output REJECT.
  • Step 4: Complexity Analysis:

    • The loop performs nn additions of numbers with at most bb bits.
    • Addition of two bb-bit numbers takes O(b)O(b) bit operations.
    • Total verifier runtime is O(nβ‹…b)=O(∣I∣)O(n \cdot b) = O(|I|) time, which is strictly deterministic polynomial time.
    • Therefore, SUBSET-SUM∈NP\text{SUBSET-SUM} \in \text{NP}.

Example 2: Polynomial-Time Reduction: Vertex Cover to Independent Set​

Problem Statement:
Recall the definitions of two classic graph decision problems:

  • VERTEX-COVER: Given an undirected graph G=(V,E)G = (V, E) and integer kk, does there exist a subset Vβ€²βŠ†VV' \subseteq V with ∣Vβ€²βˆ£β‰€k|V'| \le k such that every edge (u,v)∈E(u, v) \in E has at least one endpoint in Vβ€²V'?
  • INDEPENDENT-SET: Given an undirected graph G=(V,E)G = (V, E) and integer mm, does there exist a subset SβŠ†VS \subseteq V with ∣S∣β‰₯m|S| \ge m such that no two vertices in SS share an edge in EE?

Assuming that VERTEX-COVER is known to be NP-Complete, construct a polynomial-time reduction:

VERTEX-COVER≀pINDEPENDENT-SET\text{VERTEX-COVER} \le_p \text{INDEPENDENT-SET}

and prove its mathematical correctness.

Step-by-Step Solution:

  • Step 1: Construction of the Reduction Function ff:
    • Given an arbitrary instance of VERTEX-COVER: (G,k)(G, k) where G=(V,E)G = (V, E) with n=∣V∣n = |V| vertices.
    • Construct an instance of INDEPENDENT-SET: (Gβ€²,m)(G', m) where:
Gβ€²=G=(V,E)andm=nβˆ’kG' = G = (V, E) \quad \text{and} \quad m = n - k
  • The graph remains unchanged, and the target size is computed as m=∣Vβˆ£βˆ’km = |V| - k.

  • This transformation requires only computing nβˆ’kn - k, which executes in O(1)O(1) time (trivially polynomial O(n)O(n)).

  • Step 2: Proof of Correctness (β€…β€ŠβŸΉβ€…β€Š\implies Direction):

    • Suppose (G,k)∈VERTEX-COVER(G, k) \in \text{VERTEX-COVER}.
    • Then there exists a vertex cover Vβ€²βŠ†VV' \subseteq V with ∣Vβ€²βˆ£β‰€k|V'| \le k.
    • Consider the complementary set S=Vβˆ–Vβ€²S = V \setminus V'.
    • The size of SS is:
∣S∣=∣Vβˆ£βˆ’βˆ£Vβ€²βˆ£β‰₯nβˆ’k=m|S| = |V| - |V'| \ge n - k = m
  • We claim SS is an independent set. Suppose for contradiction that SS is not independent. Then there exists an edge (u,v)∈E(u, v) \in E with both u∈Su \in S and v∈Sv \in S.

  • Since u∈Su \in S and v∈Sv \in S, neither uu nor vv belongs to Vβ€²V'.

  • But this means edge (u,v)(u, v) has neither endpoint in Vβ€²V', which contradicts the fact that Vβ€²V' is a valid vertex cover!

  • Thus, no such edge can exist in SS, so SS is an independent set of size at least mm.

  • Therefore, (Gβ€²,m)∈INDEPENDENT-SET(G', m) \in \text{INDEPENDENT-SET}.

  • Step 3: Proof of Correctness (β€…β€ŠβŸΈβ€…β€Š\impliedby Direction):

    • Suppose (Gβ€²,m)∈INDEPENDENT-SET(G', m) \in \text{INDEPENDENT-SET}.
    • Then there exists an independent set SβŠ†VS \subseteq V with ∣S∣β‰₯m=nβˆ’k|S| \ge m = n - k.
    • Consider the complementary set Vβ€²=Vβˆ–SV' = V \setminus S.
    • The size of Vβ€²V' is:
∣Vβ€²βˆ£=∣Vβˆ£βˆ’βˆ£Sβˆ£β‰€nβˆ’(nβˆ’k)=k|V'| = |V| - |S| \le n - (n - k) = k
  • We claim Vβ€²V' is a vertex cover. Suppose for contradiction that Vβ€²V' is not a vertex cover. Then there exists an edge (u,v)∈E(u, v) \in E such that neither u∈Vβ€²u \in V' nor v∈Vβ€²v \in V'.

  • Since neither is in Vβ€²V', both uu and vv must belong to S=Vβˆ–Vβ€²S = V \setminus V'.

  • But this means edge (u,v)(u, v) connects two vertices in SS, which contradicts the fact that SS is an independent set!

  • Thus, Vβ€²V' must cover every edge in EE, and ∣Vβ€²βˆ£β‰€k|V'| \le k.

  • Therefore, (G,k)∈VERTEX-COVER(G, k) \in \text{VERTEX-COVER}.

  • Conclusion:
    Because (G,k)∈VERTEX-COVERβ€…β€ŠβŸΊβ€…β€Šf(G,k)∈INDEPENDENT-SET(G, k) \in \text{VERTEX-COVER} \iff f(G, k) \in \text{INDEPENDENT-SET} and ff runs in polynomial time, VERTEX-COVER≀pINDEPENDENT-SET\text{VERTEX-COVER} \le_p \text{INDEPENDENT-SET}. Since VERTEX-COVER is NP-Complete and INDEPENDENT-SET is in NP, INDEPENDENT-SET is also NP-Complete.


Example 3: Complexity Classification of Graph Problems​

Problem Statement:
Classify each of the following four decision problems into exactly one of three categories: Class P, NP-Complete, or Undecidable:

  1. Problem Q1Q_1: Given an undirected graph GG, does GG contain a simple cycle of length at least 3?
  2. Problem Q2Q_2: Given a Boolean formula in 3-CNF, does there exist a truth assignment that makes at least two literals true in every clause?
  3. Problem Q3Q_3: Given an arbitrary Turing Machine MM, does there exist an input ww on which MM visits at most ∣w∣2|w|^2 tape cells?
  4. Problem Q4Q_4: Given an undirected graph GG and an integer kk, does GG contain a clique of size at least kk?

Step-by-Step Solution:

  • Analysis of Q1Q_1 (Simple Cycle in Graph):

    • An undirected graph with VV vertices contains a cycle if and only if a Depth-First Search (DFS) encounters a back-edge to an ancestor (excluding the immediate parent).
    • DFS runs in O(V+E)O(V + E) time.
    • If Eβ‰₯VE \ge V, the graph is guaranteed to contain a cycle by the Pigeonhole Principle.
    • Because an algorithm exists that decides the problem in linear time O(V+E)O(V + E), Q1Q_1 belongs to Class P.
  • Analysis of Q2Q_2 (Not-All-Equal 3-SAT / 2-True SAT):

    • Requiring at least two literals true in every 3-literal clause is equivalent to requiring that not all literals are false and not all are true (the classic NAE-3-SAT problem).
    • NAE-3-SAT is one of Karp's foundational NP-Complete problems, established by reduction from standard 3-SAT.
    • A candidate truth assignment can be verified in linear time O(m)O(m) for mm clauses, placing it in NP.
    • Therefore, Q2Q_2 is NP-Complete.
  • Analysis of Q3Q_3 (TM tape usage bound):

    • This asks whether there exists an input on which a general Turing Machine MM respects a polynomial space bound.
    • This is a non-trivial property of the computational behavior of Turing machines.
    • By reduction from the Halting Problem (or Rice's Theorem on computation traces), determining whether an unconstrained TM restricts its tape head within a bound is undecidable.
    • Therefore, Q3Q_3 is Undecidable.
  • Analysis of Q4Q_4 (CLIQUE of size β‰₯k\ge k):

    • A candidate clique of kk vertices can be verified in O(k2)=O(V2)O(k^2) = O(V^2) time by checking whether all (k2)\binom{k}{2} vertex pairs are connected by edges in EE.
    • Thus, CLIQUE∈NP\text{CLIQUE} \in \text{NP}.
    • Karp (1972) proved that 3-SAT reduces to CLIQUE in polynomial time (3-SAT≀pCLIQUE3\text{-SAT} \le_p \text{CLIQUE}).
    • Therefore, Q4Q_4 is NP-Complete.

🎯 Exam & Interview Pitfall Check​

Core Conceptual Questions
  1. Can an undecidable problem be NP-Hard?

    • Answer: YES! A problem HH is NP-Hard if every problem in NP reduces to HH in polynomial time (L≀pHL \le_p H for all L∈NPL \in \text{NP}). There is no requirement that HH must be in NP or even decidable! For example, the Halting Problem (LHPL_{HP}) is provably NP-Hard because any NP problem can be decided by an exponential-time TM, which can be encoded into a halting question. However, LHPL_{HP} is not NP-Complete because LHPβˆ‰NPL_{HP} \notin \text{NP}.
  2. If someone proves that an NP-Complete problem can be solved in O(nlog⁑n)O(n^{\log n}) time, does that prove P=NPP = NP?

    • Answer: NO. O(nlog⁑n)O(n^{\log n}) is quasi-polynomial time, not polynomial time (O(nk)O(n^k) for a constant kk). Proving an NP-Complete problem runs in O(nlog⁑n)O(n^{\log n}) would shatter the Exponential Time Hypothesis (which asserts NP-Complete problems require 2Ξ©(n)2^{\Omega(n)}), but it would NOT prove P=NPP = NP.
Common Interview Traps
  • Trap: Reversing the Reduction Direction:
    • To prove a new problem BB is NP-Hard, you must reduce from an existing NP-Hard problem AA to BB (A≀pBA \le_p B). If you reduce from BB to AA (B≀pAB \le_p A), you have only proved that BB is no harder than AAβ€”you have proved nothing about BB's hardness!
  • Trap: Confusing NP with "Non-Polynomial":
    • "NP" does NOT stand for "Not Polynomial"! It stands for Non-deterministic Polynomial time. Every problem in P is also in NP (PβŠ†NP\text{P} \subseteq \text{NP}).
  • Trap: Assuming 2-SAT and 3-SAT have Similar Complexity:
    • 2-SAT is in Class P (solvable in O(V+E)O(V + E) linear time using implication graphs and strongly connected components). 3-SAT is NP-Complete. Adding just one literal per clause triggers an exponential phase transition in computational complexity.

πŸ’¬

Discussion & Doubts