I have a data set of events (start date, end date, location). These events move in the country and I need to figure out which event should go where after it ends.
Exemple:
- [1/7, 6/7, Toronto] (Starts the 1st of July, ends the 6th of July)
- [2/7, 4/7, Montreal]
- [4/7, 11/7, Ottawa]
- [17/7, 22/7, Vancouver]
..etc (Data set will be around 100 entries, so performance is not really an issue)
In this exemple, Event 1 could move and do Event 4, since it ends on the 6th of July and Event 4 starts on the 17th. (Assuming transit in the same day)
All Events that couldn’t find a suitable match will be stored in a report for someone to match manually.
This optimization code will be done in javascript.
My first thought was to have 2 arrays, with the same data. First array sorted by Start Date, 2nd array sorted by End Date. Then go through the list of End Dates and try to find an appropriate Start Date for it, then remove these entries from the array and continue like that until no more matching is possible.
Anybody has a better idea on how to approach this problem ? If you need more details let me know!
Your question isn’t very clear. If I understand correctly, your goal is to choose a subset of events such that your selection maximizes the number of events (and there are not overlapping events). If so, your problem can be seen as an Activity Selection Problem. There is a simple greedy algorithm to solve that.
Let
and assume that you already sorted the events by end time.
The following greedy algorithm will find the largest subset S of non overlapping events:
(note that lse is the Last Selected Event)
Basically: