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

The intersection of two sorted sets, the core of the resolution of a query of several words by an inverted index (Ferragina, chapter 6): \( A \) of \( n \) items and \( B \) of \( m \) items, strictly increasing, \( m \leq n \) (the roles are swapped otherwise). More...

#include "ADS/Contract.h"
#include "ADS/external/TwoLevelMemory.h"
#include "ADS/search/BinarySearch.h"
#include "ADS/sort/Sorted.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <utility>
#include <vector>
#include "ADS/set/impl/SortedIntersection.tpp"

Go to the source code of this file.

Classes

class  ADS::PermutedBuckets
 a set of values below \( 2^w \), permuted by \( x \mapsto ( a x + c ) \bmod 2^w \) and split by the top bits of the images into buckets of \( L \) items on average More...

Namespaces

namespace  ADS

Functions

template<class T, class C = std::less<>>
std::vector< T > ADS::intersectMerge (const std::vector< T > &A, const std::vector< T > &B, C less=C())
 the items common to \( A \) and \( B \), by merging
template<class T, class C = std::less<>>
std::vector< T > ADS::intersectBinarySearch (const std::vector< T > &A, const std::vector< T > &B, C less=C())
 the items common to \( A \) and \( B \), by a binary search in the longer list of each item of the shorter one
template<class T, class C = std::less<>>
std::vector< T > ADS::intersectMutualPartition (const std::vector< T > &A, const std::vector< T > &B, C less=C())
 the items common to \( A \) and \( B \), by mutual partitions
template<class T, class C = std::less<>>
std::vector< T > ADS::intersectDoubling (const std::vector< T > &A, const std::vector< T > &B, C less=C())
 the items common to \( A \) and \( B \), by doubling searches
template<class T, class C = std::less<>>
std::vector< T > ADS::intersectTwoLevel (const std::vector< T > &A, const std::vector< T > &B, std::size_t L, C less=C())
 the items common to \( A \) and \( B \), by the first items of the blocks of \( L \) items of \( A \)
template<class T>
void ADS::twoLevelHeaders (const DiskFile< T > &A, std::size_t L, DiskFile< T > &H)
 the first item of each block of \( L \) items of \( A \), into \( H \), by one scan
template<class T, class C = std::less<>>
void ADS::intersectMergeExternal (const DiskFile< T > &A, const DiskFile< T > &B, DiskFile< T > &out, C less=C())
 the items common to the sorted files \( A \) and \( B \) into out, by merging, \( \lceil n / B \rceil + \lceil m / B \rceil \) reads
template<class T, class C = std::less<>>
void ADS::intersectTwoLevelExternal (const DiskFile< T > &A, const DiskFile< T > &H, std::size_t L, const DiskFile< T > &B, DiskFile< T > &out, C less=C())
 the items common to the sorted files \( A \) and \( B \) into out, by the headers \( H \) of the blocks of \( L \) items of \( A \) [see twoLevelHeaders()]

Detailed description

The intersection of two sorted sets, the core of the resolution of a query of several words by an inverted index (Ferragina, chapter 6): \( A \) of \( n \) items and \( B \) of \( m \) items, strictly increasing, \( m \leq n \) (the roles are swapped otherwise).

  • intersectMerge() is the scan of the two lists, at most \( n + m \) comparisons, \( O( ( n + m ) / B ) \) I/Os whatever \( B \), optimal when \( m \) and \( n \) are close; it is the procedure of section 8.3 of the book that setIntersection() of SortedListSet.h applies to its lists.
  • intersectBinarySearch() searches each item of \( B \) in \( A \), \( O( m \log n ) \) comparisons, better when \( m = o( n / \log n ) \).
  • intersectMutualPartition() is the algorithm of Baeza-Yates: the median of the shorter list searched in the longer one splits both, and the two halves recur, the roles swapped when the part of the longer one is the shorter, \( O( m ( 1 + \log( n / m ) ) ) \) comparisons, which is optimal, since \( \log_2 \binom{n}{m} \) comparisons are needed to tell the subsets of \( A \) of \( m \) items apart.
  • intersectDoubling() searches each item of \( B \) from the place of the previous one by galloping, the places \( 1 , 2 , 4 , \ldots \) ahead and then a binary search within the last gap (the exponential search of Bentley and Yao; gallopSearch() of BinarySearch.h is the same search from the start of a vector, and the merges of RunSorts.h gallop in the same way), \( \sum_j ( 1 + \log \Delta_j ) = O( m ( 1 + \log( n / m ) ) ) \) comparisons with no recursion.
  • intersectTwoLevel() is the two-level storage: the first item of each block of \( L \) items of \( A \) in an array \( A' \) of \( n / L \) items, merged with \( B \) to find the block where each item of \( B \) may be, and each block met merged with its items of \( B \), \( O( n / L + m L ) \) time; on the disk [see TwoLevelMemory.h] intersectTwoLevelExternal() takes \( O( n / L B + m L / B + m ) \) I/Os, each block of \( A \) met costing at most \( \lceil L / B \rceil + 1 \) reads, against the \( \lceil n / B \rceil + \lceil m / B \rceil \) of intersectMergeExternal().
  • PermutedBuckets is the random permutation and splitting of the lists (the lookup of Sanders and Transier): the values of the universe \( [ 0 , 2^w ) \) permuted by \( \pi( x ) = ( a x + c ) \bmod 2^w \), \( a \) odd so that \( \pi \) is invertible, and a list of \( n \) items split by the top \( \ell = \lceil \log_2( n / L ) \rceil \) bits of the images into buckets of \( L \) items on average, the images sorted and addressed by a table of offsets; each image of the shorter list is looked for in the bucket of the longer one named by its own top bits, so that a bucket of \( A \) is never looked at unless \( B \) has items in it: \( O( m + \min\{ n , m L \} ) \) expected time, and the lists need not be sorted beforehand, only their images are.

Compared with the literature: the experiments of Sanders and Transier, and of Barbay et al., find the lookup best up to lists of similar length, the two-level skipping better than the mutual partition for all but the most unequal lists, whose recursion costs more than its bound says; the adaptive algorithms of Demaine, López-Ortiz and Munro and of Barbay and Kenyon extend the galloping to \( k \) lists, and Ding and König replace the buckets by word-sized bitmaps.

The contracts [see Contract.h]: \( A \) and \( B \) strictly increasing, audits; \( L \ge 1 \); the result is the intersection, strictly increasing, a postcondition checked as an audit against intersectMerge(); \( 1 \le w \le 63 \), \( a \) odd, the values below \( 2^w \), and two PermutedBuckets intersected with the same permutation; the files of the external versions share a memory of at least \( 4 B \). The functions are defined in SortedIntersection.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 6, Set Intersection, doi:10.1017/9781009128933
R. Baeza-Yates, A fast set intersection algorithm for sorted sequences, Proceedings of the 15th Symposium on Combinatorial Pattern Matching (CPM), Lecture Notes in Computer Science 3109, 400-408, 2004, doi:10.1007/978-3-540-27801-6_30
J. L. Bentley, A. C.-C. Yao, An almost optimal algorithm for unbounded searching, Information Processing Letters 5(3), 82-87, 1976, doi:10.1016/0020-0190(76)90071-5
P. Sanders, F. Transier, Intersection in integer inverted indices, Proceedings of the 9th Workshop on Algorithm Engineering and Experiments (ALENEX), 71-83, 2007, doi:10.1137/1.9781611972870.7
J. Barbay, A. López-Ortiz, T. Lu, A. Salinger, An experimental investigation of set intersection algorithms for text searching, ACM Journal of Experimental Algorithmics 14, 3.7, 2009, doi:10.1145/1498698.1564507
E. D. Demaine, A. López-Ortiz, J. I. Munro, Adaptive set intersections, unions, and differences, Proceedings of the 11th ACM-SIAM Symposium on Discrete Algorithms (SODA), 743-752, 2000
B. Ding, A. C. König, Fast set intersection in memory, Proceedings of the VLDB Endowment 4(4), 255-266, 2011, doi:10.14778/1938545.1938550
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa