Greedy Algorithms: Interval Scheduling
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Programming, Data Structures and Algorithms using Python
Week 7
Greedy Algorithms
Need to make a sequence of choices to
achieve a global optimum
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Need to make a sequence of choices to
achieve a global optimum
At each stage, make the next choice
based on some local criterion
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Need to make a sequence of choices to
achieve a global optimum
At each stage, make the next choice
based on some local criterion
Never go back and revise an earlier
decision
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Need to make a sequence of choices to
achieve a global optimum
At each stage, make the next choice
based on some local criterion
Never go back and revise an earlier
decision
Drastically reduces space to search for
solutions
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Need to make a sequence of choices to
achieve a global optimum
At each stage, make the next choice
based on some local criterion
Never go back and revise an earlier
decision
Drastically reduces space to search for
solutions
How to prove that local choices achieve
global optimum?
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Need to make a sequence of choices to Examples
achieve a global optimum
Dijkstra’s algorithm
At each stage, make the next choice
Local rule: freeze the distance to
based on some local criterion
nearest unvisited vertex
Never go back and revise an earlier Global optimum: distance assigned to
decision each vertex is shortest distance from
source
Drastically reduces space to search for
solutions
How to prove that local choices achieve
global optimum?
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Need to make a sequence of choices to Examples
achieve a global optimum
Prim’s algorithm
At each stage, make the next choice
Local rule: add to the spanning tree
based on some local criterion
nearest non-tree vertex
Never go back and revise an earlier Global optimum: final spanning tree is
decision minimum cost spanning tree
Drastically reduces space to search for
solutions
How to prove that local choices achieve
global optimum?
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Need to make a sequence of choices to Examples
achieve a global optimum
Kruskal’s algorithm
At each stage, make the next choice
Local rule: add to the current set of
based on some local criterion
edges the smallest edge that does not
Never go back and revise an earlier form a cycle
decision Global optimum: final spanning tree is
minimum cost spanning tree
Drastically reduces space to search for
solutions
How to prove that local choices achieve
global optimum?
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Interval scheduling
IIT Madras has a special video
classroom for delivering online lectures
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
IIT Madras has a special video
classroom for delivering online lectures
Different teachers want to book the
classroom
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
IIT Madras has a special video
classroom for delivering online lectures
Different teachers want to book the
classroom
Slot for instructor i starts at s(i) and
finishes at f (i)
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
IIT Madras has a special video
classroom for delivering online lectures
Different teachers want to book the
classroom
Slot for instructor i starts at s(i) and
finishes at f (i)
Slots may overlap, so not all bookings
can be honoured
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
IIT Madras has a special video
classroom for delivering online lectures
Different teachers want to book the
classroom
Slot for instructor i starts at s(i) and
finishes at f (i)
Slots may overlap, so not all bookings
can be honoured
Choose a subset of bookings to
maximize the number of teachers who
get to use the room
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
IIT Madras has a special video Greedy approach
classroom for delivering online lectures
Pick the next booking to allot based on
Different teachers want to book the a local strategy
classroom
Slot for instructor i starts at s(i) and
finishes at f (i)
Slots may overlap, so not all bookings
can be honoured
Choose a subset of bookings to
maximize the number of teachers who
get to use the room
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
IIT Madras has a special video Greedy approach
classroom for delivering online lectures
Pick the next booking to allot based on
Different teachers want to book the a local strategy
classroom
Remove all bookings that overlap with
Slot for instructor i starts at s(i) and the chosen slot
finishes at f (i)
Slots may overlap, so not all bookings
can be honoured
Choose a subset of bookings to
maximize the number of teachers who
get to use the room
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
IIT Madras has a special video Greedy approach
classroom for delivering online lectures
Pick the next booking to allot based on
Different teachers want to book the a local strategy
classroom
Remove all bookings that overlap with
Slot for instructor i starts at s(i) and the chosen slot
finishes at f (i)
Argue that this sequence of bookings
Slots may overlap, so not all bookings will maximize the number of teachers
can be honoured who get to use the room
Choose a subset of bookings to
maximize the number of teachers who
get to use the room
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
IIT Madras has a special video Greedy approach
classroom for delivering online lectures
Pick the next booking to allot based on
Different teachers want to book the a local strategy
classroom
Remove all bookings that overlap with
Slot for instructor i starts at s(i) and the chosen slot
finishes at f (i)
Argue that this sequence of bookings
Slots may overlap, so not all bookings will maximize the number of teachers
can be honoured who get to use the room
Choose a subset of bookings to What is a sound local strategy?
maximize the number of teachers who
get to use the room
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Greedy strategies for interval scheduling
Strategy 1
Choose the booking whose starting time
is earliest
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 4 / 10
Greedy strategies for interval scheduling
Strategy 1
Choose the booking whose starting time Counterexample
is earliest
Bookings
Time
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 4 / 10
Greedy strategies for interval scheduling
Strategy 1
Choose the booking whose starting time
is earliest
Strategy 2
Choose the booking spanning the
shortest interval
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 4 / 10
Greedy strategies for interval scheduling
Strategy 1
Choose the booking whose starting time Counterexample
is earliest
Bookings
Strategy 2
Choose the booking spanning the Time
shortest interval
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 4 / 10
Greedy strategies for interval scheduling
Strategy 1
Choose the booking whose starting time
is earliest
Strategy 2
Choose the booking spanning the
shortest interval
Strategy 3
Choose the booking that overlaps with
minimum number of other bookings
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 4 / 10
Greedy strategies for interval scheduling
Strategy 1
Choose the booking whose starting time Counterexample
is earliest
Strategy 2 Bookings
Choose the booking spanning the
Time
shortest interval
Strategy 3
Choose the booking that overlaps with
minimum number of other bookings
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 4 / 10
Greedy strategies for interval scheduling
Strategy 1
Choose the booking whose starting time
is earliest
Strategy 2
Choose the booking spanning the
shortest interval
Strategy 3
Choose the booking that overlaps with
minimum number of other bookings
Strategy 4
Choose the booking whose finish time is
the earliest
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 4 / 10
Greedy strategies for interval scheduling
Strategy 1
Choose the booking whose starting time
is earliest
Strategy 2
Choose the booking spanning the
shortest interval
Strategy 3
Choose the booking that overlaps with
minimum number of other bookings
Strategy 4
Choose the booking whose finish time is
the earliest
Counterexample? Proof of
correctness?
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 4 / 10
Greedy algorithm for interval scheduling
B is the set of bookings
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
B is the set of bookings
A is the set of accepted bookings
Initially, A is empty
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
B is the set of bookings
A is the set of accepted bookings
Initially, A is empty
While B is not empty
Pick b ∈ B with earliest finishing time
Add b to A
Remove from B all bookings that
overlap with b
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
B is the set of bookings The algorithm in action
A is the set of accepted bookings 6 8
Initially, A is empty
1 3 5 9
While B is not empty
Pick b ∈ B with earliest finishing time
2 4 7
Add b to A
Remove from B all bookings that
overlap with b
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
B is the set of bookings The algorithm in action
A is the set of accepted bookings 6 8
Initially, A is empty
1 3 5 9
While B is not empty
Pick b ∈ B with earliest finishing time
2 4 7
Add b to A
Remove from B all bookings that
overlap with b
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
B is the set of bookings The algorithm in action
A is the set of accepted bookings 6 8
Initially, A is empty
1 3 5 9
While B is not empty
Pick b ∈ B with earliest finishing time
2 4 7
Add b to A
Remove from B all bookings that
overlap with b
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
B is the set of bookings The algorithm in action
A is the set of accepted bookings 6 8
Initially, A is empty
1 3 5 9
While B is not empty
Pick b ∈ B with earliest finishing time
2 4 7
Add b to A
Remove from B all bookings that
overlap with b
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
B is the set of bookings The algorithm in action
A is the set of accepted bookings 6 8
Initially, A is empty
1 3 5 9
While B is not empty
Pick b ∈ B with earliest finishing time
2 4 7
Add b to A
Remove from B all bookings that
overlap with b
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Correctness
Our algorithm produces a solution A
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
Our algorithm produces a solution A
Let O be an optimal set of accepted
bookings
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
Our algorithm produces a solution A
Let O be an optimal set of accepted
bookings
A and O need not be identical
Could have multiple allocations of the
same size
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
Our algorithm produces a solution A
Let O be an optimal set of accepted
bookings
A and O need not be identical
Could have multiple allocations of the
same size
Just show that |A| = |O|
Both sets of bookings are of the same
size
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
Our algorithm produces a solution A Let A = {i1 , i2 , . . . , ik }
Let O be an optimal set of accepted Assume sorted
bookings f (i1 ) ≤ s(i2 ), f (i2 ) ≤ s(i3 ), . . .
A and O need not be identical
Could have multiple allocations of the
same size
Just show that |A| = |O|
Both sets of bookings are of the same
size
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
Our algorithm produces a solution A Let A = {i1 , i2 , . . . , ik }
Let O be an optimal set of accepted Assume sorted
bookings f (i1 ) ≤ s(i2 ), f (i2 ) ≤ s(i3 ), . . .
A and O need not be identical Let O = {j1 , j2 , . . . , jm }
Could have multiple allocations of the Also sorted
same size
f (j1 ) ≤ s(j2 ), f (j2 ) ≤ s(j3 ), . . .
Just show that |A| = |O|
Both sets of bookings are of the same
size
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
Our algorithm produces a solution A Let A = {i1 , i2 , . . . , ik }
Let O be an optimal set of accepted Assume sorted
bookings f (i1 ) ≤ s(i2 ), f (i2 ) ≤ s(i3 ), . . .
A and O need not be identical Let O = {j1 , j2 , . . . , jm }
Could have multiple allocations of the Also sorted
same size
f (j1 ) ≤ s(j2 ), f (j2 ) ≤ s(j3 ), . . .
Just show that |A| = |O|
Our goal is to show that k = m
Both sets of bookings are of the same
size
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Greedy algorithm stays ahead
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 7 / 10
Greedy algorithm stays ahead
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Claim For each ` ≤ k, f (i` ) ≤ f (j` )
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 7 / 10
Greedy algorithm stays ahead
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Claim For each ` ≤ k, f (i` ) ≤ f (j` )
Proof By induction of `
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 7 / 10
Greedy algorithm stays ahead
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Claim For each ` ≤ k, f (i` ) ≤ f (j` )
Proof By induction of `
Base case: ` = 1
By greedy strategy, i1 has earliest overall finish time
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 7 / 10
Greedy algorithm stays ahead
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Claim For each ` ≤ k, f (i` ) ≤ f (j` )
Proof By induction of `
Base case: ` = 1
By greedy strategy, i1 has earliest overall finish time
Induction step: Assume f (i`−1 ) ≤ f (j`−1 )
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 7 / 10
Greedy algorithm stays ahead
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Claim For each ` ≤ k, f (i` ) ≤ f (j` )
Proof By induction of `
Base case: ` = 1
By greedy strategy, i1 has earliest overall finish time
Induction step: Assume f (i`−1 ) ≤ f (j`−1 )
f (i`−1 ) ≤ f (j`−1 ) ≤ s(j` )
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 7 / 10
Greedy algorithm stays ahead
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Claim For each ` ≤ k, f (i` ) ≤ f (j` )
Proof By induction of `
Base case: ` = 1
By greedy strategy, i1 has earliest overall finish time
Induction step: Assume f (i`−1 ) ≤ f (j`−1 )
f (i`−1 ) ≤ f (j`−1 ) ≤ s(j` )
If f (j` ) < f (i` ), greedy strategy would pick j`
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 7 / 10
Greedy algorithm stays ahead
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Claim For each ` ≤ k, f (i` ) ≤ f (j` )
Proof By induction of `
Base case: ` = 1
By greedy strategy, i1 has earliest overall finish time
Induction step: Assume f (i`−1 ) ≤ f (j`−1 )
f (i`−1 ) ≤ f (j`−1 ) ≤ s(j` )
If f (j` ) < f (i` ), greedy strategy would pick j`
We must have f (i` ) ≤ f (j` )
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 7 / 10
Greedy strategy is optimal
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 8 / 10
Greedy strategy is optimal
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Suppose m > k
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 8 / 10
Greedy strategy is optimal
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Suppose m > k
We know f (ik ) ≤ f (jk )
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 8 / 10
Greedy strategy is optimal
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Suppose m > k
We know f (ik ) ≤ f (jk )
Greedy strategy stops when B is empty
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 8 / 10
Greedy strategy is optimal
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Suppose m > k
We know f (ik ) ≤ f (jk )
Greedy strategy stops when B is empty
Consder request jk+1
Since f (ik ) ≤ f (jk ) ≤ s(jk+1 ), this request is compatible with A
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 8 / 10
Greedy strategy is optimal
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Suppose m > k
We know f (ik ) ≤ f (jk )
Greedy strategy stops when B is empty
Consder request jk+1
Since f (ik ) ≤ f (jk ) ≤ s(jk+1 ), this request is compatible with A
jk+1 must still be in B
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 8 / 10
Greedy strategy is optimal
A = {i1 , i2 , . . . , ik }
O = {j1 , j2 , . . . , jm }
Suppose m > k
We know f (ik ) ≤ f (jk )
Greedy strategy stops when B is empty
Consder request jk+1
Since f (ik ) ≤ f (jk ) ≤ s(jk+1 ), this request is compatible with A
jk+1 must still be in B
B is not empty after choosing A, contradiction!
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 8 / 10
Implementation
Initially, sort n bookings by finish time — O(n log n)
Renumber bookings to reflect this sorted order
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 9 / 10
Implementation
Initially, sort n bookings by finish time — O(n log n)
Renumber bookings to reflect this sorted order
Record start and finish times in array/dictionary
S[i] is starting time of request i
F[i] is finish time of request i
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 9 / 10
Implementation
Initially, sort n bookings by finish time — O(n log n)
Renumber bookings to reflect this sorted order
Record start and finish times in array/dictionary
S[i] is starting time of request i
F[i] is finish time of request i
Add first booking to A
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 9 / 10
Implementation
Initially, sort n bookings by finish time — O(n log n)
Renumber bookings to reflect this sorted order
Record start and finish times in array/dictionary
S[i] is starting time of request i
F[i] is finish time of request i
Add first booking to A
In general, after adding booking j to A, Find the smallest r with S[r] > F[j]
Single scan, O(n) overall
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 9 / 10
Summary
A greedy algorithm makes a sequence of locally optimal choices to achieve a global
optimum
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
A greedy algorithm makes a sequence of locally optimal choices to achieve a global
optimum
The algorithm never goes back and reconsiders on a choice
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
A greedy algorithm makes a sequence of locally optimal choices to achieve a global
optimum
The algorithm never goes back and reconsiders on a choice
Drastically reduces space to search for solutions, but need to show prove that local
strategy is globally optimal
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
A greedy algorithm makes a sequence of locally optimal choices to achieve a global
optimum
The algorithm never goes back and reconsiders on a choice
Drastically reduces space to search for solutions, but need to show prove that local
strategy is globally optimal
Interval scheduling — many “natural” greedy strategies
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
A greedy algorithm makes a sequence of locally optimal choices to achieve a global
optimum
The algorithm never goes back and reconsiders on a choice
Drastically reduces space to search for solutions, but need to show prove that local
strategy is globally optimal
Interval scheduling — many “natural” greedy strategies
Most of them are wrong!
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
A greedy algorithm makes a sequence of locally optimal choices to achieve a global
optimum
The algorithm never goes back and reconsiders on a choice
Drastically reduces space to search for solutions, but need to show prove that local
strategy is globally optimal
Interval scheduling — many “natural” greedy strategies
Most of them are wrong!
Correct strategy needs a proof
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
A greedy algorithm makes a sequence of locally optimal choices to achieve a global
optimum
The algorithm never goes back and reconsiders on a choice
Drastically reduces space to search for solutions, but need to show prove that local
strategy is globally optimal
Interval scheduling — many “natural” greedy strategies
Most of them are wrong!
Correct strategy needs a proof
One way is to show that greedy solution “stays ahead”, step by step, of any optimal
solutions
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10