Is there a generic algorithm I can employ to solve the following problem:
Given:
Background: A month, which has 0 to 1000 events (any number really). Each event has a start and end dates. Events take place in rooms, one at a time (no overlaps, however consequent events are allowed to share end and start dates with each other). The number of rooms is unlimited.
The challenge: Allocate rooms for the events such that the number of rooms required to host monthly events is kept to a minimum.
While the complete solution is highly appreciated I’m looking for any directions, smart ideas.
class Event:
- int Id;
- DateTime StartDate;
- DateTime EndDate
class Allocation:
- int EventId
- int RoomId
so I’m looking for:
// roomIds is Enumerable.Range(1, int.MaxValue)
IEnumerable<Allocation> GetAllocations(IEnumerable<Event> events, IEnumerable<int> roomIds, int year, int month)
{
...
}
Split every event into two timepoints labeled ‘start’ and ‘end’ (keeping the back pointer to the original event), sort all the points on time – break ties so that ‘end’s come before ‘start’s with the same time.
Now go over the points (in the order defined above), allocating first free number on each ‘start’ and freeing the associated number on each ‘end’.
Example:
Events: 9AM-5PM, 9AM-2PM, 5PM-6PM, 3PM-6PM
Sorted table of timepoints:
(9AM start event1), (9AM start event2), (2PM end event 2), (3PM start event4), (5PM end event1), (5PM start event3), (6PM end event3), (6PM end event4)
Processing: