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

The headers of include/ADS/complexity realize chapter 18 of the book and the polynomial cases of its conclusion, i.e., the problems of the class NP with their certificates and their enumerations, the simulation of a nondeterministic algorithm, the polynomial reductions among the problems, and the polynomial algorithms of a few problems close to them. This page collects what the headers take for granted: the definitions of the classes P and NP and the notation (see Definitions and notation), the nondeterministic algorithms and the cost of their simulation (see Nondeterminism), the size of the trees that the enumerations visit (see Certificates and enumerations), the reductions and the chain of NP-complete problems that the library builds (see Reductions and NP-completeness), and the arguments behind the polynomial cases (see Polynomial cases). 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; the strategies for the problems that are hard are the subject of the theory of intractable problems (include/ADS/intractable).

Definitions and notation

A decision problem \( \Pi \) is a set of instances, each with the answer YES or NO, and \( Y_\Pi \) is the set of the instances whose answer is YES; the problems of optimization (the largest clique, the shortest tour) become decision problems by a bound \( k \) on the value (whether a clique of \( k \) nodes exists), which is as hard up to a polynomial factor, since a search on \( k \) finds the optimum with a logarithmic number of questions. The size \( |x| \) of an instance is the length of a reasonable encoding of it, with the numbers written in binary, so that a number \( k \) counts \( \Theta( \log k ) \) and not \( k \) (the distinction on which the pseudopolynomial algorithms rest). The class P holds the problems decided by an algorithm in time \( O( |x|^c ) \) for a constant \( c \); the class NP those that have a polynomial certificate, i.e., a polynomial \( p \) and an algorithm \( V \) of polynomial time such that

\[ x \in Y_\Pi \iff \exists\, y : \ |y| \le p( |x| ) \ \text{and} \ V( x , y ) = \text{YES} . \tag{1} \]

The functions cliqueCertificate(), tspCertificate(), satCertificate() and legalTiling(), and the postconditions of the other enumerations of Problems.h, are such algorithms \( V \), each with a certificate of polynomial size (a set of nodes, a tour, an assignment, a covering); hence every problem of the area is in NP, and \( \mathrm{P} \subseteq \mathrm{NP} \), since a polynomial algorithm is a checker that ignores \( y \). Whether the inclusion is strict is open.

The symbols of the headers of the area
SymbolMeaning
\( x \), \( |x| \)an instance and its size, the numbers in binary
\( y \), \( V( x , y ) \)a certificate and its checker, of polynomial size and time
\( f \), \( A \propto B \)a reduction, of polynomial time, with \( x \in Y_A \iff f( x ) \in Y_B \), and the relation it proves
\( G \), \( n \), \( m \)a graph as an adjacency matrix (Adjacency), its nodes and its edges; its diagonal is ignored
\( F \), \( \pm( v + 1 ) \)a formula in conjunctive normal form (Cnf), and the literal of the variable \( v \) or of its negation
\( k \)the bound of a decision problem: the size of a clique or of a cover, the length of a tour, the colors, the profit
\( D \), \( d = D[ 0 ] \), \( u_{hij} \)the tiles of the tiling, the tile of the corner, and the variable or node of the tile \( h \) in the square \( ( i , j ) \) (tileVar())

Nondeterminism

A nondeterministic algorithm may call choice( k ), which returns one of \( k \) alternatives, and ends with success or failure; it answers YES on \( x \) if some sequence of choices leads to success, and its time is that of the longest sequence of choices (section 18.2). An algorithm of this kind that runs in polynomial time is a certificate in disguise: the sequence of the choices that leads to success is a \( y \) of (1), and conversely the algorithm that chooses the bits of \( y \) one by one and then runs \( V( x , y ) \) succeeds if and only if (1) holds. Hence NP is also the class of the problems decided in polynomial time by a nondeterministic algorithm, whence its name.

Lemma 1 (the simulation). A nondeterministic algorithm that makes at most \( h \) choices of at most \( k \) alternatives each, in time \( t \), is simulated by a deterministic one in time

\[ O( k^h \, t ) . \tag{2} \]

Indeed, the sequences of choices are the paths from the root of a tree of height at most \( h \) whose nodes have at most \( k \) children, and the tree has at most \( k^h \) leaves. Chooser::run() of Nondeterminism.h replays the algorithm once per leaf, in lexicographic order, each run costing \( O( t ) \): after a failure it drops the last choices that have no alternative left and advances the one before them, which is the visit in depth of the tree with a backtrack on the last choice still open (section 18.3). With \( h \) and \( t \) polynomial in \( |x| \), (2) is \( 2^{O( |x|^c )} \); hence \( \mathrm{NP} \subseteq \mathrm{EXP} \), and no simulation essentially faster than the visit of the tree is known for an NP-complete problem.

Certificates and enumerations

The enumerations of Problems.h and squareTiling() of Domino.h are (2) specialized to each problem: a backtrack builds the certificate one piece at a time and checks each partial certificate, so that a subtree whose root already fails the check is skipped. The tree of the choices is that of the subsets, of the permutations or of the functions of the problem, and the table gives its size, i.e., the time in the worst case up to the polynomial cost of a node.

The enumerations and the trees of their choices
ProblemFunctionCertificateLeaves of the tree
CLIQUE, INDEPENDENT SETclique(), independentSet()\( k \) nodes\( \binom{n}{k} \)
VERTEX COVERvertexCover()at most \( k \) nodes\( \sum_{s \le k} \binom{n}{s} \)
GRAPH COLORABILITYcoloring()a color per node\( k^n \)
TRAVELING SALESMAN, the Hamiltonian problemstsp(), hamiltonianCircuit(), ...a permutation\( ( n - 1 )! \) or \( n! \)
SATISFIABILITY, 0-1 INTEGER PROGRAMMINGsatisfiable(), lp01()a 0-1 vector\( 2^n \)
SUBSET SUM, PARTITION, KNAPSACKsubsetSumExists(), partition(), knapsackDecision()a subset\( 2^n \)
MULTIPROCESSOR SCHEDULINGmultiprocessorScheduling()a processor per program\( m^n \)
EXACT COVER BY 3-SETS, 3-DIMENSIONAL MATCHINGexactCover3(), threeDimensionalMatching()a subset of the sets\( 2^{|Y|} \)
BOUNDED TILINGsquareTiling()a tile per square\( m^{n^2 - 1} \)

The pruning of the code changes the time in practice, never the bound: a partial tour longer than \( k \), a partial volume above the capacity or a sum above the target is not extended, and the symmetries are broken by fixing the city 0 at the start of the tours, the node 0 in the first half of a bisection, the colors in the order of their first use, and by trying a program on one empty processor only. A clause of satisfiable() is checked as soon as its largest variable is set, which cuts the tree at the first clause made false. Note that the same problems may have polynomial cases: the longest path of an acyclic graph (longestPathDag()) is computed by one scan of the nodes in topological order, since a path never comes back to a node already scanned, in \( O( n^2 ) \) on the matrix, while on a graph with cycles the longest simple path is NP-hard (see Reductions and NP-completeness).

Reductions and NP-completeness

A polynomial reduction of \( A \) to \( B \) is a function \( f \), computable in polynomial time, such that

\[ x \in Y_A \iff f( x ) \in Y_B , \tag{3} \]

written \( A \propto B \). Since \( |f( x )| \) is at most polynomial in \( |x| \) (an algorithm of polynomial time writes no more), a polynomial algorithm for \( B \) composed with \( f \) is a polynomial algorithm for \( A \), and two reductions compose into one; hence \( \propto \) is transitive, and \( A \propto B \) with \( B \in \mathrm{P} \) gives \( A \in \mathrm{P} \). A problem is NP-hard if every problem of NP reduces to it, and NP-complete if it is also in NP; one NP-complete problem in P would give \( \mathrm{P} = \mathrm{NP} \), and by transitivity a problem of NP to which a known NP-complete problem reduces is NP-complete in its turn.

Theorem 2 (Cook and Levin). SATISFIABILITY is NP-complete.

The proof writes, for a nondeterministic algorithm of polynomial time and an input \( x \), a formula of polynomial size whose variables describe the contents of the memory at each step and whose clauses force each step to follow from the one before it; the formula is satisfiable if and only if a sequence of choices leads to success. The book starts the chain instead from BOUNDED TILING, whose rows of tiles can encode the successive configurations of a computation in the same way (the tiles of Wang), and reduces it to the other problems. The reductions of Reductions.h and of the functions that delegate to one another realize the chain below, each in the time needed to write its output.

The reductions realized (sections 18.5 and 18.7 of the book)
FromToWhereFunction
BOUNDED TILINGCLIQUETheorem 18.2dominoToClique()
BOUNDED TILINGSATISFIABILITYTheorem 18.3dominoToSat()
BOUNDED TILING0-1 INTEGER PROGRAMMINGTheorem 18.4dominoToLp01()
CLIQUEINDEPENDENT SETTheorem 18.5complement()
INDEPENDENT SETVERTEX COVERexercise 18.11the complement of the set, \( k' = n - k \)
HAMILTONIAN CIRCUITTRAVELING SALESMANTheorem 18.6hamiltonToTsp()
PARTITIONMULTIPROCESSOR SCHEDULING, SUBSET SUM, KNAPSACKTheorems 18.7 to 18.9halfSum()
DIRECTED HAMILTONIAN CIRCUITDIRECTED HAMILTONIAN PATHexercise 18.13cycleToPath()
DIRECTED HAMILTONIAN CIRCUITHAMILTONIAN PATHexercise 18.14cycleToChain()
DIRECTED HAMILTONIAN PATHLONGEST PATHexercise 18.15\( k = n - 1 \)
HAMILTONIAN PATHDEGREE-CONSTRAINED SPANNING TREEexercise 18.16\( k = 2 \)
3-DIMENSIONAL MATCHINGEXACT COVER BY 3-SETSchapter 18threeDimensionalMatching()

Each reduction rests on a correspondence between the certificates of the two problems. In dominoToClique() the nodes of one square are never joined, so that a clique of \( n^2 \) nodes takes exactly one tile per square, and two of its tiles in adjacent squares are joined only if they fit; hence the cliques of \( n^2 \) nodes are the legal coverings, and the graph has \( m n^2 \) nodes. In complement() a set is independent in \( G \) if and only if it is a clique of the complement, and \( S \) is independent if and only if \( V \setminus S \) covers the edges, since an edge left uncovered by \( V \setminus S \) has both ends in \( S \). In hamiltonToTsp() a tour has \( n \) edges of length at least 1, and its length is \( n \) if and only if all its edges are edges of \( G \). In halfSum() a set of numbers of sum \( s / 2 \) leaves the other half to the second processor, or to the objects left out of the knapsack, whose profits and volumes are the numbers themselves. The cost of each reduction is that of writing its output, e.g., \( \Theta( m^2 n^4 ) \) for the adjacency matrix of dominoToClique().

The same tool proves lower bounds within P. Sorting reduces to the convex hull (sortToHull()): the points \( ( a_i , a_i^2 ) \) lie on a parabola, all of them are vertices of the hull, and the hull lists them in the order of the \( a_i \); hence a hull in \( o( n \log n ) \) comparisons would sort in \( o( n \log n ) \), against the lower bound of the sorting by comparisons, and the convex hull costs \( \Omega( n \log n ) \) (section 18.5).

Polynomial cases

The square tiling is NP-complete, while the tiling of a strip \( 1 \times n \) is in P (section 18.5): a strip is a walk of \( n - 1 \) edges from \( d \) in the graph \( H \) whose nodes are the \( m \) tiles and whose edges \( ( d_u , d_v ) \) join a tile to those that fit at its right.

Lemma 3 (the linear tiling). A strip of \( n \) tiles from \( d \) exists if and only if

\[ \text{a cycle of } H \text{ is reachable from } d \quad \text{or} \quad L( d ) \ge n - 1 , \tag{4} \]

\( L( d ) \) the edges of the longest path from \( d \).

Indeed, a walk may go round a reachable cycle as many times as it needs, and so has any length; without a reachable cycle every walk from \( d \) is a path, and its length is at most \( L( d ) \). linearTiling() decides (4) by one visit in depth of \( H \) from \( d \), which finds a reachable cycle as a back edge (see the theory of graphs) and computes \( L \) in the postvisit, in \( O( m^2 ) \) on the implicit matrix of \( H \), whatever \( n \) is; \( n \) enters the input with \( \log n \) bits, and an enumeration of the strips would take time exponential in it.

The conclusion of the book pairs several NP-complete problems with a polynomial one that differs from it in a detail, and EasyCases.h realizes the polynomial halves that the other areas do not.

The polynomial halves of the pairs (table 19.1 of the book)
NP-completePolynomialWhere
SATISFIABILITY, three literals per clausetwo literals per clausetwoSat()
3-COLORABILITY2-COLORABILITYtwoColoring()
3-DIMENSIONAL MATCHING2-DIMENSIONAL MATCHINGbipartiteMatching()
CLIQUECLIQUE on an interval graphintervalClique()
BOUNDED TILINGthe linear tilinglinearTiling() of Domino.h
HAMILTONIAN CIRCUITthe Eulerian cycleeulerianCycle() of EulerianCycle.h
KNAPSACKthe fractional knapsackfractionalKnapsack()
DEGREE-CONSTRAINED SPANNING TREEthe minimum spanning treekruskal()
LONGEST PATHthe shortest pathshortestPaths()

Theorem 4 (Aspvall, Plass and Tarjan). A formula of clauses of at most two literals is satisfiable if and only if no variable \( v \) lies in the strongly connected component of \( \neg v \) in the graph of the implications, which has the edges \( ( \neg a , b ) \) and \( ( \neg b , a ) \) for each clause \( a \vee b \).

In fact, a path of implications from \( v \) to \( \neg v \) and one back would force \( v \) both true and false. Conversely, the assignment that makes \( v \) true when the component of \( v \) comes after that of \( \neg v \) in a topological order of the components, as twoSat() does, never sets a literal true with a false one among its consequences, since the graph is symmetric under negation; hence it satisfies each clause, in \( O( n + m ) \) by the algorithm of Kosaraju. Similarly, a graph is 2-colorable if and only if it has no odd cycle, which the visit in breadth of twoColoring() finds as an edge between two nodes of the same color; a bipartite matching is a maximum flow with unit capacities (see the theory of graphs); and on intervals of the line a set that meets pairwise has a common point (the property of Helly in one dimension, the latest left end lying before the earliest right end), so that the largest clique is the largest number of intervals through one point, which a sweep of the sorted ends counts in \( O( n \log n ) \).

References

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 18 and the conclusion, https://algoritmicamente.it/materiali/libro/capitoli/cap18.pdf
S. A. Cook, The complexity of theorem-proving procedures, Proceedings of the Third Annual ACM Symposium on Theory of Computing, 151-158, 1971, doi:10.1145/800157.805047
L. A. Levin, Universal sequential search problems, Problems of Information Transmission 9(3), 265-266, 1973
R. M. Karp, Reducibility among combinatorial problems, Complexity of Computer Computations, Plenum Press, 85-103, 1972, doi:10.1007/978-1-4684-2001-2_9
M. R. Garey, D. S. Johnson, Computers and Intractability: A Guide to the Theory of NP-Completeness, W. H. Freeman, 1979
H. Wang, Proving theorems by pattern recognition II, Bell System Technical Journal 40(1), 1-41, 1961, doi:10.1002/j.1538-7305.1961.tb03975.x
B. Aspvall, M. F. Plass, R. E. Tarjan, A linear-time algorithm for testing the truth of certain quantified boolean formulas, Information Processing Letters 8(3), 121-123, 1979, doi:10.1016/0020-0190(79)90002-4