ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Reductions.h

The polynomial reductions of chapter 18. More...

Files

file  ADS/complexity/Reductions.h
 The polynomial reductions of chapter 18: a function \( f \) from the inputs of \( A \) to the inputs of \( B \), computable in polynomial time, such that \( x \) is a YES of \( A \) if and only if \( f( x ) \) is a YES of \( B \); then \( A \) is no harder than \( B \) (\( A \propto B \)).

Functions

int ADS::tileVar (int h, int i, int j, int n)
 the place \( ( h n + i ) n + j \) of the variable or node of the tile \( h \) in the square \( ( i , j ) \)
std::pair< Adjacency, int > ADS::dominoToClique (const std::vector< Tile > &D, int n)
 BOUNDED TILING to CLIQUE: the graph on the \( m n^2 \) places of tileVar(), the ones of \( ( 0 , 0 ) \) but \( u_{000} \) isolated, and \( k \).
Cnf ADS::dominoToSat (const std::vector< Tile > &D, int n)
 BOUNDED TILING to SATISFIABILITY, on the \( m n^2 \) variables of tileVar().
std::pair< std::vector< std::vector< long > >, std::vector< long > > ADS::dominoToLp01 (const std::vector< Tile > &D, int n)
 BOUNDED TILING to 0-1 INTEGER PROGRAMMING: \( A x \le b \) on the \( m n^2 \) variables of tileVar(), each equation as two inequalities.
Adjacency ADS::complement (const Adjacency &G)
 the complement graph: CLIQUE of size \( k \) to INDEPENDENT SET of size \( k \)
std::vector< std::vector< long > > ADS::hamiltonToTsp (const Adjacency &G)
 HAMILTONIAN CIRCUIT to TRAVELING SALESMAN: the distances, \( k = n \).
long ADS::halfSum (const std::vector< long > &A)
 half the sum, or \( -1 \) if the sum is odd (then no partition: the bound \( -1 \) has no YES in any of the three problems)
Adjacency ADS::cycleToPath (const Adjacency &G)
 DIRECTED HAMILTONIAN CIRCUIT to DIRECTED HAMILTONIAN PATH from \( n \) to 0: the node 0 split, a new node \( n \) with the edges out of 0; the loops of \( G \) dropped, as the problems of Problems.h ignore them.
Adjacency ADS::cycleToChain (const Adjacency &G)
 DIRECTED HAMILTONIAN CIRCUIT to HAMILTONIAN PATH: the path of cycleToPath(), each node \( v \) a chain \( v_{in} - v_{mid} - v_{out} \), each edge \( ( u , v ) \) an edge \( [ u_{out} , v_{in} ] \), and pendant nodes at the start and at the end.
std::vector< Point > ADS::sortToHull (const std::vector< long > &a)
 SORTING to CONVEX HULL: the points \( ( a , a^2 ) \).

Detailed description

The polynomial reductions of chapter 18.

The definitions, the invariants and the costs of the area are in Theory of complexity.

Function documentation

◆ complement()

Adjacency ADS::complement ( const Adjacency & G)
inline

the complement graph: CLIQUE of size \( k \) to INDEPENDENT SET of size \( k \)

In pseudocode (Theorem 18.5):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{complement}( G )\quad \triangleright \ \text{a clique of } G \text{ is an independent set of } C \cr 2 & \quad \textbf{return}\ C \text{ with } C[ u ][ v ] \text{ iff } u \ne v \text{ and not } G[ u ][ v ] \end{array}} \]

Precondition
\( G \) is square
Postcondition
the edges between distinct nodes are those that \( G \) lacks

◆ cycleToChain()

Adjacency ADS::cycleToChain ( const Adjacency & G)
inline

DIRECTED HAMILTONIAN CIRCUIT to HAMILTONIAN PATH: the path of cycleToPath(), each node \( v \) a chain \( v_{in} - v_{mid} - v_{out} \), each edge \( ( u , v ) \) an edge \( [ u_{out} , v_{in} ] \), and pendant nodes at the start and at the end.

In pseudocode (exercise 18.14), the chains keep the orientation, since a Hamiltonian path must cross each chain from \( v_{in} \) to \( v_{out} \), and the pendant nodes fix its two ends:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{cycleToChain}( G )\quad \triangleright \ \text{each node } v \text{ of the path becomes the chain } v_{\mathrm{in}} , v_{\mathrm{mid}} , v_{\mathrm{out}} \cr 2 & \quad P \gets \mathrm{cycleToPath}( G );\ N \gets n + 1 \cr 3 & \quad \text{join } v_{\mathrm{in}} \text{ to } v_{\mathrm{mid}} \text{ and } v_{\mathrm{mid}} \text{ to } v_{\mathrm{out}} \text{ for each } v < N \text{, and } u_{\mathrm{out}} \text{ to } v_{\mathrm{in}} \text{ for each edge } ( u , v ) \text{ of } P \cr 4 & \quad \text{join a new node to } n_{\mathrm{in}} \text{ and another to } 0_{\mathrm{out}}\quad \triangleright \ \text{they fix the two ends} \cr 5 & \quad \textbf{return}\ \text{the graph of the } 3 N + 2 \text{ nodes} \end{array}} \]

Precondition
\( G \) is square and not empty
Postcondition
an unoriented graph of \( 3 ( n + 1 ) + 2 \) nodes

◆ cycleToPath()

Adjacency ADS::cycleToPath ( const Adjacency & G)
inline

DIRECTED HAMILTONIAN CIRCUIT to DIRECTED HAMILTONIAN PATH from \( n \) to 0: the node 0 split, a new node \( n \) with the edges out of 0; the loops of \( G \) dropped, as the problems of Problems.h ignore them.

In pseudocode (exercise 18.13), a cycle through 0 becomes a path from the copy \( n \) of 0 to 0, and conversely:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{cycleToPath}( G )\quad \triangleright \ \text{the node } 0 \text{ split into a start } 0 \text{ and an end } n \cr 2 & \quad H \gets \text{the nodes } 0 , \ldots , n \text{ and no edge} \cr 3 & \quad \textbf{foreach}\ ( u , v ) \in E \text{ with } u \ne v \ \textbf{do}\ \text{insert } ( n , v ) \text{ in } H \text{ if } u = 0 \text{, else } ( u , v ) \cr 4 & \quad \textbf{return}\ H \end{array}} \]

Precondition
\( G \) is square and not empty (no node 0 to split otherwise)
Postcondition
a square graph of \( n + 1 \) nodes

◆ dominoToClique()

std::pair< Adjacency, int > ADS::dominoToClique ( const std::vector< Tile > & D,
int n )
inline

BOUNDED TILING to CLIQUE: the graph on the \( m n^2 \) places of tileVar(), the ones of \( ( 0 , 0 ) \) but \( u_{000} \) isolated, and \( k \).

In pseudocode (Theorem 18.2), a clique of \( n^2 \) nodes takes one tile per square, since two nodes of a square are never joined, and those tiles fit wherever two squares touch:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{dominoToClique}( D , n )\quad \triangleright\ u_{hij} \text{: the tile } h \text{ on the square } ( i , j ) \cr 2 & \quad G \gets \text{the nodes } u_{hij} \text{, the square } ( 0 , 0 ) \text{ with } u_{000} \text{ only, and no edge} \cr 3 & \quad \textbf{foreach}\ \text{two nodes } u_{hij} , u_{gab} \text{ on distinct squares}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ ( i , j ) , ( a , b ) \text{ are not adjacent, or the tiles } h , g \text{ match on their common side}\ \textbf{then}\ \text{join them} \cr 5 & \quad \textbf{return}\ ( G , n^2 )\quad \triangleright \ \text{a clique of } n^2 \text{ nodes is a tiling} \end{array}} \]

Precondition
\( n \ge 1 \) and \( D \) is not empty
Postcondition
an unoriented graph of \( m n^2 \) nodes, and \( k = n^2 \)

◆ dominoToLp01()

std::pair< std::vector< std::vector< long > >, std::vector< long > > ADS::dominoToLp01 ( const std::vector< Tile > & D,
int n )
inline

BOUNDED TILING to 0-1 INTEGER PROGRAMMING: \( A x \le b \) on the \( m n^2 \) variables of tileVar(), each equation as two inequalities.

In pseudocode (Theorem 18.4), \( x_{hij} = 1 \) if the tile \( h \) is in \( ( i , j ) \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{dominoToLp01}( D , n )\quad \triangleright\ x_{hij} = 1 \text{: the tile } h \text{ on } ( i , j ) \cr 2 & \quad \text{the rows } x_{000} = 1 \text{ and } \textstyle\sum_h x_{hij} = 1 \text{ for each square} \cr 3 & \quad \textbf{foreach}\ h \text{ and } ( i , j )\ \textbf{do} \ \text{the rows } x_{hij} + \textstyle\sum_g x_{g,i+1,j} \le 1 \cr & \qquad \text{and } x_{hij} + \textstyle\sum_g x_{g,i,j+1} \le 1 \text{,} \cr & \qquad \text{the sums over the tiles } g \text{ that do not fit on the right of } h \text{, and above } h \cr 4 & \quad \textbf{return}\ ( A , b )\quad \triangleright \ \text{each equation as two inequalities} \end{array}} \]

Precondition
\( n \ge 1 \) and \( D \) is not empty
Postcondition
\( A \) has \( m n^2 \) columns and as many rows as \( b \)

◆ dominoToSat()

Cnf ADS::dominoToSat ( const std::vector< Tile > & D,
int n )
inline

BOUNDED TILING to SATISFIABILITY, on the \( m n^2 \) variables of tileVar().

In pseudocode (Theorem 18.3), \( u_{hij} \) true if the tile \( h \) is in \( ( i , j ) \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{dominoToSat}( D , n )\quad \triangleright \ u_{hij} \text{ true: the tile } h \text{ on } ( i , j ) \cr 2 & \quad F \gets ( u_{000} ) \cr 3 & \quad \text{add, for each square } ( i , j ) \text{, the clause } u_{0ij} \vee \cdots \vee u_{m-1,ij} \text{ and the clauses } \neg u_{hij} \vee \neg u_{gij} \text{, } h < g \cr 4 & \quad \text{add } \neg u_{hij} \vee \neg u_{g,i+1,j} \text{ when } D[ h ].\mathit{right} \ne D[ g ].\mathit{left} \text{, and } \neg u_{hij} \vee \neg u_{g,i,j+1} \text{ when } D[ h ].\mathit{up} \ne D[ g ].\mathit{down} \cr 5 & \quad \textbf{return}\ F \end{array}} \]

Precondition
\( n \ge 1 \) and \( D \) is not empty
Postcondition
the literals are variables of \( [ 0 , m n^2 ) \)

◆ halfSum()

long ADS::halfSum ( const std::vector< long > & A)
inline

half the sum, or \( -1 \) if the sum is odd (then no partition: the bound \( -1 \) has no YES in any of the three problems)

In pseudocode (Theorems 18.7 to 18.9), the bound of MULTIPROCESSOR SCHEDULING on two processors, of SUBSET SUM, and of KNAPSACK with the profits and the volumes equal to the numbers:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{halfSum}( A )\quad \triangleright \ \text{the target of the subset sum for a partition} \cr 2 & \quad s \gets \textstyle\sum_i A[ i ];\ \textbf{return}\ s / 2 \text{ if } s \text{ is even, else } -1 \end{array}} \]

Precondition
\( A \) is not negative
Postcondition
\( -1 \) or half the sum

◆ hamiltonToTsp()

std::vector< std::vector< long > > ADS::hamiltonToTsp ( const Adjacency & G)
inline

HAMILTONIAN CIRCUIT to TRAVELING SALESMAN: the distances, \( k = n \).

In pseudocode (Theorem 18.6), a tour of length \( n \) uses only edges of \( G \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{hamiltonToTsp}( G )\quad \triangleright \ \text{a circuit of cost } n \text{ iff } G \text{ is Hamiltonian} \cr 2 & \quad \textbf{return}\ D \text{ with } D[ i ][ i ] = 0 \text{, } D[ i ][ j ] = 1 \text{ on the edges of } G \text{ and } 2 \text{ elsewhere} \end{array}} \]

Precondition
\( G \) is unoriented, i.e., square and symmetric
Postcondition
a square matrix of the size of \( G \)

◆ sortToHull()

std::vector< Point > ADS::sortToHull ( const std::vector< long > & a)
inline

SORTING to CONVEX HULL: the points \( ( a , a^2 ) \).

In pseudocode (section 18.5), the hull of the points lists them by increasing \( a \), from the leftmost one, and so sorts the numbers:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sortToHull}( a )\quad \triangleright \ \text{the points on the parabola } y = x^2 \text{ are all on the hull} \cr 2 & \quad \textbf{return}\ ( ( a_i , a_i^2 ) : i = 1 , \ldots , n ) \end{array}} \]

Precondition
the squares of \( a \) fit in a long
Postcondition
one point per number

◆ tileVar()

int ADS::tileVar ( int h,
int i,
int j,
int n )
inline

the place \( ( h n + i ) n + j \) of the variable or node of the tile \( h \) in the square \( ( i , j ) \)

Precondition
\( n \ge 1 \), \( h \ge 0 \), and \( ( i , j ) \) a square of the \( n \times n \) board