|
ADS
Algorithms and Data Structures
|
The general schemes of the techniques of design of algorithms, beyond the book, as functions of the parts that make a technique concrete. More...
Files | |
| file | ADS/algorithm/Paradigms.h |
| The general schemes of the techniques of design of algorithms, beyond the book, as functions of the parts that make a technique concrete. | |
Functions | |
| template<class Z, class Next, class Last, class A, class O> | |
| auto | ADS::enumerateSearch (Z z, Next &&next, Last &&last, A &&a, O &&o) -> std::optional< decltype(o(z)) > |
| the enumerative search from the first object \( z \): the answer \( o( z ) \) of the first \( z \) with \( a( z ) \), next( z ) after \( z \) until last( z ) holds; nothing if none | |
| template<class Z, class Next, class Last, class A, class O, class M, class B> | |
| auto | ADS::enumerateOptimize (Z z, Next &&next, Last &&last, A &&a, O &&o, M &&m, B &&better) -> std::optional< decltype(o(z)) > |
| the enumerative optimization: the answer of an admissible object of \( Z \) with no admissible one better, i.e., with no \( y \) such that better( m( y ) , m( x ) ), or nothing | |
| template<class N, class C, class P, class L, class V> | |
| bool | ADS::backtrack (const N &node, C &&children, P &&prune, L &&leaf, V &&visit) |
| the backtracking from the node \( N \): prune( N ) rejects \( N \) and its subtree, a leaf goes to visit( N ), which returns true to stop, children( N ) gives the children of a node that is not a leaf; true if stopped | |
| template<class X, class Cand, class Best> | |
| std::vector< X > | ADS::greedy (int k, Cand &&candidates, Best &&best) |
| the greedy scheme: for each of \( k \) stages, the best of the values candidates( i , x ) admissible after the partial solution \( x \) is appended to \( x \), the stage skipped if there is none | |
| template<class X, class S, class D, class V, class C> | |
| auto | ADS::divideEtImpera (const X &x, S &&small, D &&direct, V &÷, C &&combine) -> decltype(direct(x)) |
| divide et impera: direct( x ) if small( x ), else the combination of the solutions of the instances that divide( x ) gives | |
| template<class T, class B> | |
| std::vector< T > | ADS::maximal (const std::vector< T > &X, B &&better) |
| the elements of \( X \) that are maximal for the partial order better( a , b ), i.e., \( a \) strictly better than \( b \) | |
The general schemes of the techniques of design of algorithms, beyond the book, as functions of the parts that make a technique concrete.
The definitions, the invariants and the costs of the area are in Theory of other algorithms.
| bool ADS::backtrack | ( | const N & | node, |
| C && | children, | ||
| P && | prune, | ||
| L && | leaf, | ||
| V && | visit ) |
the backtracking from the node \( N \): prune( N ) rejects \( N \) and its subtree, a leaf goes to visit( N ), which returns true to stop, children( N ) gives the children of a node that is not a leaf; true if stopped
In pseudocode (the scheme of chapter 16):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{backtrack}( N )\quad \triangleright \ \text{true when a visit asks to stop} \cr 2 & \quad \textbf{if}\ \mathrm{prune}( N )\ \textbf{then return false} \cr 3 & \quad \textbf{if}\ \mathrm{leaf}( N )\ \textbf{then return} \ \mathrm{visit}( N ) \cr 4 & \quad \textbf{return}\ \text{whether } \mathrm{backtrack}( C ) \text{ is true for a child } C \text{ of } N \text{, trying them in order} \end{array}} \]
| auto ADS::divideEtImpera | ( | const X & | x, |
| S && | small, | ||
| D && | direct, | ||
| V && | divide, | ||
| C && | combine ) -> decltype(direct(x)) |
divide et impera: direct( x ) if small( x ), else the combination of the solutions of the instances that divide( x ) gives
In pseudocode (the scheme of chapter 12):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{divideEtImpera}( x ) \cr 2 & \quad \textbf{if}\ \mathrm{small}( x )\ \textbf{then return} \ \mathrm{direct}( x )\quad \triangleright \ \text{the base of the recursion} \cr 3 & \quad \textbf{return}\ \mathrm{combine}( \text{the solutions } \mathrm{divideEtImpera}( y ) \text{ of the parts } y \in \mathrm{divide}( x ) \text{, in order} ) \end{array}} \]
| auto ADS::enumerateOptimize | ( | Z | z, |
| Next && | next, | ||
| Last && | last, | ||
| A && | a, | ||
| O && | o, | ||
| M && | m, | ||
| B && | better ) -> std::optional< decltype(o(z)) > |
the enumerative optimization: the answer of an admissible object of \( Z \) with no admissible one better, i.e., with no \( y \) such that better( m( y ) , m( x ) ), or nothing
In pseudocode (the enumerative method), the first best object kept:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{enumerateOptimize}( z ) \cr 2 & \quad b \gets \textbf{nil}\quad \triangleright \ \text{the best admissible candidate so far} \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{if}\ a( z )\ \textbf{and}\ ( b = \textbf{nil} \ \textbf{or}\ \mathrm{better}( m( z ) , m( b ) ) )\ \textbf{then}\ b \gets z \cr 5 & \qquad \textbf{if}\ \mathrm{last}( z )\ \textbf{then return}\ o( b ) \text{ if } b \ne \textbf{nil} \text{, else } \bot \cr 6 & \qquad z \gets \mathrm{next}( z ) \end{array}} \]
| auto ADS::enumerateSearch | ( | Z | z, |
| Next && | next, | ||
| Last && | last, | ||
| A && | a, | ||
| O && | o ) -> std::optional< decltype(o(z)) > |
the enumerative search from the first object \( z \): the answer \( o( z ) \) of the first \( z \) with \( a( z ) \), next( z ) after \( z \) until last( z ) holds; nothing if none
In pseudocode (the enumerative method):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{enumerateSearch}( z )\quad \triangleright \ z \text{ the first candidate} \cr 2 & \quad \textbf{while}\ \textbf{not}\ a( z )\ \textbf{do} \cr 3 & \qquad \textbf{if}\ \mathrm{last}( z )\ \textbf{then return} \ \bot\quad \triangleright\ \text{no admissible candidate} \cr 4 & \qquad z \gets \mathrm{next}( z ) \cr 5 & \quad \textbf{return}\ o( z ) \end{array}} \]
| std::vector< X > ADS::greedy | ( | int | k, |
| Cand && | candidates, | ||
| Best && | best ) |
the greedy scheme: for each of \( k \) stages, the best of the values candidates( i , x ) admissible after the partial solution \( x \) is appended to \( x \), the stage skipped if there is none
In pseudocode (the scheme of chapter 14):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{greedy}( k ) \cr 2 & \quad x \gets ()\quad \triangleright \ \text{the choices made, never undone} \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ k - 1\ \textbf{do} \cr 4 & \qquad C \gets \mathrm{candidates}( i , x );\ \textbf{if}\ C \ne \emptyset\ \textbf{then}\ \text{append } \mathrm{best}( C ) \text{ to } x \cr 5 & \quad \textbf{return}\ x \end{array}} \]
| std::vector< T > ADS::maximal | ( | const std::vector< T > & | X, |
| B && | better ) |
the elements of \( X \) that are maximal for the partial order better( a , b ), i.e., \( a \) strictly better than \( b \)
In pseudocode, by all the pairs, \( O( n^2 ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maximal}( X ) \cr 2 & \quad M \gets \text{the empty sequence} \cr 3 & \quad \textbf{foreach}\ x \in X\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \text{no } y \in X \text{ has } \mathrm{better}( y , x )\ \textbf{then}\ M \gets M \cdot ( x ) \cr 5 & \quad \textbf{return}\ M \end{array}} \]