Lecture 7.2 - Greedy Algorithms-Interval Scheduling
Lecture 7.2 - Greedy Algorithms-Interval Scheduling
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Greedy Algorithms
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 2 / 10
Interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 3 / 10
Interval scheduling
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
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Greedy algorithm for interval scheduling
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 5 / 10
Correctness
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 6 / 10
Correctness
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
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 9 / 10
Implementation
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 9 / 10
Implementation
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 9 / 10
Implementation
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 9 / 10
Summary
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10
Summary
Madhavan Mukund Greedy Algorithms: Interval Scheduling PDSA using Python Week 7 10 / 10