Unit V - Computational Complexity Theory
P, NP, NP-Complete, NP-Hard Problems and Famous Computational Challenges
1. Introduction to Computational Complexity
Computational Complexity Theory studies how efficiently problems can be solved. It classifies problems based on the resources (time, space) needed to solve them.
Why Study Complexity?
- Decidability vs Efficiency:
- Computability theory: Can we solve it at all?
- Complexity theory: Can we solve it efficiently?
- Practical Importance:
- Some decidable problems take millions of years!
- Need to distinguish "feasible" from "infeasible"
- Algorithm Design:
- Know when to look for better algorithms
- Know when to give up on exact solutions
Time Complexity:
Measured as a function of input size n:
- O(1): Constant time
- O(log n): Logarithmic (binary search)
- O(n): Linear (scanning array)
- O(n log n): Linearithmic (merge sort)
- O(n²): Quadratic (bubble sort)
- O(n³): Cubic (matrix multiplication)
- O(2ⁿ): Exponential (subset enumeration)
- O(n!): Factorial (traveling salesman brute force)
Polynomial vs Exponential:
| n | n² | n³ | 2ⁿ | n! |
|---|---|---|---|---|
| 10 | 100 | 1,000 | 1,024 | 3,628,800 |
| 20 | 400 | 8,000 | 1,048,576 | 2.4 × 10¹⁸ |
| 30 | 900 | 27,000 | 1,073,741,824 | 2.6 × 10³² |
| 40 | 1,600 | 64,000 | 1.1 × 10¹² | 8.2 × 10⁴⁷ |
| 50 | 2,500 | 125,000 | 1.1 × 10¹⁵ | 3.0 × 10⁶⁴ |
Feasibility Threshold:
Polynomial time (P): Considered "efficient" and "feasible"
Exponential time: Considered "inefficient" and "infeasible" for large inputs
This is a simplification, but useful practical distinction!
Deterministic vs Nondeterministic:
- Deterministic TM: One transition per configuration (usual TM)
- Nondeterministic TM: Multiple transitions possible, can "guess" correctly
2. The Class P (Polynomial Time)
Definition:
P is the class of decision problems solvable by a deterministic Turing Machine in polynomial time.
Formally: P = ⋃ₖ TIME(nᵏ)
Where TIME(nᵏ) = problems solvable in O(nᵏ) time
Characteristics of P:
- Efficient: Problems in P are considered "tractable"
- Closed under: Union, concatenation, star, complement
- Robust: Same class for different computational models (multi-tape TM, RAM)
- Practical: Can be solved on real computers
Examples of Problems in P:
| Problem | Description | Time Complexity |
|---|---|---|
| Sorting | Sort n numbers | O(n log n) |
| Searching | Find element in sorted array | O(log n) |
| Graph Connectivity | Is graph connected? | O(n + m) |
| Shortest Path | Find shortest path in graph | O(n²) or O(m log n) |
| Minimum Spanning Tree | Find MST in weighted graph | O(m log n) |
| Linear Programming | Optimize linear function | Polynomial (Khachiyan, 1979) |
| Matrix Multiplication | Multiply two n×n matrices | O(n²·⁸¹) (Strassen's) |
| GCD | Greatest common divisor | O(log n) |
| Primality Testing | Is n prime? | O(log⁶ n) (AKS, 2002) |
Properties of P:
- Closed under complement: If L ∈ P, then L̄ ∈ P
- Closed under union: If L₁, L₂ ∈ P, then L₁ ∪ L₂ ∈ P
- Closed under intersection: L₁ ∩ L₂ ∈ P
- Closed under concatenation: L₁ · L₂ ∈ P
- Closed under Kleene star: L* ∈ P
Why Polynomial?
Polynomial time is chosen as "efficient" because:
- Robust across computational models
- Closed under composition (polynomial × polynomial = polynomial)
- Practical: O(n¹⁰⁰) is large but still polynomial (rare in practice)
- Clear distinction from exponential
3. The Class NP (Nondeterministic Polynomial Time)
Definition (Two Equivalent Definitions):
Definition 1 (Nondeterministic):
NP is the class of decision problems solvable by a nondeterministic Turing Machine in polynomial time.
Definition 2 (Verifier - More Intuitive):
NP is the class of problems for which a "yes" answer can be verified in polynomial time.
- If answer is YES, there exists a certificate (proof) that can be checked quickly
- Certificate has polynomial length
- Verification algorithm runs in polynomial time
Intuitive Understanding:
NP = "Easy to verify, but may be hard to solve"
- Solving: Finding the answer (may be hard)
- Verifying: Checking if given answer is correct (must be easy)
Example 1: Hamiltonian Cycle
Problem: Does graph G have a cycle visiting each vertex exactly once?
- Solving: Try all possible cycles - exponential time!
- Verifying: Given a cycle, check if it's Hamiltonian:
- Check it visits each vertex once - O(n)
- Check edges exist - O(n)
- Total: O(n) - Polynomial!
- Certificate: The Hamiltonian cycle itself
Therefore: Hamiltonian Cycle ∈ NP
Example 2: SAT (Boolean Satisfiability)
Problem: Is Boolean formula F satisfiable?
Example: (x₁ ∨ x₂) ∧ (¬x₁ ∨ x₃) ∧ (¬x₂ ∨ ¬x₃)
- Solving: Try all 2ⁿ assignments - exponential!
- Verifying: Given assignment (x₁=T, x₂=F, x₃=T):
- Substitute values
- Evaluate formula - O(n)
- Check if result is TRUE
- Certificate: Satisfying assignment
Relationship: P ⊆ NP
If we can solve in polynomial time, we can certainly verify in polynomial time!
- Just solve the problem and compare with certificate
- Therefore: P ⊆ NP
The Million Dollar Question: P = NP?
Is P = NP or P ≠ NP?
- One of the most important open problems in computer science
- $1,000,000 Clay Millennium Prize for solution
- Most believe P ≠ NP (problems easy to verify but hard to solve)
- No proof either way after 50+ years!
More Examples in NP:
| Problem | Certificate | Verification |
|---|---|---|
| Vertex Cover | Set of k vertices | Check they cover all edges - O(m) |
| Clique | Set of k vertices | Check all pairs connected - O(k²) |
| Subset Sum | Subset that sums to target | Add elements, compare - O(n) |
| 3-Coloring | Color assignment | Check adjacent nodes different - O(m) |
| Integer Programming | Solution values | Check constraints - polynomial |
Properties of NP:
- ✓ Closed under union
- ✓ Closed under intersection
- ✓ Closed under concatenation
- ✓ Closed under Kleene star
- ? Closed under complement (unknown! Related to NP = co-NP?)
4. NP-Complete Problems
NP-Complete problems are the "hardest" problems in NP. They are the bottleneck of computational complexity theory.
Definition:
A problem L is NP-Complete if:
- L ∈ NP: L is in NP (solutions verifiable in polynomial time)
- L is NP-Hard: Every problem in NP reduces to L in polynomial time
In other words: NP-Complete = NP ∩ NP-Hard
Polynomial-Time Reduction:
Problem A reduces to problem B (written A ≤ₚ B) if:
- There's a polynomial-time computable function f such that:
- x ∈ A if and only if f(x) ∈ B
- Meaning: If we can solve B, we can solve A
Significance of NP-Completeness:
- If ANY NP-Complete problem is in P, then P = NP
- If ANY NP-Complete problem is NOT in P, then P ≠ NP
- All NP-Complete problems are "equally hard"
- Solving one efficiently means solving ALL efficiently
Cook-Levin Theorem (1971):
SAT (Boolean Satisfiability) is NP-Complete
First problem proven NP-Complete - opened the door to proving thousands more!
How to Prove NP-Completeness:
To prove problem L is NP-Complete:
- Show L ∈ NP: Describe polynomial-time verifier
- Show L is NP-Hard: Reduce known NP-Complete problem K to L
- Choose known NP-Complete problem K
- Show K ≤ₚ L (polynomial-time reduction)
- Since K is NP-Hard and K ≤ₚ L, then L is NP-Hard
Classic NP-Complete Problems:
| Problem | Description | Application |
|---|---|---|
| SAT | Boolean satisfiability | Logic, verification |
| 3-SAT | SAT with 3 literals per clause | Constraint satisfaction |
| Hamiltonian Cycle | Cycle visiting each vertex once | Routing, scheduling |
| Traveling Salesman (Decision) | Tour of length ≤ k? | Logistics, optimization |
| Vertex Cover | k vertices covering all edges? | Network design |
| Clique | k fully connected vertices? | Social networks |
| Independent Set | k non-adjacent vertices? | Scheduling |
| Graph Coloring | Color graph with k colors? | Register allocation |
| Subset Sum | Subset summing to target? | Resource allocation |
| Knapsack | Max value within weight limit? | Resource optimization |
Common Reduction Chain:
SAT → 3-SAT → Clique → Vertex Cover → Hamiltonian Cycle → TSP
Practical Implications:
If your problem is NP-Complete:
- Don't waste time looking for polynomial algorithm (probably doesn't exist)
- Do use approximation algorithms
- Do use heuristics for specific instances
- Do consider special cases (may be in P)
5. NP-Hard Problems
Definition:
A problem L is NP-Hard if:
Every problem in NP reduces to L in polynomial time.
Note: L doesn't have to be in NP itself!
NP-Complete vs NP-Hard:
| Property | NP-Complete | NP-Hard |
|---|---|---|
| In NP? | Yes (must be) | Not necessarily |
| Decision problem? | Yes (NP only has decision problems) | Can be optimization, search, etc. |
| As hard as NP? | Yes | At least as hard |
| Can be harder than NP? | No | Yes (can be undecidable!) |
Examples of NP-Hard (but not NP-Complete):
- Halting Problem:
- NP-Hard: At least as hard as any NP problem
- NOT in NP: Undecidable!
- Not NP-Complete (not in NP)
- Optimization TSP:
- Find SHORTEST tour (not just "is there tour ≤ k?")
- NP-Hard: At least as hard as decision TSP
- NOT in NP: NP only contains decision problems
- Finding Largest Clique:
- Find SIZE of largest clique (not "is there clique of size k?")
- NP-Hard but not NP-Complete
Complexity Hierarchy:
All Problems
|
NP-Hard (at least as hard as NP)
|
+-------+-------+
| |
NP-Complete Harder than NP
(NP ∩ NP-Hard) (e.g., Halting Problem)
|
|
NP (verifiable in polynomial time)
|
|
P (solvable in polynomial time)
Key Relationships:
- P ⊆ NP (every solvable in P-time is verifiable in P-time)
- NP-Complete ⊆ NP-Hard (NP-Complete is special case)
- NP-Complete ⊆ NP (by definition)
- If P = NP, then P = NP = NP-Complete
- If P ≠ NP, then P ⊂ NP and NP-Complete problems not in P
Approaches for NP-Hard Problems:
| Approach | Description | Trade-off |
|---|---|---|
| Approximation Algorithms | Find solution within factor of optimal | Guaranteed quality, polynomial time |
| Heuristics | Rules of thumb for good solutions | Fast, no guarantees |
| Randomized Algorithms | Use randomness to find solutions | Probabilistic guarantees |
| Special Cases | Restrict to tractable subproblems | Limited applicability |
| Exponential Algorithm | Exact solution for small instances | Optimal but slow |
| Parameterized Complexity | Efficient when parameter is small | Fixed-parameter tractable |
6. Hamiltonian Path Problem
The Hamiltonian Path Problem is a classic NP-Complete problem named after Irish mathematician William Rowan Hamilton.
Problem Definitions:
Hamiltonian Path: A path in a graph that visits each vertex exactly once.
Hamiltonian Cycle: A cycle that visits each vertex exactly once and returns to starting vertex.
Decision Problem:
- Input: Undirected graph G = (V, E)
- Question: Does G contain a Hamiltonian path/cycle?
Example 1: Graph with Hamiltonian Cycle
Graph:
A --- B
| |
| |
D --- C
Hamiltonian Cycle: A → B → C → D → A
✓ Visits each vertex exactly once
Example 2: Graph WITHOUT Hamiltonian Path
Graph:
A --- B --- C
| |
D --- E --- F
Try all paths:
- Can't visit all vertices without repeating
- Vertices D and F have degree 1, need to be endpoints
- But can't connect them without revisiting
✗ No Hamiltonian path exists
Complexity:
- Hamiltonian Path: NP-Complete
- Hamiltonian Cycle: NP-Complete
- Brute Force: O(n!) - try all permutations
- Best Known: O(2ⁿ · n²) - dynamic programming
- No polynomial algorithm known!
Why NP-Complete?
- In NP:
- Certificate: The Hamiltonian path itself (sequence of vertices)
- Verification: Check each vertex appears once - O(n), check edges exist - O(n)
- Total: O(n) - polynomial!
- NP-Hard:
- Can reduce from 3-SAT or Vertex Cover
- Standard reduction techniques exist
Special Cases (Polynomial Time):
| Graph Type | Hamiltonian Path? | Algorithm |
|---|---|---|
| Complete Graph Kₙ | Always YES | Any path works! |
| Tree | YES iff it's a path | Check structure - O(n) |
| Cycle Cₙ | Always YES | The cycle itself |
| Grid Graph | Depends on dimensions | Special algorithms |
| Tournament Graph | Always YES | Always has Hamiltonian path |
Sufficient Conditions (Guarantee Hamiltonian Cycle):
- Dirac's Theorem (1952): If G has n ≥ 3 vertices and every vertex has degree ≥ n/2, then G has Hamiltonian cycle
- Ore's Theorem (1960): If for every pair of non-adjacent vertices u, v: deg(u) + deg(v) ≥ n, then G has Hamiltonian cycle
Applications:
- Route Planning: Visit all locations exactly once
- DNA Sequencing: Reconstruct sequence from fragments
- Circuit Board Design: Drill holes in optimal order
- Puzzle Solving: Knight's tour on chessboard
Relation to TSP:
Hamiltonian Cycle is special case of TSP where:
- All edges have weight 1
- Non-edges have weight ∞
- Ask: Is there tour of cost n?
7. Traveling Salesman Problem (TSP)
The Traveling Salesman Problem is one of the most famous and well-studied optimization problems in computer science.
Problem Statement:
Input: Complete weighted graph G = (V, E) with edge weights (distances) w: E → ℝ⁺
Question:
- Optimization Version: Find shortest tour visiting each city exactly once and returning to start
- Decision Version: Is there a tour of length ≤ k?
Example: 4-City TSP
Cities and Distances:
A -10- B
|\ /|
5| \ / |15
| \/ |
| / \ |
20|/ \|12
D -8-- C
Possible Tours:
1. A → B → C → D → A: 10 + 12 + 8 + 20 = 50
2. A → B → D → C → A: 10 + 15 + 8 + 18 = 51
3. A → C → B → D → A: 18 + 12 + 15 + 20 = 65
4. A → C → D → B → A: 18 + 8 + 15 + 10 = 51
5. A → D → B → C → A: 20 + 15 + 12 + 18 = 65
6. A → D → C → B → A: 20 + 8 + 12 + 10 = 50
Optimal Tours: #1 and #6 with cost 50
Complexity:
| Version | Complexity Class | Notes |
|---|---|---|
| Decision TSP | NP-Complete | "Is there tour ≤ k?" |
| Optimization TSP | NP-Hard | "Find shortest tour" |
| Brute Force | O((n-1)!/2) | Try all tours |
| Dynamic Programming | O(n²2ⁿ) | Held-Karp algorithm |
| Branch & Bound | Varies | Practical for ~100 cities |
Algorithms and Approaches:
1. Brute Force Algorithm:
function BruteForceTSP(G):
best_tour = null
best_cost = ∞
for each permutation P of vertices:
cost = calculate_tour_cost(P)
if cost < best_cost:
best_cost = cost
best_tour = P
return best_tour, best_cost
Time: O(n!) - Infeasible for n > 20
2. Nearest Neighbor Heuristic:
function NearestNeighbor(G, start):
current = start
unvisited = V - {start}
tour = [start]
cost = 0
while unvisited not empty:
nearest = vertex in unvisited closest to current
tour.append(nearest)
cost += distance(current, nearest)
current = nearest
unvisited.remove(nearest)
cost += distance(current, start) // Return to start
return tour, cost
Time: O(n²)
Approximation: No guarantee (can be arbitrarily bad)
Example Run:
Start at A: A → B (10) → C (12) → D (8) → A (20) = 50
In this case, found optimal! But not always.
3. 2-Approximation for Metric TSP:
If distances satisfy triangle inequality: d(a,c) ≤ d(a,b) + d(b,c)
Algorithm:
1. Find Minimum Spanning Tree (MST)
2. Do DFS traversal of MST (pre-order)
3. Take shortcut tour following DFS order
Guarantee: Tour ≤ 2 × OPT
Time: O(n²)
Approximation Algorithms:
| Algorithm | Approximation Ratio | Conditions |
|---|---|---|
| MST-based | 2-approximation | Metric TSP |
| Christofides | 1.5-approximation | Metric TSP (best known!) |
| Nearest Neighbor | No guarantee | General TSP |
| Lin-Kernighan | No guarantee | Heuristic (works well in practice) |
Variants of TSP:
- Symmetric TSP: d(i,j) = d(j,i) (undirected)
- Asymmetric TSP: d(i,j) ≠ d(j,i) (directed)
- Metric TSP: Satisfies triangle inequality
- Euclidean TSP: Points in 2D plane, Euclidean distance
- Multiple TSP: Multiple salesmen, multiple tours
- TSP with Time Windows: Visit cities in time constraints
Real-World Applications:
- Logistics: Package delivery routes
- Manufacturing: PCB drilling, robot arm movement
- DNA Sequencing: Fragment assembly
- Astronomy: Telescope observation scheduling
- Transportation: Bus routes, airline scheduling
- Genome Mapping: Ordering DNA fragments
Famous TSP Instances:
- TSPLIB: Library of benchmark instances
- World TSP: 1,904,711 cities (all cities in world) - solved to near-optimality!
- Concorde Solver: State-of-the-art exact TSP solver
8. More Examples of NP-Complete and NP-Hard Problems
1. Vertex Cover Problem
Definition: Given graph G and integer k, does G have a vertex cover of size ≤ k?
Vertex Cover: Set of vertices S such that every edge has at least one endpoint in S
Example Graph:
A --- B --- C
| | |
D --- E --- F
Vertex Cover of size 3: {B, E, D}
✓ All edges covered
Complexity: NP-Complete
Application: Network monitoring, security
2. Clique Problem
Definition: Given graph G and integer k, does G have a clique of size ≥ k?
Clique: Complete subgraph - every pair of vertices is connected
Example Graph:
A --- B
|\ /|
| X |
|/ \|
D ---C
Clique of size 4: {A, B, C, D}
✓ All pairs connected
Complexity: NP-Complete
Application: Social networks, bioinformatics
3. Graph Coloring
Definition: Can graph G be colored with k colors such that no adjacent vertices have same color?
Example - 3-Coloring:
A(R) --- B(G)
| X |
| / \ |
D(B) --- C(R)
✓ Valid 3-coloring: Red, Green, Blue
No adjacent vertices share color
Complexity:
- 2-Coloring: Polynomial (BFS/DFS)
- 3-Coloring: NP-Complete
- k-Coloring (k ≥ 3): NP-Complete
Application: Register allocation, scheduling, map coloring
4. Subset Sum Problem
Definition: Given set of integers S and target T, is there a subset of S that sums to T?
Example:
S = {3, 34, 4, 12, 5, 2}
T = 9
Solution: {4, 5} or {3, 4, 2}
Both sum to 9 ✓
Complexity: NP-Complete
Pseudo-Polynomial: O(n·T) dynamic programming (if T is small)
Application: Resource allocation, scheduling
5. Knapsack Problem
Definition: Given items with weights and values, and capacity W, maximize value without exceeding W
Example:
Items: [(weight, value)]
Item 1: (2, 3)
Item 2: (3, 4)
Item 3: (4, 5)
Item 4: (5, 6)
Capacity: W = 8
Optimal: Items 1, 2, 4
Weight: 2 + 3 + 5 = 10 > 8 ✗
Try: Items 2, 3
Weight: 3 + 4 = 7 ≤ 8 ✓
Value: 4 + 5 = 9
Complexity:
- 0/1 Knapsack: NP-Hard
- Fractional Knapsack: Polynomial (greedy)
6. Boolean Satisfiability (SAT)
Definition: Given Boolean formula, is there an assignment making it TRUE?
Example - 3-SAT:
F = (x₁ ∨ ¬x₂ ∨ x₃) ∧ (¬x₁ ∨ x₂ ∨ x₄) ∧ (¬x₃ ∨ ¬x₄ ∨ x₁)
Solution: x₁=T, x₂=T, x₃=T, x₄=F
Check:
(T ∨ F ∨ T) = T ✓
(F ∨ T ∨ F) = T ✓
(F ∨ T ∨ T) = T ✓
Result: T ∧ T ∧ T = T ✓ Satisfiable!
Variants:
- 2-SAT: Polynomial (can solve efficiently)
- 3-SAT: NP-Complete
- k-SAT (k ≥ 3): NP-Complete
Summary Table:
| Problem | Class | Best Algorithm | Approximable? |
|---|---|---|---|
| Vertex Cover | NP-Complete | O(2ⁿ) | 2-approx |
| Clique | NP-Complete | O(2ⁿ) | Hard to approx |
| 3-Coloring | NP-Complete | O(2ⁿ) | Hard to approx |
| Subset Sum | NP-Complete | O(n·T) pseudo-poly | FPTAS exists |
| Knapsack | NP-Hard | O(n·W) pseudo-poly | FPTAS exists |
| 3-SAT | NP-Complete | O(1.3ⁿ) best known | Hard to approx |
| Hamiltonian Cycle | NP-Complete | O(2ⁿn²) | Hard to approx |
| TSP (general) | NP-Hard | O(n²2ⁿ) | Hard to approx |
| Metric TSP | NP-Hard | O(n²2ⁿ) | 1.5-approx |
Practical Takeaway:
When facing NP-Complete/NP-Hard problem:
- Check for special case: Your instance may be easier
- Use approximation: Get near-optimal quickly
- Try heuristics: Often work well in practice
- Small instances: Exact algorithms may be feasible
- Accept "good enough": Don't need perfect solution
⚡ Quick Revision (Unit V Cheat Sheet)
Core Class Definitions
- P: Deterministic poly-time decidable problems.
- NP: Problems whose YES certificates verifiable in poly-time.
- NP-Complete: In NP & NP-Hard (all NP problems reduce to it).
- NP-Hard: At least as hard as NP; may lie outside NP.
Proving NP-Completeness (Checklist)
- Show problem ∈ NP (define certificate + verifier).
- Select known NP-Complete problem K.
- Define poly-time reduction f: instance of K → instance of new problem.
- Prove equivalence: x ∈ K ⇔ f(x) ∈ L.
Common NP-Complete Set
SAT, 3-SAT, CLIQUE, VERTEX COVER, INDEPENDENT SET, HAMILTONIAN CYCLE, TSP (decision), SUBSET SUM, PARTITION, 3-COLORING, KNAPSACK (decision).
Strategies for NP-Hard Problems
- Approximation: e.g., Christofides for metric TSP (1.5×).
- Heuristics: Local search / genetic / simulated annealing.
- Parameterized: Exploit small k (FPT algorithms).
- Branch & Bound / Cutting Planes: Prune search space.
If P = NP (Implications)
- Polynomial-time algorithms for all NP-Complete problems.
- Break most public-key cryptography.
- Major shift in practical algorithm design.