Find all intersections for m timespans of n parties

0 votes
asked Sep 11, 2017 by daniel-becker

I just can't figure out a nice way to solve the following problem:

For an event I have n parties (party_id) taking part. Each party has m availabilities for said event in the form of start_date and end_date.

What I would like to know is all possible combinations of overlapping availabilities containing exactly one availability for each party_id. I discovered Interval Trees ( which might be utilized, but as I said I can't quite figure it out.

So thanks for any thoughts on that!

1 Answer

0 votes
answered Sep 11, 2017 by comingstorm

The straightforward way is sort all your events (start_date and end_date alike) by time. Then go through this list in chronological order, while keeping track of all "active" parties (i.e., those which have started, but not stopped).

The above method is a batch process, which should let you find all possible combinations of a given schedule easily. The interval tree you mention is useful in cases where you want to incrementally update your set of parties -- the datastructure can make individual queries about a particular time much cheaper than re-running the batch process for each update or query.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter