Greedy Algorithm
Construct a solution iteratively, via
sequence of myopic decisions, and hope
that everything works out in the end.
Prepared by Pawan K. Mishra
Template
Greedy Algorithm(A,n) Sorting / or do something to rank
Candidates = rank (A) Initialize solution
solution = ø Some greedy choice
for i = 1 to n Check feasibility before adding
c= findbest(Candidates) Remove the selected element
solution= solution U {c} Update the set candidates (optional)
candidates= candidates \ {c}
candidates= revaluate(candidates)
return solution
Prepared by Pawan K. Mishra
Features and Bugs of the Greedy paradigm
• Easy to come up with one or more greedy algorithms
• Easy to analyse the running time
• Hard to establish correctness
• Warning: Most greedy algorithms are not always
correct.
Prepared by Pawan K. Mishra
Exchange trick, to prove correctness (Worked
often, but not always)
Let A be the greedy algorithm that we are trying to prove correct, and
A(I) the output of A on some input I.
Let O be an optimal solution on input I that is not equal to A(I).
The goal in exchange argument is to show how to modify O to create a new solution O’ with the following
properties:
• 1. O’ is at least as good of solution as O (or equivalently O’ is also optimal), and
• 2. O’ is “more like” A(I) than O.
Prepared by Pawan K. Mishra
Exchange trick, to prove correctness (Worked
often, but not always)
Let A be the greedy algorithm that we are trying to prove correct, and
A(I) the output of A on some input I.
Let O be an optimal solution on input I that is not equal to A(I).
The goal in exchange argument is to show how to modify O to create a new solution O’ with the following
properties:
• 1. O’ is at least as good of solution as O (or equivalently O’ is also optimal), and
• 2. O’ is “more like” A(I) than O.
THIS IS THE CREATIVE PART - different for each algorithm/problem.
Prepared by Pawan K. Mishra
Two ways to proceed
First way. Contradiction
Theorem: The algorithm A solves the problem.
Proof by contradiction: Algorithm A doesn’t solve the problem.
• Hence, there must be some input I on which A does not produce an optimal solution. Let the output
produced by A be A(I).
Fact: Let O be the optimal solution that is most like A(I).
• If we can show how to modify O to create a new solution O’ with the following properties:
1. O’ is at least as good of solution as O (and hence O’ is also optimal), and
2. O’ is more like A(I) than O.
Then we have a contradiction to the choice of O. Thus, the theorem.
Prepared by Pawan K. Mishra
Two ways to proceed
Second way. Constructive way
Theorem: The algorithm A solves the problem.
Let I be an arbitrary instance. Let O be arbitrary optimal solution for I. Assume that we can show how to
modify O to create a new solution O’ with the following properties
1. O’ is at least as good of solution as O (and hence O’ is also optimal), and
2. O’ is more like A(I) than O.
Then consider the sequence O’; O’’; O’’’; O’’’’; ….
Each element of this sequence is optimal, and more like A(I) than the proceeding element. Hence, ultimately
this sequence must terminate with A(I).
Hence, A(I) is optimal.
Prepared by Pawan K. Mishra
P1: Job Scheduling
Time in the system for a job: Waiting time
All jobs arrived at time ZERO
IP: Set of n jobs={j1,j2,…jn} with processing time P(j1), P(j2), …P(jn), and a
single resource.
OP: Schedule jobs on one resource s.t. it minimizes the total waiting
time in the system.
Prepared by Pawan K. Mishra
Example
Job 1- 5 units , Job 2- 10 units, Job 3- 4 units
Order waiting time
[1,2,3]- 0+(5)+(5+10)= 20
[3,1,2]- 0+(4)+(4+5)=13
Prepared by Pawan K. Mishra
Example
Job 1- 5 units , Job 2- 10 units, Job 3- 4 units
Order waiting time
[1,2,3]- 0+(5)+(5+10)= 20
[3,1,2]- 0+(4)+(4+5)=13… this order is the optimal
Prepared by Pawan K. Mishra
Developing Intuition
Some arbitrary order of jobs…
Finish time of job 1 = t1
Finish time of job 2 = t1+ t2
Finish time of job 3 = t1+ t2+t3
.
.
.
Finish time of job n = t1+ t2+…+tn
Total Finishing Time = nt1+(n-1)t2+…+tn
Prepared by Pawan K. Mishra
Developing Intuition
Some arbitrary order of jobs…
Finish time of job 1 = t1
Finish time of job 2 = t1+ t2
Finish time of job 3 = t1+ t2+t3
Can you guess the greedy choice ?
.
.
.
Finish time of job n = t1+ t2+…+tn
Total Finishing Time = nt1+(n-1)t2+…+tn
Prepared by Pawan K. Mishra
Developing Intuition
Some arbitrary order of jobs…
Finish time of job 1 = t1
Finish time of job 2 = t1+ t2
Finish time of job 3 = t1+ t2+t3
Can you guess the greedy choice ?
.
. Shortest Job First
.
Finish time of job n = t1+ t2+…+tn
Total Finishing Time = nt1+(n-1)t2+…+tn
Prepared by Pawan K. Mishra
The only schedule that minimizes the total waiting time in
the system is one that schedules jobs in nondecreasing order
by processing time.
Use exchange trick…..with contradiction
Assume the statement is not true.
Fact: OPT is an optimal solution.
Note that in OPT, there exists two consecutive jobs X and Y, s.t. X is being served before Y, and P(x) > P(y).
Else OPT will be same schedule which follows the shortest job sequence order.
Now suppose we interchange X and Y in OPT, and else remain same. What happens?
Prepared by Pawan K. Mishra
The only schedule that minimizes the total waiting time in
the system is one that schedules jobs in nondecreasing order
by processing time.
Use exchange trick…..with contradiction
Assume the statement is not true.
Fact: OPT is an optimal solution.
Note that in OPT, there exists two consecutive jobs X and Y, s.t. X is being served before Y, and P(x) > P(y).
Else OPT will be same schedule which follows the shortest job sequence order.
Now suppose we interchange X and Y in OPT, and else remain same. What happens?
Order in OPT …………………. XY………..………..
New order OPT’ …………………. YX………………….
Prepared by Pawan K. Mishra
The only schedule that minimizes the total waiting time in
the system is one that schedules jobs in nondecreasing order
by processing time.
Use exchange trick…..with contradiction
Assume that it is not true.
Fact: OPT is an optimal solution.
Note that in OPT, there exists two consecutive jobs X and Y, s.t. X is being served before Y, and P(x) > P(y).
Else OPT will be same schedule which follows the shortest job sequence order.
Now suppose we interchange X and Y in OPT, and else remain same. What happens?
Order in OPT …………………. XY………..……….. No need to worry about these jobs (think why?? )
New order OPT’ …………………. YX………………….
Prepared by Pawan K. Mishra
The only schedule that minimizes the total waiting time in
the system is one that schedules jobs in nondecreasing order
by processing time..
Use exchange trick…..with contradiction
Assume that it is not true.
Fact: OPT is an optimal solution.
Note that in OPT, there exists two consecutive jobs X and Y, s.t. X is being served before Y, and P(x) > P(y).
Else OPT will be same schedule which follows the shortest job sequence order.
Now suppose we interchange X and Y in OPT, and else remain same. What happens?
Order in OPT …………………. XY………..……….. In OPT, to do Job Y, need to wait till Job X being served.
New order OPT’ …………………. YX…………………. In OPT’, to do Job X, need to wait till Job Y being served.
Prepared by Pawan K. Mishra
The only schedule that minimizes the total waiting time in
the system is one that schedules jobs in nondecreasing order
by processing time.
Use exchange trick…..with contradiction
Assume that it is not true.
Fact: OPT is an optimal solution.
Note that in OPT, there exists two consecutive jobs X and Y, s.t. X is being served before Y, and P(x) > P(y).
Else OPT will be same schedule which follows the shortest job sequence order.
Now suppose we interchange X and Y in OPT, and else remain same. What happens?
Order in OPT …………………. XY………..……….. In OPT, to do Job Y, need to wait till Job X being served.
New order OPT’ …………………. YX…………………. In OPT’, to do Job X, need to wait till Job Y being served.
Total wait Time (OPT’)= Total wait Time(OPT)-P(X)+P(Y)
Prepared by Pawan K. Mishra
The only schedule that minimizes the total waiting time in
the system is one that schedules jobs in nondecreasing order
by processing time.
Use exchange trick…..with contradiction
Assume that statement is not true.
Fact: OPT is an optimal solution.
Note that in OPT, there exists two consecutive jobs X and Y, s.t. X is being served before Y, and P(x) > P(y).
Else OPT will be same schedule which follows the shortest job sequence order.
Now suppose we interchange X and Y in OPT, and else remain same. What happens?
Order in OPT …………………. XY………..……….. In OPT, to do Job Y, need to wait till Job X being served.
New order OPT’ …………………. YX…………………. In OPT’, to do Job X, need to wait till Job Y being served.
Total wait Time (OPT’)= Total wait Time(OPT)-P(X)+P(Y) . Now, we know that P(x) > P(y).
Thus, Total wait Time (OPT’) < Total wait Time(OPT)
Prepared by Pawan K. Mishra CONTADICTION TO THE OPTIMALITY
Job Scheduling with m resources,
(minimize waiting time)
GENERALIZE THE PREVIOUS
IDEA TO SOLVE THIS
PROBLEM.
Prepared by Pawan K. Mishra
Flow to solve a problem
Given a problem P,
Try to come up with a greedy algorithm
a) then try to construct a counter example,
b) if you can construct, GOTO step 1.
c) if you cannot construct a counter example, ask your friend.
d) if your friend also fail to find one, ask me.
e) If we cannot construct one, we will post it in [Link] or [Link], and wait for few days..
f) if no one replies, then try to prove that your greedy algorithm works.
g) if we can prove, then alright.
h) if we are unable to prove, time to tune our greedy choice based on the difficulty we face during the proof.
TRY
Algorithm TUNE Proof LOOP_eternity
i) I will discuss this step in the last class.
Prepared by Pawan K. Mishra