What is NP?

Stephen M. Walker II · Co-Founder / CEO

What is NP?

NP (Nondeterministic Polynomial time) is a complexity class in computational theory. It contains decision problems for which a given solution can be verified as correct or incorrect in polynomial time by a deterministic Turing machine. In simpler terms, if you're given a "yes" answer to a problem, you can check that it's correct relatively quickly. But finding that "yes" answer might take a very long time. This contrasts with P (Polynomial time) problems, where solutions can be both found and verified in polynomial time. It's a crucial concept in understanding the P vs NP problem, one of the most fundamental unsolved questions in computer science.

What is the relationship between NP and other complexity classes?

The complexity class NP (Non-deterministic Polynomial Time) is related to several other complexity classes, including P, Co-NP, NP-hard, and NP-complete.

  • P Class — The P class stands for Polynomial Time. It is the collection of decision problems that can be solved by a deterministic machine in polynomial time. If a problem is solvable in polynomial time, then a solution is also verifiable in polynomial time by simply solving the problem. Hence, P is a subset of NP.

  • Co-NP Class — Co-NP stands for the complement of NP Class. If a problem X is in NP, then its complement X’ is also in CoNP. Whether or not NP = co-NP is an outstanding question in complexity theory.

  • NP-hard Class — An NP-hard problem is at least as hard as the hardest problem in NP: every problem in NP can be reduced to it in polynomial time. NP-hard problems are not required to be in NP — some are (the NP-complete ones) and some aren't (for example, the Halting Problem, which isn't even decidable, let alone verifiable in polynomial time).

  • 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. If one could solve an NP-complete problem in polynomial time, then one could also solve any NP problem in polynomial time.

The relationship between NP and other complexity classes is an active area of research. Many NP-complete problems are believed to be not solvable in polynomial time by any deterministic Turing machine, but this has not been proven for all NP-complete problems. The question of whether P equals NP, known as the P vs NP problem, is one of the most significant open questions in computer science.

What are some NP-complete problems?

NP-complete problems are decision problems for which a solution can be verified in polynomial time, and if any one of them can be solved in polynomial time, all problems in NP can be solved in polynomial time. Well-known examples include:

  • Boolean satisfiability (SAT) — deciding whether a Boolean expression can be made true by some assignment of its variables. SAT was the first problem proven NP-complete (the Cook-Levin theorem), and it's the basis for proving other problems NP-complete via reduction.
  • Graph coloring — deciding whether a graph's vertices can be colored with a given number of colors so that no two adjacent vertices share a color.
  • Vertex cover — deciding whether a graph has a set of at most k vertices that touches every edge.
  • Hamiltonian cycle — deciding whether a graph contains a cycle that visits every vertex exactly once.
  • Subset sum — deciding whether some subset of a given set of integers sums to a target value.

While solutions to NP-complete problems can be verified "quickly", there is no known way to find a solution quickly. The time required to solve the problem using any currently known algorithm increases rapidly as the size of the problem grows.

What are some NP-hard problems?

NP-hard problems are computational problems that are at least as hard as the hardest problems in NP (nondeterministic polynomial time). If an NP-hard problem can be solved in polynomial time, then every problem in NP can be solved in polynomial time. Some NP-hard problems are also in NP (the NP-complete ones, listed above); others aren't, because they're optimization problems rather than decision problems, or because verifying a proposed solution isn't itself fast. Examples include:

  1. Traveling Salesman Problem (optimization version) — given a list of cities and the distances between each pair, find the shortest possible route that visits each city exactly once and returns to the start. (The decision version — "is there a route shorter than k?" — is NP-complete.)

  2. Knapsack Problem (optimization version) — given a set of items, each with a weight and a value, choose which items to include so that total weight stays within a limit while total value is maximized.

  3. Halting Problem — given a description of a program and an input, decide whether the program eventually halts or runs forever. This problem is undecidable, so it's NP-hard but not in NP.

  4. Bin Packing Problem (optimization version) — given a set of items with different sizes, pack them into the fewest possible fixed-capacity bins.

Note: While all NP-complete problems are NP-hard, not all NP-hard problems are NP-complete. An NP-hard problem is NP-complete if it is also in NP, meaning that a solution can be verified in polynomial time.

What are some heuristics for solving NP-hard problems?

Heuristics are often used to solve NP-hard problems, as they can provide approximate solutions in a reasonable amount of time, even though they may not always find the optimal solution. Here are some common heuristics used for NP-hard problems:

  1. Greedy Algorithms — These algorithms make the locally optimal choice at each decision point with the hope that these local decisions lead to a globally optimal solution. They are simple, easy to implement, and often provide good approximations for many problems.

  2. Exhaustive Search — This approach involves checking all possible solutions to find the best one. While this can guarantee finding the optimal solution, it is often impractical for large problems due to its high time complexity.

  3. Metaheuristic Algorithms — These are higher-level heuristics designed to select and modify other heuristics, with the goal of improving their performance. Examples include Genetic Algorithms, Simulated Annealing, and Tabu Search. These methods are often used when the search space is large and complex.

  4. Problem-specific Heuristics — These are heuristics designed specifically for a particular problem. For example, the Lin-Kernighan heuristic (LKH) is a well-known heuristic for the Traveling Salesman Problem.

  5. Heuristic Algorithms for Scheduling Problems — These are algorithms designed to solve scheduling problems, which are a common class of NP-hard problems. They often involve assigning tasks to resources over time.

It's important to note that the effectiveness of a heuristic can vary depending on the specific problem and instance. Therefore, it's often necessary to experiment with different heuristics to find the one that works best for a particular problem.

What is the difference between np-hard and np-complete problems?

The main difference between NP-hard and NP-complete problems lies in their definitions and the types of problems they encompass.

  1. NP-hard Problems — A problem is NP-hard if every problem in NP can be reduced to it in polynomial time. This means that an NP-hard problem is at least as hard as the hardest problems in NP. However, NP-hard problems do not necessarily have to be in NP, and they do not have to be decision problems — they could be optimization problems too. Examples include the Halting problem and the optimization version of the Traveling Salesman Problem.

  2. NP-complete Problems — A problem is NP-complete if it is both in NP and NP-hard. This means that not only can every problem in NP be reduced to it in polynomial time, but a solution to the problem can also be verified in polynomial time. NP-complete problems are always decision problems. Examples include determining whether a graph has a Hamiltonian cycle and determining whether a Boolean formula is satisfiable.

All NP-complete problems are NP-hard, but not all NP-hard problems are NP-complete. The distinction is that NP-complete problems are a subset of NP problems (i.e., their solutions can be verified in polynomial time), while NP-hard problems may or may not be in NP.

More terms

Continue exploring the glossary.

Learn how teams define, measure, and improve LLM systems.

Glossary term

What Are Multi-Task Learning Models in AI?

Multi-Task Learning Models in AI are designed to handle multiple learning tasks simultaneously, leveraging commonalities and differences across tasks to improve the performance of all tasks. They are used in various domains like natural language processing, computer vision, and speech recognition.
Read term

August 21, 2026

AI Jailbreak Red-Teaming & Vulnerability Probing

AI jailbreak red-teaming is the systematic adversarial testing of LLM applications to uncover security vulnerabilities, jailbreak vectors, prompt injections, and safety policy violations.
Read term

It's time to build

Collaborate with your team on reliable Generative AI features.
Want expert guidance? Book a 1:1 onboarding session from your dashboard.

Talk to sales