I have a set of events with start and end times. I want to render them as long boxes in a series of rows with no overlap, something like this
00:00 01:00 02:00 03:00 04:00 05:00 06:00 07:00 08:00
--|--------|--------|--------|--------|--------|--------|--------|--------|--
AAAAAAAAAAAA BBBBBB CCCCCCCCCCCCCCCCCCCC DDDDDDDDDDDD
--|--------|--------|--------|--------|--------|--------|--------|--------|--
EEEEEEEEEEEEEE FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
--|--------|--------|--------|--------|--------|--------|--------|--------|--
GGGGGGGGGGGGGGGGGGG HHHHHHHH IIIIIIIIIIIII
--|--------|--------|--------|--------|--------|--------|--------|--------|--
The assignment of any particular box to any particular row isn't important. What I want is an algorithm which will pack these boxes into the smallest number of rows.
Is there a known algorithm for this?
I've solved identical problem with the following solution:
Be aware, that when inserting an element (step 3.2.1), the set of free sectors being enumerated (step 3) will change.
The International Timetabling Competition 2007 had a lesson scheduling track and exam scheduling track. Many researchers participated in that competition. Lots of heuristics and metaheuristics were tried, but in the end the local search metaheuristics (such as Tabu Search and Simulated Annealing ) clearly beat other algorithms (such as genetic algorithms).
Take a look at the 2 open source frameworks used by some of the finalists:
JBoss OptaPlanner (Java, open source) Unitime (Java, open source) - more for universities
-- Geoffrey De Smet : Algorithm for creating a school timetable
Reference:
I would just pack them greedily (proceed from left to right and add an event to the first track in top-down direction where it fits). It's fast and it's deterministic and most likely produces in practice good results.
This is known as Interval Partitioning. The greedy algorithm that assigns boxes in order of starting time and puts a box into the first row where it fits (or opens a new row if it fits nowhere) gives an optimal result. See for example Kleinberg&Tardos: "Algorithm Design".
If I understood your problem correctly, it is most important to find out, which dates overlap. You did not say, if the algorithm should be really efficient, or if it should work for really large data. Therefore I would recommend the following steps:
Maybe the weighting of the elements by overlaps and this final check in step 5. could help to improve a normal greedy approach.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.