ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Heuristics.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File Heuristics.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
58/*--------------------------------------------------------------------------*/
59/*----------------------------- DEFINITIONS --------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62#ifndef __ADS_Heuristics
63 #define __ADS_Heuristics
64/* self-identification: #endif constant */
65
66/*--------------------------------------------------------------------------*/
67/*------------------------------ INCLUDES ----------------------------------*/
68/*--------------------------------------------------------------------------*/
69
70#include "ADS/Contract.h"
74#include "ADS/mfset/MFSet.h"
76
77#include <algorithm>
78#include <cstddef>
79#include <cstdlib>
80#include <numeric>
81#include <set>
82#include <string>
83#include <utility>
84#include <vector>
85
86/*--------------------------------------------------------------------------*/
87/*--------------------------- NAMESPACE ------------------------------------*/
88/*--------------------------------------------------------------------------*/
89
90namespace ADS
91{
92
93/*--------------------------------------------------------------------------*/
94/*------------------------------ GREEDY TSP --------------------------------*/
95/*--------------------------------------------------------------------------*/
96
125template< class T >
126std::pair< std::vector< std::pair< int , int > > , Nodes >
127greedyTsp( const std::vector< std::vector< T > > & D );
128
129/*--------------------------------------------------------------------------*/
130/*---------------------------- LOCAL SEARCH --------------------------------*/
131/*--------------------------------------------------------------------------*/
132
139inline Nodes twoOptMove( Nodes pi , int i , int j );
140
168template< class T >
169Nodes twoOpt( const std::vector< std::vector< T > > & D , Nodes pi ,
170 std::vector< T > * trace = nullptr );
171
192inline long kOptNeighbors( int n , int k );
193
195[[deprecated( "use kOptNeighbors" )]] inline long kOptNeighbours( int n ,
196 int k )
197{
198 return( kOptNeighbors( n , k ) );
199 }
200
225inline std::vector< int > partitionLocalSearch( const std::vector< long > & A ,
226 std::vector< int > side );
227
228/*--------------------------------------------------------------------------*/
229/*---------------------------- BISECTION -----------------------------------*/
230/*--------------------------------------------------------------------------*/
231
238{
239
240/*--------------------------------------------------------------------------*/
241 public:
244 explicit Buckets( int n );
245
248 void insert( int u );
249
275 void remove( int u );
276
280 void inc( int k , int u );
281
284 [[nodiscard]] bool contains( int u ) const;
285
288 [[nodiscard]] int count( int k , int u ) const;
289
292 [[nodiscard]] const Nodes & bucket( int k , int h ) const;
293
296 [[nodiscard]] int dim( int k , int h ) const;
297
301 [[nodiscard]] int min( int k ) const;
302
306 [[nodiscard]] int max( int k ) const;
307
312 [[nodiscard]] int choose( int k ) const;
313
314/*--------------------------------------------------------------------------*/
315 private:
316 void put( int k , int u );
317
318 void take( int k , int u );
319
322 [[nodiscard]] bool consistent( void ) const;
323
324 int f_n;
325 std::vector< Nodes > f_bucket[ 2 ];
326 std::vector< int > f_h[ 2 ] , f_place[ 2 ];
327 int f_min[ 2 ] , f_max[ 2 ];
328
329 }; // end( class Buckets )
330
355inline std::vector< int > minMaxGreedy( const Adjacency & G , int a , int b );
356
359inline int cutSize( const Adjacency & G , const std::vector< int > & side );
360
361/*--------------------------------------------------------------------------*/
362/*----------------------------- STRINGS ------------------------------------*/
363/*--------------------------------------------------------------------------*/
364
379inline int overlap( const std::string & a , const std::string & b );
380
409inline std::string greedySuperstring( const std::vector< std::string > & P );
410
429inline std::pair< int , std::vector< std::pair< std::string , std::string > > >
430starAlignment( const std::vector< std::string > & P );
431
432/*--------------------------------------------------------------------------*/
433/*----------------------------- COLORING -----------------------------------*/
434/*--------------------------------------------------------------------------*/
435
461inline std::vector< int > dsatur( const Adjacency & G );
462
463/*--------------------------------------------------------------------------*/
464
465} // end( namespace ADS )
466
467/*--------------------------------------------------------------------------*/
468/*-------------------------- IMPLEMENTATION --------------------------------*/
469/*--------------------------------------------------------------------------*/
470
471#include "ADS/intractable/impl/Heuristics.tpp"
472
473/*--------------------------------------------------------------------------*/
474
475#endif /* Heuristics.h included */
476
477/*--------------------------------------------------------------------------*/
478/*------------------------- End File Heuristics.h --------------------------*/
479/*--------------------------------------------------------------------------*/
The alignments of two strings and of exercises 13.7 to 13.9 of the book: spaces are inserted in or...
The approximation algorithms of section 19.2 and of the exercises 19.6 and 19.8.
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The MFSET of section 10.2, the union of disjoint sets: MFSet( n ) makes the components ,...
The minimum spanning tree of an unoriented connected graph with weights on the edges (section 14....
The problems in NP of chapter 18, each with its polynomial certificate, which checks a proposed solut...
int min(int k) const
the least with bucket( k , h ) not empty, if none
bool contains(int u) const
whether is in the structure
Buckets(int n)
the empty structure for the nodes
const Nodes & bucket(int k, int h) const
the nodes with edges to
void remove(int u)
takes out of its two buckets, moving the least and the largest bucket not empty if they empty
int count(int k, int u) const
the edges of to
int max(int k) const
the largest with bucket( k , h ) not empty, if none
int dim(int k, int h) const
the number of the nodes with edges to
void inc(int k, int u)
has one more edge to , and moves to the next bucket
void insert(int u)
with no edges to either side, in the buckets 0
int choose(int k) const
the node to add to : in the least bucket of , the one with most edges to , the first of the bucket on...
int cutSize(const Adjacency &G, const std::vector< int > &side)
the number of the edges across the two sides
Nodes twoOptMove(Nodes pi, int i, int j)
the circuit of without the edges and , , not consecutive: the stretch reversed
std::vector< int > minMaxGreedy(const Adjacency &G, int a, int b)
the side of each node of , even, from in and in (the min-max-greedy of the book)
int overlap(const std::string &a, const std::string &b)
the length of the longest suffix of that is a prefix of , at most : the whole of if ends with it,...
std::vector< int > partitionLocalSearch(const std::vector< long > &A, std::vector< int > side)
the side, 0 or 1, of each element of , by the local search from side that moves an element or swaps t...
std::vector< int > dsatur(const Adjacency &G)
the coloring by saturation degree: the color, from 0, of each node of , colored one at a time with th...
long kOptNeighbors(int n, int k)
the size of on nodes: the circuits that share exactly edges with , by enumeration of all the circu...
Nodes twoOpt(const std::vector< std::vector< T > > &D, Nodes pi, std::vector< T > *trace=nullptr)
the local search on from , for the symmetric distances , taking the best circuit of the neighborhood...
long kOptNeighbours(int n, int k)
the former name of kOptNeighbors(), kept for the code that uses it
Definition Heuristics.h:195
std::pair< int, std::vector< std::pair< std::string, std::string > > > starAlignment(const std::vector< std::string > &P)
the star alignment: the center , the string with the largest sum of the global scores against the oth...
std::pair< std::vector< std::pair< int, int > >, Nodes > 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,...
std::string greedySuperstring(const std::vector< std::string > &P)
a common superstring of , by the greedy on the overlaps: the oriented edges by decreasing overlap,...
Definition ADT.h:96
std::vector< int > Nodes
a set, a path
Definition Problems.h:86
std::vector< std::vector< bool > > Adjacency
an adjacency
Definition Problems.h:83