Download presentation

Presentation is loading. Please wait.

Published byMakaila Parkhill Modified over 2 years ago

2
CHAPTER 10 ALGORITHM DESIGN TECHNIQUES §1 Greedy Algorithms Optimization Problems: Given a set of constrains and an optimization function. Solutions that satisfy the constrains are called feasible solutions. A feasible solution for which the optimization function has the best possible value is called an optimal solution. The Greedy Method: Make the best decision at each stage, under some greedy criterion. A decision made in one stage is not changed in a later stage, so each decision should assure feasibility. 1/7

3
§1 Greedy Algorithms Note: Greedy algorithm works only if the local optimum is equal to the global optimum. Greedy algorithm does not guarantee optimal solutions. However, it generally produces solutions that are very close in value (heuristics) to the optimal, and hence is intuitively appealing when finding the optimal solution takes too much time. Note: Greedy algorithm works only if the local optimum is equal to the global optimum. Greedy algorithm does not guarantee optimal solutions. However, it generally produces solutions that are very close in value (heuristics) to the optimal, and hence is intuitively appealing when finding the optimal solution takes too much time. 1. A Simple Scheduling Problem Given N jobs j 1, j 2, …, j N, their running times t 1, t 2, …, t N, and one processor. Schedule jobs to minimize the average completion time. /* assume nonpreemptive scheduling */ The Single Processor Case 2/7

4
§1 Greedy Algorithms Example job time j1j1 j2j2 j3j3 j4j4 158310 Schedule 1 j1j1 015 j2j2 23 j3j3 26 j4j4 36 T avg = ( 15 + 23 + 26 + 36 ) / 4 = 25 Schedule 2 j1j1 36 j2j2 110 j3j3 3 j4j4 21 T avg = ( 3 + 11 + 21 + 36 ) / 4 = 17.75 In general: 0 ji1ji1 ti1ti1 ji2ji2 t i1 + t i2 ji3ji3 t i1 + t i2 + t i3 … Best scheduling is to make { t ik } nondecreasing. 3/7

5
§1 Greedy Algorithms The Multiprocessor Case – N jobs on P processors Example P = 3 job time j1j1 j2j2 j3j3 j4j4 35610 j5j5 j6j6 j7j7 j8j8 11141518 j9j9 20 An Optimal Solution j1j1 3 j2j2 5 j3j3 6 0 j4j4 13 j5j5 16 j6j6 20 j7j7 28 j8j8 34 j9j9 40 Another Optimal Solution j1j1 3 j2j2 5 j3j3 60 j4j4 15 j5j5 14 j6j6 20 j7j7 30 j8j8 38 j9j9 34 Minimizing the Final Completion Time NP Hard An Optimal Solution j1j1 3 j2j2 5 j3j3 9 j4j4 19 j5j5 16 j6j6 14 j7j7 j8j8 34 j9j9 0 4/7

6
§1 Greedy Algorithms Flow Shop Scheduling – a simple case with 2 processors Consider a machine shop that has 2 identical processors P 1 and P 2. We have N jobs J 1,..., J N that need to be processed. Each job J i can be broken into 2 tasks j i1 and j i2. A schedule is an assignment of jobs to time intervals on machines such that j ij must be processed by P j and the processing time is t ij. No machine processes more than one job at any time. j i2 may not be started before j i1 is finished. Construct a minimum-completion-time 2 machine schedule for a given set of N jobs. Let a i = t i1 0, and b i = t i2. If there is an a i = 0, then we can schedule all the jobs with a j 0 first. Then we insert the jobs with a i = 0 to the front of the schedule list in arbitrary order. Example Given N = 4, T 1234 = ?40 5/7

7
§1 Greedy Algorithms Proposition An optimal schedule exists if min { b i, a j } min { b j, a i } for any pair of adjacent jobs J i and J j. All the schedules with this property have the same completion time. Algorithm { Sort { a 1,..., a N, b 1,..., b N } into non-decreasing sequence ; m = minimum element ; do { if ( ( m == a i ) && ( J i is not in the schedule ) ) Place J i at the left-most empty position ; else if ( ( m == b j ) && ( J j is not in the schedule ) ) Place J j at the right-most empty position ; m = next minimum element ; } while ( m ); } Example Given N = 4, Sorting { b 2 a 1 a 2 b 1 a 3 b 3 a 4 b 4 } J2J2 J1J1 J3J3 J4J4 T = O( N log N ) T 1342 = ?38 6/7

8
§1 Greedy Algorithms Due: Monday, January 15 th, 2006 at 10:00pm Bonus Problem 1 The Best ACT Hint: Carefully consider the range of integers to avoid RunTime Error. Detailed requirements can be downloaded from http://10.71.45.99/list.asp?boardid=47 http://10.71.45.99/list.asp?boardid=47 Courseware Download 7/7

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google