Problem

Input:towns $t_{1},t_{2},...,t_{n}∈R$ and $k$ is a number.

Output:find the $k$ stations $s_{1},s_{2},...,s_{k}∈R$ such that we minimize the farthest town to the closest station, for any given $t_{i}$.

Intuitively, if we pick any $t_{i}$, we should not travel any further to get to a center $s_{i}$ than any other $t_{j}$.

## Setup

Solution space: all the possible tuples $(s_{1},s_{2},...,s_{k})∈R_{k}$

Objective function $f$:

$f(s_{1},s_{2},...,s_{k})=i∈[1..n]max {j∈[1..k]min {∣t_{i}−s_{j}∣}}$This says that for each town $1..n$, we pick the closest station among $1..k$ and consider the distance between them. We want to minimize $f$ for all towns.

## High-level question

We can ask “where to place the first station?” or (almost equivalently) “which towns should the first station serve?”

- Question is good because we decompose our problem: after placing the first station, we recurse and consider the remaining $k−1$ stations
- In addition, we consider the remaining set of towns that aren’t covered by a station yet
- The first phrasing of the question is not that good because it implies an
*infinite*number of answers (because it’s a real number)

The possible answers are $i∈1..n$, where $t_{i}$ signifies the last town that $s_{1}$ should cover. For each $i$, the payment of towns $t_{1},...,t_{i}$ is given by

$max distance=2∣t_{1}−t_{i}∣ $This is because we want to minimize the distance from the furthest town to station $s_{1}$. We have two towns on either end, so the median is the best.

## $G$-function

The $G$-function in this problem is given by

$G(1..n,k)=minimumf-value for servicing townst_{1},…,t_{n}usingk-centers$We don’t know how much the remaining towns will pay. If we choose to use the first station to cover up to town $t_{i}$, then

$G(1..n,k)=i∈[1..n]min {max(2∣t_{1}−t_{i}∣ ,G(i+1..n,k−1))}$Remember that the output only considers the maximum distance that any given town has to travel to its station, so we take the max of the first station, and however much the remaining towns have to pay for the remaining $k−1$ stations.

### Generalization: “proof of correctness”

We can generalize $G$ for any station last town $i$ and number of stations $j$:

$G(i,j) =minf-value of placingjstations to serve townst_{i},…,t_{n}=l∈[i..n]min {max(2t_{i}−t_{l} ,G(l+1..n,j−1))} $where $l$ is the last town covered by station $s_{i}$ from $t_{i}$ to $t_{l}$.

We have two base cases so that $G$ doesn’t infinitely recurse. Consider if we have no towns left. Then we would be recursing on town $n+1$, which doesn’t exist, so $G(n+1,j)=0$ for any $j$.

Furthermore, if we run out of stations, then $j=0$, in which case we’ve failed to cover all $n$ towns, and so this should not be considered. Then $G(i,0)=∞$ for any town $i$.

### Caching subproblems

We use a 2D array to store any previously calculated values for $(i,j)$ in $G$.

## Runtime analysis

There are $n$ towns and $k$ problems, so there are $nk$ subproblems. For each subproblem, we need to recurse through $i..n$ towns to consider each as the last town for that particular station. When $i=1$, this is $n$ towns, so total runtime is given by

$(# of subproblems)⋅(time per subproblem)=O(n_{2}k)$