0% found this document useful (0 votes)
11 views

Lecture 13

1) The document discusses greedy algorithms for minimizing time in a scheduling system and for scheduling jobs with deadlines. 2) For minimizing time, the greedy strategy is to serve the customer with the least service time first. This strategy can be proven to be optimal. 3) For scheduling with deadlines, the greedy algorithm selects the job with highest profit at each step, as long as the set of selected jobs remains feasible. The greedy algorithm is proven to always find an optimal solution.

Uploaded by

f20201862
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture 13

1) The document discusses greedy algorithms for minimizing time in a scheduling system and for scheduling jobs with deadlines. 2) For minimizing time, the greedy strategy is to serve the customer with the least service time first. This strategy can be proven to be optimal. 3) For scheduling with deadlines, the greedy algorithm selects the job with highest profit at each step, as long as the set of selected jobs remains feasible. The greedy algorithm is proven to always find an optimal solution.

Uploaded by

f20201862
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

BITS, PILANI – K. K.

BIRLA GOA CAMPUS

Design & Analysis of Algorithms


(CS F364)

Lecture No. 13

1
Minimum Spanning Tree (MST)
Optimal substructure for MST

Prims Algorithm
Choose some v ∈ V and let S = {v}
Let T = Ø
While S ≠ V
Choose a least-cost edge e with one
endpoint in S and one endpoint in V – S
Add e to T
Add both endpoints of e to S
Minimum Spanning Tree (MST)
Greedy-choice property:

Exercise:
Shortest Path Problem
Minimize time in the system
Problem Statement:
• A single server with N customers to serve
• Customer i will take time ti, 1≤ i ≤ N to be served.
• Goal: Minimize average time that a customer spends in
the system.
where time in system for customer i = total waiting time + ti
• Since N is fixed, we try to minimize time spend by all
customers to reach our goal

Minimize T = σ𝑁
𝑖=1 (time in system for customer i)
Minimize time in the system
Example:
Assume that we have 3 jobs with t1 = 5, t2 = 3, t3 = 7

Order Total Time In System


1, 2, 3 5 + (5 + 3) + (5 + 3 + 7) = 28
1, 3, 2 5 + (5 + 7) + (5 + 7 + 3) = 32
2, 3, 1 3 + (3 + 7) + (3 + 7 + 5) = 28
2, 1, 3 3 + (3 + 5) + (3 + 5 + 7) = 23 optimal
3, 1, 2 7 + (7 + 5) + (7 + 5 + 3) = 34
3, 2, 1 7 + (7 + 3) + (7 + 3 + 5) = 32
Minimize time in the system
Brute Force Solution:
Time Complexity: N!
Which is exponential!!
Optimal Substructure Property:
Exercise
Minimize time in the system
Greedy Strategy
At each step, add to the end of the schedule, the
customer requiring the least service time
among those who remain.
So serve least time consuming customer first.
Minimize time in the system
Observe
Let P = p1p2…..pN be any permutation of the
integers 1 to N and let si = 𝒕𝒑𝒊
If customers are served in the order corresponding
to P, then
Total time passed in the system by all the
customers is
T(P) = s1 + (s1 + s2) + (s1+ s2 + s3) + .....
= ns1 + (n – 1)s2 + (n – 2)s3 + ……
= σ𝑵
𝒌=𝟏(𝒏 − 𝒌 + 𝟏)𝒔𝒌
Minimize time in the system
Theorem:
Greedy strategy is optimal.
Proof:
Suppose strategy P does not arrange the customers in
increasing service time.
Then we can find two integers a & b with a < b and sa > sb
i.e, the a-th customer is served before the b-th
customer even though the former needs more service
time than the latter.
Minimize time in the system
Now, we exchange the position of these two customers to
obtain a new order of service O
Then
𝑛

𝑇 𝑂 = 𝑛 − 𝑎 + 1 𝑠𝑏 + 𝑛 − 𝑏 + 1 𝑠𝑎 + ෍ (𝑛 − 𝑘 + 1) 𝑠𝑘
𝑘=1
𝑘≠𝑎,𝑏.

And T(P) – T(O)


= (n – a + 1)(sa – sb) + (n – b + 1)(sb – sa)
= (b – a)(sa – sb)
>0
i.e., the new schedule O is better than the old schedule P
Scheduling with deadlines
Problem Statement:
• We have set of n jobs to execute
• Each job takes unit time to execute
• At any time T=1,2,.. we can execute exactly one
job
• Job i earns us profit gi > 0 if and only if it is
executed no later than time di
• Goal: Find a schedule that maximizes the profit
Scheduling with deadlines
Example,
i 1 2 3 4
gi 50 10 15 30
di 2 1 2 1

Possible schedules & corresponding profits are


Sequence Profit Sequence Profit
1 50 2, 1 60
2 10 2, 3 25
3 15 3, 1 65
4 30 4, 1 80 optimal
1, 3 65 4, 3 45
The sequence 2, 3, 1 is not considered, as job 1 would be executed at time t=3,
after its deadline
Greedy Algorithm
Definition
A set of jobs is feasible if there exits at least one
sequence (called feasible sequence) that allows all the
jobs in the set to be executed no later than their
respective deadlines.
Greedy Algorithm
Beginning with empty schedule, at each step,
Add the job with the highest profit among those not
yet considered,
provided that the chosen set of jobs remains feasible.
Greedy Algorithm
For our example,
We first choose job 1.
Next we choose job 4, the set {1, 4} is feasible because
it can be executed in the order 4, 1
Next we try the set {1, 3, 4} which is not feasible & so
job 3 is rejected
Finally we try set {1, 2, 4} which is not feasible & so job
2 is rejected
Our optimal solution is the set of jobs {1, 4}
Greedy Proof
Theorem:
The greedy algorithm always finds an optimal solution.
Proof:
Suppose greedy algorithm returns set of jobs I
And suppose the set J is optimal.
Let SI and SJ be the corresponding feasible sequences
Claim:
By rearranging the jobs in SI and those in SJ we can obtain
two feasible sequences 𝑺′𝑰 and 𝑺′𝑱 (which may include gaps)
such that every job common to I and J is scheduled at the
same time in both the sequences.
Example
For example, Suppose
p y q x r SI

r s t p u v q w SJ
Example
After reorganization

x y p r q 𝑺′𝑰
gap

u s t p r v q w 𝑺′𝑱
Proof of the claim
Proof of the claim:
Suppose that some job a occurs in both the feasible
sequences SI and SJ where it is scheduled at times
tI and tJ respectively.
Case 1: If tI = tJ there is nothing to prove
Case 2: If tI < tJ
Note, since sequence is SJ feasible, it follows that the
deadline for job a is no earlier than tJ
Modify sequence SI as follows:
Case i : Suppose there is a gap in SI at time tJ move job
a from time tI into the gap at tJ
Proof of the claim
Case ii :
Suppose there is some job b scheduled in SI at time tJ ,
then exchange jobs a and b in SI
The resulting sequence is still feasible, since in either
case job a will be executed by its deadline, and in the
second case job b is moved to an earlier time and so
can be executed.
So job a is executed at the same time tJ in both the
modified sequences SI and SJ
Case 3: If tI > tJ
Similar argument works, except in this case SJ is
modified
Greedy Proof
Once job a has been treated in this way, we never need
to move it again.
Therefore, if SI and SJ have m jobs in common, after at
most m modifications of either SI or SJ we can ensure
that all the jobs common to I and J are scheduled at the
same time in both sequences.
𝑺′𝑰 and 𝑺′𝑱 be the resulting sequences.
Suppose there is a time when the job scheduled in 𝑺′𝑰
is different from that scheduled in 𝑺′𝑱
Greedy Proof
Case1:
If some job a is scheduled in 𝑺′𝑰 opposite a gap in 𝑺′𝑱 .
Then a does not belong to J & the set J ∪ {a} is feasible.
So we have a feasible solution profitable than J.
This is impossible since J is optimal by assumption.
Case2:
If some job b is scheduled in 𝑺′𝑱 opposite a gap in 𝑺′𝑰 .
Then b does not belong to I & the set I ∪ {b} is feasible.
So the greedy algorithm would have included b in I.
This is impossible since it did not do so.
Greedy Proof
Case 3:
Some job a is scheduled in 𝑺′ opposite a different job b in 𝑺′𝑱
𝑰
In this case a does not appear in J and b does not appear in I.
Case i : If ga > gb
Then we could replace a for b in J and get a better solution.
This is impossible because J is optimal.
Case ii : If ga < gb
The greedy algorithm would have chosen b before considering a
since (I \ {a})∪ {b} would be feasible.
This is impossible because the algorithm did not include b in I.
Case iii : The only remaining possibility is ga= gb
The total profit from I is therefore equal to the profit from the
optimal set J, and so I is optimal to.

You might also like