I have a group of users with a given start and end time, for example:
{ Name = "Peter", StartTime = "10:30", EndTime = "11:00" }, { Name = "Dana", StartTime = "11:00", EndTime = "12:30" }, { Name = "Raymond", StartTime = "10:30", EndTime = "14:00" }, { Name = "Egon", StartTime = "12:00", EndTime = "13:00" }, { Name = "Winston", StartTime = "10:00", EndTime = "12:00" }
I want to put them in buckets, depending on the fact that they overlap (based on a custom threshold, for example, they should overlap for at least half an hour). I want the buckets to be ideally 4 pieces large, but any range from 2-5 is valid.
There are no 4 people in the above example, so I will have a bucket of 3 (Peter, Raymond, Winston) and one of 2 (Dana, Egon).
I prototyped an algorithm that seems to rely on chance rather than science:
- Order list by StartTime
- Create an empty container
- Select the first user from the list
- Make sure the user is against all users in the bucket
- If this user overlaps with everyone in the bucket, put that person in him and remove him from the list
- If the bucket has an ideal size (4), or if I loop and check the same user more than three times, close the bucket and create a new empty one.
This works well for the first few buckets, but leads to buckets that have only 2 people that can be combined better.
I could change the algorithm to remove all the ideal buckets from the list and shuffle and try something else, but I think this should be a common problem - is it like assigning a shift for workers or maybe a knapsack problem .
Does anyone know a standard algorithm for this type of problem?
(Tagged combinatorics, because I think this is the area of mathematics that she applies, correct me if not)