ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Theory of intractable problems

The headers of include/ADS/intractable realize chapter 19 of the book, i.e., the ways of facing a problem that is NP-hard (see the theory of complexity, include/ADS/complexity): the pseudopolynomial algorithms, the approximation algorithms, the branch-&-bound, the heuristics, and the search of the fifteen puzzle. This page collects what the headers take for granted: the notation of the problems of optimization (see Definitions and notation) and, for each strategy, the property on which its guarantee rests and the argument behind its bound (from Pseudopolynomial algorithms to The fifteen puzzle). The pseudocode of each algorithm is instead on the page of its header, in the comment of its function, together with the section of the book, or the paper, that it follows.

Definitions and notation

A problem of optimization asks, for an instance \( x \), a feasible solution of least cost (or of largest value) among the set \( F( x ) \) of the feasible ones; \( x^* \) is an optimal solution and \( \mathit{OPT} = c( x^* ) \) its cost. When the decision version is NP-complete, no algorithm is known that finds \( x^* \) in polynomial time on every instance, and the strategies of the area give up one of the three: the polynomial time in the size (the pseudopolynomial algorithms and the branch-&-bound), the optimum (the approximations and the heuristics), or every instance (the special cases of the theory of complexity).

The symbols of the headers of the area
SymbolMeaning
\( c( x ) \), \( x^* \), \( \mathit{OPT} \)the cost of a feasible solution, an optimal solution and its cost
\( d \), \( \# \)the size of the instance and its largest integer
\( e \), \( \epsilon \)the relative error of an approximation, a constant of the algorithm or an input of it
\( \mathit{lb}( S ) \), \( \mathit{minCost} \)a lower bound of the costs of the solutions that extend the partial solution \( S \), and the cost of the best solution found so far
\( I( \pi ) \), \( I_k( \pi ) \)a neighborhood of the solution \( \pi \), and the circuits that share \( n - k \) edges with the circuit \( \pi \)
\( D \), \( p_i \), \( v_i \), \( C \)the distances of the salesman, and the profits, the volumes and the capacity of the knapsack

Pseudopolynomial algorithms

An algorithm is pseudopolynomial if its time is bounded by a polynomial in \( d \) and \( \# \) (section 19.1); since \( \# \) may be exponential in \( d \), such an algorithm is polynomial only on the instances whose numbers are small. For SUBSET SUM, with \( M[ i ][ j ] \) true if some of the first \( i \) elements sum to \( j \),

\[ M[ i ][ j ] = M[ i - 1 ][ j ] \ \text{or} \ ( j \ge A_i \ \text{and} \ M[ i - 1 ][ j - A_i ] ) , \tag{1} \]

since a subset of the first \( i \) elements either avoids the \( i \)-th or takes it once; subsetSumDP() of Pseudopolynomial.h fills (1) in \( O( n k ) \) time, with one row of \( k + 1 \) bits written from the right, so that \( M[ j - A_i ] \) is still the value of the row before, and subsetSumSet() keeps all the rows to read a subset backwards. The same table with three parts gives bottino(), whose \( S[ i ][ j ][ h ] \) has \( O( n m^2 ) \) entries, and the vectors of the loads give schedulingDP(), with at most \( ( d + 1 )^m \) vectors after each program.

A problem is strongly NP-complete if it stays NP-complete on the instances with \( \# \) bounded by a polynomial in \( d \) (CLIQUE, whose numbers are bounded by \( n \), TRAVELING SALESMAN, 3-PARTITION), and weakly otherwise (SUBSET SUM, PARTITION, KNAPSACK).

Theorem 1. A strongly NP-complete problem has no pseudopolynomial algorithm unless \( \mathrm{P} = \mathrm{NP} \) (Theorem 19.1 of the book).

Indeed, on the instances with \( \# \le q( d ) \), \( q \) a polynomial, the time of a pseudopolynomial algorithm is a polynomial in \( d \) and \( q( d ) \), i.e., in \( d \); such an algorithm would solve in polynomial time a restriction that is NP-complete.

When a program may be interrupted and resumed on another processor, the scheduling becomes easy: no schedule is shorter than

\[ d = \max\Big\{ \max_i t_i , \ \sum_i t_i / m \Big\} , \tag{2} \]

since each program runs for its time and the \( m \) processors do at most \( m d \) work, and the rule of McNaughton (mcNaughton()) reaches (2) by filling the processors one after the other up to \( d \). A program split between the processors \( q \) and \( q + 1 \) runs on \( q \) at the end and on \( q + 1 \) at the start, and the two pieces do not overlap in time because \( t_i \le d \). With \( t_i = C_i / T_i \) and \( d = 1 \), the same rule schedules a period of the periodic tasks of realTimeScheduling(), when \( \sum_i C_i / T_i \le m \).

Approximation algorithms

An algorithm is an approximation with relative error \( e \) if it gives a feasible \( x' \) with

\[ \frac{| c( x' ) - \mathit{OPT} |}{\mathit{OPT}} \le e \tag{3} \]

on every instance, and an approximation scheme if it takes \( \epsilon > 0 \) in input and satisfies (3) with \( e = \epsilon \), in a time polynomial in \( d \) for each fixed \( \epsilon \) (fully polynomial if also in \( 1 / \epsilon \)). The guarantees of the area rest on a lower bound of \( \mathit{OPT} \) that the algorithm can compare with its own cost.

The approximations of the area (section 19.2 of the book and the exercises 19.6 and 19.8)
ProblemFunctionGuaranteeTime
BIN PACKINGfirstFit()\( N < 2 N^* \)\( O( n^2 ) \)
TRAVELING SALESMAN, triangulardeltaTsp()\( c( x' ) \le 2\, \mathit{OPT} \)\( O( n^2 \log n ) \)
SUBSET SUMapSubsetSum()\( e < 1 / ( h + 1 ) \)\( O( h n^{h+1} ) \)
VERTEX COVERvertexCoverApprox()\( |S| \le 2\, \mathit{OPT} \)\( O( n^2 ) \)
KNAPSACKknapsackApprox()\( c( x' ) \ge ( 1 - \epsilon ) \mathit{OPT} \)\( O( n^3 / \epsilon ) \)

For BIN PACKING, the first fit never leaves two boxes whose contents sum to at most \( k \), since the objects of the later one would have fitted in the earlier one. Summing the contents of the \( N \ge 2 \) pairs of consecutive boxes (the last with the first) counts each box twice, so that twice the total volume \( V \) exceeds \( N k \); since \( N^* \ge V / k \), one has \( N < 2 V / k \le 2 N^* \), and the decreasing order of ffd improves the constant (to \( 11 / 9 \) asymptotically, by Johnson et al.). For the triangular salesman, a minimum spanning tree \( T \) weighs less than any circuit, which is a spanning path plus an edge, and the walk that goes twice along each edge of \( T \) costs \( 2 w( T ) \); deltaTsp() takes the cities in the order of their first visit, i.e., in preorder, and each shortcut over the cities already visited costs no more than the stretch it replaces, by the triangle inequality. Without the triangle inequality there is no such constant.

Theorem 2. For no constant \( s \ge 1 \) does TRAVELING SALESMAN have a polynomial algorithm with \( c( x' ) \le s\, \mathit{OPT} \), unless \( \mathrm{P} = \mathrm{NP} \) (Theorem 19.2 of the book).

Indeed, with the distances of tspGap(), 1 on the edges of \( G \) and \( s n \) elsewhere, a Hamiltonian circuit of \( G \) is a tour of cost \( n \), while any other tour costs more than \( s n \); an algorithm within \( s \) would therefore return a tour of cost at most \( s n \) exactly when \( G \) has a Hamiltonian circuit, and decide HAMILTONIAN CIRCUIT in polynomial time.

For SUBSET SUM, let \( S^* \) be optimal, of sum \( z^* \le k \). If \( |S^*| \le h \), apSubsetSum() tries \( S^* \) itself; otherwise it tries the \( h \) largest elements of \( S^* \), and the greedy completion on the elements in decreasing order either takes all of \( S^* \), or leaves out an element \( a \) of \( S^* \), not larger than those \( h \), with \( z + a > k \ge z^* \). Since \( ( h + 1 ) a \le z^* \), the sum found has

\[ z > z^* - a \ge z^* \Big( 1 - \frac{1}{h + 1} \Big) , \tag{4} \]

and the \( O( n^h ) \) sets of at most \( h \) elements, each completed in \( O( n ) \), give the time. For VERTEX COVER the edges taken by vertexCoverApprox() share no end, i.e., form a matching \( M \); a cover has an end of each edge of \( M \), hence \( \mathit{OPT} \ge |M| \), while the cover returned has \( 2 |M| \) nodes. For KNAPSACK, the profits rounded down to multiples of \( K = \epsilon p_{\max} / n \) lose less than \( K \) per object, i.e., less than \( n K = \epsilon p_{\max} \) on any set; the set optimal for the rounded profits is therefore within \( \epsilon p_{\max} \le \epsilon\, \mathit{OPT} \) of the optimum, since each object fits alone. The table of knapsackApprox() has a column per rounded profit, at most \( n \cdot n / \epsilon \) of them, and a row per object, whence \( O( n^3 / \epsilon ) \), a fully polynomial scheme (that of Ibarra and Kim).

Branch-&-bound

A branch-&-bound visits the tree of the partial solutions of the enumeration of chapter 16 and skips a subtree when a lower bound of the costs in it is not below the best cost found (section 19.3).

Lemma 3 (the bound). If \( \mathit{lb}( S ) \le c( x ) \) for each complete solution \( x \) that extends \( S \), then skipping \( S \) when

\[ \mathit{lb}( S ) \ge \mathit{minCost} \tag{5} \]

loses no solution better than the best found, and the visit returns an optimum.

The guarantee holds whatever the order of visit and the bound, and the time in the worst case stays that of the enumeration; what changes is the number of nodes in practice, which bbTsp(), littleTsp(), maxWeightClique() and the knapsacks report in *generated. The bounds of the area satisfy the hypothesis of Lemma 3 as follows. In the bound of the text (TspBound::BOOK of tspLowerBound()), a circuit that starts with the path \( S \) enters each city out of \( S \) once and leaves it once, and goes back to \( S[ 0 ] \) from a city out of \( S \); the least cost of entering and leaving each city, \( D_h \), counts each edge of the rest of the circuit at most twice, and half the sum, with \( A \) and \( B \), is a lower bound of that rest. In the bound of the 1-tree (TspBound::ONE_TREE), the rest of the circuit is a path through the cities out of \( S \) plus two edges, and a path is a spanning tree; the weight of a minimum spanning tree, plus the least edges \( A \) and \( B \), is therefore a lower bound (Held and Karp). In the reduction of littleTsp(), subtracting a constant from a row or a column of the matrix lowers the cost of every circuit by that constant, since a circuit uses exactly one entry of each row and of each column; the sum of the constants subtracted is a lower bound, and the entries 0 left are the edges that a circuit of that cost would use. littleTsp() visits by least bound, so that the first complete circuit extracted is optimal: any other node has a bound at least as large. maxWeightClique() bounds a set by its weight plus that of all its candidates, and knapsackBB() by the fractional knapsack of the objects left, which is at least the profit of any set of them within the capacity left, since the fractional greedy by \( p_i / v_i \) is optimal for the relaxation (chapter 14).

Heuristics

A heuristic gives a feasible solution with no guarantee on its cost (section 19.4); it is judged by its time and by its results in practice. The constructive heuristics of the area are greedy. greedyTsp() keeps the lightest edges that leave every city with at most two of them and close no cycle, as kruskal() does with an MFSET, and so ends with a Hamiltonian chain, closed by the edge of its two ends; the sort of the \( n ( n - 1 ) / 2 \) edges gives \( O( n^2 \log n ) \). greedySuperstring() is the same greedy on the oriented edges weighed by the overlaps, which leaves oriented paths. minMaxGreedy() grows the two halves of a bisection in turn, each time with the node out of them that has the fewest edges to the other half; the structure Buckets keeps the nodes by their count of edges to each half, with each node at a known place of its bucket and the least and largest bucket not empty, so that a node moves to the next bucket in \( O( 1 ) \), and the scans of the bounds, which only move forward between two moves back, cost \( O( 1 ) \) amortized. dsatur() colors first the node that sees the most colors, which is the most constrained.

A local search starts from a solution \( \pi \) and moves to a better solution of its neighborhood \( I( \pi ) \) while there is one; it ends at a local optimum, since the cost decreases strictly at each step and the solutions are finitely many. For the salesman, the neighborhood \( I_2( \pi ) \) of twoOpt() holds the circuits obtained by removing two edges of \( \pi \) that do not touch and reconnecting the two paths in the only other way, i.e., by reversing the stretch between them, which gives

\[ | I_2( \pi ) | = \frac{n ( n - 1 )}{2} - n , \tag{6} \]

the pairs of edges less the \( n \) that touch; the change of cost of a move depends on the four edges alone, and a step costs \( O( n^2 ) \). kOptNeighbors() counts \( | I_k( \pi ) | \) by brute force, which checks (6) for \( k = 2 \). partitionLocalSearch() does the same for PARTITION, with the moves of one element and the swaps of two, each changing the difference of the sums by twice the elements moved.

The fifteen puzzle

solvePuzzle() of FifteenPuzzle.h visits the configurations by least \( f( x ) = g( x ) + h( x ) \), \( g( x ) \) the moves from the start and \( h( x ) \) the sum of the distances of the tiles from their places (manhattan()); this is the branch-&-bound of section 19.5 with the visit by least bound, i.e., the algorithm A* of Hart, Nilsson and Raphael.

Lemma 4 (A* with a consistent bound). If \( h \) is 0 at the goal and

\[ h( x ) \le 1 + h( y ) \quad \text{for each move from } x \text{ to } y , \tag{7} \]

then the first goal extracted from the queue is reached in the fewest moves.

Indeed, a move changes the place of one tile by one square, and so changes \( h \) by at most 1, which is (7); then \( f \) never decreases along a path, and \( h \) never exceeds the moves left. When a goal \( x \) is extracted, every node of a shorter solution would have \( f \le g^* < g( x ) = f( x ) \) and would have been extracted before it, with its successors. The map of the least level of each configuration lets a configuration enter the queue again only with fewer moves, and an entry with more moves than the map is stale and skipped; hence each configuration is expanded once. puzzleBFS() is the case \( h = 0 \), which examines far more configurations, and puzzleDFS() the backtracking of chapter 16 with a bound on the moves, which finds a solution within the bound if there is one, not necessarily the shortest.

Half of the configurations cannot reach the goal. A move of the hole along a row leaves the order of the tiles, read by rows, unchanged, and a move along a column moves one tile past \( n - 1 \) others, which changes the number of inversions by an amount of the parity of \( n - 1 \); with \( n \) odd the parity of the inversions is therefore invariant, and with \( n \) even that of the inversions plus the row of the hole is. solvable() compares that parity with the goal (Johnson and Story), which is also sufficient.

References

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 19, https://algoritmicamente.it/materiali/libro/capitoli/cap19.pdf
M. R. Garey, D. S. Johnson, "Strong" NP-completeness results: motivation, examples, and implications, Journal of the ACM 25(3), 499-508, 1978, doi:10.1145/322077.322090
R. McNaughton, Scheduling with deadlines and loss functions, Management Science 6(1), 1-12, 1959, doi:10.1287/mnsc.6.1.1
D. S. Johnson, A. Demers, J. D. Ullman, M. R. Garey, R. L. Graham, Worst-case performance bounds for simple one-dimensional packing algorithms, SIAM Journal on Computing 3(4), 299-325, 1974, doi:10.1137/0203025
D. J. Rosenkrantz, R. E. Stearns, P. M. Lewis, An analysis of several heuristics for the traveling salesman problem, SIAM Journal on Computing 6(3), 563-581, 1977, doi:10.1137/0206041
O. H. Ibarra, C. E. Kim, Fast approximation algorithms for the knapsack and sum of subset problems, Journal of the ACM 22(4), 463-468, 1975, doi:10.1145/321906.321909
J. D. C. Little, K. G. Murty, D. W. Sweeney, C. Karel, An algorithm for the traveling salesman problem, Operations Research 11(6), 972-989, 1963, doi:10.1287/opre.11.6.972
M. Held, R. M. Karp, The traveling-salesman problem and minimum spanning trees: part II, Mathematical Programming 1, 6-25, 1971, doi:10.1007/BF01584070
S. Lin, Computer solutions of the traveling salesman problem, Bell System Technical Journal 44(10), 2245-2269, 1965, doi:10.1002/j.1538-7305.1965.tb04146.x
P. E. Hart, N. J. Nilsson, B. Raphael, A formal basis for the heuristic determination of minimum cost paths, IEEE Transactions on Systems Science and Cybernetics 4(2), 100-107, 1968, doi:10.1109/TSSC.1968.300136
W. W. Johnson, W. E. Story, Notes on the "15" puzzle, American Journal of Mathematics 2(4), 397-404, 1879, doi:10.2307/2369492