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

Four classic problems on the subsequences of sequences \( X \), \( Y \), \( Z \) of lengths \( n \), \( m \), \( l \), relatives of the longest common subsequence [see LongestCommonSubsequence.h], each by dynamic programming on the prefixes. More...

#include "ADS/Contract.h"
#include "ADS/string/LongestCommonSubsequence.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <string>
#include <type_traits>
#include <vector>
#include "ADS/string/impl/SubsequenceProblems.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

template<class S>
requires std::is_class_v< S >
bool ADS::isInterleaving (const S &X, const S &Y, const S &Z)
 whether \( Z \) interleaves \( X \) and \( Y \) keeping the order of the items of each
bool ADS::isInterleaving (const std::string &X, const std::string &Y, const std::string &Z)
 isInterleaving() above on std::string (or literals)
template<class S>
requires std::is_class_v< S >
std::size_t ADS::scsLength (const S &X, const S &Y)
 the length of a shortest common supersequence of \( X \) and \( Y \)
std::size_t ADS::scsLength (const std::string &X, const std::string &Y)
 scsLength() above on std::string (or literals)
template<class S>
requires std::is_class_v< S >
S ADS::shortestCommonSupersequence (const S &X, const S &Y)
 a shortest common supersequence of \( X \) and \( Y \)
std::string ADS::shortestCommonSupersequence (const std::string &X, const std::string &Y)
 shortestCommonSupersequence() above on std::string (or literals)
template<class S>
requires std::is_class_v< S >
std::uint64_t ADS::subsequenceOccurrences (const S &P, const S &T)
 the number of occurrences of \( P \) as a subsequence of \( T \), modulo \( 2^{64} \): 1 for the empty \( P \), 0 if \( P \) is longer than \( T \)
std::uint64_t ADS::subsequenceOccurrences (const std::string &P, const std::string &T)
 subsequenceOccurrences() above on std::string (or literals)
template<class S>
requires std::is_class_v< S >
S ADS::lcs3 (const S &X, const S &Y, const S &Z)
 a longest common subsequence of \( X \), \( Y \) and \( Z \)
std::string ADS::lcs3 (const std::string &X, const std::string &Y, const std::string &Z)
 lcs3() above on std::string (or literals)

Detailed description

Four classic problems on the subsequences of sequences \( X \), \( Y \), \( Z \) of lengths \( n \), \( m \), \( l \), relatives of the longest common subsequence [see LongestCommonSubsequence.h], each by dynamic programming on the prefixes.

  • isInterleaving(): whether \( Z \) is a shuffle (an interleaving) of \( X \) and \( Y \), the items of each in their order: \( D[ i ][ j ] \), whether \( Z[ 0 .. i + j ) \) interleaves \( X[ 0 .. i ) \) and \( Y[ 0 .. j ) \), is \( D[ i - 1 ][ j ] \) with \( x_{i-1} = z_{i+j-1} \) or \( D[ i ][ j - 1 ] \) with \( y_{j-1} = z_{i+j-1} \); \( \Theta( n m ) \) time and \( \Theta( m ) \) space, one row of the table. Mansfield showed the problem NP-complete for the shuffle of an unbounded number of strings, and Warmuth and Haussler for the iterated shuffle of one string.
  • shortestCommonSupersequence(): the shortest sequence that has \( X \) and \( Y \) as subsequences, of length \( n + m - | \mathrm{LCS}( X , Y ) | \), read back from the table of the LCS: the common items once, the others in their order; \( \Theta( n m ) \) time and space, while scsLength() keeps one row, \( \Theta( m ) \) space. For an unbounded number of strings the problem is NP-hard (Maier).
  • subsequenceOccurrences(): the number of occurrences of \( P \) as a subsequence of \( T \), the tuples of places \( j_1 < \cdots < j_m \) with \( t_{j_k} = p_k \): \( C[ i ][ j ] = C[ i - 1 ][ j ] + [ t_{i-1} = p_{j-1} ] C[ i - 1 ][ j - 1 ] \), \( \Theta( n m ) \) time and one row updated from the right, \( \Theta( m ) \) space; the count grows as fast as the binomial coefficient \( \binom{n}{m} \), and it is given modulo \( 2^{64} \).
  • lcs3(): a longest common subsequence of three sequences, the table of lcs() in three dimensions, \( \Theta( n m l ) \) time and space. The LCS of two of them and then of the third is not a solution: the longest pairwise one may leave out the items that the third shares. For \( k \) sequences the table has \( \prod_i n_i \) entries, and the problem is NP-hard for an unbounded \( k \) (Maier).

Each function takes any sequence S with size(), operator[], push_back() and the comparison of its items; the overloads on std::string also take string literals.

The contracts [see Contract.h]: the supersequence has \( X \) and \( Y \) as subsequences and the length of scsLength(), and lcs3() gives a subsequence of the three (audits). The functions are defined in SubsequenceProblems.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 13, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
D. Maier, The complexity of some problems on subsequences and supersequences, Journal of the ACM 25(2), 322-336, 1978, doi:10.1145/322063.322075
A. Mansfield, On the computational complexity of a merge recognition problem, Discrete Applied Mathematics 5(1), 119-122, 1983, doi:10.1016/0166-218X(83)90021-5
M. K. Warmuth, D. Haussler, On the complexity of iterated shuffle, Journal of Computer and System Sciences 28(3), 345-358, 1984, doi:10.1016/0022-0000(84)90018-7
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa