<p>If a problem is <strong>NP</strong>-hard, under the assumption that <strong>P</strong> ≠ <strong>NP</strong> there is no algorithm that is</p>
<ul>
<li>deterministic,</li>
<li>exactly correct on all inputs all the time, and</li>
<li>efficient on all possible inputs.</li>
</ul>
<p>If you absolutely need all of the above guarantees, then you're pretty much out of luck. However, if you're willing to settle for a solution to the problem that relaxes some of these constraints, then there very well still might be hope! Here are a few options to consider.</p>
<h2>Option One: Approximation Algorithms</h2>
<p>If a problem is <strong>NP</strong>-hard and <strong>P</strong> ≠ <strong>NP</strong>, it means that there's is no algorithm that will always efficiently produce the exactly correct answer on all inputs. But what if you don't need the exact answer? What if you just need answers that are <em>close</em> to correct? In some cases, you may be able to combat <strong>NP</strong>-hardness by using an approximation algorithm.</p>
<p>For example, a canonical example of an <strong>NP</strong>-hard problem is the <a href="http://en.wikipedia.org/wiki/Travelling_salesman_problem" rel="noreferrer">traveling salesman problem</a>. In this problem, you're given as input a complete graph representing a transportation network. Each edge in the graph has an associated weight. The goal is to find a cycle that goes through every node in the graph exactly once and which has minimum total weight. In the case where the edge weights satisfy the <a href="http://en.wikipedia.org/wiki/Triangle_inequality" rel="noreferrer">triangle inequality</a> (that is, the best route from point A to point B is always to follow the direct link from A to B), then you can get back a cycle whose cost is at most 3/2 optimal by using the <a href="http://en.wikipedia.org/wiki/Christofides_algorithm" rel="noreferrer">Christofides algorithm</a>.</p>
<p>As another example, the <a href="http://en.wikipedia.org/wiki/Knapsack_problem" rel="noreferrer">0/1 knapsack problem</a> is known to be <strong>NP</strong>-hard. In this problem, you're given a bag and a collection of objects with different weights and values. The goal is to pack the maximum value of objects into the bag without exceeding the bag's weight limit. Even though computing an <em>exact</em> answer requires exponential time in the worst case, it's possible to approximate the correct answer to an arbitrary degree of precision in polynomial time. (The algorithm that does this is called a fully polynomial-time approximation scheme or <em><a href="http://en.wikipedia.org/wiki/Polynomial-time_approximation_scheme" rel="noreferrer">FPTAS</a></em>).</p>
<p>Unfortunately, we do have some theoretical limits on the approximability of certain <strong>NP</strong>-hard problems. The Christofides algorithm mentioned earlier gives a 3/2 approximation to TSP where the edges obey the triangle inequality, but interestingly enough it's possible to show that if <strong>P</strong> ≠ <strong>NP</strong>, there is no polynomial-time approximation algorithm for TSP that can get within <em>any</em> constant factor of optimal. Usually, you need to do some research to learn more about which problems can be well-approximated and which ones can't, since many <strong>NP</strong>-hard problems can be approximated well and many can't. There doesn't seem to be a unified theme.</p>
<h2>Option Two: Heuristics</h2>
<p>In many <strong>NP</strong>-hard problems, standard approaches like greedy algortihms won't always produce the right answer, but often do reasonably well on &quot;reasonable&quot; inputs. In many cases, it's reasonable to attack <strong>NP</strong>-hard problems with <em>heuristics</em>. The exact definition of a heuristic varies from context to context, but typically a heuristic is either an approach to a problem that &quot;often&quot; gives back good answers at the cost of sometimes giving back wrong answers, or is a useful rule of thumb that helps speed up searches even if it might not always guide the search the right way.</p>
<p>As an example of the first type of heuristic, let's look at the <a href="http://en.wikipedia.org/wiki/Graph_coloring" rel="noreferrer">graph-coloring problem</a>. This <strong>NP</strong>-hard problem asks, given a graph, to find the minimum number of colors necessary to paint the nodes in the graph such that no edge's endpoints are the same color. This turns out to be a particularly tough problem to solve with many other approaches (the best known approximation algorithms have terrible bounds, and it's not suspected to have a parameterized efficient algorithm). However, there are many heuristics for graph coloring that do quite well in practice. Many <a href="http://en.wikipedia.org/wiki/Greedy_coloring" rel="noreferrer">greedy coloring heuristics</a> exist for assigning colors to nodes in a reasonable order, and these heuristics often do quite well in practice. Unfortunately, sometimes these heuristics give terrible answers back, but provided that the graph isn't pathologically constructed the heuristics often work just fine.</p>
<p>As an example of the second type of heuristic, it's helpful to look at SAT solvers. <a href="http://en.wikipedia.org/wiki/Boolean_satisfiability_problem" rel="noreferrer">SAT</a>, the Boolean satisfiability problem, was the first problem proven to be <strong>NP</strong>-hard. The problem asks, given a propositional formula (often written in <a href="http://en.wikipedia.org/wiki/Conjunctive_normal_form" rel="noreferrer">conjunctive normal form</a>), to determine whether there is a way to assign values to the variables such that the overall formula evaluates to true. <a href="http://www.satcompetition.org/" rel="noreferrer">Modern SAT solvers</a> are getting quite good at solving SAT in many cases by using heuristics to guide their search over possible variable assignments. One famous SAT-solving algorithm, <a href="http://en.wikipedia.org/wiki/DPLL_algorithm" rel="noreferrer">DPLL</a>, essentially tries all possible assignments to see if the formula is satisfiable, using heuristics to speed up the search. For example, if it finds that a variable is either always true or always false, DPLL will try assigning that variable its forced value before trying other variables. DPLL also finds unit clauses (clauses with just one literal) and sets those variables' values before trying other variables. The net effect of these heuristics is that DPLL ends up being very fast in practice, even though it's known to have exponential worst-case behavior.</p>
<h2>Option Three: Pseudopolynomial-Time Algorithms</h2>
<p>If <strong>P</strong> ≠ <strong>NP</strong>, then no <strong>NP</strong>-hard problem can be solved in polynomial time. However, in some cases, the definition of &quot;polynomial time&quot; doesn't necessarily match the standard intuition of polynomial time. Formally speaking, polynomial time means polynomial in the number of bits necessary to specify the input, which doesn't always sync up with what we consider the input to be.</p>
<p>As an example, consider the <a href="http://en.wikipedia.org/wiki/Partition_problem" rel="noreferrer">set partition problem</a>. In this problem, you're given a set of numbers and need to determine whether there's a way to split the set into two smaller sets, each of which has the same sum. The naive solution to this problem runs in time O(2<sup>n</sup>) and works by just brute-force testing all subsets. With dynamic programming, though, it's possible to solve this problem in time O(nN), where n is the number of elements in the set and N is the maximum value in the set. Technically speaking, the runtime O(nN) is not polynomial time because the numeric value N is written out in only log<sub>2</sub> N bits, but assuming that the numeric value of N isn't too large, this is a perfectly reasonable runtime.</p>
<p>This algorithm is called a <a href="http://en.wikipedia.org/wiki/Pseudo-polynomial_time" rel="noreferrer">pseudopolynomial-time algorithm</a> because the runtime O(nN) &quot;looks&quot; like a polynomial, but technically speaking is exponential in the size of the input. Many <strong>NP</strong>-hard problems, especially ones involving numeric values, admit pseudopolynomial-time algorithms and are therefore easy to solve assuming that the numeric values aren't too large.</p>
<p>For more information on pseudopolynomial time, check out <a href="https://stackoverflow.com/questions/19647658/what-is-pseudopolynomial-time-how-does-it-differ-from-polynomial-time">this earlier Stack Overflow question about pseudopolynomial time</a>.</p>
<h2>Option Four: Randomized Algorithms</h2>
<p>If a problem is <strong>NP</strong>-hard and <strong>P</strong> ≠ <strong>NP</strong>, then there is no <em>deterministic</em> algorithm that can solve that problem in worst-case polynomial time. But what happens if we allow for algorithms that introduce randomness? If we're willing to settle for an algorithm that gives a good answer <em>on expectation</em>, then we can often get relatively good answers to <strong>NP</strong>-hard problems in not much time.</p>
<p>As an example, consider the <a href="http://en.wikipedia.org/wiki/Maximum_cut" rel="noreferrer">maximum cut problem</a>. In this problem, you're given an undirected graph and want to find a way to split the nodes in the graph into two nonempty groups A and B with the maximum number of edges running between the groups. This has some interesting applications in computational physics (unfortunately, I don't understand them at all, but you can peruse <a href="http://theory.stanford.edu/%7Ejvondrak/data/maxcut.ps" rel="noreferrer">this paper</a> for some details about this). This problem is known to be <strong>NP</strong>-hard, but there's a simple randomized approximation algorithm for it. If you just toss each node into one of the two groups completely at random, you end up with a cut that, on expectation, is within 50% of the optimal solution.</p>
<p>Returning to SAT, many modern SAT solvers use some degree of randomness to guide the search for a satisfying assignment. The <a href="http://en.wikipedia.org/wiki/WalkSAT" rel="noreferrer">WalkSAT and GSAT</a> algorithms, for example, work by picking a random clause that isn't currently satisfied and trying to satisfy it by flipping some variable's truth value. This often guides the search toward a satisfying assignment, causing these algorithms to work well in practice.</p>
<p>It turns out there's a lot of open theoretical problems about the ability to solve <strong>NP</strong>-hard problems using randomized algorithms. If you're curious, check out the complexity class <strong><a href="http://en.wikipedia.org/wiki/BPP_%28complexity%29" rel="noreferrer">BPP</a></strong> and the open problem of its relation to <strong>NP</strong>.</p>
<h2>Option Five: Parameterized Algorithms</h2>
<p>Some <strong>NP</strong>-hard problems take in multiple different inputs. For example, the <a href="http://en.wikipedia.org/wiki/Longest_path_problem" rel="noreferrer">long path problem</a> takes as input a graph and a length k, then asks whether there's a simple path of length k in the graph. The <a href="http://en.wikipedia.org/wiki/Subset_sum_problem" rel="noreferrer">subset sum problem</a> takes in as input a set of numbers and a target number k, then asks whether there's a subset of the numbers that dds up to exactly k.</p>
<p>Interestingly, in the case of the long path problem, there's an algorithm (the <a href="http://en.wikipedia.org/wiki/Color-coding" rel="noreferrer">color-coding algorithm</a>) whose runtime is O((n<sup>3</sup> log n) · b<sup>k</sup>), where n is the number of nodes, k is the length of the requested path, and b is some constant. This runtime is exponential in k, but is only polynomial in n, the number of nodes. This means that if k is fixed and known in advance, the runtime of the algorithm as a function of the number of nodes is only O(n<sup>3</sup> log n), which is quite a nice polynomial. Similarly, in the case of the subset sum problem, there's a dynamic programming algorithm whose runtime is O(nW), where n is the number of elements of the set and W is the maximum weight of those elements. If W is fixed in advance as some constant, then this algorithm will run in time O(n), meaning that it will be possible to exactly solve subset sum in linear time.</p>
<p>Both of these algorithms are examples of <a href="http://en.wikipedia.org/wiki/Parameterized_complexity#FPT" rel="noreferrer">parameterized algorithms</a>, algorithms for solving <strong>NP</strong>-hard problems that split the hardness of the problem into two pieces - a &quot;hard&quot; piece that depends on some input parameter to the problem, and an &quot;easy&quot; piece that scales gracefully with the size of the input. These algorithms can be useful for finding exact solutions to <strong>NP</strong>-hard problems when the parameter in question is small. The color-coding algorithm mentioned above, for example, has proven quite useful in practice in computational biology.</p>
<p>However, some problems are conjectured to not have any nice parameterized algorithms. Graph coloring, for example, is suspected to not have any efficient parameterized algorithms. In the cases where parameterized algorithms exist, they're often quite efficient, but you can't rely on them for all problems.</p>
<p>For more information on parameterized algorithms, check out <a href="https://stackoverflow.com/questions/19643939/what-is-fixed-parameter-tractability-why-is-it-useful">this earlier Stack Overflow question</a>.</p>
<h2>Option Six: Fast Exponential-Time Algorithms</h2>
<p>Exponential-time algorithms don't scale well - their runtimes approach the lifetime of the universe for inputs as small as 100 or 200 elements.</p>
<p>What if you need to solve an <strong>NP</strong>-hard problem, but you know the input is reasonably small - say, perhaps its size is somewhere between 50 and 70. Standard exponential-time algorithms are probably not going to be fast enough to solve these problems. What if you really do need an exact solution to the problem and the other approaches here won't cut it?</p>
<p>In some cases, there are &quot;optimized&quot; exponential-time algorithms for <strong>NP</strong>-hard problems. These are algorithms whose runtime is exponential, but not as bad an exponential as the naive solution. For example, a simple exponential-time algorithm for the 3-coloring problem (given a graph, determine if you can color the nodes one of three colors each so that no edge's endpoints are the same color) might work checking each possible way of coloring the nodes in the graph, testing if any of them are 3-colorings. There are 3<sup>n</sup> possible ways to do this, so in the worst case the runtime of this algorithm will be O(3<sup>n</sup> · poly(n)) for some small polynomial poly(n). However, using more clever tricks and techniques, it's possible to develop an algorithm for 3-colorability that runs in time <a href="http://arxiv.org/pdf/cs/0006046v1.pdf" rel="noreferrer">O(1.3289<sup>n</sup>)</a>. This is still an exponential-time algorithm, but it's a much faster exponential-time algorithm. For example, 3<sup>19</sup> is about 10<sup>9</sup>, so if a computer can do one billion operations per second, it can use our initial brute-force algorithm to (roughly speaking) solve 3-colorability in graphs with up to 19 nodes in one second. Using the O((1.3289<sup>n</sup>)-time exponential algorithm, we could solve instances of up to about 73 nodes in about a second. That's a huge improvement - we've grown the size we can handle in one second by more than a factor of three!</p>
<p>As another famous example, consider the traveling salesman problem. There's an obvious O(n! · poly(n))-time solution to TSP that works by enumerating all permutations of the nodes and testing the paths resulting from those permutations. However, by using a dynamic programming algorithm similar to that used by the color-coding algorithm, it's possible to improve the runtime to &quot;only&quot; O(n<sup>2</sup> 2<sup>n</sup>). Given that 13! is about one billion, the naive solution would let you solve TSP for 13-node graphs in roughly a second. For comparison, the DP solution lets you solve TSP on 28-node graphs in about one second.</p>
<p>These fast exponential-time algorithms are often useful for boosting the size of the inputs that can be exactly solved in practice. Of course, they still run in exponential time, so these approaches are typically not useful for solving very large problem instances.</p>
<h2>Option Seven: Solve an Easy Special Case</h2>
<p>Many problems that are <strong>NP</strong>-hard in general have restricted special cases that are known to be solvable efficiently. For example, while in general it’s <strong>NP</strong>-hard to determine <a href="http://en.wikipedia.org/wiki/Graph_coloring" rel="noreferrer">whether a graph has a <em>k</em>-coloring</a>, in the specific case of <em>k</em> = 2 this is equivalent to checking whether a graph is bipartite, which can be checked in linear time using a modified depth-first search. Boolean satisfiability is, generally speaking, <strong>NP</strong>-hard, but it can be solved in polynomial time if you have an input formula with at most two literals per clause, or where the formula is formed from clauses using XOR rather than inclusive-OR, etc. Finding the largest independent set in a graph is generally speaking <strong>NP</strong>-hard, but if the graph is bipartite this can be done efficiently due to König’s theorem.</p>
<p>As a result, if you find yourself needing to solve what might initially appear to be an <strong>NP</strong>-hard problem, first check whether the inputs you actually need to solve that problem on have some additional restricted structure. If so, you might be able to find an algorithm that applies to your special case and runs much faster than a solver for the problem in its full generality.</p>
<h2>Conclusion</h2>
<p>If you need to solve an <strong>NP</strong>-hard problem, don't despair! There are lots of great options available that might make your intractable problem a lot more approachable. No one of the above techniques works in all cases, but by using some combination of these approaches, it's usually possible to make progress even when confronted with <strong>NP</strong>-hardness.</p>
<p>Hope this helps!</p>
I need to solve an NP-hard problem. Is there hope?
<p>There are a lot of real-world problems that turn out to be <a href="https://en.wikipedia.org/wiki/NP-hard" rel="noreferrer"><strong>NP</strong>-hard</a>. If we assume that <strong>P</strong> &ne; <strong>NP</strong>, there aren't any polynomial-time algorithms for these problems.</p>

<p>If you have to solve one of these problems, is there any hope that you'll be able to do so efficiently? Or are you just out of luck?</p>