# Lecture 21: Interval Scheduling

$\def\Sopt{ {S_{\mathrm{opt}}} }$

## Greedy Algorithms

So far: focused on (greedy) graph algorithms

1. Finding Eulerian circuits
• greedily collect new edges
2. BFS (unweighted SSSP)
• greedily explore nearest edges
3. Dijkstra (weighted SSSP)
• greedily find closest vertex
4. Prim (MST)
• greedily add lightest outgoing edge
5. Kruskal (MST)
• greedily add lightest edge that doesn’t create a cycle

## Today

Interval Scheduling

1. Interval scheduling problem & motivation
2. Greedy algorithm for interval scheduling
3. Analysis technique: algorithm stays ahead

## Interval Scheduling

Motivation.

• start time $s$
• end time $t > s$
• Only one request can be serviced at a time
• cannot service two “overlapping” requests

Goal. Find a set of requests to service that are:

1. feasible: no two requests overlap
2. optimal: as many requests as possible are serviced, subject to feasibility

## Example and Geometric View

View requests as intervals: $r = [s_r, t_r]$

## A Meta-Strategy

To find a feasible set of requests to service:

1. Pick a request $r$ to service (according to some criteria)
2. Remove all requests $r’$ that overlap with $r$
3. Repeat 1 and 2 until all requests have been chosen or removed

Observe. This will always give a feasible set of requests.

Question. How to select a request at each step?

## Natural Greedy Selection Strategies

Select request…

1. …with earliest start time
2. …with shortest duration
3. …that overlaps the fewest other requests

Exercise. Show that none of these strategies are guaranteed to find a maximum feasible collection of requests.

## Another Strategy

• select request with earliest deadline

## EDF in Pseudocode

  # R a collection of requests, r = (s, t)
EDF(R):
sort R in ascending order of end time t
curMax <- -infinity
S <- empty collection
for each request r = (s, t) in R do
| if s > curMax then
| | add r to S
| | curMax <- t
return S


## Correctness I

Clear: EDF returns a feasible collection of requests

Claim. EDF returns a maximum feasible collection of requests.

• $S$ is set of requests selected by EDF
• $\Sopt$ is optimal (maximum) feasible collection

To show:

• each request chosen in $S$ is at least as good as corresponding request in $\Sopt$

## Correctness II

Notation:

• $S = \{(s_1, t_1), (s_2, t_2), \ldots, (s_k, t_k)\}$
• $t_1 < s_2, t_2 < s_3, \ldots$
• $k$ is number of requests selected by EDF
• $\Sopt = \{(s_1’, t_1’), (s_2’, t_2’), \ldots, (s_\ell’, t_\ell’)\}$
• $t_1’ < s_2’, t_2’ < s_3’, \ldots$
• $\ell$ is number of requests selected by $\Sopt$

Want to show: $k \geq \ell$

## Correctness III

Sub-claim. For all $i = 1, 2, \ldots, k$, $t_i \leq t_i’$.

Proof. Induction on $i$.

## Correctness IV

Sub-claim $\implies$ Claim. Argue by contradiction.

## Running Time?

  # R a collection of requests, r = (s, t)
EDF(R):
sort R in ascending order of end time t
curMax <- -infinity
S <- empty collection
for each request r = (s, t) in R do
| if s > curMax then
| | add r to S
| | curMax <- t
return S


## Conclusion

1. Earliest deadline first strategy finds a maximum feasible collection of requests.
2. “Algorithm stays ahead” analysis establishes correctness
3. Running time of EDF is $O(n \log n)$

## Next Time

Start dynamic programming!