Data structure for non-overlapping ranges within a single dimension

前端 未结 8 1282
再見小時候
再見小時候 2020-12-30 11:26

I need a data structure that can store non-overlapping ranges within a single dimension. The entire range of the dimension need not be completely covered.

An example

相关标签:
8条回答
  • 2020-12-30 12:00

    A lot depends on what you'll be doing with the data, and therefore which operations need to be efficient. However, I'd consider a doubly linked list of Ranges with logic in the setters of Start and End to check whether it now overlaps its neighbours, and to shrink them if so (or throw an exception, or however you want to handle an attempted overlap).

    That gives a nice simple linked list of booked periods to read, but no container responsible for maintaining the no-overlap rule.

    0 讨论(0)
  • 2020-12-30 12:01

    The normalized way to represent your data would be to store a record for each unit of time. This can be done in the example of the conference scheduling application. Your constraint would be a unique constraint for

    (RoomId, StartTime)
    

    In the case of continuous ranges, you necessarily need to store 2 things, one boundary and either the second boundary or the length. It is usually done by storing the second boundary and then creating a constraint on both boundary of the kind

    (boundary not between colBoudaryA and colBoundaryB)
    

    with the additional constraint that

    (startBoundary < endBoundary)
    
    0 讨论(0)
  • 2020-12-30 12:05
    1. For non-overlapping intervals you could just sort you intervals with starting point. When you add a new interval to this structure, you could just check that start and end points do not belong to this interval set. To check whether some point X belong interval set you could use binary search to find the nearest start point and check that X belongs it's interval. This approach is not so optimal for modify operations.

    2. You could look at Interval tree structure - for non-overlapping intervals it has optimal query and modify operations.

    0 讨论(0)
  • 2020-12-30 12:06

    I've had success storing a beginning time and duration. The test for overlap would be something like

    WHERE NOT EXISTS (
       SELECT 1 FROM table
       WHERE BeginTime < NewBeginTime AND BeginTime + Duration > NewBeginTime
    )
    AND NOT EXISTS (
       SELECT 1 FROM table
       WHERE NewBeginTime < BeginTime AND NewBeginTime + NewDuration > BeginTime
    )
    

    I think without testing, but hopefully you get the drift

    0 讨论(0)
  • 2020-12-30 12:08

    This is non-trivial because (in the database world) you have to compare multiple rows to determine non-overlapping ranges. Clearly, when the information is in memory, then other representations such as lists in time order are possible. I think, though, that you'd be best off with your 'start + end' notation, even in a list.

    There are whole books on the subject - part of 'Temporal Database' handling. Two you could look at are Darwen, Date and Lorentzos "Temporal Data and the Relational Model" and (at a radically different extreme) "Developing Time-Oriented Database Applications in SQL", Richard T. Snodgrass, Morgan Kaufmann Publishers, Inc., San Francisco, July, 1999, 504+xxiii pages, ISBN 1-55860-436-7. That is out of print but available as PDF on his web site at cs.arizona.edu (so a Google search makes it pretty easy to find).

    One of the relevant data structures is, I believe, an R-Tree. That is often used for 2-dimensional structures, but can also be effective for 1-dimensional structures.

    You can also look for "Allen's Relations" for intervals - they may be helpful to you.

    0 讨论(0)
  • 2020-12-30 12:13

    This is called the "Unary Resource" constraint in the Constraint Programming world. There is a lot of research in this area, specifically for the case when the event times aren't fixed, and you need to find time-slots for each of them. There is a commercial C++ package that does your problem and more Ilog CP, but it is likely overkill. There is also a somewhat open-source version called eclipse (no relation to the IDE).

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