|
ADS
Algorithms and Data Structures
|
The heuristics of section 19.4 and of the exercises 19.13 and 19.16 to 19.19. More...
Files | |
| file | ADS/intractable/Heuristics.h |
| The heuristics of section 19.4 and of the exercises 19.13 and 19.16 to 19.19: feasible solutions, neither optimal nor approximate, greedy or by local search. | |
Classes | |
| class | ADS::Buckets |
| the structure with buckets of exercise 19.17: for \( k = 0 , 1 \) the nodes by the number \( h \) of their edges to \( V_k \), bucket( k , h ), its size dim( k , h ), and the least and largest bucket not empty; a node leaves a bucket in \( O( 1 ) \) by its place in it More... | |
Functions | |
| template<class T> | |
| std::pair< std::vector< std::pair< int, int > >, Nodes > | ADS::greedyTsp (const std::vector< std::vector< T > > &D) |
| the edges chosen, in the order of insertion, the last one closing the chain, and the circuit from 0, for the symmetric distances \( D \) of \( n \ge 3 \) cities | |
| Nodes | ADS::twoOptMove (Nodes pi, int i, int j) |
| the circuit of \( I_2( \pi ) \) without the edges \( ( \pi[ i ] , \pi[ i + 1 ] ) \) and \( ( \pi[ j ] , \pi[ j + 1 ] ) \), \( 0 \le i < j < n \), not consecutive: the stretch \( \pi[ i + 1 .. j ] \) reversed | |
| template<class T> | |
| Nodes | ADS::twoOpt (const std::vector< std::vector< T > > &D, Nodes pi, std::vector< T > *trace=nullptr) |
| the local search on \( I_2 \) from \( \pi \), for the symmetric distances \( D \), taking the best circuit of the neighborhood while it improves; the costs of the circuits visited go to *trace, if given | |
| long | ADS::kOptNeighbors (int n, int k) |
| the size of \( I_k( \pi ) \) on \( n \ge 3 \) nodes: the circuits that share exactly \( n - k \) edges with \( \pi \), by enumeration of all the circuits, \( O( n! ) \) | |
| long | ADS::kOptNeighbours (int n, int k) |
| the former name of kOptNeighbors(), kept for the code that uses it | |
| std::vector< int > | ADS::partitionLocalSearch (const std::vector< long > &A, std::vector< int > side) |
| the side, 0 or 1, of each element of \( A \), by the local search from side that moves an element or swaps two while the difference of the sums decreases, the best move first; \( O( n^2 ) \) each step | |
| std::vector< int > | ADS::minMaxGreedy (const Adjacency &G, int a, int b) |
| the side of each node of \( G \), \( n \) even, from \( a \) in \( V_0 \) and \( b \) in \( V_1 \) (the min-max-greedy of the book) | |
| int | ADS::cutSize (const Adjacency &G, const std::vector< int > &side) |
| the number of the edges across the two sides | |
| int | ADS::overlap (const std::string &a, const std::string &b) |
| the length of the longest suffix of \( a \) that is a prefix of \( b \), at most \( \min\{ |a| , |b| \} \): the whole of \( b \) if \( a \) ends with it, 0 if either is empty | |
| std::string | ADS::greedySuperstring (const std::vector< std::string > &P) |
| a common superstring of \( P \), by the greedy on the overlaps: the oriented edges \( ( i , j ) \) by decreasing overlap, each added if \( \mathit{out}[ i ] \) and \( \mathit{in}[ j ] \) are 0 and \( i , j \) are apart, and the path joined; the strings contained in another are dropped first | |
| std::pair< int, std::vector< std::pair< std::string, std::string > > > | ADS::starAlignment (const std::vector< std::string > &P) |
| the star alignment: the center \( h \), the string with the largest sum of the global scores against the others, and its global alignment with each string, the center against itself included; \( O( k^2 n^2 ) \) | |
| std::vector< int > | ADS::dsatur (const Adjacency &G) |
| the coloring by saturation degree: the color, from 0, of each node of \( G \), colored one at a time with the least color not on its neighbors, the next node the one with most distinct colors among its neighbors, then with most neighbors colored, then the first; \( O( n^3 ) \) | |
The heuristics of section 19.4 and of the exercises 19.13 and 19.16 to 19.19.
The definitions, the invariants and the costs of the area are in Theory of intractable problems.
|
inline |
the number of the edges across the two sides
|
inline |
the coloring by saturation degree: the color, from 0, of each node of \( G \), colored one at a time with the least color not on its neighbors, the next node the one with most distinct colors among its neighbors, then with most neighbors colored, then the first; \( O( n^3 ) \)
In pseudocode (exercise 19.19, the algorithm of Brelaz):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{dsatur}( G )\quad \triangleright \ \text{the saturation of } u \text{: the colors among its neighbors} \cr 2 & \quad c[ v ] \gets \textbf{nil} \text{ for every node } v \cr 3 & \quad \textbf{repeat}\ n \text{ times} \cr 4 & \qquad u \gets \text{the uncolored node of the largest saturation; on a tie, of the } \text{most colored neighbors,} \cr & \qquad\quad \text{then the first} \cr 5 & \qquad c[ u ] \gets \text{the least color absent from the neighbors of } u \cr 6 & \quad \textbf{return}\ c \end{array}} \]
|
inline |
a common superstring of \( P \), by the greedy on the overlaps: the oriented edges \( ( i , j ) \) by decreasing overlap, each added if \( \mathit{out}[ i ] \) and \( \mathit{in}[ j ] \) are 0 and \( i , j \) are apart, and the path joined; the strings contained in another are dropped first
In pseudocode (exercise 19.13), the greedy of greedyTsp() on oriented edges, which leaves paths, each string written without its overlap with the one before it:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{greedySuperstring}( P ) \cr 2 & \quad Q \gets P \text{ less its copies and the strings inside another one} \cr 3 & \quad \textbf{foreach}\ \text{pair } i \ne j \text{, by decreasing } \mathrm{overlap}( Q[ i ] , Q[ j ] )\ \textbf{do}\quad \triangleright \ \text{the strongest merge first} \cr 4 & \qquad \textbf{if}\ i \text{ has no successor, } j \text{ no predecessor, and } \mathrm{find}( i ) \ne \mathrm{find}( j )\ \textbf{then} \cr 5 & \qquad\quad \mathit{next}[ i ] \gets j;\ \mathit{out}[ i ] \gets \textbf{true};\ \mathit{in}[ j ] \gets \textbf{true}; \ \mathrm{merge}( i , j ) \cr 6 & \quad \textbf{return}\ \text{the strings chained by } \mathit{next} \text{ from each start, each overlap written once} \end{array}} \]
| std::pair< std::vector< std::pair< int, int > >, Nodes > ADS::greedyTsp | ( | const std::vector< std::vector< T > > & | D | ) |
the edges chosen, in the order of insertion, the last one closing the chain, and the circuit from 0, for the symmetric distances \( D \) of \( n \ge 3 \) cities
In pseudocode (section 19.4.1), the edges kept form paths, since no node gets a third edge and no edge closes a cycle, until one path spans the cities:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{greedyTsp}( D )\quad \triangleright \ \text{a Hamiltonian path grown by the shortest edges} \cr 2 & \quad S \gets \emptyset;\ M \gets \text{the MFSET of the } n \text{ cities} \cr 3 & \quad \textbf{foreach}\ [ u , v ] \text{, by increasing } D[ u ][ v ] \text{, until } |S| = n - 1\ \textbf{do} \cr 4 & \qquad \textbf{if}\ u \text{ and } v \text{ have degree } < 2 \text{ in } S \text{ and } \mathrm{find}( M , u ) \ne \mathrm{find}( M , v )\ \textbf{then} \cr 5 & \qquad\quad S \gets S \cup \{ [ u , v ] \};\ \mathrm{merge}( M , u , v ) \cr 6 & \quad \text{add to } S \text{ the edge between the two ends of the path} \cr 7 & \quad \textbf{return}\ ( S , \text{the circuit of } S \text{ from } 0 ) \end{array}} \]
|
inline |
the size of \( I_k( \pi ) \) on \( n \ge 3 \) nodes: the circuits that share exactly \( n - k \) edges with \( \pi \), by enumeration of all the circuits, \( O( n! ) \)
In pseudocode (exercise 19.15), each circuit met twice, once per direction:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{kOptNeighbors}( n , k )\quad \triangleright\ \text{by enumeration, for the small } n \cr 2 & \quad E \gets \text{the edges of the circuit } ( 0 , 1 , \ldots , n - 1 ) \cr 3 & \quad c \gets \text{the permutations } s \text{ of } 1 , \ldots , n - 1 \text{ whose circuit } ( 0 ) \cdot s \text{ keeps exactly } n - k \text{ edges of } E \cr 4 & \quad \textbf{return}\ c / 2\quad \triangleright \ \text{each circuit is met in both directions} \end{array}} \]
|
inline |
the former name of kOptNeighbors(), kept for the code that uses it
|
inline |
the side of each node of \( G \), \( n \) even, from \( a \) in \( V_0 \) and \( b \) in \( V_1 \) (the min-max-greedy of the book)
In pseudocode (section 19.4.1), the sides filled in turn, each time with the node out of them that adds the fewest edges to the cut:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{minMaxGreedy}( G , a , b )\quad \triangleright\ \text{the two sides grow in turn} \cr 2 & \quad B \gets \text{the buckets of all the nodes};\ \mathrm{add}( a , 0 );\ \mathrm{add}( b , 1 );\ k \gets 0 \cr 3 & \quad \textbf{repeat}\ n - 2 \text{ times}\ \mathrm{add}( B.\mathrm{choose}( k ) , k );\ k \gets 1 - k \cr 4 & \quad \textbf{return}\ \mathit{side} \cr 5 & \textbf{procedure}\ \mathrm{add}( u , k )\quad \triangleright\ u \text{ on the side } k \cr 6 & \quad \mathit{side}[ u ] \gets k;\ B.\mathrm{remove}( u ) \cr 7 & \quad \textbf{foreach}\ \text{neighbor } v \text{ of } u \text{ still in } B\ \textbf{do}\ B.\mathrm{inc}( k , v ) \end{array}} \]
|
inline |
the length of the longest suffix of \( a \) that is a prefix of \( b \), at most \( \min\{ |a| , |b| \} \): the whole of \( b \) if \( a \) ends with it, 0 if either is empty
In pseudocode (exercise 19.13), \( O( \min\{ |a| , |b| \}^2 ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{overlap}( a , b )\quad \triangleright \ \text{the longest suffix of } a \text{ that is a prefix of } b \cr 2 & \quad \textbf{return}\ \text{the largest } \ell \le \min\{ |a| , |b| \} \text{ with } a[ |a| - \ell .. |a| - 1 ] = b[ 0 .. \ell - 1 ] \text{, or } 0 \end{array}} \]
|
inline |
the side, 0 or 1, of each element of \( A \), by the local search from side that moves an element or swaps two while the difference of the sums decreases, the best move first; \( O( n^2 ) \) each step
In pseudocode (exercise 19.16), \( \Delta \) the sum of the side 0 minus that of the side 1, which a move of \( A[ i ] \) changes by \( \pm 2 A[ i ] \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{partitionLocalSearch}( A , \mathit{side} )\quad \triangleright\ \Delta \text{ the difference of the two sums} \cr 2 & \quad \textbf{loop} \cr 3 & \qquad M \gets \text{among the moves of one item and the swaps of two items on } \text{opposite sides,} \cr & \qquad\quad \text{the one that leaves the least } | \Delta' | \cr 4 & \qquad \textbf{if}\ | \Delta | \le | \Delta' |\ \textbf{then return} \ \mathit{side}\quad \triangleright\ \text{a local optimum} \cr 5 & \qquad \text{apply } M;\ \Delta \gets \Delta' \end{array}} \]
|
inline |
the star alignment: the center \( h \), the string with the largest sum of the global scores against the others, and its global alignment with each string, the center against itself included; \( O( k^2 n^2 ) \)
In pseudocode (exercise 19.18, after Gusfield):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{starAlignment}( P )\quad \triangleright \ \text{the center aligned with every other string} \cr 2 & \quad h \gets \text{the first } i \text{ that maximizes } \textstyle\sum_{j \ne i} \mathrm{score}( P[ i ] , P[ j ] ) \cr 3 & \quad \textbf{return}\ ( h , ( \mathrm{globalAlignment}( P[ h ] , P[ j ] ) : j = 1 , \ldots , k ) ) \end{array}} \]
| Nodes ADS::twoOpt | ( | const std::vector< std::vector< T > > & | D, |
| Nodes | pi, | ||
| std::vector< T > * | trace = nullptr ) |
the local search on \( I_2 \) from \( \pi \), for the symmetric distances \( D \), taking the best circuit of the neighborhood while it improves; the costs of the circuits visited go to *trace, if given
In pseudocode (section 19.4.2), the cost of a move computed in \( O( 1 ) \) from the four edges that change, a step in \( O( n^2 ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{twoOpt}( D , \pi )\quad \triangleright \ \text{the best improvement, until there is none} \cr 2 & \quad c \gets \mathrm{tourCost}( D , \pi ) \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \text{for each } i < j \text{ with } ( \pi[ i ] , \pi[ i + 1 ] ) \text{ and } ( \pi[ j ] , \pi[ ( j + 1 ) \bmod n ] ) \text{ not adjacent,} \cr & \qquad\quad \text{let } c_{ij} \text{ be the cost after replacing them by } ( \pi[ i ] , \pi[ j ] ) \text{ and } ( \pi[ i + 1 ] , \pi[ ( j + 1 ) \bmod n ] ) \cr 5 & \qquad ( b_i , b_j ) \gets \text{the first pair of least } c' = c_{b_i b_j} \cr 6 & \qquad \textbf{if}\ c \le c'\ \textbf{then return}\ \pi \cr 7 & \qquad \pi \gets \mathrm{twoOptMove}( \pi , b_i , b_j );\ c \gets c' \end{array}} \]
the circuit of \( I_2( \pi ) \) without the edges \( ( \pi[ i ] , \pi[ i + 1 ] ) \) and \( ( \pi[ j ] , \pi[ j + 1 ] ) \), \( 0 \le i < j < n \), not consecutive: the stretch \( \pi[ i + 1 .. j ] \) reversed