Greedy Approach

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Interval Scheduling: Another Greedy Algorithm

Aimal Rextin

Department of Computer Science

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Interval Scheduling

Problem Definition
Given a set of n requests of time intervals

(s1 , f1 ), (s2 , f2 ) · · · (sn , fn )

si is the staring time of the request


fi is the finish time of the request
find the max number of requests that are do not overlap with each
other

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Example

Requests are (8, 5), (11, 12), (3, 5)

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Example

Requests are (8, 5), (11, 12), (3, 5)

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Example

Requests are (8, 5), (11, 12), (3, 5)

8 o clock 5 o clock

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Example

Requests are (8, 5), (11, 12), (3, 5)

11 o clock 12 o clock

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Example

Requests are (8, 5), (11, 12), (3, 5)

3 o clock 5 o clock

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Example

Requests are (8, 5), (11, 12), (3, 5)

What are the requests that can be accepted such that the max
number of accepted requests are maximum?

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Example

Requests are (8, 5), (11, 12), (3, 5)

These would be (8, 5), (11, 12), (3, 5)

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Generic Greedy Algorithm

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategy

There can be many greedy strategies.


Not all will work
Infact for most problems there is no greedy strategy that will
always work
We will be sure a strategy will work only if we can give a
logical argument (proof)
But we will be sure that a strategy will not work even if can
give one counter example

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategy

There can be many greedy strategies.


Not all will work
Infact for most problems there is no greedy strategy that will
always work
We will be sure a strategy will work only if we can give a
logical argument (proof)
But we will be sure that a strategy will not work even if can
give one counter example
Definition
A counter example is a specific example in which the proposition
is false

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategy

There can be many greedy strategies.


Not all will work
Infact for most problems there is no greedy strategy that will
always work
We will be sure a strategy will work only if we can give a
logical argument (proof)
But we will be sure that a strategy will not work even if can
give one counter example
Definition
A counter example is a specific example in which the proposition
is false

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 1
Select the request with the earliest starting time

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 1
Select the request with the earliest starting time

But the optimal possible requests to select are these


Hence it is a wrong approach as we have a counter example

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 2
Select the request with the shortest interval

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 2
Select the request with the shortest interval

The blue request will be selected by this scheme and the remaining
will be discarded

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 2
Select the request with the shortest interval

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 2
Select the request with the shortest interval

But the optimal possible requests to select are these


Hence it is a wrong approach as we have a counter example

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 3
Select the request with the least conflicts

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 3
Select the request with the least conflicts

The blue request will be selected by this scheme

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 3
Select the request with the least conflicts

The overlapping requests will be removed

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 3
Select the request with the least conflicts

Now the green request will be selected

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 3
Select the request with the least conflicts

overlapping ones will be removed

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Strategies

Strategy 3
Select the request with the least conflicts

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Interval Scheduling: The Correct Greedy Choice

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Interval Scheduling: The Correct Greedy Choice

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Correct Greedy Algorithm

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Optimality of the Algorithm

We now need to prove that the algorithm is optimal.

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Optimality of the Algorithm

We now need to prove that the algorithm is optimal.


It will be optimal if and only if the algorithm always returns
the maximum possible number of requests
It is obvious that the computed intervals must be compatible.

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Optimality of the Algorithm

We now need to prove that the algorithm is optimal.


It will be optimal if and only if the algorithm always returns
the maximum possible number of requests
It is obvious that the computed intervals must be compatible.

Compatible Intervals
A set of intervals is compatible if none of them overlap with each
other.

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Optimality of the Algorithm

We now need to prove that the algorithm is optimal.


It will be optimal if and only if the algorithm always returns
the maximum possible number of requests
It is obvious that the computed intervals must be compatible.

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.

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Optimality of the Algorithm

We now need to prove that the algorithm is optimal.


It will be optimal if and only if the algorithm always returns
the maximum possible number of requests
It is obvious that the computed intervals must be compatible.

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.

Why is the claim true?

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Proof of Optimality: Preliminaries

Let O be one of the possibly multiple optimal solution

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Proof of Optimality: Preliminaries

Let O be one of the possibly multiple optimal solution


Strategy
Since there can be many optimal solutions. It will be difficult to
show that A = O
We will instead prove the correctness of the algorithm by showing
that |A| = |O|

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Notations

1 Let i1 , i2 , · · · , ik be the set intervals in A

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Notations

1 Let i1 , i2 , · · · , ik be the set intervals in A


2 Let j1 , j2 , · · · , jm be the set of intervals in O

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Notations

1 Let i1 , i2 , · · · , ik be the set intervals in A


2 Let j1 , j2 , · · · , jm be the set of intervals in O
3 Hence |A| = k and |O| = m

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Notations

1 Let i1 , i2 , · · · , ik be the set intervals in A


2 Let j1 , j2 , · · · , jm be the set of intervals in O
3 Hence |A| = k and |O| = m
4 Assume that both A and O are chronologically ordered. This
is possible because both A and O are a set of compatible
events.

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Notations

1 Let i1 , i2 , · · · , ik be the set intervals in A


2 Let j1 , j2 , · · · , jm be the set of intervals in O
3 Hence |A| = k and |O| = m
4 Assume that both A and O are chronologically ordered. This
is possible because both A and O are a set of compatible
events.
5 Note that at this stage we dont know if k = m. That is the
objective of our proof.

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Greedy Always Finishes Early

Lemma
finish(ir ) ≤ finish(jr ) for any value of r, such that r ≤ k and r ≤ m

We prove this by induction


Base Case
r =1
The greedy algorithm will pick the interval with the earliest
finish time (by definition)
Hence the proposition is true for the base case

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Lemma Continued

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Lemma Continued

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Lemma Continued

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Lemma Continued

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Lemma Continued

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Lemma Continued

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?

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Lemma Continued

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Lemma Continued

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.

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Final Proof

Final Theorem
The greedy algorithm is optimal.

Proof
Prove contradiction:Greedy Algorithm is not optimal

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Final Proof

Final Theorem
The greedy algorithm is optimal.

Proof
Prove contradiction:Greedy Algorithm is not optimal
An optimal set O has more requests

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Final Proof

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Final Proof

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

Aimal Rextin Interval Scheduling: Another Greedy Algorithm


Concrete Algorithm

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)

Aimal Rextin Interval Scheduling: Another Greedy Algorithm

You might also like