You are on page 1of 11

13/02/2024, 07:27 P, NP, CoNP, NP hard and NP complete | Complexity Classes - GeeksforGeeks

90% Refund @Courses DSA Data Structure Analysis of Algorithms Sorting Searching Greedy Recurs

P, NP, CoNP, NP hard and NP complete | Complexity


Classes
Read Courses Practice Jobs

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 and verify the solutions.
It is the branch of the theory of computation that deals with the resources
required to solve a problem.

The common resources are time and space, meaning how much time the
algorithm takes to solve a problem and the corresponding memory usage.

The time complexity of an algorithm is used to describe the number of


steps required to solve a problem, but it can also be used to describe how
long it takes to verify the answer.
The space complexity of an algorithm describes how much memory is
required for the algorithm to operate.

Complexity classes are useful in organising similar types of problems.

We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
Policy

https://www.geeksforgeeks.org/types-of-complexity-classes-p-np-conp-np-hard-and-np-complete/?ref=lbp 1/11
13/02/2024, 07:27 P, NP, CoNP, NP hard and NP complete | Complexity Classes - GeeksforGeeks

Types of Complexity Classes


This article discusses the following complexity classes:

1. P Class
2. NP Class
3. CoNP Class
4. NP-hard
5. NP-complete

P Class
The P in the P class stands for Polynomial Time. It is the collection of
decision problems(problems with a “yes” or “no” answer) that can be solved
by a deterministic machine in polynomial time.

Features:

The solution to P problems is easy to find.


P is often a class of computational problems that are solvable and
We use cookies to ensureTractable
tractable. you have themeans
best browsing
thatexperience on our website.
the problems can Bybeusing
solved in theory as
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy

https://www.geeksforgeeks.org/types-of-complexity-classes-p-np-conp-np-hard-and-np-complete/?ref=lbp 2/11
13/02/2024, 07:27 P, NP, CoNP, NP hard and NP complete | Complexity Classes - GeeksforGeeks

well as in practice. But the problems that can be solved in theory but not
in practice are known as intractable.

This class contains many problems:

1. Calculating the greatest common divisor.


2. Finding a maximum matching.
3. Merge Sort

NP Class
The NP in NP class stands for Non-deterministic Polynomial Time. It is the
collection of decision problems that can be solved by a non-deterministic
machine in polynomial time.

Features:

The solutions of the NP class are hard to find since they are being solved
by a non-deterministic machine but the solutions are easy to verify.
Problems of NP can be verified by a Turing machine in polynomial time.

Example:

Let us consider an example to better understand the NP class. Suppose


there is a company having a total of 1000 employees having unique
employee IDs. Assume that there are 200 rooms available for them. A
selection of 200 employees must be paired together, but the CEO of the
company has the data of some employees who can’t work in the same room
due to personal reasons.
This is an example of an NP problem. Since it is easy to check if the given
choice of 200 employees proposed by a coworker is satisfactory or not i.e. no
pair taken from the coworker list appears on the list given by the CEO. But
generating such a list from scratch seems to be so hard as to be completely
impractical.

It indicates that if someone can provide us with the solution to the problem,
we can find the correct and incorrect pair in polynomial time. Thus for the NP
class problem, the answer is possible, which can be calculated in polynomial
time.
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy

https://www.geeksforgeeks.org/types-of-complexity-classes-p-np-conp-np-hard-and-np-complete/?ref=lbp 3/11
13/02/2024, 07:27 P, NP, CoNP, NP hard and NP complete | Complexity Classes - GeeksforGeeks

This class contains many problems that one would like to be able to solve
effectively:

1. Boolean Satisfiability Problem (SAT).


2. Hamiltonian Path Problem.
3. Graph coloring.

Co-NP Class
Co-NP stands for the complement of NP Class. It means if the answer to a
problem in Co-NP is No, then there is proof that can be checked in
polynomial time.

Features:

If a problem X is in NP, then its complement X’ is also in CoNP.


For an NP and CoNP problem, there is no need to verify all the answers at
once in polynomial time, there is a need to verify only one particular
answer “yes” or “no” in polynomial time for a problem to be in NP or
CoNP.

Some example problems for CoNP are:

1. To check prime number.


2. Integer Factorization.

NP-hard class
An NP-hard problem is at least as hard as the hardest problem in NP and it
is a class of problems such that every problem in NP reduces to NP-hard.

Features:

All NP-hard problems are not in NP.


It takes a long time to check them. This means if a solution for an NP-hard
problem is given then it takes a long time to check whether it is right or
not.
A problem A is in NP-hard if, for every problem L in NP, there exists a
polynomial-time reduction from L to A.

Some of the examples of problems in Np-hard are:


We use cookies to ensure you have the best browsing experience on our website. By using
our site,
1.you acknowledge
Halting that you have read and understood our Cookie Policy & Privacy
problem.
Policy

https://www.geeksforgeeks.org/types-of-complexity-classes-p-np-conp-np-hard-and-np-complete/?ref=lbp 4/11
13/02/2024, 07:27 P, NP, CoNP, NP hard and NP complete | Complexity Classes - GeeksforGeeks

2. Qualified Boolean formulas.


3. No Hamiltonian cycle.

NP-complete class
A problem is NP-complete if it is both NP and NP-hard. NP-complete
problems are the hard problems in NP.

Features:

NP-complete problems are special as any problem in NP class can be


transformed or reduced into NP-complete problems in polynomial time.
If one could solve an NP-complete problem in polynomial time, then one
could also solve any NP problem in polynomial time.

Some example problems include:

1. Hamiltonian Cycle.
2. Satisfiability.
3. Vertex cover.

Complexity
Characteristic feature
Class

P Easily solvable in polynomial time.

NP Yes, answers can be checked in polynomial time.

Co-NP No, answers can be checked in polynomial time.

All NP-hard problems are not in NP and it takes a long time to


NP-hard
check them.

NP-complete A problem that is NP and NP-hard is NP-complete.

Feeling lost in the world of random DSA topics, wasting time without
progress? It's time for a change! Join our DSA course, where we'll guide you
We use cookies to ensure you have the best browsing experience on our website. By using
onyou
our site, anacknowledge
exciting journey toread
that you have master DSA efficiently
and understood and&on
our Cookie Policy schedule.
Privacy
Policy

https://www.geeksforgeeks.org/types-of-complexity-classes-p-np-conp-np-hard-and-np-complete/?ref=lbp 5/11
13/02/2024, 07:27 P, NP, CoNP, NP hard and NP complete | Complexity Classes - GeeksforGeeks

Ready to dive in? Explore our Free Demo Content and join our DSA course,
trusted by over 100,000 geeks!

DSA in C++
DSA in Java
DSA in Python
DSA in JavaScript

Recommended Problems
Solve Problems
Frequently asked DSA Problems

Three 90 Challenge ending on 29th Feb! Last chance to get 90% refund by
completing 90% course in 90 days. Explore offer now.

Last Updated : 03 Oct, 2023 95

Previous Next

Analysis of Algorithms | Big - Θ (Big Can Run Time Complexity of a


Theta) Notation comparison-based sorting algorithm be
less than N logN?

Share your thoughts in the comments Add Your Comment

Similar Reads
Introduction to NP-Complete Difference between NP hard and NP
Complexity Classes complete problem

Time Complexity and Space Proof that traveling salesman problem


Complexity is NP Hard

NP-Hard Class Time and Space complexity of 1-D and


2-D Array Operations
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy

https://www.geeksforgeeks.org/types-of-complexity-classes-p-np-conp-np-hard-and-np-complete/?ref=lbp 6/11
13/02/2024, 07:27 P, NP, CoNP, NP hard and NP complete | Complexity Classes - GeeksforGeeks

Complexity of different operations in Time and Space Complexity Analysis of


Binary tree, Binary Search Tree and AVL Binary Search Algorithm
tree

C program for Time Complexity plot of Prune-and-Search | A Complexity


Bubble, Insertion and Selection Sort Analysis Overview
using Gnuplot

Complete Tutorials
Learn Algorithms with Javascript | DSA DSA Crash Course | Revision Checklist
using JavaScript Tutorial with Interview Guide

Learn Data Structures and Algorithms | Mathematical and Geometric


DSA Tutorial Algorithms - Data Structure and
Algorithm Tutorials

Learn Data Structures with Javascript |


DSA using JavaScript Tutorial

umng01

Article Tags : Algo-Geek 2021 , Algorithms-NP Complete , NP Complete , time complexity ,


Algo Geek , Algorithms , Analysis of Algorithms , DSA
Practice Tags : Algorithms

Additional Information

We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy

https://www.geeksforgeeks.org/types-of-complexity-classes-p-np-conp-np-hard-and-np-complete/?ref=lbp 7/11
13/02/2024, 07:27 P, NP, CoNP, NP hard and NP complete | Complexity Classes - GeeksforGeeks

A-143, 9th Floor, Sovereign Corporate


Tower, Sector-136, Noida, Uttar Pradesh -
201305

Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Apply for Mentor Geeks Community

Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL Top 100 DSA Interview Problems
R Language DSA Roadmap by Sandeep Jain
Android Tutorial All Cheat Sheets
Tutorials Archive

Data Science & ML HTML & CSS


Data Science With Python HTML
Data Science For Beginner CSS
Machine Learning Tutorial Web Templates
We use cookies to ensure you
ML have
Mathsthe best browsing experience on our website. By using
CSS Frameworks
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Data Visualisation Tutorial
Policy Bootstrap

https://www.geeksforgeeks.org/types-of-complexity-classes-p-np-conp-np-hard-and-np-complete/?ref=lbp 8/11
13/02/2024, 07:27 P, NP, CoNP, NP hard and NP complete | Complexity Classes - GeeksforGeeks

Pandas Tutorial Tailwind CSS


NumPy Tutorial SASS
NLP Tutorial LESS
Deep Learning Tutorial Web Design

Python Computer Science


Python Programming Examples GATE CS Notes
Django Tutorial Operating Systems
Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Python Tutorial Digital Logic Design
Python Interview Question Engineering Maths

DevOps Competitive Programming


Git Top DS or Algo for CP
AWS Top 50 Tree
Docker Top 50 Graph
Kubernetes Top 50 Array
Azure Top 50 String
GCP Top 50 DP
DevOps Roadmap Top 15 Websites for CP

System Design JavaScript


High Level Design JavaScript Examples
Low Level Design TypeScript
UML Diagrams ReactJS
Interview Guide NextJS
Design Patterns AngularJS
OOAD NodeJS
System Design Bootcamp Lodash
Interview Questions Web Browser

NCERT Solutions School Subjects


Class 12 Mathematics
Class
We use cookies to ensure you have11the best browsing experience on our website. By using Physics
our site, you acknowledge that
Classyou
10have read and understood our Cookie Policy & PrivacyChemistry
Policy

https://www.geeksforgeeks.org/types-of-complexity-classes-p-np-conp-np-hard-and-np-complete/?ref=lbp 9/11
13/02/2024, 07:27 P, NP, CoNP, NP hard and NP complete | Complexity Classes - GeeksforGeeks

Class 9 Biology
Class 8 Social Science
Complete Study Material English Grammar

Commerce UPSC Study Material


Accountancy Polity Notes
Business Studies Geography Notes
Economics History Notes
Management Science and Technology Notes
HR Management Economy Notes
Finance Ethics Notes
Income Tax Previous Year Papers

SSC/ BANKING Colleges


SSC CGL Syllabus Indian Colleges Admission & Campus Experiences
SBI PO Syllabus List of Central Universities - In India
SBI Clerk Syllabus Colleges in Delhi University
IBPS PO Syllabus IIT Colleges
IBPS Clerk Syllabus NIT Colleges
SSC CGL Practice Papers IIIT Colleges

Companies Preparation Corner


META Owned Companies Company-Wise Recruitment Process
Alphabhet Owned Companies Resume Templates
TATA Group Owned Companies Aptitude Preparation
Reliance Owned Companies Puzzles
Fintech Companies Company-Wise Preparation
EdTech Companies

Exams More Tutorials


JEE Mains Software Development
JEE Advanced Software Testing
GATE CS Product Management
NEET SAP
UGC NET SEO - Search Engine Optimization
We use cookies to ensure you have the best browsing experience on our website. By using Linux
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Excel
Policy

https://www.geeksforgeeks.org/types-of-complexity-classes-p-np-conp-np-hard-and-np-complete/?ref=lbp 10/11
13/02/2024, 07:27 P, NP, CoNP, NP hard and NP complete | Complexity Classes - GeeksforGeeks

Free Online Tools Write & Earn


Typing Test Write an Article
Image Editor Improve an Article
Code Formatters Pick Topics to Write
Code Converters Share your Experiences
Currency Converter Internships
Random Number Generator
Random Password Generator

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Policy

https://www.geeksforgeeks.org/types-of-complexity-classes-p-np-conp-np-hard-and-np-complete/?ref=lbp 11/11

You might also like