Computer Algorithm Notes
Computer Algorithm Notes
Example 4.5 Let n = 4, (p1,P2, P3,PA) =(100, 10, 15, 27) and (d,d2, d, d)=
(2,1, 2,1). The feasible solutions and their values are:
CHAPTER 4. THE GREEDY
METHOD
228
feasible processing
value
solution sequence
110
1 (1, 2) 2, 1
115
2 (1, 3) 1, 3or 3, 1
127
(1, 4) 4, 1
3. 25
(2,3) 2, 3 42
(3, 4)
4, 3
100
1
(1)
2 10
(2)
3 15
8 (3)
4 27
9. (4)
only jobs I and4 are processed amd
Solution3is optimal. In this solution
in the order job follomod
the value is 127. These jobs must be processed
4
ntu
J. It is discarded as J = left with
= {1,2, 4} is not feasible. Hence, we are
the solution J = {1, 4} with value 127. This is the optimal solution for the
given problem instance. Theorem 4.5 just
proves that the greedy algorithm
described always obtains
an optimal solution to this sequencing problem.
Before whether
atternpting the proof, let us see how we
a given
can determine
J is a feasible
solution. One obvious way is to all possible
try out
permutations of the jobsin J and be pro
check whether the jobs in J can
cessed in any one of the
these permutations without violating do,
deadlines. For a given (sequences) to
permutation o = i, this is easy
Ifq > d
since the earliest i2, 31 k
time job ig; 1 <
then using o, at
least job ig will
q<k, will bee completed is q. 1
However.
if J= i, this
not be comnpleted by its deadline.
requires checking i!
t
processed in the
Then J is a feasible
order o without violating any
permutation
solution
deadline
i
JOB SEQUENCINGG WITH DEADLINES
4.5. 229
Proof:
violating
Clearly,, ifthe
any deadline,
jobs in
then J
JJ
is
can
a
feasible solution. So,
be
processed in the order o
without
we have only to
VIO Jis feasible, then o
represents a possible order in which
the
can be processed. If J is ffeasible, then there exists
jobs
ch that de, 24, lSqsk. Assume o'[Link] let
orryTk
bethe
a least index
ench that re #l Let ro la Clearly, b > a. In o we can interchange
and
represents
rh. Since
an order
dya2 drh the resulting permutation
in which the jobs can be processed without violating
osj,S2,**8
adeadline. Continuing in this way, d' can be transformed into o withont
violating any deadline. Hence, thetheorem is proved.
Theorem 4.4is true even if the jolbs have different processing times t,20
(see the exercises).
Theorem 4.5 The greedy method described above always obtains an opti
mal solution to the job sequencing problem.
and to
eduled in
that
'+1
in
[.+
i
1l
S,. If then we can interchange the job
t<t,
in S,with i. If no job
is scheduled in
(if any)
IE
(,+
l in
t
.
is also feasible.
[.+1. The resulting schedule can obtain
s moved to in S,. In this way,
we
then a similar transformation can be made to andJare
that all jobscommon
property which
schedules S and S with the
interval as a t l in S in
the
scheduled at the Bame time. Consider (if any) schedule
b be the job
the job a (defined above) is scheduled. Let Scheduling a rom
choice of a, Pa
P
the or job set
in S,in From a feasibleschelulehat ofJ and
JT in S
this interval,
and discarding
a) Clearly, J'
job b gives us
has a profit value
no less than
J,n)
Greedy Job(d, deadlioc
1 Algorithm by their
a set of jobs that can be comypleted
2 //0is
3
for i2 to n do
10 )
Algorithm 4.6 High-level description of job sequencing algorithm
can be processed by their due times. The selected jobs can be processedin
the order given by Theorem 4.4.
Now, us see how to represent the set J and how to carry out the test
let
<<:
be represented by a
dimensional array J1
k such that Jrl, 1 <r<k
are thejobs in
dJsdJ2) dJk] whether J U{i} is feasible, we bave
To test
just to insert i intopreserving the deadline
J that
dJrsr, ordering and then verify
1<rSk+1.
ofa fietitious job 0 with The insertion of i into J is simplified by the use
to be inserted at do) and
position q. then only
=0 = 0. Jo
Note also that if job
s
k are
changed after the
the positions of jobs Ja.
insertion. Hence, it necessary to
veri
only that these
the insertion.
jobs (andalso
The algorithm
job ) is
do not violate their deadlines following
JS that results
(Algorithm 4.7). The fron this discussion is funetion
such that Py2
d of job i P22
s at least 1.
algorithm assumes that
Pu: Further it the jobs are already
the
sorted
nland
deadline
by its Note that no assumes that finished
deadline.
the greedy Theoremn 4.6
job
proves that JS
with di can ever be <l o
strategy is a correct lmplementation
Theorem 4.6
Function JS is
nethod deseribed a
above correct
implementation of the Breedy-based
Proof: Since
greedy solution dt21,
As
the job with the beinline
the
the jobs largest willalways
are in P
s,
order of the P
noninereasing
SEQUENCING WITH DEADLINES
45. JOB 231
1 Algorithm JS(d, j. n)
/ dll> 1, 1Sisn are the deadlines, n >1. The jobs
are ordered such that p|l| p2)22 pln]. J
/ is the ith job in the optimal solution, 1<i<k.
I Also, at termination d|J||sdJi+1J), 1<i< k.
do) := Jo :=0; // Initialize.
Ji:=l;//Include job 1.
k=1;
10 for i := 2 to n do
{
12 // Consider jobs in nonincreasing order of pli]. Find
13 //position for i and check feasibility of insertion.
14 r:= k;
15 while ((d|J(r|] > dil) and (dJrl]l r)) do r :=r-1;
16 if ((dJ(rl] < di) and >
(di] r))then
17 {
18 //Insert i into J. +
19 for q := k to (r + 1) step -1 do Jg 1] :=Jgl:
20 Jr+1:=;k:=k+ 1;
21
}
22
23 return k;
24 }
of line
8in The for loop
Pi:
with largest method
I0 Algorithm 4.7 inchudes the job order required by the greedysolution
consicders the
remaining jobs in the in the
described of jobs already included then J is
232
+ di) > This is ve
dJa||4. wed
w 1 iff w.
at position in
i can be inserted loop if
on exit from the while
addition, ne
16(note r =
line from these observations. b
of JS follows
Thecorrectness to
in terms its of which
two possibleparameters t}
For JS there are
n, the number
of jobs, and s, thecomplexity
numnber
We can use of
(k-r) time to
n-1
i.
1 times. If
is K)! This loop is iterated sis
the for loop of line 10 f
i the number of jobs in the final soluti
the fnal value of k, that is; s.
addition to the space needed for d, JS needs amount of space for J. (s) tl
Note that the profit values are not needed by JS. It is sufficientto know that t
Pi 2 P+1, 1<i< n.
The computing time of JS can be reduced from O(n) to nearly O(n) E
by using the set union and find algorithms (see Section 2.5) and a
disjoint
feasible subset of jobs, then we can determine the processing times for each
of the jobs using the rule: if job i hasn't been assigned a processing time.
then assign it to the slot a-
1,al, where a is the largest integer r sle
that 1 <r<d, and the slot la - 1, al is free, This rule simply delays the
W
processing of job i as much as possible. Consequently,
when J is being built
up job by job, jobs already in J do not have to
be moved from their assigeu
slotsto accommodate the new job. If for
the new iob being considered t E
is no a as defined
above, then cannot be included in J. The proof of the
it
validity of this
statement is left as an
exercise.
Example 4.7 The trees defined by the p(i)'s for the first three iterations
n Example 4.6 are shown in Figure 4.5.