


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Lecture 8 Greedy Algorithms Single-source shortest path (Dijkstra’s algorithm), Scheduling Shortest Path Problem Problem Specification Input: Weighted, graph G = (V,E), source node s Weight (length) function w on each edge e in E Task: Compute the length of the shortest path from s to every node v in V (Compute the actual shortest paths from s to very node v in V) Dijkstra’s algorithm Initialize set of completed nodes S to be {s} Initialize D[v] to be the length of the shortest path to v where all intermediate nodes are in S
Minimum average (total) waiting time scheduling Problem Specification Input: Set of jobs {1, 2, ..., n} with processing times {ti} Task: Find an ordering of these jobs on a single processor that minimizes T, the sum of their waiting times in the system T = (time in system for job i) = ci where ci is the completion time of each job i Greedy Algorithm The Shortest Processing Time First (SPT) Algorithm There are n! different permutations of the n jobs What is a greedy strategy for ordering these jobs? Choose jobs in order of processing times Implementation Sort jobs by processing time: (n log n) Proof of Correctness Assume SPT is not optimal Consider any optimal schedule (permutation) of the n jobs P Let the jobs be numbered by their order in P Assuming that SPT is not optimal, there must be some job i such that ti > ti+ Construct schedule P’ swapping jobs i and i+ The time in the system for all other jobs is identical to P The time in the system for job i in P’ is identical to that of job i+1 in P The time in the system for job i+1 in P’ is strictly less than that of job i in P This is because ti > ti+ This is a contradiction as P was supposed to be optimal Thus, SPT is optimal Alternative proof of correctness If we look at the mathematical sum, we get T = nt 1 + (n-1)t 2 + (n-2)t 3 + ... + 2 tn-1 + tn This is clearly minimized by making t 1 the minimum possible, t 2 the second minimum possible, ... Minimizing average waiting time with release times and preemptions Problem changes: jobs have release times ri and jobs can be preempted. Release times: Job i cannot be processed until time ri (in the above problem, for all i, ri = 0) Preemptions: We can process a job, stop it to process another job, and then resume processing the original job with no penalty Input: Set of jobs {1, 2, ..., n} with processing times {ti} and release times {ri} Task: Find a preemptive schedule of these jobs on a single processor that minimizes T, the sum of their waiting times in the system T = (time in system for job i) = (ci – ri) Greedy Strategy? Implementation cost? Proof of correctness?
Improved implementation Revised feasibility check: Lemma: A set of n jobs J is feasible if and only if we can construct a feasible sequence including all the jobs in J as follows: Start with an empty schedule of length n. For each job i in J, schedule i at the latest time t in the range [1, min(n,di)] that is free. Proof: Suppose we are unable to add a job with deadline d Let s be the earliest free slot prior to the addition of this job. We know that d<s. Furthermore, all the jobs currently in slots 1 through s-1 also have deadlines < s. Thus we have s jobs with deadlines at most s-1. The set of jobs is infeasible. Other direction obvious. Improved implementation Definitions: For any time slot t, let nt be the largest time slot less than or equal to t that is free (0 if there is none) Define two time slots i and j to be in the same set if ni = nj For a given set of time slots K, define F(K) to be the smallest member of K. Use disjoint set structures to improve efficiency Initialize each time slot to be a singleton set containing only itself Checking feasibility of job j with deadline dj Find set K containing dj If F(K) = 0, reject job j Else Assign job j to F(K) Find set L containing F(k)- Merge K and L Update F(resulting merged set) Analysis: Instead of linear work for each feasibility check, we now have 2 finds and a merge This requires time that is barely larger than constant Running time is now dominated by sorting step and is (n log n). Almost linear time if data is presorted.