• Last Updated : 08 Jul, 2021

What is scheduling ?
Scheduling is another popular problem in computer science domain. A scheduling problem is a problem of scheduling resources effectively for a given request. In operating systems, often a single processor of a computer system may encounter many jobs or user programs. One can visualize the scheduling problem as an optimal ordering of jobs such that job turnaround time is minimized. In short, aim is to schedule jobs in an optimal order so as to execute jobs faster.

Let’s visualize scenario of scheduling :
In a library there is only one librarian who is assigned the issues for that day and there are several students who are in a queue to register their issues for books, so the librarian thought of a problem to make it a bit reordering that might help satisfying all students and reduces the issue time. so in this case what would be optimal order?

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

Obviously, the student with less number of books in the queue will take less time to an issue than the student with a larger number of books. It makes sense to process the student with less number of books, so as to minimize the collective waiting time of students.

Scheduling of Jobs without Deadline :
So, what is the scheduling of jobs without a deadline?

• The goal of a scheduling problem is to schedule tasks such that the maximum total profit is obtained.
• The jobs that are considered for scheduling have no deadlines, i.e. the jobs that do not have any upper limit of execution time.
• This scheduling of jobs aims for a profit but without the deadline of the tasks.

To understand this concept, let’s take an example.
Let us assume the jobs that are connected to scheduling have no deadlines, i.e.  the jobs do not have any upper limit of execution time. Trying to solve it with a greedy approach ;
Let us assume that the service time is associated with the following three tasks :

• t1=2
• t2=7
• t3=4

For scheduling these tasks, let us first find optimal ordering by listing out all possible orderings made in the table below. Since there are three tasks, the tasks can be ordered in 3! ways, which equals 6. In general, for N jobs, there would be N! possible orders.

• To illustrate this, let’s take one of combinations, say[1, 2, 3]. If job 1 is scheduled first, then as per the given problem, job 1 requires 2-time units. Since this is the first job, there is no wasting time.
• During this time, job 2 has to wait as there is only one processor. Therefore, job 2 would take 2 ( waiting time) + 7(processing time ), which equals 9. Till this time, job 3 has to wait.
• The waiting time for job 3 is ( 2 + 7 ) and it requires 4-time units to complete. Therefore, the total time spent by job 3 in the system is 9( waiting time ) and 4 is the (processing time).

The average time of a job is obtained by dividing total time in system by number of jobs, i.e. 24/3=8.
Computations for the remaining jobs are shown in the table above.

• It can be observed that the minimum total time in system is 21, with a turnaround time of 7.
• This corresponds to order [1, 3, 2]. Therefore, this is the optimal order.
• One can also note that tasks are arranged in an ascending order of service time, i.e., {2,4,7}, which thereby illustrates principle of ‘shortest job first scheduling’, i.e., the shortest job is scheduled first before scheduling longer jobs.
• Once this principle is identified, the greedy algorithm can be written effectively.

The greedy algorithm informally for this problem can be written as follows :

Step 1 – Sort the jobs by service time in non-decreasing order.
Step 2 – Schedule next job of the sorted job list. Including this in the solution set.
Step 3 – If all instances of a sorted job list are solved, then return solution set.

The formal algorithm is given as follows :
Input – An array J with a set of jobs 1 to n along with service time
Output – An optimal schedule

```Begin
S = {sorted array of jobs in J based on service time }
i = 1
Schedule = ∅
while (i < = n ) do                 //for all jobs do
select the next job i from S    //Selection procedure selects from sorted list
solution = schedule U jobs i
i = i+1

End while
return(Solution)
End```
• The algorithm first sorts jobs, then selects jobs greedily one by one and schedules them to the server.
• It can be observed that, as there is no deadline involved, all jobs can be scheduled for the server.

Complexity Analysis :
It can be observed that the core principle of the aforementioned scheduling algorithm is the sorting of tasks
The complexity of sorting is O(n log n) time. All the remaining tasks of that scheduling algorithm would take only O(n) time.
Therefore, the resultant complexity of the scheduling algorithm is max{n log n, n } = n log n time.

My Personal Notes arrow_drop_up