p
in P
: O(n)
i,j
furthest from p
: \(O(n)\)d
\(\leftarrow |p,i| + |p,j|\)u
\(\leftarrow p\)u
.Runtime: \(O(n^2)\).
We consider all possible poles. The MST and diameter are unique given a pole. Clearly when we return the minimum over all of these trees we find the overall minimum diameter MST.
We want to do a process similar to above with all pair of points. In order to do so, we need an efficient way of calculating the diameter of a MST given poles \(i,j\), which we refer to as \(T_{ij}\).
Let \(P_i, P_j\) be the points connected to \(i,j\) respectively in \(T_{ij}\). The diameter of the MST \(d(T_{ij}) = \max_{p \in P_i} |ip| + \max_{p \in P_j} |jp| + |ij|\). We can view our task as essentially centering a circle at each pole such that all points are covered by a circle, and the sum of the radii is minimum. We denote our two radii \(L_r, R_r\) for the circle centered at \(i,j\) respectively.
Our radii are always going to be defined by an edge between our pole and points \(\in P-\{i,j\}\). This is evident -- we're seeking minimum distance so we're not going to go further than the last-covered point. Similarly, if we haven't covered a point we can't have a radius smaller than this edge.
As such, we could simply iterate through all possible pairs and see which gets us the best diameter. This takes us to \(O(n^4)\) however, so we have to try a bit harder. It turns out we can get away with checking far fewer candidate radii.
To see this, assume one circle is fixed and denote it L
. Let the points covered by L
be denoted \(P_L\). We don't need to consider any point \(\in P_L\) as a potential radius. Among all other candidates, we only need to find the point that is closest to j
while covering everything else. This point is simply the furthest non-\(P_L\) point from j
.
We can exploit this structure by making our L
as big as possible, and then shrinking it point by point. We start with our L
radius being the point furthest from i
, and then bringing it back point by point. Whenever we stop covering a point with L
, we make sure that it is covered by R
-- i.
Input: i
, j
, P
. i
and j
are poles. P
is a list of our pointset-{i
,j
} sorted in decreasing order WRT distance to i
.
x
Correctedness (of the sub-routine) follows from above -- we consider all radii which could potentially be diameter-defining, and simply return the minimum of all of these. Runtime is \(O(n)\).
We note that a poor implementation from this point can still result in \(O(n^3\log n\) runtime if we sort too frequently. As such, we sort after choosing each i
-- not after choosing a pair i,j
. That is:
P-{i}
sorted by decreasing distance t i
: \(O(n \log n)\)P-{i}
: O(n)
j
from \(P_s\): O(n)j
to \(P_s\): O(n)Runtime of \(O(n \left( n\log n + n \left( n\right) \right)) = O(n^3) \)
Correctedness is an immediate property of our arguments above. For all possible pair-of-points, we consider all potential radius-definiing points. By finding the minimum over all of these combinations we are guaranteed to solve our problem.