Complexity Classes: P, NP,Co-NP, NP-Hard and NP-Complete

 

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:

𝑂(𝑛),𝑂(𝑛2),𝑂(𝑛3),𝑂(π‘›π‘˜)

But there are also problems for which we do not know whether an efficient algorithm exists.

This leads us to the important classes:

𝑃,𝑁𝑃,𝑁𝑃-Hard,𝑁𝑃-Complete

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 u to v.

we can ask:

Is there a path from u to v containing at most k edges?

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:

𝑂(𝑛2)

time.

Since 𝑛2 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𝑂(𝐸log⁡𝑉)
Shortest PathPolynomial
Sorting𝑂(𝑛log⁡𝑛)

Therefore:

P = problems that can be solved in polynomial time

4. Class NP

NP stands for:

Nondeterministic Polynomial time

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:

Hamiltonian-Cycle∈𝑁𝑃

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

X∈NP⇒X‾∈Co-NP\boxed{X\in NP \Rightarrow \overline{X}\in Co\text{-}NP}

Similarly,

X∈Co-NP⇒X‾∈NP\boxed{X\in Co\text{-}NP \Rightarrow \overline{X}\in NP}

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:

NP-Complete=NP+NP-Hard

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:

Hamiltonian Cycle∈𝑁𝑃

But Hamiltonian Cycle is also known to be as hard as the hardest problems in NP.

Therefore:

Hamiltonian Cycle is NP-Complete


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:

𝐴 reduces to π΅

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:

  1. Transform A into B.
  2. Solve B efficiently.
  3. 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:

TSP Decision is NP-Complete

while:

TSP Optimization is NP-Hard

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.

FeatureNPNP-HardNP-Complete
Can solution be verified in polynomial time?YesNot necessarilyYes
Must be a decision problem?Usually considered decision problemsNoYes
At least as hard as every NP problem?Not necessarilyYesYes
ExampleHamiltonian Cycle is in NPTSP optimizationHamiltonian Cycle

The key difference is:

NP-Complete=NP-Hard+belongs to NP

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.

Shortest Path∈𝑃

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.

Euler Tour∈𝑃

A Hamiltonian cycle asks whether we can visit every vertex exactly once.

This is NP-Complete.

Hamiltonian Cycle∈𝑁𝑃-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:

2-CNF SAT∈𝑃

3-CNF satisfiability:

3-CNF SAT∈𝑁𝑃-Complete

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 

ClassSimple definitionExample
PSolvable in polynomial timeShortest Path, Euler Tour
NPSolution/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-CompleteBoth NP and NP-HardTSP Decision, Hamiltonian Cycle, 3-CNF SAT

The three formulas worth remembering

𝑃⊆𝑁𝑃
𝑁𝑃-Complete=𝑁𝑃∩𝑁𝑃-Hard

If any NP-Complete problem∈𝑃, then π‘ƒ=𝑁𝑃

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.

Comments

Popular posts from this blog

Design and Analysis of Algorithms PCCST502 Semester 5 KTU CS 2024 Scheme - Dr Binu V P

Greedy Algorithm-Fractional Knapsack Problem

Introduction to Algorithms