Professional Documents
Culture Documents
Greedy Approach
Greedy Approach
Greedy Approach
Aimal Rextin
Problem Definition
Given a set of n requests of time intervals
8 o clock 5 o clock
11 o clock 12 o clock
3 o clock 5 o clock
What are the requests that can be accepted such that the max
number of accepted requests are maximum?
Generic-Interval Schedualing(R)
1 R is the set of requests
2 A = ∅ // A will be set of selected requests
3 while there are requests left in R
4 Select a request r according a greedy strategy
5 A=A∪r
6 Remove all requests from R that overlap with r
Example
If proposition is Sum of two even numbers is odd
then a counter example will be
2 + 2 =Interval
Aimal Rextin
4 Scheduling: Another Greedy Algorithm
Greedy Strategies
Strategy 1
Select the request with the earliest starting time
Strategy 1
Select the request with the earliest starting time
The blue request will be selected by this scheme and the remaining
will be discarded
Strategy 1
Select the request with the earliest starting time
Strategy 1
Select the request with the earliest starting time
Strategy 2
Select the request with the shortest interval
Strategy 2
Select the request with the shortest interval
The blue request will be selected by this scheme and the remaining
will be discarded
Strategy 2
Select the request with the shortest interval
Strategy 2
Select the request with the shortest interval
Strategy 3
Select the request with the least conflicts
Strategy 3
Select the request with the least conflicts
Strategy 3
Select the request with the least conflicts
Strategy 3
Select the request with the least conflicts
Strategy 3
Select the request with the least conflicts
Strategy 3
Select the request with the least conflicts
i
c
a
k
j
g
h
e
b
d
f
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Greedy choice: earliest finish time
i
c
a
k
j
g
h
e
b
d
f
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Greedy choice: earliest finish time
Generic-Interval Schedualing(R)
1 R is the set of requests
2 A = ∅ // A will be set of selected requests
3 Sort R by finishing time
4 while there are requests left in R
5 Select a request r ∈ R with the smallest finishing time
6 A=A∪r
7 Remove all requests from R that overlap with r
Compatible Intervals
A set of intervals is compatible if none of them overlap with each
other.
Compatible Intervals
A set of intervals is compatible if none of them overlap with each
other.
Obvious Claim
The set A is always compatible during the execution of the
algorithm. Hence at the end we have a compatible set of intervals.
Compatible Intervals
A set of intervals is compatible if none of them overlap with each
other.
Obvious Claim
The set A is always compatible during the execution of the
algorithm. Hence at the end we have a compatible set of intervals.
Lemma
finish(ir ) ≤ finish(jr ) for any value of r, such that r ≤ k and r ≤ m
Induction Step
Assume that the proposition is true till r − 1 and we will try
to show that it is also true for r
x
ir −1 ir
jr −1 jr
Induction Step
Assume that the proposition is true till r − 1 and we will try
to show that it is also true for r
We will prove it by contradiction and assume the opposite
x
ir −1 ir
jr −1 jr
Induction Step
Assume that the proposition is true till r − 1 and we will try
to show that it is also true for r
We will prove it by contradiction and assume the opposite
An example of such a situation is shown below
x
ir −1 ir
jr −1 jr
Induction Step
Assume that the proposition is true till r − 1 and we will try
to show that it is also true for r
We will prove it by contradiction and assume the opposite
An example of such a situation is shown below
x
ir −1 ir
jr −1 jr
Induction Step
Assume that the proposition is true till r − 1 and we will try
to show that it is also true for r
We will prove it by contradiction and assume the opposite
An example of such a situation is shown below
x
ir −1 ir
jr −1 jr
This situation is not possible
Induction Step
Assume that the proposition is true till r − 1 and we will try
to show that it is also true for r
We will prove it by contradiction and assume the opposite
An example of such a situation is shown below
x
ir −1 ir
jr −1 jr
This situation is not possible
Why?
Induction Step
Assume that the proposition is true till r − 1 and we will try
to show that it is also true for r
We will prove it by contradiction and assume the opposite
An example of such a situation is shown below
x
ir −1 ir
jr −1 jr
This situation is not possible
Why?
Because the greedy algorithm will pick jr not ir as its finish
time is less
Induction Step
Assume that the proposition is true till r − 1 and we will try
to show that it is also true for r
We will prove it by contradiction and assume the opposite
An example of such a situation is shown below
x
ir −1 ir
jr −1 jr
This situation is not possible
Why?
Because the greedy algorithm will pick jr not ir as its finish
time is less
Hence we have a contradiction
Aimal Rextin Interval Scheduling: Another Greedy Algorithm
Final Proof
Final Theorem
The greedy algorithm is optimal.
Final Theorem
The greedy algorithm is optimal.
Proof
Prove contradiction:Greedy Algorithm is not optimal
Final Theorem
The greedy algorithm is optimal.
Proof
Prove contradiction:Greedy Algorithm is not optimal
An optimal set O has more requests
Final Theorem
The greedy algorithm is optimal.
Proof
Prove contradiction:Greedy Algorithm is not optimal
An optimal set O has more requests
But we know from Lemma that finish(ik ) ≤ finish(jk ), where
k is the last interval selected by the greedy algorithm
ik
jk jk+1 x
Final Theorem
The greedy algorithm is optimal.
Proof
Prove contradiction:Greedy Algorithm is not optimal
An optimal set O has more requests
But we know from Lemma that finish(ik ) ≤ finish(jk ), where
k is the last interval selected by the greedy algorithm
But this is not possible as the greedy algorithm will not stop
at ik it will conintue to include jk+1 · · ·
ik
jk jk+1 x
Interval Schedualing(R)
1 R is the set of requests
2 A = ∅ // A will be set of selected requests
3 sort R according to finishing time (smallest to largest)
4 A = A ∪ R[1]
5 picked=R[1] // picked denotes the latest interval that is selected
6 for i=2 to n
7 if R[i ].start ≥ picked.finish // making sure there is no overlap
8 A = A ∪ R[i ]
9 picked=R[i]
Hence it is obvious that running time O(n log n)