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

The search by prefix in a sorted dictionary of \( n \) strings, beyond the book: the strings prefixed by \( P \) are consecutive, between the lexicographic position of \( P \) (the number of strings smaller than \( P \)) and that of \( P\# \), \( \# \) being a character larger than all the others, so that two lexicographic searches give their range. More...

#include "ADS/Contract.h"
#include "ADS/search/BinarySearch.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "ADS/string/impl/PrefixSearch.tpp"

Go to the source code of this file.

Classes

class  ADS::BinnedSearch
 the search by interpolation of a sorted array of integers by bins More...
class  ADS::BinnedStringSearch
 the search by interpolation of a sorted array of strings by bins More...

Namespaces

namespace  ADS

Functions

std::size_t ADS::lexicographicPosition (const std::vector< std::string > &A, std::string_view Q)
 the number of strings of the sorted \( A \) smaller than \( Q \)
std::size_t ADS::prefixEnd (const std::vector< std::string > &A, std::string_view P)
 the number of strings of the sorted \( A \) smaller than \( P\# \), i.e., smaller than \( P \) or prefixed by \( P \)
std::pair< std::size_t, std::size_t > ADS::prefixRange (const std::vector< std::string > &A, std::string_view P)
 the range \( [ l , r ) \) of the strings of the sorted \( A \) prefixed by \( P \)
std::size_t ADS::commonPrefixLength (std::string_view a, std::string_view b)
 the length of the longest common prefix of \( a \) and \( b \), by comparing them from their start

Detailed description

The search by prefix in a sorted dictionary of \( n \) strings, beyond the book: the strings prefixed by \( P \) are consecutive, between the lexicographic position of \( P \) (the number of strings smaller than \( P \)) and that of \( P\# \), \( \# \) being a character larger than all the others, so that two lexicographic searches give their range.

  • lexicographicPosition(), prefixEnd() and prefixRange() search the array of the strings by a binary search, \( O( p \log n ) \) time for a pattern of \( p \) characters, each step a comparison of strings: simple, but each step reads a string far from the others, i.e., \( O( ( p / B ) \log n ) \) transfers of blocks of \( B \) characters, which the two-level dictionaries [see FrontCodedDictionary and TwoLevelDictionary of FrontCoding.h] reduce to a search on a sample of the strings and the scan of one bucket;
  • BinnedSearch, the search by interpolation of Demaine, Jones and Pătrașcu in a sorted array \( X \) of \( m \) integers: \( [ x_1 , x_m ] \) is cut into bins of width \( b = \lceil ( x_m - x_1 + 1 ) / m \rceil \), an array gives where each bin starts in \( X \), and \( y \) is searched by a binary search in its bin \( \lfloor ( y - x_1 ) / b \rfloor \); a bin holds at most \( \Delta \) items, \( \Delta \) being the ratio between the largest and the smallest gap of \( X \), since \( b \) is at most the largest gap (\( x_m - x_1 \) is the sum of the \( m - 1 \) gaps, and \( b \) about its \( m \)-th part), so that a search takes \( O( \log \min( \Delta , m ) ) \) time in the worst case, never worse than the binary search, and \( O( \log \log m ) \) with high probability on uniform keys, as the search by interpolation [see InterpolationSearch.h] whose worst case is \( O( m ) \), at the price of the array of the bins;
  • BinnedStringSearch, the same on strings, read as the integers of their first 8 characters (padded with the character 0), which keep their order: the bin of a pattern is found by its integer, and the binary search in the bin compares whole strings.

The contracts [see Contract.h]: the array sorted, an audit (for the strings by the order of std::string, i.e., of the unsigned characters); each position \( r \) is such that the items before \( r \) are smaller than the pattern and those from \( r \) on are not, a postcondition. The functions are defined in PrefixSearch.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 9, Searching Strings by Prefix, doi:10.1017/9781009128933
E. D. Demaine, T. Jones, M. Pătrașcu, Interpolation search for non-independent data, Proceedings of SODA 2004, 529-530, 2004
Y. Perl, A. Itai, H. Avni, Interpolation search: a log log N search, Communications of the ACM 21(7), 550-553, 1978, doi:10.1145/359545.359557
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa