|
ADS
Algorithms and Data Structures
|
The problems in NP of chapter 18, each with its polynomial certificate, which checks a proposed solution, and an enumeration by backtrack, which decides the problem in exponential time and returns a solution if there is one. More...
#include "ADS/Contract.h"#include <algorithm>#include <array>#include <cstddef>#include <cstdlib>#include <numeric>#include <optional>#include <vector>#include "ADS/complexity/impl/Problems.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Typedefs | |
| using | ADS::Adjacency = std::vector< std::vector< bool > > |
| an adjacency | |
| using | ADS::Clause = std::vector< int > |
| some literals | |
| using | ADS::Cnf = std::vector< Clause > |
| a formula | |
| using | ADS::Nodes = std::vector< int > |
| a set, a path | |
Functions | |
| bool | ADS::cliqueCertificate (const Adjacency &G, const Nodes &S, int k) |
| whether \( S \), of distinct nodes, has \( k \) of them or more, all adjacent (the certificatoCricca() of the book) | |
| std::optional< Nodes > | ADS::clique (const Adjacency &G, int k) |
| \( k \) nodes all adjacent, or nothing (the enCricca() of the book) | |
| std::optional< Nodes > | ADS::independentSet (const Adjacency &G, int k) |
| \( k \) nodes no two adjacent, or nothing | |
| std::optional< Nodes > | ADS::vertexCover (const Adjacency &G, int k) |
| at most \( k \) nodes that touch every edge, or nothing | |
| std::optional< std::vector< int > > | ADS::coloring (const Adjacency &G, int k) |
| a coloring of the nodes with at most \( k \) colors, adjacent nodes of different colors, by backtrack, or nothing (exercise 18.6) | |
| std::optional< Nodes > | ADS::bisection (const Adjacency &G, int k) |
| a partition of the nodes in two halves cut by at most \( k \) edges, or nothing; the half of node 0 | |
| template<class T> | |
| bool | ADS::tspCertificate (const std::vector< std::vector< T > > &D, T k, const Nodes &P) |
| whether the tour \( P \), a permutation, is at most \( k \) long; the empty tour of no city is 0 long (the certificatoTsp() of the book) | |
| template<class T> | |
| std::optional< Nodes > | ADS::tsp (const std::vector< std::vector< T > > &D, T k) |
| a tour of length at most \( k \) from city 0, the partial tours longer than \( k \) pruned, or nothing (the enTsp() of the book) | |
| std::optional< Nodes > | ADS::hamiltonianCircuit (const Adjacency &G) |
| an unoriented circuit through every node once (\( n \ge 3 \)), or nothing | |
| std::optional< Nodes > | ADS::hamiltonianCycle (const Adjacency &G) |
| an oriented cycle through every node once (\( n \ge 2 \)), or nothing | |
| std::optional< Nodes > | ADS::hamiltonianPath (const Adjacency &G, int r=-1, int t=-1) |
| an oriented path through every node once, from \( r \) and to \( t \) if given | |
| std::optional< Nodes > | ADS::hamiltonianChain (const Adjacency &G) |
| an unoriented chain through every node once | |
| std::optional< Nodes > | ADS::longestPath (const Adjacency &G, int r, int t, int k) |
| a simple path from \( r \) to \( t \) of at least \( k \) edges, by backtrack | |
| int | ADS::longestPathDag (const Adjacency &G, int r, int t) |
| the most edges of a path from \( r \) to \( t \) in an acyclic oriented graph, \( -1 \) if \( t \) is not reachable; \( O( n^2 ) \) by the topological order | |
| std::optional< std::vector< std::pair< int, int > > > | ADS::boundedSpanningTree (const Adjacency &G, int k) |
| a spanning tree of the unoriented graph with at most \( k \) edges at each node, as \( n - 1 \) edges, by enumeration, or nothing | |
| bool | ADS::satCertificate (const Cnf &F, const std::vector< bool > &x) |
| whether \( x \) satisfies every clause of \( F \) | |
| std::optional< std::vector< bool > > | ADS::satisfiable (const Cnf &F, int n) |
| an assignment of the \( n \) variables that satisfies \( F \), by backtrack with a clause checked as soon as its variables are set, or nothing | |
| std::optional< std::vector< int > > | ADS::lp01 (const std::vector< std::vector< long > > &A, const std::vector< long > &b, int n) |
| a 0-1 vector \( x \) with \( A x \le b \), by enumeration, or nothing | |
| std::optional< Nodes > | ADS::subsetSumExists (const std::vector< long > &A, long k) |
| a set of the places of \( A \) of sum \( k \), by backtrack, or nothing | |
| std::optional< Nodes > | ADS::partition (const std::vector< long > &A) |
| the places of a half of \( A \) of half the sum, or nothing | |
| std::optional< Nodes > | ADS::knapsackDecision (const std::vector< long > &p, const std::vector< long > &v, long C, long k) |
| objects of volume at most \( C \) and profit at least \( k \) (the enZaino() of the book) | |
| std::optional< std::vector< int > > | ADS::multiprocessorScheduling (const std::vector< long > &t, int m, long d) |
| the processor of each program, so that \( m \) processors run them all within time \( d \), or nothing (also if \( d < 0 \), even with no programs, since an idle processor is loaded 0) | |
| std::optional< Nodes > | ADS::exactCover3 (const std::vector< std::array< int, 3 > > &Y, int m) |
| sets of three of \( \{ 0 , \ldots , m - 1 \} \) that partition it, by places, the ones that meet those chosen pruned, or nothing (the enCopertura() of the book) | |
| std::optional< Nodes > | ADS::threeDimensionalMatching (const std::vector< std::array< int, 3 > > &M, int q) |
| a matching of \( q \) triples \( ( w , x , v ) \) of \( M \) with distinct \( w \), \( x \) and \( v \), the three sets being \( [ 0 , q ) \), or nothing | |
| std::optional< std::vector< long > > | ADS::bribes (std::vector< long > M, const std::vector< bool > &ps) |
| the \( n \) bribes \( M \) to the \( n \) politicians, \( \mathit{ps}[ i ] \) if the \( i \)-th is of the party PS, so that the PS gets twice the party PDF; the bribe of each politician, or nothing (the mazzette() of the book) | |
The problems in NP of chapter 18, each with its polynomial certificate, which checks a proposed solution, and an enumeration by backtrack, which decides the problem in exponential time and returns a solution if there is one.
The graphs are adjacency matrices, symmetric when unoriented, whose diagonal (a loop of a node) every problem ignores: a node with a loop may be in an independent set, need not be in a cover and takes a color like any other; the formulae in conjunctive normal form are clauses of literals, \( +( v + 1 ) \) for the variable \( v \) and \( -( v + 1 ) \) for its negation; a system of 0-1 integer programming is \( A x \le b \).
The enumerations take, in the worst case, \( O( 2^n ) \) or \( O( n! ) \) times the polynomial cost of a node of the tree of the choices; their pruning changes the time in practice, not the bound.
The contracts [see Contract.h]: the graphs are square adjacency matrices, unoriented (symmetric) where the problem is, the literals name the variables of the formula, the numbers of the numerical problems are not negative; each certificate returned by an enumeration is checked as a postcondition. The operations are defined in Problems.tpp.