Appointment scheduling algorithm - algorithm

Suppose you need to schedule meetings for a CEO. There must be as many meetings (count, not duration) scheduled as possible.
The workday has a start time and end time.
There are certain blocks of time unavailable (lunch, etc.)
The input is a list of times. The durations are either 30, 60, or 90 mins.
My approach
Represent the day as a boolean array. The index is the time. Each array index represents 30 minutes. So day[0] represents 9am and day[1] represents 9:30am. day[1] = true means 9:30 is open and closed if false.
Have a counter variable to keep track of the current earliest free time. For each time, iterate through the list of meetings and see if current free time + meeting duration is open. Runtime is O(N*N).
Flaw
However, my method does not result in the highest possible number of meetings squeezed into a day. For example, I have a 120 minute free block, and potential meetings of 90, 60, and 60 minutes. My algorithm first iterates to the 90 minute meeting and schedules it, but there is a 30 minute free block wasted. Better would be to fit both 60 minute meetings in there instead.
Another idea
Get all combinations of potential meetings with a total duration of no more than 90 minutes, and keep track of the the maximum number of meetings. I believe this would have to be done via permutation and O(N*N!) for each free block. So the total runtime of this is O(N * N * N!).
I would appreciate any feedback and alternative solutions. Thanks!

I've got a new algorithm for you, that could be relatively simple to implement. First fit all the 90 min meetings, if some are left over, they would not fit. Now go through the 60 min meetings and place those. If you have some left over, find 90 min meeting with a 30 min window (there could not be 60 min window or the meeting would have fit) and replace it with 2 of the 60 min meetings. If there are still 60 min meetings left over, replace 2 consecutive 90 min meetings with 3 of 60 min. If there are still 60 min meetings left over, replace any 90 min meeting with 60 min meeting. Then go and place 30 min meetings to empty slots. If there are any left over, replace 90 min meetings with 3x 30 min. If there are more left over, replace 60 min meetings.
From what I know from the Knapsack problem is that there should be a polynomial solution and this seems to fit, so if this works, ... also if n is the number of free slots in schedule, this is capped at 6*n^2. It is independent of the number of meetings waiting to be scheduled.

Related

How to find the cheapest combo of metro subscription

Lets say we have the following fares available :
1 trip
2 trips
10 trips
unlimited weekend (saturday to sunday, not 2 days)
1 day
3 days
unlimited week (monday to sunday, not 7 days)
unlimited month (1st to last day of month)
... with a price for every one of them.
The problem is : **How to determine what set of subscriptions to chose given a date of arrival and a date of departure ? **
Lets say we want the solution for n between 1 and 8, n being the number of time we take the metro daily (so we assume we take the metro the same number of time every day)
For example it would say something like :
n = 1
Arriving on Friday 19th and leaving Thursday 23th, the best is taking the 1 trip, then the weekend, then the 2 trips (didnt calculate but you see the point)
n = 2
...
I have found examples with only 1 day, 2 days, 7 days fares with dynamic programming, but it looks a lot harder when you considerate the days of the week.
Thanks :)
I like to view this kind of dynamic program as finding a shortest path in a directed acyclic graph.
Each node of the graph encodes
what the current day is (either during the travel period or the day after), and
how many trips remain on trip-limited passes (at most n + 9).
Each arc represents either
purchasing a specific pass at a specific time (the length of the arc is the cost of this pass), or
using trip-limited passes to cover the day's trips (the length of the arc is zero).
The time-limited passes advance the day to the first day they no longer work. The trip-limited passes increase the number of remaining trips. The zero-cost arcs advance the day by one while decreasing the number of remaining trips by n.
Given the shortest path, it is easy to decode it to a plan for purchasing passes.
(P.S. I don't know what the rules are on, e.g., purchasing a week pass on a Tuesday for the rest of the week. Even if this is not allowed, you're going to want to put arcs for the time-limited passes that could have been purchased on a previous day during the travel period.)

Dynamic Programming - Break Scheduling Problem With Decreasing Work Capacity

Assume you are given t1, t2, t3, ..., tn amount of tasks to finish every day. And once you start working, you can only finish c1, c2, c3, ..., cn tasks until spending 1 day resting. You can spend multiple days resting too. But you can only do the tasks which are given you that day. For example;
T[] = {10, 1, 4, 8} given tasks;
C[] = {8, 4, 2, 1} is the capacity of doing tasks for each day.
For this example, optimal solution is giving a break on the 3rd day. That way you can complete 17 tasks in 4 days:
1st day 8 (maximum 10 tasks, but c1=8)
2nd day 1 (maximum 1 task, c2=4)
3rd day 0 (rest to reset to c1)
4th day 8 (maximum 8 tasks, c1=8)
Any other schedule would result with fewer tasks getting done.
I'm trying to find the recurrence relation for this dynamic programming problem. Can anyone help me? I find this question but mine is different because of the decreasing work capacity and there are different number of jobs each day. Reference
If I got you right you have an amount of tasks to do, t(i) for every day i. Also you have some kind of a given internal restriction sequence c(j) for a current treak day j where j can be reseted to 0 if no task was done that day. Goal is to maximizie the solved tasks.
Naive approach is to store for each day i a list for every state j how many tasks were done. Fill the data for the first day. Then for every following day fill the values for the "no break" case - which is
value(i-1,j-1)+min(t(i),c(j)). Choose the maximum from the previous day to fill the "break" entry. Repeat until last day. Choose the highest value and trace back the path.
Example for above
Memory consumtption is pretty easy: O(number of days * number of states).
If you are only interested in the value and not the schedule the memory consumption would be the O(number of states).
Time consumption is a bit more complex, so lets write some pseudo code:
For each day i
For each possible state j
add and write values
choose maximum from previous day for break state
choose maximum
For each day
trace back path
The choose maximum-function would have a complexity of O(number of states).
This pseudo code results in time consumption O(number of days * number of states) as well.

Probability Events - Total Probability of An Event Happening

I can't seem to work out the below probability question. Wonder if anyone can help? Thanks in advance!
"10 people have made a booking for a shuttle transfer and the departure time is 10am. If
all customers arrive early, the shuttle will depart earlier. The shuttle will wait until
10:10am if any customers are late. If the probability of customers arriving 10 min earlier
is 0.1, 5 min earlier is 0.2, right at 10am is 0.5, late for 5 min is 0.1, late for 10 min is
0.05, and late for over 10min is 0.05. What is the probability for the shuttle to depart on
or before 10am?"
This can be expressed in a binomial distribution if you let p=0.8 be the probability that an individual arrives on time (0.1+0.2+0.5), and let n=10 denote the number of trials.
The probability mass function (PMF) of a binomial distribution is defined as
That is, the probability of observing k successes in n trials with probability p.
Now, the probability whether the shuttle will depart on (or before) time, is equivalent to the probability that all individuals arrive at least on time, that is P(X = k) where k=10 successes.
Using the above PMF we obtain

Calculating total over timespan with arbitrary datapoints

If I am given the total number of occurrences of an event over the last hour, and I can get this data at arbitrary times ( but at least once an hour ), how can I work out the total number of occurrences over a 24 hour period?
Obviously, you can't. For example -- if the first two observations overlap then it would be impossible to determine the number of kills during the overlap. If there is a time gap between the first two observations then there is no way to determine what happened during the gap. You could try to set up a system of equations -- but the resulting system will be underdetermined (but it could give you both a min and a max, which might be relevant).
Why not adopt a statistical approach? Let X = kills over a 1 hour period. This is a random variable. Estimate its expected value by sampling it at randomly chosen times and multiply your estimate by 24.

Event Scheduling Greedy

We are given N ranges of date offsets when N employees are present in an
organization. Something like
1-4 (i.e. employee will come on 1st, 2nd, 3rd and 4th day )
2-6
8-9
..
1-14
We have to organize an event on minimum number of days such that each
employee can attend the event at least twice.Please suggest the algorithm(probably greedy) to do this.
PS: Event is one day event.
If your data is small, you can just brute-force it. Pick all possible combination of 2 days. For each combination, try it and see if everyone can attend both. If not, pick all possible combinations of 3 days, see if everyone can attend 2 out of the 3, and so on. It's exponential, but may not be so bad for your purposes.
The greedy approach is to count how many people are at work each day, and pick the day with the maximum number of people. Repeating, count how many people are at work each day who don't already have two events scheduled and pick the day with the maximum number of people. Of course, don't pick the same day twice.
I think this can be done by the following greedy approach on events sorted with end date
Maintain a num count for all intervals. (Initialize all to 0)
If num = 0 place the two events on the last two days of this interval.
If num = 1 place one event on the last day of this interval
If num = 2 already two events have been covered for this interval.
Placing on the event in an interval can lead to increase in num count of the succeeding event.

Resources