# Minimizing maximum lateness : Greedy algorithm

Since we have chosen the greed, let continue with it for one more post at least. Today’s problem is to **minimize maximum lateness of a task**. Let me clarify the problem: given a processor which processes one process at a time and as always given a list of processes to be scheduled on that processor, with the intention that maximum late process should be minimized. Contrary to previous problems, this time, we are not provided with start time and end time, but we are given length of time `t _{i}` process will run and deadline it has to meet

`d`,

_{i}`f`is actual finish time of process completion.

_{i}Lateness of a process is defined as

`l _{i}` = max{0, f

_{i}− d

_{i}}, i.e. the length of time past its deadline that it finishes.

Goal here to schedule all tasks to minimize maximum lateness L = max l

_{i}For example:

## Minimizing maximum lateness : algorithm

Let’s decide our optimization strategy. There is some order in which jobs can be decided: shortest job first, earliest deadline first, least slack time first.

Let’s see if any of the above strategies work for the optimal solution. For shortest processing time first, consider example P1 = (1,100) P2 = (10, 10). If we schedule the shortest job first as in order (P1, P2), lateness will be 91, but if we take them as (P2, P1), lateness will be 0. So, clearly taking the shortest process first does not give us an optimal solution.

Check for the smallest slack time approach. See if you can come up with some counterexample that it does not work.

That leaves us with only one option, take the process which has the most pressing deadline, that is the one with the smallest deadline and yet not scheduled. If you have noticed, the example given for the problem statement is solved using this method. So, we know it works.

- Sort all job in ascending order of deadlines
- Start with time t = 0
- For each job in the list
- Schedule the job at time t
- Finish time = t + processing time of job
- t = finish time

- Return (start time, finish time) for each job

### Minimizing maximum lateness : implementation

from operator import itemgetter jobs = [(1, 3, 6), (2, 2, 9), (3, 1, 8), (4, 4, 9), (5, 3, 14), (6, 2, 15)] def get_minimum_lateness(): schedule =[]; max_lateness = 0 t = 0; sorted_jobs = sorted(jobs,key=itemgetter(2)) for job in sorted_jobs: job_start_time = t job_finish_time = t + job[1] t = job_finish_time if(job_finish_time > job[2]): max_lateness = max (max_lateness, (job_finish_time - job[2])) schedule.append((job_start_time, job_finish_time)) return max_lateness, schedule max_lateness, sc = get_minimum_lateness(); print "Maximum lateness will be :" + str(max_lateness) for t in sc: print t[0], t[1]

The complexity of implementation is dominated by sort function, which is `O(nlogn)`

, rest of processing takes `O(n)`

.

Please share your suggestions or if you find something is wrong in comments. We would love to hear what you have to say. If you find this post interesting, please feel free to share or like.