2. A Simple Scheduling Problem Given N jobs j 1, j 2, …, j N, their running times t 1, t 2, …, t...
-
Upload
thaddeus-swire -
Category
Documents
-
view
212 -
download
0
Transcript of 2. A Simple Scheduling Problem Given N jobs j 1, j 2, …, j N, their running times t 1, t 2, …, t...
2. A Simple Scheduling Problem
Given N jobs j1 , j2 , …, jN , their running times t1 , t2 , …, tN , 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
§1 Greedy Algorithms
〖 Example 〗 job
time
j1 j2 j3 j4
15 8 3 10
Schedule 1 j1
0 15j2
23j3
26j4
36
Tavg = ( 15 + 23 + 26 + 36 ) / 4 = 25
Schedule 2 j1
36j2
110j3
3j4
21
Tavg = ( 3 + 11 + 21 + 36 ) / 4 = 17.75
In general:
0
ji1
ti1
ji2
ti1 + ti2
ji3
ti1 + ti2 + ti3
… …
2/19
Discussion 21:Discussion 21: What is the total cost? How can we be "greedy"? What is the total cost? How can we be "greedy"?
§1 Greedy Algorithms
The Multiprocessor Case – N jobs on P processors
〖 Example〗
P = 3
job
time
j1 j2 j3 j4
3 5 6 10
j5 j6 j7 j8
11 14 15 18
j9
20
An Optimal Solution
j1
3
j2
5
j3
60
j4
13
j5
16
j6
20
j7
28
j8
34
j9
40
Another Optimal Solution
j1
3
j2
5
j3
60
j4
15
j5
14
j6
20
j7
30
j8
38
j9
34
Minimizing the Final Completion Time
NP Hard
An Optimal Solution
j1
3
j2
5
j3
9
j4
19
j5
16
j6
14
j7
j8
34
j9
0
3/19
§1 Greedy Algorithms
Flow Shop Scheduling – a simple case with 2 processors
Consider a machine shop that has 2 identical processors P1 and P2 . We have N jobs J1, ... , JN that need to be processed. Each job Ji can be broken into 2 tasks ji1 and ji2 . A schedule is an assignment of jobs to time intervals on machines such that
jij must be processed by Pj and the processing time is tij .
No machine processes more than one job at any time.
ji2 may not be started before ji1 is finished.
Construct a minimum-completion-time 2 machine schedule for a given set of N jobs.
Let ai = ti1 0, and bi = ti2 .
〖 Example 〗 Given N = 4,
15926
10843J T1234 = ?40
4/19
Discussion 22:Discussion 22: What if What if aaii = =
0? 0?
§1 Greedy Algorithms【 Proposition 】 An optimal schedule exists if min { bi , aj } min { bj , ai } for any pair of adjacent jobs Ji and Jj . All the schedule
s with this property have the same completion time.Algorithm
{ Sort { a1 , ... , aN , b1 , ... , bN } into non-decreasing sequence ; m = minimum element ; do { if ( ( m == ai ) && ( Ji is not in the schedule ) ) Place Ji at the left-most empty position ; else if ( ( m == bj ) && ( Jj is not in the schedule ) ) Place Jj at the right-most empty position ; m = next minimum element ; } while ( m );}
〖 Example 〗 Given N = 4,
15926
10843J
T = O( N log N )
5/19
Discussion 23:Discussion 23: What is the optimal What is the optimal solution? solution?
§1 Greedy Algorithms3. 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 wi and a profit pi . If xi is the percentage of the item i being packed, then the packed profit will be pi xi .
n
iii xp
1
nixMxwn
iiii
1for ]1,0[ and
1
An optimal packing is a feasible one with maximum profit. That is,
we are supposed to find the values of xi 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 pi / wi
6/19
Discussion 24:Discussion 24: n n = 3, = 3, MM = 20, = 20,
((pp1, 1, pp2, 2, pp3) = (25, 24, 15)3) = (25, 24, 15)((ww1, 1, ww2, 2, ww3)= (18, 15, 10)3)= (18, 15, 10)
Solution is...? Solution is...?
Sunny Cup 2004http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2109
§1 Greedy Algorithms
Given N items of sizes S1 , S2 , …, SN , such that 0 < Si 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; Si = 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8
0.8
0.2
B1
0.7
0.3
B2
0.1
0.5
0.4
B3
An Optimal Packing
NP Hard
7/19
§1 Greedy Algorithms
On-line Algorithms
Place an item before processing the next one, and can NOT change decision.
〖 Example 〗 Si = 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
§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
§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
§1 Greedy Algorithms
〖 Example 〗 Si = 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8
Next Fit First Fit Best Fit
〖 Example 〗 Si = 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.
610
11/19
Discussion 25:Discussion 25: Please show the Please show the results.results.
§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 〗 Si = 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.80.8, 0.7, 0.5, 0.4, 0.3, 0.2, 0.1
0.2
0.4
0.10.3
0.70.80.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.Simple greedy heuristics can give good results.
12/19
Research Project 12Bin 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 fromhttp://acm.zju.edu.cn/dsaa/
13/19
§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.
Note: Divide and conquer makes at least two recursive calls and the subproblems are disjoint.
14/19
§2 Divide and Conquer
1. Running Time of Divide and Conquer Algorithms
【 Theorem 】 The solution to the equation T(N) = a T(N / b) + (Nk logpN ), where a 1, b > 1, and p 0 is
kpk
kpk
ka
baNNO
baNNO
baNO
NT
b
if)log(
if)log(
if)(
)( 1
log
〖 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( N1.59 )
If conquer takes O( N2 ) then T = O( N2 ) .
15/19
§2 Divide and Conquer2. 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 ) / 2 N 2
Divide and Conquer – similar to the maximum subsequence sum problem〖 Example
〗 Sort according to x-coordinates and
divide;
Conquer by forminga solution from left,
right, and cross.
16/19
§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
§2 Divide and Conquer
- stripIf NumPointInStrip = , we have)( NO
/* points are all in the strip */for ( i=0; i<NumPointsInStrip; i++ ) for ( j=i+1; j<NumPointsInStrip; j++ ) if ( Dist( Pi , Pj ) < )
= Dist( Pi , Pj );
18/19
Discussion 26:Discussion 26: What is the worst case? What can we do then?What is the worst case? What can we do then?
Research Project 13Quoit 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 fromhttp://acm.zju.edu.cn/dsaa/
19/19