MIT 6.1220 — Lecture 1

Greedy and DP

Problem. (Interval Scheduling) Consider a set of nn requests R={(ai,bi)}i=1nR = \{(a_i, b_i)\}_{i = 1}^n, where:

Determine any largest possible subset of mutually compatible requests.

Example. If R={(1,2),(1,3),(1,4)}R = \{(1, 2), (1, 3), (1, 4)\}, then any one of {(1,2)}\{(1, 2)\}, {(1,3)}\{(1, 3)\}, or {(1,4)}\{(1, 4)\} is an optimal solution.

Solution. (Interval Scheduling) Greedily pick the compatible requests that end earliest.

Proof: We define the following:

If SGREEDY=SOPTS_{\textsc{GREEDY}} = S_{\textsc{OPT}}, then we win. Otherwise, let jj^* be the first index where SGREEDYS_{\textsc{GREEDY}} and SOPTS_{\textsc{OPT}} differ.

Claim 1. If j>1j^* > 1, then bj1ajb_{j^* - 1} \leq a_{j^*}'. This is because sj1=sj1s_{j^* - 1} = s_{j^* - 1}' is assumed compatible with sjs_{j^*}'.

Claim 2. We must have bjbjb_{j^*} \leq b_{j^*}'. This is because the greedy algorithm picked sjs_{j^*}, not sjs_{j^*}'.

Claim 3. By Claim 1 and Claim 2, the optimal solution can be adjusted like so:

{s1,,sj1,sj,,sm}{s1,,sj,sj+1,,sm}.\{s_1, \dots, s_{j^* - 1}, s_{j^*}', \dots, s_{m'}'\} \mapsto \{s_1, \dots, s_{j^*}, s_{j^* + 1}', \dots, s_{m'}'\}.

By repeatedly applying Claim 3, we see S={s1,,sm,sm+1,,sm}S = \{s_1, \dots, s_m, s_{m + 1}', \dots, s_{m'}'\} is an optimal solution. But m>mm' > m is impossible! If it were, the greedy algorithm would have added (sm+1,,sm)(s_{m + 1}', \dots, s_{m'}'), yet it didn't. So m=mm = m'.   \blacksquare

Problem. (Weighted Interval Scheduling) Consider a set of nn requests R={(ai,bi)}i=1nR = \{(a_i, b_i)\}_{i = 1}^n together with weights {wi}i=1n\{w_i\}_{i = 1}^n. Determine any heaviest possible subset of compatible weighted requests.

Solution. (Weighted Interval Scheduling) Just use DP. Sort the intervals by start time. Then:

W(R)=max{W({r2,,rn}),  w1+W({rjjt1})},W(R) = \max \{ W(\{r_2, \dots, r_n\}), \ \ w_1 + W(\{r_j \mid j \geq t_1\})\},

where tit_i is the least index j>ij > i for which rjr_j does not intersect rir_i. Repeat, recurse, blah blah.

Remark. For any ii, how can you find tit_i in O(logn)O(\log n) time?

How can you find all the tit_i in O(n)O(n) time, given the ordering of all interval endpoints?

Problem. (Online Interval Scheduling) Determine any largest possible subset of mutually compatible requests, given that requests come one at a time (in any order!) and must be accepted or rejected on arrival.

Unfortunately, an adversary can force the algorithm solver to always fail horribly. (Why?)

Problem. (Randomized Online Interval Scheduling) Say the set of requests RR is fixed. Now the algorithm receives these requests in a randomized order.

We'll figure out the solution to this updated problem during Probability Review on Sunday.