Ch10.1 Greedy Algorithms

Download Report

Transcript Ch10.1 Greedy Algorithms

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
§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.
1. A Simple Scheduling Problem
 The Single Processor Case
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 */
2/7
§1 Greedy Algorithms
job j1
time 15
〖Example〗
j2
8
j1
Schedule 1
j3
3
j2
0
15
j4
10
j3
23
j4
26
36
Tavg = ( 15 + 23 + 26 + 36 ) / 4 = 25
Best scheduling is to make
j3
j2
j4
j1
Schedule 2
{
t
}
nondecreasing.
ik
0
3
11
21
36
Tavg = ( 3 + 11 + 21 + 36 ) / 4 = 17.75
In general:
ji1
0
ji2
ti1
……
ji3
ti1 + ti2
ti1 + ti2 + ti3
N
N
N
k 1
k 1
k 1
Total CostC   ( N  k  1)tik  ( N  1) tik   k  tik
3/7
§1 Greedy Algorithms
 The Multiprocessor Case – N jobs on P processors
〖Example〗 job
time
P=3
j1
3
j2
5
j3
6
An Optimal Solution
j1
j2
j3
0
j4
j4
10
j1
j2
j3
j8
j6
3 56
j9
13 16 20
j6
14
j7
15
28
34
40
0
3 56
j5
j4
j7
j6
j8
14 15 20
30 34
An Optimal Solution
j5
j8
j6
j1
0
4/7
35
NP Hard
j9
j3
j4
9
14 16 19
j9
20
j9
 Minimizing the Final Completion Time
j2
j8
18
Another Optimal Solution
j7
j5
j5
11
j7
34
38
§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 .
If assignment
there is an aof
0, then
we can
i =jobs
A schedule is an
to time
intervals on machines
schedule all the jobs with aj  0 first.
such that
Then we insert the jobs with ai = 0
 jij must be processed
by P
andschedule
the processing
time is tij .
to the front
ofj the
list
in arbitrary
 No machine processes
more thanorder.
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 .
3 4 8 10
〖Example〗 Given N = 4, J  

6
2
9
15


5/7
T1234 = 4?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
schedules 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 );
}
T = O( N log N )
3 4 8 10
〖Example〗 Given N = 4, J  

6
2
9
15


Sorting  { b2 a1 a2 b1 a3 b3 a4 b4 }
6/7
T1342 = 3?8
J1
J3
J4
J2
§1 Greedy Algorithms
Bonus Problem 1
The Best ACT
Due: Monday, January 15th, 2006 at 10:00pm
Detailed requirements can be downloaded from
http://10.71.45.99/list.asp?boardid=47
Courseware Download
Hint: Carefully consider the range of integers to
avoid RunTime Error.
7/7