๐ Complexity Classes: P, NP, NP-Hard, NP-Complete
1๏ธโฃ Introduction
In computational complexity theory, problems are classified based on how efficiently they can be solved.
The most important classes are:
- P (Polynomial time)
- NP (Non-deterministic Polynomial time)
- NP-Hard
- NP-Complete
These classes help us understand:
- Which problems are easy
- Which are hard
- Which are unsolvable efficiently
๐ 2๏ธโฃ Class P (Polynomial Time)
๐น Definition
Class P consists of all problems that can be solved in polynomial time by a deterministic algorithm.
[
O(n),; O(n^2),; O(n^3),; O(n \log n)
]
๐น Characteristics
โ Efficient and tractable
โ Solvable in reasonable time
โ Deterministic algorithms used
๐น Examples
- Linear Search
- Binary Search
- Merge Sort
- Shortest Path (Dijkstra)
๐ 3๏ธโฃ Class NP (Non-deterministic Polynomial Time)
๐น Definition
Class NP consists of problems for which a given solution can be verified in polynomial time.
It may be difficult to find the solution, but easy to verify it.
๐น Characteristics
โ Solution verification is fast
โ Solution finding may be slow
โ Uses non-deterministic model
๐น Examples
- Traveling Salesperson (decision version)
- Subset Sum
- Hamiltonian Cycle
๐ 4๏ธโฃ NP-Hard Problems
๐น Definition
A problem is NP-Hard if:
It is at least as hard as the hardest problems in NP.
- Not necessarily in NP
- May not be decision problems
- May not have polynomial-time verification
๐น Characteristics
โ Very difficult problems
โ No known efficient solution
โ May be optimization problems
๐น Examples
- Traveling Salesperson (optimization version)
- Halting Problem
- Scheduling problems
๐ 5๏ธโฃ NP-Complete Problems
๐น Definition
A problem is NP-Complete if:
- It belongs to NP
- It is NP-Hard
๐น Meaning
- Hardest problems in NP
- If one NP-Complete problem is solved in polynomial time โ all NP problems can be solved in polynomial time
๐น Examples
- SAT (Boolean Satisfiability Problem)
- Subset Sum
- Clique Problem
- Vertex Cover
- Hamiltonian Cycle
๐ 6๏ธโฃ Relationship Between Classes
P โ NP
NP-Complete โ NP
NP-Hard โ NP-Complete
๐ Key idea:
- All P problems are in NP
- NP-Complete problems are hardest in NP
- NP-Hard includes even harder problems
๐ 7๏ธโฃ P vs NP Problem
- Biggest open problem in computer science
- Question:
Is P = NP?
โ If YES:
- All NP problems become easy
โ If NO:
- Some problems are inherently hard
๐ 8๏ธโฃ Summary Table
| Class | Meaning | Solvable? | Verifiable? |
|---|---|---|---|
| P | Easy problems | โ Yes | โ Yes |
| NP | Verifiable problems | โ Unknown | โ Yes |
| NP-Hard | Very hard problems | โ Unknown | โ Not always |
| NP-Complete | Hardest in NP | โ Unknown | โ Yes |
๐ Key Differences
| Feature | P | NP | NP-Hard | NP-Complete |
|---|---|---|---|---|
| Solution time | Polynomial | Unknown | Unknown | Unknown |
| Verification | Easy | Easy | Not always | Easy |
| Type | Easy | Medium | Hard | Very Hard |
๐ Conclusion
The classification of problems into P, NP, NP-Hard, and NP-Complete helps us understand the limits of computation.
NP-Complete problems are the most critical, as solving one efficiently would solve all NP problems efficiently.
๐ Exam Tip
๐ Always include:
- Definitions
- Examples
- Relationship diagram
- P vs NP statement
