NP-hard: What is the definition of NP-hardness?

Stephen M. Walker II · Co-Founder / CEO

What is the definition of NP-hardness?

In computational complexity theory, NP-hardness (non-deterministic polynomial-time hardness) is the defining property of a class of problems that are informally "at least as hard as the hardest problems in NP". NP stands for non-deterministic polynomial time, which is a class of problems for which a solution can be verified in polynomial time.

A problem H is considered NP-hard when every problem L in NP can be reduced to H by a polynomial-time reduction. This means that any instance of L can be transformed, in polynomial time, into an instance of H such that solving H also solves L. If a polynomial-time algorithm could be found to solve any NP-hard problem, that reduction would provide polynomial-time algorithms for all problems in NP. However, it is suspected that P (the class of problems that can be solved in polynomial time) is not equal to NP, making it unlikely that such an algorithm exists. It's important to note that NP-hard problems do not have to be elements of NP; indeed, they may not even be decidable.

Examples of NP-hard problems include the subset sum problem, the travelling salesman problem, and the knapsack problem. These problems are difficult to solve because they involve a large number of potential solutions that must be checked in order to find the best one. This can be a time-consuming process, especially when the problem is large and complex.

The concept of NP-hardness is closely related to NP-complete problems. A problem is NP-complete if it is in NP and all problems in NP can be reduced to it in polynomial time. All NP-complete problems are NP-hard, but not all NP-hard problems are NP-complete.

What are some examples of NP-hard problems?

NP-hard problems are a class of problems in computational complexity theory that are "at least as hard as the hardest problems in NP". Here are some examples of NP-hard problems:

  1. Travelling Salesman Problem — This problem involves finding the shortest possible route that allows a salesman to visit each city once and return to the origin city. It is NP-hard because it requires checking all possible routes to find the shortest one.

  2. Subset Sum Problem — Given a set of integers and a target value, the subset sum problem asks whether there is a subset whose sum equals the target. This problem is NP-hard because it involves checking all possible subsets of the given set.

  3. Vertex Cover Problem — This problem involves finding the smallest set of vertices that cover all edges in a graph. It is NP-hard because it requires checking all possible combinations of vertices.

  4. Hamiltonian Cycle Problem — This problem involves finding a cycle in a given graph that visits each vertex exactly once and returns to the origin vertex. It is NP-hard because it requires checking all possible cycles in the graph.

  5. Knapsack Problem — Given a set of items, each with a weight and a value, the knapsack problem involves determining the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It is NP-hard because it involves checking all possible combinations of items.

  6. Clique Problem — This problem involves finding the largest clique (a subset of vertices that form a complete graph) in a given graph. It is NP-hard because it requires checking all possible subsets of vertices.

Remember, while all NP-complete problems are NP-hard, not all NP-hard problems are NP-complete. NP-complete problems are those that are both in NP and NP-hard.

Why are NP-hard problems difficult to solve?

NP-hard problems are difficult to solve due to their inherent complexity. In computational complexity theory, a problem is considered NP-hard if every problem in NP (non-deterministic polynomial time) 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.

The difficulty in solving NP-hard problems arises from the fact that they require super-polynomial time to solve, and no known algorithms can solve them in polynomial time. This is significant because polynomial time algorithms are considered efficient, while super-polynomial and exponential time algorithms are considered inefficient due to their high computational cost for large inputs.

Moreover, if a polynomial time algorithm could be found to solve any NP-hard problem, it would imply that there are polynomial time algorithms for all problems in NP, effectively proving that P=NP. However, it is widely believed in the computer science community that P≠NP, meaning that there are problems in NP that cannot be solved in polynomial time.

Examples of NP-hard problems include the traveling salesman problem and the subset sum problem. These problems are notoriously difficult to solve, and even the best algorithms can take a very long time to find a solution.

It's important to note that while all NP-complete problems are NP-hard, not all NP-hard problems are NP-complete. NP-complete problems are a subset of NP-hard problems that are both in NP and NP-hard. This means that they can be verified in polynomial time, but it's unknown whether they can be solved in polynomial time.

The difficulty in solving NP-hard problems lies in their computational complexity and the lack of efficient (polynomial time) algorithms to solve them. This makes them a significant area of study in computer science, particularly in fields like optimization and artificial intelligence.

What are some methods for solving NP-hard problems?

NP-hard problems are a class of problems that are "at least as hard as the hardest problems in NP". While it is suspected that there are no polynomial-time algorithms for NP-hard problems, this has not been proven. However, there are several methods to approach solving these problems:

  1. Approximation Algorithms — These are algorithms that find near-optimal solutions with a guarantee on the proximity to the optimal solution. They are particularly useful when exact solutions are not feasible due to time or resource constraints.

  2. Heuristics — These are rule-of-thumb strategies or methods that help in making the problem-solving process quicker or more manageable. They do not guarantee the best solution but often produce good solutions within a reasonable time frame. Heuristics are particularly useful when dealing with very complex problems where finding the optimal solution is too resource-intensive.

  3. Pseudopolynomial-Time Algorithms — These are algorithms that have a running time that is polynomial in the numeric value of the input, but not polynomial in the length of the input (which is the case for polynomial-time algorithms). They are not truly efficient, but they can be practical for small inputs.

  4. Randomized Algorithms — These are algorithms that employ a degree of randomness as part of their logic. The algorithm typically uses uniformly random bits as an auxiliary input to guide its behavior, in the hope of achieving good performance in the "average case" over all possible choices of random bits.

  5. Exact Algorithms — These are algorithms that always produce the optimal solution, but they may require non-polynomial time. Even a faster exact algorithm that does not run in polynomial time can mean that in practice much larger instances can be solved.

  6. Parameterized Algorithms — These are algorithms that depend on certain parameters of the input. They are designed to solve problems more efficiently as the parameter value is small, even if the size of the problem (input size) is large.

It's important to note that the choice of method depends on the specific problem and the resources available. In many real-world applications, it's often more practical to find a "good enough" solution rather than the absolute optimal solution, especially when dealing with large, complex problems.

Are there any NP-hard problems that have been solved?

No general polynomial-time algorithm is known for any NP-hard problem, and none is expected to exist unless P = NP. NP-hard problems are a set of problems to which all problems in NP can be reduced in polynomial time. If a polynomial-time solution existed for even one NP-hard problem, it would imply a polynomial-time solution for every problem in NP, meaning P = NP. The P vs NP question remains open — it is one of the seven "Millennium Prize Problems," for which the Clay Mathematics Institute offers a $1 million prize for a correct proof.

That said, "solved" can mean different things in practice:

  • Specific instances can be solved exactly. The partition problem, which is NP-hard in general, has a pseudo-polynomial-time solution via dynamic programming. Boolean satisfiability (SAT) is NP-hard in general, but restricted instances such as Horn-SAT can be solved in linear time, and small or structured instances of problems like the traveling salesman problem can be solved exactly with exact algorithms (e.g., branch and bound).
  • Approximate solutions can be found efficiently. Many NP-hard problems, including the traveling salesman problem, have approximation algorithms or heuristics that produce near-optimal solutions in practical time, even though they don't guarantee the true optimum.

So while no known algorithm solves NP-hard problems exactly and efficiently in the general case, specific instances are routinely solved exactly, and good approximate solutions are routinely found for practical use.

What is the difference between NP-hard and NP-complete?

"NP-incomplete" is not standard complexity-theory terminology — the correct comparison is between NP-hard and NP-complete, two related but distinct classes of problems that help us understand the difficulty of solving certain problems.

An NP-hard problem is a problem that is "at least as hard as the hardest problems in NP". A problem H is considered NP-hard if every problem L in NP can be reduced in polynomial time to H. This means that if we can solve H, we can use its solution to solve L in polynomial time. It's important to note that NP-hard problems do not have to be in NP, and they may not even be decidable. NP-hard problems are not necessarily decision problems, which are problems with a yes or no answer.

On the other hand, an NP-complete problem is a problem that is both in NP and NP-hard. This means that an NP-complete problem is a decision problem that is as hard as the hardest problems in NP, and a solution to it can be verified in polynomial time. If a polynomial time algorithm could be found to solve any NP-complete problem, it would mean that P (the class of problems that can be solved in polynomial time) is equal to NP, which is a major open question in computer science.

The main differences between NP-hard and NP-complete problems are:

  • NP-hard problems are at least as hard as the hardest problems in NP, while NP-complete problems are the hardest problems in NP.
  • NP-hard problems do not have to be in NP, while NP-complete problems must be in NP.
  • NP-hard problems are not necessarily decision problems, while NP-complete problems are always decision problems.

More terms

Continue exploring the glossary.

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

Glossary term

What is predicate logic?

Predicate logic, also known as first-order logic or quantified logic, is a formal language used to express propositions in terms of predicates, variables, and quantifiers. It extends propositional logic by replacing propositional letters with a more complex notion of proposition involving predicates and quantifiers.
Read term

Glossary term

What is a capsule neural network?

A capsule neural network groups neurons into capsules and uses routing to model part-whole relationships and pose information in data.
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