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

The segments (subarrays) of largest sum and their variants, of chapter 2 (A Warm-up) of the Pearls of Ferragina: the gains of a stock as the differences of its quotations, and the regions of a DNA sequence rich in C and G. More...

#include "ADS/Contract.h"
#include "ADS/Wide.h"
#include "ADS/algorithm/Subsequences.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <string>
#include <type_traits>
#include <vector>
#include "ADS/algorithm/impl/MaximumSegments.tpp"

Go to the source code of this file.

Classes

struct  ADS::Segment< T >
 a segment \( D[ f .. f + \ell - 1 ] \) of the array and its sum, with \( f \) the place first and \( \ell \) the length More...

Namespaces

namespace  ADS

Functions

template<class T>
Segment< T > ADS::maxSumSegmentCubic (const std::vector< T > &D)
 a nonempty segment of largest sum, by all the pairs, \( O( n^3 ) \)
template<class T>
Segment< T > ADS::maxSumSegmentQuadratic (const std::vector< T > &D)
 a nonempty segment of largest sum, by all the pairs, \( O( n^2 ) \)
template<class T>
Segment< T > ADS::maxSumSegment (const std::vector< T > &D)
 a nonempty segment of largest sum, by one scan, \( O( n ) \)
template<class T>
Segment< T > ADS::maxSumSegmentPrefix (const std::vector< T > &D)
 a nonempty segment of largest sum, by the minimum prefix sum, \( O( n ) \)
template<class T>
Segment< T > ADS::maxSumSegmentLength (const std::vector< T > &D, std::size_t L, std::size_t U)
 a segment of largest sum among those of length in \( [ L , U ] \), \( O( n ) \)
template<class T>
Segment< T > ADS::longestSegmentAtLeast (const std::vector< T > &D, T t)
 the longest segment of sum at least \( t \), length 0 if none, \( O( n ) \)
template<class T>
Segment< T > ADS::longestSegmentAverageAtLeast (const std::vector< T > &D, T p, T q)
 the longest segment of average at least \( p / q \), length 0 if none
template<class T>
Segment< T > ADS::maxDensitySegment (const std::vector< T > &D, std::size_t L)
 a segment of largest average among those of length at least \( L \), its sum in the result, \( O( n \log n ) \)
std::vector< int > ADS::gcIndicator (const std::string &dna)
 1 for the nucleotides C and G, in either case, and 0 for the others

Detailed description

The segments (subarrays) of largest sum and their variants, of chapter 2 (A Warm-up) of the Pearls of Ferragina: the gains of a stock as the differences of its quotations, and the regions of a DNA sequence rich in C and G.

The array is \( D[ 0 .. n - 1 ] \), and \( P[ i ] = D[ 0 ] + \cdots + D[ i - 1 ] \) are its prefix sums, so that the segment \( D[ b .. s ] \) sums to \( P[ s + 1 ] - P[ b ] \).

  • maxSumSegmentCubic() and maxSumSegmentQuadratic() are the cubic-time and the quadratic-time algorithms of that chapter, all the pairs \( ( b , s ) \), the sum recomputed or extended, \( \Theta( n^3 ) \) and \( \Theta( n^2 ) \);
  • maxSumSegment() is its linear-time algorithm, the scan of Kadane with the positions: a prefix of negative sum is dropped, since no optimum starts after a segment of positive sum nor has a prefix of negative sum (two properties of that chapter), \( O( n ) \) time, one pass, \( \lceil n / B \rceil \) I/Os whatever \( B \) (cache oblivious). maxSubarraySum() of Subsequences.h is the same scan for the value only, the empty segment allowed; here the segment is not empty, so that an array of negative numbers gives its largest one;
  • maxSumSegmentPrefix() is its other linear-time algorithm: the best segment ending in \( s \) is \( P[ s + 1 ] - \min_{b \le s} P[ b ] \), the minimum carried along the scan;
  • maxSumSegmentLength() bounds the length to \( [ L , U ] \): the minimum of \( P \) over the window of the admissible starts, by a deque of increasing prefix sums (the sliding minimum), \( O( n ) \), the algorithm of Lin, Jiang and Chao;
  • longestSegmentAtLeast() is the algorithm of its variants for the longest segment of sum at least \( t \): at step \( i \) only the starts left of \( L_i = i - \ell \) can beat the current length \( \ell \), and among them only the chain of leftmost minima \( C_1 = \mathit{LMin}[ L_i ] \), \( C_2 = \mathit{LMin}[ C_1 ] \), \( \ldots \), whose prefix sums increase; the chain is walked while the sum stays at least \( t \), and every step past the first lengthens the solution, hence \( O( n ) \) amortized. longestSegmentAverageAtLeast() reduces the longest segment of average at least \( p / q \) to it, by the items \( q D[ k ] - p \);
  • maxDensitySegment() is the segment of largest average among those of length at least \( L \): the average of \( D[ x .. i - 1 ] \) is the slope from \( ( x , P[ x ] ) \) to \( ( i , P[ i ] ) \), and the best \( x \) for \( i \) lies on the lower convex hull of the points \( x \le i - L \), kept by the monotone chain as \( i \) grows, the tangent found by a binary search, \( O( n \log n ) \). Goldwasser, Kao and Lu, and Chung and Lu, reach \( O( n ) \) by walking the hull with a pointer that moves only forward under a further argument (and also handle an upper bound on the length); the binary search keeps the code short and exact on integers;
  • gcIndicator() maps a DNA sequence to 1 for C and G and 0 otherwise, the input of the two biological measures of the Pearls: \( x - p \ell \) (a segment sum of the scores \( 1 - p \) and \( -p \), e.g., \( q D[ k ] - p \) for \( p / q \)) and the density \( x / \ell \), \( x \) the count of C and G of a segment of length \( \ell \).

A Segment holds the sum of \( D[ \mathit{first} .. \mathit{first} + \mathit{length} - 1 ] \), length 0 for none. The contracts [see Contract.h]: \( D \) not empty where a segment must exist, \( 1 \le L \le U \), \( L \le n \), \( q > 0 \); the sum of a segment is that of its items, and maxSumSegment() agrees with maxSubarraySum() when its sum is not negative, audits. The sums of the segments and of the prefixes of \( D \) must fit in T, which is not checked (an item of T alone may be its largest or its smallest value), and so must those of the items \( q D[ k ] - p \) in longestSegmentAverageAtLeast(); the products of a sum and of a length in maxDensitySegment() are taken on the integers of 64 bits for an integer T of at most 32 and on those of 128 bits for a wider one, so that they do not overflow. The functions are defined in MaximumSegments.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 2 (A Warm-up), doi:10.1017/9781009128933
J. Bentley, Programming pearls: algorithm design techniques, Communications of the ACM 27(9), 865-873, 1984, doi:10.1145/358234.381162
Y.-L. Lin, T. Jiang, K.-M. Chao, Efficient algorithms for locating the length-constrained heaviest segments with applications to biomolecular sequence analysis, Journal of Computer and System Sciences 65(3), 570-586, 2002, doi:10.1016/S0022-0000(02)00010-7
M. H. Goldwasser, M.-Y. Kao, H.-I. Lu, Linear-time algorithms for computing maximum-density sequence segments with bioinformatics applications, Journal of Computer and System Sciences 70(2), 128-144, 2005, doi:10.1016/j.jcss.2004.08.001
K.-M. Chung, H.-I. Lu, An optimal algorithm for the maximum-density segment problem, SIAM Journal on Computing 34(2), 373-387, 2005, doi:10.1137/S0097539704440430
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa