Lecture 21: Interval Scheduling
$
\def\Sopt{ {S_{\mathrm{opt}}} }
$
COSC 311 Algorithms, Fall 2022
Greedy Algorithms
So far: focused on (greedy) graph algorithms
- Finding Eulerian circuits
- greedily collect new edges
- BFS (unweighted SSSP)
- greedily explore nearest edges
- Dijkstra (weighted SSSP)
- greedily find closest vertex
- Prim (MST)
- greedily add lightest outgoing edge
- Kruskal (MST)
- greedily add lightest edge that doesn’t create a cycle
Today
Interval Scheduling
- Interval scheduling problem & motivation
- Greedy algorithm for interval scheduling
- Analysis technique: algorithm stays ahead
Interval Scheduling
Motivation.
- Timed access to finite resource, e.g., CPU time
- Receive requests consisting of
- 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:
-
feasible: no two requests overlap
-
optimal: as many requests as possible are serviced, subject to feasibility
Example and Geometric View
View requests as intervals: $r = [s_r, t_r]$
Natural Greedy Selection Strategies
Select request…
- …with earliest start time
- …with shortest duration
- …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
Earliest deadline first:
- 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.
Proof strategy. “EDF stays ahead”
- $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
- Earliest deadline first strategy finds a maximum feasible collection of requests.
- “Algorithm stays ahead” analysis establishes correctness
- Running time of EDF is $O(n \log n)$
Next Time
Start dynamic programming!