33
MS 101: Algorithms Instructor Neelima Gupta [email protected]

Instructor Neelima Gupta [email protected]. Table of Contents Greedy Algorithms

Embed Size (px)

Citation preview

MS 101: Algorithms

MS 101: AlgorithmsInstructorNeelima [email protected]

Table of ContentsGreedy AlgorithmsWhat is greedy approach?Choosing a current best solution without worrying about future. In other words the choice does not depend upon future sub-problems.

Such algorithms are locally optimal,

For some problems, as we will see shortly, this local optimal is global optimal also and we are happy.

General Greedy ApproachStep 1:Choose the current best solution.Step 2:Obtain greedy solution on the rest.When to use?There must be a greedy choice to make.

The problem must have an optimal substructure.Activity Selection ProblemGiven a set of activities, S = {a1, a2, , an} that need to use some resource.Each activity ai has a possible start time si & finish time fi, such that 0 si < fi < We need to allocate the resource in a compatible manner, such that the number of activities getting the resource is maximized.The resource can be used by one and only one activity at any given time..Activity Selection ProblemTwo activities ai and aj are said to be compatible, if the interval they span do not overlap. ..i.e. fi sj or fj si

Example:Consider activities: a1, a2, a3, a4s1--------f1 s2---------f2 s3------f3s4------f4Here a1 is compatible with a3 & a4a2 is compatible with a3 & a4But a3 and a4 themselves are not compatible.Activity Selection ProblemSolution: Applying the general greedy algorithm

Select the current best choice, a1 add it to the solution set.Construct a subset S of all activities compatible with a1, find the optimal solution of this subset.Join the two. Lets think of some possible greedy solutionsShortest Job First

In the order of increasing start times

In the order of increasing finish times

Thanks to: Navneet Kaur(22), MCA 2012Time2job1job234567891011job3 1 013121514Shortest Job First

Thanks to: Navneet Kaur(22), MCA 2012Time2job1job234567891011job3 1 013121514Shortest Job First

Thanks to: Navneet Kaur(22), MCA 2012Time2job1job234567891011job3 1 013121514OPTIMAL SCHEDULESCHEDULE CHOSEN BY THIS APPROACHShortest Job First

Thanks to: Navneet Kaur(22), MCA 2012Time0job1job224681012141618job320Increasing Start Times

Thanks to: Navneet Kaur(22), MCA 2012Time0job1job224681012141618job320Increasing Start Times

Thanks to: Navneet Kaur(22), MCA 2012Time0job1job224681012141618job320SCHEDULE CHOSEN BY THIS APPROACHOPTIMAL SCHEDULEIncreasing Start Times15 iSi Fi Pi Thanks to Neha (16) 224 3115 10346 4458 20569 2 Increasing Finishing Times

Thanks to Neha (16) Increasing Finishing TimesTime0P(1)=10P(3)=4P(4)=20 P(2)=3123456789P(5)=2Increasing Finishing Times Thanks to Neha (16) Time0P(1)=10P(3)=4P(4)=20 P(2)=3123456789P(5)=2.

ACTIVITY SELECTION PROBLEMWe include a in the solution. And then recurse on S = {a S-{a} : a is compatible with a} where S is input set of activities.Thanks to: Navneet Kaur(22), MCA 201219Proving the OptimalityScheduling Jobs with Processing times and Deadlines Jobs are given with processing times pi and deadlines di.There is no specified start time. A job can be scheduled at any time. Algorithm decides the start time ( and hence the finish time) of a job.Let fi be the time at which a job finishes as per some schedule. Then its lateness is defined as li = fi diif fi>di 0, otherwiseAim : minimize max lateness i.e. minimize max{li}

Figures from Anjali, HemantPossible Greedy ApproachesShortest Job First: completely ignores half of the input data viz. the deadlinesDoesnt work : t1 = 1, d1=100, t2=10, d2=10 Minimum Slackness FirstDoesnt work : t1 = 1, d1=2, t2=10, d2=10Earliest Deadline First: completely ignores the other half of the input data viz. the processing time.but it works.gives the optimalSJF fails: figure from Anjali, HemantbackMinimum Slackness FirstLet si be the time by which the job must be assigned to meet the deadline. i.e. si = di piLet the last job scheduled finishes at time t. Then slack-time for job i is defined as sti = si t.Thus slack time represents, how much we can wait/defer to schedule the ith job. We should schedule the next job for which this time is minimum. i.e. sti is minimum. Since t is same for all the jobs, the job with minimum si is scheduled next.MSF fails: figure from Anjali, HemantbackEarliest Deadline Firstj1j2 j3f1=p1f2=f1+p2d1