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

The segments (subarrays) of largest sum and their variants, of chapter 2 (A Warm-up) of the Pearls of Ferragina. More...

Files

file  ADS/algorithm/MaximumSegments.h
 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.

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

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 definitions, the invariants and the costs of the area are in Theory of other algorithms.

Function documentation

◆ gcIndicator()

std::vector< int > ADS::gcIndicator ( const std::string & dna)
inlinenodiscard

1 for the nucleotides C and G, in either case, and 0 for the others

◆ longestSegmentAtLeast()

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 ) \)

In pseudocode (the variants of chapter 2 of the Pearls), \( \mathit{LMin}[ x ] \) the leftmost place of the minimum of \( P[ 0 .. x - 1 ] \) and \( \ell \) the length of the best segment so far:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestSegmentAtLeast}( D , t ) \cr 2 & \quad \ell \gets 0;\ \mathit{best} \gets \text{none} \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \ell \ge i\ \textbf{then continue} \cr 5 & \qquad c \gets \mathit{LMin}[ i - \ell ];\ f \gets \textbf{nil} \cr 6 & \qquad \textbf{while}\ P[ i ] - P[ c ] \ge t\ \textbf{do}\quad \triangleright\ \text{the chain of the leftmost minima} \cr 7 & \qquad\quad f \gets c \cr 8 & \qquad\quad \textbf{if}\ c = 0\ \textbf{then}\ \textbf{break} \cr 9 & \qquad\quad c \gets \mathit{LMin}[ c ] \cr 10 & \qquad \textbf{if}\ f \ne \textbf{nil}\ \textbf{then}\ \mathit{best} \gets ( P[ i ] - P[ f ] , f , i - f );\ \ell \gets i - f \cr 11 & \quad \textbf{return}\ \mathit{best} \end{array}} \]

Postcondition
its sum is that of its items and at least \( t \) (audit)

◆ longestSegmentAverageAtLeast()

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

It is longestSegmentAtLeast() on the items \( q D[ k ] - p \), whose segments have a sum at least 0 exactly when the average of the same segment of \( D \) is at least \( p / q \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestSegmentAverageAtLeast}( D , p , q ) \cr 2 & \quad S \gets \mathrm{longestSegmentAtLeast}( ( q D[ k ] - p )_{k=0}^{n - 1} , 0 ) \cr 3 & \quad \textbf{return}\ S \text{ with the sum of its items in } D \end{array}} \]

Precondition
\( q > 0 \)

◆ maxDensitySegment()

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 ) \)

In pseudocode (beyond the Pearls, after Goldwasser, Kao and Lu), the average of \( D[ x .. i - 1 ] \) being the slope from \( ( x , P[ x ] ) \) to \( ( i , P[ i ] ) \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxDensitySegment}( D , L ) \cr 2 & \quad H \gets \text{an empty stack};\ \mathit{best} \gets \text{none} \cr 3 & \quad \textbf{for}\ i \gets L\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad x \gets i - L \cr 5 & \qquad \textbf{while}\ |H| \ge 2 \text{ and the last two of } H \text{ and } x \text{ turn not left}\ \textbf{do}\ \mathrm{pop}( H ) \cr 6 & \qquad \mathrm{push}( H , x )\quad \triangleright\ \text{the lower } \text{hull of the starts} \cr 7 & \qquad b \gets \text{the vertex of } H \text{ where the tangent from } ( i , P[ i ] ) \text{ touches} \cr 8 & \qquad\quad \triangleright\ \text{by a binary search on the turns} \cr 9 & \qquad \textbf{if}\ ( P[ i ] - P[ b ] ) / ( i - b ) \text{ beats } \mathit{best}\ \textbf{then}\ \mathit{best} \gets ( P[ i ] - P[ b ] , b , i - b ) \cr 10 & \quad \textbf{return}\ \mathit{best} \end{array}} \]

Precondition
\( 1 \le L \le n \)

◆ maxSumSegment()

template<class T>
Segment< T > ADS::maxSumSegment ( const std::vector< T > & D)

a nonempty segment of largest sum, by one scan, \( O( n ) \)

In pseudocode (the linear-time algorithm of chapter 2 of the Pearls, the scan of Kadane), a new start after each prefix of negative sum:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxSumSegment}( D[ 0 .. n - 1 ] ) \cr 2 & \quad \mathit{best} \gets ( D[ 0 ] , 0 , 1 );\ x \gets 0;\ b \gets 0 \cr 3 & \quad \textbf{for}\ s \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad x \gets x + D[ s ] \cr 5 & \qquad \textbf{if}\ x > \mathit{best}.\mathit{sum}\ \textbf{then} \ \mathit{best} \gets ( x , b , s - b + 1 ) \cr 6 & \qquad \textbf{if}\ x < 0\ \textbf{then}\ x \gets 0;\ b \gets s + 1 \cr 7 & \quad \textbf{return}\ \mathit{best} \end{array}} \]

Precondition
\( D \) is not empty
Postcondition
its sum is maxSubarraySum( D ) if not negative (audit)

◆ maxSumSegmentCubic()

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 ) \)

In pseudocode (the cubic-time algorithm of chapter 2 of the Pearls):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxSumSegmentCubic}( D[ 0 .. n - 1 ] ) \cr 2 & \quad \mathit{best} \gets ( D[ 0 ] , 0 , 1 ) \cr 3 & \quad \textbf{for}\ b \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad \textbf{for}\ s \gets b\ \textbf{to}\ n - 1\ \textbf{do} \cr 5 & \qquad\quad x \gets D[ b ] + \cdots + D[ s ]\quad \triangleright \ \text{a loop of } s - b + 1 \text{ steps} \cr 6 & \qquad\quad \textbf{if}\ x > \mathit{best}.\mathit{sum}\ \textbf{then} \ \mathit{best} \gets ( x , b , s - b + 1 ) \cr 7 & \quad \textbf{return}\ \mathit{best} \end{array}} \]

Precondition
\( D \) is not empty

◆ maxSumSegmentLength()

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 ) \)

In pseudocode (the sliding minimum of Lin, Jiang and Chao), the deque \( W \) holding the admissible starts by increasing \( P \), so that its head is the best start for the end \( i \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxSumSegmentLength}( D , L , U ) \cr 2 & \quad W \gets \text{an empty deque};\ \mathit{best} \gets \text{none} \cr 3 & \quad \textbf{for}\ i \gets L\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad x \gets i - L\quad \triangleright\ \text{the new admissible } \text{start} \cr 5 & \qquad \textbf{while}\ W \ne \emptyset\ \textbf{and}\ P[ \mathrm{tail}( W ) ] \ge P[ x ]\ \textbf{do}\ \text{remove the tail of } W \cr 6 & \qquad \text{append } x \text{ to } W \cr 7 & \qquad \textbf{while}\ \mathrm{head}( W ) < i - U\ \textbf{do} \ \text{remove the head of } W \cr 8 & \qquad b \gets \mathrm{head}( W ) \cr 9 & \qquad \textbf{if}\ P[ i ] - P[ b ] > \mathit{best}.\mathit{sum} \ \textbf{then}\ \mathit{best} \gets ( P[ i ] - P[ b ] , b , i - b ) \cr 10 & \quad \textbf{return}\ \mathit{best} \end{array}} \]

Precondition
\( 1 \le L \le U \) and \( L \le n \)

◆ maxSumSegmentPrefix()

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 ) \)

In pseudocode (the other linear-time algorithm of chapter 2 of the Pearls), \( x = P[ s + 1 ] \) and \( m \) the least \( P[ b ] \), \( b \le s \), at the place \( b_m \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxSumSegmentPrefix}( D[ 0 .. n - 1 ] ) \cr 2 & \quad \mathit{best} \gets ( D[ 0 ] , 0 , 1 );\ x \gets 0;\ m \gets 0; \ b_m \gets 0 \cr 3 & \quad \textbf{for}\ s \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad x \gets x + D[ s ] \cr 5 & \qquad \textbf{if}\ x - m > \mathit{best}.\mathit{sum}\ \textbf{then} \ \mathit{best} \gets ( x - m , b_m , s - b_m + 1 ) \cr 6 & \qquad \textbf{if}\ x < m\ \textbf{then}\ m \gets x;\ b_m \gets s + 1 \cr 7 & \quad \textbf{return}\ \mathit{best} \end{array}} \]

Precondition
\( D \) is not empty

◆ maxSumSegmentQuadratic()

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 ) \)

In pseudocode (the quadratic-time algorithm of chapter 2 of the Pearls), the sum of \( D[ b .. s ] \) extended from that of \( D[ b .. s - 1 ] \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxSumSegmentQuadratic}( D[ 0 .. n - 1 ] ) \cr 2 & \quad \mathit{best} \gets ( D[ 0 ] , 0 , 1 ) \cr 3 & \quad \textbf{for}\ b \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad x \gets 0 \cr 5 & \qquad \textbf{for}\ s \gets b\ \textbf{to}\ n - 1\ \textbf{do} \cr 6 & \qquad\quad x \gets x + D[ s ] \cr 7 & \qquad\quad \textbf{if}\ x > \mathit{best}.\mathit{sum}\ \textbf{then} \ \mathit{best} \gets ( x , b , s - b + 1 ) \cr 8 & \quad \textbf{return}\ \mathit{best} \end{array}} \]

Precondition
\( D \) is not empty