|
ADS
Algorithms and Data Structures
|
The approximation algorithms of section 19.2 and of the exercises 19.6 and 19.8. More...
Files | |
| file | ADS/intractable/Approximation.h |
| The approximation algorithms of section 19.2 and of the exercises 19.6 and 19.8. | |
Functions | |
| std::vector< int > | ADS::firstFit (const std::vector< long > &A, long k, bool decreasing=false) |
| the box of each object, the first one with room for it (ff), the objects in the order given or, if decreasing, in decreasing order of volume (ffd); the boxes are \( 0 , 1 , \ldots \) | |
| template<class T> | |
| T | ADS::tourCost (const std::vector< std::vector< T > > &D, const Nodes &S) |
| the cost \( \sum_i D[ S[ i ] ][ S[ ( i + 1 ) \bmod n ] ] \) of the circuit \( S \) of the distances \( D \) | |
| template<class T> | |
| Nodes | ADS::deltaTsp (const std::vector< std::vector< T > > &D) |
| the circuit of the nodes of a minimum spanning tree of the complete graph of the symmetric distances \( D \) in preorder from 0, the children by increasing name: at most twice the optimum if \( D \) is triangular | |
| std::vector< std::vector< long > > | ADS::tspGap (const Adjacency &G, long s) |
| the distances of Theorem 19.2: 1 on the edges of \( G \), \( s n \) elsewhere; their optimum is \( n \) if \( G \) has a Hamiltonian circuit and more than \( s n \) otherwise | |
| std::pair< long, Nodes > | ADS::apSubsetSum (const std::vector< long > &A, long k, int h) |
| the sum, at most \( k \), and the places of \( A \) of the best set found from the sets of at most \( h \) elements, \( \epsilon = 1 / ( h + 1 ) \); the sets of (1) in the order of the text, by size and then lexicographically on \( A \) in decreasing order | |
| std::pair< long, Nodes > | ADS::knapsackApprox (const std::vector< long > &p, const std::vector< long > &v, long C, double eps) |
| the best profit and the objects of an \( \epsilon \)-approximation of KNAPSACK, profits \( p \) and volumes \( v \) within \( C \), by the rounded profits | |
| Nodes | ADS::vertexCoverApprox (const Adjacency &G) |
| both ends of each edge \( [ u , v ] \), \( u < v \), whose ends are both out of \( S \), at most twice a minimum vertex cover; \( O( n^2 ) \) on the adjacency matrix | |
The approximation algorithms of section 19.2 and of the exercises 19.6 and 19.8.
The definitions, the invariants and the costs of the area are in Theory of intractable problems.
|
inline |
the sum, at most \( k \), and the places of \( A \) of the best set found from the sets of at most \( h \) elements, \( \epsilon = 1 / ( h + 1 ) \); the sets of (1) in the order of the text, by size and then lexicographically on \( A \) in decreasing order
In pseudocode (section 19.2.3), the elements renamed so that \( A[ 1 ] \ge \cdots \ge A[ n ] \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{apSubsetSum}( A , k , h )\quad \triangleright\ \text{each seed of } h \text{ items at most, then a greedy completion} \cr 2 & \quad ( \mathit{max} , \mathit{maxSol} ) \gets ( 0 , \emptyset ) \cr 3 & \quad \textbf{foreach}\ S \subseteq \{ 1 , \ldots , n \} \text{ with } |S| \le h \text{ and } z = \textstyle\sum_{j \in S} A[ j ] \le k\ \textbf{do} \cr 4 & \qquad \textbf{foreach}\ j \notin S \text{, from } 1 \text{ to } n \ \textbf{do}\ \textbf{if}\ z + A[ j ] \le k\ \textbf{then}\ S \gets S \cup \{ j \};\ z \gets z + A[ j ] \cr 5 & \qquad \textbf{if}\ \mathit{max} \le z\ \textbf{then}\ ( \mathit{max} , \mathit{maxSol} ) \gets ( z , S ) \cr 6 & \quad \textbf{return}\ ( \mathit{max} , \mathit{maxSol} ) \end{array}} \]
| Nodes ADS::deltaTsp | ( | const std::vector< std::vector< T > > & | D | ) |
the circuit of the nodes of a minimum spanning tree of the complete graph of the symmetric distances \( D \) in preorder from 0, the children by increasing name: at most twice the optimum if \( D \) is triangular
In pseudocode (section 19.2.1), \( T \) costs less than any circuit, which is a spanning tree plus an edge, and the preorder skips cities of the walk that goes twice along \( T \), which by the triangle inequality costs no more:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{deltaTsp}( D )\quad \triangleright \ \text{at most twice the optimum under the triangle inequality} \cr 2 & \quad T \gets \text{the minimum spanning tree by } \mathrm{kruskal} \text{ of the complete graph weighted by } D \cr 3 & \quad \textbf{return}\ \text{the previsit of } T \text{ from } 0 \text{, the children by increasing name}\quad \triangleright \ \text{shortcuts of the walk} \end{array}} \]
|
inline |
the box of each object, the first one with room for it (ff), the objects in the order given or, if decreasing, in decreasing order of volume (ffd); the boxes are \( 0 , 1 , \ldots \)
In pseudocode (section 19.2.1), \( F \) the room left in each box open:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{firstFit}( A , k , \mathit{decreasing} )\quad \triangleright\ F[ b ] \text{ the room left in the box } b \cr 2 & \quad F \gets () \cr 3 & \quad \textbf{foreach}\ i \in [ 0 , n ) \text{, by decreasing } A[ i ] \text{ if } \mathit{decreasing} \text{, else by index}\ \textbf{do} \cr 4 & \qquad b \gets \text{the first box with } A[ i ] \le F[ b ] \text{, or a new last box with } F[ b ] = k \cr 5 & \qquad \mathit{box}[ i ] \gets b;\ F[ b ] \gets F[ b ] - A[ i ] \cr 6 & \quad \textbf{return}\ \mathit{box} \end{array}} \]
|
inline |
the best profit and the objects of an \( \epsilon \)-approximation of KNAPSACK, profits \( p \) and volumes \( v \) within \( C \), by the rounded profits
In pseudocode (exercise 19.8), \( V[ i ][ s ] \) the least volume of the first \( i \) objects that fit of rounded profit \( s \); the rounding loses less than \( K \) per object, i.e., less than \( \epsilon p_{\max} \le \epsilon\, \mathit{OPT} \) in all:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{knapsackApprox}( p , v , C , \epsilon ) \cr 2 & \quad I \gets \text{the objects that fit alone};\ \textbf{if}\ I = \emptyset\ \textbf{then return}\ ( 0 , \emptyset ) \cr 3 & \quad K \gets \max\{ 1 , \epsilon p_{\max} / |I| \};\ q_i \gets \lfloor p_i / K \rfloor \text{ for } i \in I\quad \triangleright \ \text{the profits scaled down} \cr 4 & \quad V[ 0 ][ s ] \gets 0 \text{ if } s = 0 \text{, else } \infty \cr 5 & \quad \textbf{foreach}\ \text{the } i \text{-th object of } I \text{ and } s \in [ 0 , \textstyle\sum q ]\ \textbf{do}\quad \triangleright\ \text{without it, or with it if } q_i \le s \cr 6 & \qquad V[ i + 1 ][ s ] \gets \min\{ V[ i ][ s ] , V[ i ][ s - q_i ] + v_i \} \cr 7 & \quad s \gets \text{the largest } s \text{ with } V[ |I| ][ s ] \le C \cr 8 & \quad \textbf{return}\ \text{the objects of } V[ |I| ][ s ] \text{, read back from the table, and their profit} \end{array}} \]
| T ADS::tourCost | ( | const std::vector< std::vector< T > > & | D, |
| const Nodes & | S ) |
the cost \( \sum_i D[ S[ i ] ][ S[ ( i + 1 ) \bmod n ] ] \) of the circuit \( S \) of the distances \( D \)
|
inline |
the distances of Theorem 19.2: 1 on the edges of \( G \), \( s n \) elsewhere; their optimum is \( n \) if \( G \) has a Hamiltonian circuit and more than \( s n \) otherwise
In pseudocode (section 19.2.2):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{tspGap}( G , s )\quad \triangleright \ \text{a circuit of cost } n \text{ iff } G \text{ is Hamiltonian} \cr 2 & \quad \textbf{return}\ D \text{ with } D[ u ][ u ] = 0 \text{, and } D[ u ][ v ] = 1 \text{ on the edges of } G \text{, } s n \text{ elsewhere} \end{array}} \]
both ends of each edge \( [ u , v ] \), \( u < v \), whose ends are both out of \( S \), at most twice a minimum vertex cover; \( O( n^2 ) \) on the adjacency matrix
In pseudocode (exercise 19.6), the edges taken form a matching, and a cover has an end of each of them:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{vertexCoverApprox}( G )\quad \triangleright \ \text{the ends of a maximal matching: twice the optimum} \cr 2 & \quad S \gets \emptyset \cr 3 & \quad \textbf{foreach}\ [ u , v ] \in E \text{, } u < v \text{, in lexicographic order, with } u , v \notin S\ \textbf{do}\ S \gets S \cup \{ u , v \} \cr 4 & \quad \textbf{return}\ S \end{array}} \]