How to correctly find intervals between other intervals - algorithm

I need to come up with an algorithm that can find free intervals on the timeline.
There is a time scale. From 00:00 to 24:00. Initially, when there are no vacancies, all the time is free, and the free interval is (0...1440) (in minutes).
For example, we add a vacancy, I mean that we set the working time for example from 08:00 to 21:00.
Now we will already have 2 free intervals. From 00:00 to 08:00. And from 21:00 to 24:00.
I'll attach pictures below to make it clear what I mean.
*Default variant
*Intervals of vacancies (working hours) may overlap
*Intervals of vacancies can be set without restrictions on the number, and intersect with anything, most importantly within 24 hours (1 day)
The result that I expect: Initially, we have an array with 1 free interval from 0 to 1440 (in minutes), we call the function and pass working time to it, and at the output we get an updated array of intervals, in which there are already 2 intervals. Then we can add 1 more working time, and the output function will always give us the actual array with the correct number of intervals and the free time of the intervals themselves
For writing code I use swift, but I will understand the solution in Python or similar
I really hope for your help! I hope at least that the community will help put me on the right path, at the moment, I can't figure out which way to go. :(

Turn intervals into pairs of start/end events, sort the events by time, then run through the list and keep a count of how many more start than end.
Any stretch of time where the two are equal becomes an interval in your answer.
Here is an explanatory example. Suppose we have the following intervals:
04:00 - 09:00
15:00 - 20:00
08:00 - 12:00
We get the following list of events from them.
04:00 start
09:00 end
15:00 start
20:00 end
08:00 start
12:00 end
Add 2 more to bookend the day
00:00 analysis_start
24:00 analysis_end
Which get sorted into:
00:00 analysis_start
04:00 start
08:00 start
09:00 end
12:00 end
15:00 start
20:00 end
24:00 analysis_end
And now we process them to come up with the following counts of open intervals:
00:00 - 04:00 0
04:00 - 08:00 1
08:00 - 09:00 2
09:00 - 12:00 1
12:00 - 15:00 0
15:00 - 20:00 1
20:00 - 24:00 0
And now the answer is where our tally was 0:
00:00 - 04:00
12:00 - 15:00
20:00 - 24:00

One possible way to do it could be to have an array of size 1440 (number of minutes). You can initialize all to 0 indicating all are free minutes.
For each vacancy interval you need to add, flip the values from 0 -> 1 in that interval, where 1 indicates working minute.
Whenever you need the array, you can iterate through the array and find "collections" of 0s and 1s for free-time and vacancies.
However, this is a very crude way of doing this and every query (update and select) takes linear time. You can do much better performancewise if you implement this whole thing as a segment tree (Read RMQ - range minimum query) where time complexity of your updates and selects will be logarithmic. Take this decision based on number of updates and selects and how you want your performance of code. eg. If total queries are ~10k, you need not go for segment tree. If they be ~10^5 or more, then you should.

Related

Best/suitable algorithm for project slot assignment problem?

I have a requirement where there are multiple projects available with corresponding schedule dates and schedule reviewing dates.
There will be N number of projects available and each project will have a schedule date as part of the deliverable process. The requirement is to assign each project to the earliest available project schedule review slot. Lets say a project is having a schedule date which is tomorrow, i need to find a available review date slot prior to the schedule date and assign the project based on priority level. Maximum I can assign a project to the earliest available slot is 7 days prior to the actual scheduling date of the project.
For example
Project
Complexity
Schedule Date
schedule review slot
1
A
15-02-2022
12-02-2022 09:00 - 10:00
2
B
15-02-2022
12-02-2022 11:00 - 12:00
3
C
15-02-2022
12-02-2022 12:00 - 01:00
4
A
16-02-2022
13-02-2022 09:00 - 10:00
My desired output
Project
Complexity
Schedule Date
schedule review slot
1
A
15-02-2022
12-02-2022 09:00 - 10:00
4
A
16-02-2022
12-02-2022 12:00 - 01:00
2
B
15-02-2022
12-02-2022 11:00 - 12:00
3
C
15-02-2022
13-02-2022 09:00 - 10:00
I am new to algorithm concepts. So could anyone please guide me in this
What you can do is the following:
Sort the projects into groups with equal complexity/priority, so that all projects with priority A are in the same group, all projects with priority B are in the same group and so on.
Sort the projects in each group by their schedule date, so that the projects with the earliest schedule date come first.
Sort the awailable review time slots by time, so that the earliest time slot comes first.
With this input you can go with the following algorithm:
Take the first (earliest) time slot. Let's call it T.
Take the first (earliest) project P from the sorted priority A list. If P is scheduled less than 7 days after T, assign it to T, remove T and P from their lists and go to step 1.
Else try the same for the earliest project from the priority B and C lists. If none is scheduled less than 7 days after the T, leave the time slot T unassigned and move back to step 1.

schedule algorithm for minimal contact time and group size for my child's daycare

Given the new legal regulations due to covid-19 the daycare of my child and all involved parents are overwhelmed and we need to come up with a schedule of when which child can be cared for.
Given the demanded care time per child (s. below), We need an algorithm to optimize the following:
Minimal total contacts / fixed groups. If children meet, it is best if they stay in that group and do not see children of other groups.
While point 1 is more important, the 2nd prio would be to reduce the size per group or maybe it should be phrased as minimal count of different children met per child
Even less important: reduce total contact time.
(Maybe there are other requirements, that I overlooked?)
The demands are of following nature (Timespan and type) :
Case
Child
Timespan
Type
(1) Fixed time, Required
1
Monday, 8:30 - 13:00
Required
(2) Fixed time, nice to have
1
Tuesday, 8:30 - 13:00
Nice to have
(see (1))
1
Tuesday, 13:00 - 16:00
Required
(see (1))
1
Thursday, 8:30 - 13:00
Required
(see (1))
2
Monday 8:30 - 13:00
Required
(3) Flexibel date, required
2
Any 2 other days 8:30 - 13:00
Required
(4) Flexibel date, nice to have
2
Any day 13:00 - 16:00
Nice to have
(5) Flexibel datetime, Required
3
3 hours
Required
(6) Flexibel datetime, Nice to have
3
3 additional hours
Nice to have
...
...
...
Required = The child must have daycare
Nice to have = daycare is demanded but not required. E.g. if child 1 meets child 2 and 3 on Monday and Thursday, it would be fine to meet the same children on Tuesday morning as well, but if it is a completely different group of children than this would not make sense.
All provided timespans must stay in one continuous piece (meaning that 3 hours cannot be split up into multiple slots).
Additional Information
There is only one room available.
There are 15 children in total.
If a solution is much better than the other it is ok-ish to violate "Required" demands for a few cases. We might be able to find a different solution for the parents in few situations. The algorithm should hence contain a parameter like maxAllowedViolations - let's say it's 3 and it should compare how much the solution is better than without the errors.
The demand is provided per week and might change from week to week. I only know the demand one week in advance. The ideal grouping hence might change per week, but it might be better to respect the grouping of the last week as a guidance, because corona has about 7 to 10 days of incubation time.
The caregivers are tested for covid-19 twice a week, the children are not.
I do not care in which language or pseudo-code-ish way the algorithm is, but I will try to implement the algorithm in a web-based format so other daycare centers can use it as well.

Finding closest time before/after another time

I have a database of time's that are not necessarily exact to the specified time. For example, if I want to pick 12:00 PM and my columns have times in column A:
[9:00 AM, 11:55 AM, 2:00 PM, 6:00 PM],
The closest before would be 11:55 AM, and closest after would be 2:00 PM. My attempt at the code:
=MAXIF(A:A, A:A, 12:00)
=MINIF(A:A, A:A, 12:00)
But to no avail, anythoughts?
You want the max where the time is less than or equal to the criteria and the Min for the time greater than or equal to the criteria:
=MAXIFS(A:A, A:A,"<=12:00")
=MINIFS(A:A, A:A,">=12:00")

How to store periodicity in Oracle?

Which column type should be used to store a periodicity in Oracle?
Daily: time, e. g. "14:00"
Weekly: day of week, time, e. g. "Mon 14:00"
Monthly: day of month, time, e. g. "24 14:00"
Yearly: month, day, time, e. g. "12-24 14:00"
Week of year: calendar week, day, time, e. g. "W01 Mon 14:00"
Especially because of the calendar week I guess storing a pattern as text in ISO 8601 format could work. Or is there a better solution that works e. g. on timestamps?
The intent is only to store a pattern in the database. This pattern will then be used to create a periodicity for dates in a given time interval.
Example using Jodatime in Java:
Start DateTime: 2017-01-01
End DateTime: 2017-01-03
Periodicity: Daily, 02:00 - 06:00
(this is what my question is about, i. e. how to store it)
In Java the following intervals will be created using these data, the created intervals will not be stored in the database, they'll be only processed:
2017-01-01 02:00 - 2017-01-01 06:00
2017-01-02 02:00 - 2017-01-02 06:00

Business opening hours grouping algorithm

I have row of data like this that represent a business opening hours
day / opentime / closetime / isOpen
0 09:00:00 17:00:00 true
1 09:00:00 17:00:00 true
2 08:00:00 17:00:00 true
3 09:00:00 17:00:00 true
4 09:00:00 17:00:00 true
5 false
6 09:00:00 17:00:00 true
with day being an Integer from 0-6 (mon to sunday) and iOpen
Before Re-inventing the wheel and start thinking on a new algorithm I would like to know if there already some algo that would do something similar to this:
MON - TUE 9am - 5pm
WED 8am - 5pm
THU - FRI 9am - 5pm
SUN 9am - 5pm
basically grouping the day that have the opening and closing time together ?
I'm not asking for a ready to go algorithm but more of an advice to where to look if there is already something similar that has been done.
ps: bonus question. Is the way I store the data efficient to achieve my goal ?
This is a fairly straightforward algorithm. Just write down in code what you would do as a human.
As a human, when I look at the data you give me, I start with Monday, and I write down the open/close times on a sheet of paper and write "Monday" next to them. Then I look at Tuesday. If Tuesday has the same open/close times as the previous day, then I simply go to where the time was already written on my paper and add "Tuesday" right next to Monday. I keep doing this until I find an open/close time that is not the same as the previous day's. In that case, I go to a new line on my paper and write down the new open/close times and continue on like this until the end.
If I were to program this in C, I would use an array of structs, where the struct merely had the open time, the close time, and an array of character arrays (ie, an array of strings) to store the dates associated with those hours. (Note: there could be more efficient ways of storing this, but this seems good enough for your purposes)

Resources