This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Illinois Institute of Technology Department of Computer Science Lecture 12: March 4, 2009 CS 430 Introduction to Algorithms Spring Semester, 2009 "Greed is good. Greed works." Gordon Gekko (the 1987 movie Wall Street ). "Greedy is pretty good. Greedy sometimes works." Edward Reingold (CS 430, Spring, 2009). 1 The Greedy Heuristic Greedy algorithms make decisions according to a greedy heuristic which chooses the best local decision possible without paying attention to the long-term consequences. For example, a greedy hare competing with a turtle, might run as fast as he can for as long as he can, and then have to rest for a long time because he is tired. The non-greedy turtle, on the other hand, might conserve his energy and be able to beat the hare by not requiring a rest. Greedy algorithms are appropriate for many types of problems, and we will look at some examples where the greedy algorithm works well. 2 Activity Selection Consider a simplified version of job scheduling: You are given a list of jobs J 1 , J 2 , J 3 , . . . J n to run on a single processor; each job has a start time s i when it must start and finish time f i at which point it must conclude. However, the processor can only run one job at any given moment. Your task is to schedule the maximum number of jobs to run on the processor. Clearly this formulation of job scheduling is unrealistic because it ignores important constraints such as the fact that some jobs are more important than others and that some jobs absolutely must be run for the system to remain operational, etc. It is precisely this simplistic formulation which allows the greedy heuristic to work well. Consider the following list of jobs: CS 430Spring, 2009 2 Lecture 12: March 4, 2009 There are many ways to do optimally schedule these jobs. In the exhaustive scheduling method, we could just examine every subset of these jobs and see which subset has the largest cardinality (i.e. number of jobs). There are 2 n ways to make a subset of a set of size n , so our exhaustive algorithm will require exponential time. If we allow for some preprocessing, we could significantly improve this algorithm using the greedy heuristic. Specifically, consider first sorting the jobs in ascending order of finish time: Now, we may start at the top of our sorted list and greedily include the first job in our schedule. The second and third jobs conflict with our current job selection, so we skip over them and pick add the fourth job to our schedule. In this way we can go through the entire list of jobs, greedily adding jobs that fit to our schedule to get the following list of jobs: This method is called the GREEDY ACTIVITY SELECTOR , where activity refers to a job, in the current context. The code for GREEDY ACTIVITY SELECTOR may be found in CLRS (page 378). We will now analyze the running time of this algorithm....
View Full Document
- Spring '08