Recitation 8

Recitation notes for the week of October 15, 2025.

Proof (of Correctness) Idea

First note that the algorithm above places $\left\lceil \frac{d_{\max}}{100}\right\rceil$ many cell towers.

To complete the proof, we claim that any valid placement of towers must place at least $\left\lceil \frac{d_{\max}}{100}\right\rceil$ cell towers. For the sake of contradiction assume that we are able to come up with a valid placement with \[\le \left\lceil \frac{d_{\max}}{100}\right\rceil -1 < \frac{d_{\max}}{100}\] cell towers. Since each cell tower can cover $100$ miles, this means that the total number of miles covered will be \[<100\cdot \left(\frac{d_{\max}}{100}\right) = d_{\max},\] which means at least $1$ mile from the first house and the last house does not have cell coverage, which means that our placement was not valid.

Runtime analysis

We will assume the following, which you need to prove on your own:

Exercise 1

\[d_{\max}\le 99n.\]

This implies that the while runs for at most $99n/100\le n$ times. Each iteration takes $O(1)$ time: so overall the loop takes $O(n)$ time. Since computing $d_{\max}$ takes $O(n)$ time, the overall runtime is $O(n)$, as desired.

Part (b)

We would like to point out two things:

  1. The algorithm above for part (a) does not work for part (b). The basic idea is that the algorithm from part (a) can place a cell tower at a mile where there is no house but for part (b) the cell towers have to be at a house. See the sample input/output above for a concrete example.
  2. Think how you could modify the algorithm for part (a) could be modified to handle the extra restriction that the cell towers have to placed next to a house.

    Hint

    Think of a greedy way to decide on which houses to pick for cell tower installation. It might help to forget about the scheduling algorithms we have seen in class and just think of a greedy algorithm from "scratch." Then try to analyze the algorithm using similar arguments to one we have seen in class.

    Note

    The proof idea for part (a) does not work for part (b): you are encouraged to use greedy stays ahead technique we have seen in class to prove correctness of a greedy algorithm.

Greedy Stays Ahead

We briefly (re)state the greedy stays ahead technique in a generic way (and illustrate it with the proof of the greedy algorithm for the interval scheduling problem that we saw in class).

Roughly speaking the greedy stays ahead technique has the following steps:

  1. Sync up partial greedy and optimal solution Greedy algorithms almost always build up the final solution iteratively. In other words, at the end of iteration $\ell$, the algorithm has computed a `partial' solution $\texttt{Greedy}(\ell)$. The trick is to somehow `order' the optimal solution so that one can have a corresponding `partial' optimal solution $\texttt{Opt}(\ell)$ as well.
  2. Define a 'progress' measure For each iteration $\ell$ (as defined above), we pick a 'progress' measure $P(\cdot)$ so that $P\left(\texttt{Greedy}(\ell)\right)$ and $P\left(\texttt{Opt}(\ell)\right)$ are well-defined. This is the step that is probably most vague but that is by necessity-- the definition of the progress measure has to be specific to the problem so it is hard to specify generically.
  3. Argue 'Greedy Stays Ahead' (as per the progress measure) Now prove that for every iteration $\ell$, we have \[P\left(\texttt{Greedy}(\ell)\right) \ge P\left(\texttt{Opt}(\ell)\right).\]
  4. Connect 'Greedy Stays Ahead' to showing the greedy algorithm is optimal. Finally, prove that with the specific interpretation of 'Greedy Stays Ahead' as above, at the end of the run of the greedy algorithm the final output is 'just as good' as the optimal solution. Note that the progress measure $P(\cdot)$ need not be the objective function that the greedy algorithm is trying to minimize (indeed this will not be true in most cases) so you will still have to argue another step to show that greedy stays ahead indeed implies that the greedy solution is no worse than the optimal one.

Next, we illustrate the above framework with the proof we saw in class that showed that the greedy algorithm for the interval scheduling problem was optimal:

  1. Sync up partial greedy and optimal solution We did this by ordering the intervals in both the greedy and optimal solution in non-decreasing order of their finish time. Specifically, recall that we defined the greedy solution as \[S^*=\left\{i_1,\dots,i_k\right\}\] and an optimal solution as \[\mathcal O=\left\{j_1,\dots,j_m\right\},\] where $f(i_1)\le \cdots \le f_(i_k)$ and $f(j_1)\le \cdots f(j_m)$. With this understanding, the 'partial' solutions are defined as follows for every $1\le \ell\le k$: \[\texttt{Greedy}(\ell)=\left\{i_1,\dots,i_\ell\right\}\] and \[\texttt{Opt}(\ell)=\left\{j_1,\dots,j_\ell\right\}.\]
  2. Define a 'progress' measure. We define the progress measure as the (negation of) finish time of the last element in the partial solution, i.e. \[P\left(\texttt{Greedy}(\ell)\right) = -f\left(i_\ell\right)\] and \[P\left(\texttt{Opt}(\ell)\right) = - f\left(j_\ell\right).\] Two comments:
    • If the "$-$" sign in the definition of $P(\cdot)$ is throwing you off-- it's for a syntactic reason (and it'll become clear in the next step why this makes sense).
    • Note that the progress measure above has nothing (directly) to do with the objective function of the problem (which is to maximize the number of intervals picked).
  3. Argue 'Greedy Stays Ahead' (as per the progress measure). In class, we proved a lemma that stays that for every $1\le \ell\le k$, we have \[f\left(i_\ell\right)\le f\left(j_\ell\right).\] Note that this implies that for our progress measure (and this is why we had the negative sign when defining the progress measure), for every $1\le \ell\le k$, \[P\left(\texttt{Greedy}(\ell)\right) \ge P\left(\texttt{Opt}(\ell)\right),\] as desired.
  4. Connect 'Greedy Stays Ahead' to showing the greedy algorithm is optimal. For this step, we have a separate proof that assumed that if the 'Greedy Stays Ahead' lemma is true then $\left|S^*\right|\ge \left|\mathcal{O}\right|$.

A warning

Please note that while the above 4-step framework for the 'Greedy Stays Ahead' techniques works in many cases, there is no guarantee that this framework will always work, i.e. your mileage might vary.

HW 4, Question 2

Speedy Skate

There are $n$ skaters, where each skater $i$ has a triple $(s_i,p_i,d_i)$. $s_i,p_i$ and $d_i$ are the times needed by skater $i$ for their skate, post workout and drive parts of the competition. They have to do the three parts in order: skate first, then post workout and then drive back to their hotel/home. Only one skater can skate in the rink at a time but there is no such restriction on post-workout and driving (i.e. if possible all skaters can post-workout/drive in "parallel"). Your goal is to schedule the skaters: i.e. the order in which the skaters start their skate part so that the completion time of the last skater is minimized. I.e. you should come up with a schedule such that all skaters finish their competition as soon as possible.

Sample Input/Outputs

Part (a)

Here we have to argue that there is always an optimal solution with no idle time: i.e. there should be no gap between when a skater finishes their skate and when the next skater starts their skate.

Proof Idea

Assume that skater $i$ has start time $st_i$. Note we want to argue that for every $i$, $st_{i+1}=st_i+s_i$. If this condition is satisfied for all $i$, then we are done. So assume not. We will now show how to convert this into another set of start times $st'_i$ for every skater $i$ that has no idle time and the resulting schedule is still optimal. Repeat the following until there exists a skater $i$ such that $st_{i+1}> st_i+s_i$; update $st_{i+1}=st_i+s_i$. Once this procedure stops, let $st'_i$ be the final start time of skater $i$.

Let $f_i=st_i+s_i+p_i+d_i$ be the original finish time for skater $i$ and let $f'_i=st'_i+s_i+p_i+d_i$ be the new finish time. Then one can argue by induction that (and this is your exercise):

Exercise 2

For every skater $i$, $f'_i\le f_i$.

Given the above note that $\max_i f'_i\le \max_i f_i$. Since the former is the new completion time and the latter is the old completion time, this means that the new schedule has a smaller completion time. Since we started with an optimal solution, this means that the new schedule is also optimal (and indeed we have $\max_i f'_i = \max_i f_i$).

Part (b)

Note

Part (a) actually then shows that we only need to figure out the order of the skaters: once that is set, the start times are automatically fixed. Hence for part (b), one just needs to output the ordering among the skaters.

In this part you have to come up with an efficient (i.e. polynomial time) algorithm to come up with an optimal schedule for the skaters that minimizes the completion time. We repeat the following note and hint from the problem statement:

Note

As has been mentioned earlier, in many real life problems, not all parameters are equally important. Also sometimes it might make sense to "combine" two parameters into one. Keep these in mind when tackling this problem.

Hint

In the solution that we have in mind, the analysis of the algorithm's correctness follows the exchange argument. Of course that does not mean it cannot be proven in another way.

As a reminder, the exchange argument is done in care package on the minimizing the max lateness problem or see (4.9) in the textbook, below is a quick recap.

Exchange Argument for Minimizing Max Lateness

Main Idea

If we have a schedule with no idle time and with an inversion, then we can rid of an inversion in such a way that will not increase the max lateness of the schedule.

Recall

In (4.8) in the book, we showed that schedules with no inversions and no idle time have the same max lateness.

Given the schedule below where the deadline of $j$ comes before the deadline of $i$ (the top schedule has an inversion):

Let us consider what happens when we go from top schedule to the bottom schedule. Obviously $j$ will finish earlier so it will not increase the max lateness, but what about $i$? Now $i$ is super late.

However, note that $i$’s lateness is $f(j) - d(i)$ where $f(j)$ is the finish time of $j$ in the top schedule. Now note that \[f(j) - d(i) < f(j) - d(j),\] so the swap does not increase maximum lateness.

An Example

Here is an explicit example showing the above proof argument: \[d(a) = 2~~~~ t(a) = 1\] \[d(b) = 5~~~~ t(b) = 6\] \[d(c) = 8~~~~ t(c) = 3\]

Now consider this schedule with inversions: \[__a__|____c____|______b_______\] And another schedule without inversions: \[__a__|______b_______|____c____\]

The first schedule has max lateness of $5$ while the second schedule has max lateness of $2$.

NOTE

Examples do not suffice as proofs. But they are good for understanding purposes.