Download presentation

Presentation is loading. Please wait.

Published byThaddeus Swire Modified about 1 year ago

2
2. 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 §1 Greedy Algorithms Review: 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/19

3
§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 … 2/19 Discussion 21: What is the total cost? How can we be "greedy"?

4
§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 3/19

5
§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. 〖 Example 〗 Given N = 4, T 1234 = ?40 4/19 Discussion 22: What if a i = 0?

6
§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, T = O( N log N ) 5/19 Discussion 23: What is the optimal solution?

7
§1 Greedy Algorithms 3. Approximate Bin Packing The Knapsack Problem A knapsack with a capacity M is to be packed. Given N items. Each item i has a weight w i and a profit p i. If x i is the percentage of the item i being packed, then the packed profit will be p i x i. An optimal packing is a feasible one with maximum profit. That is, we are supposed to find the values of x i such that obtains its maximum under the constrains Q: What must we do in each stage? A: Pack one item into the knapsack. Q: On which criterion shall we be greedy? maximum profit minimum weight maximum profit density p i / w i 6/19 Discussion 24: n = 3, M = 20, (p1, p2, p3) = (25, 24, 15) (w1, w2, w3)= (18, 15, 10) Solution is...? Sunny Cup 2004 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2109

8
§1 Greedy Algorithms Given N items of sizes S 1, S 2, …, S N, such that 0 < S i 1 for all 1 i N. Pack these items in the fewest number of bins, each of which has unit capacity. The Bin Packing Problem 〖 Example 〗 N = 7; S i = 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8 0.8 0.2 B1B1 0.7 0.3 B2B2 0.1 0.5 0.4 B3B3 An Optimal Packing NP Hard 7/19

9
§1 Greedy Algorithms On-line Algorithms Place an item before processing the next one, and can NOT change decision. 〖 Example 〗 S i = 0.4 0.4, 0.4 0.4, 0.6 0.6, 0.6 0.6 You never know when the input might end. Hence an on-line algorithm cannot always give an optimal solution. 【 Theorem 】 There are inputs that force any on-line bin-packing algorithm to use at least 4/3 the optimal number of bins. 8/19

10
§1 Greedy Algorithms Next Fit void NextFit ( ) { read item1; while ( read item2 ) { if ( item2 can be packed in the same bin as item1 ) place item2 in the bin; else create a new bin for item2; item1 = item2; } /* end-while */ } 【 Theorem 】 Let M be the optimal number of bins required to pack a list I of items. Then next fit never uses more than 2M bins. There exist sequences such that next fit uses 2M – 2 bins. 9/19

11
§1 Greedy Algorithms First Fit void FirstFit ( ) { while ( read item ) { scan for the first bin that is large enough for item; if ( found ) place item in that bin; else create a new bin for item; } /* end-while */ } Can be implemented in O( N log N ) 【 Theorem 】 Let M be the optimal number of bins required to pack a list I of items. Then first fit never uses more than 17M / 10 bins. There exist sequences such that first fit uses 17(M – 1) / 10 bins. Best Fit Place a new item in the tightest spot among all bins. T = O( N log N ) and bin no. < 1.7M 10/19

12
§1 Greedy Algorithms 〖 Example 〗 S i = 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8 Next FitFirst FitBest Fit 〖 Example 〗 S i = 1/7+ , 1/7+ , 1/7+ , 1/7+ , 1/7+ , 1/7+ , 1/3+ , 1/3+ , 1/3+ , 1/3+ , 1/3+ , 1/3+ , 1/2+ , 1/2+ , 1/2+ , 1/2+ , 1/2+ , 1/2+ where = 0.001. The optimal solution requires ? bins. However, all the three on-line algorithms require ? bins. 6 10 11/19 Discussion 25: Please show the results.

13
§1 Greedy Algorithms Off-line Algorithms View the entire item list before producing an answer. Trouble-maker : The large items Solution : Sort the items into non-increasing sequence of sizes. Then apply first (or best) fit – first (or best) fit decreasing. 〖 Example 〗 S i = 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8 0.8, 0.7, 0.5, 0.4, 0.3, 0.2, 0.1 0.2 0.4 0.1 0.3 0.7 0.8 0.5 【 Theorem 】 Let M be the optimal number of bins required to pack a list I of items. Then first fit decreasing never uses more than 11M / 9 + 4 bins. There exist sequences such that first fit decreasing uses 11M / 9 bins. Simple greedy heuristics can give good results. 12/19

14
Research Project 12 Bin Packing Heuristics (25) This project requires you to implement and compare the performance (both in time and number of bins used) of the various bin packing heuristics, including the on- line, next fit, first fit, best fit, and first-fit decreasing algorithms. Detailed requirements can be downloaded from http://acm.zju.edu.cn/dsaa/ 13/19

15
§2 Divide and Conquer Divide: Smaller problems are solved recursively (except base cases). Conquer: The solution to the original problem is then formed from the solutions to the subproblems. Cases solved by divide and conquer The maximum subsequence sum – the O( N log N ) solution Tree traversals – O( N ) Mergesort and quicksort – O( N log N ) Note: Divide and conquer makes at least two recursive calls and the subproblems are disjoint. 14/19

16
§2 Divide and Conquer 1. Running Time of Divide and Conquer Algorithms 【 Theorem 】 The solution to the equation T(N) = a T(N / b) + (N k log p N ), where a 1, b > 1, and p 0 is 〖 Example 〗 Mergesort has a = b = 2, p = 0 and k = 1. T = O( N log N ) 〖 Example 〗 Divide with a = 3, and b = 2 for each recursion; Conquer with O( N ) – that is, k = 1 and p = 0. T = O( N 1.59 ) If conquer takes O( N 2 ) then T = O( N 2 ). 15/19

17
§2 Divide and Conquer 2. Closest Points Problem Given N points in a plane. Find the closest pair of points. (If two points have the same position, then that pair is the closest with distance 0.) Simple Exhaustive Search Check ? pairs of points. T = O( ? ).N ( N – 1 ) / 2N 2 Divide and Conquer – similar to the maximum subsequence sum problem 〖 Example 〗 Sort according to x-coordinates and divide; Conquer by forming a solution from left, right, and cross. 16/19

18
§2 Divide and Conquer It is so simple, and we clearly have an O( N log N ) algorithm. It is O( N log N ) all right. But is it really clearly so? Just like finding the max subsequence sum, we have a = b = 2 … How about k ? Can you find the cross distance in linear time? 17/19

19
§2 Divide and Conquer - strip If NumPointInStrip =, we have /* points are all in the strip */ for ( i=0; i

20
Research Project 13 Quoit Design (25) Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded. In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring. Detailed requirements can be downloaded from http://acm.zju.edu.cn/dsaa/ 19/19

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google