Data structure to build and lookup set of integer ranges

前端 未结 7 1588
你的背包
你的背包 2020-12-19 09:58

I have a set of uint32 integers, there may be millions of items in the set. 50-70% of them are consecutive, but in input stream they appear in unpredictable ord

相关标签:
7条回答
  • 2020-12-19 10:22

    Rather than a 'comparison' based storage/retrieval ( which will always be O(log(n)) ), You need to work on 'radix' based storage/retrieval .

    In other words .. extract nibbles from the uint32, and make a trie ..

    0 讨论(0)
  • 2020-12-19 10:25

    Regarding the second issue:

    You could look-up on Bloom Filters. Bloom Filters are specifically designed to answer the membership question in O(1), though the response is either no or maybe (which is not as clear cut as a yes/no :p).

    In the maybe case, of course, you need further processing to actually answer the question (unless a probabilistic answer is sufficient in your case), but even so the Bloom Filter may act as a gate keeper, and reject most of the queries outright.

    Also, you might want to keep actual ranges and degenerate ranges (single elements) in different structures.

    • single elements may be best stored in a hash-table
    • actual ranges can be stored in a sorted array

    This diminishes the number of elements stored in the sorted array, and thus the complexity of the binary search performed there. Since you state that many ranges are degenerate, I take it that you only have some 500-1000 ranges (ie, an order of magnitude less), and log(1000) ~ 10

    I would therefore suggest the following steps:

    • Bloom Filter: if no, stop
    • Sorted Array of real ranges: if yes, stop
    • Hash Table of single elements

    The Sorted Array test is performed first, because from the number you give (millions of number coalesced in a a few thousands of ranges) if a number is contained, chances are it'll be in a range rather than being single :)

    One last note: beware of O(1), while it may seem appealing, you are not here in an asymptotic case. Barely 5000-10000 ranges is few, as log(10000) is something like 13. So don't pessimize your implementation by getting a O(1) solution with such a high constant factor that it actually runs slower than a O(log N) solution :)

    0 讨论(0)
  • 2020-12-19 10:26

    Keep your ranges into a sorted array and use binary search for lookups.

    It's easy to implement, O(log N), and uses less memory and needs less memory accesses than any other tree based approach, so it will probably be also much faster.

    0 讨论(0)
  • 2020-12-19 10:27

    If you know in advance what the ranges are, then you can check whether a given integer is present in one of the ranges in O(lg n) using the strategy outlined below. It's not O(1), but it's still quite fast in practice.

    The idea behind this approach is that if you've merged all of the ranges together, you have a collection of disjoint ranges on the number line. From there, you can define an ordering on those intervals by saying that the interval [a, b] ≤ [c, d] iff b ≤ c. This is a total ordering because all of the ranges are disjoint. You can thus put all of the intervals together into a static array and then sort them by this ordering. This means that the leftmost interval is in the first slot of the array, and the rightmost interval is in the rightmost slot. This construction takes O(n lg n) time.

    To check if a some interval contains a given integer, you can do a binary search on this array. Starting at the middle interval, check if the integer is contained in that interval. If so, you're done. Otherwise, if the value is less than the smallest value in the range, continue the search on the left, and if the value is greater than the largest value in the range, continue the search on the right. This is essentially a standard binary search, and it should run in O(lg n) time.

    Hope this helps!

    0 讨论(0)
  • 2020-12-19 10:36

    From the description of you problem it sounds like the following might be a good compromise. I've described it using an Object oriented language, but is easily convertible to C using a union type or structure with a type member and a pointer.

    Use the first 16 bits to index an array of objects (of size 65536). In that array there are 5 possible objects

    • a NONE object means no elements beginning with those 16bits are in the set
    • an ALL object means all elements beginning with 16 bits are in the set
    • a RANGE object means all items with the final 16bits between an upper and lower bound are in the set
    • a SINGLE object means just one element beginning with the 16bits is in the array
    • a BITSET object handles all remaining cases with a 65536 bit bitset

    Of course, you don't need to split at 16bits, you can adjust to reflect the statistics of your set. In fact you don't need to use consecutive bits, but it speeds up the bit twiddling, and if many of your elements are consecutive as you claim will give good properties.

    Hopefully this makes sense, please comment if I need to explain more fully. Effectively you've combined a depth 2 binary tree with a ranges and a bitset for a time/speed tradeoff. If you need to save memory then make the tree deeper with a corresponding slight increase in lookup time.

    0 讨论(0)
  • 2020-12-19 10:37

    AFAIK there is no such algorithm that search over integer list in O(1).

    One only can do O(1) search with vast amount of memory.

    So it is not very promising to try to find O(1) search algorithm over list of range of integer.

    On the other hand, you could try time/memory trade-off approach by carefully examining your data set (eventually building a kind of hash table).

    0 讨论(0)
提交回复
热议问题