|
ADS
Algorithms and Data Structures
|
Two problems on a sequence \( A[ 0 .. n - 1 ] \) solved by the dynamic programming of chapter 13. More...
Files | |
| file | ADS/algorithm/Subsequences.h |
| Two problems on a sequence \( A[ 0 .. n - 1 ] \) solved by the dynamic programming of chapter 13: | |
Functions | |
| template<class T> | |
| std::vector< T > | ADS::lis (const std::vector< T > &A) |
| a longest increasing subsequence, \( O( n^2 ) \) | |
| template<class T> | |
| std::size_t | ADS::lisLength (const std::vector< T > &A) |
| the length of a longest increasing subsequence, \( O( n \log n ) \) | |
| template<class T> | |
| T | ADS::maxSubarraySum (const std::vector< T > &A) |
| the largest sum of a subvector, the empty one included, \( O( n ) \) | |
Two problems on a sequence \( A[ 0 .. n - 1 ] \) solved by the dynamic programming of chapter 13.
The definitions, the invariants and the costs of the area are in Theory of other algorithms.
| std::vector< T > ADS::lis | ( | const std::vector< T > & | A | ) |
a longest increasing subsequence, \( O( n^2 ) \)
In pseudocode (exercise 13.6), \( \mathit{prev}[ i ] \) the item before \( A[ i ] \) in the best subsequence ending there:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lis}( A[ 0 .. n - 1 ] )\quad \triangleright\ L[ i ] \text{ the longest ending at } A[ i ] \cr 2 & \quad \mathit{best} \gets 0 \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad L[ i ] \gets 1;\ \mathit{prev}[ i ] \gets \textbf{nil} \cr 5 & \qquad \textbf{for}\ j \gets 0\ \textbf{to}\ i - 1\ \textbf{do} \ \textbf{if}\ A[ j ] < A[ i ]\ \textbf{and}\ L[ i ] < L[ j ] + 1 \ \textbf{then}\ ( L[ i ] , \mathit{prev}[ i ] ) \gets ( L[ j ] + 1 , j ) \cr 6 & \qquad \textbf{if}\ L[ i ] > L[ \mathit{best} ]\ \textbf{then} \ \mathit{best} \gets i \cr 7 & \quad \textbf{return}\ \text{the chain } A[ \mathit{best} ] , A[ \mathit{prev}[ \mathit{best} ] ] , \ldots \text{, reversed} \end{array}} \]
| std::size_t ADS::lisLength | ( | const std::vector< T > & | A | ) |
the length of a longest increasing subsequence, \( O( n \log n ) \)
In pseudocode (Fredman, beyond the book), \( \mathit{tails}[ \ell ] \) the least item that ends an increasing subsequence of \( \ell + 1 \) items, an increasing vector:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lisLength}( A[ 0 .. n - 1 ] ) \cr 2 & \quad \mathit{tails} \gets \text{an empty vector} \cr 3 & \quad \textbf{foreach}\ a \in A\ \textbf{do} \cr 4 & \qquad \ell \gets \text{the first place with } \mathit{tails}[ \ell ] \ge a \text{, by binary search} \cr 5 & \qquad \textbf{if}\ \ell = |\mathit{tails}|\ \textbf{then} \ \text{append } a \text{ to } \mathit{tails}\ \textbf{else} \ \mathit{tails}[ \ell ] \gets a \cr 6 & \quad \textbf{return}\ |\mathit{tails}| \end{array}} \]
| T ADS::maxSubarraySum | ( | const std::vector< T > & | A | ) |
the largest sum of a subvector, the empty one included, \( O( n ) \)
In pseudocode (exercise 13.12), \( t \) the best sum of a subvector ending in the current item:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maxSubarraySum}( A[ 0 .. n - 1 ] )\quad \triangleright\ \text{the empty segment counts } 0 \cr 2 & \quad \mathit{best} \gets 0;\ t \gets 0\quad \triangleright\ t \text{ the best sum of a segment ending here} \cr 3 & \quad \textbf{foreach}\ a \in A\ \textbf{do}\ t \gets \max\{ 0 , t + a \};\ \mathit{best} \gets \max\{ t , \mathit{best} \} \cr 4 & \quad \textbf{return}\ \mathit{best} \end{array}} \]