# Algorithm for scheduling unit time tasks with arrival times and deadlines

Suppose we have $$n$$ tasks to order over $$n$$ days. Each tasks takes 1 day to be completed. Each task has a start date when the task becomes available and a deadline when the task must be delivered.

Here is an example:

Is there an algorithm that can solve this problem with a worst case efficiency of $$O(n)$$ where $$n$$ is the number of tasks and days.

• Could you please credit the source of the problem? Proper attribution helps people answer your question. – Apass.Jack Apr 19 at 11:35
• It's a school assignement. – Shanksme Apr 20 at 10:31
• Hm, it looks like there is an $O(n)$ algorithm. – Apass.Jack Apr 20 at 16:18
• Really ? Can you tell me what it is ? – Shanksme Apr 20 at 16:22

The more general version of your problem is the scheduling problem where we have $$n$$ independent tasks, each with an arrival time, a resource requirement (time), and a deadline. We define the Earliest Deadline First algorithm such that at every timepoint, we choose to process the task with the nearest deadline. While time is continuous, it is trivial to see that we can discretize time to be all points at which a task finishes or a new task arrives.

It is known that EDF is an optimal uniprocessor scheduling algorithm i.e. if there is a feasible schedule, EDF will find it and if EDF does not find a feasible schedule, then no feasible schedule exists. [1], [2]. However, this is not linear time if we aren't guaranteed an ordering of the deadlines of the arriving tasks. Let's use a priority queue to represent the waiting tasks. At every time point, which there are $$O(n)$$ of, we must insert and/or pull the next task from a priority queue sorted on deadlines. Each insertion is $$O(\log n)$$ time, thus resulting in $$O(n\log n)$$ time.

If your input was sorted by arrival, we could easily find whether or not there is a valid schedule by following the algorithm above, iterating through the days, adding tasks for that day to a priority queue and selecting the task with the nearest deadline from the queue. We fail if we ever try to assign a task to a day on or past its deadline.

As mentioned in the comments, we can sort the input by arrival time in linear time using counting sort or pigeonhole sort. The issue then is to somehow make the priority queue also linear time.

• This works well but you have to take in account that the sorted order of starts is not the same than deadlines one. Thus, you need two heap queues in the general case. One sorted on increasing starts that unstack on the other on each time step (when current time passes start time). The other is indeed on deadline increasing and let know the next task to schedule on each time step. – Vince Apr 20 at 13:35
• Yes you are right, I should have said you sort by arrival date (after all that’s how the algorithm works). If you sort by start date, and use preemption based off of due date, it will work in your examples and all others. With respect to the sorting, you are also right! I’ll update my answer. – Bryce Kille Apr 20 at 13:39
• @Shanksme as Vince mentioned, we need two things to be sorted. While I agree that we can use a linear time sort on arrival time, I am not quite sure how we could know, at each day, what remaining task has the nearest deadline. Do you have any ideas? – Bryce Kille Apr 20 at 13:52

As each task has the atomic time step as duration, you can consider your scheduling problem as an assignment problem :

You have N tasks and N days and have to assign each task a day. Just consider a bipartite graph with N "task" nodes on a side and N "days" tasks on the other. There are edges between a task and a day when assignement is possible ($$start \le day \le deadline$$). The problem is now to select exactly N edges with one per task and one per day.

Then the best is probably to use an Hopcroft-Karp algorithm to look for the maximum cardinality assignment. If a perfect solution exists, it would be found else the number of "lost" task would be minimized.

This algorithm is $$O(|E|\sqrt N)$$ with $$E$$ the set of edges in the bipartite graph. If the time slots of the tasks are short with respect to the whole time horizon ($$deadline - start \ll N$$), then this algorithm is near-linear in the number of edges $$O(|E|)$$.

• @Apass.Jack You are right, I totally confused $V$ and $E$ here, I correct it, thank you. – Vince Apr 19 at 14:42
• @Apass.Jack about the need of proper attribution, I totally understand your point of view and CS policy on it. On the other hand, this is a school case and I do not expect any clear "propery" behind. But well I will be more patient before posting answers. – Vince Apr 19 at 14:49
• Let $deadline-start=c$ for all tasks. Then $|E|=Nc$. $|V|=2N$. Then $|E|\sqrt{|V|}=\sqrt 2cN^{1.5}$. Suppose $c$ is a constant. Then $O(|E|{\sqrt {|V|}})=O(|E|^{1.5})$. Is that case part of what you meant by "near-linear in the number of edges"? – Apass.Jack Apr 19 at 15:09
• While I agree that the time complexity of $O(|E|\sqrt{N})$ and near linear I’m the edges if we have a sparse bipartite graph, I don’t think that answers the question since it’s possible to have a dense graph, in which case the runtime could be $O(N^{2.5}$, right? – Bryce Kille Apr 19 at 17:06
• @Apass.Jack In fact I just copied the wikipedia line on it. But I realise there is no big-O in it... They probably talk about amortized time. Below they mention that it would be O(|E| log |V|) for a random sparse graph. – Vince Apr 19 at 22:21