ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Problems.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File Problems.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
54/*--------------------------------------------------------------------------*/
55/*----------------------------- DEFINITIONS --------------------------------*/
56/*--------------------------------------------------------------------------*/
57
58#ifndef __ADS_Problems
59 #define __ADS_Problems
60/* self-identification: #endif constant */
61
62/*--------------------------------------------------------------------------*/
63/*------------------------------ INCLUDES ----------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66#include "ADS/Contract.h"
67
68#include <algorithm>
69#include <array>
70#include <cstddef>
71#include <cstdlib>
72#include <numeric>
73#include <optional>
74#include <vector>
75
76/*--------------------------------------------------------------------------*/
77/*--------------------------- NAMESPACE ------------------------------------*/
78/*--------------------------------------------------------------------------*/
79
80namespace ADS
81{
82
83using Adjacency = std::vector< std::vector< bool > >;
84using Clause = std::vector< int >;
85using Cnf = std::vector< Clause >;
86using Nodes = std::vector< int >;
87
88/*--------------------------------------------------------------------------*/
89/*------------------------------ GRAPHS ------------------------------------*/
90/*--------------------------------------------------------------------------*/
91
105inline bool cliqueCertificate( const Adjacency & G , const Nodes & S , int k );
106
107namespace detail
108{
109
111template< class Ok , class Done >
112bool chooseK( int n , int k , Ok && ok , Done && done );
113
114} // end( namespace detail )
115
140inline std::optional< Nodes > clique( const Adjacency & G , int k );
141
156inline std::optional< Nodes > independentSet( const Adjacency & G , int k );
157
175inline std::optional< Nodes > vertexCover( const Adjacency & G , int k );
176
202inline std::optional< std::vector< int > > coloring( const Adjacency & G ,
203 int k );
204
222inline std::optional< Nodes > bisection( const Adjacency & G , int k );
223
237template< class T >
238bool tspCertificate( const std::vector< std::vector< T > > & D , T k ,
239 const Nodes & P );
240
264template< class T >
265std::optional< Nodes > tsp( const std::vector< std::vector< T > > & D , T k );
266
267namespace detail
268{
269
272inline std::optional< Nodes > hamilton( const Adjacency & G ,
273 const Nodes & starts , bool cycle ,
274 int end = -1 );
275
276inline Nodes all( int n );
277
278} // end( namespace detail )
279
300inline std::optional< Nodes > hamiltonianCircuit( const Adjacency & G );
301
316inline std::optional< Nodes > hamiltonianCycle( const Adjacency & G );
317
337inline std::optional< Nodes > hamiltonianPath( const Adjacency & G ,
338 int r = -1 ,
339 int t = -1 );
340
353inline std::optional< Nodes > hamiltonianChain( const Adjacency & G );
354
373inline std::optional< Nodes > longestPath( const Adjacency & G , int r ,
374 int t ,
375 int k );
376
397inline int longestPathDag( const Adjacency & G , int r , int t );
398
425inline std::optional< std::vector< std::pair< int , int > > >
426boundedSpanningTree( const Adjacency & G , int k );
427
428/*--------------------------------------------------------------------------*/
429/*---------------------- LOGIC AND LINEAR PROGRAMMING ----------------------*/
430/*--------------------------------------------------------------------------*/
431
443inline bool satCertificate( const Cnf & F , const std::vector< bool > & x );
444
468inline std::optional< std::vector< bool > > satisfiable( const Cnf & F ,
469 int n );
470
486inline std::optional< std::vector< int > >
487lp01( const std::vector< std::vector< long > > & A ,
488 const std::vector< long > & b , int n );
489
490/*--------------------------------------------------------------------------*/
491/*------------------------------- NUMBERS ----------------------------------*/
492/*--------------------------------------------------------------------------*/
493
514inline std::optional< Nodes > subsetSumExists( const std::vector< long > & A ,
515 long k );
516
531inline std::optional< Nodes > partition( const std::vector< long > & A );
532
555inline std::optional< Nodes > knapsackDecision( const std::vector< long > & p ,
556 const std::vector< long > & v ,
557 long C , long k );
558
585inline std::optional< std::vector< int > >
586multiprocessorScheduling( const std::vector< long > & t , int m , long d );
587
611inline std::optional< Nodes >
612exactCover3( const std::vector< std::array< int , 3 > > & Y , int m );
613
630inline std::optional< Nodes >
631threeDimensionalMatching( const std::vector< std::array< int , 3 > > & M ,
632 int q );
633
654inline std::optional< std::vector< long > >
655bribes( std::vector< long > M , const std::vector< bool > & ps );
656
657/*--------------------------------------------------------------------------*/
658
659} // end( namespace ADS )
660
661/*--------------------------------------------------------------------------*/
662/*-------------------------- IMPLEMENTATION --------------------------------*/
663/*--------------------------------------------------------------------------*/
664
665#include "ADS/complexity/impl/Problems.tpp"
666
667/*--------------------------------------------------------------------------*/
668
669#endif /* Problems.h included */
670
671/*--------------------------------------------------------------------------*/
672/*-------------------------- End File Problems.h ---------------------------*/
673/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
std::optional< std::vector< int > > lp01(const std::vector< std::vector< long > > &A, const std::vector< long > &b, int n)
a 0-1 vector with , by enumeration, or nothing
std::optional< Nodes > independentSet(const Adjacency &G, int k)
nodes no two adjacent, or nothing
std::optional< Nodes > tsp(const std::vector< std::vector< T > > &D, T k)
a tour of length at most from city 0, the partial tours longer than pruned, or nothing (the enTsp()...
std::optional< std::vector< int > > multiprocessorScheduling(const std::vector< long > &t, int m, long d)
the processor of each program, so that processors run them all within time , or nothing (also if ,...
bool satCertificate(const Cnf &F, const std::vector< bool > &x)
whether satisfies every clause of
std::optional< std::vector< int > > coloring(const Adjacency &G, int k)
a coloring of the nodes with at most colors, adjacent nodes of different colors, by backtrack,...
std::optional< Nodes > vertexCover(const Adjacency &G, int k)
at most nodes that touch every edge, or nothing
std::optional< Nodes > hamiltonianPath(const Adjacency &G, int r=-1, int t=-1)
an oriented path through every node once, from and to if given
std::optional< Nodes > hamiltonianCircuit(const Adjacency &G)
an unoriented circuit through every node once ( ), or nothing
std::optional< Nodes > longestPath(const Adjacency &G, int r, int t, int k)
a simple path from to of at least edges, by backtrack
std::optional< Nodes > hamiltonianChain(const Adjacency &G)
an unoriented chain through every node once
bool tspCertificate(const std::vector< std::vector< T > > &D, T k, const Nodes &P)
whether the tour , a permutation, is at most long; the empty tour of no city is 0 long (the certific...
std::optional< Nodes > knapsackDecision(const std::vector< long > &p, const std::vector< long > &v, long C, long k)
objects of volume at most and profit at least (the enZaino() of the book)
std::optional< Nodes > partition(const std::vector< long > &A)
the places of a half of of half the sum, or nothing
bool cliqueCertificate(const Adjacency &G, const Nodes &S, int k)
whether , of distinct nodes, has of them or more, all adjacent (the certificatoCricca() of the book)
int longestPathDag(const Adjacency &G, int r, int t)
the most edges of a path from to in an acyclic oriented graph, if is not reachable; by the topol...
std::optional< Nodes > threeDimensionalMatching(const std::vector< std::array< int, 3 > > &M, int q)
a matching of triples of with distinct , and , the three sets being , or nothing
std::optional< Nodes > subsetSumExists(const std::vector< long > &A, long k)
a set of the places of of sum , by backtrack, or nothing
std::optional< Nodes > clique(const Adjacency &G, int k)
nodes all adjacent, or nothing (the enCricca() of the book)
std::optional< std::vector< std::pair< int, int > > > boundedSpanningTree(const Adjacency &G, int k)
a spanning tree of the unoriented graph with at most edges at each node, as edges,...
std::optional< std::vector< bool > > satisfiable(const Cnf &F, int n)
an assignment of the variables that satisfies , by backtrack with a clause checked as soon as its va...
std::optional< Nodes > bisection(const Adjacency &G, int k)
a partition of the nodes in two halves cut by at most edges, or nothing; the half of node 0
std::optional< Nodes > exactCover3(const std::vector< std::array< int, 3 > > &Y, int m)
sets of three of that partition it, by places, the ones that meet those chosen pruned,...
std::optional< std::vector< long > > bribes(std::vector< long > M, const std::vector< bool > &ps)
the bribes to the politicians, if the -th is of the party PS, so that the PS gets twice the party...
std::optional< Nodes > hamiltonianCycle(const Adjacency &G)
an oriented cycle through every node once ( ), or nothing
Definition ADT.h:96
std::vector< int > Clause
some literals
Definition Problems.h:84
std::vector< int > Nodes
a set, a path
Definition Problems.h:86
std::vector< Clause > Cnf
a formula
Definition Problems.h:85
std::vector< std::vector< bool > > Adjacency
an adjacency
Definition Problems.h:83