|
ADS
Algorithms and Data Structures
|
The problems in NP of chapter 18, each with its polynomial certificate, which checks a proposed solution, and an enumeration by backtrack, which decides the problem in exponential time and returns a solution if there is one. More...
Files | |
| file | ADS/complexity/Problems.h |
| The problems in NP of chapter 18, each with its polynomial certificate, which checks a proposed solution, and an enumeration by backtrack, which decides the problem in exponential time and returns a solution if there is one. | |
Functions | |
| bool | ADS::cliqueCertificate (const Adjacency &G, const Nodes &S, int k) |
| whether \( S \), of distinct nodes, has \( k \) of them or more, all adjacent (the certificatoCricca() of the book) | |
| std::optional< Nodes > | ADS::clique (const Adjacency &G, int k) |
| \( k \) nodes all adjacent, or nothing (the enCricca() of the book) | |
| std::optional< Nodes > | ADS::independentSet (const Adjacency &G, int k) |
| \( k \) nodes no two adjacent, or nothing | |
| std::optional< Nodes > | ADS::vertexCover (const Adjacency &G, int k) |
| at most \( k \) nodes that touch every edge, or nothing | |
| std::optional< std::vector< int > > | ADS::coloring (const Adjacency &G, int k) |
| a coloring of the nodes with at most \( k \) colors, adjacent nodes of different colors, by backtrack, or nothing (exercise 18.6) | |
| std::optional< Nodes > | ADS::bisection (const Adjacency &G, int k) |
| a partition of the nodes in two halves cut by at most \( k \) edges, or nothing; the half of node 0 | |
| template<class T> | |
| bool | ADS::tspCertificate (const std::vector< std::vector< T > > &D, T k, const Nodes &P) |
| whether the tour \( P \), a permutation, is at most \( k \) long; the empty tour of no city is 0 long (the certificatoTsp() of the book) | |
| template<class T> | |
| std::optional< Nodes > | ADS::tsp (const std::vector< std::vector< T > > &D, T k) |
| a tour of length at most \( k \) from city 0, the partial tours longer than \( k \) pruned, or nothing (the enTsp() of the book) | |
| std::optional< Nodes > | ADS::hamiltonianCircuit (const Adjacency &G) |
| an unoriented circuit through every node once (\( n \ge 3 \)), or nothing | |
| std::optional< Nodes > | ADS::hamiltonianCycle (const Adjacency &G) |
| an oriented cycle through every node once (\( n \ge 2 \)), or nothing | |
| std::optional< Nodes > | ADS::hamiltonianPath (const Adjacency &G, int r=-1, int t=-1) |
| an oriented path through every node once, from \( r \) and to \( t \) if given | |
| std::optional< Nodes > | ADS::hamiltonianChain (const Adjacency &G) |
| an unoriented chain through every node once | |
| std::optional< Nodes > | ADS::longestPath (const Adjacency &G, int r, int t, int k) |
| a simple path from \( r \) to \( t \) of at least \( k \) edges, by backtrack | |
| int | ADS::longestPathDag (const Adjacency &G, int r, int t) |
| the most edges of a path from \( r \) to \( t \) in an acyclic oriented graph, \( -1 \) if \( t \) is not reachable; \( O( n^2 ) \) by the topological order | |
| std::optional< std::vector< std::pair< int, int > > > | ADS::boundedSpanningTree (const Adjacency &G, int k) |
| a spanning tree of the unoriented graph with at most \( k \) edges at each node, as \( n - 1 \) edges, by enumeration, or nothing | |
| bool | ADS::satCertificate (const Cnf &F, const std::vector< bool > &x) |
| whether \( x \) satisfies every clause of \( F \) | |
| std::optional< std::vector< bool > > | ADS::satisfiable (const Cnf &F, int n) |
| an assignment of the \( n \) variables that satisfies \( F \), by backtrack with a clause checked as soon as its variables are set, or nothing | |
| std::optional< std::vector< int > > | ADS::lp01 (const std::vector< std::vector< long > > &A, const std::vector< long > &b, int n) |
| a 0-1 vector \( x \) with \( A x \le b \), by enumeration, or nothing | |
| std::optional< Nodes > | ADS::subsetSumExists (const std::vector< long > &A, long k) |
| a set of the places of \( A \) of sum \( k \), by backtrack, or nothing | |
| std::optional< Nodes > | ADS::partition (const std::vector< long > &A) |
| the places of a half of \( A \) of half the sum, or nothing | |
| std::optional< Nodes > | ADS::knapsackDecision (const std::vector< long > &p, const std::vector< long > &v, long C, long k) |
| objects of volume at most \( C \) and profit at least \( k \) (the enZaino() of the book) | |
| std::optional< std::vector< int > > | ADS::multiprocessorScheduling (const std::vector< long > &t, int m, long d) |
| the processor of each program, so that \( m \) processors run them all within time \( d \), or nothing (also if \( d < 0 \), even with no programs, since an idle processor is loaded 0) | |
| std::optional< Nodes > | ADS::exactCover3 (const std::vector< std::array< int, 3 > > &Y, int m) |
| sets of three of \( \{ 0 , \ldots , m - 1 \} \) that partition it, by places, the ones that meet those chosen pruned, or nothing (the enCopertura() of the book) | |
| std::optional< Nodes > | ADS::threeDimensionalMatching (const std::vector< std::array< int, 3 > > &M, int q) |
| a matching of \( q \) triples \( ( w , x , v ) \) of \( M \) with distinct \( w \), \( x \) and \( v \), the three sets being \( [ 0 , q ) \), or nothing | |
| std::optional< std::vector< long > > | ADS::bribes (std::vector< long > M, const std::vector< bool > &ps) |
| the \( n \) bribes \( M \) to the \( n \) politicians, \( \mathit{ps}[ i ] \) if the \( i \)-th is of the party PS, so that the PS gets twice the party PDF; the bribe of each politician, or nothing (the mazzette() of the book) | |
The problems in NP of chapter 18, each with its polynomial certificate, which checks a proposed solution, and an enumeration by backtrack, which decides the problem in exponential time and returns a solution if there is one.
The definitions, the invariants and the costs of the area are in Theory of complexity.
a partition of the nodes in two halves cut by at most \( k \) edges, or nothing; the half of node 0
In pseudocode (chapter 18), the halves that hold the node 0, so that each partition is tried once:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{bisection}( G , k )\quad \triangleright\ 0 \in S \text{ breaks the symmetry of the two halves} \cr 2 & \quad \textbf{if}\ n \text{ is odd}\ \textbf{then return nil} \cr 3 & \quad \textbf{return}\ \text{the first } S \ni 0 \text{ of } n / 2 \text{ nodes crossed by at most } k \text{ edges, or } \textbf{nil} \end{array}} \]
|
inline |
a spanning tree of the unoriented graph with at most \( k \) edges at each node, as \( n - 1 \) edges, by enumeration, or nothing
In pseudocode (exercise 18.16), each edge \( E[ i ] \) taken and then left, in order, an edge that closes a cycle or exceeds a degree never taken:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{extend}( T , i )\quad \triangleright \ \text{the edges } E[ i ] , \ldots , E[ m ] \text{ to decide, from } ( \emptyset , 1 ) \cr 2 & \quad \textbf{if}\ |T| = n - 1\ \textbf{then return true}\quad \triangleright\ \text{a spanning tree of degree} \le k \cr 3 & \quad \textbf{if}\ n - 1 - |T| > m - i + 1 \ \textbf{then return false}\quad \triangleright \ \text{too few edges left, } i > m \text{ included} \cr 4 & \quad [ u , v ] \gets E[ i ] \cr 5 & \quad \textbf{if}\ u , v \text{ have degree } < k \text{ in } T \text{, are in two trees of } T \text{, and } \mathrm{extend}( T \cup \{ [ u , v ] \} , i + 1 )\ \textbf{then return true} \cr 6 & \quad \textbf{return}\ \mathrm{extend}( T , i + 1 ) \end{array}} \]
|
inline |
the \( n \) bribes \( M \) to the \( n \) politicians, \( \mathit{ps}[ i ] \) if the \( i \)-th is of the party PS, so that the PS gets twice the party PDF; the bribe of each politician, or nothing (the mazzette() of the book)
In pseudocode (exercises 18.1 and 18.4), the permutations of the bribes in lexicographic order:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{bribes}( M , \mathit{ps} )\quad \triangleright\ \text{by enumeration of the permutations} \cr 2 & \quad \textbf{return}\ \text{the first permutation } \pi \text{ of } M \text{ in lexicographic order with} \cr & \qquad \textstyle\sum_{i : \mathit{ps}[ i ]} \pi[ i ] = 2 \textstyle\sum_{i : \textbf{not}\ \mathit{ps}[ i ]} \pi[ i ] \text{, or } \textbf{nil} \end{array}} \]
\( k \) nodes all adjacent, or nothing (the enCricca() of the book)
In pseudocode (chapter 18), the sets of \( k \) nodes in increasing order, a set extended only by a node adjacent to all of it:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{clique}( G , k ) \cr 2 & \quad \textbf{return}\ \emptyset \text{ if } k \le 0 \text{, else } \mathrm{choose}( \emptyset , 0 ) \cr 3 & \textbf{function}\ \mathrm{choose}( S , i )\quad \triangleright\ S \text{ a clique of nodes before } i \cr 4 & \quad \textbf{if}\ |S| = k\ \textbf{then return}\ S \cr 5 & \quad \textbf{if}\ k - |S| > n - i\ \textbf{then return nil}\quad \triangleright\ \text{not enough nodes left} \cr 6 & \quad \textbf{foreach}\ v \in [ i , n ) \text{ adjacent to every node of } S \text{, in increasing order} \ \textbf{do} \cr 7 & \qquad R \gets \mathrm{choose}( S \cup \{ v \} , v + 1 ); \ \textbf{if}\ R \ne \textbf{nil}\ \textbf{then return}\ R \cr 8 & \quad \textbf{return nil} \end{array}} \]
whether \( S \), of distinct nodes, has \( k \) of them or more, all adjacent (the certificatoCricca() of the book)
In pseudocode (chapter 18), \( O( |S|^2 ) \) time:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{cliqueCertificate}( G , S , k )\quad \triangleright\ O( |S|^2 ) \cr 2 & \quad \textbf{return}\ k \le |S| \text{ and every two places } a < b \text{ of } S \text{ hold distinct nodes with } G[ S[ a ] ][ S[ b ] ] \end{array}} \]
|
inline |
a coloring of the nodes with at most \( k \) colors, adjacent nodes of different colors, by backtrack, or nothing (exercise 18.6)
In pseudocode (exercise 18.6), the colors numbered in the order of their first use, so that two colorings that differ by a renaming of the colors are not both tried:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{color}( u )\quad \triangleright\ c[ 0 .. u - 1 ] \text{ a proper coloring, from } u = 0 \cr 2 & \quad \textbf{if}\ u = n\ \textbf{then return true} \cr 3 & \quad \mathit{used} \gets \text{the number of the colors on } 0 , \ldots , u - 1 \text{, i.e., } 1 + \max_{v < u} c[ v ] \text{ or } 0\quad \triangleright\ \text{colors in order} \cr 4 & \quad \textbf{foreach}\ x \in [ 0 , \min\{ k , \mathit{used} + 1 \} ) \text{ on no neighbor } v < u \text{ of } u\ \textbf{do}\quad \triangleright\ \text{one new color at most} \cr 5 & \qquad c[ u ] \gets x;\ \textbf{if}\ \mathrm{color}( u + 1 ) \ \textbf{then return true} \cr 6 & \quad \textbf{return false} \end{array}} \]
|
inline |
sets of three of \( \{ 0 , \ldots , m - 1 \} \) that partition it, by places, the ones that meet those chosen pruned, or nothing (the enCopertura() of the book)
In pseudocode (exercise 18.7), each set \( Y[ j ] \) taken, if it meets none of those chosen, and then left:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{extend}( S , j , \mathit{left} )\quad \triangleright\ \mathit{left} \text{ the elements not covered, from } ( \emptyset , 0 , m ) \cr 2 & \quad \textbf{if}\ \mathit{left} = 0\ \textbf{then return true} \cr 3 & \quad \textbf{if}\ j = |Y|\ \textbf{then return false} \cr 4 & \quad \textbf{if}\ Y[ j ] \text{ has three distinct elements, none covered by } S \text{, and} \cr & \qquad \mathrm{extend}( S \cup \{ j \} , j + 1 , \mathit{left} - 3 ) \ \textbf{then return true} \cr 5 & \quad \textbf{return}\ \mathrm{extend}( S , j + 1 , \mathit{left} ) \end{array}} \]
an unoriented chain through every node once
In pseudocode (exercise 18.14):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{hamiltonianChain}( G )\quad \triangleright\ G \text{ unoriented: a symmetric matrix} \cr 2 & \quad \textbf{return}\ \mathrm{hamiltonianPath}( G , -1 , -1 ) \end{array}} \]
an unoriented circuit through every node once (\( n \ge 3 \)), or nothing
In pseudocode (chapter 18), the backtracking shared by the four Hamiltonian problems, here from \( P = ( 0 ) \), closed and with no end given:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{hamilton}( P )\quad \triangleright\ P \text{ a path of } G \cr 2 & \quad \textbf{if}\ |P| = n\ \textbf{then return}\ ( P \text{ ends at } \mathit{end} \text{, if given} ) \text{ and } ( \text{an edge closes it, if } \mathit{closed} ) \cr 3 & \quad \textbf{return}\ \text{whether } \mathrm{hamilton}( P \cdot ( v ) ) \text{ holds for a } v \notin P \text{ after the last node of } P \text{, by increasing } v \end{array}} \]
an oriented cycle through every node once (\( n \ge 2 \)), or nothing
In pseudocode (exercise 18.12), the backtracking of hamiltonianCircuit() on the oriented edges:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{hamiltonianCycle}( G )\quad \triangleright\ \text{a cycle passes by } 0 \cr 2 & \quad \textbf{return}\ \textbf{nil} \text{ if } n < 2 \text{, else } \mathrm{hamilton}( ( 0 ) ) \text{, closed and with no end} \end{array}} \]
an oriented path through every node once, from \( r \) and to \( t \) if given
In pseudocode (exercise 18.13), the backtracking of hamiltonianCircuit() from each possible start:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{hamiltonianPath}( G , r , t )\quad \triangleright\ r , t \text{ the ends, } -1 \text{ if free} \cr 2 & \quad \textbf{foreach}\ \text{start } s \text{, only } r \text{ if } r \ge 0\ \textbf{do}\ \textbf{if}\ \mathrm{hamilton}( ( s ) ) \text{, open and ending at } t \text{, finds } P \ \textbf{then return}\ P \cr 3 & \quad \textbf{return nil} \end{array}} \]
\( k \) nodes no two adjacent, or nothing
In pseudocode (chapter 18), the enumeration of clique() with the test of its line 8 negated:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{independentSet}( G , k ) \cr 2 & \quad \textbf{return}\ \text{as } \mathrm{clique}( G , k ) \text{, a node } v \text{ being added when it is adjacent to no node of } S \end{array}} \]
|
inline |
objects of volume at most \( C \) and profit at least \( k \) (the enZaino() of the book)
In pseudocode (exercise 18.5), each object taken and then left, a volume above \( C \) pruned:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{extend}( S , i , \mathit{tp} , \mathit{tv} )\quad \triangleright\ \text{the profit and the volume of } S \text{, from } ( \emptyset , 0 , 0 , 0 ) \cr 2 & \quad \textbf{if}\ C < \mathit{tv}\ \textbf{then return false}\quad \triangleright\ \text{too heavy} \cr 3 & \quad \textbf{if}\ k \le \mathit{tp}\ \textbf{then return true} \cr 4 & \quad \textbf{if}\ i = n\ \textbf{then return false} \cr 5 & \quad \textbf{return}\ \mathrm{extend}( S \cup \{ i \} , i + 1 , \mathit{tp} + p[ i ] , \mathit{tv} + v[ i ] )\ \textbf{or} \ \mathrm{extend}( S , i + 1 , \mathit{tp} , \mathit{tv} ) \end{array}} \]
a simple path from \( r \) to \( t \) of at least \( k \) edges, by backtrack
In pseudocode (exercise 18.15), a path that reaches \( t \) too short is not extended, since it could not come back to \( t \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{extend}( P )\quad \triangleright\ P \text{ a simple path from } r \text{, from } P = ( r ) \cr 2 & \quad \textbf{if}\ P \text{ ends at } t\ \textbf{then return}\ k \le |P| - 1\quad \triangleright\ \text{its edges} \cr 3 & \quad \textbf{return}\ \text{whether } \mathrm{extend}( P \cdot ( v ) ) \text{ holds for a } v \notin P \text{ after the last node of } P \end{array}} \]
|
inline |
the most edges of a path from \( r \) to \( t \) in an acyclic oriented graph, \( -1 \) if \( t \) is not reachable; \( O( n^2 ) \) by the topological order
In pseudocode (exercise 18.15), one scan in topological order, as dagLongestPaths() of Paths.h:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestPathDag}( G , r , t )\quad \triangleright\ L[ v ] \text{ the longest path from } r \text{, } -1 \text{ if none} \cr 2 & \quad L[ v ] \gets -1 \text{ for every node } v;\ L[ r ] \gets 0 \cr 3 & \quad \textbf{foreach}\ u \text{ in a topological order with } L[ u ] \ge 0\ \textbf{do} \cr 4 & \qquad L[ v ] \gets \max\{ L[ v ] , L[ u ] + 1 \} \text{ for each edge } ( u , v ) \cr 5 & \quad \textbf{return}\ L[ t ] \end{array}} \]
|
inline |
a 0-1 vector \( x \) with \( A x \le b \), by enumeration, or nothing
In pseudocode (chapter 18), the \( 2^n \) vectors in lexicographic order:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lp01}( A , b , n ) \cr 2 & \quad \textbf{return}\ \text{the first } x \in \{ 0 , 1 \}^n \text{ in lexicographic order with } A x \le b \text{, or } \textbf{nil}\quad \triangleright\ 2^n \text{ tries} \end{array}} \]
|
inline |
the processor of each program, so that \( m \) processors run them all within time \( d \), or nothing (also if \( d < 0 \), even with no programs, since an idle processor is loaded 0)
In pseudocode (chapter 18), the empty processors being all equal, a program is tried on the first of them only:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{assign}( i )\quad \triangleright \ \text{the jobs before } i \text{ placed, from } i = 0 \text{ and no load} \cr 2 & \quad \textbf{if}\ i = n\ \textbf{then return true} \cr 3 & \quad \textbf{foreach}\ \text{processor } q \text{, in order, with } \mathit{load}[ q ] + t[ i ] \le d\ \textbf{do} \cr 4 & \qquad ( \mathit{load}[ q ] , a[ i ] ) \gets ( \mathit{load}[ q ] + t[ i ] , q );\ \textbf{if}\ \mathrm{assign}( i + 1 ) \ \textbf{then return true} \cr 5 & \qquad \mathit{load}[ q ] \gets \mathit{load}[ q ] - t[ i ] \cr 6 & \qquad \textbf{if}\ \mathit{load}[ q ] = 0 \ \textbf{then return false}\quad \triangleright \ \text{the other empty processors are the same} \cr 7 & \quad \textbf{return false} \end{array}} \]
|
inline |
the places of a half of \( A \) of half the sum, or nothing
In pseudocode (chapter 18):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{partition}( A )\quad \triangleright \ \text{a subset of half the sum} \cr 2 & \quad s \gets \textstyle\sum_i A[ i ] \cr 3 & \quad \textbf{return}\ \textbf{nil} \text{ if } s \text{ is odd, else } \mathrm{subsetSumExists}( A , s / 2 ) \end{array}} \]
|
inline |
whether \( x \) satisfies every clause of \( F \)
In pseudocode (chapter 18), in time linear in the size of \( F \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{satCertificate}( F , x ) \cr 2 & \quad \textbf{return}\ \text{whether every clause of } F \text{ has a literal true under } x \end{array}} \]
|
inline |
an assignment of the \( n \) variables that satisfies \( F \), by backtrack with a clause checked as soon as its variables are set, or nothing
In pseudocode (chapter 18), \( \mathit{last}( c ) \) the largest variable of the clause \( c \), counted from 1:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{assign}( v )\quad \triangleright\ x[ 0 .. v - 1 ] \text{ fixed, from } v = 0 \cr 2 & \quad \textbf{if}\ \text{a clause whose last variable is } v \text{ is false under } x\ \textbf{then return false}\quad \triangleright\ \text{now fully fixed} \cr 3 & \quad \textbf{if}\ v = n\ \textbf{then return true} \cr 4 & \quad \textbf{foreach}\ b \in ( \textbf{false} , \textbf{true} ) \ \textbf{do}\ x[ v ] \gets b;\ \textbf{if}\ \mathrm{assign}( v + 1 )\ \textbf{then return true} \cr 5 & \quad \textbf{return false} \end{array}} \]
|
inline |
a set of the places of \( A \) of sum \( k \), by backtrack, or nothing
In pseudocode (chapter 18), each element taken and then left, a sum above \( k \) pruned since the numbers are not negative:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{extend}( S , i , s )\quad \triangleright \ s \text{ the sum of } S \text{, from } ( \emptyset , 0 , 0 ) \cr 2 & \quad \textbf{if}\ s = k\ \textbf{then return true} \cr 3 & \quad \textbf{if}\ k < s\ \textbf{or}\ i = n \ \textbf{then return false}\quad \triangleright \ \text{the items are not negative} \cr 4 & \quad \textbf{return}\ \mathrm{extend}( S \cup \{ i \} , i + 1 , s + A[ i ] )\ \textbf{or}\ \mathrm{extend}( S , i + 1 , s )\quad \triangleright\ \text{with } i \text{ first} \end{array}} \]
|
inline |
a matching of \( q \) triples \( ( w , x , v ) \) of \( M \) with distinct \( w \), \( x \) and \( v \), the three sets being \( [ 0 , q ) \), or nothing
In pseudocode (chapter 18), by the reduction to EXACT COVER BY 3-SETS, with the three coordinates made disjoint:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{threeDimensionalMatching}( M , q )\quad \triangleright\ \text{the three sets one after the other} \cr 2 & \quad \textbf{return}\ \mathrm{exactCover3}( ( \{ w , q + x , 2 q + v \} : ( w , x , v ) \in M ) , 3 q ) \end{array}} \]
| std::optional< Nodes > ADS::tsp | ( | const std::vector< std::vector< T > > & | D, |
| T | k ) |
a tour of length at most \( k \) from city 0, the partial tours longer than \( k \) pruned, or nothing (the enTsp() of the book)
In pseudocode (chapter 18), the permutations that start with the city 0:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{extend}( S , \ell )\quad \triangleright\ S \text{ a path from } 0 \text{ of length } \ell \text{, from } ( ( 0 ) , 0 ) \cr 2 & \quad \textbf{if}\ k < \ell\ \textbf{then return false}\quad \triangleright\ \text{the costs are not negative} \cr 3 & \quad \textbf{if}\ |S| = n\ \textbf{then return}\ \ell + D[ S[ n - 1 ] ][ 0 ] \le k \cr 4 & \quad \textbf{return}\ \text{whether } \mathrm{extend}( S \cdot ( c ) , \ell + D[ S[ |S| - 1 ] ][ c ] ) \text{ holds for a } c \notin S \text{, tried by increasing } c \end{array}} \]
| bool ADS::tspCertificate | ( | const std::vector< std::vector< T > > & | D, |
| T | k, | ||
| const Nodes & | P ) |
whether the tour \( P \), a permutation, is at most \( k \) long; the empty tour of no city is 0 long (the certificatoTsp() of the book)
In pseudocode (chapter 18):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{tspCertificate}( D , k , P ) \cr 2 & \quad \textbf{return}\ P \text{ is a permutation of the cities and the circuit } P[ 0 ] , \ldots , P[ n - 1 ] , P[ 0 ] \text{ costs } \le k \end{array}} \]
at most \( k \) nodes that touch every edge, or nothing
In pseudocode (chapter 18), the sets by increasing size:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{vertexCover}( G , k )\quad \triangleright \ \text{the smallest first} \cr 2 & \quad \textbf{foreach}\ S \subseteq V \text{, by size } 0 , 1 , \ldots , \min\{ k , n \} \text{ and then in lexicographic order} \ \textbf{do} \cr 3 & \qquad \textbf{if}\ \text{every edge has an end in } S \ \textbf{then return}\ S \cr 4 & \quad \textbf{return nil} \end{array}} \]