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

Two problems on a sequence \( A[ 0 .. n - 1 ] \) solved by the dynamic programming of chapter 13: More...

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <vector>
#include "ADS/algorithm/impl/Subsequences.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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

Detailed description

Two problems on a sequence \( A[ 0 .. n - 1 ] \) solved by the dynamic programming of chapter 13:

  • the longest increasing subsequence (exercise 13.6): \( L[ i ] \), the length of the longest one that ends in \( A[ i ] \), is 1 plus the largest \( L[ j ] \), \( j < i \), with \( A[ j ] < A[ i ] \), \( O( n^2 ) \) (lis()); lisLength() keeps, for each length \( \ell \), the least item that ends an increasing subsequence of length \( \ell \), an increasing vector, and puts each item in place by a binary search, \( O( n \log n ) \);
  • the subvector of largest sum (exercises 12.2 and 13.12): \( t_i \), the best sum of a subvector ending in \( A[ i ] \), is \( \max\{ t_{i-1} + A[ i ] , 0 \} \), the empty subvector of sum 0 allowed, \( O( n ) \) (the scan of Kadane, maxSubarraySum()).

The contracts [see Contract.h]: lis() gives a strictly increasing subsequence of \( A \), as long as lisLength() says, an audit; maxSubarraySum() is never negative, the empty subvector counting 0. The functions are defined in Subsequences.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, exercises 13.6 and 13.12, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
M. L. Fredman, On computing the length of longest increasing subsequences, Discrete Mathematics 11(1), 29-35, 1975, doi:10.1016/0012-365X(75)90103-X
J. Bentley, Programming pearls: algorithm design techniques, Communications of the ACM 27(9), 865-873, 1984, doi:10.1145/358234.381162
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa