Complexity Classes: P, NP,Co-NP, NP-Hard and NP-Complete
- Get link
- X
- Other Apps
Complexity Classes: P, NP,Co-NP, NP-Hard and NP-Complete
1. Why do we need complexity classes?
When studying algorithms, we have seen many problems that can be solved efficiently.
For example:
- Sorting
- Searching
- Breadth-first search
- Depth-first search
- Shortest paths
- Minimum spanning trees
Most of these have polynomial-time algorithms, such as:
But there are also problems for which we do not know whether an efficient algorithm exists.
This leads us to the important classes:
The most famous question associated with these classes is:
This is still an open problem.
2. Decision Problems
Before understanding NP, we need to understand a decision problem.
A decision problem has only two possible answers:
YES or NO
Example: Shortest Path
Instead of asking:
Find the shortest path from
utov.
we can ask:
Is there a path from
utovcontaining at mostkedges?
The answer is:
YES or NO
This is a decision version of the shortest-path problem.
Why are decision problems important?
NP-completeness is defined primarily for decision problems.
3. Class P
Definition
P consists of problems that can be solved in polynomial time.
That means there is an algorithm whose running time is:
for some constant .
Here, represents the size of the input.
Simple interpretation
Think of P as:
Problems for which we know efficient algorithms.
For example, suppose an algorithm takes:
time.
Since is a polynomial, the problem belongs to P.
Examples of P problems
Many problems studied earlier in algorithms belong to P:
| Problem | Typical complexity |
|---|---|
| BFS | |
| DFS | |
| Minimum Spanning Tree | |
| Shortest Path | Polynomial |
| Sorting |
Therefore:
4. Class NP
NP stands for:
For undergraduate students, the easiest interpretation is:
NP contains problems for which a proposed solution, called a certificate, can be verified in polynomial time.
Notice that NP is about verification, not necessarily about finding the solution quickly.
Example: Hamiltonian Cycle
Consider a directed graph.
The problem is:
Does the graph contain a Hamiltonian cycle?
A Hamiltonian cycle visits every vertex exactly once and returns to the starting vertex.
Suppose somebody gives us this certificate:
v1 → v3 → v5 → v2 → v4 → v1
We can verify it efficiently.
We check:
Step 1: Does every vertex occur?
v1, v2, v3, v4, v5
Yes.
Step 2: Does every required edge exist?
Check:
(v1,v3) (v3,v5) (v5,v2) (v2,v4) (v4,v1)
Step 3: Does the cycle return to the starting vertex?
Yes.
Therefore, we can verify the certificate in polynomial time.
So:
5. Another Example: 3-CNF SAT
Consider a Boolean formula such as:
(x1 OR x2 OR NOT x3) AND (NOT x1 OR x3 OR x4) AND (x2 OR NOT x3 OR x4)
The question is:
Is there an assignment of 0 and 1 to the variables that makes the formula TRUE?
Suppose someone gives us:
x1 = 1 x2 = 0 x3 = 1 x4 = 0
We can simply substitute these values into the formula and check whether the complete formula is TRUE.
This can be done in polynomial time.
Therefore, 3-CNF satisfiability belongs to NP.
This class contains many problems that one would like to be able to solve effectively:
- Boolean Satisfiability Problem (SAT).
- Hamiltonian Path Problem.
- Graph coloring.
6. P is a subset of NP
This is one of the most important ideas.
Suppose a problem belongs to P.
That means:
We can solve the problem in polynomial time.
If we can solve a problem quickly, then obviously we can also verify its answer quickly.
Therefore:
In simple words:
Every problem that can be solved efficiently can also be verified efficiently.
7. The P versus NP Question
The important question is:
There are two possibilities.
Possibility 1
This would mean that every problem whose solution can be verified efficiently can also be solved efficiently.
Possibility 2
This would mean that there are problems whose solutions can be verified efficiently but cannot be solved efficiently.
We currently do not know which possibility is correct.
8.Co-NP Class
Co-NP is the class of decision problems where a NO answer can be verified in polynomial time.
In simple terms:
- NP: A YES answer has a polynomial-time verifiable certificate.
- Co-NP: A NO answer has a polynomial-time verifiable certificate.
Example
Consider the SAT problem:
Does a Boolean formula have a satisfying assignment?
For SAT:
- YES: We can give an assignment that satisfies the formula → easy to verify → SAT is in NP.
- NO: There is no satisfying assignment → this is the complementary problem and belongs to Co-NP.
The complement of an NP problem is therefore naturally associated with Co-NP.
Important relationship
Similarly,
One-line memory trick
NP → YES can be verified quickly.
Co-NP → NO can be verified quickly.
9. NP-Complete Problems
Now we come to the central topic
A problem is NP-complete if it belongs to NP and is as hard as any problem in NP.
Therefore, an NP-complete problem must satisfy two conditions.
Condition 1
It must belong to NP.
That means:
Its solution can be verified in polynomial time.
Condition 2
It must be at least as hard as every problem in NP.
Therefore:
10. Example: Hamiltonian Cycle
Hamiltonian Cycle as an important example.
The problem is:
Given a directed graph , does there exist a simple cycle that contains every vertex in ?
A proposed cycle can be verified in polynomial time.
Therefore:
But Hamiltonian Cycle is also known to be as hard as the hardest problems in NP.
Therefore:
Some example problems include:
- Hamiltonian Cycle.
- Satisfiability.
- Vertex cover.
11. What Does "As Hard as Any Problem in NP" Mean?
This is where polynomial-time reductions are used.
Suppose we have two decision problems:
Problem A Problem B
Suppose we can transform every instance of A into an instance of B in polynomial time:
Instance of A | | polynomial-time reduction ↓ Instance of B
and the answers remain the same:
A = YES → B = YES A = NO → B = NO
Then we say:
or
12. Why Are Reductions Useful?
Suppose A is a problem we already know to be very hard.
If:
then B must be at least as hard as A.
Why?
Suppose we suddenly discover a polynomial-time algorithm for B.
Then we could:
- Transform A into B.
- Solve B efficiently.
- Use the answer to solve A.
Therefore, A would also have a polynomial-time algorithm.
This is the basic idea behind proving that a problem is hard.
A first NP-complete problem
Because the technique of reduction relies on having a problem already known to be NP-complete in order to prove a different problem NP-complete, there must be some first NP-complete problem. We’ll use the circuit-satisΓ»ability problem, in which the input is a boolean combinational circuit composed of AND, OR, and NOT gates, and the question is whether there exists some set of boolean inputs to this circuit that causes its output to be 1.
13. NP-Hard
Now we can understand NP-Hard.
A problem is NP-Hard if:
Every problem in NP can be polynomially reduced to it.
In simpler language:
An NP-Hard problem is at least as hard as every problem in NP.
Important point
An NP-Hard problem does not necessarily belong to NP.
This is the important difference between NP-Hard and NP-Complete.
14. NP-Hard Example: TSP Optimization
Consider the Travelling Salesman Problem.
Optimization version
Find the minimum-cost tour that visits every city exactly once and returns to the starting city.
The goal is to find the best solution.
This is an optimization problem.
The optimization version of TSP is NP-Hard.
Why isn't it simply called NP-Complete?
Because NP-completeness is defined for decision problems.
15. TSP Decision Version
We can convert TSP into a decision problem.
Instead of asking:
What is the minimum-cost tour?
ask:
Is there a tour whose total cost is at most ?
The answer is:
YES or NO
A proposed tour can be checked efficiently.
Therefore, the decision version is in NP.
It is also NP-Hard.
Hence:
while:
This is a very useful example for students.
Some of the examples of problems in Np-hard are:
- Halting problem.
- Qualified Boolean formulas.
- No Hamiltonian cycle.
16. NP-Complete vs NP-Hard
This is probably the most important comparison.
| Feature | NP | NP-Hard | NP-Complete |
|---|---|---|---|
| Can solution be verified in polynomial time? | Yes | Not necessarily | Yes |
| Must be a decision problem? | Usually considered decision problems | No | Yes |
| At least as hard as every NP problem? | Not necessarily | Yes | Yes |
| Example | Hamiltonian Cycle is in NP | TSP optimization | Hamiltonian Cycle |
The key difference is:
17. A Simple Diagram
Relationship among classes is shown in this diagram:
The exact set-theoretic relationship between P and NP is unknown because we do not know whether .
18. Three Important Examples
The textbook gives some particularly interesting pairs of problems.
1. Shortest Path vs Longest Simple Path
Shortest path:
Can be solved in polynomial time.
But determining whether a graph contains a simple path with at least a given number of edges is NP-Complete.
2. Euler Tour vs Hamiltonian Cycle
An Euler tour asks whether we can traverse every edge exactly once.
This can be solved efficiently.
A Hamiltonian cycle asks whether we can visit every vertex exactly once.
This is NP-Complete.
The difference looks small, but the computational difficulty is dramatically different.
3. 2-CNF vs 3-CNF Satisfiability
The textbook points out another interesting pair.
2-CNF satisfiability:
3-CNF satisfiability:
Again, the difference appears small: two variables per clause versus three variables per clause.
But the computational complexity changes dramatically.
19. Why NP-Complete Problems Matter
Suppose we prove that a problem is NP-Complete.
Does that mean that we have mathematically proved that it cannot be solved efficiently?
No.
This is an important point.
We currently do not know whether NP-Complete problems can be solved in polynomial time.
If someone discovers a polynomial-time algorithm for one NP-Complete problem, then:
and every problem in NP would have a polynomial-time algorithm.
20. Why Do We Believe NP-Complete Problems Are Difficult?
No polynomial-time algorithm has been discovered for any NP-Complete problem despite enormous research effort.
Therefore, most computer scientists believe:
and consequently believe that NP-Complete problems are computationally hard.
But this has not been proved.
So it is better to say:
NP-Complete problems are believed to be intractable, but their intractability has not been proved.
21. Optimization vs Decision
Consider the Shortest Path problem.
Optimization version
Find the shortest path from to .
Decision version
Is there a path from to having at most edges?
The decision version simply asks:
YES / NO
Similarly, for TSP:
Optimization
Find the cheapest tour.
Decision
Is there a tour costing at most ?
NP-Completeness theory works directly with the decision version.
22. Complete Picture
Students can remember the four classes with these four sentences:
P
Can solve efficiently.
NP
Can verify a given solution efficiently.
NP-Hard
At least as hard as every problem in NP.
NP-Complete
Both NP and NP-Hard.
23.Quick Summary
| Class | Simple definition | Example |
|---|---|---|
| P | Solvable in polynomial time | Shortest Path, Euler Tour |
| NP | Solution/certificate verifiable in polynomial time. ( yes answers) | Hamiltonian Cycle |
Co-NP NP-Hard | No, answers can be checked in polynomial time. At least as hard as every problem in NP | SAT with No answer TSP Optimization |
| NP-Complete | Both NP and NP-Hard | TSP Decision, Hamiltonian Cycle, 3-CNF SAT |
The three formulas worth remembering
And the famous unanswered question:
In one line:
P problems can be solved efficiently; NP problems can have their solutions verified efficiently; NP-Hard problems are at least as hard as every NP problem; and NP-Complete problems are the problems that are both in NP and NP-Hard.
- Get link
- X
- Other Apps

Comments
Post a Comment