Lecture 13
Lecture 13
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
𝑇 𝑂 = 𝑛 − 𝑎 + 1 𝑠𝑏 + 𝑛 − 𝑏 + 1 𝑠𝑎 + (𝑛 − 𝑘 + 1) 𝑠𝑘
𝑘=1
𝑘≠𝑎,𝑏.
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.