What is the most efficient algorithm/data structure for finding the smallest range containing a point?
Asked Answered
R

4

8

Given a data set of a few millions of price ranges, we need to find the smallest range that contains a given price.
The following rules apply:

  • Ranges can be fully nested (ie, 1-10 and 5-10 is valid)
  • Ranges cannot be partially nested (ie, 1-10 and 5-15 is invalid)

Example:
Given the following price ranges:

  • 1-100
  • 50-100
  • 100-120
  • 5-10
  • 5-20

The result for searching price 7 should be 5-10
The result for searching price 100 should be 100-120 (smallest range containing 100).

What's the most efficient algorithm/data structure to implement this?
Searching the web, I only found solutions for searching ranges within ranges.
I've been looking at Morton count and Hilbert curve, but can't wrap my head around how to use them for this case.
Thanks.

Reconstitute answered 13/8, 2018 at 7:46 Comment(10)
There is an obvious O(n) algorithm, is that what you are looking for ?Mikesell
If your problem is to index the data for efficient querying then you should look at en.wikipedia.org/wiki/Interval_tree and en.wikipedia.org/wiki/Segment_treeResting
Are you going to search for multiple price points, are the segments already ordered? Will each queried price point have different segment inputs as well?Terena
@pLOPeGG of course, O(n) is the obvious answer, but I'm looking for a more efficient one (maybe O(logn)?)Reconstitute
@Resting interval tree is an interesting solution, however it starts to lose its efficiency when there are many nesting rangesReconstitute
@YiğitArasTunalı every time a single price is searched. the data set is fixed and orderedReconstitute
Are ranges integer? How big is the largest value of the right range border?Shepard
@Shepard yes, ranges are integers. we use 32-bit numbers. currently the largest number is 7 digits (a few millions), but 1-2 billion is a potential in our case.Reconstitute
Is the data set static? You want to optimize more the query than the update?Spain
@Spain that is correct. queries are far more frequent than updatesReconstitute
M
2

Because you did not mention this ad hoc algorithm, I'll propose this as a simple answer to your question:

This is a python function, but it's fairly easy to understand and convert it in another language.

def min_range(ranges, value):
    # ranges = [(1, 100), (50, 100), (100, 120), (5, 10), (5, 20)]
    # value = 100

    # INIT
    import math
    best_range = None
    best_range_len = math.inf

    # LOOP THROUGH ALL RANGES
    for b, e in ranges:

        # PICK THE SMALLEST
        if b <= value <= e and e - b < best_range_len:
            best_range = (b, e)
            best_range_len = e - b

    print(f'Minimal range containing {value} = {best_range}')

I believe there are more efficient and complicated solutions (if you can do some precomputation for example) but this is the first step you must take.

EDIT : Here is a better solution, probably in O(log(n)) but it's not trivial. It is a tree where each node is an interval, and has a child list of all strictly non overlapping intervals that are contained inside him. Preprocessing is done in O(n log(n)) time and queries are O(n) in worst case (when you can't find 2 ranges that don't overlap) and probably O(log(n)) in average.

2 classes: Tree that holds the tree and can query:

class tree:
    def __init__(self, ranges):
        # sort the ranges by lowest starting and then greatest ending
        ranges = sorted(ranges, key=lambda i: (i[0], -i[1]))
        # recursive building -> might want to optimize that in python
        self.node = node( (-float('inf'), float('inf')) , ranges)

    def __str__(self):
        return str(self.node)

    def query(self, value):
        # bisect is for binary search
        import bisect
        curr_sol = self.node.inter
        node_list = self.node.child_list

        while True:
            # which of the child ranges can include our value ?
            i = bisect.bisect_left(node_list, (value, float('inf'))) - 1
            # does it includes it ?
            if i < 0 or i == len(node_list):
                return curr_sol
            if value > node_list[i].inter[1]:
                return curr_sol
            else:
                # if it does then go deeper
                curr_sol = node_list[i].inter
                node_list = node_list[i].child_list

Node that holds the structure and information:

class node:
    def __init__(self, inter, ranges):
        # all elements in ranges will be descendant of this node !
        import bisect

        self.inter = inter
        self.child_list = []

        for i, r in enumerate(ranges):
            if len(self.child_list) == 0:
                # append a new child when list is empty
                self.child_list.append(node(r, ranges[i + 1:bisect.bisect_left(ranges, (r[1], r[1] - 1))]))

            else:
                # the current range r is included in a previous range 
                # r is not a child of self but a descendant !
                if r[0] < self.child_list[-1].inter[1]:
                    continue
                # else -> this is a new child
                self.child_list.append(node(r, ranges[i + 1:bisect.bisect_left(ranges, (r[1], r[1] - 1))]))

    def __str__(self):
        # fancy
        return f'{self.inter} : [{", ".join([str(n) for n in self.child_list])}]'

    def __lt__(self, other):
        # this is '<' operator -> for bisect to compare our items
        return self.inter < other

and to test that:

ranges = [(1, 100), (50, 100), (100, 120), (5, 10), (5, 20), (50, 51)]
t = tree(ranges)
print(t)
print(t.query(10))
print(t.query(5))
print(t.query(40))
print(t.query(50))
Mikesell answered 13/8, 2018 at 8:39 Comment(2)
This is an obvious algorithm, but it's O(n) in the worst case. We need an efficient methodReconstitute
Oh okay, then consider SaiBot method. Another solution is because there is no overlapping, you can build a tree where each node is a range and has a list of range as child. In this list put only ranges that are directly contained in the parent range and so on. To query the tree, start to the root, binary search in his child list the right child range. If there is noone -> the current range is your answer, else go deeper and repeat. It might be faster (or slower) depending on your data.Mikesell
S
1

Preprocessing that generates disjoined intervals
(I call source segments as ranges and resulting segments as intervals)

For ever range border (both start and end) make tuple: (value, start/end fiels, range length, id), put them in array/list

Sort these tuples by the first field. In case of tie make longer range left for start and right for end.

Make a stack
Make StartValue variable.
Walk through the list:
     if current tuple contains start:
          if interval is opened:   //we close it
             if  current value > StartValue:   //interval is not empty
                  make interval with   //note id remains in stack
                      (start=StartValue, end = current value, id = stack.peek)       
                  add interval to result list
          StartValue = current value //we open new interval
          push id from current tuple onto stack
     else:   //end of range
             if  current value > StartValue:   //interval is not empty
                 make interval with    //note id is removed from stack
                      (start=StartValue, end = current value, id = stack.pop)
                 add interval to result list
         if stack is not empty:
              StartValue = current value //we open new interval

After that we have sorted list of disjointed intervals containing start/end value and id of the source range (note that many intervals might correspond to the same source range), so we can use binary search easily.

If we add source ranges one-by-one in nested order (nested after it parent), we can see that every new range might generate at most two new intervals, so overall number of intervals M <= 2*N and overall complexity is O(Nlog N + Q * logN) where Q is number of queries

Edit: Added if stack is not empty section

Result for your example 1-100, 50-100, 100-120, 5-10, 5-20 is

1-5(0), 5-10(3), 10-20(4), 20-50(0), 50-100(1), 100-120(2) 
Shepard answered 13/8, 2018 at 13:8 Comment(2)
This is log(m) worst case, being m the amount of ranges left after making them disjoint. If you can prove that m is on the same order as n, I think this is the best solutionSpain
@Spain Yes, new interval might appear only at start and end point of source range, so m~n. Added to answer.Shepard
R
0

Since pLOPeGG already covered the ad hoc case, I will answer the question under the premise that preporcessing is performed in order to support multiple queries efficiently.

General data structures for efficient queries on intervals are the Interval Tree and the Segment Tree

Resting answered 13/8, 2018 at 10:38 Comment(2)
I think this might not work, or might be harder to implement than you think. The problem is, once you've found the largest starting value smaller than the query, the final result might not start with this largest value. Here is an example : ranges = [(1, 100), (5, 20), (5, 10), (50, 100)] If we query for 30, then the largest stating value is 5 but the right answer is (1, 100) This imply some difficulties that might prevent us from getting this O(log(n)) computation time.Mikesell
@pLOPeGG You are right I have to go back to the whiteborad ;-) I am removing the second part of my answer for now.Resting
S
0

What about an approach like this. Since we only allow nested and not partial-nesting. This looks to be a do-able approach.

  • Split segments into (left,val) and (right,val) pairs.
  • Order them with respect to their vals and left/right relation.
  • Search the list with binary search. We get two outcomes not found and found.
  • If found check if it is a left or right. If it is a left go right until you find a right without finding a left. If it is a right go left until you find a left without finding a right. Pick the smallest.
  • If not found stop when the high-low is 1 or 0. Then compare the queried value with the value of the node you are at and then according to that search right and left to it just like before.

As an example;

We would have (l,10) (l,20) (l,30) (r,45) (r,60) (r,100) when searching for say, 65 you drop on (r,100) so you go left and can't find a spot with a (l,x) such that x>=65 so you go left until you get balanced lefts and rights and first right and last left is your interval. The reprocessing part will be long but since you will keep it that way. It is still O(n) in worst-case. But that worst case requires you to have everything nested inside each other and you searching for the outer-most.

Snapback answered 13/8, 2018 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.