ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Heuristics.h File Reference

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. More...

#include "ADS/Contract.h"
#include "ADS/complexity/Problems.h"
#include "ADS/graph/MinimumSpanningTree.h"
#include "ADS/intractable/Approximation.h"
#include "ADS/mfset/MFSet.h"
#include "ADS/string/Alignment.h"
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "ADS/intractable/impl/Heuristics.tpp"

Go to the source code of this file.

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...

Namespaces

namespace  ADS

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 ) \)

Detailed description

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.

  • greedyTsp(): the edges by increasing weight, each added if its ends have fewer than two edges and are in different components of an MFSET, until a Hamiltonian chain, closed by the edge of its two ends, \( O( n^2 \log n ) \);
  • minMaxGreedy() for the BISECTION: from a node in \( V_0 \) and one in \( V_1 \), a node added to \( V_0 \) and then one to \( V_1 \), the one with fewest edges to the other side and, among those, most edges to its own; the counts are kept by the structure with buckets of exercise 19.17 [see Buckets];
  • twoOpt(): the local search on the neighborhood \( I_2( \pi ) \), the circuits made of \( \pi \) by replacing two edges that do not touch with the two that close it again, \( n ( n - 1 ) / 2 - n \) of them; kOptNeighbors() counts \( I_k( \pi ) \) (exercise 19.15);
  • partitionLocalSearch(): the PARTITION that minimizes the difference of the sums, moving an element or swapping two (exercise 19.16);
  • greedySuperstring(): the greedy of greedyTsp() on the graph of the overlaps, an oriented path (exercise 19.13);
  • starAlignment(): the multiple alignment against the string with the best sum of the global scores (exercise 19.18);
  • dsatur(): the coloring by saturation degree (exercise 19.19).

The contracts [see Contract.h]: the distances are square and symmetric, the graphs unoriented, the circuits permutations; each heuristic checks that its result is feasible, not that it is good, and the structure with buckets keeps its invariant at level 3. The operations are defined in Heuristics.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 19.4 and exercises 19.13-19.19, https://algoritmicamente.it/materiali/libro/capitoli/cap19.pdf
B. W. Kernighan, S. Lin, An efficient heuristic procedure for partitioning graphs, Bell System Technical Journal 49(2), 291-307, 1970, doi:10.1002/j.1538-7305.1970.tb01770.x
S. Lin, Computer solutions of the traveling salesman problem, Bell System Technical Journal 44(10), 2245-2269, 1965, doi:10.1002/j.1538-7305.1965.tb04146.x
D. Gusfield, Efficient methods for multiple sequence alignment with guaranteed error bounds, Bulletin of Mathematical Biology 55(1), 141-154, 1993, doi:10.1007/BF02460299
D. Brelaz, New methods to color the vertices of a graph, Communications of the ACM 22(4), 251-256, 1979, doi:10.1145/359094.359101
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa