ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LinearPartition.h File Reference

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. More...

#include "ADS/Contract.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <utility>
#include <vector>
#include "ADS/optimization/impl/LinearPartition.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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

Detailed description

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.

  • linearPartition() solves it for items of any sign and exactly \( k \) nonempty parts by the dynamic programming on the prefix sums \( S \), \( D[ t ][ i ] = \min_{t - 1 \le j < i} \max( D[ t - 1 ][ j ] , S[ i ] - S[ j ] ) \), \( D[ 1 ][ i ] = S[ i ] \), with the best cut kept for each entry; \( O( k n^2 ) \) time and \( O( k n ) \) space;
  • painterPartition() solves it for integer items not negative and at most \( k \) parts by a binary search on the answer \( x \): the greedy that closes a part only when the next item does not fit in \( x \) uses the fewest parts, so \( x \) is feasible if and only if the greedy needs at most \( k \) of them; \( O( n \log \sum_i A_i ) \) time and \( O( 1 ) \) space. With items not negative and \( k \le n \), at most \( k \) parts and exactly \( k \) nonempty ones give the same value.

In the literature, the problem on a chain of \( n \) items is solved in \( O( n ) \) time for \( k \) fixed, and in \( O( n + k^2 \log^2 n ) \) in general, by parametric search on the prefix sums (Frederickson); the dynamic programming above has the quadrangle inequality, so that the monotone cuts of Knuth and Yao bring it to \( O( k n ) \) for items not negative.

The contracts [see Contract.h]: \( 1 \le k \le n \) for linearPartition(), and its cuts give the cost found (checked at level 3); \( k \ge 1 \) and the items not negative for painterPartition(). The functions are defined in LinearPartition.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 13, the Esercizi Programmazione dinamica, exercise B4 of 05/06/2014, and the Esercizi vari, exercise B2 of 21/01/2019, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
S. S. Skiena, The Algorithm Design Manual, 3rd ed., Springer, 2020, section 10.7
G. N. Frederickson, Optimal algorithms for tree partitioning, Proc. 2nd ACM-SIAM Symposium on Discrete Algorithms, 168-177, 1991
F. F. Yao, Efficient dynamic programming using quadrangle inequalities, Proc. 12th ACM Symposium on Theory of Computing, 429-435, 1980, doi:10.1145/800141.804691
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa