|
ADS
Algorithms and Data Structures
|
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 | |
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 ] \).
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.