|
ADS
Algorithms and Data Structures
|
The linear partition problem (also the painter's partition, or the bottleneck partition of a chain). More...
Files | |
| file | ADS/optimization/LinearPartition.h |
| The linear partition problem (also the painter's partition, or the bottleneck partition of a chain): the items \( A[ 0 .. n - 1 ] \) split into \( k \) contiguous parts so that the largest sum of a part is the least, e.g., \( n \) boards of lengths \( A_i \) to be painted by \( k \) painters, each one a block of consecutive boards, in the least time. | |
Functions | |
| template<class T> | |
| std::pair< T, std::vector< std::size_t > > | ADS::linearPartition (const std::vector< T > &A, std::size_t k) |
| the least largest sum of a part over the splits of \( A \) in \( k \) nonempty contiguous parts, and the starts of the parts after the first one | |
| template<std::integral T> | |
| T | ADS::painterPartition (const std::vector< T > &A, std::size_t k) |
| the least largest sum of a part over the splits of \( A \) in at most \( k \) contiguous parts; 0 for \( A \) empty | |
The linear partition problem (also the painter's partition, or the bottleneck partition of a chain).
The definitions, the invariants and the costs of the area are in Theory of optimization.
| std::pair< T, std::vector< std::size_t > > ADS::linearPartition | ( | const std::vector< T > & | A, |
| std::size_t | k ) |
the least largest sum of a part over the splits of \( A \) in \( k \) nonempty contiguous parts, and the starts of the parts after the first one
In pseudocode (exercise B4 of 05/06/2014, Partition k, of the Esercizi Programmazione dinamica of the book), \( S \) the prefix sums and \( \mathit{cut}[ t ][ i ] \) the start of the last part of a best split of the first \( i \) items in \( t \) parts:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{linearPartition}( A , k )\quad \triangleright\ S \text{ the prefix sums, } D[ t ][ i ] \text{ the best of } A[ 0 .. i - 1 ] \text{ in } t \text{ parts} \cr 2 & \quad D[ 1 ][ i ] \gets S[ i ] \text{ for } i = 1 , \ldots , n \cr 3 & \quad \textbf{for}\ t \gets 2\ \textbf{to}\ k\ \textbf{do} \cr 4 & \qquad \textbf{for}\ i \gets t\ \textbf{to}\ n\ \textbf{do}\quad \triangleright\ \text{the last part is } A[ j .. i - 1 ] \cr 5 & \qquad\quad \mathit{cut}[ t ][ i ] \gets \text{the least } j \in [ t - 1 , i ) \text{ that minimizes } \max\{ D[ t - 1 ][ j ] , S[ i ] - S[ j ] \} \cr 6 & \qquad\quad D[ t ][ i ] \gets \text{that minimum} \cr 7 & \quad \textbf{return}\ D[ k ][ n ] \text{ and the starts of the parts, read back from } \mathit{cut}[ k ][ n ] \end{array}} \]
| T ADS::painterPartition | ( | const std::vector< T > & | A, |
| std::size_t | k ) |
the least largest sum of a part over the splits of \( A \) in at most \( k \) contiguous parts; 0 for \( A \) empty
In pseudocode (beyond the book, for the exercise B2 of 21/01/2019, Partition, of its Esercizi vari), the binary search on the answer, each candidate checked by the greedy:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fits}( A , k , x ) \cr 2 & \quad \mathit{parts} \gets 1;\ s \gets 0 \cr 3 & \quad \textbf{foreach}\ a \in A\ \textbf{do} \cr 4 & \qquad \textbf{if}\ s + a \le x\ \textbf{then}\ s \gets s + a \cr 5 & \qquad \textbf{else}\ \mathit{parts} \gets \mathit{parts} + 1;\ s \gets a;\ \textbf{if}\ \mathit{parts} > k\ \textbf{then return false} \cr 6 & \quad \textbf{return true} \cr 7 & \textbf{function}\ \mathrm{painterPartition}( A , k ) \cr 8 & \quad \mathit{lo} \gets \max_i A_i;\ \mathit{hi} \gets \sum_i A_i \cr 9 & \quad \textbf{while}\ \mathit{lo} < \mathit{hi}\ \textbf{do} \cr 10 & \qquad m \gets \lfloor ( \mathit{lo} + \mathit{hi} ) / 2 \rfloor \cr 11 & \qquad \textbf{if}\ \mathrm{fits}( A , k , m )\ \textbf{then} \ \mathit{hi} \gets m\ \textbf{else}\ \mathit{lo} \gets m + 1 \cr 12 & \quad \textbf{return}\ \mathit{lo} \end{array}} \]