Complexity Classes

Distribute Education like Computer Geek

Introduction

.

In the field of algorithms, understanding how efficiently an algorithm solves a problem is crucial. This is where complexity classes come into play. Complexity classes group problems based on the resources (time and space) required to solve them. In simple terms, they help us understand how “hard” a problem is and what kind of algorithms can solve it.

In this post, we will discuss the major complexity classes in the context of time complexity (how long it takes for an algorithm to run), such as P, NP, NP-Complete, and NP-Hard. These classes form the foundation for analyzing algorithms in computer science.

Computational Complexity Theory

.

Complexity Classes

1. Class P Problem (Polynomial Time)

Problems in class P are those that can be solved by an algorithm in polynomial time. In other words, the time taken to solve these problems grows at a polynomial rate as the input size increases.

Polynomial time algorithms are considered efficient and practical for real-world use. If a problem is in class P, it means we can solve it relatively quickly even for large inputs.

Examples of Polynomial Time Algorithms

Polynomial Time Algorithm

.

2. Class NP (Nondeterministic Polynomial Time)

Problems in class NP are those where a solution can be verified in polynomial time, but we don’t necessarily know how to solve them efficiently.

The key idea here is that if someone gives us a solution, we can check if it’s correct in a reasonable amount of time.

NP problems are crucial because they are often more challenging to solve but still easy to verify. Easy means Polynomial time.

Examples of Non-Polynomial Time Algorithms

Non Polynomial Time Algorithm

.

Tree with miss hit

In the image, the tree represents different potential paths for solving a problem which is in NP. Solving it requires O(2n) time.

However, if someone gives you the correct solution (like the “Hit” in the tree), verifying that it’s correct is much simpler. You only need to check the path and see if it satisfies all the rules or conditions of the problem.

A language L is considered to be NP if and only if a polynomial-time verification algorithm for it exists. Keep in mind that P ⊆ NP. We are now unsure of whether P = NP or P ⊂ NP.

This NP class also introduces us to the famous P vs NP problem, a major unsolved question in computer science: “Is every NP problem also in P?” In simpler terms, can every problem whose solution is easy to check also be solved quickly?

You can read about this in the next post. Full details are provided.

.

3. NP-Hard Problems

NP-Hard problems are at least as hard as the hardest problems in NP, but they may not even belong to class NP.

We could develop an algorithm to solve the problem, but once it’s executed, it keeps running endlessly without ever stopping. That would only happen if you use a large input for a given problem.

Example

  1. If you’re trying to solve a traveling salesman problem and you input 1000 cities, expecting to get a solution, it won’t happen. You’ve essentially set the program to run indefinitely, and it will take about 240 years to solve if you have a super computer.
  2. Scheduling a Family Gathering is an NP-hard problem. When organizing a gathering for many family members, each with their own availability, it becomes increasingly difficult to find a time that works for everyone. While it’s easy to accommodate a few people, as the number of participants grows, the combinations of schedules become exponentially complex, making it challenging to find the best solution.

.

The Travelling Salesman Problem is as hard as the hardest problem in NP. If we take large input, then we can say that it is NP Hard.

Also, the examples of NP problem are hard problem, if we take large input.

NP Hard Problems

So, how can we say that all the problem written above are NP-Hard?

We can say this thing with the help of Reduction.

.

What is Reduction ?

Reduction is a concept used in computer science to compare the complexity of different problems. It refers to the process of transforming one problem into another in such a way that a solution to the second problem can be used to solve the first.

If a problem A can be reduced to problem B, it means that solving problem B would allow us to solve problem A as well.

Also, we know that Boolean Satisfiability Problem (SAT) is NP-Hard. So, if we can say that Boolean Satisfiability Problem (SAT) can be reduced to Hamiltonian Path Problem, it means that solving Hamiltonian Path Problem would allow us to solve Boolean Satisfiability Problem (SAT) as well.

To find the relationship between the problems, we are using polynomial time reduction.

.

Polynomial Time Reductions

There exists a polynomial-time computable function f that maps any instance of problem A to an instance of problem B. That is, given an instance x of problem A, the function f(x) produces an instance of problem B.

The answer to the instance x of problem A is the same as the answer to the transformed instance f(x) of problem B. More formally, for any instance x of problem A

x ∈ A  ⟺  f(x) ∈ B

This means that if x is a “yes” instance of decision problem A, then f(x) is a “yes” instance of decision problem B, and if x is a “no” instance of A, then f(x) is a “no” instance of B.

So, Boolean Satisfiability Problem (SAT) is NP-Hard and it is reducing to Hamiltonian Path Problem in polynomial time. So, Hamiltonian Path Problem is NP-Hard.

Boolean Satisfiability Problem (SAT) ∝ Hamiltonian Path Problem

The same way, we can reduce to other problems also by taking polynomial time.

SAT ∝ Subset Sum Problem

SAT ∝ Travelling Salesman Problem

SAT ∝ Graph Colouring Problem

SAT ∝ Sudoku Solver

SAT ∝ Clique Problem

SAT ∝ Partition Problem

SAT ∝ 0/1 Knapsack Problem

So, every problem we mentioned here is the hardest problem and it is a problem of NP-Hard Class.

.

4. NP-Complete Problems

A problem A is NP-Complete if it satisfies two properties

  • For every problem B ϵ NP, problem B can be reduced to problem A in polynomial time (A is NP-Hard).
  • Problem A is in NP means a non-deterministic algorithm can be written for a problem A.

If the Problem satisfies the property (1) only, then it is an NP-Hard Problem. If the problem satisfies both the properties mentioned here, then it is an NP-Complete Problem.

NP Complete Problems

.

Further, we must look upon some problem that is an NP-hard problems but not NP-complete.
  1. Halting Problem is an undecidable Problem and is also proved by Alan Turing. It is not in NP.
  2. Graph Isomorphism Problem is not yet classified as NP- Complete Problem.
  3. Post Correspondence Problem (PCP) is an undecidable language.
  4. Integer Programming involves finding integer solutions to a system of linear equations or inequalities.
  5. Minimum Circuit Size Problem asks for the smallest Boolean circuit that can compute a given Boolean function.

Test Yourself

Q1- Consider two decision problems Q1, Q2 such that Q1 reduces in polynomial time to 3-SAT and 3-SAT reduces in polynomial time to Q2. Then which one of the following is consistent with the above statement?
  1. Q1 is in NP, Q2 is NP hard
  2. Q2 is in NP, Q1 is NP hard
  3. Both Q1 and Q2 are in NP
  4. Both Q1 and Q2 are in NP hard

Ans – (1)

Explanation – 3-SAT is a Boolean satisfiability problem with exactly three literals per clause (NP-Complete).

Q1 reduces to 3-SAT in polynomial time means Q1 is in NP.

3-SAT reduces to Q2 in polynomial time means Q2 is NP-Hard. 

Q2- Which of the following is an NP-Complete problem?
  1. Binary Search
  2. Quick Sort
  3. Boolean Satisfiability Problem (SAT)
  4. Halting Problem

Ans – (3)

Explanation – SAT is a well-known NP-Complete problem. It can be verified in polynomial time, and every NP problem can be reduced to it.

Q3- Which of the following problems is NP-Hard but not NP-Complete?
  1. Hamiltonian Path Problem
  2. 0/1 Knapsack Problem
  3. Halting Problem
  4. Clique Problem

Ans – (3)

Explanation – The Halting Problem is undecidable and falls under NP-Hard but not NP-Complete because it is not in NP.

Q4- What does NP stand for in complexity theory?
  1. Non-deterministic Polynomial
  2. Negative Polynomial
  3. Null Polynomial
  4. Non-polynomial

Ans – (1)

Explanation – NP stands for Non-deterministic Polynomial, referring to problems that can be solved by a non-deterministic machine in polynomial time.

Q5- Which of the following statements are TRUE?
  1. The problem of determining whether there exists a cycle in an undirected graph is in P.
  2. The problem of determining whether there exists a cycle in an undirected graph is in NP.
  3. If a problem A is NP-Complete, there exists a non-deterministic polynomial time algorithm to solve A.
  • 1, 2 and 3
  • 1 and 2 only
  • 2 and 3 only
  • 1 and 3 only
(GATE)

Ans – (1)

Q6- Let πA be a problem that belongs to the class NP. Then which one of the following is TRUE?
  1. There is no polynomial time algorithm for πA.
  2. If πAcan be solved deterministically in polynomial time, then P = NP.
  3. If πAis NP-hard, then it is NP-complete.
  4. πAmay be undecidable.

Ans – (3)

Explanation – Since a problem in P is likewise in NP, option 1 cannot be true. We simply put the problem in P if it can be solved deterministically in polynomial time. In this case, we are unable to remark on P=NP. Option 2 is therefore also not true. Given the definition of NP-completeness, option 3 is TRUE.

It is decidable in polynomial time, so option 4 is false.

Q7- Let S be an NP-complete problem and Q and R be two other problems not known to be in NP. Q is polynomial time reducible to S and S is polynomial-time reducible to R. Which one of the following statements is true?
  1. R is NP-complete
  2. R is NP-hard
  3. Q is NP-complete
  4. Q is NP-hard

Ans – (2)

Explanation – We don’t know if R has an algorithm which is non-deterministic in nature. So, option 1 is false.

S is polynomial time reducible to R which concludes that R is NP-Hard.

We can’t say about option 3 and 4 that which is true. Q could be NP, also Q could be P, NP-complete and NP-Hard.

Q8- The problem 3-SAT and 2-SAT are
  1. both in P
  2. both NP complete
  3. NP-complete and in P respectively
  4. undecidable and NP-complete respectively

Ans – (3)

Explanation

  • SAT: General Boolean satisfiability problem (NP-complete).
  • 2-SAT: Boolean satisfiability problem with exactly two literals per clause (solvable in polynomial time).
  • 3-SAT: Boolean satisfiability problem with exactly three literals per clause (NP-complete).
Q9- Ram and Shyam have been asked to show that a certain problem Π is NP-complete. Ram shows a polynomial time reduction from the 3-SAT problem to Π, and Shyam shows a polynomial time reduction from Π to 3-SAT. Which of the following can be inferred from these reductions ?
  1. Π is NP-hard but not NP-complete
  2. Π is in NP, but is not NP-complete
  3. Π is NP-complete
  4. Π is neither NP-hard, nor in NP

Ans – (3)

Explanation – It is NP hard since the 3-SAT problem can be reduced to Π in polynomial time. The algorithm is NP-Complete since it can reduce polynomial time from Π to 3-SAT.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha