|
ADS
Algorithms and Data Structures
|
The pseudopolynomial algorithms of section 19.1 and of the exercises 19.1 to 19.5. More...
Files | |
| file | ADS/intractable/Pseudopolynomial.h |
| The pseudopolynomial algorithms of section 19.1 and of the exercises 19.1 to 19.5. | |
Classes | |
| struct | ADS::Piece |
| an interval \( [ \mathit{start} , \mathit{end} ) \) of the program on a processor More... | |
Functions | |
| template<class Bits = std::vector< bool >> | |
| bool | ADS::subsetSumDP (const std::vector< long > &A, long k, std::vector< std::vector< bool > > *rows=nullptr) |
| whether some elements of \( A \) sum to \( k \) (false if \( k < 0 \)), by the vector \( M \) with \( M[ j ] \) true if some of \( A[ 0 .. i ] \) sum to \( j \); the vector \( M \) after each element goes to *rows, if given, the first one before any; \( O( n k ) \) time; \( M \) is a vector of Bits, std::vector< bool > by default, or boost::dynamic_bitset, or BitVector of ADS [see BitVector.h], with the same cells written | |
| std::optional< Nodes > | ADS::subsetSumSet (const std::vector< long > &A, long k) |
| the places of a subset of \( A \) of sum \( k \), by the table of all the rows, \( O( n k ) \) time and space, or nothing | |
| std::optional< Nodes > | ADS::partitionDP (const std::vector< long > &A) |
| PARTITION by dynamic programming: the places of a half of \( A \) of half the sum, or nothing (exercise 19.1). | |
| std::optional< std::vector< int > > | ADS::schedulingDP (const std::vector< long > &t, int m, long d) |
| MULTIPROCESSOR SCHEDULING by dynamic programming on the vectors of the loads of the \( m \) processors, each at most \( d \): the processor of each program, or nothing (also if \( d < 0 \), even with no programs, since an idle processor is loaded 0); \( O( n ( d + 1 )^m ) \) time (exercise 19.2). | |
| std::pair< double, std::vector< std::vector< Piece > > > | ADS::mcNaughton (const std::vector< double > &t, int m) |
| the rule of McNaughton: the length \( d \) of the schedule and the pieces of each processor; the programs fill a processor up to \( d \) and go on at time 0 of the next one, and the two pieces of a program do not overlap because \( t[ i ] \le d \); \( O( n + m ) \) time (exercise 19.3) | |
| std::optional< std::vector< std::vector< Piece > > > | ADS::realTimeScheduling (const std::vector< double > &C, const std::vector< double > &T, int m) |
| REAL-TIME SCHEDULING without priority rule: the tasks of time \( C[ i ] \) every \( T[ i ] \) run on \( m \) processors if and only if \( C[ i ] \le T[ i ] \) and \( \sum_i C_i / T_i \le m \); the schedule of a period of length 1, task \( i \) for the fraction \( C[ i ] / T[ i ] \) of it, or nothing (exercise 19.4). | |
| std::optional< std::vector< int > > | ADS::bottino (const std::vector< long > &P) |
| the thief of each jewel of price \( P[ i ] \), so that the three thieves take \( m / 3 \) each, or nothing; \( S[ i ][ j ][ h ] \) tells whether the first \( i \) jewels give \( j \) to the first and \( h \) to the second thief, and the prices are not negative, so that \( j \) and \( h \) range in \( [ 0 , m / 3 ] \); \( O( n m^2 ) \) time (exercise 19.5) | |
The pseudopolynomial algorithms of section 19.1 and of the exercises 19.1 to 19.5.
The definitions, the invariants and the costs of the area are in Theory of intractable problems.
|
inline |
the thief of each jewel of price \( P[ i ] \), so that the three thieves take \( m / 3 \) each, or nothing; \( S[ i ][ j ][ h ] \) tells whether the first \( i \) jewels give \( j \) to the first and \( h \) to the second thief, and the prices are not negative, so that \( j \) and \( h \) range in \( [ 0 , m / 3 ] \); \( O( n m^2 ) \) time (exercise 19.5)
In pseudocode (exercise 19.5), the third thief taking what is left:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{bottino}( P )\quad \triangleright\ S[ i ][ j ][ h ] \text{: the first } i \text{ items give } j \text{ to one thief and } h \text{ to another} \cr 2 & \quad m \gets \textstyle\sum_i P[ i ];\ \textbf{if}\ 3 \nmid m \ \textbf{then return nil} \cr 3 & \quad S[ 0 ][ j ][ h ] \gets ( j = h = 0 ) \cr 4 & \quad \textbf{foreach}\ i \in [ 0 , n ) \text{ and } j , h \in [ 0 , m / 3 ]\ \textbf{do}\quad \triangleright\ \text{the item } i \text{ to the third thief, the first or the second} \cr 5 & \qquad S[ i + 1 ][ j ][ h ] \gets S[ i ][ j ][ h ] \text{ or } S[ i ][ j - P[ i ] ][ h ] \text{ or } S[ i ][ j ][ h - P[ i ] ] \cr 6 & \quad \textbf{return}\ \textbf{nil} \text{ if not } S[ n ][ m / 3 ][ m / 3 ] \text{, else the thief of each item, read back from it} \end{array}} \]
|
inline |
the rule of McNaughton: the length \( d \) of the schedule and the pieces of each processor; the programs fill a processor up to \( d \) and go on at time 0 of the next one, and the two pieces of a program do not overlap because \( t[ i ] \le d \); \( O( n + m ) \) time (exercise 19.3)
In pseudocode (exercise 19.3), \( d \) the least length possible, since no program is shorter than its time and the work cannot be done faster than in \( \sum_i t_i / m \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{mcNaughton}( t , m )\quad \triangleright \ d \text{ the optimum, a lower bound met} \cr 2 & \quad d \gets \max\{ \max_i t_i , \textstyle\sum_i t_i / m \};\ q \gets 0;\ \mathit{now} \gets 0\quad \triangleright \ \text{the processor and the time filled} \cr 3 & \quad \textbf{foreach}\ \text{job } i \text{, in order}\ \textbf{do} \cr 4 & \qquad r \gets t_i \cr 5 & \qquad \textbf{while}\ r > 0 \text{ and } q < m\ \textbf{do}\quad \triangleright\ \text{a job split ends on } q \text{ and restarts on } q + 1 \cr 6 & \qquad\quad s \gets \min\{ r , d - \mathit{now} \};\ \text{give } i \text{ the time } [ \mathit{now} , \mathit{now} + s ) \text{ of } q \cr 7 & \qquad\quad r \gets r - s;\ \mathit{now} \gets \mathit{now} + s; \ \textbf{if}\ \mathit{now} = d\ \textbf{then}\ ( q , \mathit{now} ) \gets ( q + 1 , 0 ) \cr 8 & \quad \textbf{return}\ ( d , \text{the pieces} ) \end{array}} \]
|
inline |
PARTITION by dynamic programming: the places of a half of \( A \) of half the sum, or nothing (exercise 19.1).
In pseudocode (exercise 19.1):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{partitionDP}( A )\quad \triangleright \ \text{two halves of equal sum} \cr 2 & \quad s \gets \textstyle\sum_i A[ i ] \cr 3 & \quad \textbf{return}\ \textbf{nil} \text{ if } s \text{ is odd, else } \mathrm{subsetSumSet}( A , s / 2 ) \end{array}} \]
|
inline |
REAL-TIME SCHEDULING without priority rule: the tasks of time \( C[ i ] \) every \( T[ i ] \) run on \( m \) processors if and only if \( C[ i ] \le T[ i ] \) and \( \sum_i C_i / T_i \le m \); the schedule of a period of length 1, task \( i \) for the fraction \( C[ i ] / T[ i ] \) of it, or nothing (exercise 19.4).
In pseudocode (exercise 19.4):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{realTimeScheduling}( C , T , m )\quad \triangleright\ \text{the utilizations as the lengths of the jobs} \cr 2 & \quad ( d , P ) \gets \mathrm{mcNaughton}( ( C_i / T_i : i = 1 , \ldots , n ) , m ) \cr 3 & \quad \textbf{return}\ P \text{ if } d \le 1 \text{, else } \textbf{nil} \end{array}} \]
|
inline |
MULTIPROCESSOR SCHEDULING by dynamic programming on the vectors of the loads of the \( m \) processors, each at most \( d \): the processor of each program, or nothing (also if \( d < 0 \), even with no programs, since an idle processor is loaded 0); \( O( n ( d + 1 )^m ) \) time (exercise 19.2).
In pseudocode (exercise 19.2), \( L_i \) the vectors of the loads reachable with the first \( i \) programs, each with the vector it comes from and the processor of the \( i \)-th program:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{schedulingDP}( t , m , d )\quad \triangleright\ L_i \text{ the loads of the } m \text{ processors after } i \text{ jobs} \cr 2 & \quad \textbf{if}\ d < 0\ \textbf{then return nil} \cr 3 & \quad L_0 \gets \{ ( 0 , \ldots , 0 ) \} \cr 4 & \quad L_{i+1} \gets \{ x + t_i e_q : x \in L_i \text{, } x_q + t_i \le d \} \text{ for } i = 0 , \ldots , n - 1\quad \triangleright \ \text{a set: no copies} \cr 5 & \quad \textbf{return}\ \textbf{nil} \text{ if } L_n = \emptyset \text{, else the processor of each job, read back from a vector of } L_n \end{array}} \]
| bool ADS::subsetSumDP | ( | const std::vector< long > & | A, |
| long | k, | ||
| std::vector< std::vector< bool > > * | rows = nullptr ) |
whether some elements of \( A \) sum to \( k \) (false if \( k < 0 \)), by the vector \( M \) with \( M[ j ] \) true if some of \( A[ 0 .. i ] \) sum to \( j \); the vector \( M \) after each element goes to *rows, if given, the first one before any; \( O( n k ) \) time; \( M \) is a vector of Bits, std::vector< bool > by default, or boost::dynamic_bitset, or BitVector of ADS [see BitVector.h], with the same cells written
In pseudocode (section 19.1), one row of the table overwritten, from the right so that each element is used once:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{subsetSumDP}( A , k )\quad \triangleright \ M[ j ] \text{: some subset of the items seen sums to } j \cr 2 & \quad \textbf{if}\ k < 0\ \textbf{then return false} \cr 3 & \quad M \gets ( \textbf{true} , \textbf{false} , \ldots , \textbf{false} ) \text{, of } k + 1 \text{ entries} \cr 4 & \quad \textbf{foreach}\ a \in A\ \textbf{do}\ M[ j ] \gets M[ j ] \text{ or } M[ j - a ] \text{ for } j = k , k - 1 , \ldots , a\quad \triangleright\ \text{downwards: } a \text{ once} \cr 5 & \quad \textbf{return}\ M[ k ] \end{array}} \]
|
inline |
the places of a subset of \( A \) of sum \( k \), by the table of all the rows, \( O( n k ) \) time and space, or nothing
In pseudocode (section 19.1), the subset read backwards from the table, an element needed when the row above has not the sum:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{subsetSumSet}( A , k )\quad \triangleright\ M[ i ][ j ] \text{ for the first } i \text{ items} \cr 2 & \quad \textbf{if}\ k < 0\ \textbf{then return nil} \cr 3 & \quad M[ 0 ][ j ] \gets ( j = 0 ) \text{ for every } j \cr 4 & \quad M[ i + 1 ][ j ] \gets M[ i ][ j ] \text{ or } ( A[ i ] \le j \text{ and } M[ i ][ j - A[ i ] ] ) \text{, for } i < n \text{ and } j \le k \cr 5 & \quad \textbf{if}\ \textbf{not}\ M[ n ][ k ] \ \textbf{then return nil} \cr 6 & \quad S \gets \emptyset;\ j \gets k \cr 7 & \quad \textbf{for}\ i \gets n\ \textbf{downto}\ 1\ \textbf{do}\quad \triangleright\ \text{the item } i - 1 \text{ is needed iff } j \text{ was not reachable without it} \cr 8 & \qquad \textbf{if}\ \textbf{not}\ M[ i - 1 ][ j ]\ \textbf{then}\ S \gets S \cup \{ i - 1 \};\ j \gets j - A[ i - 1 ] \cr 9 & \quad \textbf{return}\ S \end{array}} \]