The Wayback Machine - https://web.archive.org/web/20240728233545/https://www.geeksforgeeks.org/introduction-to-np-completeness/
Open In App

Introduction to NP-Complete Complexity Classes

Last Updated : 15 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

NP-complete problems are a subset of the larger class of NP (nondeterministic polynomial time) problems. NP problems are a class of computational problems that can be solved in polynomial time by a non-deterministic machine and can be verified in polynomial time by a deterministic Machine. A problem L in NP is NP-complete if all other problems in NP can be reduced to L in polynomial time. If any NP-complete problem can be solved in polynomial time, then every problem in NP can be solved in polynomial time. NP-complete problems are the hardest problems in the NP set.

A decision problem L is NP-complete if it follow the below two properties:

  1. L is in NP (Any solution to NP-complete problems can be checked quickly, but no efficient solution is known).
  2. Every problem in NP is reducible to L in polynomial time (Reduction is defined below). 

A problem is NP-Hard if it obeys Property 2 above and need not obey Property 1. Therefore, a problem is NP-complete if it is both NP and NP-hard.

np-complete-complexity-classes

NP-Complete Complexity Classes

Decision vs Optimization Problems 

NP-completeness applies to the realm of decision problems.  It was set up this way because it’s easier to compare the difficulty of decision problems than that of optimization problems.   In reality, though, being able to solve a decision problem in polynomial time will often permit us to solve the corresponding optimization problem in polynomial time (using a polynomial number of calls to the decision problem). So, discussing the difficulty of decision problems is often really equivalent to discussing the difficulty of optimization problems. 
For example, consider the vertex cover problem (Given a graph, find out the minimum sized vertex set that covers all edges). It is an optimization problem. The corresponding decision problem is, given undirected graph G and k, is there a vertex cover of size k? 

What is Reduction? 

Let L1 and L2 be two decision problems. Suppose algorithm A2 solves L2. That is, if y is an input for L2 then algorithm A2 will answer Yes or No depending upon whether y belongs to L2 or not.
The idea is to find a transformation from L1 to L2 so that algorithm A2 can be part of algorithm A1 to solve L1.
 

Learning reduction, in general, is very important. For example, if we have library functions to solve certain problems and if we can reduce a new problem to one of the solved problems, we save a lot of time. Consider the example of a problem where we have to find the minimum product path in a given directed graph where the product of the path is the multiplication of weights of edges along the path. If we have code for Dijkstra’s algorithm to find the shortest path, we can take the log of all weights and use Dijkstra’s algorithm to find the minimum product path rather than writing a fresh code for this new problem.

How to prove that a given problem is NP-complete? 

From the definition of NP-complete, it appears impossible to prove that a problem L is NP-Complete.  By definition, it requires us to that show every problem in NP is polynomial time reducible to L.  Fortunately, there is an alternate way to prove it.  The idea is to take a known NP-Complete problem and reduce it to L. If a polynomial-time reduction is possible, we can prove that L is NP-Complete by transitivity of reduction (If an NP-Complete problem is reducible to L in polynomial time, then all problems are reducible to L in polynomial time). 

What was the first problem proved as NP-Complete? 

There must be some first NP-Complete problem proved by the definition of NP-Complete problems.  SAT (Boolean satisfiability problem) is the first NP-Complete problem proved by Cook (See CLRS book for proof). 

It is always useful to know about NP-Completeness even for engineers. Suppose you are asked to write an efficient algorithm to solve an extremely important problem for your company. After a lot of thinking, you can only come up exponential time approach which is impractical. If you don’t know about NP-Completeness, you can only say that I could not come up with an efficient algorithm. If you know about NP-Completeness and prove that the problem is NP-complete, you can proudly say that the polynomial-time solution is unlikely to exist. If there is a polynomial-time solution possible, then that solution solves a big problem of computer science many scientists have been trying for years. 

NP-Complete problems and their proof for NP-Completeness. 

  1. Prove that SAT is NP Complete
  2. Prove that Sparse Graph is NP-Complete
  3. Prove that KITE is NP-Complete
  4. Prove that Hamiltonian Cycle is NP-Complete
  5. Subset Sum is NP Complete
  6. Prove that Collinearity Problem is NP Complete
  7. Set partition is NP complete
  8. Hitting Set problem is NP Complete
  9. 3-coloring is NP Complete
  10. Set cover is NP Complete
  11. Optimized Longest Path is NP Complete
  12. Double SAT is NP Complete
  13. Prove that 4 SAT is NP complete
  14. Prove that Dense Subgraph is NP Complete by Generalisation
  15. Prove that a problem consisting of Clique and Independent Set is NP Complete
  16. Prove that Almost-SAT is NP Complete
  17. Prove that MAX-SAT is NP Complete
  18. Prove Max2SAT is NP-Complete by Generalisation
  19. Subset Equality is NP Complete
  20. Hitting Set problem is NP Complete


Previous Article
Next Article

Similar Reads

P, NP, CoNP, NP hard and NP complete | Complexity Classes
In computer science, there exist some problems whose solutions are not yet found, the problems are divided into classes known as Complexity Classes. In complexity theory, a Complexity Class is a set of problems with related complexity. These classes help scientists to group problems based on how much time and space they require to solve problems an
5 min read
Time Complexity and Space Complexity
Generally, there is always more than one way to solve a problem in computer science with different algorithms. Therefore, it is highly required to use a method to compare the solutions in order to judge which one is more optimal. The method must be: Independent of the machine and its configuration, on which the algorithm is running on.Shows a direc
14 min read
What does 'Space Complexity' mean?
Space Complexity: The term Space Complexity is misused for Auxiliary Space at many places. Following are the correct definitions of Auxiliary Space and Space Complexity. Auxiliary Space is the extra space or temporary space used by an algorithm. The space Complexity of an algorithm is the total space taken by the algorithm with respect to the input
2 min read
Sum of first n odd numbers in O(1) Complexity
Given the sequence of odd numbers 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, .... Find the sum of first n odd numbersExamples: Input : n = 2 Output : 4 Sum of first two odd numbers is 1 + 3 = 4. Input : 5 Output : 25 Sum of first 5 odd numbers is 1 + 3 + 5 + 7 + 9 = 25 A simple solution is to iterate through all odd numbers. C/C++ Code // A naive C
7 min read
Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
In this article, we will discuss the complexity of different operations in binary trees including BST and AVL trees. Before understanding this article, you should have a basic idea about Binary Tree, Binary Search Tree, and AVL Tree. The main operations in a binary tree are: search, insert and delete. We will see the worst-case time complexity of t
4 min read
Comparison of an Array and Hash table in terms of Storage structure and Access time complexity
Arrays and Hash Tables are two of the most widely used data structures in computer science, both serving as efficient solutions for storing and accessing data in Java. They have different storage structures and time complexities, making them suitable for different use cases. In this article, we will explore the differences between arrays and hash t
3 min read
Time Complexity Analysis | Tower Of Hanoi (Recursion)
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1) Only one disk can be moved at a time. 2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk ca
2 min read
Queries of nCr%p in O(1) time complexity
Given Q queries and P where P is a prime number, each query has two numbers N and R and the task is to calculate nCr mod p. Constraints: N <= 106 R <= 106 p is a prime number Examples: Input: Q = 2 p = 1000000007 1st query: N = 15, R = 4 2nd query: N = 20, R = 3 Output: 1st query: 1365 2nd query: 1140 15!/(4!*(15-4)!)%1000000007 = 1365 20!/(2
9 min read
Python Code for time Complexity plot of Heap Sort
Prerequisite : HeapSort Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for remaining element. We implement Heap Sort here, call it for different sized random lists, meas
3 min read
How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
Pre-requisite: Sieve of Eratosthenes What is Sieve of Eratosthenes algorithm? In order to analyze it, let's take a number n and the task is to print the prime numbers less than n. Therefore, by definition of Sieve of Eratosthenes, for every prime number, it has to check the multiples of the prime and mark it as composite. This process continues unt
3 min read
three90RightbarBannerImg