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

Problems on strings solved by a suffix array [see SuffixArray.h] or a suffix tree [see SuffixTree.h], beyond the book (those of chapter 10, Searching Strings by Substring, of Ferragina's book and their classical companions). More...

#include "ADS/Contract.h"
#include "ADS/string/SuffixArray.h"
#include "ADS/string/SuffixTree.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "ADS/string/impl/SuffixApplications.tpp"

Go to the source code of this file.

Classes

struct  ADS::CopyPhrase
 a phrase of the parsing of Ziv and Lempel: copy length symbols from distance places back, then append next, if any More...

Namespaces

namespace  ADS

Functions

std::pair< std::size_t, std::size_t > ADS::longestRepeatedSubstring (const SuffixArray &SA)
 the place and the length of a longest substring that occurs twice (0 and 0 if none)
std::optional< std::size_t > ADS::repeatedSubstring (const SuffixArray &SA, std::size_t L, std::size_t C)
 the place of a substring of length \( L \) that occurs at least \( C \) times, if any
std::uint64_t ADS::distinctSubstrings (const SuffixArray &SA)
 the number of distinct nonempty substrings, \( n ( n + 1 ) / 2 - \sum_r \mathit{LCP}[ r ] \)
std::string ADS::longestCommonSubstring (const std::vector< std::string > &S, std::size_t k)
 a longest substring common to at least \( k \) of the strings \( S \)
std::vector< std::pair< std::size_t, std::size_t > > ADS::maximalRepeats (const SuffixTree &ST)
 the maximal repeats, as the place of an occurrence and the length
bool ADS::proximity (const SuffixArray &SA, std::string_view P, std::string_view Q, std::size_t k)
 whether an occurrence of \( P \) and one of \( Q \) start at most \( k \) places apart
std::vector< std::size_t > ADS::kMismatches (std::string_view T, std::string_view P, std::size_t k)
 the places where \( P \) occurs in \( T \) with at most \( k \) mismatches
std::vector< CopyPhrase > ADS::lz77ByTree (std::string_view T)
 the parsing of Ziv and Lempel of \( T \), the copies from the farthest occurrence, by the suffix tree
std::string ADS::decodeCopyPhrases (const std::vector< CopyPhrase > &Z)
 the text of a parsing, each phrase copying its symbols one at a time, so that a copy may overlap the symbols it writes

Detailed description

Problems on strings solved by a suffix array [see SuffixArray.h] or a suffix tree [see SuffixTree.h], beyond the book (those of chapter 10, Searching Strings by Substring, of Ferragina's book and their classical companions).

A substring that occurs twice is a common prefix of two suffixes, and the suffixes that share a prefix of length \( L \) are a range of \( \mathit{SA} \) whose \( \mathit{LCP} \) entries are all at least \( L \): hence a scan of \( \mathit{LCP} \), or a visit of the internal nodes of the suffix tree, answers the questions on the repeats in \( O( n ) \) time.

  • longestRepeatedSubstring(): the largest entry of \( \mathit{LCP} \), \( O( n ) \), against the \( O( n^2 ) \) of the dynamic programming of a string against itself;
  • repeatedSubstring( SA , L , C ), a query of text mining: a substring of length \( L \) that occurs at least \( C \) times is a run of at least \( C - 1 \) entries of \( \mathit{LCP} \) that are at least \( L \), one scan, \( O( n ) \), against the \( O( n^2 L ) \) of counting every substring;
  • distinctSubstrings(): each suffix \( \mathit{SA}[ r ] \) adds the \( n - \mathit{SA}[ r ] \) prefixes less the \( \mathit{LCP}[ r - 1 ] \) it shares with the previous one, hence \( n ( n + 1 ) / 2 - \sum_r \mathit{LCP}[ r ] \), \( O( n ) \);
  • longestCommonSubstring( S , k ): the longest substring common to at least \( k \) of the \( m \) strings of \( S \), on the suffix array of their join with \( m \) distinct separators (an integer alphabet): a window of ranks that covers suffixes of \( k \) distinct strings, slid by two pointers, whose common prefix is the lce() of its two ends, \( O( N ) \) for \( N \) the total length, against the \( O( |P| |T| ) \) of the dynamic programming for two strings [see LongestCommonSubsequence.h] and the generalized suffix tree with the colors of Hui for any \( k \);
  • maximalRepeats(): the maximal repeats, the substrings that occur twice and cannot be extended to the right (an internal node) nor to the left (the symbols before their occurrences are not all the same, or one occurrence starts the text): the internal nodes whose leaves are left-diverse, by one visit from the leaves up, \( O( n ) \), as in Gusfield;
  • proximity( SA , P , Q , k ), the proximity search: the occurrences of \( P \) and of \( Q \), sorted together, have an occurrence of each at distance at most \( k \) exactly when two adjacent ones of different patterns do, \( O( |P| + |Q| + \mathit{occ} \log \mathit{occ} ) \);
  • kMismatches( T , P , k ): the places where \( P \) occurs with at most \( k \) substitutions, each checked by at most \( k + 1 \) longest common extensions of a suffix of \( T \) and a suffix of \( P \), both suffixes of \( T \# P \), by the range minima of its \( \mathit{LCP} \) in \( O( 1 ) \): \( O( n k ) \) after \( O( n + p ) \) preprocessing, against the \( O( n p ) \) of trying every place, as in Landau and Vishkin;
  • lz77ByTree( T ), the parsing of Ziv and Lempel by the suffix tree: the phrase at \( i \) copies the longest prefix of the suffix \( i \) that starts earlier, found by walking down along the suffix \( i \) while the least leaf below the next node is smaller than \( i \), the least leaves computed by one visit; the copy is taken from the farthest occurrence, the least leaf, \( O( n \log \sigma ) \) in all (\( O( n ) \) with perfect hashing at the nodes). The phrase is the triple (distance, length, next symbol), the last one without the symbol when the copy reaches the end of \( T \); decodeCopyPhrases() inverts it.

The contracts [see Contract.h]: \( k \) is at least 1 and at most the number of strings for longestCommonSubstring(), \( C \) at least 1; the parsing decodes to the text (an audit). The functions are defined in SuffixApplications.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 10, Searching Strings by Substring, doi:10.1017/9781009128933
D. Gusfield, Algorithms on Strings, Trees, and Sequences, Cambridge University Press, 1997, sections 7.4-7.12, doi:10.1017/CBO9780511574931
L. C. K. Hui, Color set size problem with applications to string matching, Proceedings of the 3rd Symposium on Combinatorial Pattern Matching (CPM), LNCS 644, 230-243, 1992, doi:10.1007/3-540-56024-6_19
M. I. Abouelhoda, S. Kurtz, E. Ohlebusch, Replacing suffix trees with enhanced suffix arrays, Journal of Discrete Algorithms 2(1), 53-86, 2004, doi:10.1016/S1570-8667(03)00065-0
G. M. Landau, U. Vishkin, Efficient string matching with k mismatches, Theoretical Computer Science 43, 239-249, 1986, doi:10.1016/0304-3975(86)90178-7
J. Ziv, A. Lempel, A universal algorithm for sequential data compression, IEEE Transactions on Information Theory 23(3), 337-343, 1977, doi:10.1109/TIT.1977.1055714
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa